Chromium Code Reviews| Index: components/tracing/core/proto_utils.cc |
| diff --git a/components/tracing/core/proto_utils.cc b/components/tracing/core/proto_utils.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..eee8029f82ee1ac895005065aab43d566aae1d77 |
| --- /dev/null |
| +++ b/components/tracing/core/proto_utils.cc |
| @@ -0,0 +1,77 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/tracing/core/proto_utils.h" |
| + |
| +#include <limits> |
| + |
| +#include "base/sys_byteorder.h" |
| + |
| +namespace tracing { |
| +namespace v2 { |
| +namespace proto { |
| + |
| +const uint8_t* ParseVarInt(const uint8_t* start, |
| + const uint8_t* end, |
| + uint64_t* value) { |
| + const uint8_t* pos = start; |
| + uint64_t shift = 0; |
| + *value = 0; |
| + do { |
| + CHECK_LT(pos, end); |
| + DCHECK_LT(shift, 64ull); |
| + *value |= static_cast<uint64_t>(*pos & 0x7f) << shift; |
| + shift += 7; |
| + } while (*pos++ & 0x80); |
|
kraynov
2016/09/02 16:06:49
Termination condition looks too tricky
Primiano Tucci (use gerrit)
2016/09/02 16:54:59
uh, but the only other option I see is doing:
whi
|
| + return pos; |
| +} |
| + |
| +const uint8_t* ParseField(const uint8_t* start, |
| + const uint8_t* end, |
| + uint32_t* field_id, |
| + FieldType* field_type, |
| + uint64_t* field_intvalue) { |
| + const uint8_t* pos = start; |
| + CHECK_LT(pos, end); |
| + *field_type = static_cast<FieldType>(*pos & 7); |
| + |
| + uint64_t raw_field_id; |
| + pos = ParseVarInt(pos, end, &raw_field_id); |
| + raw_field_id >>= 3; |
| + |
| + DCHECK_LE(raw_field_id, std::numeric_limits<uint32_t>::max()); |
| + *field_id = static_cast<uint32_t>(raw_field_id); |
| + |
| + switch (*field_type) { |
| + case kFieldTypeFixed64: { |
| + CHECK_LT(pos + 8, end); |
| + memcpy(field_intvalue, pos, 8); |
| + *field_intvalue = base::ByteSwapToLE64(*field_intvalue); |
| + pos += 8; |
| + break; |
| + } |
| + case kFieldTypeFixed32: { |
| + CHECK_LT(pos + 4, end); |
| + uint32_t tmp; |
| + memcpy(&tmp, pos, 4); |
| + *field_intvalue = base::ByteSwapToLE32(tmp); |
| + pos += 8; |
| + break; |
| + } |
| + case kFieldTypeVarInt: |
| + case kFieldTypeLengthDelimited: { |
| + pos = ParseVarInt(pos, end, field_intvalue); |
| + if (*field_type == kFieldTypeLengthDelimited) |
| + CHECK_LE(pos + *field_intvalue, end); |
| + break; |
| + } |
| + default: |
| + NOTREACHED() << "Unsupported proto field type " << *field_type; |
| + } |
| + return pos; |
| +} |
| + |
| +} // namespace proto |
| +} // namespace v2 |
| +} // namespace tracing |