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();
94 
95     /**
96      * Write APIs for dumping protobuf data. Returns true if the write succeeds.
97      */
98     bool write(uint64_t fieldId, double val);
99     bool write(uint64_t fieldId, float val);
100     bool write(uint64_t fieldId, int val);
101     bool write(uint64_t fieldId, long val);
102     bool write(uint64_t fieldId, long long val);
103     bool write(uint64_t fieldId, bool val);
104     bool write(uint64_t fieldId, std::string val);
105     bool write(uint64_t fieldId, const char* val, size_t size);
106 
107     /**
108      * Starts a sub-message write session.
109      * Returns a token of this write session.
110      * Must call end(token) exactly once when finish write this sub-message.
111      */
112     uint64_t start(uint64_t fieldId);
113     void end(uint64_t token);
114 
115     /**
116      * Returns how many bytes are buffered in ProtoOutputStream.
117      * Notice, this is not the actual(compact) size of the output data.
118      */
119     size_t bytesWritten();
120 
121     /**
122      * Flushes the protobuf data out to given fd. When the following functions are called,
123      * it is not able to write to ProtoOutputStream any more since the data is compact.
124      */
125     size_t size(); // Get the size of the serialized protobuf.
126     sp<ProtoReader> data(); // Get the reader apis of the data.
127     bool flush(int fd); // Flush data directly to a file descriptor.
128     bool serializeToString(std::string* out); // Serializes the proto to a string.
129     bool serializeToVector(std::vector<uint8_t>* out); // Serializes the proto to a vector<uint8_t>.
130 
131     /**
132      * Clears the ProtoOutputStream so the buffer can be reused instead of deallocation/allocation again.
133      */
134     void clear();
135 
136     // Please don't use the following functions to dump protos unless you are familiar with protobuf encoding.
137     void writeRawVarint(uint64_t varint);
138     void writeLengthDelimitedHeader(uint32_t id, size_t size);
139     void writeRawByte(uint8_t byte);
140 
141 private:
142     sp<EncodedBuffer> mBuffer;
143     size_t mCopyBegin;
144     bool mCompact;
145     uint32_t mDepth;
146     uint32_t mObjectId;
147     uint64_t mExpectedObjectToken;
148 
149     inline void writeDoubleImpl(uint32_t id, double val);
150     inline void writeFloatImpl(uint32_t id, float val);
151     inline void writeInt64Impl(uint32_t id, int64_t val);
152     inline void writeInt32Impl(uint32_t id, int32_t val);
153     inline void writeUint64Impl(uint32_t id, uint64_t val);
154     inline void writeUint32Impl(uint32_t id, uint32_t val);
155     inline void writeFixed64Impl(uint32_t id, uint64_t val);
156     inline void writeFixed32Impl(uint32_t id, uint32_t val);
157     inline void writeSFixed64Impl(uint32_t id, int64_t val);
158     inline void writeSFixed32Impl(uint32_t id, int32_t val);
159     inline void writeZigzagInt64Impl(uint32_t id, int64_t val);
160     inline void writeZigzagInt32Impl(uint32_t id, int32_t val);
161     inline void writeEnumImpl(uint32_t id, int val);
162     inline void writeBoolImpl(uint32_t id, bool val);
163     inline void writeUtf8StringImpl(uint32_t id, const char* val, size_t size);
164     inline void writeMessageBytesImpl(uint32_t id, const char* val, size_t size);
165 
166     bool compact();
167     size_t editEncodedSize(size_t rawSize);
168     bool compactSize(size_t rawSize);
169 
170     template<typename T>
171     bool internalWrite(uint64_t fieldId, T val, const char* typeName);
172 };
173 
174 }
175 }
176 
177 #endif // ANDROID_UTIL_PROTOOUTPUT_STREAM_H
178