title: aplib decompression
body: <pre><code>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
  </code></pre>
  
  <p>Above is the aplib decompression. It has been mentioned that it is a derivation of lz78.
  I want to know the meaning of <code>getbit</code>, how and where in the <code>algorithm</code> it uses for reading codes from dictionary.</p>