1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "src/trace_processor/util/gzip_utils.h"
18
19 // For bazel build.
20 #include "perfetto/base/build_config.h"
21 #include "perfetto/base/compiler.h"
22
23 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
24 #include <zlib.h>
25 #else
26 struct z_stream_s {};
27 #endif
28
29 namespace perfetto {
30 namespace trace_processor {
31 namespace util {
32
IsGzipSupported()33 bool IsGzipSupported() {
34 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
35 return true;
36 #else
37 return false;
38 #endif
39 }
40
41 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB) // Real Implementation
42
GzipDecompressor(InputMode mode)43 GzipDecompressor::GzipDecompressor(InputMode mode) : z_stream_(new z_stream()) {
44 z_stream_->zalloc = nullptr;
45 z_stream_->zfree = nullptr;
46 z_stream_->opaque = nullptr;
47 // zlib uses a window size of -15..-8 to indicate it's a raw inflate stream
48 // (i.e. there's no zlib or gzip header).
49 int wbits = (mode == InputMode::kRawDeflate) ? -15 : 32 + MAX_WBITS;
50 inflateInit2(z_stream_.get(), wbits);
51 }
52
~GzipDecompressor()53 GzipDecompressor::~GzipDecompressor() {
54 inflateEnd(z_stream_.get());
55 }
56
Reset()57 void GzipDecompressor::Reset() {
58 inflateReset(z_stream_.get());
59 }
60
Feed(const uint8_t * data,size_t size)61 void GzipDecompressor::Feed(const uint8_t* data, size_t size) {
62 // This const_cast is not harmfull as zlib will not modify the data in this
63 // pointer. This is only necessary because of the build flags we use to be
64 // compatible with other embedders.
65 z_stream_->next_in = const_cast<uint8_t*>(data);
66 z_stream_->avail_in = static_cast<uInt>(size);
67 }
68
ExtractOutput(uint8_t * out,size_t out_size)69 GzipDecompressor::Result GzipDecompressor::ExtractOutput(uint8_t* out,
70 size_t out_size) {
71 if (z_stream_->avail_in == 0)
72 return Result{ResultCode::kNeedsMoreInput, 0};
73
74 z_stream_->next_out = out;
75 z_stream_->avail_out = static_cast<uInt>(out_size);
76
77 int ret = inflate(z_stream_.get(), Z_NO_FLUSH);
78 switch (ret) {
79 case Z_NEED_DICT:
80 case Z_DATA_ERROR:
81 case Z_MEM_ERROR:
82 // Ignore inflateEnd error as we will error out anyway.
83 inflateEnd(z_stream_.get());
84 return Result{ResultCode::kError, 0};
85 case Z_STREAM_END:
86 return Result{ResultCode::kEof, out_size - z_stream_->avail_out};
87 case Z_BUF_ERROR:
88 return Result{ResultCode::kNeedsMoreInput, 0};
89 default:
90 return Result{ResultCode::kOk, out_size - z_stream_->avail_out};
91 }
92 }
93
94 #else // Dummy Implementation
95
GzipDecompressor(InputMode)96 GzipDecompressor::GzipDecompressor(InputMode) {}
97 GzipDecompressor::~GzipDecompressor() = default;
Reset()98 void GzipDecompressor::Reset() {}
Feed(const uint8_t *,size_t)99 void GzipDecompressor::Feed(const uint8_t*, size_t) {}
ExtractOutput(uint8_t *,size_t)100 GzipDecompressor::Result GzipDecompressor::ExtractOutput(uint8_t*, size_t) {
101 return Result{ResultCode::kError, 0};
102 }
103
104 #endif // PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
105
106 // static
DecompressFully(const uint8_t * data,size_t len)107 std::vector<uint8_t> GzipDecompressor::DecompressFully(const uint8_t* data,
108 size_t len) {
109 std::vector<uint8_t> whole_data;
110 GzipDecompressor decompressor;
111 auto decom_output_consumer = [&](const uint8_t* buf, size_t buf_len) {
112 whole_data.insert(whole_data.end(), buf, buf + buf_len);
113 };
114 decompressor.FeedAndExtract(data, len, decom_output_consumer);
115 return whole_data;
116 }
117
118 } // namespace util
119 } // namespace trace_processor
120 } // namespace perfetto
121