Coverage Report

Created: 2026-10-28 08:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ghostpdl/base/slzwe.c
Line
Count
Source
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
/* LZW encoding filter */
18
#include "stdio_.h" /* includes std.h */
19
#include "gdebug.h"
20
#include "strimpl.h"
21
#include "slzwx.h"
22
23
/********************************************************/
24
/* LZW routines are based on:       */
25
/* Dr. Dobbs Journal --- Oct. 1989.       */
26
/* Article on LZW Data Compression by Mark R. Nelson  */
27
/********************************************************/
28
29
/* Define the special codes */
30
228k
#define code_reset 256
31
6.23G
#define code_eod 257
32
114k
#define code_0 258      /* first assignable code */
33
34
/* Import the stream closing procedure */
35
extern stream_proc_release(s_LZW_release);
36
37
typedef struct lzw_encode_s {
38
        byte datum;     /* last byte of this code */
39
        ushort prefix;      /* code for prefix of this code */
40
} lzw_encode;
41
42
69.4G
#define encode_max 4095    /* max # of codes, must be */
43
                                /* > code_0 and <= 4095 */
44
34.7G
#define hash_size (encode_max + encode_max / 4)
45
46
struct lzw_encode_table_s {
47
        lzw_encode encode[encode_max];
48
        ushort hashed[hash_size];
49
};
50
gs_private_st_simple(st_lzwe_table, lzw_encode_table, "lzw_encode_table");
51
52
/* Hashing function */
53
#define encode_hash(code, chr)\
54
17.0G
  ((uint)((code) * 59 + (chr) * ((hash_size / 256) | 1)) % hash_size)
55
56
/* Internal routine to put a code into the output buffer. */
57
/* Let S = ss->code_size, M = ss->next_code, N = 1 << M. */
58
/* Relevant invariants: 9 <= S <= 12; N / 2 <= M < N; 0 <= code < N; */
59
/* 1 <= ss->bits_left <= 8; only the rightmost (8 - ss->bits_left) */
60
/* bits of ss->bits contain valid data. */
61
static byte *
62
lzw_put_code(register stream_LZW_state *ss, byte *q, uint code)
63
206M
{ uint size = ss->code_size;
64
206M
        byte cb = (ss->bits << ss->bits_left) +
65
206M
                (code >> (size - ss->bits_left));
66
206M
        if_debug2m('W', ss->memory, "[w]writing 0x%x,%d\n", code, ss->code_size);
67
206M
        *++q = cb;
68
206M
        if ( (ss->bits_left += 8 - size) <= 0 )
69
81.6M
        { *++q = code >> -ss->bits_left;
70
81.6M
                ss->bits_left += 8;
71
81.6M
        }
72
206M
        ss->bits = code;
73
206M
        return q;
74
206M
}
75
76
/* Internal routine to reset the encoding table */
77
static void
78
lzw_reset_encode(stream_LZW_state *ss)
79
114k
{ register int c;
80
114k
        lzw_encode_table *table = ss->table.encode;
81
114k
        ss->next_code = code_0;
82
114k
        ss->code_size = 9;
83
114k
        ss->prev_code = code_eod;
84
584M
        for ( c = 0; c < hash_size; c++ )
85
584M
                table->hashed[c] = code_eod;
86
29.3M
        for ( c = 0; c < 256; c++ )
87
29.2M
        { lzw_encode *ec = &table->encode[c];
88
29.2M
                register ushort *tc = &table->hashed[encode_hash(code_eod, c)];
89
29.2M
                while ( *tc != code_eod )
90
0
                  if ( ++tc == &table->hashed[hash_size] )
91
0
                    tc = &table->hashed[0];
92
29.2M
                *tc = c;
93
29.2M
                ec->datum = c, ec->prefix = code_eod;
94
29.2M
        }
95
114k
        table->encode[code_eod].prefix = code_reset; /* guarantee no match */
96
114k
}
97
98
1.03G
#define ss ((stream_LZW_state *)st)
99
100
/* Initialize LZWEncode filter */
101
static int
102
s_LZWE_init(stream_state *st)
103
68.5k
{ ss->bits_left = 8;
104
68.5k
        ss->bits = 0; /* for Purify, the value unimportant due to ss->bits_left == 8 */
105
68.5k
        ss->table.encode = gs_alloc_struct(st->memory,
106
68.5k
                        lzw_encode_table, &st_lzwe_table, "LZWEncode init");
107
68.5k
        if ( ss->table.encode == 0 )
108
0
                return ERRC;   /****** WRONG ******/
109
68.5k
        ss->first = true;
110
68.5k
        lzw_reset_encode(ss);
111
68.5k
        return 0;
112
68.5k
}
113
114
/* Process a buffer */
115
static int
116
s_LZWE_process(stream_state *st, stream_cursor_read *pr,
117
  stream_cursor_write *pw, bool last)
118
68.4M
{ register const byte *p = pr->ptr;
119
68.4M
        const byte *rlimit = pr->limit;
120
68.4M
        register byte *q = pw->ptr;
121
68.4M
        byte *wlimit = pw->limit;
122
68.4M
        int code = ss->prev_code;
123
68.4M
        lzw_encode_table *table = ss->table.encode;
124
68.4M
        ushort *table_end = &table->hashed[hash_size];
125
68.4M
        int status = 0;
126
68.4M
        int limit_code;
127
68.4M
#define set_limit_code()\
128
68.6M
  limit_code = (1 << ss->code_size) - ss->EarlyChange;\
129
68.6M
  if ( limit_code > encode_max ) limit_code = encode_max
130
68.4M
        set_limit_code();
131
68.4M
        if ( ss->first )
132
68.5k
        { /* Emit the initial reset code. */
133
68.5k
                if ( wlimit - q < 2 )
134
0
                        return 1;
135
68.5k
                q = lzw_put_code(ss, q, code_reset);
136
68.5k
                ss->first = false;
137
68.5k
        }
138
17.0G
        while ( p < rlimit )
139
16.9G
        { byte c = p[1];
140
16.9G
                ushort *tp;
141
16.9G
                for ( tp = &table->hashed[encode_hash(code, c)]; ; )
142
22.1G
                { lzw_encode *ep = &table->encode[*tp];
143
22.1G
                        if ( ep->prefix == code && ep->datum == c )
144
16.7G
                        { code = *tp;
145
16.7G
                                p++;
146
16.7G
                                break;
147
16.7G
                        }
148
5.38G
                        else if ( *tp != code_eod )
149
5.17G
                        { if ( ++tp == table_end )
150
3.50M
                                  tp = &table->hashed[0]; /* wrap around */
151
5.17G
                        }
152
208M
                        else
153
208M
                        { /* end of recognized sequence */
154
208M
                                if ( wlimit - q <= 4 )
155
2.25M
                                { status = 1;
156
2.25M
                                        goto out;
157
2.25M
                                }
158
206M
                                q = lzw_put_code(ss, q, code);
159
206M
                                if ( ss->next_code == limit_code )
160
220k
                                { /* Reached power of 2 or limit. */
161
                                        /* Determine which. */
162
220k
                                        if ( ss->next_code == encode_max )
163
45.7k
                                        { q = lzw_put_code(ss, q, code_reset);
164
45.7k
                                                lzw_reset_encode(ss);
165
45.7k
                                                set_limit_code();
166
45.7k
                                                goto cx;
167
45.7k
                                        }
168
175k
                                        ss->code_size++;
169
175k
                                        set_limit_code();
170
175k
                                }
171
206M
                                if_debug3m('W', ss->memory, "[W]encoding 0x%x=0x%x+%c\n",
172
206M
                                          ss->next_code, code, c);
173
206M
                                *tp = ss->next_code++;
174
206M
                                ep = &table->encode[*tp];
175
206M
                                ep->datum = c;
176
206M
                                ep->prefix = code;
177
206M
cx:       code = code_eod;
178
206M
                                break;
179
206M
                        }
180
22.1G
                }
181
16.9G
        }
182
66.2M
        if ( last && status == 0 )
183
68.5k
        { if ( wlimit - q < 4 )
184
0
                        status = 1;
185
68.5k
                else
186
68.5k
                  { if ( code != code_eod )
187
67.1k
                          { q = lzw_put_code(ss, q, code); /* put out final code */
188
67.1k
                                if (ss->next_code == limit_code && ss->next_code != encode_max)
189
32
                                    ss->code_size++;
190
67.1k
                          }
191
68.5k
                        q = lzw_put_code(ss, q, code_eod);
192
68.5k
                        if ( ss->bits_left < 8 )
193
66.9k
                          *++q = ss->bits << ss->bits_left;  /* final byte */
194
68.5k
                  }
195
68.5k
        }
196
68.4M
out:  ss->prev_code = code;
197
68.4M
        pr->ptr = p;
198
68.4M
        pw->ptr = q;
199
68.4M
        return status;
200
66.2M
}
201
202
#undef ss
203
204
/* Stream template */
205
const stream_template s_LZWE_template =
206
{ &st_LZW_state, s_LZWE_init, s_LZWE_process, 1, 4, s_LZW_release,
207
        s_LZW_set_defaults
208
};