Пример #1
0
def flip_bit(good, which):
    """Flip the low-order bit of good[which]."""
    if which == -1:
        pieces = good[:which], good[-1:], b""
    else:
        pieces = good[:which], good[which:which + 1], good[which + 1:]
    return pieces[0] + bchr(ord(pieces[1]) ^ 0x01) + pieces[2]
Пример #2
0
def flip_one_bit(s, offset=0, size=None):
    """ flip one random bit of the string s, in a byte greater than or equal to offset and less
    than offset+size. """
    precondition(isinstance(s, binary_type))
    if size is None:
        size=len(s)-offset
    i = randrange(offset, offset+size)
    result = s[:i] + bchr(bord(s[i])^(0x01<<randrange(0, 8))) + s[i+1:]
    assert result != s, "Internal error -- flip_one_bit() produced the same string as its input: %s == %s" % (result, s)
    return result
Пример #3
0
 def flip_bit(start, end):
     offset = random.randrange(start, end)
     bit = random.randrange(0, 8)
     print("[%d..%d):  %d.b%d" % (start, end, offset, bit), file=out)
     f = open(fn, "rb+")
     f.seek(offset)
     d = f.read(1)
     d = bchr(ord(d) ^ 0x01)
     f.seek(offset)
     f.write(d)
     f.close()
Пример #4
0
def serialize(obj):
    def serialize_int(i):
        # make sure i is non negative
        i = abs(i)

        def int_bytes(i):
            rest = True
            while rest:
                lower = i & 0x7f
                rest = i >> 7
                yield lower | (0x80 if rest else 0)
                i = rest

        return ibytes(int_bytes(i))

    if obj is None:
        return bchr(SerialConstants.SO_NULL)

    if isinstance(obj, unicode):
        return b''.join([
            bchr(SerialConstants.SO_STRING),
            serialize_int(len(obj)),
            bytes(obj, 'utf-8')
        ])

    if isinstance(obj, int):
        return b''.join([
            bchr(SerialConstants.SO_POS_INTEGER)
            if obj >= 0 else bchr(SerialConstants.SO_NEG_INTEGER),
            serialize_int(obj)
        ])

    try:
        # Byte vector
        if obj.typecode == b'b':
            return b''.join([
                bchr(SerialConstants.SO_BYTEVECTOR),
                serialize_int(len(obj)),
                obj.tostring()
            ])
    except:
        pass

    try:
        iobj = iter(obj)
        return b''.join([
            bchr(SerialConstants.SO_VECTOR),
            serialize_int(len(obj)),
            b''.join([serialize(elem) for elem in iobj])
        ])
    except:
        pass

    raise TypeError("cannot serialize object of type %s" % type(obj))
Пример #5
0
def serialize(obj):
    def serialize_int(i):
        # make sure i is non negative
        i = abs(i)
        def int_bytes(i):
            rest = True
            while rest:
                lower = i & 0x7f
                rest = i >> 7
                yield lower | (0x80 if rest else 0)
                i = rest

        return ibytes(int_bytes(i))

    if obj is None:
        return bchr(SerialConstants.SO_NULL)

    if isinstance(obj, unicode):
        return b''.join([bchr(SerialConstants.SO_STRING), serialize_int(len(obj)),
            bytes(obj, 'utf-8')])

    if isinstance(obj, int):
        return b''.join([bchr(SerialConstants.SO_POS_INTEGER) if obj >= 0 else
            bchr(SerialConstants.SO_NEG_INTEGER), serialize_int(obj)])

    try:
        # Byte vector
        if obj.typecode == b'b':
            return b''.join([bchr(SerialConstants.SO_BYTEVECTOR),
                serialize_int(len(obj)), obj.tostring()])
    except:
        pass

    try:
        iobj = iter(obj)
        return b''.join([bchr(SerialConstants.SO_VECTOR),
            serialize_int(len(obj)),
            b''.join([serialize(elem) for elem in iobj])])
    except:
        pass

    raise TypeError("cannot serialize object of type %s" % type(obj))
Пример #6
0
def add_two(original, byte_offset):
    # It isn't enough to simply flip the bit for the version number,
    # because 1 is a valid version number. So we add two instead.
    return (original[:byte_offset] +
            bchr(ord(original[byte_offset:byte_offset + 1]) ^ 0x02) +
            original[byte_offset + 1:])
Пример #7
0
def flip_bit(original, byte_offset):
    return (original[:byte_offset] +
            bchr(ord(original[byte_offset:byte_offset + 1]) ^ 0x01) +
            original[byte_offset + 1:])
Пример #8
0
def _randbytes(length):
    # type: (int) -> bytes
    """Return random bytes string of given length."""
    return b"".join([bchr(_RANDOM.randrange(0, 256)) for _ in range(length)])