def __getitem__(self, i): if isinstance(i, tuple): result = "" source = bytes(self) for s in i: result += source[s] return result elif isinstance(i, slice): if i.step is not None: i = i.indices(len(self)) start = i[0] stop = i[1] if stop <= start: return "" buf = ctypes.create_string_buffer(stop - start) ctypes.memmove( buf, core.BNGetDataBufferContentsAt(self.handle, start), stop - start) return buf.raw else: return bytes(self)[i] elif i < 0: if i >= -len(self): return chr( core.BNGetDataBufferByte(self.handle, int(len(self) + i))) raise IndexError("index out of range") elif i < len(self): return chr(core.BNGetDataBufferByte(self.handle, int(i))) else: raise IndexError("index out of range")
def __setitem__(self, i, value): if isinstance(i, slice): if i.step is not None: raise IndexError("step not supported on assignment") i = i.indices(len(self)) start = i[0] stop = i[1] if stop < start: stop = start if len(value) != (stop - start): data = bytes(self) data = data[0:start] + value + data[stop:] core.BNSetDataBufferContents(self.handle, data, len(data)) else: value = str(value) buf = ctypes.create_string_buffer(value) ctypes.memmove( core.BNGetDataBufferContentsAt(self.handle, start), buf, len(value)) elif i < 0: if i >= -len(self): if len(value) != 1: raise ValueError("expected single byte for assignment") value = str(value) buf = ctypes.create_string_buffer(value) ctypes.memmove( core.BNGetDataBufferContentsAt(self.handle, int(len(self) + i)), buf, 1) else: raise IndexError("index out of range") elif i < len(self): if len(value) != 1: raise ValueError("expected single byte for assignment") value = str(value) buf = ctypes.create_string_buffer(value) ctypes.memmove(core.BNGetDataBufferContentsAt(self.handle, int(i)), buf, 1) else: raise IndexError("index out of range")