|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
|
2 * vim: set ts=8 sts=4 et sw=4 tw=99: |
|
3 * This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #ifndef vm_Compression_h |
|
8 #define vm_Compression_h |
|
9 |
|
10 #ifdef USE_ZLIB |
|
11 |
|
12 #include <zlib.h> |
|
13 |
|
14 #include "jstypes.h" |
|
15 |
|
16 namespace js { |
|
17 |
|
18 class Compressor |
|
19 { |
|
20 /* Number of bytes we should hand to zlib each compressMore() call. */ |
|
21 static const size_t CHUNKSIZE = 2048; |
|
22 z_stream zs; |
|
23 const unsigned char *inp; |
|
24 size_t inplen; |
|
25 size_t outbytes; |
|
26 bool initialized; |
|
27 |
|
28 public: |
|
29 enum Status { |
|
30 MOREOUTPUT, |
|
31 DONE, |
|
32 CONTINUE, |
|
33 OOM |
|
34 }; |
|
35 |
|
36 Compressor(const unsigned char *inp, size_t inplen); |
|
37 ~Compressor(); |
|
38 bool init(); |
|
39 void setOutput(unsigned char *out, size_t outlen); |
|
40 size_t outWritten() const { return outbytes; } |
|
41 /* Compress some of the input. Return true if it should be called again. */ |
|
42 Status compressMore(); |
|
43 }; |
|
44 |
|
45 /* |
|
46 * Decompress a string. The caller must know the length of the output and |
|
47 * allocate |out| to a string of that length. |
|
48 */ |
|
49 bool DecompressString(const unsigned char *inp, size_t inplen, |
|
50 unsigned char *out, size_t outlen); |
|
51 |
|
52 } /* namespace js */ |
|
53 |
|
54 #endif /* USE_ZLIB */ |
|
55 #endif /* vm_Compression_h */ |