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 : * this layer allows for other compression methods/libraries 25 : * besides ZLib. it also simplifies the interface for user code and 26 : * does error checking, etc. 27 : */ 28 : 29 : #ifndef INCLUDED_CODEC 30 : #define INCLUDED_CODEC 31 : 32 : #include "lib/status.h" 33 : 34 : #include <cstddef> 35 : #include <memory> 36 : 37 : #define CODEC_COMPUTE_CHECKSUM 1 38 : 39 0 : struct ICodec 40 : { 41 : public: 42 : /** 43 : * note: the implementation should not check whether any data remains - 44 : * codecs are sometimes destroyed without completing a transfer. 45 : **/ 46 : virtual ~ICodec(); 47 : 48 : /** 49 : * @return an upper bound on the output size for the given amount of input. 50 : * this is used when allocating a single buffer for the whole operation. 51 : **/ 52 : virtual size_t MaxOutputSize(size_t inSize) const = 0; 53 : 54 : /** 55 : * clear all previous state and prepare for reuse. 56 : * 57 : * this is as if the object were destroyed and re-created, but more 58 : * efficient since it avoids reallocating a considerable amount of 59 : * memory (about 200KB for LZ). 60 : **/ 61 : virtual Status Reset() = 0; 62 : 63 : /** 64 : * process (i.e. compress or decompress) data. 65 : * 66 : * @param in 67 : * @param inSize 68 : * @param out 69 : * @param outSize Bytes remaining in the output buffer; shall not be zero. 70 : * @param inConsumed,outProduced How many bytes in the input and 71 : * output buffers were used. either or both of these can be zero if 72 : * the input size is small or there's not enough output space. 73 : **/ 74 : virtual Status Process(const std::uint8_t* in, size_t inSize, std::uint8_t* out, size_t outSize, 75 : size_t& inConsumed, size_t& outProduced) = 0; 76 : 77 : /** 78 : * Flush buffers and make sure all output has been produced. 79 : * 80 : * @param checksum Checksum over all input data. 81 : * @param outProduced 82 : * @return error status for the entire operation. 83 : **/ 84 : virtual Status Finish(std::uint32_t& checksum, size_t& outProduced) = 0; 85 : 86 : /** 87 : * update a checksum to reflect the contents of a buffer. 88 : * 89 : * @param checksum the initial value (must be 0 on first call) 90 : * @param in 91 : * @param inSize 92 : * @return the new checksum. note: after all data has been seen, this is 93 : * identical to the what Finish would return. 94 : **/ 95 : virtual std::uint32_t UpdateChecksum(std::uint32_t checksum, const std::uint8_t* in, 96 : size_t inSize) const = 0; 97 : }; 98 : 99 : typedef std::shared_ptr<ICodec> PICodec; 100 : 101 : #endif // #ifndef INCLUDED_CODEC