1
aP_depack_asm:
    ; aP_depack_asm(const void *source, void *destination)

    _ret$  equ 7*4
    _src$  equ 8*4 + 4
    _dst$  equ 8*4 + 8

    pushad

    mov    esi, [esp + _src$] ; C calling convention
    mov    edi, [esp + _dst$]

    cld
    mov    dl, 80h
    xor    ebx,ebx

literal:
    movsb
    mov    bl, 2
nexttag:
    call   getbit
    jnc    literal-------jump if no carry

    xor    ecx, ecx
    call   getbit
    jnc    codepair

    xor    eax, eax
    call   getbit
    jnc    shortmatch

    mov    bl, 2
    inc    ecx
    mov    al, 10h

.getmorebits:
    call   getbit
    adc    al, al
    jnc    .getmorebits

    jnz    domatch

    stosb
    jmp    nexttag

codepair:
    call   getgamma_no_ecx
    sub    ecx, ebx
    jnz    normalcodepair

    call   getgamma
    jmp    domatch_lastpos


shortmatch:
    lodsb
    shr    eax, 1
    jz     donedepacking

    adc    ecx, ecx
    jmp    domatch_with_2inc

normalcodepair:
    xchg   eax, ecx
    dec    eax
    shl    eax, 8
    lodsb
    call   getgamma

    cmp    eax, 32000
    jae    domatch_with_2inc
    cmp    ah, 5
    jae    domatch_with_inc

    cmp    eax, 7fh
    ja     domatch_new_lastpos

domatch_with_2inc:
    inc    ecx

domatch_with_inc:
    inc    ecx

domatch_new_lastpos:
    xchg   eax, ebp
domatch_lastpos:
    mov    eax, ebp

    mov    bl, 1

domatch:
    push   esi
    mov    esi, edi
    sub    esi, eax
    rep    movsb
    pop    esi
    jmp    nexttag

getbit:
    add    dl, dl
    jnz    .stillbitsleft
    mov    dl, [esi]
    inc    esi
    adc    dl, dl
  .stillbitsleft:
    ret

getgamma:
    xor    ecx, ecx

getgamma_no_ecx:
    inc    ecx
  .getgammaloop:
    call   getbit
    adc    ecx, ecx
    call   getbit
    jc     .getgammaloop

    ret

donedepacking:
    sub    edi, [esp + _dst$]
    mov    [esp + _ret$], edi ; return unpacked length in eax

    popad

    ret

上面是aplib解压。已经提到它是lz78的派生。我想知道它用于从字典中读取代码的含义getbit、方式和位置。algorithm

4

1 回答 1

1

函数getbit 负责从输入数据流中读取一位(这是“混合的”,即包含纯字节和打包成字节的8 个独立位组)。

位读取是通过在寄存器 dl 中一次存储 8 位来完成的,并将这些位向左移动(添加 dl,dl 就可以了)。如果在移位寄存器后 dl 变为等于 0,这意味着所有位都已移出,需要从流中读取另一个字节,这在代码中完成:

getbit:
    add    dl, dl
    jnz    .stillbitsleft
    mov    dl, [esi]
    inc    esi
    adc    dl, dl
.stillbitsleft:
    ret

ApLib 中使用的实际算法是原创的,但它建立在与 LZ78 非常不同的基础上,并且与 LZ77 有很多关系。基本上,在解压缩器循环的每次迭代中,从输入流中读取一位,如果为 0,则将一个字节从输入流复制到输出中。另一方面,如果该位为 1,则解压缩器知道必须从已解压缩的数据中复制匹配项。这个信号位的想法来自 LZSS。匹配的详细信息也在压缩数据流中指定。该格式的一个不太常见的功能包括使用rep-offsets 的可能性,即允许重用偏移量的命令。

于 2019-08-20T14:18:34.527 回答