~ubuntu-branches/ubuntu/wily/pyfits/wily-proposed

« back to all changes in this revision

Viewing changes to src/inftrees.c

  • Committer: Package Import Robot
  • Author(s): Aurelien Jarno
  • Date: 2013-12-07 16:18:48 UTC
  • mfrom: (1.1.11)
  • Revision ID: package-import@ubuntu.com-20131207161848-mcw0saz0iprjhbju
Tags: 1:3.2-1
* New upstream version.
* Bump Standards-Version to 3.9.5 (no changes).
* Remove build-depends on zlib1g-dev and remove patches/01-zlib.diff.
* Add build-depends on libcfitsio3-dev and add 
  patches/01-system-cfitsio.diff.
* Update debian/copyright.
* Install upstream changelog now that it is provided in the upstream
  tarball.
* Don't compress the binary packages with xz, it's no the dpkg's default.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* $Id$
2
 
*/
3
 
 
4
 
/*****************************************************************************/
5
 
/*                                                                           */
6
 
/* This file, inffast.c, contains the code required to compress and          */
7
 
/* uncompress data using the GZIP_1 compression format.                      */
8
 
/*                                                                           */
9
 
/* Copyright (C) 2004 Association of Universities for Research in Astronomy  */
10
 
/* (AURA)                                                                    */
11
 
/*                                                                           */
12
 
/* Redistribution and use in source and binary forms, with or without        */
13
 
/* modification, are permitted provided that the following conditions are    */
14
 
/* met:                                                                      */
15
 
/*                                                                           */
16
 
/*    1. Redistributions of source code must retain the above copyright      */
17
 
/*      notice, this list of conditions and the following disclaimer.        */
18
 
/*                                                                           */
19
 
/*    2. Redistributions in binary form must reproduce the above             */
20
 
/*      copyright notice, this list of conditions and the following          */
21
 
/*      disclaimer in the documentation and/or other materials provided      */
22
 
/*      with the distribution.                                               */
23
 
/*                                                                           */
24
 
/*    3. The name of AURA and its representatives may not be used to         */
25
 
/*      endorse or promote products derived from this software without       */
26
 
/*      specific prior written permission.                                   */
27
 
/*                                                                           */
28
 
/* THIS SOFTWARE IS PROVIDED BY AURA ``AS IS'' AND ANY EXPRESS OR IMPLIED    */
29
 
/* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF      */
30
 
/* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE                  */
31
 
/* DISCLAIMED. IN NO EVENT SHALL AURA BE LIABLE FOR ANY DIRECT, INDIRECT,    */
32
 
/* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
33
 
/* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS     */
34
 
/* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND    */
35
 
/* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR     */
36
 
/* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE    */
37
 
/* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH          */
38
 
/* DAMAGE.                                                                   */
39
 
/*                                                                           */
40
 
/* This code was copied and heavily modified from the ZLIB compression       */
41
 
/* library that was written by Jean-loup Gailly and Mark Adler.  That        */
42
 
/* software containes the following copyright and warranty notices:          */
43
 
/*                                                                           */
44
 
/*  Copyright (C) 1995-2010 Jean-loup Gailly and Mark Adler                  */
45
 
/*                                                                           */
46
 
/*  This software is provided 'as-is', without any express or implied        */
47
 
/*  warranty.  In no event will the authors be held liable for any damages   */
48
 
/*  arising from the use of this software.                                   */
49
 
/*                                                                           */
50
 
/*  Permission is granted to anyone to use this software for any purpose,    */
51
 
/*  including commercial applications, and to alter it and redistribute it   */
52
 
/*  freely, subject to the following restrictions:                           */
53
 
/*                                                                           */
54
 
/*  1. The origin of this software must not be misrepresented; you must not  */
55
 
/*     claim that you wrote the original software. If you use this software  */
56
 
/*     in a product, an acknowledgment in the product documentation would be */
57
 
/*     appreciated but is not required.                                      */
58
 
/*  2. Altered source versions must be plainly marked as such, and must not  */
59
 
/*     be misrepresented as being the original software.                     */
60
 
/*  3. This notice may not be removed or altered from any source             */
61
 
/*     distribution.                                                         */
62
 
/*                                                                           */
63
 
/*  Jean-loup Gailly        Mark Adler                                       */
64
 
/*  jloup@gzip.org          madler@alumni.caltech.edu                        */
65
 
/*                                                                           */
66
 
/*                                                                           */
67
 
/*  The data format used by the zlib library is described by RFCs (Request   */
68
 
/*  for Comments) 1950 to 1952 in the files                                  */
69
 
/*  http://www.ietf.org/rfc/rfc1950.txt (zlib format),                       */
70
 
/*  rfc1951.txt (deflate format) and rfc1952.txt (gzip format).              */
71
 
/*                                                                           */
72
 
/*****************************************************************************/
73
 
 
74
 
/* This code was copied unmodified from ZLIB inftrees.c -- generate Huffman  */
75
 
/* trees for efficient decoding                                              */
76
 
 
77
 
#include "zutil.h"
78
 
#include "inftrees.h"
79
 
 
80
 
#define MAXBITS 15
81
 
 
82
 
const char inflate_copyright[] =
83
 
   " inflate 1.2.5 Copyright 1995-2010 Mark Adler ";
84
 
/*
85
 
  If you use the zlib library in a product, an acknowledgment is welcome
86
 
  in the documentation of your product. If for some reason you cannot
87
 
  include such an acknowledgment, I would appreciate that you keep this
88
 
  copyright string in the executable of your product.
89
 
 */
90
 
 
91
 
/*
92
 
   Build a set of tables to decode the provided canonical Huffman code.
93
 
   The code lengths are lens[0..codes-1].  The result starts at *table,
94
 
   whose indices are 0..2^bits-1.  work is a writable array of at least
95
 
   lens shorts, which is used as a work area.  type is the type of code
96
 
   to be generated, CODES, LENS, or DISTS.  On return, zero is success,
97
 
   -1 is an invalid code, and +1 means that ENOUGH isn't enough.  table
98
 
   on return points to the next available entry's address.  bits is the
99
 
   requested root table index bits, and on return it is the actual root
100
 
   table index bits.  It will differ if the request is greater than the
101
 
   longest code or if it is less than the shortest code.
102
 
 */
103
 
int ZLIB_INTERNAL inflate_table(type, lens, codes, table, bits, work)
104
 
codetype type;
105
 
unsigned short FAR *lens;
106
 
unsigned codes;
107
 
code FAR * FAR *table;
108
 
unsigned FAR *bits;
109
 
unsigned short FAR *work;
110
 
{
111
 
    unsigned len;               /* a code's length in bits */
112
 
    unsigned sym;               /* index of code symbols */
113
 
    unsigned min, max;          /* minimum and maximum code lengths */
114
 
    unsigned root;              /* number of index bits for root table */
115
 
    unsigned curr;              /* number of index bits for current table */
116
 
    unsigned drop;              /* code bits to drop for sub-table */
117
 
    int left;                   /* number of prefix codes available */
118
 
    unsigned used;              /* code entries in table used */
119
 
    unsigned huff;              /* Huffman code */
120
 
    unsigned incr;              /* for incrementing code, index */
121
 
    unsigned fill;              /* index for replicating entries */
122
 
    unsigned low;               /* low bits for current root entry */
123
 
    unsigned mask;              /* mask for low root bits */
124
 
    code here;                  /* table entry for duplication */
125
 
    code FAR *next;             /* next available space in table */
126
 
    const unsigned short FAR *base;     /* base value table to use */
127
 
    const unsigned short FAR *extra;    /* extra bits table to use */
128
 
    int end;                    /* use base and extra for symbol > end */
129
 
    unsigned short count[MAXBITS+1];    /* number of codes of each length */
130
 
    unsigned short offs[MAXBITS+1];     /* offsets in table for each length */
131
 
    static const unsigned short lbase[31] = { /* Length codes 257..285 base */
132
 
        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
133
 
        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
134
 
    static const unsigned short lext[31] = { /* Length codes 257..285 extra */
135
 
        16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 18, 18, 18, 18,
136
 
        19, 19, 19, 19, 20, 20, 20, 20, 21, 21, 21, 21, 16, 73, 195};
137
 
    static const unsigned short dbase[32] = { /* Distance codes 0..29 base */
138
 
        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
139
 
        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
140
 
        8193, 12289, 16385, 24577, 0, 0};
141
 
    static const unsigned short dext[32] = { /* Distance codes 0..29 extra */
142
 
        16, 16, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22,
143
 
        23, 23, 24, 24, 25, 25, 26, 26, 27, 27,
144
 
        28, 28, 29, 29, 64, 64};
145
 
 
146
 
    /*
147
 
       Process a set of code lengths to create a canonical Huffman code.  The
148
 
       code lengths are lens[0..codes-1].  Each length corresponds to the
149
 
       symbols 0..codes-1.  The Huffman code is generated by first sorting the
150
 
       symbols by length from short to long, and retaining the symbol order
151
 
       for codes with equal lengths.  Then the code starts with all zero bits
152
 
       for the first code of the shortest length, and the codes are integer
153
 
       increments for the same length, and zeros are appended as the length
154
 
       increases.  For the deflate format, these bits are stored backwards
155
 
       from their more natural integer increment ordering, and so when the
156
 
       decoding tables are built in the large loop below, the integer codes
157
 
       are incremented backwards.
158
 
 
159
 
       This routine assumes, but does not check, that all of the entries in
160
 
       lens[] are in the range 0..MAXBITS.  The caller must assure this.
161
 
       1..MAXBITS is interpreted as that code length.  zero means that that
162
 
       symbol does not occur in this code.
163
 
 
164
 
       The codes are sorted by computing a count of codes for each length,
165
 
       creating from that a table of starting indices for each length in the
166
 
       sorted table, and then entering the symbols in order in the sorted
167
 
       table.  The sorted table is work[], with that space being provided by
168
 
       the caller.
169
 
 
170
 
       The length counts are used for other purposes as well, i.e. finding
171
 
       the minimum and maximum length codes, determining if there are any
172
 
       codes at all, checking for a valid set of lengths, and looking ahead
173
 
       at length counts to determine sub-table sizes when building the
174
 
       decoding tables.
175
 
     */
176
 
 
177
 
    /* accumulate lengths for codes (assumes lens[] all in 0..MAXBITS) */
178
 
    for (len = 0; len <= MAXBITS; len++)
179
 
        count[len] = 0;
180
 
    for (sym = 0; sym < codes; sym++)
181
 
        count[lens[sym]]++;
182
 
 
183
 
    /* bound code lengths, force root to be within code lengths */
184
 
    root = *bits;
185
 
    for (max = MAXBITS; max >= 1; max--)
186
 
        if (count[max] != 0) break;
187
 
    if (root > max) root = max;
188
 
    if (max == 0) {                     /* no symbols to code at all */
189
 
        here.op = (unsigned char)64;    /* invalid code marker */
190
 
        here.bits = (unsigned char)1;
191
 
        here.val = (unsigned short)0;
192
 
        *(*table)++ = here;             /* make a table to force an error */
193
 
        *(*table)++ = here;
194
 
        *bits = 1;
195
 
        return 0;     /* no symbols, but wait for decoding to report error */
196
 
    }
197
 
    for (min = 1; min < max; min++)
198
 
        if (count[min] != 0) break;
199
 
    if (root < min) root = min;
200
 
 
201
 
    /* check for an over-subscribed or incomplete set of lengths */
202
 
    left = 1;
203
 
    for (len = 1; len <= MAXBITS; len++) {
204
 
        left <<= 1;
205
 
        left -= count[len];
206
 
        if (left < 0) return -1;        /* over-subscribed */
207
 
    }
208
 
    if (left > 0 && (type == CODES || max != 1))
209
 
        return -1;                      /* incomplete set */
210
 
 
211
 
    /* generate offsets into symbol table for each length for sorting */
212
 
    offs[1] = 0;
213
 
    for (len = 1; len < MAXBITS; len++)
214
 
        offs[len + 1] = offs[len] + count[len];
215
 
 
216
 
    /* sort symbols by length, by symbol order within each length */
217
 
    for (sym = 0; sym < codes; sym++)
218
 
        if (lens[sym] != 0) work[offs[lens[sym]]++] = (unsigned short)sym;
219
 
 
220
 
    /*
221
 
       Create and fill in decoding tables.  In this loop, the table being
222
 
       filled is at next and has curr index bits.  The code being used is huff
223
 
       with length len.  That code is converted to an index by dropping drop
224
 
       bits off of the bottom.  For codes where len is less than drop + curr,
225
 
       those top drop + curr - len bits are incremented through all values to
226
 
       fill the table with replicated entries.
227
 
 
228
 
       root is the number of index bits for the root table.  When len exceeds
229
 
       root, sub-tables are created pointed to by the root entry with an index
230
 
       of the low root bits of huff.  This is saved in low to check for when a
231
 
       new sub-table should be started.  drop is zero when the root table is
232
 
       being filled, and drop is root when sub-tables are being filled.
233
 
 
234
 
       When a new sub-table is needed, it is necessary to look ahead in the
235
 
       code lengths to determine what size sub-table is needed.  The length
236
 
       counts are used for this, and so count[] is decremented as codes are
237
 
       entered in the tables.
238
 
 
239
 
       used keeps track of how many table entries have been allocated from the
240
 
       provided *table space.  It is checked for LENS and DIST tables against
241
 
       the constants ENOUGH_LENS and ENOUGH_DISTS to guard against changes in
242
 
       the initial root table size constants.  See the comments in inftrees.h
243
 
       for more information.
244
 
 
245
 
       sym increments through all symbols, and the loop terminates when
246
 
       all codes of length max, i.e. all codes, have been processed.  This
247
 
       routine permits incomplete codes, so another loop after this one fills
248
 
       in the rest of the decoding tables with invalid code markers.
249
 
     */
250
 
 
251
 
    /* set up for code type */
252
 
    switch (type) {
253
 
    case CODES:
254
 
        base = extra = work;    /* dummy value--not used */
255
 
        end = 19;
256
 
        break;
257
 
    case LENS:
258
 
        base = lbase;
259
 
        base -= 257;
260
 
        extra = lext;
261
 
        extra -= 257;
262
 
        end = 256;
263
 
        break;
264
 
    default:            /* DISTS */
265
 
        base = dbase;
266
 
        extra = dext;
267
 
        end = -1;
268
 
    }
269
 
 
270
 
    /* initialize state for loop */
271
 
    huff = 0;                   /* starting code */
272
 
    sym = 0;                    /* starting code symbol */
273
 
    len = min;                  /* starting code length */
274
 
    next = *table;              /* current table to fill in */
275
 
    curr = root;                /* current table index bits */
276
 
    drop = 0;                   /* current bits to drop from code for index */
277
 
    low = (unsigned)(-1);       /* trigger new sub-table when len > root */
278
 
    used = 1U << root;          /* use root table entries */
279
 
    mask = used - 1;            /* mask for comparing low */
280
 
 
281
 
    /* check available table space */
282
 
    if ((type == LENS && used >= ENOUGH_LENS) ||
283
 
        (type == DISTS && used >= ENOUGH_DISTS))
284
 
        return 1;
285
 
 
286
 
    /* process all codes and make table entries */
287
 
    for (;;) {
288
 
        /* create table entry */
289
 
        here.bits = (unsigned char)(len - drop);
290
 
        if ((int)(work[sym]) < end) {
291
 
            here.op = (unsigned char)0;
292
 
            here.val = work[sym];
293
 
        }
294
 
        else if ((int)(work[sym]) > end) {
295
 
            here.op = (unsigned char)(extra[work[sym]]);
296
 
            here.val = base[work[sym]];
297
 
        }
298
 
        else {
299
 
            here.op = (unsigned char)(32 + 64);         /* end of block */
300
 
            here.val = 0;
301
 
        }
302
 
 
303
 
        /* replicate for those indices with low len bits equal to huff */
304
 
        incr = 1U << (len - drop);
305
 
        fill = 1U << curr;
306
 
        min = fill;                 /* save offset to next table */
307
 
        do {
308
 
            fill -= incr;
309
 
            next[(huff >> drop) + fill] = here;
310
 
        } while (fill != 0);
311
 
 
312
 
        /* backwards increment the len-bit code huff */
313
 
        incr = 1U << (len - 1);
314
 
        while (huff & incr)
315
 
            incr >>= 1;
316
 
        if (incr != 0) {
317
 
            huff &= incr - 1;
318
 
            huff += incr;
319
 
        }
320
 
        else
321
 
            huff = 0;
322
 
 
323
 
        /* go to next symbol, update count, len */
324
 
        sym++;
325
 
        if (--(count[len]) == 0) {
326
 
            if (len == max) break;
327
 
            len = lens[work[sym]];
328
 
        }
329
 
 
330
 
        /* create new sub-table if needed */
331
 
        if (len > root && (huff & mask) != low) {
332
 
            /* if first time, transition to sub-tables */
333
 
            if (drop == 0)
334
 
                drop = root;
335
 
 
336
 
            /* increment past last table */
337
 
            next += min;            /* here min is 1 << curr */
338
 
 
339
 
            /* determine length of next table */
340
 
            curr = len - drop;
341
 
            left = (int)(1 << curr);
342
 
            while (curr + drop < max) {
343
 
                left -= count[curr + drop];
344
 
                if (left <= 0) break;
345
 
                curr++;
346
 
                left <<= 1;
347
 
            }
348
 
 
349
 
            /* check for enough space */
350
 
            used += 1U << curr;
351
 
            if ((type == LENS && used >= ENOUGH_LENS) ||
352
 
                (type == DISTS && used >= ENOUGH_DISTS))
353
 
                return 1;
354
 
 
355
 
            /* point entry in root table to sub-table */
356
 
            low = huff & mask;
357
 
            (*table)[low].op = (unsigned char)curr;
358
 
            (*table)[low].bits = (unsigned char)root;
359
 
            (*table)[low].val = (unsigned short)(next - *table);
360
 
        }
361
 
    }
362
 
 
363
 
    /*
364
 
       Fill in rest of table for incomplete codes.  This loop is similar to the
365
 
       loop above in incrementing huff for table indices.  It is assumed that
366
 
       len is equal to curr + drop, so there is no loop needed to increment
367
 
       through high index bits.  When the current sub-table is filled, the loop
368
 
       drops back to the root table to fill in any remaining entries there.
369
 
     */
370
 
    here.op = (unsigned char)64;                /* invalid code marker */
371
 
    here.bits = (unsigned char)(len - drop);
372
 
    here.val = (unsigned short)0;
373
 
    while (huff != 0) {
374
 
        /* when done with sub-table, drop back to root table */
375
 
        if (drop != 0 && (huff & mask) != low) {
376
 
            drop = 0;
377
 
            len = root;
378
 
            next = *table;
379
 
            here.bits = (unsigned char)len;
380
 
        }
381
 
 
382
 
        /* put invalid code marker in table */
383
 
        next[huff >> drop] = here;
384
 
 
385
 
        /* backwards increment the len-bit code huff */
386
 
        incr = 1U << (len - 1);
387
 
        while (huff & incr)
388
 
            incr >>= 1;
389
 
        if (incr != 0) {
390
 
            huff &= incr - 1;
391
 
            huff += incr;
392
 
        }
393
 
        else
394
 
            huff = 0;
395
 
    }
396
 
 
397
 
    /* set return parameters */
398
 
    *table += used;
399
 
    *bits = root;
400
 
    return 0;
401
 
}