1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
#include <u.h>
#include <libc.h>
#include <flate.h>
#include "zlib.h"
typedef struct ZRead ZRead;
struct ZRead
{
ulong adler;
void *rr;
int (*r)(void*, void*, int);
};
static int
zlread(void *vzr, void *buf, int n)
{
ZRead *zr;
zr = vzr;
n = (*zr->r)(zr->rr, buf, n);
if(n <= 0)
return n;
zr->adler = adler32(zr->adler, buf, n);
return n;
}
int
deflatezlib(void *wr, int (*w)(void*, void*, int), void *rr, int (*r)(void*, void*, int), int level, int debug)
{
ZRead zr;
uchar buf[4];
int ok;
buf[0] = ZlibDeflate | ZlibWin32k;
/* bogus zlib encoding of compression level */
buf[1] = ((level > 2) + (level > 5) + (level > 8)) << 6;
/* header check field */
buf[1] |= 31 - ((buf[0] << 8) | buf[1]) % 31;
if((*w)(wr, buf, 2) != 2)
return FlateOutputFail;
zr.rr = rr;
zr.r = r;
zr.adler = 1;
ok = deflate(wr, w, &zr, zlread, level, debug);
if(ok != FlateOk)
return ok;
buf[0] = zr.adler >> 24;
buf[1] = zr.adler >> 16;
buf[2] = zr.adler >> 8;
buf[3] = zr.adler;
if((*w)(wr, buf, 4) != 4)
return FlateOutputFail;
return FlateOk;
}
|