Coverage Report

Created: 2025-06-10 06:59

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