blob: ed6616362fcc3377fe0fd920572734eee91a5a34 [file] [log] [blame]
Rumou Duana518f632016-09-21 21:59:01 +00001// Copyright 2016 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef THIRD_PARTY_IJAR_ZLIB_CLIENT_H_
16#define THIRD_PARTY_IJAR_ZLIB_CLIENT_H_
17
18#include <limits.h>
19
20#include "third_party/ijar/common.h"
21
22namespace devtools_ijar {
23// Try to compress a file entry in memory using the deflate algorithm.
24// It will compress buf (of size length) unless the compressed size is bigger
25// than the input size. The result will overwrite the content of buf and the
26// final size is returned.
27size_t TryDeflate(u1* buf, size_t length);
28
29u4 ComputeCrcChecksum(u1* buf, size_t length);
30
31struct DecompressedFile {
32 u1* uncompressed_data;
33 u4 uncompressed_size;
34 u4 compressed_size;
35};
36
37class Decompressor {
38 public:
39 Decompressor();
40 ~Decompressor();
41 DecompressedFile* UncompressFile(const u1* buffer, size_t bytes_avail);
42 char* GetError();
43
44 private:
45 // Administration of memory reserved for decompressed data. We use the same
46 // buffer for each file to avoid some malloc()/free() calls and free the
47 // memory only in the dtor. C-style memory management is used so that we
48 // can call realloc.
49 u1* uncompressed_data_;
50 size_t uncompressed_data_allocated_;
51 // last error
52 char errmsg[4 * PATH_MAX];
53
54 int error(const char* fmt, ...);
55
56 // Buffer size is initially INITIAL_BUFFER_SIZE. It doubles in size every
57 // time it is found too small, until it reaches MAX_BUFFER_SIZE. If that is
58 // not enough, we bail out. We only decompress class files, so they should
59 // be smaller than 64K anyway, but we give a little leeway.
60 // MAX_BUFFER_SIZE must be bigger than the size of the biggest file in the
61 // ZIP. It is set to 2GB here because no one has audited the code for 64-bit
62 // cleanliness.
63 static const size_t INITIAL_BUFFER_SIZE = 256 * 1024; // 256K
64 static const size_t MAX_BUFFER_SIZE = std::numeric_limits<int32_t>::max();
65};
66} // namespace devtools_ijar
67
68#endif // THIRD_PARTY_IJAR_ZLIB_CLIENT_H_