3 M(Y @sdZddlmZyddlZejWnek r8YnXddlZddlmZddl m Z ddl m Z m Z mZmZmZmZddZdd d Zefd d ZdddZedkrddlZejdS)zNData transformation functions. From bytes to a number, number to bytes, etc. )absolute_importN)pack)common) is_integerbbyteget_word_alignment ZERO_BYTE EMPTY_BYTEcCsttj|dS)zConverts a list of bytes or an 8-bit string to an integer. When using unicode strings, encode it to some encoding like UTF8 first. >>> (((128 * 256) + 64) * 256) + 15 8405007 >>> bytes2int(b'\x80@\x0f') 8405007 )intbinasciihexlify) raw_bytesr4/private/tmp/pip-build-nl73fm5q/rsa/rsa/transform.py bytes2int(s rcCst|std|j|dkr*td||dkr>d}tg}ntj|}g}|rp|dkrp||krptd||fx(|dkr|jdt |d@|dL}qrW|r|dkr||t}nt }|t j |S)aMConverts a number to a string of bytes. Usage:: >>> _int2bytes(123456789) b'\x07[\xcd\x15' >>> bytes2int(_int2bytes(123456789)) 123456789 >>> _int2bytes(123456789, 6) b'\x00\x00\x07[\xcd\x15' >>> bytes2int(_int2bytes(123456789, 128)) 123456789 >>> _int2bytes(123456789, 3) Traceback (most recent call last): ... OverflowError: Needed 4 bytes for number, but block size is 3 @param number: the number to convert @param block_size: the number of bytes to output. If the number encoded to bytes is less than this, the block will be zero-padded. When not given, the returned block is not padded. @throws OverflowError when block_size is given and the number takes up more bytes than fit into the block. z-You must pass an integer for 'number', not %srz#Negative numbers cannot be used: %iz0Needed %i bytes for number, but block size is %i) r TypeError __class__ ValueErrorr rZ byte_size OverflowErrorinsertrr join)number block_sizeZ needed_bytesrpaddingrrr _int2bytes7s*        rcCs2d}|d}x |D]}||kr(|d7}qPqW|S)a Finds the number of prefixed byte occurrences in the haystack. Useful when you want to deal with padding. :param raw_bytes: Raw bytes. :param needle: The byte to count. Default . :returns: The number of leading needle bytes. rrr)rZneedleleadingZ_bytexrrr bytes_leadingxs  r"FcCs|dkrtd||r$|r$td|d@td}|}t|\}}}} d| } x&|dkrvt| ||@|}||L}qRWt|} |dkrt}|| d}t|} |r|dkr| r| |krtd| |f|j|t}n4|o|dkr | |} | r || }|j| |t}|S) a) Convert an unsigned integer to bytes (base-256 representation):: Does not preserve leading zeros if you don't specify a chunk size or fill size. .. NOTE: You must not specify both fill_size and chunk_size. Only one of them is allowed. :param number: Integer value :param fill_size: If the optional fill size is given the length of the resulting byte string is expected to be the fill size and will be padded with prefix zero bytes to satisfy that length. :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``. :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 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. rz&Number must be an unsigned integer: %dz/You can either fill or pad chunks, but not bothrz>%sNz-Need %d bytes for number, but fill size is %d) rrrrr"r lenrrjust)rZ fill_size chunk_sizeZoverflowrnumZ word_bits_Zmax_uintZ pack_typeZ pack_formatZ zero_leadinglength remainderZ padding_sizerrr int2bytess:#      r+__main__)N)NNF)__doc__ __future__rZpsycofull ImportErrorr structrZrsarZ rsa._compatrrrrr r rrr"r+__name__doctesttestmodrrrrs"      A  L