Line data Source code
1 : /* Copyright (C) 2026 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 : /* 24 : * output buffer and 'stream' layered on top of a compression codec 25 : */ 26 : 27 : #ifndef INCLUDED_STREAM 28 : #define INCLUDED_STREAM 29 : 30 : #include "lib/code_annotation.h" 31 : #include "lib/file/archive/codec.h" 32 : #include "lib/status.h" 33 : 34 : #include <cstddef> 35 : #include <memory> 36 : 37 : // note: this is similar in function to std::vector, but we don't need 38 : // iterators etc. and would prefer to avoid initializing each byte. 39 : class OutputBufferManager 40 : { 41 : public: 42 : OutputBufferManager(); 43 : 44 : void Reset(); 45 : void SetBuffer(std::uint8_t* buffer, size_t size); 46 : 47 : /** 48 : * allocate a new output buffer. 49 : * 50 : * @param size [bytes] to allocate. 51 : * 52 : * notes: 53 : * - if a buffer had previously been allocated and is large enough, 54 : * it is reused (this reduces the number of allocations). 55 : * - this class manages the lifetime of the buffer. 56 : **/ 57 : void AllocateBuffer(size_t size); 58 : 59 0 : std::uint8_t* Buffer() const 60 : { 61 0 : return m_buffer; 62 : } 63 : 64 0 : size_t Size() const 65 : { 66 0 : return m_size; 67 : } 68 : 69 : private: 70 : bool IsAllowableBuffer(std::uint8_t* buffer, size_t size); 71 : 72 : std::uint8_t* m_buffer; 73 : size_t m_size; 74 : 75 : std::shared_ptr<std::uint8_t> m_mem; 76 : // size of m_mem. allows reusing previously allocated buffers 77 : // (user-specified buffers can't be reused because we have no control 78 : // over their lifetime) 79 : size_t m_capacity; 80 : }; 81 : 82 : 83 : class Stream 84 : { 85 : public: 86 : Stream(const PICodec& codec); 87 : 88 : void SetOutputBuffer(std::uint8_t* out, size_t outSize); 89 : 90 : void AllocateOutputBuffer(size_t outSizeMax); 91 : 92 : /** 93 : * 'feed' the codec with a data block. 94 : **/ 95 : Status Feed(const std::uint8_t* in, size_t inSize); 96 : 97 : Status Finish(); 98 : 99 0 : size_t OutSize() const 100 : { 101 0 : return m_outProduced; 102 : } 103 : 104 0 : std::uint32_t Checksum() const 105 : { 106 0 : return m_checksum; 107 : } 108 : 109 : private: 110 : PICodec m_codec; 111 : OutputBufferManager m_outputBufferManager; 112 : 113 : size_t m_inConsumed; 114 : size_t m_outProduced; 115 : std::uint32_t m_checksum; 116 : }; 117 : 118 : // avoids the need for std::bind (not supported on all compilers) and boost::bind (can't be 119 : // used at work) 120 : struct StreamFeeder 121 : { 122 : NONCOPYABLE(StreamFeeder); 123 : public: 124 0 : StreamFeeder(Stream& stream) 125 0 : : stream(stream) 126 : { 127 : } 128 : 129 0 : Status operator()(const std::uint8_t* data, size_t size) const 130 : { 131 0 : return stream.Feed(data, size); 132 : } 133 : 134 : private: 135 : Stream& stream; 136 : }; 137 : 138 : #endif // #ifndef INCLUDED_STREAM