blob: 05de8314deb41552e40c3bc9bfef66a0133cbd0e [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
2 * Copyright (C) 2016 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 "protobuf.h"
18
Yi Jin99c248f2017-08-25 18:11:58 -070019uint8_t read_wire_type(uint32_t varint)
20{
21 return (uint8_t) (varint & 0x07);
22}
Yi Jin0ed9b682017-08-18 14:51:20 -070023
Yi Jin99c248f2017-08-25 18:11:58 -070024uint32_t read_field_id(uint32_t varint)
25{
26 return varint >> 3;
27}
28
29uint8_t*
Joe Onorato1754d742016-11-21 17:51:35 -080030write_raw_varint(uint8_t* buf, uint32_t val)
31{
32 uint8_t* p = buf;
33 while (true) {
34 if ((val & ~0x7F) == 0) {
35 *p++ = (uint8_t)val;
36 return p;
37 } else {
38 *p++ = (uint8_t)((val & 0x7F) | 0x80);
39 val >>= 7;
40 }
41 }
42}
43
Yi Jin99c248f2017-08-25 18:11:58 -070044uint8_t*
Joe Onorato1754d742016-11-21 17:51:35 -080045write_length_delimited_tag_header(uint8_t* buf, uint32_t fieldId, size_t size)
46{
47 buf = write_raw_varint(buf, (fieldId << 3) | 2);
48 buf = write_raw_varint(buf, size);
49 return buf;
50}
51
Yi Jin99c248f2017-08-25 18:11:58 -070052size_t
53write_raw_varint(vector<uint8_t> &buf, uint32_t val)
54{
55 size_t size = 0;
56 while (true) {
57 size++;
58 if ((val & ~0x7F) == 0) {
59 buf.push_back((uint8_t) val);
60 return size;
61 } else {
62 buf.push_back((uint8_t)((val & 0x7F) | 0x80));
63 val >>= 7;
64 }
65 }
66}
67
68size_t
69write_header(vector<uint8_t> &buf, uint32_t fieldId, uint8_t wireType)
70{
71 return write_raw_varint(buf, (fieldId << 3) | wireType);
72}