Subversion Repositories SvarDOS

Rev

Rev 269 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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