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