Exemplo n.º 1
0
 def write(self, integer, length):
     '''Given the specified integer and its length, encode it into its little-endian form and return a string.'''
     res = array.array('B')
     binteger = bitmap.new(integer, 8 * length)
     octets = bitmap.split(binteger, 8)
     for item in reversed(octets):
         res.append(bitmap.int(item))
     return res.tostring()
Exemplo n.º 2
0
    def details(self):
        bytes_per_item = self.new(self._object_).a.size()
        bits_per_item = bytes_per_item * 8
        bytes_per_row = bytes_per_item * (1 if self.bits() < 0x200 else 2)
        bits_per_row = bits_per_item * (1 if self.bits() < 0x200 else 2)

        items = bitmap.split(self.bitmap(), bits_per_row)

        width = len("{:x}".format(self.bits()))
        return '\n'.join(("[{:x}] {{{:0{:d}x}:{:0{:d}x}}} {:s}".format(self.getoffset() + i * bytes_per_row, i * bits_per_row, width, min(self.bits(), i * bits_per_row + bits_per_row) - 1, width, bitmap.string(item)) for i, item in enumerate(items)))
Exemplo n.º 3
0
    def summary(self):
        iterable = (item.int() for item in self)
        res = functools.reduce(lambda agg, item: agg * 0x100000000 + item, iterable, 0)
        num = bitmap.new(res, 128)
        components = bitmap.split(num, 16)

        # FIXME: there's got to be a more elegant way than a hacky state machine
        result, counter = [], 0
        for item in map(bitmap.number, components):
            if counter < 2:
                if item == 0:
                    counter = 1
                    if len(result) == 0:
                        result.append('')
                    continue
                elif counter > 0:
                    result.extend(['', "{:x}".format(item)])
                    counter = 2
                    continue
            result.append("{:x}".format(item))
        return ':'.join(result)
Exemplo n.º 4
0
 def summary(self):
     num = bitmap.new(super(in_addr,self).int(), 32)
     octets = bitmap.split(num, 8)[::-1]
     return '0x{:x} {:d}.{:d}.{:d}.{:d}'.format(*map(bitmap.number,[num]+octets))
Exemplo n.º 5
0
 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
Exemplo n.º 6
0
 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
Exemplo n.º 7
0
 def summary(self):
     res = self.int()
     integer = bitmap.new(res, 32)
     octets = bitmap.split(integer, 8)
     return '{:#x} {:d}.{:d}.{:d}.{:d}'.format(*map(bitmap.int, [integer] +
                                                    octets))
Exemplo n.º 8
0
 def summary(self):
     num = bitmap.new(super(in_addr, self).int(), 32)
     octets = bitmap.split(num, 8)[::-1]
     return '0x{:x} {:d}.{:d}.{:d}.{:d}'.format(
         *map(bitmap.number, [num] + octets))