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()43 GzipDecompressor::GzipDecompressor() : z_stream_(new z_stream()) {
44 z_stream_->zalloc = nullptr;
45 z_stream_->zfree = nullptr;
46 z_stream_->opaque = nullptr;
47 inflateInit2(z_stream_.get(), 32 + MAX_WBITS);
48 }
49
~GzipDecompressor()50 GzipDecompressor::~GzipDecompressor() {
51 inflateEnd(z_stream_.get());
52 }
53
Reset()54 void GzipDecompressor::Reset() {
55 inflateReset(z_stream_.get());
56 }
57
Feed(const uint8_t * data,size_t size)58 void GzipDecompressor::Feed(const uint8_t* data, size_t size) {
59 // This const_cast is not harmfull as zlib will not modify the data in this
60 // pointer. This is only necessary because of the build flags we use to be
61 // compatible with other embedders.
62 z_stream_->next_in = const_cast<uint8_t*>(data);
63 z_stream_->avail_in = static_cast<uInt>(size);
64 }
65
ExtractOutput(uint8_t * out,size_t out_size)66 GzipDecompressor::Result GzipDecompressor::ExtractOutput(uint8_t* out,
67 size_t out_size) {
68 if (z_stream_->avail_in == 0)
69 return Result{ResultCode::kNeedsMoreInput, 0};
70
71 z_stream_->next_out = out;
72 z_stream_->avail_out = static_cast<uInt>(out_size);
73
74 int ret = inflate(z_stream_.get(), Z_NO_FLUSH);
75 switch (ret) {
76 case Z_NEED_DICT:
77 case Z_DATA_ERROR:
78 case Z_MEM_ERROR:
79 // Ignore inflateEnd error as we will error out anyway.
80 inflateEnd(z_stream_.get());
81 return Result{ResultCode::kError, 0};
82 case Z_STREAM_END:
83 return Result{ResultCode::kEof, out_size - z_stream_->avail_out};
84 case Z_BUF_ERROR:
85 return Result{ResultCode::kNeedsMoreInput, 0};
86 default:
87 return Result{ResultCode::kOk, out_size - z_stream_->avail_out};
88 }
89 }
90
91 #else // Dummy Implementation
92
93 GzipDecompressor::GzipDecompressor() = default;
94 GzipDecompressor::~GzipDecompressor() = default;
Reset()95 void GzipDecompressor::Reset() {}
Feed(const uint8_t *,size_t)96 void GzipDecompressor::Feed(const uint8_t*, size_t) {}
ExtractOutput(uint8_t *,size_t)97 GzipDecompressor::Result GzipDecompressor::ExtractOutput(uint8_t*, size_t) {
98 return Result{ResultCode::kError, 0};
99 }
100
101 #endif // PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
102
103 // static
DecompressFully(const uint8_t * data,size_t len)104 std::vector<uint8_t> GzipDecompressor::DecompressFully(const uint8_t* data,
105 size_t len) {
106 std::vector<uint8_t> whole_data;
107 GzipDecompressor decompressor;
108 auto decom_output_consumer = [&](const uint8_t* buf, size_t buf_len) {
109 whole_data.insert(whole_data.end(), buf, buf + buf_len);
110 };
111 decompressor.FeedAndExtract(data, len, decom_output_consumer);
112 return whole_data;
113 }
114
115 } // namespace util
116 } // namespace trace_processor
117 } // namespace perfetto
118