def base_encode(raw_bytes, base, base_bytes, base_zero, padding=True): """ Encodes raw bytes given a base. :param raw_bytes: Raw bytes to encode. :param base: Unsigned integer base. :param base_bytes: The ASCII bytes used in the encoded string. "Character set" or "alphabet". :param base_zero: """ if not builtins.is_bytes(raw_bytes): raise TypeError("data must be raw bytes: got %r" % type(raw_bytes).__name__) number = integer.bytes_to_uint(raw_bytes) encoded = EMPTY_BYTE while number > 0: number, remainder = divmod(number, base) encoded = base_bytes[remainder] + encoded if padding: zero_leading = builtins.bytes_leading(raw_bytes) encoded = encoded.rjust(len(encoded) + zero_leading, base_zero) return encoded
def base_encode(raw_bytes, base, base_bytes, base_zero, padding=True): """ Encodes raw bytes given a base. :param raw_bytes: Raw bytes to encode. :param base: Unsigned integer base. :param base_bytes: The ASCII bytes used in the encoded string. "Character set" or "alphabet". :param base_zero: """ if not is_bytes(raw_bytes): raise TypeError("data must be raw bytes: got %r" % type(raw_bytes).__name__) number = bytes_to_uint(raw_bytes) encoded = EMPTY_BYTE while number > 0: number, remainder = divmod(number, base) encoded = base_bytes[remainder] + encoded if padding: zero_leading = bytes_leading(raw_bytes) encoded = encoded.rjust(len(encoded) + zero_leading, base_zero) return encoded
def test_leading(self): self.assertEqual(builtins.bytes_leading(b("\x00\x00\x00\x00")), 4) self.assertEqual(builtins.bytes_leading(b("\x00\x00\x00")), 3) self.assertEqual(builtins.bytes_leading(b("\x00\x00\xff")), 2) self.assertEqual(builtins.bytes_leading(b("\xff")), 0) self.assertEqual(builtins.bytes_leading(b("\x00\xff")), 1) self.assertEqual(builtins.bytes_leading(b("\x00")), 1) self.assertEqual(builtins.bytes_leading(b("\x00\x00\x00\xff")), 3) self.assertEqual(builtins.bytes_leading(b("")), 0)
def test_leading(self): self.assertEqual(bytes_leading(b('\x00\x00\x00\x00')), 4) self.assertEqual(bytes_leading(b('\x00\x00\x00')), 3) self.assertEqual(bytes_leading(b('\x00\x00\xff')), 2) self.assertEqual(bytes_leading(b('\xff')), 0) self.assertEqual(bytes_leading(b('\x00\xff')), 1) self.assertEqual(bytes_leading(b('\x00')), 1) self.assertEqual(bytes_leading(b('\x00\x00\x00\xff')), 3) self.assertEqual(bytes_leading(b('')), 0)
def uint_to_base256(number, encoded, base_zero): """Convert uint to base 256.""" if number == 0: raw_bytes = EMPTY_BYTE else: raw_bytes = uint_to_bytes(number) zero_leading = bytes_leading(encoded, base_zero) if zero_leading: raw_bytes = raw_bytes.rjust(len(raw_bytes) + zero_leading, ZERO_BYTE) return raw_bytes
def uint_to_base256(number, encoded, base_zero): """Convert uint to base 256.""" if number == 0: raw_bytes = EMPTY_BYTE else: raw_bytes = integer.uint_to_bytes(number) zero_leading = builtins.bytes_leading(encoded, base_zero) if zero_leading: raw_bytes = raw_bytes.rjust(len(raw_bytes) + zero_leading, ZERO_BYTE) return raw_bytes
def decimal_decode(encoded): """ Decodes decimal-encoded bytes to raw bytes. Leading zeros are converted to leading zero bytes. :param encoded: Decimal-encoded representation. :returns: Raw bytes. """ padding = ZERO_BYTE * bytes_leading(encoded, DIGIT_ZERO_BYTE) int_val = int(encoded) if int_val: decoded = padding + uint_to_bytes(int_val) else: decoded = padding return decoded
def decimal_decode(encoded): """ Decodes decimal-encoded bytes to raw bytes. Leading zeros are converted to leading zero bytes. :param encoded: Decimal-encoded representation. :returns: Raw bytes. """ padding = ZERO_BYTE * builtins.bytes_leading(encoded, DIGIT_ZERO_BYTE) int_val = int(encoded) if int_val: decoded = padding + integer.uint_to_bytes(int_val) else: decoded = padding return decoded
def decimal_encode(raw_bytes): """ Encodes raw bytes into decimal representation. Leading zero bytes are preserved. Encode your Unicode strings to a byte encoding before decimal-encoding them. :param raw_bytes: Bytes. :returns: Decimal-encoded representation. """ padding = DIGIT_ZERO_BYTE * bytes_leading(raw_bytes) int_val = bytes_to_uint(raw_bytes) if int_val: encoded = padding + str(int_val).encode("ascii") else: encoded = padding return encoded
def decimal_encode(raw_bytes): """ Encodes raw bytes into decimal representation. Leading zero bytes are preserved. Encode your Unicode strings to a byte encoding before decimal-encoding them. :param raw_bytes: Bytes. :returns: Decimal-encoded representation. """ padding = DIGIT_ZERO_BYTE * builtins.bytes_leading(raw_bytes) int_val = integer.bytes_to_uint(raw_bytes) if int_val: encoded = padding + str(int_val).encode("ascii") else: encoded = padding return encoded
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(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 = _compat.get_word_alignment(num) pack_format = ">%s" % pack_type while num > 0: raw_bytes = struct.pack(pack_format, num & max_uint) + raw_bytes num >>= word_bits # Obtain the index of the first non-zero byte. zero_leading = builtins.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