- Timestamp:
- May 30, 2015, 7:59:48 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/ohana.20150429/src/libfits/extern/gzip.c
r38339 r38340 2 2 # include <gfitsio.h> 3 3 # include <zlib.h> 4 # define EXTRA_VERBOSE 1 4 5 5 6 /** the two functions in this file re-implement the uncompress function of zlib. … … 10 11 11 12 /* zlib.h -- interface of the 'zlib' general purpose compression library 12 version 1.2.3, July 18th, 200513 14 Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler15 16 This software is provided 'as-is', without any express or implied17 warranty. In no event will the authors be held liable for any damages18 arising from the use of this software.19 20 Permission is granted to anyone to use this software for any purpose,21 including commercial applications, and to alter it and redistribute it22 freely, subject to the following restrictions:23 24 1. The origin of this software must not be misrepresented; you must not25 claim that you wrote the original software. If you use this software26 in a product, an acknowledgment in the product documentation would be27 appreciated but is not required.28 2. Altered source versions must be plainly marked as such, and must not be29 misrepresented as being the original software.30 3. This notice may not be removed or altered from any source distribution.31 32 Jean-loup Gailly Mark Adler33 jloup@gzip.org madler@alumni.caltech.edu34 35 36 The data format used by the zlib library is described by RFCs (Request for37 Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt38 (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).13 version 1.2.3, July 18th, 2005 14 15 Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler 16 17 This software is provided 'as-is', without any express or implied 18 warranty. In no event will the authors be held liable for any damages 19 arising from the use of this software. 20 21 Permission is granted to anyone to use this software for any purpose, 22 including commercial applications, and to alter it and redistribute it 23 freely, subject to the following restrictions: 24 25 1. The origin of this software must not be misrepresented; you must not 26 claim that you wrote the original software. If you use this software 27 in a product, an acknowledgment in the product documentation would be 28 appreciated but is not required. 29 2. Altered source versions must be plainly marked as such, and must not be 30 misrepresented as being the original software. 31 3. This notice may not be removed or altered from any source distribution. 32 33 Jean-loup Gailly Mark Adler 34 jloup@gzip.org madler@alumni.caltech.edu 35 36 37 The data format used by the zlib library is described by RFCs (Request for 38 Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt 39 (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format). 39 40 */ 40 41 41 42 /* 42 The 'zlib' compression library provides in-memory compression and43 The 'zlib' compression library provides in-memory compression and 43 44 decompression functions, including integrity checks of the uncompressed 44 45 data. This version of the library supports only one compression method … … 46 47 stream interface. 47 48 48 Compression can be done in a single step if the buffers are large49 Compression can be done in a single step if the buffers are large 49 50 enough (for example if an input file is mmap'ed), or can be done by 50 51 repeated calls of the compression function. In the latter case, the … … 52 53 (providing more output space) before each call. 53 54 54 The compressed data format used by default by the in-memory functions is55 The compressed data format used by default by the in-memory functions is 55 56 the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped 56 57 around a deflate stream, which is itself documented in RFC 1951. 57 58 58 The library also supports reading and writing files in gzip (.gz) format59 The library also supports reading and writing files in gzip (.gz) format 59 60 with an interface similar to that of stdio using the functions that start 60 61 with "gz". The gzip format is different from the zlib format. gzip is a 61 62 gzip wrapper, documented in RFC 1952, wrapped around a deflate stream. 62 63 63 This library can optionally read and write gzip streams in memory as well.64 65 The zlib format was designed to be compact and fast for use in memory64 This library can optionally read and write gzip streams in memory as well. 65 66 The zlib format was designed to be compact and fast for use in memory 66 67 and on communications channels. The gzip format was designed for single- 67 68 file compression on file systems, has a larger header than zlib to maintain 68 69 directory information, and uses a different, slower check method than zlib. 69 70 70 The library does not install any signal handler. The decoder checks71 The library does not install any signal handler. The decoder checks 71 72 the consistency of the compressed data, so the library should never 72 73 crash even in case of corrupted input. … … 116 117 } 117 118 118 // XXX ??? is cfitsio writing data which is incompatible with zlib ??? 119 # define ESCAPE(RET) { fprintf (stderr, "gzip error in %s @ %s:%d\n", __func__, __FILE__, __LINE__); return (RET); } 120 121 // NOTE: CFITSIO uses gzip format, not just zlib 119 122 int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) 120 123 { 121 z_stream stream; 122 int i, err; 123 124 stream.next_in = (Bytef*)source; 125 stream.avail_in = (uInt)sourceLen; 126 /* Check for source > 64K on 16-bit machine: */ 127 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR; 128 129 stream.next_out = dest; 130 stream.avail_out = (uInt)*destLen; 131 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR; 132 133 stream.zalloc = Z_NULL; 134 stream.zfree = Z_NULL; 135 stream.opaque = Z_NULL; 136 137 if (0) { 138 fprintf (stderr, "inp unc: "); 139 for (i = 0; i < sourceLen; i++) { 140 fprintf (stderr, "0x%02hhx ", source[i]); 141 } 142 fprintf (stderr, "\n"); 143 } 144 145 // err = inflateInit2(&stream, -15); 146 err = inflateInit(&stream); 147 if (0) fprintf (stderr, "inp buffers unc 0: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out); 148 149 if (err != Z_OK) return err; 150 151 err = inflate(&stream, Z_FINISH); 152 if (0) fprintf (stderr, "out buffers unc 1: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out); 153 if (err != Z_STREAM_END) { 154 inflateEnd(&stream); 155 if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) return Z_DATA_ERROR; 156 return err; 157 } 158 if (0) fprintf (stderr, "out buffers unc 2: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out); 159 160 if (0) { 161 fprintf (stderr, "out unc: "); 162 for (i = 0; i < stream.total_out; i++) { 163 fprintf (stderr, "0x%02hhx ", dest[i]); 164 } 165 fprintf (stderr, "\n"); 166 } 167 168 assert (stream.total_out <= *destLen); 169 *destLen = stream.total_out; 170 171 err = inflateEnd(&stream); 172 return err; 124 z_stream stream; 125 int i, err; 126 127 stream.next_in = (Bytef*)source; 128 stream.avail_in = (uInt)sourceLen; 129 130 /* Check for source > 64K on 16-bit machine: */ 131 if ((uLong)stream.avail_in != sourceLen) ESCAPE (Z_BUF_ERROR); 132 133 stream.next_out = dest; 134 stream.avail_out = (uInt)*destLen; 135 if ((uLong)stream.avail_out != *destLen) ESCAPE (Z_BUF_ERROR); 136 137 stream.zalloc = Z_NULL; 138 stream.zfree = Z_NULL; 139 stream.opaque = Z_NULL; 140 141 if (EXTRA_VERBOSE) { 142 fprintf (stderr, "inp unc: "); 143 for (i = 0; i < sourceLen; i++) { 144 fprintf (stderr, "0x%02hhx ", source[i]); 145 } 146 fprintf (stderr, "\n"); 147 } 148 149 // a value of 32 tells inflate to use the window size used in the compression AND to look for both zlib and gzip headers 150 err = inflateInit2(&stream, 32); 151 if (err != Z_OK) ESCAPE (err); 152 153 err = inflate(&stream, Z_FINISH); 154 if (err != Z_STREAM_END) { 155 inflateEnd(&stream); 156 if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) ESCAPE (Z_DATA_ERROR); 157 ESCAPE(err); 158 } 159 160 if (EXTRA_VERBOSE) { 161 fprintf (stderr, "out unc: "); 162 for (i = 0; i < stream.total_out; i++) { 163 fprintf (stderr, "0x%02hhx ", dest[i]); 164 } 165 fprintf (stderr, "\n"); 166 } 167 168 assert (stream.total_out <= *destLen); 169 *destLen = stream.total_out; 170 171 err = inflateEnd(&stream); 172 return err; 173 173 } 174 174 … … 182 182 183 183 /* Check for source > 64K on 16-bit machine: */ 184 if ((uLong)stream.avail_in != sourceLen) return Z_BUF_ERROR;184 if ((uLong)stream.avail_in != sourceLen) ESCAPE (Z_BUF_ERROR); 185 185 186 186 stream.next_out = dest; 187 187 stream.avail_out = (uInt)*destLen; // allocated space 188 if ((uLong)stream.avail_out != *destLen) return Z_BUF_ERROR;188 if ((uLong)stream.avail_out != *destLen) ESCAPE (Z_BUF_ERROR); 189 189 190 190 stream.zalloc = Z_NULL; … … 192 192 stream.opaque = Z_NULL; 193 193 194 if ( 0) {194 if (EXTRA_VERBOSE) { 195 195 fprintf (stderr, "inp cmp: "); 196 196 for (i = 0; i < sourceLen; i++) { … … 202 202 // the '1' is the compression level: make this a user argument 203 203 // NOTE: cfitsio uses a fixed value of 1 204 err = deflateInit(&stream, 1); 204 // I am using deflateInit2 because cfitsio expects gzip, not just zlib 205 err = deflateInit2(&stream, 1, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY); 205 206 if (0) fprintf (stderr, "inp buffers cmp 0: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out); 206 207 207 if (err != Z_OK) return err;208 if (err != Z_OK) ESCAPE(err); 208 209 209 210 // XXX this is written to do the compression in a single pass. it could be re-done to have 210 211 // the compression occur in a series of steps 211 212 err = deflate(&stream, Z_FINISH); 212 if (0) fprintf (stderr, "out buffers cmp 1: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out);213 214 213 if (err != Z_STREAM_END) { 215 214 err = deflateEnd(&stream); 216 return Z_BUF_ERROR; 217 } 218 if (0) fprintf (stderr, "out buffers cmp 2: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out); 219 220 if (0) { 215 ESCAPE (Z_BUF_ERROR); 216 } 217 218 if (EXTRA_VERBOSE) { 221 219 fprintf (stderr, "out cmp: "); 222 220 for (i = 0; i < stream.total_out; i++) {
Note:
See TracChangeset
for help on using the changeset viewer.
