/proc/self/cwd/opencensus/common/internal/varint.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2019, OpenCensus Authors |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #include <cstdint> |
16 | | #include <string> |
17 | | |
18 | | #include "absl/strings/string_view.h" |
19 | | #include "opencensus/common/internal/varint.h" |
20 | | |
21 | | namespace opencensus { |
22 | | namespace common { |
23 | | |
24 | 0 | void AppendVarint32(uint32_t i, std::string* out) { |
25 | 0 | do { |
26 | | // Encode 7 bits. |
27 | 0 | uint8_t c = i & 0x7F; |
28 | 0 | i = i >> 7; |
29 | 0 | if (i != 0) { |
30 | 0 | c |= 0x80; |
31 | 0 | } |
32 | 0 | out->push_back(c); |
33 | 0 | } while (i != 0); |
34 | 0 | } |
35 | | |
36 | 244k | bool ParseVarint32(absl::string_view* input, uint32_t* out) { |
37 | 244k | absl::string_view s = *input; |
38 | 244k | uint32_t i = 0; |
39 | 244k | uint8_t c; |
40 | 244k | int shift = 0; |
41 | 246k | do { |
42 | 246k | if (s.empty()) { |
43 | 32 | return false; // Too short. |
44 | 32 | } |
45 | 246k | c = s[0]; |
46 | 246k | s = s.substr(1); |
47 | 246k | if (shift == 28 && c > 0x0f) { |
48 | 5 | return false; // Out of range for uint32_t. |
49 | 5 | } |
50 | 246k | i |= (c & 0x7F) << shift; |
51 | 246k | shift += 7; |
52 | 246k | } while (c & 0x80); |
53 | 244k | *input = s; |
54 | 244k | *out = i; |
55 | 244k | return true; |
56 | 244k | } |
57 | | |
58 | | } // namespace common |
59 | | } // namespace opencensus |