szlibd.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* Copyright (C) 1995, 1996, 1997, 1998 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: szlibd.c,v 1.3 2000/11/01 22:36:13 lpd Exp $ */
  16. /* zlib decoding (decompression) filter stream */
  17. #include "std.h"
  18. #include "gsmemory.h"
  19. #include "gsmalloc.h" /* for gs_memory_default */
  20. #include "strimpl.h"
  21. #include "szlibxx.h"
  22. /* Initialize the filter. */
  23. private int
  24. s_zlibD_init(stream_state * st)
  25. {
  26. stream_zlib_state *const ss = (stream_zlib_state *)st;
  27. int code = s_zlib_alloc_dynamic_state(ss);
  28. if (code < 0)
  29. return ERRC; /****** WRONG ******/
  30. if (inflateInit2(&ss->dynamic->zstate,
  31. (ss->no_wrapper ? -ss->windowBits : ss->windowBits))
  32. != Z_OK
  33. ) {
  34. s_zlib_free_dynamic_state(ss);
  35. return ERRC; /****** WRONG ******/
  36. }
  37. st->min_left=1;
  38. return 0;
  39. }
  40. /* Reinitialize the filter. */
  41. private int
  42. s_zlibD_reset(stream_state * st)
  43. {
  44. stream_zlib_state *const ss = (stream_zlib_state *)st;
  45. if (inflateReset(&ss->dynamic->zstate) != Z_OK)
  46. return ERRC; /****** WRONG ******/
  47. return 0;
  48. }
  49. /* Process a buffer */
  50. private int
  51. s_zlibD_process(stream_state * st, stream_cursor_read * pr,
  52. stream_cursor_write * pw, bool ignore_last)
  53. {
  54. stream_zlib_state *const ss = (stream_zlib_state *)st;
  55. z_stream *zs = &ss->dynamic->zstate;
  56. const byte *p = pr->ptr;
  57. int status;
  58. /* Detect no input or full output so that we don't get */
  59. /* a Z_BUF_ERROR return. */
  60. if (pw->ptr == pw->limit)
  61. return 1;
  62. if (pr->ptr == pr->limit)
  63. return 0;
  64. zs->next_in = (Bytef *)p + 1;
  65. zs->avail_in = pr->limit - p;
  66. zs->next_out = pw->ptr + 1;
  67. zs->avail_out = pw->limit - pw->ptr;
  68. status = inflate(zs, Z_PARTIAL_FLUSH);
  69. pr->ptr = zs->next_in - 1;
  70. pw->ptr = zs->next_out - 1;
  71. switch (status) {
  72. case Z_OK:
  73. return (pw->ptr == pw->limit ? 1 : pr->ptr > p ? 0 : 1);
  74. case Z_STREAM_END:
  75. return EOFC;
  76. default:
  77. return ERRC;
  78. }
  79. }
  80. /* Release the stream */
  81. private void
  82. s_zlibD_release(stream_state * st)
  83. {
  84. stream_zlib_state *const ss = (stream_zlib_state *)st;
  85. inflateEnd(&ss->dynamic->zstate);
  86. s_zlib_free_dynamic_state(ss);
  87. }
  88. /* Stream template */
  89. const stream_template s_zlibD_template = {
  90. &st_zlib_state, s_zlibD_init, s_zlibD_process, 1, 1, s_zlibD_release,
  91. s_zlib_set_defaults, s_zlibD_reset
  92. };