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 19 #include <freetype/internal/ftdebug.h> 20 #include <freetype/internal/ftobjs.h> 21 #include <freetype/internal/ftstream.h> 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 55 FT_LOCAL_DEF( FT_Error ) cid_parser_new(CID_Parser * parser,FT_Stream stream,FT_Memory memory,PSAux_Service psaux)56 cid_parser_new( CID_Parser* parser, 57 FT_Stream stream, 58 FT_Memory memory, 59 PSAux_Service psaux ) 60 { 61 FT_Error error; 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 85 FT_FRAME_EXIT(); 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 158 FT_MEM_MOVE( buffer, 159 buffer + read_offset + read_len - STARTDATA_LEN, 160 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; 192 cid_parser_skip_PS_token( parser ); 193 cid_parser_skip_spaces ( parser ); 194 arg2 = parser->root.cursor; 195 cid_parser_skip_PS_token( parser ); 196 cid_parser_skip_spaces ( parser ); 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 237 cid_parser_skip_PS_token( parser ); 238 cid_parser_skip_spaces ( parser ); 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 ) cid_parser_done(CID_Parser * parser)262 cid_parser_done( CID_Parser* parser ) 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 */ 277