Coverage Report

Created: 2025-06-10 07:27

/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
220k
{
26
220k
    stream_zlib_state *const ss = (stream_zlib_state *)st;
27
220k
    int code = s_zlib_alloc_dynamic_state(ss);
28
29
220k
    if (code < 0)
30
0
        return ERRC; /****** WRONG ******/
31
220k
    if (deflateInit2(&ss->dynamic->zstate, ss->level, ss->method,
32
220k
                     (ss->no_wrapper ? -ss->windowBits : ss->windowBits),
33
220k
                     ss->memLevel, ss->strategy) != Z_OK)
34
0
        return ERRC; /****** WRONG ******/
35
220k
    return 0;
36
220k
}
37
38
/* Reinitialize the filter. */
39
static int
40
s_zlibE_reset(stream_state * st)
41
0
{
42
0
    stream_zlib_state *const ss = (stream_zlib_state *)st;
43
44
0
    if (deflateReset(&ss->dynamic->zstate) != Z_OK)
45
0
        return ERRC; /****** WRONG ******/
46
0
    return 0;
47
0
}
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
6.87M
{
54
6.87M
    stream_zlib_state *const ss = (stream_zlib_state *)st;
55
6.87M
    z_stream *zs = &ss->dynamic->zstate;
56
6.87M
    const byte *p = pr->ptr;
57
6.87M
    int status;
58
59
    /* Detect no input or full output so that we don't get */
60
    /* a Z_BUF_ERROR return. */
61
6.87M
    if (pw->ptr == pw->limit)
62
190k
        return 1;
63
6.68M
    if (p == pr->limit && !last)
64
2.10M
        return 0;
65
4.57M
    zs->next_in = (Bytef *)p + 1;
66
4.57M
    zs->avail_in = pr->limit - p;
67
4.57M
    zs->next_out = pw->ptr + 1;
68
4.57M
    zs->avail_out = pw->limit - pw->ptr;
69
4.57M
    status = deflate(zs, (last ? Z_FINISH : Z_NO_FLUSH));
70
4.57M
    pr->ptr = zs->next_in - 1;
71
4.57M
    pw->ptr = zs->next_out - 1;
72
4.57M
    switch (status) {
73
4.35M
        case Z_OK:
74
4.35M
            return (pw->ptr == pw->limit ? 1 : pr->ptr > p && !last ? 0 : 1);
75
220k
        case Z_STREAM_END:
76
220k
            return (last && pr->ptr == pr->limit ? 0 : ERRC);
77
0
        default:
78
0
            return ERRC;
79
4.57M
    }
80
4.57M
}
81
82
/* Release the stream */
83
static void
84
s_zlibE_release(stream_state * st)
85
220k
{
86
220k
    stream_zlib_state *const ss = (stream_zlib_state *)st;
87
88
220k
    deflateEnd(&ss->dynamic->zstate);
89
220k
    s_zlib_free_dynamic_state(ss);
90
220k
}
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
};