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 | |
| 38 | const uint64_t FIELD_TYPE_UNKNOWN = 0; |
| 39 | const uint64_t FIELD_TYPE_DOUBLE = 1ULL << FIELD_TYPE_SHIFT; // double, exactly eight bytes on the wire. |
| 40 | const uint64_t FIELD_TYPE_FLOAT = 2ULL << FIELD_TYPE_SHIFT; // float, exactly four bytes on the wire. |
| 41 | const 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. |
| 44 | const uint64_t FIELD_TYPE_UINT64 = 4ULL << FIELD_TYPE_SHIFT; // uint64, varint on the wire. |
| 45 | const 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. |
| 48 | const uint64_t FIELD_TYPE_FIXED64 = 6ULL << FIELD_TYPE_SHIFT; // uint64, exactly eight bytes on the wire. |
| 49 | const uint64_t FIELD_TYPE_FIXED32 = 7ULL << FIELD_TYPE_SHIFT; // uint32, exactly four bytes on the wire. |
| 50 | const uint64_t FIELD_TYPE_BOOL = 8ULL << FIELD_TYPE_SHIFT; // bool, varint on the wire. |
| 51 | const uint64_t FIELD_TYPE_STRING = 9ULL << FIELD_TYPE_SHIFT; // UTF-8 text. |
| 52 | const uint64_t FIELD_TYPE_GROUP = 10ULL << FIELD_TYPE_SHIFT; // Tag-delimited message. Deprecated. |
| 53 | const uint64_t FIELD_TYPE_MESSAGE = 11ULL << FIELD_TYPE_SHIFT; // Length-delimited message. |
| 54 | |
| 55 | const uint64_t FIELD_TYPE_BYTES = 12ULL << FIELD_TYPE_SHIFT; // Arbitrary byte array. |
| 56 | const uint64_t FIELD_TYPE_UINT32 = 13ULL << FIELD_TYPE_SHIFT; // uint32, varint on the wire |
| 57 | const uint64_t FIELD_TYPE_ENUM = 14ULL << FIELD_TYPE_SHIFT; // Enum, varint on the wire |
| 58 | const uint64_t FIELD_TYPE_SFIXED32 = 15ULL << FIELD_TYPE_SHIFT; // int32, exactly four bytes on the wire |
| 59 | const uint64_t FIELD_TYPE_SFIXED64 = 16ULL << FIELD_TYPE_SHIFT; // int64, exactly eight bytes on the wire |
| 60 | const uint64_t FIELD_TYPE_SINT32 = 17ULL << FIELD_TYPE_SHIFT; // int32, ZigZag-encoded varint on the wire |
| 61 | const 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 | // |
| 67 | const uint64_t FIELD_COUNT_SHIFT = 40; |
| 68 | const uint64_t FIELD_COUNT_MASK = 0x0fULL << FIELD_COUNT_SHIFT; |
| 69 | const uint64_t FIELD_COUNT_UNKNOWN = 0; |
| 70 | const uint64_t FIELD_COUNT_SINGLE = 1ULL << FIELD_COUNT_SHIFT; |
| 71 | const uint64_t FIELD_COUNT_REPEATED = 2ULL << FIELD_COUNT_SHIFT; |
| 72 | const uint64_t FIELD_COUNT_PACKED = 4ULL << FIELD_COUNT_SHIFT; |
| 73 | |
| 74 | /** |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 75 | * 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 | */ |
| 83 | class ProtoOutputStream |
| 84 | { |
| 85 | public: |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 86 | ProtoOutputStream(); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 87 | ~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 Jin | e083330 | 2017-10-23 15:42:44 -0700 | [diff] [blame] | 98 | bool write(uint64_t fieldId, const char* val, size_t size); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 99 | |
| 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 Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame^] | 109 | * 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 Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 111 | */ |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame^] | 112 | 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 Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 115 | |
Yi Jin | 8ad1938 | 2017-10-30 16:07:20 -0700 | [diff] [blame] | 116 | // 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] | 117 | void writeRawVarint(uint64_t varint); |
| 118 | void writeLengthDelimitedHeader(uint32_t id, size_t size); |
| 119 | void writeRawByte(uint8_t byte); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 120 | |
| 121 | private: |
| 122 | EncodedBuffer mBuffer; |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 123 | 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 Jin | 8ad1938 | 2017-10-30 16:07:20 -0700 | [diff] [blame] | 144 | inline void writeMessageBytesImpl(uint32_t id, const char* val, size_t size); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 145 | |
| 146 | bool compact(); |
| 147 | size_t editEncodedSize(size_t rawSize); |
| 148 | bool compactSize(size_t rawSize); |
| 149 | }; |
| 150 | |
| 151 | } |
| 152 | } |
| 153 | |
Yi Jin | e083330 | 2017-10-23 15:42:44 -0700 | [diff] [blame] | 154 | #endif // ANDROID_UTIL_PROTOOUTPUT_STREAM_H |