blob: 2155084db534e7e38f8cb8b58d82e65b0d3f35f7 [file] [log] [blame]
Yi Jin974a9c22017-10-02 18:37:08 -07001/*
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#ifndef ANDROID_UTIL_PROTOOUTPUT_STREAM_H
18#define ANDROID_UTIL_PROTOOUTPUT_STREAM_H
19
20#include <android/util/EncodedBuffer.h>
21
Yi Jin974a9c22017-10-02 18:37:08 -070022#include <string>
23
24namespace android {
25namespace util {
26
27/**
Yi Jin04625ad2017-10-17 18:29:33 -070028 * Position of the field type in a (long long) fieldId.
29 */
30const uint64_t FIELD_TYPE_SHIFT = 32;
31
32/**
33 * Mask for the field types stored in a fieldId. Leaves a whole
34 * byte for future expansion, even though there are currently only 17 types.
35 */
36const uint64_t FIELD_TYPE_MASK = 0x0ffULL << FIELD_TYPE_SHIFT;
37
38const uint64_t FIELD_TYPE_UNKNOWN = 0;
39const uint64_t FIELD_TYPE_DOUBLE = 1ULL << FIELD_TYPE_SHIFT; // double, exactly eight bytes on the wire.
40const uint64_t FIELD_TYPE_FLOAT = 2ULL << FIELD_TYPE_SHIFT; // float, exactly four bytes on the wire.
41const uint64_t FIELD_TYPE_INT64 = 3ULL << FIELD_TYPE_SHIFT; // int64, varint on the wire. Negative numbers
42 // take 10 bytes. Use TYPE_SINT64 if negative
43 // values are likely.
44const uint64_t FIELD_TYPE_UINT64 = 4ULL << FIELD_TYPE_SHIFT; // uint64, varint on the wire.
45const uint64_t FIELD_TYPE_INT32 = 5ULL << FIELD_TYPE_SHIFT; // int32, varint on the wire. Negative numbers
46 // take 10 bytes. Use TYPE_SINT32 if negative
47 // values are likely.
48const uint64_t FIELD_TYPE_FIXED64 = 6ULL << FIELD_TYPE_SHIFT; // uint64, exactly eight bytes on the wire.
49const uint64_t FIELD_TYPE_FIXED32 = 7ULL << FIELD_TYPE_SHIFT; // uint32, exactly four bytes on the wire.
50const uint64_t FIELD_TYPE_BOOL = 8ULL << FIELD_TYPE_SHIFT; // bool, varint on the wire.
51const uint64_t FIELD_TYPE_STRING = 9ULL << FIELD_TYPE_SHIFT; // UTF-8 text.
52const uint64_t FIELD_TYPE_GROUP = 10ULL << FIELD_TYPE_SHIFT; // Tag-delimited message. Deprecated.
53const uint64_t FIELD_TYPE_MESSAGE = 11ULL << FIELD_TYPE_SHIFT; // Length-delimited message.
54
55const uint64_t FIELD_TYPE_BYTES = 12ULL << FIELD_TYPE_SHIFT; // Arbitrary byte array.
56const uint64_t FIELD_TYPE_UINT32 = 13ULL << FIELD_TYPE_SHIFT; // uint32, varint on the wire
57const uint64_t FIELD_TYPE_ENUM = 14ULL << FIELD_TYPE_SHIFT; // Enum, varint on the wire
58const uint64_t FIELD_TYPE_SFIXED32 = 15ULL << FIELD_TYPE_SHIFT; // int32, exactly four bytes on the wire
59const uint64_t FIELD_TYPE_SFIXED64 = 16ULL << FIELD_TYPE_SHIFT; // int64, exactly eight bytes on the wire
60const uint64_t FIELD_TYPE_SINT32 = 17ULL << FIELD_TYPE_SHIFT; // int32, ZigZag-encoded varint on the wire
61const uint64_t FIELD_TYPE_SINT64 = 18ULL << FIELD_TYPE_SHIFT; // int64, ZigZag-encoded varint on the wire
62
63//
64// FieldId flags for whether the field is single, repeated or packed.
65// TODO: packed is not supported yet.
66//
67const uint64_t FIELD_COUNT_SHIFT = 40;
68const uint64_t FIELD_COUNT_MASK = 0x0fULL << FIELD_COUNT_SHIFT;
69const uint64_t FIELD_COUNT_UNKNOWN = 0;
70const uint64_t FIELD_COUNT_SINGLE = 1ULL << FIELD_COUNT_SHIFT;
71const uint64_t FIELD_COUNT_REPEATED = 2ULL << FIELD_COUNT_SHIFT;
72const uint64_t FIELD_COUNT_PACKED = 4ULL << FIELD_COUNT_SHIFT;
73
74/**
Yi Jin974a9c22017-10-02 18:37:08 -070075 * Class to write to a protobuf stream.
76 *
77 * Each write method takes an ID code from the protoc generated classes
78 * and the value to write. To make a nested object, call start
79 * and then end when you are done.
80 *
81 * See the java version implementation (ProtoOutputStream.java) for more infos.
82 */
83class ProtoOutputStream
84{
85public:
Yi Jin42711a02017-10-11 18:20:24 -070086 ProtoOutputStream();
Yi Jin974a9c22017-10-02 18:37:08 -070087 ~ProtoOutputStream();
88
89 /**
90 * Write APIs for dumping protobuf data. Returns true if the write succeeds.
91 */
92 bool write(uint64_t fieldId, double val);
93 bool write(uint64_t fieldId, float val);
94 bool write(uint64_t fieldId, int val);
95 bool write(uint64_t fieldId, long long val);
96 bool write(uint64_t fieldId, bool val);
97 bool write(uint64_t fieldId, std::string val);
Yi Jine0833302017-10-23 15:42:44 -070098 bool write(uint64_t fieldId, const char* val, size_t size);
Yi Jin974a9c22017-10-02 18:37:08 -070099
100 /**
101 * Starts a sub-message write session.
102 * Returns a token of this write session.
103 * Must call end(token) when finish write this sub-message.
104 */
105 long long start(uint64_t fieldId);
106 void end(long long token);
107
108 /**
Yi Jin04625ad2017-10-17 18:29:33 -0700109 * Flushes the protobuf data out to given fd. When the following functions are called,
110 * it is not able to write to ProtoOutputStream any more since the data is compact.
Yi Jin974a9c22017-10-02 18:37:08 -0700111 */
Yi Jin04625ad2017-10-17 18:29:33 -0700112 size_t size(); // Get the size of the serialized protobuf.
113 EncodedBuffer::iterator data(); // Get the reader apis of the data.
114 bool flush(int fd); // Flush data directly to a file descriptor.
Yi Jin42711a02017-10-11 18:20:24 -0700115
Yi Jin8ad19382017-10-30 16:07:20 -0700116 // Please don't use the following functions to dump protos unless you are familiar with protobuf encoding.
Yi Jin42711a02017-10-11 18:20:24 -0700117 void writeRawVarint(uint64_t varint);
118 void writeLengthDelimitedHeader(uint32_t id, size_t size);
119 void writeRawByte(uint8_t byte);
Yi Jin974a9c22017-10-02 18:37:08 -0700120
121private:
122 EncodedBuffer mBuffer;
Yi Jin974a9c22017-10-02 18:37:08 -0700123 size_t mCopyBegin;
124 bool mCompact;
125 int mDepth;
126 int mObjectId;
127 long long mExpectedObjectToken;
128
129 inline void writeDoubleImpl(uint32_t id, double val);
130 inline void writeFloatImpl(uint32_t id, float val);
131 inline void writeInt64Impl(uint32_t id, long long val);
132 inline void writeInt32Impl(uint32_t id, int val);
133 inline void writeUint64Impl(uint32_t id, uint64_t val);
134 inline void writeUint32Impl(uint32_t id, uint32_t val);
135 inline void writeFixed64Impl(uint32_t id, uint64_t val);
136 inline void writeFixed32Impl(uint32_t id, uint32_t val);
137 inline void writeSFixed64Impl(uint32_t id, long long val);
138 inline void writeSFixed32Impl(uint32_t id, int val);
139 inline void writeZigzagInt64Impl(uint32_t id, long long val);
140 inline void writeZigzagInt32Impl(uint32_t id, int val);
141 inline void writeEnumImpl(uint32_t id, int val);
142 inline void writeBoolImpl(uint32_t id, bool val);
143 inline void writeUtf8StringImpl(uint32_t id, const char* val, size_t size);
Yi Jin8ad19382017-10-30 16:07:20 -0700144 inline void writeMessageBytesImpl(uint32_t id, const char* val, size_t size);
Yi Jin974a9c22017-10-02 18:37:08 -0700145
146 bool compact();
147 size_t editEncodedSize(size_t rawSize);
148 bool compactSize(size_t rawSize);
149};
150
151}
152}
153
Yi Jine0833302017-10-23 15:42:44 -0700154#endif // ANDROID_UTIL_PROTOOUTPUT_STREAM_H