bzread.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /* THIS FILE HAS BEEN MODIFIED -- rsc split bzlib.c into bzlib.c,
  2. bzlibcompress.c, bzlibdecompress.c, bzlibread.c, bzlibwrite.c
  3. */
  4. /*-------------------------------------------------------------*/
  5. /*--- Library top-level functions. ---*/
  6. /*--- bzlib.c ---*/
  7. /*-------------------------------------------------------------*/
  8. /*--
  9. This file is a part of bzip2 and/or libbzip2, a program and
  10. library for lossless, block-sorting data compression.
  11. Copyright (C) 1996-2000 Julian R Seward. All rights reserved.
  12. Redistribution and use in source and binary forms, with or without
  13. modification, are permitted provided that the following conditions
  14. are met:
  15. 1. Redistributions of source code must retain the above copyright
  16. notice, this list of conditions and the following disclaimer.
  17. 2. The origin of this software must not be misrepresented; you must
  18. not claim that you wrote the original software. If you use this
  19. software in a product, an acknowledgment in the product
  20. documentation would be appreciated but is not required.
  21. 3. Altered source versions must be plainly marked as such, and must
  22. not be misrepresented as being the original software.
  23. 4. The name of the author may not be used to endorse or promote
  24. products derived from this software without specific prior written
  25. permission.
  26. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
  27. OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  30. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  32. GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  34. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. Julian Seward, Cambridge, UK.
  38. jseward@acm.org
  39. bzip2/libbzip2 version 1.0 of 21 March 2000
  40. This program is based on (at least) the work of:
  41. Mike Burrows
  42. David Wheeler
  43. Peter Fenwick
  44. Alistair Moffat
  45. Radford Neal
  46. Ian H. Witten
  47. Robert Sedgewick
  48. Jon L. Bentley
  49. For more information on these sources, see the manual.
  50. --*/
  51. /*--
  52. CHANGES
  53. ~~~~~~~
  54. 0.9.0 -- original version.
  55. 0.9.0a/b -- no changes in this file.
  56. 0.9.0c
  57. * made zero-length BZ_FLUSH work correctly in bzCompress().
  58. * fixed bzWrite/bzRead to ignore zero-length requests.
  59. * fixed bzread to correctly handle read requests after EOF.
  60. * wrong parameter order in call to bzDecompressInit in
  61. bzBuffToBuffDecompress. Fixed.
  62. --*/
  63. #include "os.h"
  64. #include "bzlib.h"
  65. #include "bzlib_private.h"
  66. #include "bzlib_stdio.h"
  67. #include "bzlib_stdio_private.h"
  68. /*---------------------------------------------------*/
  69. BZFILE* BZ_API(BZ2_bzReadOpen)
  70. ( int* bzerror,
  71. FILE* f,
  72. int verbosity,
  73. int small,
  74. void* unused,
  75. int nUnused )
  76. {
  77. bzFile* bzf = NULL;
  78. int ret;
  79. BZ_SETERR(BZ_OK);
  80. if (f == NULL ||
  81. (small != 0 && small != 1) ||
  82. (verbosity < 0 || verbosity > 4) ||
  83. (unused == NULL && nUnused != 0) ||
  84. (unused != NULL && (nUnused < 0 || nUnused > BZ_MAX_UNUSED)))
  85. { BZ_SETERR(BZ_PARAM_ERROR); return NULL; };
  86. if (ferror(f))
  87. { BZ_SETERR(BZ_IO_ERROR); return NULL; };
  88. bzf = malloc ( sizeof(bzFile) );
  89. if (bzf == NULL)
  90. { BZ_SETERR(BZ_MEM_ERROR); return NULL; };
  91. BZ_SETERR(BZ_OK);
  92. bzf->initialisedOk = False;
  93. bzf->handle = f;
  94. bzf->bufN = 0;
  95. bzf->writing = False;
  96. bzf->strm.bzalloc = NULL;
  97. bzf->strm.bzfree = NULL;
  98. bzf->strm.opaque = NULL;
  99. while (nUnused > 0) {
  100. bzf->buf[bzf->bufN] = *((UChar*)(unused)); bzf->bufN++;
  101. unused = ((void*)( 1 + ((UChar*)(unused)) ));
  102. nUnused--;
  103. }
  104. ret = BZ2_bzDecompressInit ( &(bzf->strm), verbosity, small );
  105. if (ret != BZ_OK)
  106. { BZ_SETERR(ret); free(bzf); return NULL; };
  107. bzf->strm.avail_in = bzf->bufN;
  108. bzf->strm.next_in = bzf->buf;
  109. bzf->initialisedOk = True;
  110. return bzf;
  111. }
  112. /*---------------------------------------------------*/
  113. void BZ_API(BZ2_bzReadClose) ( int *bzerror, BZFILE *b )
  114. {
  115. bzFile* bzf = (bzFile*)b;
  116. BZ_SETERR(BZ_OK);
  117. if (bzf == NULL)
  118. { BZ_SETERR(BZ_OK); return; };
  119. if (bzf->writing)
  120. { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
  121. if (bzf->initialisedOk)
  122. (void)BZ2_bzDecompressEnd ( &(bzf->strm) );
  123. free ( bzf );
  124. }
  125. /*---------------------------------------------------*/
  126. int BZ_API(BZ2_bzRead)
  127. ( int* bzerror,
  128. BZFILE* b,
  129. void* buf,
  130. int len )
  131. {
  132. Int32 n, ret;
  133. bzFile* bzf = (bzFile*)b;
  134. BZ_SETERR(BZ_OK);
  135. if (bzf == NULL || buf == NULL || len < 0)
  136. { BZ_SETERR(BZ_PARAM_ERROR); return 0; };
  137. if (bzf->writing)
  138. { BZ_SETERR(BZ_SEQUENCE_ERROR); return 0; };
  139. if (len == 0)
  140. { BZ_SETERR(BZ_OK); return 0; };
  141. bzf->strm.avail_out = len;
  142. bzf->strm.next_out = buf;
  143. while (True) {
  144. if (ferror(bzf->handle))
  145. { BZ_SETERR(BZ_IO_ERROR); return 0; };
  146. if (bzf->strm.avail_in == 0 && !bz_feof(bzf->handle)) {
  147. n = fread ( bzf->buf, sizeof(UChar),
  148. BZ_MAX_UNUSED, bzf->handle );
  149. if (ferror(bzf->handle))
  150. { BZ_SETERR(BZ_IO_ERROR); return 0; };
  151. bzf->bufN = n;
  152. bzf->strm.avail_in = bzf->bufN;
  153. bzf->strm.next_in = bzf->buf;
  154. }
  155. ret = BZ2_bzDecompress ( &(bzf->strm) );
  156. if (ret != BZ_OK && ret != BZ_STREAM_END)
  157. { BZ_SETERR(ret); return 0; };
  158. if (ret == BZ_OK && bz_feof(bzf->handle) &&
  159. bzf->strm.avail_in == 0 && bzf->strm.avail_out > 0)
  160. { BZ_SETERR(BZ_UNEXPECTED_EOF); return 0; };
  161. if (ret == BZ_STREAM_END)
  162. { BZ_SETERR(BZ_STREAM_END);
  163. return len - bzf->strm.avail_out; };
  164. if (bzf->strm.avail_out == 0)
  165. { BZ_SETERR(BZ_OK); return len; };
  166. }
  167. }
  168. /*---------------------------------------------------*/
  169. void BZ_API(BZ2_bzReadGetUnused)
  170. ( int* bzerror,
  171. BZFILE* b,
  172. void** unused,
  173. int* nUnused )
  174. {
  175. bzFile* bzf = (bzFile*)b;
  176. if (bzf == NULL)
  177. { BZ_SETERR(BZ_PARAM_ERROR); return; };
  178. if (bzf->lastErr != BZ_STREAM_END)
  179. { BZ_SETERR(BZ_SEQUENCE_ERROR); return; };
  180. if (unused == NULL || nUnused == NULL)
  181. { BZ_SETERR(BZ_PARAM_ERROR); return; };
  182. BZ_SETERR(BZ_OK);
  183. *nUnused = bzf->strm.avail_in;
  184. *unused = bzf->strm.next_in;
  185. }