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/importers/gzip/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 gzip {
32
IsGzipSupported()33 bool IsGzipSupported() {
34 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
35 return true;
36 #else
37 return false;
38 #endif
39 }
40
41 } // namespace gzip
42
43 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
GzipDecompressor()44 GzipDecompressor::GzipDecompressor() : z_stream_(new z_stream()) {
45 z_stream_->zalloc = Z_NULL;
46 z_stream_->zfree = Z_NULL;
47 z_stream_->opaque = Z_NULL;
48 inflateInit2(z_stream_.get(), 32 + 15);
49 }
50 #else
51 GzipDecompressor::GzipDecompressor() = default;
52 #endif
53
~GzipDecompressor()54 GzipDecompressor::~GzipDecompressor() {
55 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
56 // Ensure the call to inflateEnd to prevent leaks of internal state.
57 inflateEnd(z_stream_.get());
58 #endif
59 }
60
Reset()61 void GzipDecompressor::Reset() {
62 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
63 inflateReset(z_stream_.get());
64 #endif
65 }
66
SetInput(const uint8_t * data,size_t size)67 void GzipDecompressor::SetInput(const uint8_t* data, size_t size) {
68 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
69 // This const_cast is not harmfull as zlib will not modify the data in this
70 // pointer. This is only necessary because of the build flags we use to be
71 // compatible with other embedders.
72 z_stream_->next_in = const_cast<uint8_t*>(data);
73 z_stream_->avail_in = static_cast<uInt>(size);
74 #else
75 base::ignore_result(data);
76 base::ignore_result(size);
77 #endif
78 }
79
Decompress(uint8_t * out,size_t out_size)80 GzipDecompressor::Result GzipDecompressor::Decompress(uint8_t* out,
81 size_t out_size) {
82 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
83 if (z_stream_->avail_in == 0)
84 return Result{ResultCode::kNeedsMoreInput, 0};
85
86 z_stream_->next_out = out;
87 z_stream_->avail_out = static_cast<uInt>(out_size);
88
89 int ret = inflate(z_stream_.get(), Z_NO_FLUSH);
90 switch (ret) {
91 case Z_NEED_DICT:
92 case Z_DATA_ERROR:
93 case Z_MEM_ERROR:
94 // Ignore inflateEnd error as we will error out anyway.
95 inflateEnd(z_stream_.get());
96 return Result{ResultCode::kError, 0};
97 case Z_STREAM_END:
98 return Result{ResultCode::kEof, out_size - z_stream_->avail_out};
99 case Z_BUF_ERROR:
100 return Result{ResultCode::kNoProgress, 0};
101 default:
102 return Result{ResultCode::kOk, out_size - z_stream_->avail_out};
103 }
104 #else
105 base::ignore_result(out);
106 base::ignore_result(out_size);
107 return Result{ResultCode::kError, 0};
108 #endif
109 }
110
111 } // namespace trace_processor
112 } // namespace perfetto
113