1 /*
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 <cstdint>
21 #include <string>
22 #include <vector>
23 
24 #include <android/util/EncodedBuffer.h>
25 
26 namespace android {
27 namespace util {
28 
29 /**
30  * Position of the field type in a 64-bits fieldId.
31  */
32 const uint64_t FIELD_TYPE_SHIFT = 32;
33 
34 /**
35  * Mask for the field types stored in a fieldId.  Leaves a whole
36  * byte for future expansion, even though there are currently only 17 types.
37  */
38 const uint64_t FIELD_TYPE_MASK = 0x0ffULL << FIELD_TYPE_SHIFT;
39 
40 /**
41  * The types are copied from external/protobuf/src/google/protobuf/descriptor.h directly,
42  * so no extra mapping needs to be maintained in this case.
43  */
44 const uint64_t FIELD_TYPE_UNKNOWN  = 0;
45 const uint64_t FIELD_TYPE_DOUBLE   = 1ULL << FIELD_TYPE_SHIFT;   // double, exactly eight bytes on the wire.
46 const uint64_t FIELD_TYPE_FLOAT    = 2ULL << FIELD_TYPE_SHIFT;   // float, exactly four bytes on the wire.
47 const uint64_t FIELD_TYPE_INT64    = 3ULL << FIELD_TYPE_SHIFT;   // int64, varint on the wire.  Negative numbers
48                                                                  // take 10 bytes.  Use TYPE_SINT64 if negative
49                                                                  // values are likely.
50 const uint64_t FIELD_TYPE_UINT64   = 4ULL << FIELD_TYPE_SHIFT;   // uint64, varint on the wire.
51 const uint64_t FIELD_TYPE_INT32    = 5ULL << FIELD_TYPE_SHIFT;   // int32, varint on the wire.  Negative numbers
52                                                                  // take 10 bytes.  Use TYPE_SINT32 if negative
53                                                                  // values are likely.
54 const uint64_t FIELD_TYPE_FIXED64  = 6ULL << FIELD_TYPE_SHIFT;   // uint64, exactly eight bytes on the wire.
55 const uint64_t FIELD_TYPE_FIXED32  = 7ULL << FIELD_TYPE_SHIFT;   // uint32, exactly four bytes on the wire.
56 const uint64_t FIELD_TYPE_BOOL     = 8ULL << FIELD_TYPE_SHIFT;   // bool, varint on the wire.
57 const uint64_t FIELD_TYPE_STRING   = 9ULL << FIELD_TYPE_SHIFT;   // UTF-8 text.
58 // const uint64_t FIELD_TYPE_GROUP = 10ULL << FIELD_TYPE_SHIFT;  // Tag-delimited message.  Deprecated.
59 const uint64_t FIELD_TYPE_MESSAGE  = 11ULL << FIELD_TYPE_SHIFT;  // Length-delimited message.
60 
61 const uint64_t FIELD_TYPE_BYTES    = 12ULL << FIELD_TYPE_SHIFT;  // Arbitrary byte array.
62 const uint64_t FIELD_TYPE_UINT32   = 13ULL << FIELD_TYPE_SHIFT;  // uint32, varint on the wire
63 const uint64_t FIELD_TYPE_ENUM     = 14ULL << FIELD_TYPE_SHIFT;  // Enum, varint on the wire
64 const uint64_t FIELD_TYPE_SFIXED32 = 15ULL << FIELD_TYPE_SHIFT;  // int32, exactly four bytes on the wire
65 const uint64_t FIELD_TYPE_SFIXED64 = 16ULL << FIELD_TYPE_SHIFT;  // int64, exactly eight bytes on the wire
66 const uint64_t FIELD_TYPE_SINT32   = 17ULL << FIELD_TYPE_SHIFT;  // int32, ZigZag-encoded varint on the wire
67 const uint64_t FIELD_TYPE_SINT64   = 18ULL << FIELD_TYPE_SHIFT;  // int64, ZigZag-encoded varint on the wire
68 
69 //
70 // FieldId flags for whether the field is single, repeated or packed.
71 // TODO: packed is not supported yet.
72 //
73 const uint64_t FIELD_COUNT_SHIFT = 40;
74 const uint64_t FIELD_COUNT_MASK = 0x0fULL << FIELD_COUNT_SHIFT;
75 const uint64_t FIELD_COUNT_UNKNOWN = 0;
76 const uint64_t FIELD_COUNT_SINGLE = 1ULL << FIELD_COUNT_SHIFT;
77 const uint64_t FIELD_COUNT_REPEATED = 2ULL << FIELD_COUNT_SHIFT;
78 const uint64_t FIELD_COUNT_PACKED = 5ULL << FIELD_COUNT_SHIFT;
79 
80 /**
81  * Class to write to a protobuf stream.
82  *
83  * Each write method takes an ID code from the protoc generated classes
84  * and the value to write.  To make a nested object, call start
85  * and then end when you are done.
86  *
87  * See the java version implementation (ProtoOutputStream.java) for more infos.
88  */
89 class ProtoOutputStream
90 {
91 public:
92     ProtoOutputStream();
93     ProtoOutputStream(sp<EncodedBuffer> buffer);
94     ~ProtoOutputStream();
95 
96     /**
97      * Write APIs for dumping protobuf data. Returns true if the write succeeds.
98      */
99     bool write(uint64_t fieldId, double val);
100     bool write(uint64_t fieldId, float val);
101     bool write(uint64_t fieldId, int val);
102     bool write(uint64_t fieldId, long val);
103     bool write(uint64_t fieldId, long long val);
104     bool write(uint64_t fieldId, bool val);
105     bool write(uint64_t fieldId, std::string val);
106     bool write(uint64_t fieldId, const char* val, size_t size);
107 
108     /**
109      * Starts a sub-message write session.
110      * Returns a token of this write session.
111      * Must call end(token) exactly once when finish write this sub-message.
112      */
113     uint64_t start(uint64_t fieldId);
114     void end(uint64_t token);
115 
116     /**
117      * Returns how many bytes are buffered in ProtoOutputStream.
118      * Notice, this is not the actual(compact) size of the output data.
119      */
120     size_t bytesWritten();
121 
122     /**
123      * Flushes the protobuf data out to given fd. When the following functions are called,
124      * it is not able to write to ProtoOutputStream any more since the data is compact.
125      */
126     size_t size(); // Get the size of the serialized protobuf.
127     sp<ProtoReader> data(); // Get the reader apis of the data.
128     bool flush(int fd); // Flush data directly to a file descriptor.
129     bool serializeToString(std::string* out); // Serializes the proto to a string.
130     bool serializeToVector(std::vector<uint8_t>* out); // Serializes the proto to a vector<uint8_t>.
131 
132     /**
133      * Clears the ProtoOutputStream so the buffer can be reused instead of deallocation/allocation again.
134      */
135     void clear();
136 
137     // Please don't use the following functions to dump protos unless you are familiar with protobuf encoding.
138     void writeRawVarint(uint64_t varint);
139     void writeLengthDelimitedHeader(uint32_t id, size_t size);
140     void writeRawByte(uint8_t byte);
141 
142 private:
143     sp<EncodedBuffer> mBuffer;
144     size_t mCopyBegin;
145     bool mCompact;
146     uint32_t mDepth;
147     uint32_t mObjectId;
148     uint64_t mExpectedObjectToken;
149 
150     inline void writeDoubleImpl(uint32_t id, double val);
151     inline void writeFloatImpl(uint32_t id, float val);
152     inline void writeInt64Impl(uint32_t id, int64_t val);
153     inline void writeInt32Impl(uint32_t id, int32_t val);
154     inline void writeUint64Impl(uint32_t id, uint64_t val);
155     inline void writeUint32Impl(uint32_t id, uint32_t val);
156     inline void writeFixed64Impl(uint32_t id, uint64_t val);
157     inline void writeFixed32Impl(uint32_t id, uint32_t val);
158     inline void writeSFixed64Impl(uint32_t id, int64_t val);
159     inline void writeSFixed32Impl(uint32_t id, int32_t val);
160     inline void writeZigzagInt64Impl(uint32_t id, int64_t val);
161     inline void writeZigzagInt32Impl(uint32_t id, int32_t val);
162     inline void writeEnumImpl(uint32_t id, int val);
163     inline void writeBoolImpl(uint32_t id, bool val);
164     inline void writeUtf8StringImpl(uint32_t id, const char* val, size_t size);
165     inline void writeMessageBytesImpl(uint32_t id, const char* val, size_t size);
166 
167     bool compact();
168     size_t editEncodedSize(size_t rawSize);
169     bool compactSize(size_t rawSize);
170 
171     template<typename T>
172     bool internalWrite(uint64_t fieldId, T val, const char* typeName);
173 };
174 
175 }
176 }
177 
178 #endif // ANDROID_UTIL_PROTOOUTPUT_STREAM_H
179