LCOV - code coverage report
Current view: top level - source/lib/file/archive - stream.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 48 0.0 %
Date: 2026-12-03 01:19:39 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2025 Wildfire Games.
       2             :  *
       3             :  * Permission is hereby granted, free of charge, to any person obtaining
       4             :  * a copy of this software and associated documentation files (the
       5             :  * "Software"), to deal in the Software without restriction, including
       6             :  * without limitation the rights to use, copy, modify, merge, publish,
       7             :  * distribute, sublicense, and/or sell copies of the Software, and to
       8             :  * permit persons to whom the Software is furnished to do so, subject to
       9             :  * the following conditions:
      10             :  *
      11             :  * The above copyright notice and this permission notice shall be included
      12             :  * in all copies or substantial portions of the Software.
      13             :  *
      14             :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
      15             :  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
      16             :  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
      17             :  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
      18             :  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
      19             :  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
      20             :  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
      21             :  */
      22             : 
      23             : #include "precompiled.h"
      24             : 
      25             : #include "stream.h"
      26             : 
      27             : #include "lib/allocators/shared_ptr.h"
      28             : #include "lib/debug.h"
      29             : #include "lib/file/archive/codec.h"
      30             : //#include "lib/timer.h"
      31             : 
      32             : //TIMER_ADD_CLIENT(tc_stream);
      33             : 
      34             : 
      35           0 : OutputBufferManager::OutputBufferManager()
      36             : {
      37           0 :     Reset();
      38           0 : }
      39             : 
      40           0 : void OutputBufferManager::Reset()
      41             : {
      42           0 :     m_buffer = 0;
      43           0 :     m_size = 0;
      44           0 :     m_capacity = 0;
      45           0 : }
      46             : 
      47           0 : void OutputBufferManager::SetBuffer(std::uint8_t* buffer, size_t size)
      48             : {
      49           0 :     ENSURE(IsAllowableBuffer(buffer, size));
      50             : 
      51           0 :     m_buffer = buffer;
      52           0 :     m_size = size;
      53           0 : }
      54             : 
      55           0 : void OutputBufferManager::AllocateBuffer(size_t size)
      56             : {
      57             :     // notes:
      58             :     // - this implementation allows reusing previous buffers if they
      59             :     //   are big enough, which reduces the number of allocations.
      60             :     // - no further attempts to reduce allocations (e.g. by doubling
      61             :     //   the current size) are made; this strategy is enough.
      62             :     // - Pool etc. cannot be used because files may be huge (larger
      63             :     //   than the address space of 32-bit systems).
      64             : 
      65             :     // no buffer or the previous one wasn't big enough: reallocate
      66           0 :     if(!m_mem || m_capacity < size)
      67             :     {
      68           0 :         AllocateAligned(m_mem, size);
      69           0 :         m_capacity = size;
      70             :     }
      71             : 
      72           0 :     SetBuffer(m_mem.get(), size);
      73           0 : }
      74             : 
      75           0 : bool OutputBufferManager::IsAllowableBuffer(std::uint8_t* buffer, size_t size)
      76             : {
      77             :     // none yet established
      78           0 :     if(m_buffer == 0 && m_size == 0)
      79             :         return true;
      80             : 
      81             :     // same as last time (happens with temp buffers)
      82           0 :     if(m_buffer == buffer && m_size == size)
      83             :         return true;
      84             : 
      85             :     // located after the last buffer (note: not necessarily after
      86             :     // the entire buffer; a lack of input can cause the output buffer
      87             :     // to only partially be used before the next call.)
      88           0 :     if((unsigned)(buffer - m_buffer) <= m_size)
      89           0 :         return true;
      90             : 
      91             :     return false;
      92             : }
      93             : 
      94             : 
      95             : //-----------------------------------------------------------------------------
      96             : 
      97             : 
      98           0 : Stream::Stream(const PICodec& codec)
      99           0 :     : m_codec(codec)
     100           0 :     , m_inConsumed(0), m_outProduced(0)
     101             : {
     102           0 : }
     103             : 
     104             : 
     105           0 : void Stream::AllocateOutputBuffer(size_t outSizeMax)
     106             : {
     107           0 :     m_outputBufferManager.AllocateBuffer(outSizeMax);
     108           0 : }
     109             : 
     110             : 
     111           0 : void Stream::SetOutputBuffer(std::uint8_t* out, size_t outSize)
     112             : {
     113           0 :     m_outputBufferManager.SetBuffer(out, outSize);
     114           0 : }
     115             : 
     116             : 
     117           0 : Status Stream::Feed(const std::uint8_t* in, size_t inSize)
     118             : {
     119           0 :     if(m_outProduced == m_outputBufferManager.Size())   // output buffer full; must not call Process
     120             :         return INFO::ALL_COMPLETE;
     121             : 
     122           0 :     size_t inConsumed, outProduced;
     123           0 :     std::uint8_t* const out = m_outputBufferManager.Buffer() + m_outProduced;
     124           0 :     const size_t outSize = m_outputBufferManager.Size() - m_outProduced;
     125           0 :     RETURN_STATUS_IF_ERR(m_codec->Process(in, inSize, out, outSize, inConsumed, outProduced));
     126             : 
     127           0 :     m_inConsumed += inConsumed;
     128           0 :     m_outProduced += outProduced;
     129           0 :     return INFO::OK;
     130             : }
     131             : 
     132             : 
     133           0 : Status Stream::Finish()
     134             : {
     135           0 :     size_t outProduced;
     136           0 :     RETURN_STATUS_IF_ERR(m_codec->Finish(m_checksum, outProduced));
     137           0 :     m_outProduced += outProduced;
     138           0 :     return INFO::OK;
     139             : }

Generated by: LCOV version 1.16