1# Protocol Buffers - Google's data interchange format
2# Copyright 2008 Google Inc. All rights reserved.
3#
4# Use of this source code is governed by a BSD-style
5# license that can be found in the LICENSE file or at
6# https://developers.google.com/open-source/licenses/bsd
7"""Constants and static functions to support protocol buffer wire format."""
8
9__author__ = 'robinson@google.com (Will Robinson)'
10
11import struct
12from google.protobuf import descriptor
13from google.protobuf import message
14
15TAG_TYPE_BITS = 3 # Number of bits used to hold type info in a proto tag.
16TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1 # 0x7
17
18# These numbers identify the wire type of a protocol buffer value.
19# We use the least-significant TAG_TYPE_BITS bits of the varint-encoded
20# tag-and-type to store one of these WIRETYPE_* constants.
21# These values must match WireType enum in //google/protobuf/wire_format.h.
22WIRETYPE_VARINT = 0
23WIRETYPE_FIXED64 = 1
24WIRETYPE_LENGTH_DELIMITED = 2
25WIRETYPE_START_GROUP = 3
26WIRETYPE_END_GROUP = 4
27WIRETYPE_FIXED32 = 5
28_WIRETYPE_MAX = 5
29
30# Bounds for various integer types.
31INT32_MAX = int((1 << 31) - 1)
32INT32_MIN = int(-(1 << 31))
33UINT32_MAX = (1 << 32) - 1
34
35INT64_MAX = (1 << 63) - 1
36INT64_MIN = -(1 << 63)
37UINT64_MAX = (1 << 64) - 1
38
39# "struct" format strings that will encode/decode the specified formats.
40FORMAT_UINT32_LITTLE_ENDIAN = '<I'
41FORMAT_UINT64_LITTLE_ENDIAN = '<Q'
42FORMAT_FLOAT_LITTLE_ENDIAN = '<f'
43FORMAT_DOUBLE_LITTLE_ENDIAN = '<d'
44
45# We'll have to provide alternate implementations of AppendLittleEndian*() on
46# any architectures where these checks fail.
47if struct.calcsize(FORMAT_UINT32_LITTLE_ENDIAN) != 4:
48 raise AssertionError('Format "I" is not a 32-bit number.')
49if struct.calcsize(FORMAT_UINT64_LITTLE_ENDIAN) != 8:
50 raise AssertionError('Format "Q" is not a 64-bit number.')
51
52
53def PackTag(field_number, wire_type):
54 """Returns an unsigned 32-bit integer that encodes the field number and
55
56 wire type information in standard protocol message wire format.
57
58 Args:
59 field_number: Expected to be an integer in the range [1, 1 << 29)
60 wire_type: One of the WIRETYPE_* constants.
61 """
62 if not 0 <= wire_type <= _WIRETYPE_MAX:
63 raise message.EncodeError('Unknown wire type: %d' % wire_type)
64 return (field_number << TAG_TYPE_BITS) | wire_type
65
66
67def UnpackTag(tag):
68 """The inverse of PackTag().
69
70 Given an unsigned 32-bit number, returns a (field_number, wire_type) tuple.
71 """
72 return (tag >> TAG_TYPE_BITS), (tag & TAG_TYPE_MASK)
73
74
75def ZigZagEncode(value):
76 """ZigZag Transform: Encodes signed integers so that they can be
77
78 effectively used with varint encoding. See wire_format.h for
79 more details.
80 """
81 if value >= 0:
82 return value << 1
83 return (value << 1) ^ (~0)
84
85
86def ZigZagDecode(value):
87 """Inverse of ZigZagEncode()."""
88 if not value & 0x1:
89 return value >> 1
90 return (value >> 1) ^ (~0)
91
92
93# The *ByteSize() functions below return the number of bytes required to
94# serialize "field number + type" information and then serialize the value.
95
96
97def Int32ByteSize(field_number, int32):
98 return Int64ByteSize(field_number, int32)
99
100
101def Int32ByteSizeNoTag(int32):
102 return _VarUInt64ByteSizeNoTag(0xFFFFFFFFFFFFFFFF & int32)
103
104
105def Int64ByteSize(field_number, int64):
106 # Have to convert to uint before calling UInt64ByteSize().
107 return UInt64ByteSize(field_number, 0xFFFFFFFFFFFFFFFF & int64)
108
109
110def UInt32ByteSize(field_number, uint32):
111 return UInt64ByteSize(field_number, uint32)
112
113
114def UInt64ByteSize(field_number, uint64):
115 return TagByteSize(field_number) + _VarUInt64ByteSizeNoTag(uint64)
116
117
118def SInt32ByteSize(field_number, int32):
119 return UInt32ByteSize(field_number, ZigZagEncode(int32))
120
121
122def SInt64ByteSize(field_number, int64):
123 return UInt64ByteSize(field_number, ZigZagEncode(int64))
124
125
126def Fixed32ByteSize(field_number, fixed32):
127 return TagByteSize(field_number) + 4
128
129
130def Fixed64ByteSize(field_number, fixed64):
131 return TagByteSize(field_number) + 8
132
133
134def SFixed32ByteSize(field_number, sfixed32):
135 return TagByteSize(field_number) + 4
136
137
138def SFixed64ByteSize(field_number, sfixed64):
139 return TagByteSize(field_number) + 8
140
141
142def FloatByteSize(field_number, flt):
143 return TagByteSize(field_number) + 4
144
145
146def DoubleByteSize(field_number, double):
147 return TagByteSize(field_number) + 8
148
149
150def BoolByteSize(field_number, b):
151 return TagByteSize(field_number) + 1
152
153
154def EnumByteSize(field_number, enum):
155 return UInt32ByteSize(field_number, enum)
156
157
158def StringByteSize(field_number, string):
159 return BytesByteSize(field_number, string.encode('utf-8'))
160
161
162def BytesByteSize(field_number, b):
163 return TagByteSize(field_number) + _VarUInt64ByteSizeNoTag(len(b)) + len(b)
164
165
166def GroupByteSize(field_number, message):
167 return (
168 2 * TagByteSize(field_number) + message.ByteSize() # START and END group.
169 )
170
171
172def MessageByteSize(field_number, message):
173 return (
174 TagByteSize(field_number)
175 + _VarUInt64ByteSizeNoTag(message.ByteSize())
176 + message.ByteSize()
177 )
178
179
180def MessageSetItemByteSize(field_number, msg):
181 # First compute the sizes of the tags.
182 # There are 2 tags for the beginning and ending of the repeated group, that
183 # is field number 1, one with field number 2 (type_id) and one with field
184 # number 3 (message).
185 total_size = 2 * TagByteSize(1) + TagByteSize(2) + TagByteSize(3)
186
187 # Add the number of bytes for type_id.
188 total_size += _VarUInt64ByteSizeNoTag(field_number)
189
190 message_size = msg.ByteSize()
191
192 # The number of bytes for encoding the length of the message.
193 total_size += _VarUInt64ByteSizeNoTag(message_size)
194
195 # The size of the message.
196 total_size += message_size
197 return total_size
198
199
200def TagByteSize(field_number):
201 """Returns the bytes required to serialize a tag with this field number."""
202 # Just pass in type 0, since the type won't affect the tag+type size.
203 return _VarUInt64ByteSizeNoTag(PackTag(field_number, 0))
204
205
206# Private helper function for the *ByteSize() functions above.
207
208
209def _VarUInt64ByteSizeNoTag(uint64):
210 """Returns the number of bytes required to serialize a single varint
211
212 using boundary value comparisons. (unrolled loop optimization -WPierce)
213 uint64 must be unsigned.
214 """
215 if uint64 <= 0x7F:
216 return 1
217 if uint64 <= 0x3FFF:
218 return 2
219 if uint64 <= 0x1FFFFF:
220 return 3
221 if uint64 <= 0xFFFFFFF:
222 return 4
223 if uint64 <= 0x7FFFFFFFF:
224 return 5
225 if uint64 <= 0x3FFFFFFFFFF:
226 return 6
227 if uint64 <= 0x1FFFFFFFFFFFF:
228 return 7
229 if uint64 <= 0xFFFFFFFFFFFFFF:
230 return 8
231 if uint64 <= 0x7FFFFFFFFFFFFFFF:
232 return 9
233 if uint64 > UINT64_MAX:
234 raise message.EncodeError('Value out of range: %d' % uint64)
235 return 10
236
237
238NON_PACKABLE_TYPES = (
239 descriptor.FieldDescriptor.TYPE_STRING,
240 descriptor.FieldDescriptor.TYPE_GROUP,
241 descriptor.FieldDescriptor.TYPE_MESSAGE,
242 descriptor.FieldDescriptor.TYPE_BYTES,
243)
244
245
246def IsTypePackable(field_type):
247 """Return true iff packable = true is valid for fields of this type.
248
249 Args:
250 field_type: a FieldDescriptor::Type value.
251
252 Returns:
253 True iff fields of this type are packable.
254 """
255 return field_type not in NON_PACKABLE_TYPES