Hex Artifact Content

Not logged in

Artifact 52ba94de56ae84aa663865c74532d6271a7db1df:


0000: 2f 2a 20 69 6e 66 6c 61 74 65 5f 75 74 69 6c 2e  /* inflate_util.
0010: 63 20 2d 2d 20 64 61 74 61 20 61 6e 64 20 72 6f  c -- data and ro
0020: 75 74 69 6e 65 73 20 63 6f 6d 6d 6f 6e 20 74 6f  utines common to
0030: 20 62 6c 6f 63 6b 73 20 61 6e 64 20 63 6f 64 65   blocks and code
0040: 73 0a 20 2a 20 43 6f 70 79 72 69 67 68 74 20 28  s. * Copyright (
0050: 43 29 20 31 39 39 35 2d 31 39 39 38 20 4d 61 72  C) 1995-1998 Mar
0060: 6b 20 41 64 6c 65 72 0a 20 2a 20 46 6f 72 20 63  k Adler. * For c
0070: 6f 6e 64 69 74 69 6f 6e 73 20 6f 66 20 64 69 73  onditions of dis
0080: 74 72 69 62 75 74 69 6f 6e 20 61 6e 64 20 75 73  tribution and us
0090: 65 2c 20 73 65 65 20 63 6f 70 79 72 69 67 68 74  e, see copyright
00a0: 20 6e 6f 74 69 63 65 20 69 6e 20 7a 6c 69 62 2e   notice in zlib.
00b0: 68 20 0a 20 2a 2f 0a 0a 23 69 6e 63 6c 75 64 65  h . */..#include
00c0: 20 22 7a 75 74 69 6c 2e 68 22 0a 23 69 6e 63 6c   "zutil.h".#incl
00d0: 75 64 65 20 22 69 6e 66 62 6c 6f 63 6b 2e 68 22  ude "infblock.h"
00e0: 0a 23 69 6e 63 6c 75 64 65 20 22 69 6e 66 74 72  .#include "inftr
00f0: 65 65 73 2e 68 22 0a 23 69 6e 63 6c 75 64 65 20  ees.h".#include 
0100: 22 69 6e 66 63 6f 64 65 73 2e 68 22 0a 23 69 6e  "infcodes.h".#in
0110: 63 6c 75 64 65 20 22 69 6e 66 75 74 69 6c 2e 68  clude "infutil.h
0120: 22 0a 0a 73 74 72 75 63 74 20 69 6e 66 6c 61 74  "..struct inflat
0130: 65 5f 63 6f 64 65 73 5f 73 74 61 74 65 20 7b 69  e_codes_state {i
0140: 6e 74 20 64 75 6d 6d 79 3b 7d 3b 20 2f 2a 20 66  nt dummy;}; /* f
0150: 6f 72 20 62 75 67 67 79 20 63 6f 6d 70 69 6c 65  or buggy compile
0160: 72 73 20 2a 2f 0a 0a 2f 2a 20 41 6e 64 27 69 6e  rs */../* And'in
0170: 67 20 77 69 74 68 20 6d 61 73 6b 5b 6e 5d 20 6d  g with mask[n] m
0180: 61 73 6b 73 20 74 68 65 20 6c 6f 77 65 72 20 6e  asks the lower n
0190: 20 62 69 74 73 20 2a 2f 0a 75 49 6e 74 20 69 6e   bits */.uInt in
01a0: 66 6c 61 74 65 5f 6d 61 73 6b 5b 31 37 5d 20 3d  flate_mask[17] =
01b0: 20 7b 0a 20 20 20 20 30 78 30 30 30 30 2c 0a 20   {.    0x0000,. 
01c0: 20 20 20 30 78 30 30 30 31 2c 20 30 78 30 30 30     0x0001, 0x000
01d0: 33 2c 20 30 78 30 30 30 37 2c 20 30 78 30 30 30  3, 0x0007, 0x000
01e0: 66 2c 20 30 78 30 30 31 66 2c 20 30 78 30 30 33  f, 0x001f, 0x003
01f0: 66 2c 20 30 78 30 30 37 66 2c 20 30 78 30 30 66  f, 0x007f, 0x00f
0200: 66 2c 0a 20 20 20 20 30 78 30 31 66 66 2c 20 30  f,.    0x01ff, 0
0210: 78 30 33 66 66 2c 20 30 78 30 37 66 66 2c 20 30  x03ff, 0x07ff, 0
0220: 78 30 66 66 66 2c 20 30 78 31 66 66 66 2c 20 30  x0fff, 0x1fff, 0
0230: 78 33 66 66 66 2c 20 30 78 37 66 66 66 2c 20 30  x3fff, 0x7fff, 0
0240: 78 66 66 66 66 0a 7d 3b 0a 0a 0a 2f 2a 20 63 6f  xffff.};.../* co
0250: 70 79 20 61 73 20 6d 75 63 68 20 61 73 20 70 6f  py as much as po
0260: 73 73 69 62 6c 65 20 66 72 6f 6d 20 74 68 65 20  ssible from the 
0270: 73 6c 69 64 69 6e 67 20 77 69 6e 64 6f 77 20 74  sliding window t
0280: 6f 20 74 68 65 20 6f 75 74 70 75 74 20 61 72 65  o the output are
0290: 61 20 2a 2f 0a 69 6e 74 20 69 6e 66 6c 61 74 65  a */.int inflate
02a0: 5f 66 6c 75 73 68 28 73 2c 20 7a 2c 20 72 29 0a  _flush(s, z, r).
02b0: 69 6e 66 6c 61 74 65 5f 62 6c 6f 63 6b 73 5f 73  inflate_blocks_s
02c0: 74 61 74 65 66 20 2a 73 3b 0a 7a 5f 73 74 72 65  tatef *s;.z_stre
02d0: 61 6d 70 20 7a 3b 0a 69 6e 74 20 72 3b 0a 7b 0a  amp z;.int r;.{.
02e0: 20 20 75 49 6e 74 20 6e 3b 0a 20 20 42 79 74 65    uInt n;.  Byte
02f0: 66 20 2a 70 3b 0a 20 20 42 79 74 65 66 20 2a 71  f *p;.  Bytef *q
0300: 3b 0a 0a 20 20 2f 2a 20 6c 6f 63 61 6c 20 63 6f  ;..  /* local co
0310: 70 69 65 73 20 6f 66 20 73 6f 75 72 63 65 20 61  pies of source a
0320: 6e 64 20 64 65 73 74 69 6e 61 74 69 6f 6e 20 70  nd destination p
0330: 6f 69 6e 74 65 72 73 20 2a 2f 0a 20 20 70 20 3d  ointers */.  p =
0340: 20 7a 2d 3e 6e 65 78 74 5f 6f 75 74 3b 0a 20 20   z->next_out;.  
0350: 71 20 3d 20 73 2d 3e 72 65 61 64 3b 0a 0a 20 20  q = s->read;..  
0360: 2f 2a 20 63 6f 6d 70 75 74 65 20 6e 75 6d 62 65  /* compute numbe
0370: 72 20 6f 66 20 62 79 74 65 73 20 74 6f 20 63 6f  r of bytes to co
0380: 70 79 20 61 73 20 66 61 72 20 61 73 20 65 6e 64  py as far as end
0390: 20 6f 66 20 77 69 6e 64 6f 77 20 2a 2f 0a 20 20   of window */.  
03a0: 6e 20 3d 20 28 75 49 6e 74 29 28 28 71 20 3c 3d  n = (uInt)((q <=
03b0: 20 73 2d 3e 77 72 69 74 65 20 3f 20 73 2d 3e 77   s->write ? s->w
03c0: 72 69 74 65 20 3a 20 73 2d 3e 65 6e 64 29 20 2d  rite : s->end) -
03d0: 20 71 29 3b 0a 20 20 69 66 20 28 6e 20 3e 20 7a   q);.  if (n > z
03e0: 2d 3e 61 76 61 69 6c 5f 6f 75 74 29 20 6e 20 3d  ->avail_out) n =
03f0: 20 7a 2d 3e 61 76 61 69 6c 5f 6f 75 74 3b 0a 20   z->avail_out;. 
0400: 20 69 66 20 28 6e 20 26 26 20 72 20 3d 3d 20 5a   if (n && r == Z
0410: 5f 42 55 46 5f 45 52 52 4f 52 29 20 72 20 3d 20  _BUF_ERROR) r = 
0420: 5a 5f 4f 4b 3b 0a 0a 20 20 2f 2a 20 75 70 64 61  Z_OK;..  /* upda
0430: 74 65 20 63 6f 75 6e 74 65 72 73 20 2a 2f 0a 20  te counters */. 
0440: 20 7a 2d 3e 61 76 61 69 6c 5f 6f 75 74 20 2d 3d   z->avail_out -=
0450: 20 6e 3b 0a 20 20 7a 2d 3e 74 6f 74 61 6c 5f 6f   n;.  z->total_o
0460: 75 74 20 2b 3d 20 6e 3b 0a 0a 20 20 2f 2a 20 75  ut += n;..  /* u
0470: 70 64 61 74 65 20 63 68 65 63 6b 20 69 6e 66 6f  pdate check info
0480: 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 69 66 20  rmation */.  if 
0490: 28 73 2d 3e 63 68 65 63 6b 66 6e 20 21 3d 20 5a  (s->checkfn != Z
04a0: 5f 4e 55 4c 4c 29 0a 20 20 20 20 7a 2d 3e 61 64  _NULL).    z->ad
04b0: 6c 65 72 20 3d 20 73 2d 3e 63 68 65 63 6b 20 3d  ler = s->check =
04c0: 20 28 2a 73 2d 3e 63 68 65 63 6b 66 6e 29 28 73   (*s->checkfn)(s
04d0: 2d 3e 63 68 65 63 6b 2c 20 71 2c 20 6e 29 3b 0a  ->check, q, n);.
04e0: 0a 20 20 2f 2a 20 63 6f 70 79 20 61 73 20 66 61  .  /* copy as fa
04f0: 72 20 61 73 20 65 6e 64 20 6f 66 20 77 69 6e 64  r as end of wind
0500: 6f 77 20 2a 2f 0a 20 20 7a 6d 65 6d 63 70 79 28  ow */.  zmemcpy(
0510: 70 2c 20 71 2c 20 6e 29 3b 0a 20 20 70 20 2b 3d  p, q, n);.  p +=
0520: 20 6e 3b 0a 20 20 71 20 2b 3d 20 6e 3b 0a 0a 20   n;.  q += n;.. 
0530: 20 2f 2a 20 73 65 65 20 69 66 20 6d 6f 72 65 20   /* see if more 
0540: 74 6f 20 63 6f 70 79 20 61 74 20 62 65 67 69 6e  to copy at begin
0550: 6e 69 6e 67 20 6f 66 20 77 69 6e 64 6f 77 20 2a  ning of window *
0560: 2f 0a 20 20 69 66 20 28 71 20 3d 3d 20 73 2d 3e  /.  if (q == s->
0570: 65 6e 64 29 0a 20 20 7b 0a 20 20 20 20 2f 2a 20  end).  {.    /* 
0580: 77 72 61 70 20 70 6f 69 6e 74 65 72 73 20 2a 2f  wrap pointers */
0590: 0a 20 20 20 20 71 20 3d 20 73 2d 3e 77 69 6e 64  .    q = s->wind
05a0: 6f 77 3b 0a 20 20 20 20 69 66 20 28 73 2d 3e 77  ow;.    if (s->w
05b0: 72 69 74 65 20 3d 3d 20 73 2d 3e 65 6e 64 29 0a  rite == s->end).
05c0: 20 20 20 20 20 20 73 2d 3e 77 72 69 74 65 20 3d        s->write =
05d0: 20 73 2d 3e 77 69 6e 64 6f 77 3b 0a 0a 20 20 20   s->window;..   
05e0: 20 2f 2a 20 63 6f 6d 70 75 74 65 20 62 79 74 65   /* compute byte
05f0: 73 20 74 6f 20 63 6f 70 79 20 2a 2f 0a 20 20 20  s to copy */.   
0600: 20 6e 20 3d 20 28 75 49 6e 74 29 28 73 2d 3e 77   n = (uInt)(s->w
0610: 72 69 74 65 20 2d 20 71 29 3b 0a 20 20 20 20 69  rite - q);.    i
0620: 66 20 28 6e 20 3e 20 7a 2d 3e 61 76 61 69 6c 5f  f (n > z->avail_
0630: 6f 75 74 29 20 6e 20 3d 20 7a 2d 3e 61 76 61 69  out) n = z->avai
0640: 6c 5f 6f 75 74 3b 0a 20 20 20 20 69 66 20 28 6e  l_out;.    if (n
0650: 20 26 26 20 72 20 3d 3d 20 5a 5f 42 55 46 5f 45   && r == Z_BUF_E
0660: 52 52 4f 52 29 20 72 20 3d 20 5a 5f 4f 4b 3b 0a  RROR) r = Z_OK;.
0670: 0a 20 20 20 20 2f 2a 20 75 70 64 61 74 65 20 63  .    /* update c
0680: 6f 75 6e 74 65 72 73 20 2a 2f 0a 20 20 20 20 7a  ounters */.    z
0690: 2d 3e 61 76 61 69 6c 5f 6f 75 74 20 2d 3d 20 6e  ->avail_out -= n
06a0: 3b 0a 20 20 20 20 7a 2d 3e 74 6f 74 61 6c 5f 6f  ;.    z->total_o
06b0: 75 74 20 2b 3d 20 6e 3b 0a 0a 20 20 20 20 2f 2a  ut += n;..    /*
06c0: 20 75 70 64 61 74 65 20 63 68 65 63 6b 20 69 6e   update check in
06d0: 66 6f 72 6d 61 74 69 6f 6e 20 2a 2f 0a 20 20 20  formation */.   
06e0: 20 69 66 20 28 73 2d 3e 63 68 65 63 6b 66 6e 20   if (s->checkfn 
06f0: 21 3d 20 5a 5f 4e 55 4c 4c 29 0a 20 20 20 20 20  != Z_NULL).     
0700: 20 7a 2d 3e 61 64 6c 65 72 20 3d 20 73 2d 3e 63   z->adler = s->c
0710: 68 65 63 6b 20 3d 20 28 2a 73 2d 3e 63 68 65 63  heck = (*s->chec
0720: 6b 66 6e 29 28 73 2d 3e 63 68 65 63 6b 2c 20 71  kfn)(s->check, q
0730: 2c 20 6e 29 3b 0a 0a 20 20 20 20 2f 2a 20 63 6f  , n);..    /* co
0740: 70 79 20 2a 2f 0a 20 20 20 20 7a 6d 65 6d 63 70  py */.    zmemcp
0750: 79 28 70 2c 20 71 2c 20 6e 29 3b 0a 20 20 20 20  y(p, q, n);.    
0760: 70 20 2b 3d 20 6e 3b 0a 20 20 20 20 71 20 2b 3d  p += n;.    q +=
0770: 20 6e 3b 0a 20 20 7d 0a 0a 20 20 2f 2a 20 75 70   n;.  }..  /* up
0780: 64 61 74 65 20 70 6f 69 6e 74 65 72 73 20 2a 2f  date pointers */
0790: 0a 20 20 7a 2d 3e 6e 65 78 74 5f 6f 75 74 20 3d  .  z->next_out =
07a0: 20 70 3b 0a 20 20 73 2d 3e 72 65 61 64 20 3d 20   p;.  s->read = 
07b0: 71 3b 0a 0a 20 20 2f 2a 20 64 6f 6e 65 20 2a 2f  q;..  /* done */
07c0: 0a 20 20 72 65 74 75 72 6e 20 72 3b 0a 7d 0a     .  return r;.}.