Example #1
0
def decompress(compressed_data, c):
    br = BitReader(compressed_data)
    state = LzwState(c)
    state.clear()

    d = {}
    htab = {}
    output = []
    output2 = []

    while True:
        if br.isEnd():
            return []
        cur_code = br.getBits(state.req_bits)
        incode = cur_code
        if cur_code == state.M_EOD:
            break
        if cur_code == state.M_CLR:
            state.clear()
        else:
            if cur_code == state.next_code:
                cur_code = state.oldcode
                output.append(state.finchar)
            while cur_code >= state.M_CLR:
                if cur_code not in htab:
                    return []
                output.append(htab[cur_code])
                cur_code = d[cur_code + 1]
            state.finchar = cur_code
            while True:
                output2.append(cur_code)
                if not output:
                    break
                cur_code = output.pop()
            if state.next_code < 4096 and state.oldcode != -1:
                d[state.next_code + 1] = state.oldcode
                htab[state.next_code] = state.finchar
                state.next_code += 1
                if state.next_code >= state.next_shift:
                    if state.req_bits < 12:
                        state.req_bits += 1
                        state.next_shift = 1 << state.req_bits
            state.oldcode = incode
    return bytes(output2)