output_stream.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. """OutputStream is the primitive interface for sticking bits on the wire.
  31. All protocol buffer serialization can be expressed in terms of
  32. the OutputStream primitives provided here.
  33. """
  34. __author__ = 'robinson@google.com (Will Robinson)'
  35. import array
  36. import struct
  37. from froofle.protobuf import message
  38. from froofle.protobuf.internal import wire_format
  39. # Note that much of this code is ported from //net/proto/ProtocolBuffer, and
  40. # that the interface is strongly inspired by CodedOutputStream from the C++
  41. # proto2 implementation.
  42. class OutputStream(object):
  43. """Contains all logic for writing bits, and ToString() to get the result."""
  44. def __init__(self):
  45. self._buffer = array.array('B')
  46. def AppendRawBytes(self, raw_bytes):
  47. """Appends raw_bytes to our internal buffer."""
  48. self._buffer.fromstring(raw_bytes)
  49. def AppendLittleEndian32(self, unsigned_value):
  50. """Appends an unsigned 32-bit integer to the internal buffer,
  51. in little-endian byte order.
  52. """
  53. if not 0 <= unsigned_value <= wire_format.UINT32_MAX:
  54. raise message.EncodeError(
  55. 'Unsigned 32-bit out of range: %d' % unsigned_value)
  56. self._buffer.fromstring(struct.pack(
  57. wire_format.FORMAT_UINT32_LITTLE_ENDIAN, unsigned_value))
  58. def AppendLittleEndian64(self, unsigned_value):
  59. """Appends an unsigned 64-bit integer to the internal buffer,
  60. in little-endian byte order.
  61. """
  62. if not 0 <= unsigned_value <= wire_format.UINT64_MAX:
  63. raise message.EncodeError(
  64. 'Unsigned 64-bit out of range: %d' % unsigned_value)
  65. self._buffer.fromstring(struct.pack(
  66. wire_format.FORMAT_UINT64_LITTLE_ENDIAN, unsigned_value))
  67. def AppendVarint32(self, value):
  68. """Appends a signed 32-bit integer to the internal buffer,
  69. encoded as a varint. (Note that a negative varint32 will
  70. always require 10 bytes of space.)
  71. """
  72. if not wire_format.INT32_MIN <= value <= wire_format.INT32_MAX:
  73. raise message.EncodeError('Value out of range: %d' % value)
  74. self.AppendVarint64(value)
  75. def AppendVarUInt32(self, value):
  76. """Appends an unsigned 32-bit integer to the internal buffer,
  77. encoded as a varint.
  78. """
  79. if not 0 <= value <= wire_format.UINT32_MAX:
  80. raise message.EncodeError('Value out of range: %d' % value)
  81. self.AppendVarUInt64(value)
  82. def AppendVarint64(self, value):
  83. """Appends a signed 64-bit integer to the internal buffer,
  84. encoded as a varint.
  85. """
  86. if not wire_format.INT64_MIN <= value <= wire_format.INT64_MAX:
  87. raise message.EncodeError('Value out of range: %d' % value)
  88. if value < 0:
  89. value += (1 << 64)
  90. self.AppendVarUInt64(value)
  91. def AppendVarUInt64(self, unsigned_value):
  92. """Appends an unsigned 64-bit integer to the internal buffer,
  93. encoded as a varint.
  94. """
  95. if not 0 <= unsigned_value <= wire_format.UINT64_MAX:
  96. raise message.EncodeError('Value out of range: %d' % unsigned_value)
  97. while True:
  98. bits = unsigned_value & 0x7f
  99. unsigned_value >>= 7
  100. if not unsigned_value:
  101. self._buffer.append(bits)
  102. break
  103. self._buffer.append(0x80|bits)
  104. def ToString(self):
  105. """Returns a string containing the bytes in our internal buffer."""
  106. return self._buffer.tostring()