The Android Open Source Project | cf31fe9 | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | # Protocol Buffers - Google's data interchange format |
| 2 | # Copyright 2008 Google Inc. All rights reserved. |
| 3 | # http://code.google.com/p/protobuf/ |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | """Constants and static functions to support protocol buffer wire format.""" |
| 32 | |
| 33 | __author__ = 'robinson@google.com (Will Robinson)' |
| 34 | |
| 35 | import struct |
| 36 | from froofle.protobuf import message |
| 37 | |
| 38 | |
| 39 | TAG_TYPE_BITS = 3 # Number of bits used to hold type info in a proto tag. |
| 40 | _TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1 # 0x7 |
| 41 | |
| 42 | # These numbers identify the wire type of a protocol buffer value. |
| 43 | # We use the least-significant TAG_TYPE_BITS bits of the varint-encoded |
| 44 | # tag-and-type to store one of these WIRETYPE_* constants. |
| 45 | # These values must match WireType enum in //net/proto2/public/wire_format.h. |
| 46 | WIRETYPE_VARINT = 0 |
| 47 | WIRETYPE_FIXED64 = 1 |
| 48 | WIRETYPE_LENGTH_DELIMITED = 2 |
| 49 | WIRETYPE_START_GROUP = 3 |
| 50 | WIRETYPE_END_GROUP = 4 |
| 51 | WIRETYPE_FIXED32 = 5 |
| 52 | _WIRETYPE_MAX = 5 |
| 53 | |
| 54 | |
| 55 | # Bounds for various integer types. |
| 56 | INT32_MAX = int((1 << 31) - 1) |
| 57 | INT32_MIN = int(-(1 << 31)) |
| 58 | UINT32_MAX = (1 << 32) - 1 |
| 59 | |
| 60 | INT64_MAX = (1 << 63) - 1 |
| 61 | INT64_MIN = -(1 << 63) |
| 62 | UINT64_MAX = (1 << 64) - 1 |
| 63 | |
| 64 | # "struct" format strings that will encode/decode the specified formats. |
| 65 | FORMAT_UINT32_LITTLE_ENDIAN = '<I' |
| 66 | FORMAT_UINT64_LITTLE_ENDIAN = '<Q' |
| 67 | |
| 68 | |
| 69 | # We'll have to provide alternate implementations of AppendLittleEndian*() on |
| 70 | # any architectures where these checks fail. |
| 71 | if struct.calcsize(FORMAT_UINT32_LITTLE_ENDIAN) != 4: |
| 72 | raise AssertionError('Format "I" is not a 32-bit number.') |
| 73 | if struct.calcsize(FORMAT_UINT64_LITTLE_ENDIAN) != 8: |
| 74 | raise AssertionError('Format "Q" is not a 64-bit number.') |
| 75 | |
| 76 | |
| 77 | def PackTag(field_number, wire_type): |
| 78 | """Returns an unsigned 32-bit integer that encodes the field number and |
| 79 | wire type information in standard protocol message wire format. |
| 80 | |
| 81 | Args: |
| 82 | field_number: Expected to be an integer in the range [1, 1 << 29) |
| 83 | wire_type: One of the WIRETYPE_* constants. |
| 84 | """ |
| 85 | if not 0 <= wire_type <= _WIRETYPE_MAX: |
| 86 | raise message.EncodeError('Unknown wire type: %d' % wire_type) |
| 87 | return (field_number << TAG_TYPE_BITS) | wire_type |
| 88 | |
| 89 | |
| 90 | def UnpackTag(tag): |
| 91 | """The inverse of PackTag(). Given an unsigned 32-bit number, |
| 92 | returns a (field_number, wire_type) tuple. |
| 93 | """ |
| 94 | return (tag >> TAG_TYPE_BITS), (tag & _TAG_TYPE_MASK) |
| 95 | |
| 96 | |
| 97 | def ZigZagEncode(value): |
| 98 | """ZigZag Transform: Encodes signed integers so that they can be |
| 99 | effectively used with varint encoding. See wire_format.h for |
| 100 | more details. |
| 101 | """ |
| 102 | if value >= 0: |
| 103 | return value << 1 |
| 104 | return (value << 1) ^ (~0) |
| 105 | |
| 106 | |
| 107 | def ZigZagDecode(value): |
| 108 | """Inverse of ZigZagEncode().""" |
| 109 | if not value & 0x1: |
| 110 | return value >> 1 |
| 111 | return (value >> 1) ^ (~0) |
| 112 | |
| 113 | |
| 114 | |
| 115 | # The *ByteSize() functions below return the number of bytes required to |
| 116 | # serialize "field number + type" information and then serialize the value. |
| 117 | |
| 118 | |
| 119 | def Int32ByteSize(field_number, int32): |
| 120 | return Int64ByteSize(field_number, int32) |
| 121 | |
| 122 | |
| 123 | def Int64ByteSize(field_number, int64): |
| 124 | # Have to convert to uint before calling UInt64ByteSize(). |
| 125 | return UInt64ByteSize(field_number, 0xffffffffffffffff & int64) |
| 126 | |
| 127 | |
| 128 | def UInt32ByteSize(field_number, uint32): |
| 129 | return UInt64ByteSize(field_number, uint32) |
| 130 | |
| 131 | |
| 132 | def UInt64ByteSize(field_number, uint64): |
| 133 | return _TagByteSize(field_number) + _VarUInt64ByteSizeNoTag(uint64) |
| 134 | |
| 135 | |
| 136 | def SInt32ByteSize(field_number, int32): |
| 137 | return UInt32ByteSize(field_number, ZigZagEncode(int32)) |
| 138 | |
| 139 | |
| 140 | def SInt64ByteSize(field_number, int64): |
| 141 | return UInt64ByteSize(field_number, ZigZagEncode(int64)) |
| 142 | |
| 143 | |
| 144 | def Fixed32ByteSize(field_number, fixed32): |
| 145 | return _TagByteSize(field_number) + 4 |
| 146 | |
| 147 | |
| 148 | def Fixed64ByteSize(field_number, fixed64): |
| 149 | return _TagByteSize(field_number) + 8 |
| 150 | |
| 151 | |
| 152 | def SFixed32ByteSize(field_number, sfixed32): |
| 153 | return _TagByteSize(field_number) + 4 |
| 154 | |
| 155 | |
| 156 | def SFixed64ByteSize(field_number, sfixed64): |
| 157 | return _TagByteSize(field_number) + 8 |
| 158 | |
| 159 | |
| 160 | def FloatByteSize(field_number, flt): |
| 161 | return _TagByteSize(field_number) + 4 |
| 162 | |
| 163 | |
| 164 | def DoubleByteSize(field_number, double): |
| 165 | return _TagByteSize(field_number) + 8 |
| 166 | |
| 167 | |
| 168 | def BoolByteSize(field_number, b): |
| 169 | return _TagByteSize(field_number) + 1 |
| 170 | |
| 171 | |
| 172 | def EnumByteSize(field_number, enum): |
| 173 | return UInt32ByteSize(field_number, enum) |
| 174 | |
| 175 | |
| 176 | def StringByteSize(field_number, string): |
| 177 | return BytesByteSize(field_number, string.encode('utf-8')) |
| 178 | |
| 179 | |
| 180 | def BytesByteSize(field_number, b): |
| 181 | return (_TagByteSize(field_number) |
| 182 | + _VarUInt64ByteSizeNoTag(len(b)) |
| 183 | + len(b)) |
| 184 | |
| 185 | |
| 186 | def GroupByteSize(field_number, message): |
| 187 | return (2 * _TagByteSize(field_number) # START and END group. |
| 188 | + message.ByteSize()) |
| 189 | |
| 190 | |
| 191 | def MessageByteSize(field_number, message): |
| 192 | return (_TagByteSize(field_number) |
| 193 | + _VarUInt64ByteSizeNoTag(message.ByteSize()) |
| 194 | + message.ByteSize()) |
| 195 | |
| 196 | |
| 197 | def MessageSetItemByteSize(field_number, msg): |
| 198 | # First compute the sizes of the tags. |
| 199 | # There are 2 tags for the beginning and ending of the repeated group, that |
| 200 | # is field number 1, one with field number 2 (type_id) and one with field |
| 201 | # number 3 (message). |
| 202 | total_size = (2 * _TagByteSize(1) + _TagByteSize(2) + _TagByteSize(3)) |
| 203 | |
| 204 | # Add the number of bytes for type_id. |
| 205 | total_size += _VarUInt64ByteSizeNoTag(field_number) |
| 206 | |
| 207 | message_size = msg.ByteSize() |
| 208 | |
| 209 | # The number of bytes for encoding the length of the message. |
| 210 | total_size += _VarUInt64ByteSizeNoTag(message_size) |
| 211 | |
| 212 | # The size of the message. |
| 213 | total_size += message_size |
| 214 | return total_size |
| 215 | |
| 216 | |
| 217 | # Private helper functions for the *ByteSize() functions above. |
| 218 | |
| 219 | |
| 220 | def _TagByteSize(field_number): |
| 221 | """Returns the bytes required to serialize a tag with this field number.""" |
| 222 | # Just pass in type 0, since the type won't affect the tag+type size. |
| 223 | return _VarUInt64ByteSizeNoTag(PackTag(field_number, 0)) |
| 224 | |
| 225 | |
| 226 | def _VarUInt64ByteSizeNoTag(uint64): |
| 227 | """Returns the bytes required to serialize a single varint. |
| 228 | uint64 must be unsigned. |
| 229 | """ |
| 230 | if uint64 > UINT64_MAX: |
| 231 | raise message.EncodeError('Value out of range: %d' % uint64) |
| 232 | bytes = 1 |
| 233 | while uint64 > 0x7f: |
| 234 | bytes += 1 |
| 235 | uint64 >>= 7 |
| 236 | return bytes |