Exemple #1
0
def delta_decode(binary_string):
    length, encoded_bits = unary.decode(binary_string)
    limit_offset = (length + 1) + length # encoding of the value itself, then 'value' number of bits to read
    first_part, next_part = binary_string[:limit_offset], binary_string[limit_offset:] # we can now extract the first encoded binary

    bits_to_decode = getvaluefrombinarystring(first_part) - 1
    value = (1 << bits_to_decode) + getvaluefrombinarystring(next_part[:bits_to_decode])

    consumed_bits = (length + 1) + length + bits_to_decode
    return value, consumed_bits
Exemple #2
0
def delta_decode(binary_string):
    length, encoded_bits = unary.decode(binary_string)
    limit_offset = (length + 1) + length # encoding of the value itself, then 'value' number of bits to read
    first_part, next_part = binary_string[:limit_offset], binary_string[limit_offset:] # we can now extract the first encoded binary

    bits_to_decode = getvaluefrombinarystring(first_part) - 1
    value = (1 << bits_to_decode) + getvaluefrombinarystring(next_part[:bits_to_decode])

    consumed_bits = (length + 1) + length + bits_to_decode
    return value, consumed_bits
Exemple #3
0
def omega_decode(binary_string, digits_to_read=1, consumed_bits=0):
    if binary_string[0] == "0":
        return digits_to_read, consumed_bits + 1
    else:
        bits = binary_string[:digits_to_read + 1]
        next_part = binary_string[digits_to_read + 1:]
        digits_to_read_next = getvaluefrombinarystring(bits)
        return omega_decode(next_part, digits_to_read_next, consumed_bits + digits_to_read + 1)
Exemple #4
0
def omega_decode(binary_string, digits_to_read=1, consumed_bits=0):
    if binary_string[0] == "0":
        return digits_to_read, consumed_bits + 1
    else:
        bits = binary_string[:digits_to_read + 1]
        next_part = binary_string[digits_to_read + 1:]
        digits_to_read_next = getvaluefrombinarystring(bits)
        return omega_decode(next_part, digits_to_read_next, consumed_bits + digits_to_read + 1)
Exemple #5
0
def gamma_decode(binary_string):
    """reads a gamma encoded value from the binary string, return the value and the number of consumed bits"""
#    assert binary_string[0] == "0"
    length, encoded_bits = unary.decode(binary_string)
    offset = length + 1
    remaining_bits = binary_string[offset: offset + length]

    read_value = (1 << length ) + getvaluefrombinarystring(remaining_bits)

    consumed_bits = length * 2 + 1
    return read_value, consumed_bits
Exemple #6
0
def gamma_decode(binary_string):
    """reads a gamma encoded value from the binary string, return the value and the number of consumed bits"""
    #    assert binary_string[0] == "0"
    length, encoded_bits = unary.decode(binary_string)
    offset = length + 1
    remaining_bits = binary_string[offset:offset + length]

    read_value = (1 << length) + getvaluefrombinarystring(remaining_bits)

    consumed_bits = length * 2 + 1
    return read_value, consumed_bits
Exemple #7
0
def generate_root(polynom):
    exponents = get_exponents(polynom)
    # assert polynom == sorted(polynom, reverse=True)
    width = max(exponents) # polynom[0]

    # the number whose all bits of an exponent are sets, except the highest one
    all_bits = or_all(1 << i for i in exponents)
    highest_bit = 1 << width
    polynomial_constant = all_bits ^ highest_bit

    constant_bitstring = getpadbinstr(polynomial_constant, width)
    reverse_bitstring = constant_bitstring[::-1]
    reverse_constant = getvaluefrombinarystring(reverse_bitstring)

    return polynomial_constant, reverse_constant, width
Exemple #8
0
def generate_root(polynom):
    exponents = get_exponents(polynom)
    # assert polynom == sorted(polynom, reverse=True)
    width = max(exponents)  # polynom[0]

    # the number whose all bits of an exponent are sets, except the highest one
    all_bits = or_all(1 << i for i in exponents)
    highest_bit = 1 << width
    polynomial_constant = all_bits ^ highest_bit

    constant_bitstring = getpadbinstr(polynomial_constant, width)
    reverse_bitstring = constant_bitstring[::-1]
    reverse_constant = getvaluefrombinarystring(reverse_bitstring)

    return polynomial_constant, reverse_constant, width
Exemple #9
0
def change_base(block, block_length, source_base, source_size, target_base, target_size):
    """takes block as a block_length-bit long binary stream.
    reads it as entries of source_base, on source_size bits,
    resplit bits as target_base chars, which are stored on target_size"""

    # let's get a binary representation of our block, in our source base
    bits_string = ""
    for char in list(block):
        bits_string += getpadbinstr(source_base.index(char), source_size)

    # then split that representation in target_size bits long blocks
    split_bits = (bits_string[i : i + target_size] for i in range(0, block_length, target_size))

    # get the according values,
    encoded_values = (getvaluefrombinarystring(i) for i in split_bits)
    # and the corresponding entries from our target base
    output_chars = [target_base[i] for i in encoded_values]
    output_block = "".join(output_chars)

    return output_block
Exemple #10
0
def change_base(block, block_length, source_base, source_size, target_base,
                target_size):
    """takes block as a block_length-bit long binary stream.
    reads it as entries of source_base, on source_size bits,
    resplit bits as target_base chars, which are stored on target_size"""

    #let's get a binary representation of our block, in our source base
    bits_string = ""
    for char in list(block):
        bits_string += getpadbinstr(source_base.index(char), source_size)

    #then split that representation in target_size bits long blocks
    split_bits = (bits_string[i:i + target_size]
                  for i in range(0, block_length, target_size))

    #get the according values,
    encoded_values = (getvaluefrombinarystring(i) for i in split_bits)
    #and the corresponding entries from our target base
    output_chars = [target_base[i] for i in encoded_values]
    output_block = "".join(output_chars)

    return output_block
Exemple #11
0
assert char_range("A", "Z") == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

#gcd
#returns the greatest common divisor of both parameters
assert gcd(6, 8) == 2

#lcm
#returns the least common multiplier of both parameters
assert lcm(6, 9) == 18
assert lcm(6, 8) == 24

assert getbinlen(0) == 1
assert getbinlen(1) == 1
assert getbinlen(3) == 2

assert getvaluefrombinarystring("1000") == 8
assert getvaluefrombinarystring("001000") == 8

assert getpadbinstr(8, 8) == "00001000"

assert getunkbinstr(0, 0, 8) == "000000000"
assert getunkbinstr(1, 0, 8) == "000000001"
assert getunkbinstr(2, 1, 8) == "00000001x"
assert getunkbinstr(237, 3, 8) == "011101xxx"

assert gethexstr("\x00") == "00"
assert gethexstr("\x00\x01") == "00 01"
assert gethexstr("abcd") == "61 62 63 64"

assert int2lebin(1, 2) == '\x01\x00'
assert int2lebin(65535, 2) == "\xff\xff"
Exemple #12
0
assert char_range("A", "Z") == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

#gcd
#returns the greatest common divisor of both parameters
assert gcd(6, 8) == 2

#lcm
#returns the least common multiplier of both parameters
assert lcm(6, 9) == 18
assert lcm(6, 8) == 24

assert getbinlen(0) == 1
assert getbinlen(1) == 1
assert getbinlen(3) == 2

assert getvaluefrombinarystring("1000") == 8
assert getvaluefrombinarystring("001000") == 8

assert getpadbinstr(8, 8) == "00001000"

assert getunkbinstr(0, 0, 8) == "000000000"
assert getunkbinstr(1, 0, 8) == "000000001"
assert getunkbinstr(2, 1, 8) == "00000001x"
assert getunkbinstr(237, 3, 8) == "011101xxx"

assert gethexstr("\x00") == "00"
assert gethexstr("\x00\x01") == "00 01"
assert gethexstr("abcd") == "61 62 63 64"

assert int2lebin(1, 2) == '\x01\x00'
assert int2lebin(65535, 2) == "\xff\xff"