protolib.py (12208:621c3f3f80f2) protolib.py (12209:2c8d30e495ab)
1#!/usr/bin/env python2
2
3# Copyright (c) 2013 ARM Limited
4# All rights reserved
5#
6# The license below extends only to copyright in the software and shall
7# not be construed as granting a license to any other intellectual
8# property including but not limited to intellectual property relating

--- 84 unchanged lines hidden (view full) ---

93 proto_in.seek(0)
94 except IOError:
95 proto_in = open(in_file, 'rb')
96 except IOError:
97 print "Failed to open ", in_file, " for reading"
98 exit(-1)
99 return proto_in
100
1#!/usr/bin/env python2
2
3# Copyright (c) 2013 ARM Limited
4# All rights reserved
5#
6# The license below extends only to copyright in the software and shall
7# not be construed as granting a license to any other intellectual
8# property including but not limited to intellectual property relating

--- 84 unchanged lines hidden (view full) ---

93 proto_in.seek(0)
94 except IOError:
95 proto_in = open(in_file, 'rb')
96 except IOError:
97 print "Failed to open ", in_file, " for reading"
98 exit(-1)
99 return proto_in
100
101def DecodeVarint(in_file):
101def _DecodeVarint32(in_file):
102 """
103 The decoding of the Varint32 is copied from
104 google.protobuf.internal.decoder and is only repeated here to
105 avoid depending on the internal functions in the library. If the
106 end of file is reached, return (0, 0).
107 """
108 result = 0
109 shift = 0

--- 19 unchanged lines hidden (view full) ---

129 raise IOError('Too many bytes when decoding varint.')
130
131def decodeMessage(in_file, message):
132 """
133 Attempt to read a message from the file and decode it. Return
134 False if no message could be read.
135 """
136 try:
102 """
103 The decoding of the Varint32 is copied from
104 google.protobuf.internal.decoder and is only repeated here to
105 avoid depending on the internal functions in the library. If the
106 end of file is reached, return (0, 0).
107 """
108 result = 0
109 shift = 0

--- 19 unchanged lines hidden (view full) ---

129 raise IOError('Too many bytes when decoding varint.')
130
131def decodeMessage(in_file, message):
132 """
133 Attempt to read a message from the file and decode it. Return
134 False if no message could be read.
135 """
136 try:
137 size, pos = DecodeVarint(in_file)
137 size, pos = _DecodeVarint32(in_file)
138 if size == 0:
139 return False
140 buf = in_file.read(size)
141 message.ParseFromString(buf)
142 return True
143 except IOError:
144 return False
145
138 if size == 0:
139 return False
140 buf = in_file.read(size)
141 message.ParseFromString(buf)
142 return True
143 except IOError:
144 return False
145
146def EncodeVarint(out_file, value):
146def _EncodeVarint32(out_file, value):
147 """
148 The encoding of the Varint32 is copied from
149 google.protobuf.internal.encoder and is only repeated here to
150 avoid depending on the internal functions in the library.
151 """
152 bits = value & 0x7f
153 value >>= 7
154 while value:
155 out_file.write(struct.pack('<B', 0x80|bits))
156 bits = value & 0x7f
157 value >>= 7
158 out_file.write(struct.pack('<B', bits))
159
160def encodeMessage(out_file, message):
161 """
162 Encoded a message with the length prepended as a 32-bit varint.
163 """
164 out = message.SerializeToString()
147 """
148 The encoding of the Varint32 is copied from
149 google.protobuf.internal.encoder and is only repeated here to
150 avoid depending on the internal functions in the library.
151 """
152 bits = value & 0x7f
153 value >>= 7
154 while value:
155 out_file.write(struct.pack('<B', 0x80|bits))
156 bits = value & 0x7f
157 value >>= 7
158 out_file.write(struct.pack('<B', bits))
159
160def encodeMessage(out_file, message):
161 """
162 Encoded a message with the length prepended as a 32-bit varint.
163 """
164 out = message.SerializeToString()
165 EncodeVarint(out_file, len(out))
165 _EncodeVarint32(out_file, len(out))
166 out_file.write(out)
166 out_file.write(out)