Coverage Report

Created: 2025-06-24 07:01

/src/ghostpdl/base/szlibc.c
Line
Count
Source (jump to first uncovered line)
1
/* Copyright (C) 2001-2024 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
/* Code common to zlib encoding and decoding streams */
18
#include "std.h"
19
#include "gserrors.h"
20
#include "gstypes.h"
21
#include "gsmemory.h"
22
#include "gsstruct.h"
23
#include "strimpl.h"
24
#include "szlibxx.h"
25
#include "zconf.h"
26
27
private_st_zlib_block();
28
private_st_zlib_dynamic_state();
29
public_st_zlib_state();
30
31
/* Set defaults for stream parameters. */
32
void
33
s_zlib_set_defaults(stream_state * st)
34
7.31M
{
35
7.31M
    stream_zlib_state *const ss = (stream_zlib_state *)st;
36
37
7.31M
    ss->windowBits = MAX_WBITS;
38
7.31M
    ss->no_wrapper = false;
39
7.31M
    ss->level = Z_DEFAULT_COMPRESSION;
40
7.31M
    ss->method = Z_DEFLATED;
41
    /* DEF_MEM_LEVEL should be in zlib.h or zconf.h, but it isn't. */
42
7.31M
    ss->memLevel = min(MAX_MEM_LEVEL, 8);
43
7.31M
    ss->strategy = Z_DEFAULT_STRATEGY;
44
    /* Clear pointers */
45
7.31M
    ss->dynamic = 0;
46
7.31M
}
47
48
/* Allocate the dynamic state. */
49
int
50
s_zlib_alloc_dynamic_state(stream_zlib_state *ss)
51
2.25M
{
52
2.25M
    gs_memory_t *mem = ss->memory;
53
2.25M
    zlib_dynamic_state_t *zds =
54
2.25M
        gs_alloc_struct_immovable(mem, zlib_dynamic_state_t,
55
2.25M
                                  &st_zlib_dynamic_state,
56
2.25M
                                  "s_zlib_alloc_dynamic_state");
57
58
2.25M
    ss->dynamic = zds;
59
2.25M
    if (zds == 0)
60
0
        return_error(gs_error_VMerror);
61
2.25M
    zds->blocks = 0;
62
2.25M
    zds->memory = mem;
63
2.25M
    zds->zstate.zalloc = (alloc_func)s_zlib_alloc;
64
2.25M
    zds->zstate.zfree = (free_func)s_zlib_free;
65
2.25M
    zds->zstate.opaque = (voidpf)zds;
66
2.25M
    return 0;
67
2.25M
}
68
69
/* Free the dynamic state. */
70
void
71
s_zlib_free_dynamic_state(stream_zlib_state *ss)
72
2.25M
{
73
2.25M
    if (ss->dynamic)
74
2.25M
        gs_free_object(ss->dynamic->memory, ss->dynamic,
75
2.25M
                       "s_zlib_free_dynamic_state");
76
2.25M
}
77
78
/* Provide zlib-compatible allocation and freeing functions. */
79
void *
80
s_zlib_alloc(void *zmem, uint items, uint size)
81
4.19M
{
82
4.19M
    zlib_dynamic_state_t *const zds = zmem;
83
4.19M
    gs_memory_t *mem = zds->memory->stable_memory;
84
4.19M
    zlib_block_t *block =
85
4.19M
        gs_alloc_struct(mem, zlib_block_t, &st_zlib_block,
86
4.19M
                        "s_zlib_alloc(block)");
87
4.19M
    void *data =
88
4.19M
        gs_alloc_byte_array_immovable(mem, items, size, "s_zlib_alloc(data)");
89
90
4.19M
    if (block == 0 || data == 0) {
91
0
        gs_free_object(mem, data, "s_zlib_alloc(data)");
92
0
        gs_free_object(mem, block, "s_zlib_alloc(block)");
93
0
        return Z_NULL;
94
0
    }
95
4.19M
    block->data = data;
96
4.19M
    block->next = zds->blocks;
97
4.19M
    block->prev = 0;
98
4.19M
    if (zds->blocks)
99
1.94M
        zds->blocks->prev = block;
100
4.19M
    zds->blocks = block;
101
4.19M
    return data;
102
4.19M
}
103
void
104
s_zlib_free(void *zmem, void *data)
105
4.19M
{
106
4.19M
    zlib_dynamic_state_t *const zds = zmem;
107
4.19M
    gs_memory_t *mem = zds->memory->stable_memory;
108
4.19M
    zlib_block_t *block = zds->blocks;
109
110
4.19M
    gs_free_object(mem, data, "s_zlib_free(data)");
111
4.19M
    for (; ; block = block->next) {
112
4.19M
        if (block == 0) {
113
#ifdef DEBUG
114
            lprintf1("Freeing unrecorded data "PRI_INTPTR"!\n", (intptr_t)data);
115
#endif
116
0
            return;
117
0
        }
118
4.19M
        if (block->data == data)
119
4.19M
            break;
120
4.19M
    }
121
4.19M
    if (block->next)
122
1.94M
        block->next->prev = block->prev;
123
4.19M
    if (block->prev)
124
0
        block->prev->next = block->next;
125
4.19M
    else
126
4.19M
        zds->blocks = block->next;
127
4.19M
    gs_free_object(mem, block, "s_zlib_free(block)");
128
4.19M
}