1(zpaq 1.07 minimum (fast) compression.
2Uses 4 x 2^$1 MB memory. $2 increases minimum match length.
3Requires lzppre as an external preprocessor.
4
5(C) 2009, Ocarina Networks Inc. Written by Matt Mahoney.
6This software is free under GPL v3.
7http://www.gnu.org/copyleft/gpl.html )
8
9comp 3 3 $1+18 $1+20 1 (hh hm PH PM n)
10  0 cm $1+19 5 (context model size=2^19, limit=5*4)
11hcomp
12  *d<>a a^=*d a<<= 8 *d=a (order 2 context)
13  halt
14pcomp lzppre $1+18 $1+20 127 $2+2 96 ;
15  (lzppre PH PM ESC MINLEN HMUL)
16  (If you change these values, then change them in the code too)
17
18  (The sequence ESC 0 codes for ESC. The sequence ESC LEN
19   codes for a match of length LEN+MINLEN at the last place
20   in the output buffer M (size 2^PM) that had the same context
21   hash in the low PH bits of D. D indexes hash table H
22   which points into buffer M, which contains B bytes.
23   When called, A contains the byte to be decoded and F=true
24   if the last byte was ESC. The rolling context hash D is
25   updated by D=D*HMUL+M[B])
26
27  if (last byte was ESC then copy from match)
28    a> 0 jf 50 (goto output esc)
29    a+= $2+2 (MINLEN)
30    r=a 0 (save length in R0)
31    c=*d (c points to match)
32    do (find match and output it)
33      *d=b (update index with last output byte)
34      a=*c *b=a b++ c++ out (copy and output matching byte)
35      d<>a a*= 96 (HMUL)
36      a+=d d<>a (update context hash)
37      a=r 0 a-- r=a 0 (decrement length)
38    a> 0 while (repeat until length is 0)
39    halt
40  endif
41
42  (otherwise, set F for ESC)
43  a== 127 (ESC) if
44    halt
45  endif
46
47  (reset state at EOF)
48  a> 255 if
49    b=0 c=0 a= 1 a<<= $1+18 d=a
50    do
51      d-- *d=0 a=d (clear index)
52    a> 0 while
53    halt (F=0)
54
55    (goto here: output esc)
56    a= 127 (ESC)
57  endif
58  *d=b (update index)
59  *b=a b++ out (update buffer and output)
60  d<>a a*= 96 (HMUL)
61  a+=d d<>a (update context hash)
62  halt
63end
64