infutil.c 1.9 KB

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