LZW解压缩算法——python版

July 6, 2019 · python · 86次阅读
def uncompress(compressed):
    k = compressed
    n = [[_] for _ in range(256)]
    q = 0
    t = 0
    Ea = 8
    H = [0 for _ in range(ceil(1.5 * len(k)))]
    K = 0
    L = []
    while (q < len(k) and 8 * (len(k) - q) - t > Ea):
        S = 0
        A = 0
        while (S < Ea):
            J = min(Ea - S, 8 - t)
            B = (k[q] << t & 255) >> 8 - J
            S = S + J
            t = t + J
            if 8 == t:
                t = 0
                q += 1
            A |= (B & 255) << Ea - S
        S = [] if A >= len(n) else n[A]
        if 0 == len(L):
            Ea += 1
        else:
            L.append(S[0]) if S else L.append(L[0])
            n.append(L)
            L = []
            if len(n) == 1 << Ea:
                Ea += 1
            S = n[A]
        A = K + len(S)
        if A >= len(H):
            J = [0 for _ in  range(ceil(1.5 * A))]
            J[:len(H)] = H
            H = J
        H[K:K+len(S)]=S
        K = A
        L += S
    return bytes(H[:K]).decode("utf-8")

标签:Netflix

最后编辑于:2019/08/11 12:15

添加新评论