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/proto/proto_trace_tokenizer.h"
18 #include "perfetto/trace_processor/trace_blob.h"
19
20 #include "perfetto/ext/base/utils.h"
21
22 namespace perfetto {
23 namespace trace_processor {
24
25 ProtoTraceTokenizer::ProtoTraceTokenizer() = default;
26
Decompress(TraceBlobView input,TraceBlobView * output)27 util::Status ProtoTraceTokenizer::Decompress(TraceBlobView input,
28 TraceBlobView* output) {
29 PERFETTO_DCHECK(util::IsGzipSupported());
30
31 std::vector<uint8_t> data;
32 data.reserve(input.length());
33
34 // Ensure that the decompressor is able to cope with a new stream of data.
35 decompressor_.Reset();
36 using ResultCode = util::GzipDecompressor::ResultCode;
37 ResultCode ret = decompressor_.FeedAndExtract(
38 input.data(), input.length(),
39 [&data](const uint8_t* buffer, size_t buffer_len) {
40 data.insert(data.end(), buffer, buffer + buffer_len);
41 });
42
43 if (ret == ResultCode::kError || ret == ResultCode::kNeedsMoreInput) {
44 return util::ErrStatus("Failed to decompress (error code: %d)",
45 static_cast<int>(ret));
46 }
47
48 TraceBlob out_blob = TraceBlob::CopyFrom(data.data(), data.size());
49 *output = TraceBlobView(std::move(out_blob));
50 return util::OkStatus();
51 }
52
53 } // namespace trace_processor
54 } // namespace perfetto
55