Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/google/protobuf/internal/wire_format.py: 53%
105 statements
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:40 +0000
« prev ^ index » next coverage.py v7.3.2, created at 2023-12-08 06:40 +0000
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
8"""Constants and static functions to support protocol buffer wire format."""
10__author__ = 'robinson@google.com (Will Robinson)'
12import struct
13from google.protobuf import descriptor
14from google.protobuf import message
17TAG_TYPE_BITS = 3 # Number of bits used to hold type info in a proto tag.
18TAG_TYPE_MASK = (1 << TAG_TYPE_BITS) - 1 # 0x7
20# These numbers identify the wire type of a protocol buffer value.
21# We use the least-significant TAG_TYPE_BITS bits of the varint-encoded
22# tag-and-type to store one of these WIRETYPE_* constants.
23# These values must match WireType enum in //google/protobuf/wire_format.h.
24WIRETYPE_VARINT = 0
25WIRETYPE_FIXED64 = 1
26WIRETYPE_LENGTH_DELIMITED = 2
27WIRETYPE_START_GROUP = 3
28WIRETYPE_END_GROUP = 4
29WIRETYPE_FIXED32 = 5
30_WIRETYPE_MAX = 5
33# Bounds for various integer types.
34INT32_MAX = int((1 << 31) - 1)
35INT32_MIN = int(-(1 << 31))
36UINT32_MAX = (1 << 32) - 1
38INT64_MAX = (1 << 63) - 1
39INT64_MIN = -(1 << 63)
40UINT64_MAX = (1 << 64) - 1
42# "struct" format strings that will encode/decode the specified formats.
43FORMAT_UINT32_LITTLE_ENDIAN = '<I'
44FORMAT_UINT64_LITTLE_ENDIAN = '<Q'
45FORMAT_FLOAT_LITTLE_ENDIAN = '<f'
46FORMAT_DOUBLE_LITTLE_ENDIAN = '<d'
49# We'll have to provide alternate implementations of AppendLittleEndian*() on
50# any architectures where these checks fail.
51if struct.calcsize(FORMAT_UINT32_LITTLE_ENDIAN) != 4:
52 raise AssertionError('Format "I" is not a 32-bit number.')
53if struct.calcsize(FORMAT_UINT64_LITTLE_ENDIAN) != 8:
54 raise AssertionError('Format "Q" is not a 64-bit number.')
57def PackTag(field_number, wire_type):
58 """Returns an unsigned 32-bit integer that encodes the field number and
59 wire type information in standard protocol message wire format.
61 Args:
62 field_number: Expected to be an integer in the range [1, 1 << 29)
63 wire_type: One of the WIRETYPE_* constants.
64 """
65 if not 0 <= wire_type <= _WIRETYPE_MAX:
66 raise message.EncodeError('Unknown wire type: %d' % wire_type)
67 return (field_number << TAG_TYPE_BITS) | wire_type
70def UnpackTag(tag):
71 """The inverse of PackTag(). Given an unsigned 32-bit number,
72 returns a (field_number, wire_type) tuple.
73 """
74 return (tag >> TAG_TYPE_BITS), (tag & TAG_TYPE_MASK)
77def ZigZagEncode(value):
78 """ZigZag Transform: Encodes signed integers so that they can be
79 effectively used with varint encoding. See wire_format.h for
80 more details.
81 """
82 if value >= 0:
83 return value << 1
84 return (value << 1) ^ (~0)
87def ZigZagDecode(value):
88 """Inverse of ZigZagEncode()."""
89 if not value & 0x1:
90 return value >> 1
91 return (value >> 1) ^ (~0)
95# The *ByteSize() functions below return the number of bytes required to
96# serialize "field number + type" information and then serialize the value.
99def Int32ByteSize(field_number, int32):
100 return Int64ByteSize(field_number, int32)
103def Int32ByteSizeNoTag(int32):
104 return _VarUInt64ByteSizeNoTag(0xffffffffffffffff & int32)
107def Int64ByteSize(field_number, int64):
108 # Have to convert to uint before calling UInt64ByteSize().
109 return UInt64ByteSize(field_number, 0xffffffffffffffff & int64)
112def UInt32ByteSize(field_number, uint32):
113 return UInt64ByteSize(field_number, uint32)
116def UInt64ByteSize(field_number, uint64):
117 return TagByteSize(field_number) + _VarUInt64ByteSizeNoTag(uint64)
120def SInt32ByteSize(field_number, int32):
121 return UInt32ByteSize(field_number, ZigZagEncode(int32))
124def SInt64ByteSize(field_number, int64):
125 return UInt64ByteSize(field_number, ZigZagEncode(int64))
128def Fixed32ByteSize(field_number, fixed32):
129 return TagByteSize(field_number) + 4
132def Fixed64ByteSize(field_number, fixed64):
133 return TagByteSize(field_number) + 8
136def SFixed32ByteSize(field_number, sfixed32):
137 return TagByteSize(field_number) + 4
140def SFixed64ByteSize(field_number, sfixed64):
141 return TagByteSize(field_number) + 8
144def FloatByteSize(field_number, flt):
145 return TagByteSize(field_number) + 4
148def DoubleByteSize(field_number, double):
149 return TagByteSize(field_number) + 8
152def BoolByteSize(field_number, b):
153 return TagByteSize(field_number) + 1
156def EnumByteSize(field_number, enum):
157 return UInt32ByteSize(field_number, enum)
160def StringByteSize(field_number, string):
161 return BytesByteSize(field_number, string.encode('utf-8'))
164def BytesByteSize(field_number, b):
165 return (TagByteSize(field_number)
166 + _VarUInt64ByteSizeNoTag(len(b))
167 + len(b))
170def GroupByteSize(field_number, message):
171 return (2 * TagByteSize(field_number) # START and END group.
172 + message.ByteSize())
175def MessageByteSize(field_number, message):
176 return (TagByteSize(field_number)
177 + _VarUInt64ByteSizeNoTag(message.ByteSize())
178 + message.ByteSize())
181def MessageSetItemByteSize(field_number, msg):
182 # First compute the sizes of the tags.
183 # There are 2 tags for the beginning and ending of the repeated group, that
184 # is field number 1, one with field number 2 (type_id) and one with field
185 # number 3 (message).
186 total_size = (2 * TagByteSize(1) + TagByteSize(2) + TagByteSize(3))
188 # Add the number of bytes for type_id.
189 total_size += _VarUInt64ByteSizeNoTag(field_number)
191 message_size = msg.ByteSize()
193 # The number of bytes for encoding the length of the message.
194 total_size += _VarUInt64ByteSizeNoTag(message_size)
196 # The size of the message.
197 total_size += message_size
198 return total_size
201def TagByteSize(field_number):
202 """Returns the bytes required to serialize a tag with this field number."""
203 # Just pass in type 0, since the type won't affect the tag+type size.
204 return _VarUInt64ByteSizeNoTag(PackTag(field_number, 0))
207# Private helper function for the *ByteSize() functions above.
209def _VarUInt64ByteSizeNoTag(uint64):
210 """Returns the number of bytes required to serialize a single varint
211 using boundary value comparisons. (unrolled loop optimization -WPierce)
212 uint64 must be unsigned.
213 """
214 if uint64 <= 0x7f: return 1
215 if uint64 <= 0x3fff: return 2
216 if uint64 <= 0x1fffff: return 3
217 if uint64 <= 0xfffffff: return 4
218 if uint64 <= 0x7ffffffff: return 5
219 if uint64 <= 0x3ffffffffff: return 6
220 if uint64 <= 0x1ffffffffffff: return 7
221 if uint64 <= 0xffffffffffffff: return 8
222 if uint64 <= 0x7fffffffffffffff: return 9
223 if uint64 > UINT64_MAX:
224 raise message.EncodeError('Value out of range: %d' % uint64)
225 return 10
228NON_PACKABLE_TYPES = (
229 descriptor.FieldDescriptor.TYPE_STRING,
230 descriptor.FieldDescriptor.TYPE_GROUP,
231 descriptor.FieldDescriptor.TYPE_MESSAGE,
232 descriptor.FieldDescriptor.TYPE_BYTES
233)
236def IsTypePackable(field_type):
237 """Return true iff packable = true is valid for fields of this type.
239 Args:
240 field_type: a FieldDescriptor::Type value.
242 Returns:
243 True iff fields of this type are packable.
244 """
245 return field_type not in NON_PACKABLE_TYPES