def _build(self, obj, stream, context, path):
        if not isinstance(obj, integertypes):
            raise IntegerError("value is not an integer", path=path)
        if obj < 0:
            raise IntegerError("cannot build from negative number: %r" %
                               (obj, ),
                               path=path)
        if obj > 0xFFFF:
            raise IntegerError(
                "cannot build from number above short range: %r" % (obj, ),
                path=path)

        x = obj

        if (x > 0x3FFF):
            x |= 0xFF0000
            nbytes = 3
        elif (x > 0x7F):
            x |= 0x8000
            nbytes = 2
        else:
            nbytes = 1

        for i in range(nbytes, 0, -1):
            stream_write(stream, int2byte((x >> (8 * (i - 1))) & 0xFF), 1,
                         path)

        return obj
Beispiel #2
0
 def _parse(self, stream, context, path):
     length = self.length
     if callable(length):
         length = length(context)
     if length < 0:
         raise IntegerError("length must be non-negative")
     data = stream_read(stream, length, "WhateverPath")
     if self.swapped:
         if length & 7:
             raise IntegerError(
                 "little-endianness is only defined for multiples of 8 bits"
             )
         data = swapbytes(data)
     data = data[::-1]
     return bits2integer(data, self.signed)
Beispiel #3
0
 def _build(self, obj, stream, context, path):
     if not isinstance(obj, integertypes):
         raise IntegerError("value is not an integer")
     if obj < 0:
         raise IntegerError(
             "asn1length cannot build from negative number: %r" % (obj, ))
     num = obj
     if num < 0x80:
         stream_write(stream, int2byte(num), 1)
     else:
         acc = b""
         while num != 0:
             acc += int2byte(num & 0xFF)
             num >>= 8
         stream_write(stream, int2byte(0x80 | len(acc)), 1)
         stream_write(stream, swapbytes(acc))
     return obj
    def _build(self, obj, stream, context, path):
        if not isinstance(obj, integertypes):
            raise IntegerError("value is not an integer", path=path)
        if obj < 0:
            raise IntegerError("cannot build from negative number: %r" %
                               (obj, ),
                               path=path)
        if obj > 0xFFFFFFFFFFFFFFFF:
            raise IntegerError(
                "cannot build from number above short range: %r" % (obj, ),
                path=path)

        low = obj & 0xFFFFFFFF
        IdaVarInt32._build(low, stream, context, path)
        high = obj >> 32
        IdaVarInt32._build(high, stream, context, path)

        return obj
Beispiel #5
0
 def _build(self, obj, stream, context, path):
     if not isinstance(obj, integertypes):
         raise IntegerError("value %r is not an integer" % (obj,))
     if obj < 0 and not self.signed:
         raise IntegerError("value %r is negative, but field is not signed" % (obj,))
     length = self.length
     if callable(length):
         length = length(context)
     if length < 0:
         raise IntegerError("length must be non-negative")
     data = integer2bits(obj, length)
     if self.swapped:
         if length & 7:
             raise IntegerError(
                 "little-endianness is only defined for multiples of 8 bits"
             )
         data = swapbytes(data)
     data = data[::-1]
     stream_write(stream, data, length, path)
     return obj
Beispiel #6
0
 def _build(self, obj, stream, context, path):
     if not isinstance(obj, integertypes):
         raise IntegerError("Value is not an integer")
     to_write = obj
     for i in range(5):
         byte = 0
         length = self.lengths[i]
         if i == 0:
             negative = obj < 0
             byte |= self.negative_bit * negative
             if negative:
                 to_write *= -1
         mask = self._get_data_mask(length)
         byte |= to_write & mask
         to_write >>= length
         more_bit = (to_write > 0) and self._get_more_bit(length)
         byte |= more_bit
         byte &= 0xFF
         stream_write(stream, int2byte(byte), 1, path)
         if not more_bit:
             break
     return obj