OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [fastjar/] [compress.c] - Blame information for rev 20

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.