/src/ghostpdl/base/sbrotlid.c
Line | Count | Source |
1 | | /* Copyright (C) 2025 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 | | /* brotli decoding (decompression) filter stream */ |
18 | | #include "memory_.h" |
19 | | #include "std.h" |
20 | | #include "strimpl.h" |
21 | | #include "sbrotlix.h" |
22 | | |
23 | | #if defined(ENABLE_BROTLI) && ENABLE_BROTLI==1 |
24 | | #define ENABLE_BROTLI_DECODE 1 |
25 | | #include "brotli/decode.h" |
26 | | #else |
27 | | #define ENABLE_BROTLI_DECODE 0 |
28 | | #endif |
29 | | |
30 | | #if ENABLE_BROTLI_DECODE |
31 | | /* Initialize the filter. */ |
32 | | static int |
33 | | s_brotliD_init(stream_state * st) |
34 | 0 | { |
35 | 0 | stream_brotlid_state *ss = (stream_brotlid_state *)st; |
36 | |
|
37 | 0 | ss->dec_state = BrotliDecoderCreateInstance( |
38 | 0 | s_brotli_alloc, |
39 | 0 | s_brotli_free, |
40 | 0 | st->memory->non_gc_memory); |
41 | 0 | if (ss->dec_state == NULL) |
42 | 0 | return ERRC; |
43 | 0 | st->min_left=1; |
44 | 0 | return 0; |
45 | 0 | } |
46 | | |
47 | | /* Process a buffer */ |
48 | | static int |
49 | | s_brotliD_process(stream_state * st, stream_cursor_read * pr, |
50 | | stream_cursor_write * pw, bool ignore_last) |
51 | 0 | { |
52 | 0 | stream_brotlid_state *const ss = (stream_brotlid_state *)st; |
53 | 0 | const byte *p = pr->ptr; |
54 | 0 | const byte *inBuf = p + 1; |
55 | 0 | size_t inLen = pr->limit - p; |
56 | 0 | byte *outBuf = pw->ptr + 1; |
57 | 0 | size_t outLen = pw->limit - pw->ptr; |
58 | 0 | BrotliDecoderResult status; |
59 | |
|
60 | 0 | status = BrotliDecoderDecompressStream((BrotliDecoderState *)ss->dec_state, |
61 | 0 | &inLen, /* Available in */ |
62 | 0 | &inBuf, /* Input buffer */ |
63 | 0 | &outLen, /* Available out */ |
64 | 0 | &outBuf, /* Output buffer */ |
65 | 0 | NULL); |
66 | 0 | pr->ptr = inBuf - 1; |
67 | 0 | pw->ptr = outBuf - 1; |
68 | 0 | switch (status) { |
69 | 0 | case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT: |
70 | 0 | return 0; |
71 | 0 | case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT: |
72 | 0 | return 1; |
73 | 0 | case BROTLI_DECODER_RESULT_SUCCESS: |
74 | 0 | return EOFC; |
75 | 0 | default: |
76 | 0 | return ERRC; |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | /* Release the stream */ |
81 | | static void |
82 | | s_brotliD_release(stream_state * st) |
83 | 0 | { |
84 | 0 | stream_brotlid_state *ss = (stream_brotlid_state *)st; |
85 | |
|
86 | 0 | if (ss->dec_state != NULL) |
87 | 0 | { |
88 | 0 | BrotliDecoderDestroyInstance((BrotliDecoderState *)ss->dec_state); |
89 | 0 | ss->dec_state = NULL; |
90 | 0 | } |
91 | 0 | } |
92 | | |
93 | | static void |
94 | | brotliD_final(const gs_memory_t *mem, void *st) |
95 | 0 | { |
96 | 0 | stream_brotlid_state *ss = (stream_brotlid_state *)st; |
97 | |
|
98 | 0 | if (ss->dec_state != NULL) |
99 | 0 | { |
100 | 0 | BrotliDecoderDestroyInstance((BrotliDecoderState *)ss->dec_state); |
101 | | ss->dec_state = NULL; |
102 | 0 | } |
103 | 0 | } |
104 | | #else |
105 | | static int |
106 | | s_brotliD_init(stream_state * st) |
107 | | { |
108 | | (void)st; |
109 | | return ERRC; |
110 | | } |
111 | | static void |
112 | | brotliD_final(const gs_memory_t *mem, void *st) |
113 | | { |
114 | | (void)mem; |
115 | | (void)st; |
116 | | } |
117 | | static int |
118 | | s_brotliD_process(stream_state * st, stream_cursor_read * pr, |
119 | | stream_cursor_write * pw, bool ignore_last) |
120 | | { |
121 | | (void)st; |
122 | | (void)pr; |
123 | | (void)pw; |
124 | | (void)ignore_last; |
125 | | return ERRC; |
126 | | } |
127 | | static void |
128 | | s_brotliD_release(stream_state * st) |
129 | | { |
130 | | (void)st; |
131 | | } |
132 | | #endif |
133 | | |
134 | | gs_public_st_simple_final(st_brotlid_state, stream_brotlid_state, |
135 | | "brotli decode state", brotliD_final); |
136 | | |
137 | | /* Stream template */ |
138 | | const stream_template s_brotliD_template = { |
139 | | &st_brotlid_state, s_brotliD_init, s_brotliD_process, 1, 1, s_brotliD_release |
140 | | }; |