def decode(self, object, **kwds): szone,pu = self.getparent(szone_t),object.get() cookie = szone['cookie'].li.int() t = bitmap.new(pu, object.size()*8) t = bitmap.rol(t, 4) ptr = bitmap.number(t) & ~0xf cs = ptr ^ cookie if bitmap.number(t)&0xf != self.generate_checksum(cs): logging.warn("{:s}.ptr_union.decode : Checksum doesn't match with cookie 0x{:x} : 0x{:x} != 0x{:x}".format(__name__, cookie, bitmap.number(t)&0xf, self.generate_checksum(cs))) res = object.copy().set(ptr) return super(ptr_union,self).decode(res, **kwds)
def encode(self, object, **kwds): szone,pu = self.getparent(szone_t),object.get() cookie = szone['cookie'].li.int() csum = self.generate_checksum(pu^cookie) t = bitmap.new(pu, object.size()*8) t = bitmap.rol(t, 4) cs = bitmap.new(bitmap.number(t)|csum, bitmap.size(t)) cs = bitmap.ror(cs, 4) logging.info("{:s}.ptr_union.encode : Encoded pointer 0x{:x} with 0x{:x} results in checksum 0x{:x} : {:s}".format(__name__, pu, cookie, csum, bitmap.hex(cs))) res = object.copy().set(bitmap.number(cs)) return super(ptr_union,self).encode(res, **kwds)
def decode(self, object, **kwds): szone, pu = self.getparent(szone_t), object.get() cookie = szone['cookie'].li.int() t = bitmap.new(pu, object.size() * 8) t = bitmap.rol(t, 4) ptr = bitmap.number(t) & ~0xf cs = ptr ^ cookie if bitmap.number(t) & 0xf != self.generate_checksum(cs): logging.warn( "{:s}.ptr_union.decode : Checksum doesn't match with cookie 0x{:x} : 0x{:x} != 0x{:x}" .format(__name__, cookie, bitmap.number(t) & 0xf, self.generate_checksum(cs))) res = object.copy().set(ptr) return super(ptr_union, self).decode(res, **kwds)
def encode(self, object, **kwds): szone, pu = self.getparent(szone_t), object.get() cookie = szone['cookie'].li.int() csum = self.generate_checksum(pu ^ cookie) t = bitmap.new(pu, object.size() * 8) t = bitmap.rol(t, 4) cs = bitmap.new(bitmap.number(t) | csum, bitmap.size(t)) cs = bitmap.ror(cs, 4) logging.info( "{:s}.ptr_union.encode : Encoded pointer 0x{:x} with 0x{:x} results in checksum 0x{:x} : {:s}" .format(__name__, pu, cookie, csum, bitmap.hex(cs))) res = object.copy().set(bitmap.number(cs)) return super(ptr_union, self).encode(res, **kwds)
def str(self): data = map(ord, self.serialize()) res = [data[0] / 40, data.pop(0) % 40] data = iter(data) for n in data: v = bitmap.new(0, 0) while n & 0x80: v = bitmap.push(v, (n & 0x7f, 7)) n = data.next() v = bitmap.push(v, (n, 7)) res.append(bitmap.number(v)) return '.'.join(map(str, res))
def get_pointer(self): '''Returns the encoded pointer''' res = bitmap.new(self.int(), self.size()*8) res = bitmap.rol(res, 4) res = bitmap.number(res) & ~0xf return res
def get_checksum(self): '''Returns the encoded checksum for the ptr_union''' pu = self.int() t = bitmap.new(pu, self.size()*8) t = bitmap.rol(t, 4) return bitmap.number(t) & 0xf
def generate_checksum(self, ptr): pu = bitmap.new(ptr, self.size()*8) res = reduce(bitmap.add, map(bitmap.number,bitmap.split(pu,8)), bitmap.new(0,8)) return bitmap.number(res) & 0xf
def get_pointer(self): '''Returns the encoded pointer''' res = bitmap.new(self.int(), self.size() * 8) res = bitmap.rol(res, 4) res = bitmap.number(res) & ~0xf return res
def get_checksum(self): '''Returns the encoded checksum for the ptr_union''' pu = self.int() t = bitmap.new(pu, self.size() * 8) t = bitmap.rol(t, 4) return bitmap.number(t) & 0xf
def generate_checksum(self, ptr): pu = bitmap.new(ptr, self.size() * 8) res = reduce(bitmap.add, map(bitmap.number, bitmap.split(pu, 8)), bitmap.new(0, 8)) return bitmap.number(res) & 0xf