IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 8, 2015, 11:01:33 AM (11 years ago)
Author:
eugene
Message:

tap-ify zlib test code; fix windowBits problem (make libfits compatible with older <= 1.2.3.4 versions of zlib); pass catcompress to subcatalogs; force compress and format updates for catalogs even if they already exist

File:
1 edited

Legend:

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

    r38342 r38427  
    119119# define ESCAPE(RET) { fprintf (stderr, "gzip error in %s @ %s:%d\n", __func__, __FILE__, __LINE__); return (RET); }
    120120
    121 // NOTE: CFITSIO uses gzip format, not just zlib
    122 int gfits_uncompress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
    123 {
     121int gfits_compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen) {
     122
    124123  z_stream stream;
    125124  int i, err;
     
    130129  /* Check for source > 64K on 16-bit machine: */
    131130  if ((uLong)stream.avail_in != sourceLen) ESCAPE (Z_BUF_ERROR);
    132  
     131
    133132  stream.next_out = dest;
    134   stream.avail_out = (uInt)*destLen;
     133  stream.avail_out = (uInt)*destLen; // allocated space
    135134  if ((uLong)stream.avail_out != *destLen) ESCAPE (Z_BUF_ERROR);
    136  
     135
    137136  stream.zalloc = Z_NULL;
    138137  stream.zfree  = Z_NULL;
    139138  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 }
    174 
    175 int gfits_compress (Bytef *dest, uLongf *destLen, const Bytef *source, uLong sourceLen)
    176 {
    177   z_stream stream;
    178   int i, err;
    179  
    180   stream.next_in = (Bytef*)source;
    181   stream.avail_in = (uInt)sourceLen;
    182 
    183   /* Check for source > 64K on 16-bit machine: */
    184   if ((uLong)stream.avail_in != sourceLen) ESCAPE (Z_BUF_ERROR);
    185 
    186   stream.next_out = dest;
    187   stream.avail_out = (uInt)*destLen; // allocated space
    188   if ((uLong)stream.avail_out != *destLen) ESCAPE (Z_BUF_ERROR);
    189 
    190   stream.zalloc = Z_NULL;
    191   stream.zfree  = Z_NULL;
    192   stream.opaque = Z_NULL;
    193139
    194140  if (EXTRA_VERBOSE) {
     
    203149  // NOTE: cfitsio uses a fixed value of 1
    204150  // I am using deflateInit2 because cfitsio expects gzip, not just zlib
     151  // windowBits = 31 = (15 + 16) = (2^15 window bits) + (create a gzip stream)
    205152  err = deflateInit2(&stream, 1, Z_DEFLATED, 31, 8, Z_DEFAULT_STRATEGY);
    206153  if (0) fprintf (stderr, "inp buffers cmp 0: %d => %d => %d\n", stream.avail_in, stream.avail_out, (int) stream.total_out);
     
    230177  return err;
    231178}
     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.