ReactOS 0.4.16-dev-2380-gf63df20
cidparse.c
Go to the documentation of this file.
1/****************************************************************************
2 *
3 * cidparse.c
4 *
5 * CID-keyed Type1 parser (body).
6 *
7 * Copyright (C) 1996-2020 by
8 * David Turner, Robert Wilhelm, and Werner Lemberg.
9 *
10 * This file is part of the FreeType project, and may only be used,
11 * modified, and distributed under the terms of the FreeType project
12 * license, LICENSE.TXT. By continuing to use, modify, or distribute
13 * this file you indicate that you have read the license and
14 * understand and accept it fully.
15 *
16 */
17
18
22
23#include "cidparse.h"
24
25#include "ciderrs.h"
26
27
28 /**************************************************************************
29 *
30 * The macro FT_COMPONENT is used in trace mode. It is an implicit
31 * parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log
32 * messages during execution.
33 */
34#undef FT_COMPONENT
35#define FT_COMPONENT cidparse
36
37
38 /*************************************************************************/
39 /*************************************************************************/
40 /*************************************************************************/
41 /***** *****/
42 /***** INPUT STREAM PARSER *****/
43 /***** *****/
44 /*************************************************************************/
45 /*************************************************************************/
46 /*************************************************************************/
47
48
49#define STARTDATA "StartData"
50#define STARTDATA_LEN ( sizeof ( STARTDATA ) - 1 )
51#define SFNTS "/sfnts"
52#define SFNTS_LEN ( sizeof ( SFNTS ) - 1 )
53
54
59 PSAux_Service psaux )
60 {
62 FT_ULong base_offset, offset, ps_len;
63 FT_Byte *cur, *limit;
64 FT_Byte *arg1, *arg2;
65
66
67 FT_ZERO( parser );
68 psaux->ps_parser_funcs->init( &parser->root, 0, 0, memory );
69
70 parser->stream = stream;
71
72 base_offset = FT_STREAM_POS();
73
74 /* first of all, check the font format in the header */
75 if ( FT_FRAME_ENTER( 31 ) )
76 goto Exit;
77
78 if ( ft_strncmp( (char *)stream->cursor,
79 "%!PS-Adobe-3.0 Resource-CIDFont", 31 ) )
80 {
81 FT_TRACE2(( " not a CID-keyed font\n" ));
82 error = FT_THROW( Unknown_File_Format );
83 }
84
86 if ( error )
87 goto Exit;
88
89 Again:
90 /* now, read the rest of the file until we find */
91 /* `StartData' or `/sfnts' */
92 {
93 /*
94 * The algorithm is as follows (omitting the case with less than 256
95 * bytes to fill for simplicity).
96 *
97 * 1. Fill the buffer with 256 + STARTDATA_LEN bytes.
98 *
99 * 2. Search for the STARTDATA and SFNTS strings at positions
100 * buffer[0], buffer[1], ...,
101 * buffer[255 + STARTDATA_LEN - SFNTS_LEN].
102 *
103 * 3. Move the last STARTDATA_LEN bytes to buffer[0].
104 *
105 * 4. Fill the buffer with 256 bytes, starting at STARTDATA_LEN.
106 *
107 * 5. Repeat with step 2.
108 *
109 */
110 FT_Byte buffer[256 + STARTDATA_LEN + 1];
111
112 /* values for the first loop */
113 FT_ULong read_len = 256 + STARTDATA_LEN;
114 FT_ULong read_offset = 0;
115 FT_Byte* p = buffer;
116
117
118 for ( offset = FT_STREAM_POS(); ; offset += 256 )
119 {
120 FT_ULong stream_len;
121
122
123 stream_len = stream->size - FT_STREAM_POS();
124
125 read_len = FT_MIN( read_len, stream_len );
126 if ( FT_STREAM_READ( p, read_len ) )
127 goto Exit;
128
129 /* ensure that we do not compare with data beyond the buffer */
130 p[read_len] = '\0';
131
132 limit = p + read_len - SFNTS_LEN;
133
134 for ( p = buffer; p < limit; p++ )
135 {
136 if ( p[0] == 'S' &&
137 ft_strncmp( (char*)p, STARTDATA, STARTDATA_LEN ) == 0 )
138 {
139 /* save offset of binary data after `StartData' */
140 offset += (FT_ULong)( p - buffer ) + STARTDATA_LEN + 1;
141 goto Found;
142 }
143 else if ( p[1] == 's' &&
144 ft_strncmp( (char*)p, SFNTS, SFNTS_LEN ) == 0 )
145 {
146 offset += (FT_ULong)( p - buffer ) + SFNTS_LEN + 1;
147 goto Found;
148 }
149 }
150
151 if ( read_offset + read_len < STARTDATA_LEN )
152 {
153 FT_TRACE2(( "cid_parser_new: no `StartData' keyword found\n" ));
154 error = FT_THROW( Invalid_File_Format );
155 goto Exit;
156 }
157
159 buffer + read_offset + read_len - STARTDATA_LEN,
161
162 /* values for the next loop */
163 read_len = 256;
164 read_offset = STARTDATA_LEN;
165 p = buffer + read_offset;
166 }
167 }
168
169 Found:
170 /* We have found the start of the binary data or the `/sfnts' token. */
171 /* Now rewind and extract the frame corresponding to this PostScript */
172 /* section. */
173
174 ps_len = offset - base_offset;
175 if ( FT_STREAM_SEEK( base_offset ) ||
176 FT_FRAME_EXTRACT( ps_len, parser->postscript ) )
177 goto Exit;
178
179 parser->data_offset = offset;
180 parser->postscript_len = ps_len;
181 parser->root.base = parser->postscript;
182 parser->root.cursor = parser->postscript;
183 parser->root.limit = parser->root.cursor + ps_len;
184 parser->num_dict = -1;
185
186 /* Finally, we check whether `StartData' or `/sfnts' was real -- */
187 /* it could be in a comment or string. We also get the arguments */
188 /* of `StartData' to find out whether the data is represented in */
189 /* binary or hex format. */
190
191 arg1 = parser->root.cursor;
194 arg2 = parser->root.cursor;
197
198 limit = parser->root.limit;
199 cur = parser->root.cursor;
200
201 while ( cur <= limit - SFNTS_LEN )
202 {
203 if ( parser->root.error )
204 {
205 error = parser->root.error;
206 goto Exit;
207 }
208
209 if ( cur[0] == 'S' &&
210 cur <= limit - STARTDATA_LEN &&
211 ft_strncmp( (char*)cur, STARTDATA, STARTDATA_LEN ) == 0 )
212 {
213 if ( ft_strncmp( (char*)arg1, "(Hex)", 5 ) == 0 )
214 {
215 FT_Long tmp = ft_strtol( (const char *)arg2, NULL, 10 );
216
217
218 if ( tmp < 0 )
219 {
220 FT_ERROR(( "cid_parser_new: invalid length of hex data\n" ));
221 error = FT_THROW( Invalid_File_Format );
222 }
223 else
224 parser->binary_length = (FT_ULong)tmp;
225 }
226
227 goto Exit;
228 }
229 else if ( cur[1] == 's' &&
230 ft_strncmp( (char*)cur, SFNTS, SFNTS_LEN ) == 0 )
231 {
232 FT_TRACE2(( "cid_parser_new: cannot handle Type 11 fonts\n" ));
233 error = FT_THROW( Unknown_File_Format );
234 goto Exit;
235 }
236
239 arg1 = arg2;
240 arg2 = cur;
241 cur = parser->root.cursor;
242 }
243
244 /* we haven't found the correct `StartData'; go back and continue */
245 /* searching */
246 FT_FRAME_RELEASE( parser->postscript );
247 if ( !FT_STREAM_SEEK( offset ) )
248 goto Again;
249
250 Exit:
251 return error;
252 }
253
254
255#undef STARTDATA
256#undef STARTDATA_LEN
257#undef SFNTS
258#undef SFNTS_LEN
259
260
261 FT_LOCAL_DEF( void )
263 {
264 /* always free the private dictionary */
265 if ( parser->postscript )
266 {
267 FT_Stream stream = parser->stream;
268
269
270 FT_FRAME_RELEASE( parser->postscript );
271 }
272 parser->root.funcs.done( &parser->root );
273 }
274
275
276/* END */
return Found
Definition: dirsup.c:1270
#define STARTDATA_LEN
Definition: cidparse.c:50
cid_parser_new(CID_Parser *parser, FT_Stream stream, FT_Memory memory, PSAux_Service psaux)
Definition: cidparse.c:56
#define STARTDATA
Definition: cidparse.c:49
#define SFNTS
Definition: cidparse.c:51
#define SFNTS_LEN
Definition: cidparse.c:52
cid_parser_done(CID_Parser *parser)
Definition: cidparse.c:262
#define cid_parser_skip_PS_token(p)
Definition: cidparse.h:104
FT_BEGIN_HEADER struct CID_Parser_ CID_Parser
#define cid_parser_skip_spaces(p)
Definition: cidparse.h:102
#define FT_LOCAL_DEF(x)
#define NULL
Definition: types.h:112
#define FT_ERROR(varformat)
Definition: ftdebug.h:211
#define FT_THROW(e)
Definition: ftdebug.h:243
#define FT_TRACE2(varformat)
Definition: ftdebug.h:189
#define FT_ZERO(p)
Definition: ftmemory.h:246
#define FT_MEM_MOVE(dest, source, count)
Definition: ftmemory.h:240
#define FT_MIN(a, b)
Definition: ftobjs.h:70
#define ft_strncmp
Definition: ftstdlib.h:89
#define ft_strtol
Definition: ftstdlib.h:145
#define FT_FRAME_ENTER(size)
Definition: ftstream.h:548
#define FT_FRAME_RELEASE(bytes)
Definition: ftstream.h:562
#define FT_STREAM_SEEK(position)
Definition: ftstream.h:525
#define FT_STREAM_POS()
Definition: ftstream.h:522
#define FT_FRAME_EXIT()
Definition: ftstream.h:553
#define FT_FRAME_EXTRACT(size, bytes)
Definition: ftstream.h:556
#define FT_STREAM_READ(buffer, count)
Definition: ftstream.h:533
typedefFT_BEGIN_HEADER struct FT_MemoryRec_ * FT_Memory
Definition: ftsystem.h:64
unsigned long FT_ULong
Definition: fttypes.h:253
unsigned char FT_Byte
Definition: fttypes.h:154
int FT_Error
Definition: fttypes.h:299
signed long FT_Long
Definition: fttypes.h:242
FxCollectionEntry * cur
GLuint buffer
Definition: glext.h:5915
GLintptr offset
Definition: glext.h:5920
GLuint GLuint GLuint GLuint arg1
Definition: glext.h:9513
GLuint GLuint GLuint GLuint GLuint GLuint GLuint arg2
Definition: glext.h:9514
GLint limit
Definition: glext.h:10326
GLfloat GLfloat p
Definition: glext.h:8902
#define error(str)
Definition: mkdosfs.c:1605
static char memory[1024 *256]
Definition: process.c:122
static void Exit(void)
Definition: sock.c:1330
Definition: import.c:81
unsigned int error
Definition: inffile.c:97
Definition: parse.h:23
unsigned int size
Definition: parse.h:27