Subversion Repositories SvarDOS

Rev

Rev 614 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
219 mateuszvis 1
/* This file is part of the FDNPKG project. It is an adaptation of the
2
 * "zpipe.c" example of zlib's inflate() and deflate() usage.
3
 *
4
 * Not copyrighted -- provided to the public domain
5
 * original version 1.4  11 December 2005  Mark Adler
6
 * adaptations for FDNPKG integration by Mateusz Viste, 2015
7
 */
8
 
9
#include <stdio.h>
10
#include <string.h>
11
 
12
#include "crc32.h"
13
#include "zlib\zlib.h"
14
 
15
#include "inf.h"
16
 
17
#define CHUNK 16384
18
 
19
/* 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
21
 * 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
23
 * linked do not match, or Z_ERRNO if there is an error reading or writing the
24
 * files. */
25
int inf(FILE *source, FILE *dest, unsigned char *buff32K, unsigned long *cksum, long streamlen) {
26
  int ret;
27
  unsigned int have;
28
  z_stream strm;
29
  unsigned char *in = buff32K;
30
  unsigned char *out = buff32K + CHUNK;
31
 
32
  /* allocate inflate state */
33
  strm.zalloc = Z_NULL;
34
  strm.zfree = Z_NULL;
35
  strm.opaque = Z_NULL;
36
  strm.avail_in = 0;
37
  strm.next_in = Z_NULL;
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) */
39
  if (ret != Z_OK) return(ret);
40
 
41
  /* decompress until deflate stream ends or end of file */
42
  do {
43
    strm.avail_in = fread(in, 1, (streamlen > CHUNK ? CHUNK : streamlen), source);
44
    if (ferror(source)) {
45
      (void)inflateEnd(&strm);
46
      return(Z_ERRNO);
47
    }
48
    streamlen -= strm.avail_in;
49
    if (strm.avail_in == 0) break;
50
    strm.next_in = in;
51
 
52
    /* run inflate() on input until output buffer not full */
53
    do {
54
      strm.avail_out = CHUNK;
55
      strm.next_out = out;
56
      ret = inflate(&strm, Z_NO_FLUSH);
57
      switch (ret) {
58
        case Z_NEED_DICT:
59
          ret = Z_DATA_ERROR;     /* and fall through */
60
        case Z_DATA_ERROR:
61
        case Z_MEM_ERROR:
62
          (void)inflateEnd(&strm);
63
          return(ret);
64
      }
65
      have = CHUNK - strm.avail_out;
66
      if ((fwrite(out, 1, have, dest) != have) || (ferror(dest))) {
67
        (void)inflateEnd(&strm);
68
        return(Z_ERRNO);
69
      }
70
      /* feed the CRC32 */
71
      crc32_feed(cksum, out, have);
72
    } while (strm.avail_out == 0);
73
 
74
    /* done when inflate() says it's done */
75
  } while (ret != Z_STREAM_END);
76
 
77
  /* clean up and return */
78
  (void)inflateEnd(&strm);
79
 
80
  if (Z_STREAM_END) {
81
    return(Z_OK);
82
  } else {
83
    return(Z_DATA_ERROR);
84
  }
85
}