1 // Copyright 2006 The Android Open Source Project
8 #include "trace_common.h"
10 // This array provides a fast conversion from the initial byte in
11 // a varint-encoded object to the length (in bytes) of that object.
12 int prefix_to_len[] = {
13 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
14 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
15 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
16 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
17 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
18 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
19 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
20 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
21 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
22 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
23 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
24 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
25 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
26 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
27 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
28 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 9, 9, 17, 17
31 // This array provides a fast conversion from the initial byte in
32 // a varint-encoded object to the initial data bits for that object.
33 int prefix_to_data[] = {
34 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
35 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
36 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
37 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
38 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,
39 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95,
40 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111,
41 112, 113, 114, 115, 116, 117, 118, 119,
42 120, 121, 122, 123, 124, 125, 126, 127,
43 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
44 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
45 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
46 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
47 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
48 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
49 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
50 0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 0, 0, 0, 0
53 signed char prefix_to_signed_data[] = {
54 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
55 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
56 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
57 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
58 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
59 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
60 0x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fd, 0xfe, 0xff,
82 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
83 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff,
84 0x00, 0x01, 0x02, 0x03, 0xfc, 0xfd, 0xfe, 0xff,
85 0x00, 0x01, 0xfe, 0xff, 0x00, 0xff, 0x00, 0xff,
102 void Decoder::Close()
110 void Decoder::Open(char *filename)
112 if (filename_ != NULL) {
115 filename_ = new char[strlen(filename) + 1];
116 strcpy(filename_, filename);
117 fstream_ = fopen(filename_, "r");
118 if (fstream_ == NULL) {
123 int rval = fread(buf_, 1, kBufSize, fstream_);
124 if (rval != kBufSize) {
125 if (ferror(fstream_)) {
129 if (!feof(fstream_)) {
130 fprintf(stderr, "Unexpected short fread() before eof\n");
138 void Decoder::FillBuffer()
140 assert(next_ <= end_);
142 if (end_ - next_ < kDecodingSpace && end_ == &buf_[kBufSize]) {
143 // Copy the unused bytes left at the end to the beginning of the
145 int len = end_ - next_;
147 memcpy(buf_, next_, len);
149 // Read enough bytes to fill up the buffer, if possible.
150 int nbytes = kBufSize - len;
151 int rval = fread(buf_ + len, 1, nbytes, fstream_);
153 if (ferror(fstream_)) {
157 if (!feof(fstream_)) {
158 fprintf(stderr, "Unexpected short fread() before eof\n");
162 end_ = &buf_[len + rval];
167 void Decoder::Read(char *dest, int len)
170 int nbytes = end_ - next_;
173 nbytes = end_ - next_;
179 memcpy(dest, next_, nbytes);
186 // Decode a varint-encoded object starting at the current position in
187 // the array "buf_" and return the decoded value as a 64-bit integer.
188 // A varint-encoded object has an initial prefix that specifies how many
189 // data bits follow. If the first bit is zero, for example, then there
190 // are 7 data bits that follow. The table below shows the prefix values
191 // and corresponding data bits.
193 // Prefix Bytes Data bits
204 int64_t Decoder::Decode(bool is_signed)
208 if (end_ - next_ < kDecodingSpace)
211 #if BYTE_ORDER == BIG_ENDIAN
212 uint8_t byte0 = *next_;
214 // Get the number of bytes to decode based on the first byte.
215 int len = prefix_to_len[byte0];
217 if (next_ + len > end_) {
218 fprintf(stderr, "%s: decoding past end of file.\n", filename_);
222 // Get the first data byte.
224 val64 = prefix_to_signed_data[byte0];
226 val64 = prefix_to_data[byte0];
229 for (int ii = 1; ii < len; ++ii) {
230 val64 = (val64 << 8) | *next_++;
233 // If we are on a little-endian machine, then use large, unaligned loads.
234 uint64_t data = *(reinterpret_cast<uint64_t*>(next_));
235 uint8_t byte0 = data;
236 data = bswap64(data);
238 // Get the number of bytes to decode based on the first byte.
239 int len = prefix_to_len[byte0];
241 if (next_ + len > end_) {
242 fprintf(stderr, "%s: decoding past end of file.\n", filename_);
246 // Get the first data byte.
248 val64 = prefix_to_signed_data[byte0];
250 val64 = prefix_to_data[byte0];
256 val64 = (val64 << 8) | ((data >> 48) & 0xffull);
259 val64 = (val64 << 16) | ((data >> 40) & 0xffffull);
262 val64 = (val64 << 24) | ((data >> 32) & 0xffffffull);
265 val64 = (val64 << 32) | ((data >> 24) & 0xffffffffull);
268 val64 = (val64 << 40) | ((data >> 16) & 0xffffffffffull);
271 data = *(reinterpret_cast<uint64_t*>(&next_[1]));
272 val64 = bswap64(data);