jdk/src/share/native/java/util/zip/zlib-1.1.3/infutil.c
changeset 3713 3f49733cf145
parent 3637 84b603d2b088
parent 3712 8851d55adef0
child 3714 6a4eb8f53f91
child 4197 f6266efbfc05
equal deleted inserted replaced
3637:84b603d2b088 3713:3f49733cf145
     1 /*
       
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     3  *
       
     4  * This code is free software; you can redistribute it and/or modify it
       
     5  * under the terms of the GNU General Public License version 2 only, as
       
     6  * published by the Free Software Foundation.  Sun designates this
       
     7  * particular file as subject to the "Classpath" exception as provided
       
     8  * by Sun in the LICENSE file that accompanied this code.
       
     9  *
       
    10  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    13  * version 2 for more details (a copy is included in the LICENSE file that
       
    14  * accompanied this code).
       
    15  *
       
    16  * You should have received a copy of the GNU General Public License version
       
    17  * 2 along with this work; if not, write to the Free Software Foundation,
       
    18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    19  *
       
    20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    21  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    22  * have any questions.
       
    23  */
       
    24 
       
    25 /*
       
    26  * This file is available under and governed by the GNU General Public
       
    27  * License version 2 only, as published by the Free Software Foundation.
       
    28  * However, the following notice accompanied the original version of this
       
    29  * file and, per its terms, should not be removed:
       
    30  *
       
    31  * inflate_util.c -- data and routines common to blocks and codes
       
    32  * Copyright (C) 1995-1998 Mark Adler
       
    33  * For conditions of distribution and use, see copyright notice in zlib.h
       
    34  */
       
    35 
       
    36 #include "zutil.h"
       
    37 #include "infblock.h"
       
    38 #include "inftrees.h"
       
    39 #include "infcodes.h"
       
    40 #include "infutil.h"
       
    41 
       
    42 struct inflate_codes_state {int dummy;}; /* for buggy compilers */
       
    43 
       
    44 /* And'ing with mask[n] masks the lower n bits */
       
    45 uInt inflate_mask[17] = {
       
    46     0x0000,
       
    47     0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
       
    48     0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
       
    49 };
       
    50 
       
    51 
       
    52 /* copy as much as possible from the sliding window to the output area */
       
    53 int inflate_flush(s, z, r)
       
    54 inflate_blocks_statef *s;
       
    55 z_streamp z;
       
    56 int r;
       
    57 {
       
    58   uInt n;
       
    59   Bytef *p;
       
    60   Bytef *q;
       
    61 
       
    62   /* local copies of source and destination pointers */
       
    63   p = z->next_out;
       
    64   q = s->read;
       
    65 
       
    66   /* compute number of bytes to copy as far as end of window */
       
    67   n = (uInt)((q <= s->write ? s->write : s->end) - q);
       
    68   if (n > z->avail_out) n = z->avail_out;
       
    69   if (n && r == Z_BUF_ERROR) r = Z_OK;
       
    70 
       
    71   /* update counters */
       
    72   z->avail_out -= n;
       
    73   z->total_out += n;
       
    74 
       
    75   /* update check information */
       
    76   if (s->checkfn != Z_NULL)
       
    77     z->adler = s->check = (*s->checkfn)(s->check, q, n);
       
    78 
       
    79   /* copy as far as end of window */
       
    80   zmemcpy(p, q, n);
       
    81   p += n;
       
    82   q += n;
       
    83 
       
    84   /* see if more to copy at beginning of window */
       
    85   if (q == s->end)
       
    86   {
       
    87     /* wrap pointers */
       
    88     q = s->window;
       
    89     if (s->write == s->end)
       
    90       s->write = s->window;
       
    91 
       
    92     /* compute bytes to copy */
       
    93     n = (uInt)(s->write - q);
       
    94     if (n > z->avail_out) n = z->avail_out;
       
    95     if (n && r == Z_BUF_ERROR) r = Z_OK;
       
    96 
       
    97     /* update counters */
       
    98     z->avail_out -= n;
       
    99     z->total_out += n;
       
   100 
       
   101     /* update check information */
       
   102     if (s->checkfn != Z_NULL)
       
   103       z->adler = s->check = (*s->checkfn)(s->check, q, n);
       
   104 
       
   105     /* copy */
       
   106     zmemcpy(p, q, n);
       
   107     p += n;
       
   108     q += n;
       
   109   }
       
   110 
       
   111   /* update pointers */
       
   112   z->next_out = p;
       
   113   s->read = q;
       
   114 
       
   115   /* done */
       
   116   return r;
       
   117 }