File: proto_utils.cc

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (89 lines) | stat: -rw-r--r-- 2,756 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// 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"

#define CHECK_PTR_LE(a, b) \
  CHECK_LE(reinterpret_cast<const void*>(a), reinterpret_cast<const void*>(b))
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_PTR_LE(pos, end - 1);
    DCHECK_LT(shift, 64ull);
    *value |= static_cast<uint64_t>(*pos & 0x7f) << shift;
    shift += 7;
  } while (*pos++ & 0x80);
  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) {
  // The first byte of a proto field is structured as follows:
  // The least 3 significant bits determine the field type.
  // The most 5 significant bits determine the field id. If MSB == 1, the
  // field id continues on the next bytes following the VarInt encoding.
  const uint8_t kFieldTypeNumBits = 3;
  const uint8_t kFieldTypeMask = (1 << kFieldTypeNumBits) - 1;  // 0000 0111;

  const uint8_t* pos = start;
  CHECK_PTR_LE(pos, end - 1);
  *field_type = static_cast<FieldType>(*pos & kFieldTypeMask);

  uint64_t raw_field_id;
  pos = ParseVarInt(pos, end, &raw_field_id);
  raw_field_id >>= kFieldTypeNumBits;

  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_PTR_LE(pos + sizeof(uint64_t), end);
      memcpy(field_intvalue, pos, sizeof(uint64_t));
      *field_intvalue = base::ByteSwapToLE64(*field_intvalue);
      pos += sizeof(uint64_t);
      break;
    }
    case kFieldTypeFixed32: {
      CHECK_PTR_LE(pos + sizeof(uint32_t), end);
      uint32_t tmp;
      memcpy(&tmp, pos, sizeof(uint32_t));
      *field_intvalue = base::ByteSwapToLE32(tmp);
      pos += sizeof(uint32_t);
      break;
    }
    case kFieldTypeVarInt: {
      pos = ParseVarInt(pos, end, field_intvalue);
      break;
    }
    case kFieldTypeLengthDelimited: {
      pos = ParseVarInt(pos, end, field_intvalue);
      pos += *field_intvalue;
      CHECK_PTR_LE(pos, end);
      break;
    }
    default:
      NOTREACHED() << "Unsupported proto field type " << *field_type;
  }
  return pos;
}

}  // namespace proto
}  // namespace v2
}  // namespace tracing