File: compress.c

package info (click to toggle)
mixmaster 3.0b2-4
  • links: PTS
  • area: main
  • in suites: etch-m68k
  • size: 1,356 kB
  • ctags: 1,173
  • sloc: ansic: 18,314; sh: 1,285; yacc: 698; perl: 314; makefile: 161
file content (210 lines) | stat: -rw-r--r-- 4,790 bytes parent folder | download | duplicates (3)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* Mixmaster version 3.0  --  (C) 1999 - 2004 Anonymizer Inc. and others.

   Mixmaster may be redistributed and modified under certain conditions.
   This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
   ANY KIND, either express or implied. See the file COPYRIGHT for
   details.

   Buffer compression (interface to zlib)
   $Id: compress.c 710 2004-01-21 00:47:21Z rabbi $ */


#include "mix3.h"
#include <stdio.h>
#include <assert.h>

static byte gz_magic[2] =
{0x1f, 0x8b};			/* gzip magic header */

/* gzip flag byte */
#define ASCII_FLAG   0x01
#define HEAD_CRC     0x02
#define EXTRA_FIELD  0x04
#define ORIG_NAME    0x08
#define COMMENT      0x10
#define RESERVED     0xE0
#define Z_DEFLATED   8

#ifdef USE_ZLIB
#include "zlib.h"

int buf_unzip(BUFFER *in, int type)
{
  BUFFER *out;
  z_stream s;
  long outstart;
  int err;
  int ret = 0;

  out = buf_new();

  s.zalloc = (alloc_func) 0;
  s.zfree = (free_func) 0;
  s.opaque = (voidpf) 0;

  s.next_in = in->data + in->ptr;
  s.avail_in = in->length + 1 - in->ptr;	/* terminating 0 byte as "dummy" */
  s.next_out = NULL;

  if (type == 1)
    err = inflateInit(&s);    /* zlib */
  else
    err = inflateInit2(&s, -MAX_WBITS);
  if (err != Z_OK) {
    ret = -1;
    goto end;
  }
  outstart = 0;
  buf_append(out, NULL, in->length * 15 / 10);

  for (;;) {
    s.next_out = out->data + s.total_out + outstart;
    s.avail_out = out->length - outstart - s.total_out;
    err = inflate(&s, Z_PARTIAL_FLUSH);
    out->length -= s.avail_out;
    if (err != Z_OK)
      break;
    buf_append(out, NULL, BUFSIZE);
  }
  if (err != Z_STREAM_END)
    errlog(WARNING, "Decompression error %d\n", err);

  err = inflateEnd(&s);
  if (err != Z_OK)
    ret = -1;
end:
  if (ret != 0)
    switch (err) {
    case Z_STREAM_ERROR:
      errlog(ERRORMSG, "Decompression error Z_STREAM_ERROR.\n", err);
      break;
    case Z_MEM_ERROR:
      errlog(ERRORMSG, "Decompression error Z_MEM_ERROR.\n", err);
      break;
    case Z_BUF_ERROR:
      errlog(ERRORMSG, "Decompression error Z_BUF_ERROR.\n", err);
      break;
    case Z_VERSION_ERROR:
      errlog(ERRORMSG, "Decompression error Z_VERSION_ERROR.\n", err);
      break;
    default:
      errlog(ERRORMSG, "Decompression error %d.\n", err);
    }
  buf_move(in, out);
  buf_free(out);
  return (ret);
}

int buf_zip(BUFFER *out, BUFFER *in, int bits)
{
  z_stream s;
  long outstart;
  int err = -1;

  assert(in != out);

  s.zalloc = (alloc_func) 0;
  s.zfree = (free_func) 0;
  s.opaque = (voidpf) 0;
  s.next_in = NULL;

  if (bits == 0)
    bits = MAX_WBITS;

  if (deflateInit2(&s, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -bits, 8, 0) != Z_OK)
    goto end;

  outstart = out->length;
  /* 12 is overhead, 1.01 is maximum expansion, and 1 is there to force a round-up */
  buf_append(out, NULL, (int)13+in->length*1.01); /* fit it in one chunk */

  s.next_in = in->data;
  s.avail_in = in->length;

  for (;;) {
    s.next_out = out->data + s.total_out + outstart;
    s.avail_out = out->length - outstart - s.total_out;
    err = deflate(&s, Z_FINISH);
    out->length -= s.avail_out;
    if (err != Z_OK)
      break;
    errlog(ERRORMSG, "Compressed data did not fit in one chunk.\n");
    buf_append(out, NULL, BUFSIZE);
  }
  if (deflateEnd(&s) != Z_OK || err != Z_STREAM_END)
    err = -1;
  else
    err = 0;
end:
  if (err != 0)
    errlog(ERRORMSG, "Compression error.\n");
  return (err);
}

#else /* end of USE_ZLIB */
int buf_zip(BUFFER *out, BUFFER *in, int bits)
{
  return (-1);
}

int buf_unzip(BUFFER *b, int type)
{
  errlog(ERRORMSG, "Can't uncompress: no zlib\n");
  return (-1);
}
#endif /* else not USE_ZLIB */

int compressed(BUFFER *b)
{
  return (b->length >= 10 && b->data[0] == gz_magic[0] &&
	  b->data[1] == gz_magic[1]);
}

int buf_uncompress(BUFFER *in)
{
  int type;
  int err = -1;
  unsigned int len;

  if (!compressed(in))
    return (0);
  type = in->data[3];
  if (in->data[2] != Z_DEFLATED || (type & RESERVED) == 0) {
    in->ptr = 10;
    if ((type & EXTRA_FIELD) != 0) {
      len = buf_geti(in);
      in->ptr += len;
    }
    if ((type & ORIG_NAME) != 0)
      while (buf_getc(in) > 0) ;
    if ((type & COMMENT) != 0)
      while (buf_getc(in) > 0) ;
    if ((type & HEAD_CRC) != 0)
      buf_geti(in);
    err = buf_unzip(in, 0);
  }
  return (err);
}

int buf_compress(BUFFER *in)
{
  BUFFER *out;
  int err;

  if (compressed(in))
    return (0);

  out = buf_new();
  buf_appendc(out, gz_magic[0]);
  buf_appendc(out, gz_magic[1]);
  buf_appendc(out, Z_DEFLATED);
  buf_appendc(out, 0);		/* flags */
  buf_appendl(out, 0);		/* time */
  buf_appendc(out, 0);		/* xflags */
  buf_appendc(out, 3);		/* Unix */
  err = buf_zip(out, in, 0);
  if (err == 0)
    buf_move(in, out);
  buf_free(out);
  return (err);
}