IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 12, 2015, 6:18:23 PM (11 years ago)
Author:
eugene
Message:

merge changes from EAM dev branch ohana.20150429

Location:
trunk/Ohana
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana

  • trunk/Ohana/src/libfits/extern/gzip.c

    r23816 r38441  
    22# include <gfitsio.h>
    33# include <zlib.h>
     4# define EXTRA_VERBOSE 0
    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 int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
    120 {
    121     z_stream stream;
    122     int 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 = (alloc_func)0;
    134     stream.zfree = (free_func)0;
    135 
    136     // MAX_WBITS = 15
    137     err = inflateInit2(&stream, -15);
    138     if (err != Z_OK) return err;
    139 
    140     err = inflate(&stream, Z_FINISH);
    141     if (err != Z_STREAM_END) {
    142         inflateEnd(&stream);
    143         if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0))
    144             return Z_DATA_ERROR;
    145         return err;
    146     }
    147     assert (stream.total_out <= *destLen);
    148     *destLen = stream.total_out;
    149 
    150     err = inflateEnd(&stream);
    151     return err;
     119# define ESCAPE(RET) { fprintf (stderr, "gzip error in %s @ %s:%d\n", __func__, __FILE__, __LINE__); return (RET); }
     120
     121int gfits_compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) {
     122
     123  z_stream stream;
     124  int i, err;
     125 
     126  stream.next_in = (Bytef*)source;
     127  stream.avail_in = (uInt)sourceLen;
     128
     129  /* Check for source > 64K on 16-bit machine: */
     130  if ((uLong)stream.avail_in != sourceLen) ESCAPE (Z_BUF_ERROR);
     131
     132  stream.next_out = dest;
     133  stream.avail_out = (uInt)*destLen; // allocated space
     134  if ((uLong)stream.avail_out != *destLen) ESCAPE (Z_BUF_ERROR);
     135
     136  stream.zalloc = Z_NULL;
     137  stream.zfree  = Z_NULL;
     138  stream.opaque = Z_NULL;
     139
     140  if (EXTRA_VERBOSE) {
     141    fprintf (stderr, "inp cmp: ");
     142    for (i = 0; i < sourceLen; i++) {
     143      fprintf (stderr, "0x%02hhx ", source[i]);
     144    }
     145    fprintf (stderr, "\n");
     146  }
     147
     148  // the '1' is the compression level: make this a user argument
     149  // NOTE: cfitsio uses a fixed value of 1
     150  // I am using deflateInit2 because cfitsio expects gzip, not just zlib
     151  // windowBits = 31 = (15 + 16) = (2^15 window bits) + (create a gzip stream)
     152  err = deflateInit2(&stream, 1, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
     153  if (0) fprintf (stderr, "inp buffers cmp 0: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out);
     154
     155  if (err != Z_OK) ESCAPE(err);
     156
     157  // XXX this is written to do the compression in a single pass.  it could be re-done to have
     158  // the compression occur in a series of steps
     159  err = deflate(&stream, Z_FINISH);
     160  if (err != Z_STREAM_END) {
     161    err = deflateEnd(&stream);
     162    ESCAPE (Z_BUF_ERROR);
     163  }
     164
     165  if (EXTRA_VERBOSE) {
     166    fprintf (stderr, "out cmp: ");
     167    for (i = 0; i < stream.total_out; i++) {
     168      fprintf (stderr, "0x%02hhx ", dest[i]);
     169    }
     170    fprintf (stderr, "\n");
     171  }
     172
     173  assert (stream.total_out <= *destLen);
     174  *destLen = stream.total_out;
     175 
     176  err = deflateEnd(&stream);
     177  return err;
    152178}
     179
     180// NOTE: CFITSIO uses gzip format, not just zlib
     181int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) {
     182
     183  z_stream stream;
     184  int i, err;
     185 
     186  stream.next_in = (Bytef*)source;
     187  stream.avail_in = (uInt)sourceLen;
     188
     189  /* Check for source > 64K on 16-bit machine: */
     190  if ((uLong)stream.avail_in != sourceLen) ESCAPE (Z_BUF_ERROR);
     191 
     192  stream.next_out = dest;
     193  stream.avail_out = (uInt)*destLen;
     194  if ((uLong)stream.avail_out != *destLen) ESCAPE (Z_BUF_ERROR);
     195 
     196  stream.zalloc = Z_NULL;
     197  stream.zfree  = Z_NULL;
     198  stream.opaque = Z_NULL;
     199 
     200  if (EXTRA_VERBOSE) {
     201    fprintf (stderr, "inp unc: ");
     202    for (i = 0; i < sourceLen; i++) {
     203      fprintf (stderr, "0x%02hhx ", source[i]);
     204    }
     205    fprintf (stderr, "\n");
     206  }
     207 
     208  // windowBits = 47 = (15 + 32) = (2^15 window bits) + (test for either gzip or zlib
     209  // streams).  NOTE: modern versions of zlib (version > 1.2.3.4) allow for windowBits = 0
     210  // (or 16 or 32) to use the compressed stream windowBits value.  IPP cluster still has
     211  // zlib version = 1.2.3, so we cannot use this feature.
     212  err = inflateInit2(&stream, 47);
     213  if (err != Z_OK) ESCAPE (err);
     214 
     215  err = inflate(&stream, Z_FINISH);
     216  if (err != Z_STREAM_END) {
     217    inflateEnd(&stream);
     218    if (err == Z_NEED_DICT || (err == Z_BUF_ERROR && stream.avail_in == 0)) ESCAPE (Z_DATA_ERROR);
     219    ESCAPE(err);
     220  }
     221 
     222  if (EXTRA_VERBOSE) {
     223    fprintf (stderr, "out unc: ");
     224    for (i = 0; i < stream.total_out; i++) {
     225      fprintf (stderr, "0x%02hhx ", dest[i]);
     226    }
     227    fprintf (stderr, "\n");
     228  }
     229
     230  assert (stream.total_out <= *destLen);
     231  *destLen = stream.total_out;
     232
     233  err = inflateEnd(&stream);
     234  return err;
     235}
     236
Note: See TracChangeset for help on using the changeset viewer.