2 |
- |
1 |
/* Copyright (C) 1995, 1996, 1997, 1998 Aladdin Enterprises. All rights reserved.
|
|
|
2 |
|
|
|
3 |
This software is provided AS-IS with no warranty, either express or
|
|
|
4 |
implied.
|
|
|
5 |
|
|
|
6 |
This software is distributed under license and may not be copied,
|
|
|
7 |
modified or distributed except as expressly authorized under the terms
|
|
|
8 |
of the license contained in the file LICENSE in this distribution.
|
|
|
9 |
|
|
|
10 |
For more information about licensing, please refer to
|
|
|
11 |
http://www.ghostscript.com/licensing/. For information on
|
|
|
12 |
commercial licensing, go to http://www.artifex.com/licensing/ or
|
|
|
13 |
contact Artifex Software, Inc., 101 Lucas Valley Road #110,
|
|
|
14 |
San Rafael, CA 94903, U.S.A., +1(415)492-9861.
|
|
|
15 |
*/
|
|
|
16 |
|
|
|
17 |
/* $Id: szlibd.c,v 1.7 2004/03/11 14:50:50 igor Exp $ */
|
|
|
18 |
/* zlib decoding (decompression) filter stream */
|
|
|
19 |
#include "memory_.h"
|
|
|
20 |
#include "std.h"
|
|
|
21 |
#include "gsmemory.h"
|
|
|
22 |
#include "gsmalloc.h" /* for gs_memory_default */
|
|
|
23 |
#include "strimpl.h"
|
|
|
24 |
#include "szlibxx.h"
|
|
|
25 |
|
|
|
26 |
/* Initialize the filter. */
|
|
|
27 |
private int
|
|
|
28 |
s_zlibD_init(stream_state * st)
|
|
|
29 |
{
|
|
|
30 |
stream_zlib_state *const ss = (stream_zlib_state *)st;
|
|
|
31 |
int code = s_zlib_alloc_dynamic_state(ss);
|
|
|
32 |
|
|
|
33 |
if (code < 0)
|
|
|
34 |
return ERRC; /****** WRONG ******/
|
|
|
35 |
if (inflateInit2(&ss->dynamic->zstate,
|
|
|
36 |
(ss->no_wrapper ? -ss->windowBits : ss->windowBits))
|
|
|
37 |
!= Z_OK
|
|
|
38 |
) {
|
|
|
39 |
s_zlib_free_dynamic_state(ss);
|
|
|
40 |
return ERRC; /****** WRONG ******/
|
|
|
41 |
}
|
|
|
42 |
st->min_left=1;
|
|
|
43 |
return 0;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/* Reinitialize the filter. */
|
|
|
47 |
private int
|
|
|
48 |
s_zlibD_reset(stream_state * st)
|
|
|
49 |
{
|
|
|
50 |
stream_zlib_state *const ss = (stream_zlib_state *)st;
|
|
|
51 |
|
|
|
52 |
if (inflateReset(&ss->dynamic->zstate) != Z_OK)
|
|
|
53 |
return ERRC; /****** WRONG ******/
|
|
|
54 |
return 0;
|
|
|
55 |
}
|
|
|
56 |
|
|
|
57 |
/* Process a buffer */
|
|
|
58 |
private int
|
|
|
59 |
s_zlibD_process(stream_state * st, stream_cursor_read * pr,
|
|
|
60 |
stream_cursor_write * pw, bool ignore_last)
|
|
|
61 |
{
|
|
|
62 |
stream_zlib_state *const ss = (stream_zlib_state *)st;
|
|
|
63 |
z_stream *zs = &ss->dynamic->zstate;
|
|
|
64 |
const byte *p = pr->ptr;
|
|
|
65 |
int status;
|
|
|
66 |
static const unsigned char jaws_empty[] = {0x58, 0x85, 1, 0, 0, 0, 0, 0, 1, 0x0A};
|
|
|
67 |
|
|
|
68 |
/* Detect no input or full output so that we don't get */
|
|
|
69 |
/* a Z_BUF_ERROR return. */
|
|
|
70 |
if (pw->ptr == pw->limit)
|
|
|
71 |
return 1;
|
|
|
72 |
if (pr->ptr == pr->limit)
|
|
|
73 |
return 0;
|
|
|
74 |
zs->next_in = (Bytef *)p + 1;
|
|
|
75 |
zs->avail_in = pr->limit - p;
|
|
|
76 |
zs->next_out = pw->ptr + 1;
|
|
|
77 |
zs->avail_out = pw->limit - pw->ptr;
|
|
|
78 |
if (zs->total_in == 0 && zs->avail_in >= 10 && !memcmp(zs->next_in, jaws_empty, 10)) {
|
|
|
79 |
/* JAWS PDF generator encodes empty stream as jaws_empty[].
|
|
|
80 |
* The stream declares that the data block length is zero
|
|
|
81 |
* but zlib routines regard a zero length data block to be an error.
|
|
|
82 |
*/
|
|
|
83 |
pr->ptr += 10;
|
|
|
84 |
return EOFC;
|
|
|
85 |
}
|
|
|
86 |
status = inflate(zs, Z_PARTIAL_FLUSH);
|
|
|
87 |
pr->ptr = zs->next_in - 1;
|
|
|
88 |
pw->ptr = zs->next_out - 1;
|
|
|
89 |
switch (status) {
|
|
|
90 |
case Z_OK:
|
|
|
91 |
return (pw->ptr == pw->limit ? 1 : pr->ptr > p ? 0 : 1);
|
|
|
92 |
case Z_STREAM_END:
|
|
|
93 |
return EOFC;
|
|
|
94 |
default:
|
|
|
95 |
return ERRC;
|
|
|
96 |
}
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
/* Release the stream */
|
|
|
100 |
private void
|
|
|
101 |
s_zlibD_release(stream_state * st)
|
|
|
102 |
{
|
|
|
103 |
stream_zlib_state *const ss = (stream_zlib_state *)st;
|
|
|
104 |
|
|
|
105 |
inflateEnd(&ss->dynamic->zstate);
|
|
|
106 |
s_zlib_free_dynamic_state(ss);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
/* Stream template */
|
|
|
110 |
const stream_template s_zlibD_template = {
|
|
|
111 |
&st_zlib_state, s_zlibD_init, s_zlibD_process, 1, 1, s_zlibD_release,
|
|
|
112 |
s_zlib_set_defaults, s_zlibD_reset
|
|
|
113 |
};
|