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 | #define LOG_TAG "libprotoutil" |
| 17 | |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 18 | #include <cinttypes> |
| 19 | #include <type_traits> |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 20 | |
Yi Jin | c5b71ee | 2018-04-25 16:51:40 -0700 | [diff] [blame] | 21 | #include <android-base/file.h> |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 22 | #include <android/util/protobuf.h> |
| 23 | #include <android/util/ProtoOutputStream.h> |
| 24 | #include <cutils/log.h> |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | namespace util { |
| 28 | |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 29 | ProtoOutputStream::ProtoOutputStream() |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 30 | :mBuffer(), |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 31 | mCopyBegin(0), |
| 32 | mCompact(false), |
| 33 | mDepth(0), |
| 34 | mObjectId(0), |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 35 | mExpectedObjectToken(UINT64_C(-1)) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
| 39 | ProtoOutputStream::~ProtoOutputStream() |
| 40 | { |
| 41 | } |
| 42 | |
Yi Jin | 7f9e63b | 2018-02-02 16:25:11 -0800 | [diff] [blame] | 43 | |
| 44 | void |
| 45 | ProtoOutputStream::clear() |
| 46 | { |
| 47 | mBuffer.clear(); |
| 48 | mCopyBegin = 0; |
| 49 | mCompact = false; |
| 50 | mDepth = 0; |
| 51 | mObjectId = 0; |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 52 | mExpectedObjectToken = UINT64_C(-1); |
Yi Jin | 7f9e63b | 2018-02-02 16:25:11 -0800 | [diff] [blame] | 53 | } |
| 54 | |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 55 | template<typename T> |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 56 | bool |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 57 | ProtoOutputStream::internalWrite(uint64_t fieldId, T val, const char* typeName) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 58 | { |
| 59 | if (mCompact) return false; |
| 60 | const uint32_t id = (uint32_t)fieldId; |
| 61 | switch (fieldId & FIELD_TYPE_MASK) { |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 62 | case FIELD_TYPE_DOUBLE: writeDoubleImpl(id, (double)val); break; |
| 63 | case FIELD_TYPE_FLOAT: writeFloatImpl(id, (float)val); break; |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 64 | case FIELD_TYPE_INT64: writeInt64Impl(id, (int64_t)val); break; |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 65 | case FIELD_TYPE_UINT64: writeUint64Impl(id, (uint64_t)val); break; |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 66 | case FIELD_TYPE_INT32: writeInt32Impl(id, (int32_t)val); break; |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 67 | case FIELD_TYPE_FIXED64: writeFixed64Impl(id, (uint64_t)val); break; |
| 68 | case FIELD_TYPE_FIXED32: writeFixed32Impl(id, (uint32_t)val); break; |
| 69 | case FIELD_TYPE_UINT32: writeUint32Impl(id, (uint32_t)val); break; |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 70 | case FIELD_TYPE_SFIXED32: writeSFixed32Impl(id, (int32_t)val); break; |
| 71 | case FIELD_TYPE_SFIXED64: writeSFixed64Impl(id, (int64_t)val); break; |
| 72 | case FIELD_TYPE_SINT32: writeZigzagInt32Impl(id, (int32_t)val); break; |
| 73 | case FIELD_TYPE_SINT64: writeZigzagInt64Impl(id, (int64_t)val); break; |
| 74 | case FIELD_TYPE_ENUM: |
| 75 | if (std::is_integral<T>::value) { |
| 76 | writeEnumImpl(id, (int)val); |
| 77 | } else { |
| 78 | goto unsupported; |
| 79 | } |
| 80 | break; |
| 81 | case FIELD_TYPE_BOOL: |
| 82 | if (std::is_integral<T>::value) { |
| 83 | writeBoolImpl(id, val != 0); |
| 84 | } else { |
| 85 | goto unsupported; |
| 86 | } |
| 87 | break; |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 88 | default: |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 89 | goto unsupported; |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 90 | } |
| 91 | return true; |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 92 | |
| 93 | unsupported: |
| 94 | ALOGW("Field type %" PRIu64 " is not supported when writing %s val.", |
| 95 | (fieldId & FIELD_TYPE_MASK) >> FIELD_TYPE_SHIFT, typeName); |
| 96 | return false; |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | bool |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 100 | ProtoOutputStream::write(uint64_t fieldId, double val) |
| 101 | { |
| 102 | return internalWrite(fieldId, val, "double"); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | bool |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 107 | ProtoOutputStream::write(uint64_t fieldId, float val) |
| 108 | { |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 109 | return internalWrite(fieldId, val, "float"); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool |
| 113 | ProtoOutputStream::write(uint64_t fieldId, int val) |
| 114 | { |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 115 | return internalWrite(fieldId, val, "int"); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | bool |
| 119 | ProtoOutputStream::write(uint64_t fieldId, long long val) |
| 120 | { |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 121 | return internalWrite(fieldId, val, "long long"); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | bool |
| 125 | ProtoOutputStream::write(uint64_t fieldId, bool val) |
| 126 | { |
| 127 | if (mCompact) return false; |
| 128 | const uint32_t id = (uint32_t)fieldId; |
| 129 | switch (fieldId & FIELD_TYPE_MASK) { |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 130 | case FIELD_TYPE_BOOL: |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 131 | writeBoolImpl(id, val); |
| 132 | return true; |
| 133 | default: |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 134 | ALOGW("Field type %" PRIu64 " is not supported when writing bool val.", |
| 135 | (fieldId & FIELD_TYPE_MASK) >> FIELD_TYPE_SHIFT); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | bool |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 141 | ProtoOutputStream::write(uint64_t fieldId, std::string val) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 142 | { |
| 143 | if (mCompact) return false; |
| 144 | const uint32_t id = (uint32_t)fieldId; |
| 145 | switch (fieldId & FIELD_TYPE_MASK) { |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 146 | case FIELD_TYPE_STRING: |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 147 | writeUtf8StringImpl(id, val.c_str(), val.size()); |
| 148 | return true; |
| 149 | default: |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 150 | ALOGW("Field type %" PRIu64 " is not supported when writing string val.", |
| 151 | (fieldId & FIELD_TYPE_MASK) >> FIELD_TYPE_SHIFT); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 152 | return false; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | bool |
Yi Jin | e083330 | 2017-10-23 15:42:44 -0700 | [diff] [blame] | 157 | ProtoOutputStream::write(uint64_t fieldId, const char* val, size_t size) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 158 | { |
| 159 | if (mCompact) return false; |
| 160 | const uint32_t id = (uint32_t)fieldId; |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 161 | switch (fieldId & FIELD_TYPE_MASK) { |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 162 | case FIELD_TYPE_STRING: |
| 163 | case FIELD_TYPE_BYTES: |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 164 | writeUtf8StringImpl(id, val, size); |
| 165 | return true; |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 166 | case FIELD_TYPE_MESSAGE: |
Yi Jin | 8ad1938 | 2017-10-30 16:07:20 -0700 | [diff] [blame] | 167 | // can directly write valid format of message bytes into ProtoOutputStream without calling start/end |
| 168 | writeMessageBytesImpl(id, val, size); |
| 169 | return true; |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 170 | default: |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 171 | ALOGW("Field type %" PRIu64 " is not supported when writing char[] val.", |
| 172 | (fieldId & FIELD_TYPE_MASK) >> FIELD_TYPE_SHIFT); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Make a token. |
| 179 | * Bits 61-63 - tag size (So we can go backwards later if the object had not data) |
| 180 | * - 3 bits, max value 7, max value needed 5 |
| 181 | * Bit 60 - true if the object is repeated |
| 182 | * Bits 59-51 - depth (For error checking) |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 183 | * - 9 bits, max value 511, when checking, value is masked (if we really |
| 184 | * are more than 511 levels deep) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 185 | * Bits 32-50 - objectId (For error checking) |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 186 | * - 19 bits, max value 524,287. that's a lot of objects. IDs will wrap |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 187 | * because of the overflow, and only the tokens are compared. |
| 188 | * Bits 0-31 - offset of the first size field in the buffer. |
| 189 | */ |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 190 | static uint64_t |
| 191 | makeToken(uint32_t tagSize, bool repeated, uint32_t depth, uint32_t objectId, size_t sizePos) { |
| 192 | return ((UINT64_C(0x07) & (uint64_t)tagSize) << 61) |
| 193 | | (repeated ? (UINT64_C(1) << 60) : 0) |
| 194 | | (UINT64_C(0x01ff) & (uint64_t)depth) << 51 |
| 195 | | (UINT64_C(0x07ffff) & (uint64_t)objectId) << 32 |
| 196 | | (UINT64_C(0x0ffffffff) & (uint64_t)sizePos); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Get the encoded tag size from the token. |
| 201 | */ |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 202 | static uint32_t getTagSizeFromToken(uint64_t token) { |
| 203 | return 0x7 & (token >> 61); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Get the nesting depth of startObject calls from the token. |
| 208 | */ |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 209 | static uint32_t getDepthFromToken(uint64_t token) { |
| 210 | return 0x01ff & (token >> 51); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Get the location of the childRawSize (the first 32 bit size field) in this object. |
| 215 | */ |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 216 | static uint32_t getSizePosFromToken(uint64_t token) { |
| 217 | return (uint32_t)token; |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 220 | uint64_t |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 221 | ProtoOutputStream::start(uint64_t fieldId) |
| 222 | { |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 223 | if ((fieldId & FIELD_TYPE_MASK) != FIELD_TYPE_MESSAGE) { |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 224 | ALOGE("Can't call start for non-message type field: 0x%" PRIx64, fieldId); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | uint32_t id = (uint32_t)fieldId; |
Yi Jin | 295d9b1 | 2018-02-02 14:33:20 -0800 | [diff] [blame] | 229 | size_t prevPos = mBuffer.wp()->pos(); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 230 | mBuffer.writeHeader(id, WIRE_TYPE_LENGTH_DELIMITED); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 231 | size_t sizePos = mBuffer.wp()->pos(); |
| 232 | |
| 233 | mDepth++; |
| 234 | mObjectId++; |
| 235 | mBuffer.writeRawFixed64(mExpectedObjectToken); // push previous token into stack. |
| 236 | |
Yi Jin | 295d9b1 | 2018-02-02 14:33:20 -0800 | [diff] [blame] | 237 | mExpectedObjectToken = makeToken(sizePos - prevPos, |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 238 | (bool)(fieldId & FIELD_COUNT_REPEATED), mDepth, mObjectId, sizePos); |
| 239 | return mExpectedObjectToken; |
| 240 | } |
| 241 | |
| 242 | void |
Yi Jin | 5ee0787 | 2018-03-05 18:18:27 -0800 | [diff] [blame] | 243 | ProtoOutputStream::end(uint64_t token) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 244 | { |
| 245 | if (token != mExpectedObjectToken) { |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 246 | ALOGE("Unexpected token: 0x%" PRIx64 ", should be 0x%" PRIx64, token, mExpectedObjectToken); |
Yi Jin | 18678bd | 2018-04-27 11:51:13 -0700 | [diff] [blame] | 247 | mDepth = UINT32_C(-1); // make depth invalid |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 248 | return; |
| 249 | } |
| 250 | |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 251 | uint32_t depth = getDepthFromToken(token); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 252 | if (depth != (mDepth & 0x01ff)) { |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 253 | ALOGE("Unexpected depth: %" PRIu32 ", should be %" PRIu32, depth, mDepth); |
Yi Jin | 18678bd | 2018-04-27 11:51:13 -0700 | [diff] [blame] | 254 | mDepth = UINT32_C(-1); // make depth invalid |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 255 | return; |
| 256 | } |
| 257 | mDepth--; |
| 258 | |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 259 | uint32_t sizePos = getSizePosFromToken(token); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 260 | // number of bytes written in this start-end session. |
| 261 | int childRawSize = mBuffer.wp()->pos() - sizePos - 8; |
| 262 | |
| 263 | // retrieve the old token from stack. |
| 264 | mBuffer.ep()->rewind()->move(sizePos); |
| 265 | mExpectedObjectToken = mBuffer.readRawFixed64(); |
| 266 | |
| 267 | // If raw size is larger than 0, write the negative value here to indicate a compact is needed. |
| 268 | if (childRawSize > 0) { |
| 269 | mBuffer.editRawFixed32(sizePos, -childRawSize); |
| 270 | mBuffer.editRawFixed32(sizePos+4, -1); |
| 271 | } else { |
| 272 | // reset wp which erase the header tag of the message when its size is 0. |
| 273 | mBuffer.wp()->rewind()->move(sizePos - getTagSizeFromToken(token)); |
| 274 | } |
| 275 | } |
| 276 | |
Yi Jin | 0abdfb0 | 2017-11-16 15:32:27 -0800 | [diff] [blame] | 277 | size_t |
| 278 | ProtoOutputStream::bytesWritten() |
| 279 | { |
| 280 | return mBuffer.size(); |
| 281 | } |
| 282 | |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 283 | bool |
| 284 | ProtoOutputStream::compact() { |
| 285 | if (mCompact) return true; |
| 286 | if (mDepth != 0) { |
Yi Jin | 18678bd | 2018-04-27 11:51:13 -0700 | [diff] [blame] | 287 | ALOGE("Can't compact when depth(%" PRIu32 ") is not zero. Missing or extra calls to end.", mDepth); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 288 | return false; |
| 289 | } |
| 290 | // record the size of the original buffer. |
| 291 | size_t rawBufferSize = mBuffer.size(); |
| 292 | if (rawBufferSize == 0) return true; // nothing to do if the buffer is empty; |
| 293 | |
| 294 | // reset edit pointer and recursively compute encoded size of messages. |
| 295 | mBuffer.ep()->rewind(); |
| 296 | if (editEncodedSize(rawBufferSize) == 0) { |
| 297 | ALOGE("Failed to editEncodedSize."); |
| 298 | return false; |
| 299 | } |
| 300 | |
| 301 | // reset both edit pointer and write pointer, and compact recursively. |
| 302 | mBuffer.ep()->rewind(); |
| 303 | mBuffer.wp()->rewind(); |
| 304 | if (!compactSize(rawBufferSize)) { |
| 305 | ALOGE("Failed to compactSize."); |
| 306 | return false; |
| 307 | } |
| 308 | // copy the reset to the buffer. |
| 309 | if (mCopyBegin < rawBufferSize) { |
| 310 | mBuffer.copy(mCopyBegin, rawBufferSize - mCopyBegin); |
| 311 | } |
| 312 | |
| 313 | // mark true means it is not legal to write to this ProtoOutputStream anymore |
| 314 | mCompact = true; |
| 315 | return true; |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * First compaction pass. Iterate through the data, and fill in the |
| 320 | * nested object sizes so the next pass can compact them. |
| 321 | */ |
| 322 | size_t |
| 323 | ProtoOutputStream::editEncodedSize(size_t rawSize) |
| 324 | { |
| 325 | size_t objectStart = mBuffer.ep()->pos(); |
| 326 | size_t objectEnd = objectStart + rawSize; |
| 327 | size_t encodedSize = 0; |
| 328 | int childRawSize, childEncodedSize; |
| 329 | size_t childEncodedSizePos; |
| 330 | |
| 331 | while (mBuffer.ep()->pos() < objectEnd) { |
| 332 | uint32_t tag = (uint32_t)mBuffer.readRawVarint(); |
| 333 | encodedSize += get_varint_size(tag); |
| 334 | switch (read_wire_type(tag)) { |
| 335 | case WIRE_TYPE_VARINT: |
| 336 | do { |
| 337 | encodedSize++; |
| 338 | } while ((mBuffer.readRawByte() & 0x80) != 0); |
| 339 | break; |
| 340 | case WIRE_TYPE_FIXED64: |
| 341 | encodedSize += 8; |
| 342 | mBuffer.ep()->move(8); |
| 343 | break; |
| 344 | case WIRE_TYPE_LENGTH_DELIMITED: |
| 345 | childRawSize = (int)mBuffer.readRawFixed32(); |
| 346 | childEncodedSizePos = mBuffer.ep()->pos(); |
| 347 | childEncodedSize = (int)mBuffer.readRawFixed32(); |
| 348 | if (childRawSize >= 0 && childRawSize == childEncodedSize) { |
| 349 | mBuffer.ep()->move(childRawSize); |
| 350 | } else if (childRawSize < 0 && childEncodedSize == -1){ |
| 351 | childEncodedSize = editEncodedSize(-childRawSize); |
| 352 | mBuffer.editRawFixed32(childEncodedSizePos, childEncodedSize); |
| 353 | } else { |
| 354 | ALOGE("Bad raw or encoded values: raw=%d, encoded=%d at %zu", |
| 355 | childRawSize, childEncodedSize, childEncodedSizePos); |
| 356 | return 0; |
| 357 | } |
| 358 | encodedSize += get_varint_size(childEncodedSize) + childEncodedSize; |
| 359 | break; |
| 360 | case WIRE_TYPE_FIXED32: |
| 361 | encodedSize += 4; |
| 362 | mBuffer.ep()->move(4); |
| 363 | break; |
| 364 | default: |
| 365 | ALOGE("Unexpected wire type %d in editEncodedSize at [%zu, %zu]", |
| 366 | read_wire_type(tag), objectStart, objectEnd); |
| 367 | return 0; |
| 368 | } |
| 369 | } |
| 370 | return encodedSize; |
| 371 | } |
| 372 | |
| 373 | /** |
| 374 | * Second compaction pass. Iterate through the data, and copy the data |
| 375 | * forward in the buffer, converting the pairs of uint32s into a single |
| 376 | * unsigned varint of the size. |
| 377 | */ |
| 378 | bool |
| 379 | ProtoOutputStream::compactSize(size_t rawSize) |
| 380 | { |
| 381 | size_t objectStart = mBuffer.ep()->pos(); |
| 382 | size_t objectEnd = objectStart + rawSize; |
| 383 | int childRawSize, childEncodedSize; |
| 384 | |
| 385 | while (mBuffer.ep()->pos() < objectEnd) { |
| 386 | uint32_t tag = (uint32_t)mBuffer.readRawVarint(); |
| 387 | switch (read_wire_type(tag)) { |
| 388 | case WIRE_TYPE_VARINT: |
| 389 | while ((mBuffer.readRawByte() & 0x80) != 0) {} |
| 390 | break; |
| 391 | case WIRE_TYPE_FIXED64: |
| 392 | mBuffer.ep()->move(8); |
| 393 | break; |
| 394 | case WIRE_TYPE_LENGTH_DELIMITED: |
| 395 | mBuffer.copy(mCopyBegin, mBuffer.ep()->pos() - mCopyBegin); |
| 396 | |
| 397 | childRawSize = (int)mBuffer.readRawFixed32(); |
| 398 | childEncodedSize = (int)mBuffer.readRawFixed32(); |
| 399 | mCopyBegin = mBuffer.ep()->pos(); |
| 400 | |
| 401 | // write encoded size to buffer. |
| 402 | mBuffer.writeRawVarint32(childEncodedSize); |
| 403 | if (childRawSize >= 0 && childRawSize == childEncodedSize) { |
| 404 | mBuffer.ep()->move(childEncodedSize); |
| 405 | } else if (childRawSize < 0){ |
| 406 | if (!compactSize(-childRawSize)) return false; |
| 407 | } else { |
| 408 | ALOGE("Bad raw or encoded values: raw=%d, encoded=%d", |
| 409 | childRawSize, childEncodedSize); |
| 410 | return false; |
| 411 | } |
| 412 | break; |
| 413 | case WIRE_TYPE_FIXED32: |
| 414 | mBuffer.ep()->move(4); |
| 415 | break; |
| 416 | default: |
| 417 | ALOGE("Unexpected wire type %d in compactSize at [%zu, %zu]", |
| 418 | read_wire_type(tag), objectStart, objectEnd); |
| 419 | return false; |
| 420 | } |
| 421 | } |
| 422 | return true; |
| 423 | } |
| 424 | |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 425 | size_t |
| 426 | ProtoOutputStream::size() |
| 427 | { |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 428 | if (!compact()) { |
| 429 | ALOGE("compact failed, the ProtoOutputStream data is corrupted!"); |
Yi Jin | 18678bd | 2018-04-27 11:51:13 -0700 | [diff] [blame] | 430 | return 0; |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 431 | } |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 432 | return mBuffer.size(); |
| 433 | } |
| 434 | |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 435 | bool |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 436 | ProtoOutputStream::flush(int fd) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 437 | { |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 438 | if (fd < 0) return false; |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 439 | if (!compact()) return false; |
| 440 | |
| 441 | EncodedBuffer::iterator it = mBuffer.begin(); |
| 442 | while (it.readBuffer() != NULL) { |
Yi Jin | c5b71ee | 2018-04-25 16:51:40 -0700 | [diff] [blame] | 443 | if (!android::base::WriteFully(fd, it.readBuffer(), it.currentToRead())) return false; |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 444 | it.rp()->move(it.currentToRead()); |
| 445 | } |
| 446 | return true; |
| 447 | } |
| 448 | |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 449 | EncodedBuffer::iterator |
| 450 | ProtoOutputStream::data() |
| 451 | { |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 452 | if (!compact()) { |
| 453 | ALOGE("compact failed, the ProtoOutputStream data is corrupted!"); |
Yi Jin | 18678bd | 2018-04-27 11:51:13 -0700 | [diff] [blame] | 454 | mBuffer.clear(); |
Yi Jin | 00a7544 | 2018-03-30 11:01:58 -0700 | [diff] [blame] | 455 | } |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 456 | return mBuffer.begin(); |
| 457 | } |
| 458 | |
| 459 | void |
| 460 | ProtoOutputStream::writeRawVarint(uint64_t varint) |
| 461 | { |
| 462 | mBuffer.writeRawVarint64(varint); |
| 463 | } |
| 464 | |
| 465 | void |
| 466 | ProtoOutputStream::writeLengthDelimitedHeader(uint32_t id, size_t size) |
| 467 | { |
| 468 | mBuffer.writeHeader(id, WIRE_TYPE_LENGTH_DELIMITED); |
| 469 | // reserves 64 bits for length delimited fields, if first field is negative, compact it. |
| 470 | mBuffer.writeRawFixed32(size); |
| 471 | mBuffer.writeRawFixed32(size); |
| 472 | } |
| 473 | |
| 474 | void |
| 475 | ProtoOutputStream::writeRawByte(uint8_t byte) |
| 476 | { |
| 477 | mBuffer.writeRawByte(byte); |
| 478 | } |
| 479 | |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 480 | |
| 481 | // ========================================================================= |
| 482 | // Private functions |
| 483 | |
| 484 | /** |
| 485 | * bit_cast |
| 486 | */ |
| 487 | template <class From, class To> |
| 488 | inline To bit_cast(From const &from) { |
| 489 | To to; |
| 490 | memcpy(&to, &from, sizeof(to)); |
| 491 | return to; |
| 492 | } |
| 493 | |
| 494 | inline void |
| 495 | ProtoOutputStream::writeDoubleImpl(uint32_t id, double val) |
| 496 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 497 | mBuffer.writeHeader(id, WIRE_TYPE_FIXED64); |
| 498 | mBuffer.writeRawFixed64(bit_cast<double, uint64_t>(val)); |
| 499 | } |
| 500 | |
| 501 | inline void |
| 502 | ProtoOutputStream::writeFloatImpl(uint32_t id, float val) |
| 503 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 504 | mBuffer.writeHeader(id, WIRE_TYPE_FIXED32); |
| 505 | mBuffer.writeRawFixed32(bit_cast<float, uint32_t>(val)); |
| 506 | } |
| 507 | |
| 508 | inline void |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 509 | ProtoOutputStream::writeInt64Impl(uint32_t id, int64_t val) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 510 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 511 | mBuffer.writeHeader(id, WIRE_TYPE_VARINT); |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 512 | mBuffer.writeRawVarint64(val); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | inline void |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 516 | ProtoOutputStream::writeInt32Impl(uint32_t id, int32_t val) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 517 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 518 | mBuffer.writeHeader(id, WIRE_TYPE_VARINT); |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 519 | mBuffer.writeRawVarint32(val); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | inline void |
| 523 | ProtoOutputStream::writeUint64Impl(uint32_t id, uint64_t val) |
| 524 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 525 | mBuffer.writeHeader(id, WIRE_TYPE_VARINT); |
| 526 | mBuffer.writeRawVarint64(val); |
| 527 | } |
| 528 | |
| 529 | inline void |
| 530 | ProtoOutputStream::writeUint32Impl(uint32_t id, uint32_t val) |
| 531 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 532 | mBuffer.writeHeader(id, WIRE_TYPE_VARINT); |
| 533 | mBuffer.writeRawVarint32(val); |
| 534 | } |
| 535 | |
| 536 | inline void |
| 537 | ProtoOutputStream::writeFixed64Impl(uint32_t id, uint64_t val) |
| 538 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 539 | mBuffer.writeHeader(id, WIRE_TYPE_FIXED64); |
| 540 | mBuffer.writeRawFixed64(val); |
| 541 | } |
| 542 | |
| 543 | inline void |
| 544 | ProtoOutputStream::writeFixed32Impl(uint32_t id, uint32_t val) |
| 545 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 546 | mBuffer.writeHeader(id, WIRE_TYPE_FIXED32); |
| 547 | mBuffer.writeRawFixed32(val); |
| 548 | } |
| 549 | |
| 550 | inline void |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 551 | ProtoOutputStream::writeSFixed64Impl(uint32_t id, int64_t val) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 552 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 553 | mBuffer.writeHeader(id, WIRE_TYPE_FIXED64); |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 554 | mBuffer.writeRawFixed64(val); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | inline void |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 558 | ProtoOutputStream::writeSFixed32Impl(uint32_t id, int32_t val) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 559 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 560 | mBuffer.writeHeader(id, WIRE_TYPE_FIXED32); |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 561 | mBuffer.writeRawFixed32(val); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | inline void |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 565 | ProtoOutputStream::writeZigzagInt64Impl(uint32_t id, int64_t val) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 566 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 567 | mBuffer.writeHeader(id, WIRE_TYPE_VARINT); |
| 568 | mBuffer.writeRawVarint64((val << 1) ^ (val >> 63)); |
| 569 | } |
| 570 | |
| 571 | inline void |
Yi Jin | fd2a4c7 | 2018-04-26 11:49:08 -0700 | [diff] [blame] | 572 | ProtoOutputStream::writeZigzagInt32Impl(uint32_t id, int32_t val) |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 573 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 574 | mBuffer.writeHeader(id, WIRE_TYPE_VARINT); |
| 575 | mBuffer.writeRawVarint32((val << 1) ^ (val >> 31)); |
| 576 | } |
| 577 | |
| 578 | inline void |
| 579 | ProtoOutputStream::writeEnumImpl(uint32_t id, int val) |
| 580 | { |
| 581 | mBuffer.writeHeader(id, WIRE_TYPE_VARINT); |
| 582 | mBuffer.writeRawVarint32((uint32_t) val); |
| 583 | } |
| 584 | |
| 585 | inline void |
| 586 | ProtoOutputStream::writeBoolImpl(uint32_t id, bool val) |
| 587 | { |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 588 | mBuffer.writeHeader(id, WIRE_TYPE_VARINT); |
| 589 | mBuffer.writeRawVarint32(val ? 1 : 0); |
| 590 | } |
| 591 | |
| 592 | inline void |
| 593 | ProtoOutputStream::writeUtf8StringImpl(uint32_t id, const char* val, size_t size) |
| 594 | { |
Yi Jin | 04625ad | 2017-10-17 18:29:33 -0700 | [diff] [blame] | 595 | if (val == NULL) return; |
Yi Jin | 42711a0 | 2017-10-11 18:20:24 -0700 | [diff] [blame] | 596 | writeLengthDelimitedHeader(id, size); |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 597 | for (size_t i=0; i<size; i++) { |
| 598 | mBuffer.writeRawByte((uint8_t)val[i]); |
| 599 | } |
| 600 | } |
| 601 | |
Yi Jin | 8ad1938 | 2017-10-30 16:07:20 -0700 | [diff] [blame] | 602 | inline void |
| 603 | ProtoOutputStream::writeMessageBytesImpl(uint32_t id, const char* val, size_t size) |
| 604 | { |
| 605 | if (val == NULL) return; |
| 606 | writeLengthDelimitedHeader(id, size); |
| 607 | for (size_t i=0; i<size; i++) { |
| 608 | mBuffer.writeRawByte(val[i]); |
| 609 | } |
| 610 | } |
| 611 | |
Yi Jin | 974a9c2 | 2017-10-02 18:37:08 -0700 | [diff] [blame] | 612 | } // util |
| 613 | } // android |
| 614 | |