IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 30, 2015, 7:59:48 PM (11 years ago)
Author:
eugene
Message:

more work on compression : cfitsio assumes gzip, not zlib (I have made uncompress general )

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ohana.20150429/src/libfits/extern/gzip.c

    r38339 r38340  
    22# include <gfitsio.h>
    33# include <zlib.h>
     4# define EXTRA_VERBOSE 1
    45
    56/** the two functions in this file re-implement the uncompress function of zlib. 
     
    1011
    1112/* zlib.h -- interface of the 'zlib' general purpose compression library
    12   version 1.2.3, July 18th, 2005
    13 
    14   Copyright (C) 1995-2005 Jean-loup Gailly and Mark Adler
    15 
    16   This software is provided 'as-is', without any express or implied
    17   warranty.  In no event will the authors be held liable for any damages
    18   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 it
    22   freely, subject to the following restrictions:
    23 
    24   1. The origin of this software must not be misrepresented; you must not
    25      claim that you wrote the original software. If you use this software
    26      in a product, an acknowledgment in the product documentation would be
    27      appreciated but is not required.
    28   2. Altered source versions must be plainly marked as such, and must not be
    29      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 Adler
    33   jloup@gzip.org          madler@alumni.caltech.edu
    34 
    35 
    36   The data format used by the zlib library is described by RFCs (Request for
    37   Comments) 1950 to 1952 in the files http://www.ietf.org/rfc/rfc1950.txt
    38   (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).
    3940*/
    4041
    4142/*
    42      The 'zlib' compression library provides in-memory compression and
     43  The 'zlib' compression library provides in-memory compression and
    4344  decompression functions, including integrity checks of the uncompressed
    4445  data.  This version of the library supports only one compression method
     
    4647  stream interface.
    4748
    48      Compression can be done in a single step if the buffers are large
     49  Compression can be done in a single step if the buffers are large
    4950  enough (for example if an input file is mmap'ed), or can be done by
    5051  repeated calls of the compression function.  In the latter case, the
     
    5253  (providing more output space) before each call.
    5354
    54      The compressed data format used by default by the in-memory functions is
     55  The compressed data format used by default by the in-memory functions is
    5556  the zlib format, which is a zlib wrapper documented in RFC 1950, wrapped
    5657  around a deflate stream, which is itself documented in RFC 1951.
    5758
    58      The library also supports reading and writing files in gzip (.gz) format
     59  The library also supports reading and writing files in gzip (.gz) format
    5960  with an interface similar to that of stdio using the functions that start
    6061  with "gz".  The gzip format is different from the zlib format.  gzip is a
    6162  gzip wrapper, documented in RFC 1952, wrapped around a deflate stream.
    6263
    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 memory
     64  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
    6667  and on communications channels.  The gzip format was designed for single-
    6768  file compression on file systems, has a larger header than zlib to maintain
    6869  directory information, and uses a different, slower check method than zlib.
    6970
    70      The library does not install any signal handler. The decoder checks
     71  The library does not install any signal handler. The decoder checks
    7172  the consistency of the compressed data, so the library should never
    7273  crash even in case of corrupted input.
     
    116117}
    117118
    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
    119122int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
    120123{
    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;
    173173}
    174174
     
    182182
    183183  /* 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);
    185185
    186186  stream.next_out = dest;
    187187  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);
    189189
    190190  stream.zalloc = Z_NULL;
     
    192192  stream.opaque = Z_NULL;
    193193
    194   if (0) {
     194  if (EXTRA_VERBOSE) {
    195195    fprintf (stderr, "inp cmp: ");
    196196    for (i = 0; i < sourceLen; i++) {
     
    202202  // the '1' is the compression level: make this a user argument
    203203  // 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);
    205206  if (0) fprintf (stderr, "inp buffers cmp 0: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out);
    206207
    207   if (err != Z_OK) return err;
     208  if (err != Z_OK) ESCAPE(err);
    208209
    209210  // XXX this is written to do the compression in a single pass.  it could be re-done to have
    210211  // the compression occur in a series of steps
    211212  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 
    214213  if (err != Z_STREAM_END) {
    215214    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) {
    221219    fprintf (stderr, "out cmp: ");
    222220    for (i = 0; i < stream.total_out; i++) {
Note: See TracChangeset for help on using the changeset viewer.