infutil.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /* inflate_util.c -- data and routines common to blocks and codes
  2. * Copyright (C) 1995-2002 Mark Adler
  3. * For conditions of distribution and use, see copyright notice in zlib.h
  4. */
  5. #include "zutil.h"
  6. #include "infblock.h"
  7. #include "inftrees.h"
  8. #include "infcodes.h"
  9. #include "infutil.h"
  10. /* And'ing with mask[n] masks the lower n bits */
  11. local uInt inflate_mask[17] = {
  12. 0x0000,
  13. 0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
  14. 0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
  15. };
  16. /* copy as much as possible from the sliding window to the output area */
  17. local int inflate_flush(s, z, r)
  18. inflate_blocks_statef *s;
  19. z_streamp z;
  20. int r;
  21. {
  22. uInt n;
  23. Bytef *p;
  24. Bytef *q;
  25. /* local copies of source and destination pointers */
  26. p = z->next_out;
  27. q = s->read;
  28. /* compute number of bytes to copy as far as end of window */
  29. n = (uInt)((q <= s->write ? s->write : s->end) - q);
  30. if (n > z->avail_out) n = z->avail_out;
  31. if (n && r == Z_BUF_ERROR) r = Z_OK;
  32. /* update counters */
  33. z->avail_out -= n;
  34. z->total_out += n;
  35. /* update check information */
  36. if (s->checkfn != Z_NULL)
  37. z->adler = s->check = (*s->checkfn)(s->check, q, n);
  38. /* copy as far as end of window */
  39. zmemcpy(p, q, n);
  40. p += n;
  41. q += n;
  42. /* see if more to copy at beginning of window */
  43. if (q == s->end)
  44. {
  45. /* wrap pointers */
  46. q = s->window;
  47. if (s->write == s->end)
  48. s->write = s->window;
  49. /* compute bytes to copy */
  50. n = (uInt)(s->write - q);
  51. if (n > z->avail_out) n = z->avail_out;
  52. if (n && r == Z_BUF_ERROR) r = Z_OK;
  53. /* update counters */
  54. z->avail_out -= n;
  55. z->total_out += n;
  56. /* update check information */
  57. if (s->checkfn != Z_NULL)
  58. z->adler = s->check = (*s->checkfn)(s->check, q, n);
  59. /* copy */
  60. zmemcpy(p, q, n);
  61. p += n;
  62. q += n;
  63. }
  64. /* update pointers */
  65. z->next_out = p;
  66. s->read = q;
  67. /* done */
  68. return r;
  69. }