sdk-world v2026.11.05
PS2 SDK - world tier (copyleft-allowed, non-essential packages)
Loading...
Searching...
No Matches
ftzopen.h
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * ftzopen.h
4 *
5 * FreeType support for .Z compressed files.
6 *
7 * This optional component relies on NetBSD's zopen(). It should mainly
8 * be used to parse compressed PCF fonts, as found with many X11 server
9 * distributions.
10 *
11 * Copyright (C) 2005-2025 by
12 * David Turner.
13 *
14 * This file is part of the FreeType project, and may only be used,
15 * modified, and distributed under the terms of the FreeType project
16 * license, LICENSE.TXT. By continuing to use, modify, or distribute
17 * this file you indicate that you have read the license and
18 * understand and accept it fully.
19 *
20 */
21
26#ifndef FTZOPEN_H_
27#define FTZOPEN_H_
28
29#include <freetype/freetype.h>
30
31FT_BEGIN_HEADER
32
33 /*
34 * This is a complete re-implementation of the LZW file reader,
35 * since the old one was incredibly badly written, using
36 * 400 KByte of heap memory before decompressing anything.
37 *
38 */
39
40#define FT_LZW_IN_BUFF_SIZE 64
41#define FT_LZW_DEFAULT_STACK_SIZE 64
42
43#define LZW_INIT_BITS 9
44#define LZW_MAX_BITS 16
45
46#define LZW_CLEAR 256
47#define LZW_FIRST 257
48
49#define LZW_BIT_MASK 0x1F
50#define LZW_BLOCK_MASK 0x80
51#define LZW_MASK( n ) ( ( 1U << (n) ) - 1U )
52
53
54 typedef enum FT_LzwPhase_
55 {
56 FT_LZW_PHASE_START = 0,
57 FT_LZW_PHASE_CODE,
58 FT_LZW_PHASE_STACK,
59 FT_LZW_PHASE_EOF
60
61 } FT_LzwPhase;
62
63
64 /*
65 * state of LZW decompressor
66 *
67 * small technical note
68 * --------------------
69 *
70 * We use a few tricks in this implementation that are explained here to
71 * ease debugging and maintenance.
72 *
73 * - First of all, the `prefix' and `suffix' arrays contain the suffix
74 * and prefix for codes over 256; this means that
75 *
76 * prefix_of(code) == state->prefix[code-256]
77 * suffix_of(code) == state->suffix[code-256]
78 *
79 * Each prefix is a 16-bit code, and each suffix an 8-bit byte.
80 *
81 * Both arrays are stored in a single memory block, pointed to by
82 * `state->prefix'. This means that the following equality is always
83 * true:
84 *
85 * state->suffix == (FT_Byte*)(state->prefix + state->prefix_size)
86 *
87 * Of course, state->prefix_size is the number of prefix/suffix slots
88 * in the arrays, corresponding to codes 256..255+prefix_size.
89 *
90 * - `free_ent' is the index of the next free entry in the `prefix'
91 * and `suffix' arrays. This means that the corresponding `next free
92 * code' is really `256+free_ent'.
93 *
94 * Moreover, `max_free' is the maximum value that `free_ent' can reach.
95 *
96 * `max_free' corresponds to `(1 << max_bits) - 256'. Note that this
97 * value is always <= 0xFF00, which means that both `free_ent' and
98 * `max_free' can be stored in an FT_UInt variable, even on 16-bit
99 * machines.
100 *
101 * If `free_ent == max_free', you cannot add new codes to the
102 * prefix/suffix table.
103 *
104 * - `num_bits' is the current number of code bits, starting at 9 and
105 * growing each time `free_ent' reaches the value of `free_bits'. The
106 * latter is computed as follows
107 *
108 * if num_bits < max_bits:
109 * free_bits = (1 << num_bits)-256
110 * else:
111 * free_bits = max_free + 1
112 *
113 * Since the value of `max_free + 1' can never be reached by
114 * `free_ent', `num_bits' cannot grow larger than `max_bits'.
115 */
116
117 typedef struct FT_LzwStateRec_
118 {
119 FT_LzwPhase phase;
120 FT_Int in_eof;
121
122 FT_Byte buf_tab[16];
123 FT_UInt buf_offset;
124 FT_UInt buf_size;
125 FT_Bool buf_clear;
126 FT_Offset buf_total;
127
128 FT_UInt max_bits; /* max code bits, from file header */
129 FT_Int block_mode; /* block mode flag, from file header */
130 FT_UInt max_free; /* (1 << max_bits) - 256 */
131
132 FT_UInt num_bits; /* current code bit number */
133 FT_UInt free_ent; /* index of next free entry */
134 FT_UInt free_bits; /* if reached by free_ent, increment num_bits */
135 FT_UInt old_code;
136 FT_UInt old_char;
137 FT_UInt in_code;
138
139 FT_UShort* prefix; /* always dynamically allocated / reallocated */
140 FT_Byte* suffix; /* suffix = (FT_Byte*)(prefix + prefix_size) */
141 FT_UInt prefix_size; /* number of slots in `prefix' or `suffix' */
142
143 FT_Byte* stack; /* character stack */
144 FT_UInt stack_top;
145 FT_Offset stack_size;
146 FT_Byte stack_0[FT_LZW_DEFAULT_STACK_SIZE]; /* minimize heap alloc */
147
148 FT_Stream source; /* source stream */
149 FT_Memory memory;
150
151 } FT_LzwStateRec, *FT_LzwState;
152
153
154 FT_LOCAL( void )
155 ft_lzwstate_init( FT_LzwState state,
156 FT_Stream source );
157
158 FT_LOCAL( void )
159 ft_lzwstate_done( FT_LzwState state );
160
161
162 FT_LOCAL( void )
163 ft_lzwstate_reset( FT_LzwState state );
164
165
166 FT_LOCAL( FT_ULong )
167 ft_lzwstate_io( FT_LzwState state,
168 FT_Byte* buffer,
169 FT_ULong out_size );
170
171/* */
172
173FT_END_HEADER
174
175#endif /* FTZOPEN_H_ */
176
177
178/* END */