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 | """Class for encoding protocol message primitives. |
| 32 | |
| 33 | Contains the logic for encoding every logical protocol field type |
| 34 | into one of the 5 physical wire types. |
| 35 | """ |
| 36 | |
| 37 | __author__ = 'robinson@google.com (Will Robinson)' |
| 38 | |
| 39 | import struct |
| 40 | from froofle.protobuf import message |
| 41 | from froofle.protobuf.internal import wire_format |
| 42 | from froofle.protobuf.internal import output_stream |
| 43 | |
| 44 | |
| 45 | # Note that much of this code is ported from //net/proto/ProtocolBuffer, and |
| 46 | # that the interface is strongly inspired by WireFormat from the C++ proto2 |
| 47 | # implementation. |
| 48 | |
| 49 | |
| 50 | class Encoder(object): |
| 51 | |
| 52 | """Encodes logical protocol buffer fields to the wire format.""" |
| 53 | |
| 54 | def __init__(self): |
| 55 | self._stream = output_stream.OutputStream() |
| 56 | |
| 57 | def ToString(self): |
| 58 | """Returns all values encoded in this object as a string.""" |
| 59 | return self._stream.ToString() |
| 60 | |
| 61 | # All the Append*() methods below first append a tag+type pair to the buffer |
| 62 | # before appending the specified value. |
| 63 | |
| 64 | def AppendInt32(self, field_number, value): |
| 65 | """Appends a 32-bit integer to our buffer, varint-encoded.""" |
| 66 | self._AppendTag(field_number, wire_format.WIRETYPE_VARINT) |
| 67 | self._stream.AppendVarint32(value) |
| 68 | |
| 69 | def AppendInt64(self, field_number, value): |
| 70 | """Appends a 64-bit integer to our buffer, varint-encoded.""" |
| 71 | self._AppendTag(field_number, wire_format.WIRETYPE_VARINT) |
| 72 | self._stream.AppendVarint64(value) |
| 73 | |
| 74 | def AppendUInt32(self, field_number, unsigned_value): |
| 75 | """Appends an unsigned 32-bit integer to our buffer, varint-encoded.""" |
| 76 | self._AppendTag(field_number, wire_format.WIRETYPE_VARINT) |
| 77 | self._stream.AppendVarUInt32(unsigned_value) |
| 78 | |
| 79 | def AppendUInt64(self, field_number, unsigned_value): |
| 80 | """Appends an unsigned 64-bit integer to our buffer, varint-encoded.""" |
| 81 | self._AppendTag(field_number, wire_format.WIRETYPE_VARINT) |
| 82 | self._stream.AppendVarUInt64(unsigned_value) |
| 83 | |
| 84 | def AppendSInt32(self, field_number, value): |
| 85 | """Appends a 32-bit integer to our buffer, zigzag-encoded and then |
| 86 | varint-encoded. |
| 87 | """ |
| 88 | self._AppendTag(field_number, wire_format.WIRETYPE_VARINT) |
| 89 | zigzag_value = wire_format.ZigZagEncode(value) |
| 90 | self._stream.AppendVarUInt32(zigzag_value) |
| 91 | |
| 92 | def AppendSInt64(self, field_number, value): |
| 93 | """Appends a 64-bit integer to our buffer, zigzag-encoded and then |
| 94 | varint-encoded. |
| 95 | """ |
| 96 | self._AppendTag(field_number, wire_format.WIRETYPE_VARINT) |
| 97 | zigzag_value = wire_format.ZigZagEncode(value) |
| 98 | self._stream.AppendVarUInt64(zigzag_value) |
| 99 | |
| 100 | def AppendFixed32(self, field_number, unsigned_value): |
| 101 | """Appends an unsigned 32-bit integer to our buffer, in little-endian |
| 102 | byte-order. |
| 103 | """ |
| 104 | self._AppendTag(field_number, wire_format.WIRETYPE_FIXED32) |
| 105 | self._stream.AppendLittleEndian32(unsigned_value) |
| 106 | |
| 107 | def AppendFixed64(self, field_number, unsigned_value): |
| 108 | """Appends an unsigned 64-bit integer to our buffer, in little-endian |
| 109 | byte-order. |
| 110 | """ |
| 111 | self._AppendTag(field_number, wire_format.WIRETYPE_FIXED64) |
| 112 | self._stream.AppendLittleEndian64(unsigned_value) |
| 113 | |
| 114 | def AppendSFixed32(self, field_number, value): |
| 115 | """Appends a signed 32-bit integer to our buffer, in little-endian |
| 116 | byte-order. |
| 117 | """ |
| 118 | sign = (value & 0x80000000) and -1 or 0 |
| 119 | if value >> 32 != sign: |
| 120 | raise message.EncodeError('SFixed32 out of range: %d' % value) |
| 121 | self._AppendTag(field_number, wire_format.WIRETYPE_FIXED32) |
| 122 | self._stream.AppendLittleEndian32(value & 0xffffffff) |
| 123 | |
| 124 | def AppendSFixed64(self, field_number, value): |
| 125 | """Appends a signed 64-bit integer to our buffer, in little-endian |
| 126 | byte-order. |
| 127 | """ |
| 128 | sign = (value & 0x8000000000000000) and -1 or 0 |
| 129 | if value >> 64 != sign: |
| 130 | raise message.EncodeError('SFixed64 out of range: %d' % value) |
| 131 | self._AppendTag(field_number, wire_format.WIRETYPE_FIXED64) |
| 132 | self._stream.AppendLittleEndian64(value & 0xffffffffffffffff) |
| 133 | |
| 134 | def AppendFloat(self, field_number, value): |
| 135 | """Appends a floating-point number to our buffer.""" |
| 136 | self._AppendTag(field_number, wire_format.WIRETYPE_FIXED32) |
| 137 | self._stream.AppendRawBytes(struct.pack('f', value)) |
| 138 | |
| 139 | def AppendDouble(self, field_number, value): |
| 140 | """Appends a double-precision floating-point number to our buffer.""" |
| 141 | self._AppendTag(field_number, wire_format.WIRETYPE_FIXED64) |
| 142 | self._stream.AppendRawBytes(struct.pack('d', value)) |
| 143 | |
| 144 | def AppendBool(self, field_number, value): |
| 145 | """Appends a boolean to our buffer.""" |
| 146 | self.AppendInt32(field_number, value) |
| 147 | |
| 148 | def AppendEnum(self, field_number, value): |
| 149 | """Appends an enum value to our buffer.""" |
| 150 | self.AppendInt32(field_number, value) |
| 151 | |
| 152 | def AppendString(self, field_number, value): |
| 153 | """Appends a length-prefixed unicode string, encoded as UTF-8 to our buffer, |
| 154 | with the length varint-encoded. |
| 155 | """ |
| 156 | self.AppendBytes(field_number, value.encode('utf-8')) |
| 157 | |
| 158 | def AppendBytes(self, field_number, value): |
| 159 | """Appends a length-prefixed sequence of bytes to our buffer, with the |
| 160 | length varint-encoded. |
| 161 | """ |
| 162 | self._AppendTag(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) |
| 163 | self._stream.AppendVarUInt32(len(value)) |
| 164 | self._stream.AppendRawBytes(value) |
| 165 | |
| 166 | # TODO(robinson): For AppendGroup() and AppendMessage(), we'd really like to |
| 167 | # avoid the extra string copy here. We can do so if we widen the Message |
| 168 | # interface to be able to serialize to a stream in addition to a string. The |
| 169 | # challenge when thinking ahead to the Python/C API implementation of Message |
| 170 | # is finding a stream-like Python thing to which we can write raw bytes |
| 171 | # from C. I'm not sure such a thing exists(?). (array.array is pretty much |
| 172 | # what we want, but it's not directly exposed in the Python/C API). |
| 173 | |
| 174 | def AppendGroup(self, field_number, group): |
| 175 | """Appends a group to our buffer. |
| 176 | """ |
| 177 | self._AppendTag(field_number, wire_format.WIRETYPE_START_GROUP) |
| 178 | self._stream.AppendRawBytes(group.SerializeToString()) |
| 179 | self._AppendTag(field_number, wire_format.WIRETYPE_END_GROUP) |
| 180 | |
| 181 | def AppendMessage(self, field_number, msg): |
| 182 | """Appends a nested message to our buffer. |
| 183 | """ |
| 184 | self._AppendTag(field_number, wire_format.WIRETYPE_LENGTH_DELIMITED) |
| 185 | self._stream.AppendVarUInt32(msg.ByteSize()) |
| 186 | self._stream.AppendRawBytes(msg.SerializeToString()) |
| 187 | |
| 188 | def AppendMessageSetItem(self, field_number, msg): |
| 189 | """Appends an item using the message set wire format. |
| 190 | |
| 191 | The message set message looks like this: |
| 192 | message MessageSet { |
| 193 | repeated group Item = 1 { |
| 194 | required int32 type_id = 2; |
| 195 | required string message = 3; |
| 196 | } |
| 197 | } |
| 198 | """ |
| 199 | self._AppendTag(1, wire_format.WIRETYPE_START_GROUP) |
| 200 | self.AppendInt32(2, field_number) |
| 201 | self.AppendMessage(3, msg) |
| 202 | self._AppendTag(1, wire_format.WIRETYPE_END_GROUP) |
| 203 | |
| 204 | def _AppendTag(self, field_number, wire_type): |
| 205 | """Appends a tag containing field number and wire type information.""" |
| 206 | self._stream.AppendVarUInt32(wire_format.PackTag(field_number, wire_type)) |