1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3  * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #ifndef XZSTREAM_h
6 #define XZSTREAM_h
7 
8 #include <cstdlib>
9 #include <stdint.h>
10 
11 #define XZ_DEC_DYNALLOC
12 #include "xz.h"
13 
14 // Used to decode XZ stream buffers.
15 class XZStream {
16  public:
17   // Returns whether the provided buffer is likely a XZ stream.
18   static bool IsXZ(const void* aBuf, size_t aBufSize);
19 
20   // Creates a XZ stream object for the given input buffer.
21   XZStream(const void* aInBuf, size_t aInSize);
22   ~XZStream();
23 
24   // Initializes the decoder and returns whether decoding may commence.
25   bool Init();
26   // Decodes the next chunk of input into the given output buffer.
27   size_t Decode(void* aOutBuf, size_t aOutSize);
28   // Returns the number of yet undecoded bytes in the input buffer.
29   size_t RemainingInput() const;
30   // Returns the total number of bytes in the input buffer (compressed size).
31   size_t Size() const;
32   // Returns the expected final number of bytes in the output buffer.
33   // Note: will return 0 before successful Init().
34   size_t UncompressedSize() const;
35 
36  private:
37   // Parses the stream footer and returns the size of the index in bytes.
38   size_t ParseIndexSize() const;
39   // Parses the stream index and returns the expected uncompressed size in
40   // bytes.
41   size_t ParseUncompressedSize() const;
42 
43   const uint8_t* mInBuf;
44   size_t mUncompSize;
45   xz_buf mBuffers;
46   xz_dec* mDec;
47 };
48 
49 #endif  // XZSTREAM_h
50