Subversion Repositories SvarDOS

Rev

Rev 614 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 614 Rev 1609
Line 12... Line 12...
12
#include "crc32.h"
12
#include "crc32.h"
13
#include "zlib\zlib.h"
13
#include "zlib\zlib.h"
14
 
14
 
15
#include "inf.h"
15
#include "inf.h"
16
 
16
 
17
#define CHUNK 16384
-
 
18
 
-
 
19
/* Decompress from file source to file dest until stream ends or EOF.
17
/* Decompress from file source to file dest until stream ends or EOF.
20
 * inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be allocated
18
 * inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be allocated
21
 * for processing, Z_DATA_ERROR if the deflate data is invalid or incomplete,
19
 * for processing, Z_DATA_ERROR if the deflate data is invalid or incomplete,
22
 * Z_VERSION_ERROR if the version of zlib.h and the version of the library
20
 * Z_VERSION_ERROR if the version of zlib.h and the version of the library
23
 * linked do not match, or Z_ERRNO if there is an error reading or writing the
21
 * linked do not match, or Z_ERRNO if there is an error reading or writing the
24
 * files. */
22
 * files. */
25
int inf(FILE *source, FILE *dest, unsigned char *buff32K, unsigned long *cksum, long streamlen) {
23
int inf(FILE *source, FILE *dest, unsigned char *buffin, unsigned short buffinsz, unsigned char *buffout, unsigned short buffoutsz, unsigned long *cksum, long streamlen) {
26
  int ret;
24
  int ret;
27
  unsigned int have;
25
  unsigned int have;
28
  z_stream strm;
26
  z_stream strm;
29
  unsigned char *in = buff32K;
-
 
30
  unsigned char *out = buff32K + CHUNK;
-
 
31
 
27
 
32
  /* allocate inflate state */
28
  /* allocate inflate state */
33
  strm.zalloc = Z_NULL;
29
  strm.zalloc = Z_NULL;
34
  strm.zfree = Z_NULL;
30
  strm.zfree = Z_NULL;
35
  strm.opaque = Z_NULL;
31
  strm.opaque = Z_NULL;
Line 38... Line 34...
38
  ret = inflateInit2(&strm, -15); /* according to the zlib doc, passing -15 to inflateInit2() means "this is a raw deflate stream" (as opposed to a zlib- or gz- wrapped stream) */
34
  ret = inflateInit2(&strm, -15); /* according to the zlib doc, passing -15 to inflateInit2() means "this is a raw deflate stream" (as opposed to a zlib- or gz- wrapped stream) */
39
  if (ret != Z_OK) return(ret);
35
  if (ret != Z_OK) return(ret);
40
 
36
 
41
  /* decompress until deflate stream ends or end of file */
37
  /* decompress until deflate stream ends or end of file */
42
  do {
38
  do {
43
    strm.avail_in = fread(in, 1, (streamlen > CHUNK ? CHUNK : streamlen), source);
39
    strm.avail_in = fread(buffin, 1, (streamlen > buffinsz ? buffinsz : streamlen), source);
44
    if (ferror(source)) {
40
    if (ferror(source)) {
45
      (void)inflateEnd(&strm);
41
      (void)inflateEnd(&strm);
46
      return(Z_ERRNO);
42
      return(Z_ERRNO);
47
    }
43
    }
48
    streamlen -= strm.avail_in;
44
    streamlen -= strm.avail_in;
49
    if (strm.avail_in == 0) break;
45
    if (strm.avail_in == 0) break;
50
    strm.next_in = in;
46
    strm.next_in = buffin;
51
 
47
 
52
    /* run inflate() on input until output buffer not full */
48
    /* run inflate() on input until output buffer not full */
53
    do {
49
    do {
54
      strm.avail_out = CHUNK;
50
      strm.avail_out = buffoutsz;
55
      strm.next_out = out;
51
      strm.next_out = buffout;
56
      ret = inflate(&strm, Z_NO_FLUSH);
52
      ret = inflate(&strm, Z_NO_FLUSH);
57
      switch (ret) {
53
      switch (ret) {
58
        case Z_NEED_DICT:
54
        case Z_NEED_DICT:
59
          ret = Z_DATA_ERROR;     /* and fall through */
55
          ret = Z_DATA_ERROR;     /* and fall through */
60
        case Z_DATA_ERROR:
56
        case Z_DATA_ERROR:
61
        case Z_MEM_ERROR:
57
        case Z_MEM_ERROR:
62
          (void)inflateEnd(&strm);
58
          (void)inflateEnd(&strm);
63
          return(ret);
59
          return(ret);
64
      }
60
      }
65
      have = CHUNK - strm.avail_out;
61
      have = buffoutsz - strm.avail_out;
66
      if ((fwrite(out, 1, have, dest) != have) || (ferror(dest))) {
62
      if ((fwrite(buffout, 1, have, dest) != have) || (ferror(dest))) {
67
        (void)inflateEnd(&strm);
63
        (void)inflateEnd(&strm);
68
        return(Z_ERRNO);
64
        return(Z_ERRNO);
69
      }
65
      }
70
      /* feed the CRC32 */
66
      /* feed the CRC32 */
71
      crc32_feed(cksum, out, have);
67
      crc32_feed(cksum, buffout, have);
72
    } while (strm.avail_out == 0);
68
    } while (strm.avail_out == 0);
73
 
69
 
74
    /* done when inflate() says it's done */
70
    /* done when inflate() says it's done */
75
  } while (ret != Z_STREAM_END);
71
  } while (ret != Z_STREAM_END);
76
 
72