blob: 52830d397772afbcc8e18c63ea189191bb544c5a [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
Yi Jinf9ed04b2017-10-20 16:17:58 -070038/**
39 * The types are copied from external/protobuf/src/google/protobuf/descriptor.h directly,
40 * so no extra mapping needs to be maintained in this case.
41 */
Yi Jin04625ad2017-10-17 18:29:33 -070042const uint64_t FIELD_TYPE_UNKNOWN = 0;
43const uint64_t FIELD_TYPE_DOUBLE = 1ULL << FIELD_TYPE_SHIFT; // double, exactly eight bytes on the wire.
44const uint64_t FIELD_TYPE_FLOAT = 2ULL << FIELD_TYPE_SHIFT; // float, exactly four bytes on the wire.
45const uint64_t FIELD_TYPE_INT64 = 3ULL << FIELD_TYPE_SHIFT; // int64, varint on the wire. Negative numbers
46 // take 10 bytes. Use TYPE_SINT64 if negative
47 // values are likely.
48const uint64_t FIELD_TYPE_UINT64 = 4ULL << FIELD_TYPE_SHIFT; // uint64, varint on the wire.
49const uint64_t FIELD_TYPE_INT32 = 5ULL << FIELD_TYPE_SHIFT; // int32, varint on the wire. Negative numbers
50 // take 10 bytes. Use TYPE_SINT32 if negative
51 // values are likely.
52const uint64_t FIELD_TYPE_FIXED64 = 6ULL << FIELD_TYPE_SHIFT; // uint64, exactly eight bytes on the wire.
53const uint64_t FIELD_TYPE_FIXED32 = 7ULL << FIELD_TYPE_SHIFT; // uint32, exactly four bytes on the wire.
54const uint64_t FIELD_TYPE_BOOL = 8ULL << FIELD_TYPE_SHIFT; // bool, varint on the wire.
55const uint64_t FIELD_TYPE_STRING = 9ULL << FIELD_TYPE_SHIFT; // UTF-8 text.
Yi Jinf9ed04b2017-10-20 16:17:58 -070056// const uint64_t FIELD_TYPE_GROUP = 10ULL << FIELD_TYPE_SHIFT; // Tag-delimited message. Deprecated.
Yi Jin04625ad2017-10-17 18:29:33 -070057const uint64_t FIELD_TYPE_MESSAGE = 11ULL << FIELD_TYPE_SHIFT; // Length-delimited message.
58
59const uint64_t FIELD_TYPE_BYTES = 12ULL << FIELD_TYPE_SHIFT; // Arbitrary byte array.
60const uint64_t FIELD_TYPE_UINT32 = 13ULL << FIELD_TYPE_SHIFT; // uint32, varint on the wire
61const uint64_t FIELD_TYPE_ENUM = 14ULL << FIELD_TYPE_SHIFT; // Enum, varint on the wire
62const uint64_t FIELD_TYPE_SFIXED32 = 15ULL << FIELD_TYPE_SHIFT; // int32, exactly four bytes on the wire
63const uint64_t FIELD_TYPE_SFIXED64 = 16ULL << FIELD_TYPE_SHIFT; // int64, exactly eight bytes on the wire
64const uint64_t FIELD_TYPE_SINT32 = 17ULL << FIELD_TYPE_SHIFT; // int32, ZigZag-encoded varint on the wire
65const uint64_t FIELD_TYPE_SINT64 = 18ULL << FIELD_TYPE_SHIFT; // int64, ZigZag-encoded varint on the wire
66
67//
68// FieldId flags for whether the field is single, repeated or packed.
69// TODO: packed is not supported yet.
70//
71const uint64_t FIELD_COUNT_SHIFT = 40;
72const uint64_t FIELD_COUNT_MASK = 0x0fULL << FIELD_COUNT_SHIFT;
73const uint64_t FIELD_COUNT_UNKNOWN = 0;
74const uint64_t FIELD_COUNT_SINGLE = 1ULL << FIELD_COUNT_SHIFT;
75const uint64_t FIELD_COUNT_REPEATED = 2ULL << FIELD_COUNT_SHIFT;
Yi Jinf9ed04b2017-10-20 16:17:58 -070076const uint64_t FIELD_COUNT_PACKED = 5ULL << FIELD_COUNT_SHIFT;
Yi Jin04625ad2017-10-17 18:29:33 -070077
78/**
Yi Jin974a9c22017-10-02 18:37:08 -070079 * Class to write to a protobuf stream.
80 *
81 * Each write method takes an ID code from the protoc generated classes
82 * and the value to write. To make a nested object, call start
83 * and then end when you are done.
84 *
85 * See the java version implementation (ProtoOutputStream.java) for more infos.
86 */
87class ProtoOutputStream
88{
89public:
Yi Jin42711a02017-10-11 18:20:24 -070090 ProtoOutputStream();
Yi Jin974a9c22017-10-02 18:37:08 -070091 ~ProtoOutputStream();
92
93 /**
94 * Write APIs for dumping protobuf data. Returns true if the write succeeds.
95 */
96 bool write(uint64_t fieldId, double val);
97 bool write(uint64_t fieldId, float val);
98 bool write(uint64_t fieldId, int val);
99 bool write(uint64_t fieldId, long long val);
100 bool write(uint64_t fieldId, bool val);
101 bool write(uint64_t fieldId, std::string val);
Yi Jine0833302017-10-23 15:42:44 -0700102 bool write(uint64_t fieldId, const char* val, size_t size);
Yi Jin974a9c22017-10-02 18:37:08 -0700103
104 /**
105 * Starts a sub-message write session.
106 * Returns a token of this write session.
107 * Must call end(token) when finish write this sub-message.
108 */
109 long long start(uint64_t fieldId);
110 void end(long long token);
111
112 /**
Yi Jin0abdfb02017-11-16 15:32:27 -0800113 * Returns how many bytes are buffered in ProtoOutputStream.
114 * Notice, this is not the actual(compact) size of the output data.
115 */
116 size_t bytesWritten();
117
118 /**
Yi Jin04625ad2017-10-17 18:29:33 -0700119 * Flushes the protobuf data out to given fd. When the following functions are called,
120 * it is not able to write to ProtoOutputStream any more since the data is compact.
Yi Jin974a9c22017-10-02 18:37:08 -0700121 */
Yi Jin04625ad2017-10-17 18:29:33 -0700122 size_t size(); // Get the size of the serialized protobuf.
123 EncodedBuffer::iterator data(); // Get the reader apis of the data.
124 bool flush(int fd); // Flush data directly to a file descriptor.
Yi Jin42711a02017-10-11 18:20:24 -0700125
Yi Jin7f9e63b2018-02-02 16:25:11 -0800126 /**
127 * Clears the ProtoOutputStream so the buffer can be reused instead of deallocation/allocation again.
128 */
129 void clear();
130
Yi Jin8ad19382017-10-30 16:07:20 -0700131 // Please don't use the following functions to dump protos unless you are familiar with protobuf encoding.
Yi Jin42711a02017-10-11 18:20:24 -0700132 void writeRawVarint(uint64_t varint);
133 void writeLengthDelimitedHeader(uint32_t id, size_t size);
134 void writeRawByte(uint8_t byte);
Yi Jin974a9c22017-10-02 18:37:08 -0700135
136private:
137 EncodedBuffer mBuffer;
Yi Jin974a9c22017-10-02 18:37:08 -0700138 size_t mCopyBegin;
139 bool mCompact;
140 int mDepth;
141 int mObjectId;
142 long long mExpectedObjectToken;
143
144 inline void writeDoubleImpl(uint32_t id, double val);
145 inline void writeFloatImpl(uint32_t id, float val);
146 inline void writeInt64Impl(uint32_t id, long long val);
147 inline void writeInt32Impl(uint32_t id, int val);
148 inline void writeUint64Impl(uint32_t id, uint64_t val);
149 inline void writeUint32Impl(uint32_t id, uint32_t val);
150 inline void writeFixed64Impl(uint32_t id, uint64_t val);
151 inline void writeFixed32Impl(uint32_t id, uint32_t val);
152 inline void writeSFixed64Impl(uint32_t id, long long val);
153 inline void writeSFixed32Impl(uint32_t id, int val);
154 inline void writeZigzagInt64Impl(uint32_t id, long long val);
155 inline void writeZigzagInt32Impl(uint32_t id, int val);
156 inline void writeEnumImpl(uint32_t id, int val);
157 inline void writeBoolImpl(uint32_t id, bool val);
158 inline void writeUtf8StringImpl(uint32_t id, const char* val, size_t size);
Yi Jin8ad19382017-10-30 16:07:20 -0700159 inline void writeMessageBytesImpl(uint32_t id, const char* val, size_t size);
Yi Jin974a9c22017-10-02 18:37:08 -0700160
161 bool compact();
162 size_t editEncodedSize(size_t rawSize);
163 bool compactSize(size_t rawSize);
164};
165
166}
167}
168
Yi Jine0833302017-10-23 15:42:44 -0700169#endif // ANDROID_UTIL_PROTOOUTPUT_STREAM_H