def create_data(version, error_correction, data_list): buffer = BitBuffer() for data in data_list: buffer.put(data.mode, 4) buffer.put(len(data), length_in_bits(data.mode, version)) data.write(buffer) # Calculate the maximum number of bits for the given version. rs_blocks = base.rs_blocks(version, error_correction) bit_limit = 0 for block in rs_blocks: bit_limit += block.data_count * 8 if len(buffer) > bit_limit: raise exceptions.DataOverflowError( "Code length overflow. Data size (%s) > size available (%s)" % (len(buffer), bit_limit)) # Terminate the bits (add up to four 0s). for i in range(min(bit_limit - len(buffer), 4)): buffer.put_bit(False) # Delimit the string into 8-bit words, padding with 0s if necessary. delimit = len(buffer) % 8 if delimit: for i in range(8 - delimit): buffer.put_bit(False) # Add special alternating padding bitstrings until buffer if full. bytes_to_fill = (bit_limit - len(buffer)) // 8 for i in range(bytes_to_fill): buffer.put(PAD0 if i % 2 == 0 else PAD1, 8) return create_bytes(buffer, rs_blocks)
def create_data(version, error_correction, data_list, colors, control_colors=None, copy_colors_to_ec=False): if None == control_colors: control_colors = defaultdict(lambda :True) last_color = colors[-1] bit_buf = BitBuffer() for data in data_list: bit_buf.put(data.mode, 4, colors[0]) bit_buf.put(len(data), length_in_bits(data.mode, version), colors[0]) data.write_to_buffer(bit_buf, colors[:len(data)]) colors = colors[len(data):] # Calculate the maximum number of bits for the given version. rs_blocks = base.rs_blocks(version, error_correction) bit_limit = 0 for block in rs_blocks: bit_limit += block.data_count * 8 if len(bit_buf) > bit_limit: raise exceptions.DataOverflowError( "Code length overflow. Data size (%s) > size available (%s)" % (len(bit_buf), bit_limit)) # Terminate the bits (add up to four 0s). for i in range(min(bit_limit - len(bit_buf), 4)): bit_buf.put_bit(False, last_color) # Delimit the string into 8-bit words, padding with 0s if necessary. delimit = len(bit_buf) % 8 if delimit: bit_buf.put(0, 8 - delimit, control_colors['padding']) # Add special alternating padding bitstrings until bit_buf is full. bytes_to_fill = (bit_limit - len(bit_buf)) // 8 for i in range(bytes_to_fill): if i % 2 == 0: bit_buf.put(PAD0, 8, control_colors['padding']) else: bit_buf.put(PAD1, 8, control_colors['padding']) data_and_error_correction = generate_error_correction(bit_buf, rs_blocks, control_colors, copy_colors_to_ec) encoded_data = combain_data_and_error_correction(data_and_error_correction) return encoded_data
def create_data(version, error_correction, data_list): rs_blocks = base.rs_blocks(version, error_correction) buffer = BitBuffer() for data in data_list: buffer.put(data.mode, 4) buffer.put(len(data), length_in_bits(data.mode, version)) data.write(buffer) # calc num max data. total_data_count = 0 for block in rs_blocks: total_data_count += block.data_count if len(buffer) > total_data_count * 8: raise exceptions.DataOverflowError("Code length overflow. Data size " "(%s) > size available (%s)" % (len(buffer), total_data_count * 8)) # end code if len(buffer) + 4 <= total_data_count * 8: buffer.put(0, 4) # padding while len(buffer) % 8: buffer.put_bit(False) # padding while True: if len(buffer) >= total_data_count * 8: break buffer.put(PAD0, 8) if len(buffer) >= total_data_count * 8: break buffer.put(PAD1, 8) return create_bytes(buffer, rs_blocks)
(1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0)) G18 = ( (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0)) G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1) PAD0 = 0xEC PAD1 = 0x11 MAX_VERSION = 40 # Precompute bit count limits, indexed by error correction level and code size _data_count = lambda block: block.data_count BIT_LIMIT_TABLE = [ [0] + [8*sum(map(_data_count, base.rs_blocks(version, error_correction))) for version in xrange(1, MAX_VERSION+1)] for error_correction in xrange(4) ] def BCH_type_info(data): d = data << 10 while BCH_digit(d) - BCH_digit(G15) >= 0: d ^= (G15 << (BCH_digit(d) - BCH_digit(G15))) return ((data << 10) | d) ^ G15_MASK def BCH_type_number(data): d = data << 12
G15 = ( (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0)) G18 = ( (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0)) G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1) PAD0 = 0xEC PAD1 = 0x11 # Precompute bit count limits, indexed by error correction level and code size _data_count = lambda block: block.data_count BIT_LIMIT_TABLE = [ [0] + [8*sum(map(_data_count, base.rs_blocks(version, error_correction))) for version in xrange(1, 41)] for error_correction in xrange(4) ] def BCH_type_info(data): d = data << 10 while BCH_digit(d) - BCH_digit(G15) >= 0: d ^= (G15 << (BCH_digit(d) - BCH_digit(G15))) return ((data << 10) | d) ^ G15_MASK def BCH_type_number(data): d = data << 12