Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 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 <android/util/EncodedBuffer.h> |
| 21 | |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 22 | #include <string> |
| 23 | |
| 24 | namespace android { |
| 25 | namespace util { |
| 26 | |
| 27 | /** |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 28 | * Position of the field type in a (long long) fieldId. |
| 29 | */ |
| 30 | const 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 | */ |
| 36 | const uint64_t FIELD_TYPE_MASK = 0x0ffULL << FIELD_TYPE_SHIFT; |
| 37 | |
Yi Jin | f9ed04b | 2017-10-20 16:17:58 -0700 | [diff] [blame^] | 38 | /** |
| 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 Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 42 | const uint64_t FIELD_TYPE_UNKNOWN = 0; |
| 43 | const uint64_t FIELD_TYPE_DOUBLE = 1ULL << FIELD_TYPE_SHIFT; // double, exactly eight bytes on the wire. |
| 44 | const uint64_t FIELD_TYPE_FLOAT = 2ULL << FIELD_TYPE_SHIFT; // float, exactly four bytes on the wire. |
| 45 | const 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. |
| 48 | const uint64_t FIELD_TYPE_UINT64 = 4ULL << FIELD_TYPE_SHIFT; // uint64, varint on the wire. |
| 49 | const 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. |
| 52 | const uint64_t FIELD_TYPE_FIXED64 = 6ULL << FIELD_TYPE_SHIFT; // uint64, exactly eight bytes on the wire. |
| 53 | const uint64_t FIELD_TYPE_FIXED32 = 7ULL << FIELD_TYPE_SHIFT; // uint32, exactly four bytes on the wire. |
| 54 | const uint64_t FIELD_TYPE_BOOL = 8ULL << FIELD_TYPE_SHIFT; // bool, varint on the wire. |
| 55 | const uint64_t FIELD_TYPE_STRING = 9ULL << FIELD_TYPE_SHIFT; // UTF-8 text. |
Yi Jin | f9ed04b | 2017-10-20 16:17:58 -0700 | [diff] [blame^] | 56 | // const uint64_t FIELD_TYPE_GROUP = 10ULL << FIELD_TYPE_SHIFT; // Tag-delimited message. Deprecated. |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 57 | const uint64_t FIELD_TYPE_MESSAGE = 11ULL << FIELD_TYPE_SHIFT; // Length-delimited message. |
| 58 | |
| 59 | const uint64_t FIELD_TYPE_BYTES = 12ULL << FIELD_TYPE_SHIFT; // Arbitrary byte array. |
| 60 | const uint64_t FIELD_TYPE_UINT32 = 13ULL << FIELD_TYPE_SHIFT; // uint32, varint on the wire |
| 61 | const uint64_t FIELD_TYPE_ENUM = 14ULL << FIELD_TYPE_SHIFT; // Enum, varint on the wire |
| 62 | const uint64_t FIELD_TYPE_SFIXED32 = 15ULL << FIELD_TYPE_SHIFT; // int32, exactly four bytes on the wire |
| 63 | const uint64_t FIELD_TYPE_SFIXED64 = 16ULL << FIELD_TYPE_SHIFT; // int64, exactly eight bytes on the wire |
| 64 | const uint64_t FIELD_TYPE_SINT32 = 17ULL << FIELD_TYPE_SHIFT; // int32, ZigZag-encoded varint on the wire |
| 65 | const 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 | // |
| 71 | const uint64_t FIELD_COUNT_SHIFT = 40; |
| 72 | const uint64_t FIELD_COUNT_MASK = 0x0fULL << FIELD_COUNT_SHIFT; |
| 73 | const uint64_t FIELD_COUNT_UNKNOWN = 0; |
| 74 | const uint64_t FIELD_COUNT_SINGLE = 1ULL << FIELD_COUNT_SHIFT; |
| 75 | const uint64_t FIELD_COUNT_REPEATED = 2ULL << FIELD_COUNT_SHIFT; |
Yi Jin | f9ed04b | 2017-10-20 16:17:58 -0700 | [diff] [blame^] | 76 | const uint64_t FIELD_COUNT_PACKED = 5ULL << FIELD_COUNT_SHIFT; |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 77 | |
| 78 | /** |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 79 | * 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 | */ |
| 87 | class ProtoOutputStream |
| 88 | { |
| 89 | public: |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 90 | ProtoOutputStream(); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 91 | ~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 Jin | e083330 | 2017-10-23 15:42:44 -0700 | [diff] [blame] | 102 | bool write(uint64_t fieldId, const char* val, size_t size); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 103 | |
| 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 Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 113 | * Flushes the protobuf data out to given fd. When the following functions are called, |
| 114 | * it is not able to write to ProtoOutputStream any more since the data is compact. |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 115 | */ |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 116 | size_t size(); // Get the size of the serialized protobuf. |
| 117 | EncodedBuffer::iterator data(); // Get the reader apis of the data. |
| 118 | bool flush(int fd); // Flush data directly to a file descriptor. |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 119 | |
Yi Jin | 8ad1938 | 2017-10-30 16:07:20 -0700 | [diff] [blame] | 120 | // Please don't use the following functions to dump protos unless you are familiar with protobuf encoding. |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 121 | void writeRawVarint(uint64_t varint); |
| 122 | void writeLengthDelimitedHeader(uint32_t id, size_t size); |
| 123 | void writeRawByte(uint8_t byte); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 124 | |
| 125 | private: |
| 126 | EncodedBuffer mBuffer; |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 127 | size_t mCopyBegin; |
| 128 | bool mCompact; |
| 129 | int mDepth; |
| 130 | int mObjectId; |
| 131 | long long mExpectedObjectToken; |
| 132 | |
| 133 | inline void writeDoubleImpl(uint32_t id, double val); |
| 134 | inline void writeFloatImpl(uint32_t id, float val); |
| 135 | inline void writeInt64Impl(uint32_t id, long long val); |
| 136 | inline void writeInt32Impl(uint32_t id, int val); |
| 137 | inline void writeUint64Impl(uint32_t id, uint64_t val); |
| 138 | inline void writeUint32Impl(uint32_t id, uint32_t val); |
| 139 | inline void writeFixed64Impl(uint32_t id, uint64_t val); |
| 140 | inline void writeFixed32Impl(uint32_t id, uint32_t val); |
| 141 | inline void writeSFixed64Impl(uint32_t id, long long val); |
| 142 | inline void writeSFixed32Impl(uint32_t id, int val); |
| 143 | inline void writeZigzagInt64Impl(uint32_t id, long long val); |
| 144 | inline void writeZigzagInt32Impl(uint32_t id, int val); |
| 145 | inline void writeEnumImpl(uint32_t id, int val); |
| 146 | inline void writeBoolImpl(uint32_t id, bool val); |
| 147 | inline void writeUtf8StringImpl(uint32_t id, const char* val, size_t size); |
Yi Jin | 8ad1938 | 2017-10-30 16:07:20 -0700 | [diff] [blame] | 148 | inline void writeMessageBytesImpl(uint32_t id, const char* val, size_t size); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 149 | |
| 150 | bool compact(); |
| 151 | size_t editEncodedSize(size_t rawSize); |
| 152 | bool compactSize(size_t rawSize); |
| 153 | }; |
| 154 | |
| 155 | } |
| 156 | } |
| 157 | |
Yi Jin | e083330 | 2017-10-23 15:42:44 -0700 | [diff] [blame] | 158 | #endif // ANDROID_UTIL_PROTOOUTPUT_STREAM_H |