def test_values(self): if _compat.MACHINE_WORD_SIZE == 32: self.assertEqual(_compat.get_word_alignment(1 << 64), (32, 4, _compat.UINT32_MAX, "L")) self.assertEqual(_compat.get_word_alignment(1 << 32), (32, 4, _compat.UINT32_MAX, "L")) elif _compat.MACHINE_WORD_SIZE == 64: self.assertEqual(_compat.get_word_alignment(1 << 64), (64, 8, _compat.UINT64_MAX, "Q")) self.assertEqual(_compat.get_word_alignment(1 << 32), (64, 8, _compat.UINT64_MAX, "Q")) else: raise NotImplementedError("Do we support other than 32/64-bit?") # Anything 32-bit or below: values = [ (1 << 31, (32, 4, _compat.UINT32_MAX, "L")), (1 << 16, (32, 4, _compat.UINT32_MAX, "L")), (1 << 15, (16, 2, _compat.UINT16_MAX, "H")), (1 << 8, (16, 2, _compat.UINT16_MAX, "H")), (1 << 7, (8, 1, _compat.UINT8_MAX, "B")) ] for num, tup in values: self.assertEqual(_compat.get_word_alignment(num), tup, "%d, %r" % (num, tup))
def test_values(self): if MACHINE_WORD_SIZE == 32: self.assertEqual(get_word_alignment(1 << 64), (32, 4, UINT32_MAX, 'L')) self.assertEqual(get_word_alignment(1 << 32), (32, 4, UINT32_MAX, 'L')) elif MACHINE_WORD_SIZE == 64: self.assertEqual(get_word_alignment(1 << 64), (64, 8, UINT64_MAX, 'Q')) self.assertEqual(get_word_alignment(1 << 32), (64, 8, UINT64_MAX, 'Q')) else: raise NotImplementedError("Do we support other than 32/64-bit?") # Anything 32-bit or below: values = [ (1 << 31, (32, 4, UINT32_MAX, 'L')), (1 << 16, (32, 4, UINT32_MAX, 'L')), (1 << 15, (16, 2, UINT16_MAX, 'H')), (1 << 8, (16, 2, UINT16_MAX, 'H')), (1 << 7, (8, 1, UINT8_MAX, 'B')) ] for num, tup in values: self.assertEqual(get_word_alignment(num), tup, "%d, %r" % (num, tup))
def uint_to_bytes(number, fill_size=0, chunk_size=0, overflow=False): """ Convert an unsigned integer to bytes (base-256 representation). Leading zeros are not preserved for positive integers unless a chunk size or a fill size is specified. A single zero byte is returned if the number is 0 and no padding is specified. When a chunk size or a fill size is specified, the resulting bytes are prefix-padded with zero bytes to satisfy the size. The total size of the number in bytes is either the fill size or an integral multiple of the chunk size. .. NOTE: You cannot specify both the fill size and the chunk size. :param number: Integer value :param fill_size: The maxmimum number of bytes with which to represent the integer. Prefix zero padding is added as necessary to satisfy the size. If the number of bytes needed to represent the integer is greater than the fill size, an ``OverflowError`` is raised. To suppress this error and allow overflow, you may set the ``overfloww`` argument to this function to ``True``. :param chunk_size: If optional chunk size is given and greater than zero, the resulting sequence of bytes is prefix-padded with zero bytes so that the total number of bytes is a multiple of ``chunk_size``. :param overflow: ``False`` (default). If this is ``True``, no ``OverflowError`` will be raised when the fill_size is shorter than the length of the generated byte sequence. Instead the byte sequence will be returned as is. :returns: Raw bytes (base-256 representation). :raises: ``OverflowError`` when a fill size is given and the number takes up more bytes than fit into the block. This requires the ``overflow`` argument to this function to be set to ``False`` otherwise, no error will be raised. """ if number < 0: raise ValueError("Number must be an unsigned integer: %d" % number) if fill_size and chunk_size: raise ValueError("You can either fill or pad chunks, but not both") # Ensure these are integers. _ = number & 1 and chunk_size & 1 and fill_size & 1 raw_bytes = EMPTY_BYTE # Pack the integer one machine word at a time into bytes. num = number word_bits, _, max_uint, pack_type = get_word_alignment(num) pack_format = ">%s" % pack_type while num > 0: raw_bytes = pack(pack_format, num & max_uint) + raw_bytes num >>= word_bits # Obtain the index of the first non-zero byte. zero_leading = bytes_leading(raw_bytes) if number == 0: raw_bytes = ZERO_BYTE # De-padding. raw_bytes = raw_bytes[zero_leading:] length = len(raw_bytes) if fill_size > 0: if not overflow and length > fill_size: raise OverflowError( "Need %d bytes for number, but fill size is %d" % (length, fill_size) ) raw_bytes = raw_bytes.rjust(fill_size, ZERO_BYTE) elif chunk_size > 0: remainder = length % chunk_size if remainder: padding_size = chunk_size - remainder raw_bytes = raw_bytes.rjust(length + padding_size, ZERO_BYTE) return raw_bytes
def uint_to_bytes_array_based(number, chunk_size=0): """ Convert a integer to bytes (base-256 representation):: integer_to_bytes(n:int, chunk_size:int) : string .. WARNING: Does not preserve leading zeros if you don't specify a chunk size. :param number: Integer value :param chunk_size: If optional chunk size is given and greater than zero, pad the front of the byte string with binary zeros so that the length is a multiple of ``chunk_size``. Raises an OverflowError if the chunk_size is not sufficient to represent the integer. :returns: Raw bytes (base-256 representation). :raises: ``OverflowError`` when block_size is given and the number takes up more bytes than fit into the block. """ # Machine word aligned byte array based implementation. if number < 0: raise ValueError("Number must be unsigned integer: %d" % number) raw_bytes = EMPTY_BYTE if not number: raw_bytes = ZERO_BYTE # Align packing to machine word size. num = number word_bits, word_bytes, max_uint, pack_type = _compat.get_word_alignment(num) pack_format = ">" + pack_type temp_buffer = array.array("B", [0] * word_bytes) byte_array = array.array("B", raw_bytes) while num > 0: struct.pack_into(pack_format, temp_buffer, 0, num & max_uint) byte_array = temp_buffer + byte_array num >>= word_bits # Count the number of zero prefix bytes. zero_leading = 0 length = len(byte_array) for zero_leading in builtins.range(length): if byte_array[zero_leading]: break raw_bytes = byte_array[zero_leading:].tostring() if chunk_size > 0: # Bounds checking. We're not doing this up-front because the # most common use case is not specifying a chunk size. In the worst # case, the number will already have been converted to bytes above. length = len(raw_bytes) if length > chunk_size: raise OverflowError("Need %d bytes for number, but chunk size is %d" % (length, chunk_size)) remainder = length % chunk_size if remainder: raw_bytes = (chunk_size - remainder) * ZERO_BYTE + raw_bytes return raw_bytes