Primiano Tucci | 97440f4 | 2017-10-24 13:27:18 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "protozero/proto_utils.h" |
| 18 | |
| 19 | #include <string.h> |
| 20 | |
| 21 | #include <limits> |
| 22 | |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 23 | #include "perfetto_base/logging.h" |
Primiano Tucci | 97440f4 | 2017-10-24 13:27:18 +0100 | [diff] [blame] | 24 | |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 25 | #define PERFETTO_CHECK_PTR_LE(a, b) \ |
| 26 | PERFETTO_CHECK(reinterpret_cast<uintptr_t>(a) <= \ |
| 27 | reinterpret_cast<uintptr_t>(b)) |
Primiano Tucci | 97440f4 | 2017-10-24 13:27:18 +0100 | [diff] [blame] | 28 | |
| 29 | namespace protozero { |
| 30 | namespace proto_utils { |
| 31 | |
| 32 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 33 | #define BYTE_SWAP_TO_LE32(x) (x) |
| 34 | #define BYTE_SWAP_TO_LE64(x) (x) |
| 35 | #else |
| 36 | #error Unimplemented for big endian archs. |
| 37 | #endif |
| 38 | |
| 39 | const uint8_t* ParseVarInt(const uint8_t* start, |
| 40 | const uint8_t* end, |
| 41 | uint64_t* value) { |
| 42 | const uint8_t* pos = start; |
| 43 | uint64_t shift = 0; |
| 44 | *value = 0; |
| 45 | do { |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 46 | PERFETTO_CHECK_PTR_LE(pos, end - 1); |
| 47 | PERFETTO_DCHECK(shift < 64ull); |
Primiano Tucci | 97440f4 | 2017-10-24 13:27:18 +0100 | [diff] [blame] | 48 | *value |= static_cast<uint64_t>(*pos & 0x7f) << shift; |
| 49 | shift += 7; |
| 50 | } while (*pos++ & 0x80); |
| 51 | return pos; |
| 52 | } |
| 53 | |
| 54 | const uint8_t* ParseField(const uint8_t* start, |
| 55 | const uint8_t* end, |
| 56 | uint32_t* field_id, |
| 57 | FieldType* field_type, |
| 58 | uint64_t* field_intvalue) { |
| 59 | // The first byte of a proto field is structured as follows: |
| 60 | // The least 3 significant bits determine the field type. |
| 61 | // The most 5 significant bits determine the field id. If MSB == 1, the |
| 62 | // field id continues on the next bytes following the VarInt encoding. |
| 63 | const uint8_t kFieldTypeNumBits = 3; |
| 64 | const uint8_t kFieldTypeMask = (1 << kFieldTypeNumBits) - 1; // 0000 0111; |
| 65 | |
| 66 | const uint8_t* pos = start; |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 67 | PERFETTO_CHECK_PTR_LE(pos, end - 1); |
Primiano Tucci | 97440f4 | 2017-10-24 13:27:18 +0100 | [diff] [blame] | 68 | *field_type = static_cast<FieldType>(*pos & kFieldTypeMask); |
| 69 | |
| 70 | uint64_t raw_field_id; |
| 71 | pos = ParseVarInt(pos, end, &raw_field_id); |
| 72 | raw_field_id >>= kFieldTypeNumBits; |
| 73 | |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 74 | PERFETTO_DCHECK(raw_field_id <= std::numeric_limits<uint32_t>::max()); |
Primiano Tucci | 97440f4 | 2017-10-24 13:27:18 +0100 | [diff] [blame] | 75 | *field_id = static_cast<uint32_t>(raw_field_id); |
| 76 | |
| 77 | switch (*field_type) { |
| 78 | case kFieldTypeFixed64: { |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 79 | PERFETTO_CHECK_PTR_LE(pos + sizeof(uint64_t), end); |
Primiano Tucci | 97440f4 | 2017-10-24 13:27:18 +0100 | [diff] [blame] | 80 | memcpy(field_intvalue, pos, sizeof(uint64_t)); |
| 81 | *field_intvalue = BYTE_SWAP_TO_LE64(*field_intvalue); |
| 82 | pos += sizeof(uint64_t); |
| 83 | break; |
| 84 | } |
| 85 | case kFieldTypeFixed32: { |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 86 | PERFETTO_CHECK_PTR_LE(pos + sizeof(uint32_t), end); |
Primiano Tucci | 97440f4 | 2017-10-24 13:27:18 +0100 | [diff] [blame] | 87 | uint32_t tmp; |
| 88 | memcpy(&tmp, pos, sizeof(uint32_t)); |
| 89 | *field_intvalue = BYTE_SWAP_TO_LE32(tmp); |
| 90 | pos += sizeof(uint32_t); |
| 91 | break; |
| 92 | } |
| 93 | case kFieldTypeVarInt: { |
| 94 | pos = ParseVarInt(pos, end, field_intvalue); |
| 95 | break; |
| 96 | } |
| 97 | case kFieldTypeLengthDelimited: { |
| 98 | pos = ParseVarInt(pos, end, field_intvalue); |
| 99 | pos += *field_intvalue; |
Primiano Tucci | d7d1be0 | 2017-10-30 17:41:34 +0000 | [diff] [blame] | 100 | PERFETTO_CHECK_PTR_LE(pos, end); |
Primiano Tucci | 97440f4 | 2017-10-24 13:27:18 +0100 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | } |
| 104 | return pos; |
| 105 | } |
| 106 | |
| 107 | } // namespace proto_utils |
| 108 | } // namespace protozero |