Subversion Repositories SvarDOS

Rev

Rev 614 | Go to most recent revision | 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
/* Decompress from file source to file dest until stream ends or EOF.
18
 * inf() returns Z_OK on success, Z_MEM_ERROR if memory could not be allocated
19
 * for processing, Z_DATA_ERROR if the deflate data is invalid or incomplete,
20
 * Z_VERSION_ERROR if the version of zlib.h and the version of the library
21
 * linked do not match, or Z_ERRNO if there is an error reading or writing the
22
 * files. */
1609 mateusz.vi 23
int inf(FILE *source, FILE *dest, unsigned char *buffin, unsigned short buffinsz, unsigned char *buffout, unsigned short buffoutsz, unsigned long *cksum, long streamlen) {
219 mateuszvis 24
  int ret;
25
  unsigned int have;
26
  z_stream strm;
27
 
28
  /* allocate inflate state */
29
  strm.zalloc = Z_NULL;
30
  strm.zfree = Z_NULL;
31
  strm.opaque = Z_NULL;
32
  strm.avail_in = 0;
33
  strm.next_in = Z_NULL;
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) */
35
  if (ret != Z_OK) return(ret);
36
 
37
  /* decompress until deflate stream ends or end of file */
38
  do {
1609 mateusz.vi 39
    strm.avail_in = fread(buffin, 1, (streamlen > buffinsz ? buffinsz : streamlen), source);
219 mateuszvis 40
    if (ferror(source)) {
41
      (void)inflateEnd(&strm);
42
      return(Z_ERRNO);
43
    }
44
    streamlen -= strm.avail_in;
45
    if (strm.avail_in == 0) break;
1609 mateusz.vi 46
    strm.next_in = buffin;
219 mateuszvis 47
 
48
    /* run inflate() on input until output buffer not full */
49
    do {
1609 mateusz.vi 50
      strm.avail_out = buffoutsz;
51
      strm.next_out = buffout;
219 mateuszvis 52
      ret = inflate(&strm, Z_NO_FLUSH);
53
      switch (ret) {
54
        case Z_NEED_DICT:
55
          ret = Z_DATA_ERROR;     /* and fall through */
56
        case Z_DATA_ERROR:
57
        case Z_MEM_ERROR:
58
          (void)inflateEnd(&strm);
59
          return(ret);
60
      }
1609 mateusz.vi 61
      have = buffoutsz - strm.avail_out;
62
      if ((fwrite(buffout, 1, have, dest) != have) || (ferror(dest))) {
219 mateuszvis 63
        (void)inflateEnd(&strm);
64
        return(Z_ERRNO);
65
      }
66
      /* feed the CRC32 */
1609 mateusz.vi 67
      crc32_feed(cksum, buffout, have);
219 mateuszvis 68
    } while (strm.avail_out == 0);
69
 
70
    /* done when inflate() says it's done */
71
  } while (ret != Z_STREAM_END);
72
 
73
  /* clean up and return */
74
  (void)inflateEnd(&strm);
75
 
76
  if (Z_STREAM_END) {
77
    return(Z_OK);
78
  } else {
79
    return(Z_DATA_ERROR);
80
  }
81
}