inflate_sync.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* inflate.c -- zlib interface to inflate modules
  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 "infutil.h"
  8. #if 0
  9. int zlib_inflateSync(
  10. z_streamp z
  11. )
  12. {
  13. uInt n; /* number of bytes to look at */
  14. Byte *p; /* pointer to bytes */
  15. uInt m; /* number of marker bytes found in a row */
  16. uLong r, w; /* temporaries to save total_in and total_out */
  17. /* set up */
  18. if (z == NULL || z->state == NULL)
  19. return Z_STREAM_ERROR;
  20. if (z->state->mode != I_BAD)
  21. {
  22. z->state->mode = I_BAD;
  23. z->state->sub.marker = 0;
  24. }
  25. if ((n = z->avail_in) == 0)
  26. return Z_BUF_ERROR;
  27. p = z->next_in;
  28. m = z->state->sub.marker;
  29. /* search */
  30. while (n && m < 4)
  31. {
  32. static const Byte mark[4] = {0, 0, 0xff, 0xff};
  33. if (*p == mark[m])
  34. m++;
  35. else if (*p)
  36. m = 0;
  37. else
  38. m = 4 - m;
  39. p++, n--;
  40. }
  41. /* restore */
  42. z->total_in += p - z->next_in;
  43. z->next_in = p;
  44. z->avail_in = n;
  45. z->state->sub.marker = m;
  46. /* return no joy or set up to restart on a new block */
  47. if (m != 4)
  48. return Z_DATA_ERROR;
  49. r = z->total_in; w = z->total_out;
  50. zlib_inflateReset(z);
  51. z->total_in = r; z->total_out = w;
  52. z->state->mode = BLOCKS;
  53. return Z_OK;
  54. }
  55. #endif /* 0 */
  56. /* Returns true if inflate is currently at the end of a block generated
  57. * by Z_SYNC_FLUSH or Z_FULL_FLUSH. This function is used by one PPP
  58. * implementation to provide an additional safety check. PPP uses Z_SYNC_FLUSH
  59. * but removes the length bytes of the resulting empty stored block. When
  60. * decompressing, PPP checks that at the end of input packet, inflate is
  61. * waiting for these length bytes.
  62. */
  63. #if 0
  64. int zlib_inflateSyncPoint(
  65. z_streamp z
  66. )
  67. {
  68. if (z == NULL || z->state == NULL || z->state->blocks == NULL)
  69. return Z_STREAM_ERROR;
  70. return zlib_inflate_blocks_sync_point(z->state->blocks);
  71. }
  72. #endif /* 0 */
  73. /*
  74. * This subroutine adds the data at next_in/avail_in to the output history
  75. * without performing any output. The output buffer must be "caught up";
  76. * i.e. no pending output (hence s->read equals s->write), and the state must
  77. * be BLOCKS (i.e. we should be willing to see the start of a series of
  78. * BLOCKS). On exit, the output will also be caught up, and the checksum
  79. * will have been updated if need be.
  80. */
  81. static int zlib_inflate_addhistory(inflate_blocks_statef *s,
  82. z_stream *z)
  83. {
  84. uLong b; /* bit buffer */ /* NOT USED HERE */
  85. uInt k; /* bits in bit buffer */ /* NOT USED HERE */
  86. uInt t; /* temporary storage */
  87. Byte *p; /* input data pointer */
  88. uInt n; /* bytes available there */
  89. Byte *q; /* output window write pointer */
  90. uInt m; /* bytes to end of window or read pointer */
  91. if (s->read != s->write)
  92. return Z_STREAM_ERROR;
  93. if (s->mode != TYPE)
  94. return Z_DATA_ERROR;
  95. /* we're ready to rock */
  96. LOAD
  97. /* while there is input ready, copy to output buffer, moving
  98. * pointers as needed.
  99. */
  100. while (n) {
  101. t = n; /* how many to do */
  102. /* is there room until end of buffer? */
  103. if (t > m) t = m;
  104. /* update check information */
  105. if (s->checkfn != NULL)
  106. s->check = (*s->checkfn)(s->check, q, t);
  107. memcpy(q, p, t);
  108. q += t;
  109. p += t;
  110. n -= t;
  111. z->total_out += t;
  112. s->read = q; /* drag read pointer forward */
  113. /* WWRAP */ /* expand WWRAP macro by hand to handle s->read */
  114. if (q == s->end) {
  115. s->read = q = s->window;
  116. m = WAVAIL;
  117. }
  118. }
  119. UPDATE
  120. return Z_OK;
  121. }
  122. /*
  123. * This subroutine adds the data at next_in/avail_in to the output history
  124. * without performing any output. The output buffer must be "caught up";
  125. * i.e. no pending output (hence s->read equals s->write), and the state must
  126. * be BLOCKS (i.e. we should be willing to see the start of a series of
  127. * BLOCKS). On exit, the output will also be caught up, and the checksum
  128. * will have been updated if need be.
  129. */
  130. int zlib_inflateIncomp(
  131. z_stream *z
  132. )
  133. {
  134. if (z->state->mode != BLOCKS)
  135. return Z_DATA_ERROR;
  136. return zlib_inflate_addhistory(z->state->blocks, z);
  137. }