source: trunk/src/gcc/fastjar/compress.c@ 2

Last change on this file since 2 was 2, checked in by bird, 22 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 13.2 KB
Line 
1/* $Id: compress.c,v 1.3 2002/01/03 04:57:56 rodrigc Exp $
2
3 $Log: compress.c,v $
4 Revision 1.3 2002/01/03 04:57:56 rodrigc
5 2001-01-02 Craig Rodrigues <rodrigc@gcc.gnu.org>
6
7 PR bootstrap/5117
8 * configure.in (AC_CHECK_HEADERS): Check for stdlib.h.
9 * Makefile.am: Move grepjar to bin_PROGRAMS.
10 * config.h.in: Regenerated.
11 * Makefile.in: Regenerated.
12 * aclocal.m4: Regenerated.
13 * jargrep.c: Eliminate some signed/unsigned and default
14 uninitialized warnings. Use HAVE_STDLIB_H instead of
15 STDC_HEADERS macro.
16 * jartool.c: Likewise.
17 * compress.c: Likewise.
18
19 Revision 1.2 2000/12/14 18:45:35 ghazi
20 Warning fixes:
21
22 * compress.c: Include stdlib.h and compress.h.
23 (rcsid): Delete.
24 (report_str_error): Make static.
25 (ez_inflate_str): Delete unused variable. Add parens in if-stmt.
26 (hrd_inflate_str): Likewise.
27
28 * compress.h (init_compression, end_compression, init_inflation,
29 end_inflation): Prototype void arguments.
30
31 * dostime.c (rcsid): Delete.
32
33 * jargrep.c: Include ctype.h, stdlib.h, zlib.h and compress.h.
34 Make functions static. Cast ctype function argument to `unsigned
35 char'. Add parens in if-stmts. Constify.
36 (Usage): Change into a macro.
37 (jargrep): Remove unused parameter.
38
39 * jartool.c: Constify. Add parens in if-stmts. Align
40 signed/unsigned char pointers in functions calls using casts.
41 (rcsid): Delete.
42 (list_jar): Fix printf format specifier.
43 (usage): Chop long string into bits. Reformat.
44
45 * pushback.c (rcsid): Delete.
46
47 Revision 1.1 2000/12/09 03:08:23 apbianco
48 2000-12-08 Alexandre Petit-Bianco <apbianco@cygnus.com>
49
50 * fastjar: Imported.
51
52 Revision 1.7 2000/09/13 14:02:02 cory
53 Reformatted some of the code to more closly match the layout of the orriginal
54 fastjar utility.
55
56 Revision 1.6 2000/09/12 22:29:36 cory
57 Jargrep now seems to do what I want it to do. Performs properly on Linux x86,
58 will test some other platforms later.
59
60 Revision 1.1.1.1 1999/12/06 03:09:16 toast
61 initial checkin..
62
63
64
65 Revision 1.7 1999/05/10 08:50:05 burnsbr
66 *** empty log message ***
67
68 Revision 1.6 1999/05/10 08:38:44 burnsbr
69 *** empty log message ***
70
71 Revision 1.5 1999/05/10 08:30:29 burnsbr
72 added inflation code
73
74 Revision 1.4 1999/04/27 10:03:33 burnsbr
75 added configure support
76
77 Revision 1.3 1999/04/26 02:35:32 burnsbr
78 compression now works.. yahoo
79
80 Revision 1.2 1999/04/23 12:01:59 burnsbr
81 added licence stuff.
82
83 Revision 1.1 1999/04/23 11:58:25 burnsbr
84 Initial revision
85
86
87*/
88
89/*
90 compress.c - code for handling deflation
91 Copyright (C) 1999 Bryan Burns
92
93 This program is free software; you can redistribute it and/or
94 modify it under the terms of the GNU General Public License
95 as published by the Free Software Foundation; either version 2
96 of the License, or (at your option) any later version.
97
98 This program is distributed in the hope that it will be useful,
99 but WITHOUT ANY WARRANTY; without even the implied warranty of
100 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
101 GNU General Public License for more details.
102
103 You should have received a copy of the GNU General Public License
104 along with this program; if not, write to the Free Software
105 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
106 */
107
108#include "config.h"
109
110#include <zlib.h>
111#include <string.h>
112#include <stdio.h>
113#include <errno.h>
114
115#ifdef HAVE_UNISTD_H
116#include <unistd.h>
117#endif
118#ifdef STDC_HEADERS
119#include <stdlib.h>
120#endif
121
122#include <sys/types.h>
123
124#include "jartool.h"
125#include "pushback.h"
126#include "compress.h"
127
128extern int seekable;
129
130static z_stream zs;
131
132void init_compression(){
133
134 memset(&zs, 0, sizeof(z_stream));
135
136 zs.zalloc = Z_NULL;
137 zs.zfree = Z_NULL;
138 zs.opaque = Z_NULL;
139
140 /* Why -MAX_WBITS? zlib has an undocumented feature, where if the windowbits
141 parameter is negative, it omits the zlib header, which seems to kill
142 any other zip/unzip program. This caused me SO much pain.. */
143 if(deflateInit2(&zs, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -MAX_WBITS,
144 9, Z_DEFAULT_STRATEGY) != Z_OK){
145
146 fprintf(stderr, "Error initializing deflation!\n");
147 exit(1);
148 }
149}
150
151int compress_file(int in_fd, int out_fd, struct zipentry *ze){
152 Bytef in_buff[RDSZ];
153 Bytef out_buff[RDSZ];
154 unsigned int rdamt, wramt;
155 unsigned long tr = 0;
156 int rtval;
157
158 rdamt = 0;
159
160 zs.avail_in = 0;
161 zs.next_in = in_buff;
162
163 zs.next_out = out_buff;
164 zs.avail_out = (uInt)RDSZ;
165
166 ze->crc = crc32(0L, Z_NULL, 0);
167
168 for(; ;){
169
170 /* If deflate is out of input, fill the input buffer for it */
171 if(zs.avail_in == 0 && zs.avail_out > 0){
172 if((rtval = read(in_fd, in_buff, RDSZ)) == 0)
173 break;
174
175 if(rtval == -1){
176 perror("read");
177 exit(1);
178 }
179
180 rdamt = rtval;
181
182 /* compute the CRC while we're at it */
183 ze->crc = crc32(ze->crc, in_buff, rdamt);
184
185 /* update the total amount read sofar */
186 tr += rdamt;
187
188 zs.next_in = in_buff;
189 zs.avail_in = rdamt;
190 }
191
192 /* deflate the data */
193 if(deflate(&zs, 0) != Z_OK){
194 fprintf(stderr, "Error deflating! %s:%d\n", __FILE__, __LINE__);
195 exit(1);
196 }
197
198 /* If the output buffer is full, dump it to disk */
199 if(zs.avail_out == 0){
200
201 if(write(out_fd, out_buff, RDSZ) != RDSZ){
202 perror("write");
203 exit(1);
204 }
205
206 /* clear the output buffer */
207 zs.next_out = out_buff;
208 zs.avail_out = (uInt)RDSZ;
209 }
210
211 }
212
213 /* If we have any data waiting in the buffer after we're done with the file
214 we can flush it */
215 if(zs.avail_out < RDSZ){
216
217 wramt = RDSZ - zs.avail_out;
218
219 if(write(out_fd, out_buff, wramt) != (int)wramt){
220 perror("write");
221 exit(1);
222 }
223 /* clear the output buffer */
224 zs.next_out = out_buff;
225 zs.avail_out = (uInt)RDSZ;
226 }
227
228
229 /* finish deflation. This purges zlib's internal data buffers */
230 while(deflate(&zs, Z_FINISH) == Z_OK){
231 wramt = RDSZ - zs.avail_out;
232
233 if(write(out_fd, out_buff, wramt) != (int)wramt){
234 perror("write");
235 exit(1);
236 }
237
238 zs.next_out = out_buff;
239 zs.avail_out = (uInt)RDSZ;
240 }
241
242 /* If there's any data left in the buffer, write it out */
243 if(zs.avail_out != RDSZ){
244 wramt = RDSZ - zs.avail_out;
245
246 if(write(out_fd, out_buff, wramt) != (int)wramt){
247 perror("write");
248 exit(1);
249 }
250 }
251
252 /* update fastjar's entry information */
253 ze->usize = (ub4)zs.total_in;
254 ze->csize = (ub4)zs.total_out;
255
256 /* Reset the deflation for the next time around */
257 if(deflateReset(&zs) != Z_OK){
258 fprintf(stderr, "Error resetting deflation\n");
259 exit(1);
260 }
261
262 return 0;
263}
264
265void end_compression(){
266 int rtval;
267
268 /* Oddly enough, zlib always returns Z_DATA_ERROR if you specify no
269 zlib header. Go fig. */
270 if((rtval = deflateEnd(&zs)) != Z_OK && rtval != Z_DATA_ERROR){
271 fprintf(stderr, "Error calling deflateEnd\n");
272 fprintf(stderr, "error: (%d) %s\n", rtval, zs.msg);
273 exit(1);
274 }
275}
276
277
278void init_inflation(){
279
280 memset(&zs, 0, sizeof(z_stream));
281
282 zs.zalloc = Z_NULL;
283 zs.zfree = Z_NULL;
284 zs.opaque = Z_NULL;
285
286 if(inflateInit2(&zs, -15) != Z_OK){
287 fprintf(stderr, "Error initializing deflation!\n");
288 exit(1);
289 }
290
291}
292
293int inflate_file(pb_file *pbf, int out_fd, struct zipentry *ze){
294 Bytef in_buff[RDSZ];
295 Bytef out_buff[RDSZ];
296 unsigned int rdamt;
297 int rtval;
298 ub4 crc = 0;
299
300 zs.avail_in = 0;
301
302 crc = crc32(crc, NULL, 0); /* initialize crc */
303
304 /* loop until we've consumed all the compressed data */
305 for(;;){
306
307 if(zs.avail_in == 0){
308 if((rdamt = pb_read(pbf, in_buff, RDSZ)) == 0)
309 break;
310 else if((int)rdamt < 0){
311 perror("read");
312 exit(1);
313 }
314
315#ifdef DEBUG
316 printf("%d bytes read\n", rdamt);
317#endif
318
319 zs.next_in = in_buff;
320 zs.avail_in = rdamt;
321 }
322
323 zs.next_out = out_buff;
324 zs.avail_out = RDSZ;
325
326 if((rtval = inflate(&zs, 0)) != Z_OK){
327 if(rtval == Z_STREAM_END){
328#ifdef DEBUG
329 printf("end of stream\n");
330#endif
331 if(zs.avail_out != RDSZ){
332 crc = crc32(crc, out_buff, (RDSZ - zs.avail_out));
333
334 if(out_fd >= 0)
335 if(write(out_fd, out_buff, (RDSZ - zs.avail_out)) !=
336 (int)(RDSZ - zs.avail_out)){
337 perror("write");
338 exit(1);
339 }
340 }
341
342 break;
343 } else {
344 fprintf(stderr, "Error inflating file! (%d)\n", rtval);
345 exit(1);
346 }
347 } else {
348 if(zs.avail_out != RDSZ){
349 crc = crc32(crc, out_buff, (RDSZ - zs.avail_out));
350
351 if(out_fd >= 0)
352 if(write(out_fd, out_buff, (RDSZ - zs.avail_out)) !=
353 (int)(RDSZ - zs.avail_out)){
354 perror("write");
355 exit(1);
356 }
357 zs.next_out = out_buff;
358 zs.avail_out = RDSZ;
359 }
360 }
361 }
362#ifdef DEBUG
363 printf("done inflating\n");
364#endif
365
366#ifdef DEBUG
367 printf("%d bytes left over\n", zs.avail_in);
368#endif
369
370#ifdef DEBUG
371 printf("CRC is %x\n", crc);
372#endif
373
374 ze->crc = crc;
375
376 pb_push(pbf, zs.next_in, zs.avail_in);
377
378 ze->usize = zs.total_out;
379
380 inflateReset(&zs);
381 return 0;
382}
383
384/*
385Function name: report_str_error
386args: val Error code returned from zlib.
387purpose: Put out an error message corresponding to error code returned from zlib.
388Be suitably cryptic seeing I don't really know exactly what these errors mean.
389*/
390
391static void report_str_error(int val) {
392 switch(val) {
393 case Z_STREAM_END:
394 break;
395 case Z_NEED_DICT:
396 fprintf(stderr, "Need a dictionary?\n");
397 exit(1);
398 case Z_DATA_ERROR:
399 fprintf(stderr, "Z_DATA_ERROR\n");
400 exit(1);
401 case Z_STREAM_ERROR:
402 fprintf(stderr, "Z_STREAM_ERROR\n");
403 exit(1);
404 case Z_MEM_ERROR:
405 fprintf(stderr, "Z_MEM_ERROR\n");
406 exit(1);
407 case Z_BUF_ERROR:
408 fprintf(stderr, "Z_BUF_ERROR\n");
409 exit(1);
410 case Z_OK:
411 break;
412 default:
413 fprintf(stderr, "Unknown behavior from inflate\n");
414 exit(1);
415 }
416}
417
418/*
419Function name: ez_inflate_str
420args: pbf Pointer to pushback handle for file.
421 csize Compressed size of embedded file.
422 usize Uncompressed size of embedded file.
423purpose: Read in and decompress the contents of an embedded file and store it in a
424byte array.
425returns: Byte array of uncompressed embedded file.
426*/
427
428static Bytef *ez_inflate_str(pb_file *pbf, ub4 csize, ub4 usize) {
429 Bytef *out_buff;
430 Bytef *in_buff;
431 unsigned int rdamt;
432
433 if((zs.next_in = in_buff = (Bytef *) malloc(csize))) {
434 if((zs.next_out = out_buff = (Bytef *) malloc(usize + 1))) {
435 if((rdamt = pb_read(pbf, zs.next_in, csize)) == csize) {
436 zs.avail_in = csize;
437 zs.avail_out = usize;
438 report_str_error(inflate(&zs, 0));
439 free(in_buff);
440 inflateReset(&zs);
441 out_buff[usize] = '\0';
442 }
443 else {
444 fprintf(stderr, "Read failed on input file.\n");
445 fprintf(stderr, "Tried to read %u but read %u instead.\n", csize, rdamt);
446 free(in_buff);
447 free(out_buff);
448 exit(1);
449 }
450 }
451 else {
452 fprintf(stderr, "Malloc of out_buff failed.\n");
453 fprintf(stderr, "Error: %s\n", strerror(errno));
454 free(in_buff);
455 exit(1);
456 }
457 }
458 else {
459 fprintf(stderr, "Malloc of in_buff failed.\n");
460 fprintf(stderr, "Error: %s\n", strerror(errno));
461 exit(1);
462 }
463
464 return out_buff;
465}
466
467/*
468Function name: hrd_inflate_str
469args: pbf Pointer to pushback handle for file.
470 csize Pointer to compressed size of embedded file.
471 usize Pointer to uncompressed size of embedded file.
472purpose: Read and decompress an embedded file into a string. Set csize and usize
473accordingly. This function does the reading for us in the case there is not size
474information in the header for the embedded file.
475returns: Byte array of the contents of the embedded file.
476*/
477
478static Bytef *hrd_inflate_str(pb_file *pbf, ub4 *csize, ub4 *usize) {
479 Bytef *out_buff;
480 Bytef *tmp;
481 Bytef in_buff[RDSZ];
482 unsigned int rdamt;
483 int i;
484 int zret;
485
486 i = 1;
487 out_buff = NULL;
488 zret = Z_OK;
489 while(zret != Z_STREAM_END && (rdamt = pb_read(pbf, in_buff, RDSZ)))
490 {
491 zs.avail_in = rdamt;
492 zs.avail_out = 0;
493 zs.next_in = in_buff;
494 do {
495 if((tmp = (Bytef *) realloc(out_buff, (RDSZ * i) + 1))) {
496 out_buff = tmp;
497 zs.next_out = &(out_buff[(RDSZ * (i - 1)) - zs.avail_out]);
498 zs.avail_out += RDSZ;
499 i++;
500 }
501 else {
502 fprintf(stderr, "Realloc of out_buff failed.\n");
503 fprintf(stderr, "Error: %s\n", strerror(errno));
504 exit(1);
505 }
506 } while((zret = inflate(&zs, 0)) == Z_OK);
507 report_str_error(zret);
508 }
509 pb_push(pbf, zs.next_in, zs.avail_in);
510
511 out_buff[(RDSZ * (i - 1)) - zs.avail_out] = '\0';
512 *usize = zs.total_out;
513 *csize = zs.total_in;
514
515 inflateReset(&zs);
516
517 return out_buff;
518}
519
520/*
521Function name: inflate_string
522args: pbf Pointer to pushback handle for file.
523 csize Pointer to compressed size of embedded file. May be 0 if not set.
524 usize Pointer to uncompressed size of embedded file. May be 0 if not set.
525purpose: Decide the easiest (in computer terms) methos of decompressing this embedded
526file to a string.
527returns: Pointer to a string containing the decompressed contents of the embedded file.
528If csize and usize are not set set them to correct numbers.
529*/
530
531Bytef *inflate_string(pb_file *pbf, ub4 *csize, ub4 *usize) {
532Bytef *ret_buf;
533
534 if(*csize && *usize) ret_buf = ez_inflate_str(pbf, *csize, *usize);
535 else ret_buf = hrd_inflate_str(pbf, csize, usize);
536
537 return ret_buf;
538}
Note: See TracBrowser for help on using the repository browser.