8 #define HSHIFT 3 /* HSHIFT==5 runs slightly faster, but hash table is 64x bigger */
9 #define NHASH (1<<(HSHIFT*NMATCH))
10 #define HMASK (NHASH-1)
11 #define hupdate(h, c) ((((h)<<HSHIFT)^(c))&HMASK)
12 typedef struct Hlist Hlist;
19 writememimage(int fd, Memimage *i)
21 uchar *outbuf, *outp, *eout; /* encoded data, pointer, end */
22 uchar *loutp; /* start of encoded line */
23 Hlist *hash; /* heads of hash chains of past strings */
24 Hlist *chain, *hp; /* hash chain members, pointer */
25 Hlist *cp; /* next Hlist to fall out of window */
26 int h; /* hash value */
27 uchar *line, *eline; /* input line, end pointer */
28 uchar *data, *edata; /* input buffer, end pointer */
29 ulong n; /* length of input buffer */
30 int bpl; /* input line length */
31 int offs, runlen; /* offset, length of consumed data */
32 uchar dumpbuf[NDUMP]; /* dump accumulator */
33 int ndump; /* length of dump accumulator */
34 int ncblock; /* size of compressed blocks */
36 uchar *p, *q, *s, *es, *t;
41 bpl = bytesperline(r, i->depth);
42 ncblock = _compblocksize(r, i->depth);
44 sprint(hdr, "%11s %11d %11d %11d %11d ",
45 chantostr(cbuf, i->chan), r.min.x, r.min.y, r.max.x, r.max.y);
46 if(write(fd, hdr, 5*12) != 5*12)
48 for(; r.min.y < r.max.y; r.min.y++)
49 if(write(fd, byteaddr(i, r.min), bpl) != bpl)
61 if(unloadmemimage(i, r, data, n) != n)
63 outbuf = malloc(ncblock);
64 hash = malloc(NHASH*sizeof(Hlist));
65 chain = malloc(NMEM*sizeof(Hlist));
66 if(outbuf == 0 || hash == 0 || chain == 0){
73 sprint(hdr, "compressed\n%11s %11d %11d %11d %11d ",
74 chantostr(cbuf, i->chan), r.min.x, r.min.y, r.max.x, r.max.y);
75 if(write(fd, hdr, 11+5*12) != 11+5*12)
78 eout = outbuf+ncblock;
82 memset(hash, 0, NHASH*sizeof(Hlist));
83 memset(chain, 0, NMEM*sizeof(Hlist));
87 for(n = 0; n != NMATCH; n++)
88 h = hupdate(h, line[n]);
93 for(p = line; p != eline; ){
100 for(hp = hash[h].next; hp; hp = hp->next){
124 if(eout-outp < ndump+1)
126 *outp++ = ndump-1+128;
127 memmove(outp, dumpbuf, ndump);
131 dumpbuf[ndump++] = *p;
136 if(eout-outp < ndump+1)
138 *outp++ = ndump-1+128;
139 memmove(outp, dumpbuf, ndump);
146 *outp++ = ((runlen-NMATCH)<<2) + (offs>>8);
149 for(q = p+runlen; p != q; p++){
152 cp->next = hash[h].next;
158 if(++cp == &chain[NMEM])
161 h = hupdate(h, p[NMATCH]);
165 if(eout-outp < ndump+1)
167 *outp++ = ndump-1+128;
168 memmove(outp, dumpbuf, ndump);
179 sprint(hdr, "%11d %11ld ", r.max.y, n);
180 write(fd, hdr, 2*12);
181 write(fd, outbuf, n);