Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/base/sbrotlid.c
Line
Count
Source (jump to first uncovered line)
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
#include "brotli/decode.h"
24
25
/* Initialize the filter. */
26
static int
27
s_brotliD_init(stream_state * st)
28
0
{
29
0
    stream_brotlid_state *ss = (stream_brotlid_state *)st;
30
31
0
    ss->dec_state = BrotliDecoderCreateInstance(
32
0
                                    s_brotli_alloc,
33
0
                                    s_brotli_free,
34
0
                                    st->memory->non_gc_memory);
35
0
    if (ss->dec_state == NULL)
36
0
        return ERRC;
37
0
    st->min_left=1;
38
0
    return 0;
39
0
}
40
41
/* Process a buffer */
42
static int
43
s_brotliD_process(stream_state * st, stream_cursor_read * pr,
44
                stream_cursor_write * pw, bool ignore_last)
45
0
{
46
0
    stream_brotlid_state *const ss = (stream_brotlid_state *)st;
47
0
    const byte *p = pr->ptr;
48
0
    const byte *inBuf  = p + 1;
49
0
    size_t      inLen  = pr->limit - p;
50
0
    byte       *outBuf = pw->ptr + 1;
51
0
    size_t      outLen = pw->limit - pw->ptr;
52
0
    BrotliDecoderResult status;
53
54
0
    status = BrotliDecoderDecompressStream((BrotliDecoderState *)ss->dec_state,
55
0
                                         &inLen, /* Available in */
56
0
                                         &inBuf, /* Input buffer */
57
0
                                         &outLen, /* Available out */
58
0
                                         &outBuf, /* Output buffer */
59
0
                                         NULL);
60
0
    pr->ptr = inBuf - 1;
61
0
    pw->ptr = outBuf - 1;
62
0
    switch (status) {
63
0
        case BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT:
64
0
            return 0;
65
0
        case BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT:
66
0
            return 1;
67
0
        case BROTLI_DECODER_RESULT_SUCCESS:
68
0
            return EOFC;
69
0
        default:
70
0
            return ERRC;
71
0
    }
72
0
}
73
74
/* Release the stream */
75
static void
76
s_brotliD_release(stream_state * st)
77
0
{
78
0
    stream_brotlid_state *ss = (stream_brotlid_state *)st;
79
80
0
    if (ss->dec_state != NULL)
81
0
    {
82
0
        BrotliDecoderDestroyInstance((BrotliDecoderState *)ss->dec_state);
83
0
        ss->dec_state = NULL;
84
0
    }
85
0
}
86
87
static void
88
brotliD_final(const gs_memory_t *mem, void *st)
89
0
{
90
0
    stream_brotlid_state *ss = (stream_brotlid_state *)st;
91
92
0
    if (ss->dec_state != NULL)
93
0
    {
94
0
        BrotliDecoderDestroyInstance((BrotliDecoderState *)ss->dec_state);
95
0
        ss->dec_state = NULL;
96
0
    }
97
0
}
98
99
gs_public_st_simple_final(st_brotlid_state, stream_brotlid_state,
100
        "brotli decode state", brotliD_final);
101
102
/* Stream template */
103
const stream_template s_brotliD_template = {
104
    &st_brotlid_state, s_brotliD_init, s_brotliD_process, 1, 1, s_brotliD_release
105
};