blob: 62d268b7650ba04588a740a345fde39caceee94b [file] [log] [blame]
Elad Aloncb21ffe2018-10-19 18:30:30 +02001/*
2 * Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11#include "logging/rtc_event_log/encoder/blob_encoding.h"
12
13#include <algorithm>
14
15#include "rtc_base/logging.h"
16
17namespace webrtc {
18
19const size_t kMaxVarIntLengthBytes = 10; // ceil(64 / 7.0) is 10.
20
21namespace {
22
23// Encode a given uint64_t as a varint. From least to most significant,
24// each batch of seven bits are put into the lower bits of a byte, and the last
25// remaining bit in that byte (the highest one) marks whether additional bytes
26// follow (which happens if and only if there are other bits in |input| which
27// are non-zero).
28// Notes: If input == 0, one byte is used. If input is uint64_t::max, exactly
29// kMaxVarIntLengthBytes are used.
30std::string EncodeVarInt(uint64_t input) {
31 std::string output;
32 output.reserve(kMaxVarIntLengthBytes);
33
34 do {
35 uint8_t byte = static_cast<uint8_t>(input & 0x7f);
36 input >>= 7;
37 if (input > 0) {
38 byte |= 0x80;
39 }
40 output += byte;
41 } while (input > 0);
42
43 RTC_DCHECK_GE(output.size(), 1u);
44 RTC_DCHECK_LE(output.size(), kMaxVarIntLengthBytes);
45
46 return output;
47}
48
49// Inverse of EncodeVarInt().
50// If decoding is successful, a non-zero number is returned, indicating the
51// number of bytes read from |input|, and the decoded varint is written
52// into |output|.
53// If not successful, 0 is returned, and |output| is not modified.
54size_t DecodeVarInt(absl::string_view input, uint64_t* output) {
55 RTC_DCHECK(output);
56
57 uint64_t decoded = 0;
58 for (size_t i = 0; i < input.length() && i < kMaxVarIntLengthBytes; ++i) {
59 decoded += (static_cast<uint64_t>(input[i] & 0x7f)
60 << static_cast<uint64_t>(7 * i));
61 if (!(input[i] & 0x80)) {
62 *output = decoded;
63 return i + 1;
64 }
65 }
66
67 return 0;
68}
69
70} // namespace
71
72std::string EncodeBlobs(const std::vector<std::string>& blobs) {
73 RTC_DCHECK(!blobs.empty());
74
75 size_t result_length_bound = kMaxVarIntLengthBytes * blobs.size();
76 for (const auto& blob : blobs) {
77 // Providing an input so long that it would cause a wrap-around is an error.
78 RTC_DCHECK_GE(result_length_bound + blob.length(), result_length_bound);
79 result_length_bound += blob.length();
80 }
81
82 std::string result;
83 result.reserve(result_length_bound);
84
85 // First, encode all of the lengths.
86 for (absl::string_view blob : blobs) {
87 result += EncodeVarInt(blob.length());
88 }
89
90 // Second, encode the actual blobs.
91 for (absl::string_view blob : blobs) {
92 result.append(blob.data(), blob.length());
93 }
94
95 RTC_DCHECK_LE(result.size(), result_length_bound);
96 return result;
97}
98
99std::vector<absl::string_view> DecodeBlobs(absl::string_view encoded_blobs,
100 size_t num_of_blobs) {
101 if (encoded_blobs.empty()) {
102 RTC_LOG(LS_WARNING) << "Corrupt input; empty input.";
103 return std::vector<absl::string_view>();
104 }
105
106 if (num_of_blobs == 0u) {
107 RTC_LOG(LS_WARNING)
108 << "Corrupt input; number of blobs must be greater than 0.";
109 return std::vector<absl::string_view>();
110 }
111
112 size_t read_idx = 0;
113
114 // Read the lengths of all blobs.
115 std::vector<uint64_t> lengths(num_of_blobs);
116 for (size_t i = 0; i < num_of_blobs; ++i) {
117 if (read_idx >= encoded_blobs.length()) {
118 RTC_DCHECK_EQ(read_idx, encoded_blobs.length());
119 RTC_LOG(LS_WARNING) << "Corrupt input; excessive number of blobs.";
120 return std::vector<absl::string_view>();
121 }
122
123 const size_t read_bytes =
124 DecodeVarInt(encoded_blobs.substr(read_idx), &lengths[i]);
125 if (read_bytes == 0) {
126 RTC_LOG(LS_WARNING) << "Corrupt input; varint decoding failed.";
127 return std::vector<absl::string_view>();
128 }
129
130 read_idx += read_bytes;
131
132 // Note: It might be that read_idx == encoded_blobs.length(), if this
133 // is the last iteration, and all of the blobs are the empty string.
134 RTC_DCHECK_LE(read_idx, encoded_blobs.length());
135 }
136
137 // Read the blobs themselves.
138 std::vector<absl::string_view> blobs(num_of_blobs);
139 for (size_t i = 0; i < num_of_blobs; ++i) {
140 if (read_idx + lengths[i] < read_idx) { // Wrap-around detection.
141 RTC_LOG(LS_WARNING) << "Corrupt input; unreasonably large blob sequence.";
142 return std::vector<absl::string_view>();
143 }
144
145 if (read_idx + lengths[i] > encoded_blobs.length()) {
146 RTC_LOG(LS_WARNING) << "Corrupt input; blob sizes exceed input size.";
147 return std::vector<absl::string_view>();
148 }
149
150 blobs[i] = encoded_blobs.substr(read_idx, lengths[i]);
151 read_idx += lengths[i];
152 }
153
154 if (read_idx != encoded_blobs.length()) {
155 RTC_LOG(LS_WARNING) << "Corrupt input; unrecognized trailer.";
156 return std::vector<absl::string_view>();
157 }
158
159 return blobs;
160}
161
162} // namespace webrtc