/src/ghostpdl/base/szlibe.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 2001-2023 Artifex Software, Inc. |
2 | | All Rights Reserved. |
3 | | |
4 | | This software is provided AS-IS with no warranty, either express or |
5 | | implied. |
6 | | |
7 | | This software is distributed under license and may not be copied, |
8 | | modified or distributed except as expressly authorized under the terms |
9 | | of the license contained in the file LICENSE in this distribution. |
10 | | |
11 | | Refer to licensing information at http://www.artifex.com or contact |
12 | | Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco, |
13 | | CA 94129, USA, for further information. |
14 | | */ |
15 | | |
16 | | |
17 | | /* zlib encoding (compression) filter stream */ |
18 | | #include "std.h" |
19 | | #include "strimpl.h" |
20 | | #include "szlibxx.h" |
21 | | |
22 | | /* Initialize the filter. */ |
23 | | static int |
24 | | s_zlibE_init(stream_state * st) |
25 | 112k | { |
26 | 112k | stream_zlib_state *const ss = (stream_zlib_state *)st; |
27 | 112k | int code = s_zlib_alloc_dynamic_state(ss); |
28 | | |
29 | 112k | if (code < 0) |
30 | 0 | return ERRC; /****** WRONG ******/ |
31 | 112k | if (deflateInit2(&ss->dynamic->zstate, ss->level, ss->method, |
32 | 112k | (ss->no_wrapper ? -ss->windowBits : ss->windowBits), |
33 | 112k | ss->memLevel, ss->strategy) != Z_OK) |
34 | 0 | return ERRC; /****** WRONG ******/ |
35 | 112k | return 0; |
36 | 112k | } |
37 | | |
38 | | /* Reinitialize the filter. */ |
39 | | static int |
40 | | s_zlibE_reset(stream_state * st) |
41 | 71.1k | { |
42 | 71.1k | stream_zlib_state *const ss = (stream_zlib_state *)st; |
43 | | |
44 | 71.1k | if (deflateReset(&ss->dynamic->zstate) != Z_OK) |
45 | 0 | return ERRC; /****** WRONG ******/ |
46 | 71.1k | return 0; |
47 | 71.1k | } |
48 | | |
49 | | /* Process a buffer */ |
50 | | static int |
51 | | s_zlibE_process(stream_state * st, stream_cursor_read * pr, |
52 | | stream_cursor_write * pw, bool last) |
53 | 2.09M | { |
54 | 2.09M | stream_zlib_state *const ss = (stream_zlib_state *)st; |
55 | 2.09M | z_stream *zs = &ss->dynamic->zstate; |
56 | 2.09M | const byte *p = pr->ptr; |
57 | 2.09M | int status; |
58 | | |
59 | | /* Detect no input or full output so that we don't get */ |
60 | | /* a Z_BUF_ERROR return. */ |
61 | 2.09M | if (pw->ptr == pw->limit) |
62 | 61.8k | return 1; |
63 | 2.03M | if (p == pr->limit && !last) |
64 | 513k | return 0; |
65 | 1.52M | zs->next_in = (Bytef *)p + 1; |
66 | 1.52M | zs->avail_in = pr->limit - p; |
67 | 1.52M | zs->next_out = pw->ptr + 1; |
68 | 1.52M | zs->avail_out = pw->limit - pw->ptr; |
69 | 1.52M | status = deflate(zs, (last ? Z_FINISH : Z_NO_FLUSH)); |
70 | 1.52M | pr->ptr = zs->next_in - 1; |
71 | 1.52M | pw->ptr = zs->next_out - 1; |
72 | 1.52M | switch (status) { |
73 | 1.33M | case Z_OK: |
74 | 1.33M | return (pw->ptr == pw->limit ? 1 : pr->ptr > p && !last ? 0 : 1); |
75 | 184k | case Z_STREAM_END: |
76 | 184k | return (last && pr->ptr == pr->limit ? 0 : ERRC); |
77 | 0 | default: |
78 | 0 | return ERRC; |
79 | 1.52M | } |
80 | 1.52M | } |
81 | | |
82 | | /* Release the stream */ |
83 | | static void |
84 | | s_zlibE_release(stream_state * st) |
85 | 112k | { |
86 | 112k | stream_zlib_state *const ss = (stream_zlib_state *)st; |
87 | | |
88 | 112k | deflateEnd(&ss->dynamic->zstate); |
89 | 112k | s_zlib_free_dynamic_state(ss); |
90 | 112k | } |
91 | | |
92 | | /* Stream template */ |
93 | | const stream_template s_zlibE_template = { |
94 | | &st_zlib_state, s_zlibE_init, s_zlibE_process, 1, 1, s_zlibE_release, |
95 | | s_zlib_set_defaults, s_zlibE_reset |
96 | | }; |