Commit 986a3cbe authored by Sven Robertz's avatar Sven Robertz
Browse files

probably implemented packed32 in python

parent ceb0da14
...@@ -89,6 +89,12 @@ ...@@ -89,6 +89,12 @@
#?? +----+----+----+----+ #?? +----+----+----+----+
# #
# #
# type numbers and lengths do not have a fixed lenght, but are packed into sequences
# of 7 bit chunks, represented in bytes with the high bit meaning that more data
# is to come.
#
# The chunks are sent "little endian": each 7 bit chunk is more significant than
# the previous. See encode_packed32 and decode_packed32 in in Codec classes below.
import struct as packer import struct as packer
...@@ -306,9 +312,9 @@ class array(object): ...@@ -306,9 +312,9 @@ class array(object):
def encode_decl(self, encoder): def encode_decl(self, encoder):
encoder.encode_type(i_ARRAY) encoder.encode_type(i_ARRAY)
encoder.encode_int(len(self.indices)) encoder.encode_packed32(len(self.indices))
for i in self.indices: for i in self.indices:
encoder.encode_int(i) encoder.encode_packed32(i)
encoder.encode_type_number(self.decl) encoder.encode_type_number(self.decl)
def min_max_shape(self, l, depth=0, shape=[]): def min_max_shape(self, l, depth=0, shape=[]):
...@@ -344,7 +350,7 @@ class array(object): ...@@ -344,7 +350,7 @@ class array(object):
(shape, self.indices)) (shape, self.indices))
for i in range(len(self.indices)): for i in range(len(self.indices)):
if self.indices[i] == 0: if self.indices[i] == 0:
encoder.encode_int(shape[i]) encoder.encode_packed32(shape[i])
elif self.indices[i] != shape[i]: elif self.indices[i] != shape[i]:
raise Exception("Actual dimension %d in %s differs from %s" % raise Exception("Actual dimension %d in %s differs from %s" %
(i, shape, self.indices)) (i, shape, self.indices))
...@@ -362,10 +368,10 @@ class array(object): ...@@ -362,10 +368,10 @@ class array(object):
self.encode_value(encoder, value, depth) self.encode_value(encoder, value, depth)
def decode_decl(self, decoder): def decode_decl(self, decoder):
n_indices = decoder.decode_int() n_indices = decoder.decode_packed32()
indices = [] indices = []
for i in range(n_indices): for i in range(n_indices):
index = decoder.decode_int() index = decoder.decode_packed32()
indices.append(index) indices.append(index)
elem_decl = decoder.decode_decl() elem_decl = decoder.decode_decl()
return array(indices, elem_decl) return array(indices, elem_decl)
...@@ -383,7 +389,7 @@ class array(object): ...@@ -383,7 +389,7 @@ class array(object):
indices = [] indices = []
for i in self.indices: for i in self.indices:
if i == 0: if i == 0:
i = decoder.decode_int() i = decoder.decode_packed32()
indices.append(i) indices.append(i)
return self.decode_value(decoder, indices) return self.decode_value(decoder, indices)
...@@ -400,7 +406,7 @@ class array(object): ...@@ -400,7 +406,7 @@ class array(object):
indices = [] indices = []
for i in self.indices: for i in self.indices:
if i == 0: if i == 0:
i = decoder.decode_int() i = decoder.decode_packed32()
indices.append(i) indices.append(i)
return self.new_instance_value(indices) return self.new_instance_value(indices)
...@@ -414,7 +420,7 @@ class struct: ...@@ -414,7 +420,7 @@ class struct:
def encode_decl(self, encoder): def encode_decl(self, encoder):
encoder.encode_type(i_STRUCT) encoder.encode_type(i_STRUCT)
encoder.encode_int(len(self.field)) encoder.encode_packed32(len(self.field))
for (name, decl) in self.field: for (name, decl) in self.field:
encoder.encode_string(name) encoder.encode_string(name)
encoder.encode_type_number(decl) encoder.encode_type_number(decl)
...@@ -429,7 +435,7 @@ class struct: ...@@ -429,7 +435,7 @@ class struct:
decl.encode(encoder, obj.__getattribute__(name)) decl.encode(encoder, obj.__getattribute__(name))
def decode_decl(self, decoder): def decode_decl(self, decoder):
n_field = decoder.decode_int() n_field = decoder.decode_packed32()
field = [] field = []
for i in range(n_field): for i in range(n_field):
name = decoder.decode_string() name = decoder.decode_string()
...@@ -540,7 +546,7 @@ class Codec(object): ...@@ -540,7 +546,7 @@ class Codec(object):
index.sort() index.sort()
for i in index: for i in index:
e = self.index_to_decl[i] e = self.index_to_decl[i]
if i >= 0x80 and isinstance(e, sample): if i >= i_USER and isinstance(e, sample):
result.append(e) result.append(e)
return result return result
...@@ -573,6 +579,14 @@ class Encoder(Codec): ...@@ -573,6 +579,14 @@ class Encoder(Codec):
except KeyError: except KeyError:
decl.encode_decl(self) decl.encode_decl(self)
def encode_packed32(self, v):
tmp = v & 0xffffffff;
while(tmp >= 0x80 ):
self.encode_byte( (tmp & 0x7f) | 0x80 )
tmp >>= 7
self.encode_byte(tmp & 0x7f)
def encode_type(self, index): def encode_type(self, index):
self.pack("!i", index) self.pack("!i", index)
...@@ -648,8 +662,18 @@ class Decoder(Codec): ...@@ -648,8 +662,18 @@ class Decoder(Codec):
return result return result
def decode_packed32(self):
res = 0
i = 0
cont = True
while (cont):
c = self.decode_byte()
res |= (c & 0x7f) << 7*i
cont = (c & 0x80) != 0;
return res
def decode_type_number(self): def decode_type_number(self):
return self.unpack("!i") return self.decode_packed32()
def decode_boolean(self): def decode_boolean(self):
return self.unpack("!b") != 0 return self.unpack("!b") != 0
...@@ -673,7 +697,7 @@ class Decoder(Codec): ...@@ -673,7 +697,7 @@ class Decoder(Codec):
return self.unpack("!d") return self.unpack("!d")
def decode_string(self): def decode_string(self):
length = self.unpack("!i") length = self.decode_packed32()
return self.unpack("!%ds" % length).decode("utf8") return self.unpack("!%ds" % length).decode("utf8")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment