blob: 56e917e51fcd0b03b74c9a4954cea8c091869a72 [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#include <stdlib.h>
16#include <algorithm>
Laszlo Csomor84fa11d2016-09-26 11:02:12 +000017#include <cstdio>
Rumou Duana518f632016-09-21 21:59:01 +000018
19#include "third_party/ijar/common.h"
20#include "third_party/ijar/zlib_client.h"
21#include <zlib.h>
22
23namespace devtools_ijar {
24
25u4 ComputeCrcChecksum(u1 *buf, size_t length) {
26 return crc32(0, buf, length);
27}
28
29size_t TryDeflate(u1 *buf, size_t length) {
30 u1 *outbuf = reinterpret_cast<u1 *>(malloc(length));
31 z_stream stream;
32
33 // Initialize the z_stream strcut for reading from buf and wrinting in outbuf.
34 stream.zalloc = Z_NULL;
35 stream.zfree = Z_NULL;
36 stream.opaque = Z_NULL;
37 stream.total_in = length;
38 stream.avail_in = length;
39 stream.total_out = length;
40 stream.avail_out = length;
41 stream.next_in = buf;
42 stream.next_out = outbuf;
43
44 // deflateInit2 negative windows size prevent the zlib wrapper to be used.
45 if (deflateInit2(&stream, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS, 8,
46 Z_DEFAULT_STRATEGY) != Z_OK) {
47 // Failure to compress => return the buffer uncompressed
48 free(outbuf);
49 return length;
50 }
51
52 if (deflate(&stream, Z_FINISH) == Z_STREAM_END) {
53 // Compression successful and fits in outbuf, let's copy the result in buf.
54 length = stream.total_out;
55 memcpy(buf, outbuf, length);
56 }
57
58 deflateEnd(&stream);
59 free(outbuf);
60
61 // Return the length of the resulting buffer
62 return length;
63}
64
65Decompressor::Decompressor() {
66 uncompressed_data_allocated_ = INITIAL_BUFFER_SIZE;
67 uncompressed_data_ =
68 reinterpret_cast<u1 *>(malloc(uncompressed_data_allocated_));
69}
70
71Decompressor::~Decompressor() { free(uncompressed_data_); }
72
73DecompressedFile *Decompressor::UncompressFile(const u1 *buffer,
74 size_t bytes_avail) {
75 z_stream stream;
76
77 stream.zalloc = Z_NULL;
78 stream.zfree = Z_NULL;
79 stream.opaque = Z_NULL;
80 stream.avail_in = bytes_avail;
81 stream.next_in = const_cast<Bytef*>(reinterpret_cast<const Bytef*>(buffer));
82
83 int ret = inflateInit2(&stream, -MAX_WBITS);
84 if (ret != Z_OK) {
85 error("inflateInit: %d\n", ret);
86 return NULL;
87 }
88
89 int uncompressed_until_now = 0;
90
91 while (true) {
92 stream.avail_out = uncompressed_data_allocated_ - uncompressed_until_now;
93 stream.next_out = uncompressed_data_ + uncompressed_until_now;
94 int old_avail_out = stream.avail_out;
95
96 ret = inflate(&stream, Z_SYNC_FLUSH);
97 int uncompressed_now = old_avail_out - stream.avail_out;
98 uncompressed_until_now += uncompressed_now;
99
100 switch (ret) {
101 case Z_STREAM_END: {
102 struct DecompressedFile *decompressedFile =
103 reinterpret_cast<DecompressedFile *>(
104 malloc(sizeof(DecompressedFile)));
105 // zlib said that there is no more data to decompress.
106 u1 *new_p = reinterpret_cast<u1 *>(stream.next_in);
107 decompressedFile->compressed_size = new_p - buffer;
108 decompressedFile->uncompressed_size = uncompressed_until_now;
109 decompressedFile->uncompressed_data = uncompressed_data_;
110 inflateEnd(&stream);
111 return decompressedFile;
112 }
113
114 case Z_OK: {
115 // zlib said that there is no more room in the buffer allocated for
116 // the decompressed data. Enlarge that buffer and try again.
117
118 if (uncompressed_data_allocated_ == MAX_BUFFER_SIZE) {
119 error(
120 "ijar does not support decompressing files "
121 "larger than %dMB.\n",
122 static_cast<int>((MAX_BUFFER_SIZE / (1024 * 1024))));
123 return NULL;
124 }
125
126 uncompressed_data_allocated_ *= 2;
127 if (uncompressed_data_allocated_ > MAX_BUFFER_SIZE) {
128 uncompressed_data_allocated_ = MAX_BUFFER_SIZE;
129 }
130
131 uncompressed_data_ = reinterpret_cast<u1 *>(
132 realloc(uncompressed_data_, uncompressed_data_allocated_));
133 break;
134 }
135
136 case Z_DATA_ERROR:
137 case Z_BUF_ERROR:
138 case Z_STREAM_ERROR:
139 case Z_NEED_DICT:
140 default: {
141 error("zlib returned error code %d during inflate.\n", ret);
142 return NULL;
143 }
144 }
145 }
146}
147
148char *Decompressor::GetError() {
149 if (errmsg[0] == 0) {
150 return NULL;
151 }
152 return errmsg;
153}
154
155int Decompressor::error(const char *fmt, ...) {
156 va_list ap;
157 va_start(ap, fmt);
158 vsnprintf(errmsg, 4 * PATH_MAX, fmt, ap);
159 va_end(ap);
160 return -1;
161}
162} // namespace devtools_ijar