t1parse.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /***************************************************************************/
  2. /* */
  3. /* t1parse.c */
  4. /* */
  5. /* Type 1 parser (body). */
  6. /* */
  7. /* Copyright 1996-2001, 2002 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. /* The Type 1 parser is in charge of the following: */
  20. /* */
  21. /* - provide an implementation of a growing sequence of objects called */
  22. /* a `T1_Table' (used to build various tables needed by the loader). */
  23. /* */
  24. /* - opening .pfb and .pfa files to extract their top-level and private */
  25. /* dictionaries. */
  26. /* */
  27. /* - read numbers, arrays & strings from any dictionary. */
  28. /* */
  29. /* See `t1load.c' to see how data is loaded from the font file. */
  30. /* */
  31. /*************************************************************************/
  32. #include <ft2build.h>
  33. #include FT_INTERNAL_DEBUG_H
  34. #include FT_INTERNAL_CALC_H
  35. #include FT_INTERNAL_STREAM_H
  36. #include FT_INTERNAL_POSTSCRIPT_AUX_H
  37. #include "t1parse.h"
  38. #include "t1errors.h"
  39. /*************************************************************************/
  40. /* */
  41. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  42. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  43. /* messages during execution. */
  44. /* */
  45. #undef FT_COMPONENT
  46. #define FT_COMPONENT trace_t1parse
  47. /*************************************************************************/
  48. /*************************************************************************/
  49. /*************************************************************************/
  50. /***** *****/
  51. /***** INPUT STREAM PARSER *****/
  52. /***** *****/
  53. /*************************************************************************/
  54. /*************************************************************************/
  55. /*************************************************************************/
  56. #define IS_T1_WHITESPACE( c ) ( (c) == ' ' || (c) == '\t' )
  57. #define IS_T1_LINESPACE( c ) ( (c) == '\r' || (c) == '\n' )
  58. #define IS_T1_SPACE( c ) ( IS_T1_WHITESPACE( c ) || IS_T1_LINESPACE( c ) )
  59. typedef struct PFB_Tag_
  60. {
  61. FT_UShort tag;
  62. FT_Long size;
  63. } PFB_Tag;
  64. #undef FT_STRUCTURE
  65. #define FT_STRUCTURE PFB_Tag
  66. static
  67. const FT_Frame_Field pfb_tag_fields[] =
  68. {
  69. FT_FRAME_START( 6 ),
  70. FT_FRAME_USHORT ( tag ),
  71. FT_FRAME_LONG_LE( size ),
  72. FT_FRAME_END
  73. };
  74. static FT_Error
  75. read_pfb_tag( FT_Stream stream,
  76. FT_UShort* tag,
  77. FT_Long* size )
  78. {
  79. FT_Error error;
  80. PFB_Tag head;
  81. *tag = 0;
  82. *size = 0;
  83. if ( !FT_STREAM_READ_FIELDS( pfb_tag_fields, &head ) )
  84. {
  85. if ( head.tag == 0x8001U || head.tag == 0x8002U )
  86. {
  87. *tag = head.tag;
  88. *size = head.size;
  89. }
  90. }
  91. return error;
  92. }
  93. FT_LOCAL_DEF( FT_Error )
  94. T1_New_Parser( T1_Parser parser,
  95. FT_Stream stream,
  96. FT_Memory memory,
  97. PSAux_Service psaux )
  98. {
  99. FT_Error error;
  100. FT_UShort tag;
  101. FT_Long size;
  102. psaux->ps_parser_funcs->init( &parser->root,0, 0, memory );
  103. parser->stream = stream;
  104. parser->base_len = 0;
  105. parser->base_dict = 0;
  106. parser->private_len = 0;
  107. parser->private_dict = 0;
  108. parser->in_pfb = 0;
  109. parser->in_memory = 0;
  110. parser->single_block = 0;
  111. /******************************************************************/
  112. /* */
  113. /* Here a short summary of what is going on: */
  114. /* */
  115. /* When creating a new Type 1 parser, we try to locate and load */
  116. /* the base dictionary if this is possible (i.e. for PFB */
  117. /* files). Otherwise, we load the whole font into memory. */
  118. /* */
  119. /* When `loading' the base dictionary, we only setup pointers */
  120. /* in the case of a memory-based stream. Otherwise, we */
  121. /* allocate and load the base dictionary in it. */
  122. /* */
  123. /* parser->in_pfb is set if we are in a binary (".pfb") font. */
  124. /* parser->in_memory is set if we have a memory stream. */
  125. /* */
  126. /* try to compute the size of the base dictionary; */
  127. /* look for a Postscript binary file tag, i.e 0x8001 */
  128. if ( FT_STREAM_SEEK( 0L ) )
  129. goto Exit;
  130. error = read_pfb_tag( stream, &tag, &size );
  131. if ( error )
  132. goto Exit;
  133. if ( tag != 0x8001U )
  134. {
  135. /* assume that this is a PFA file for now; an error will */
  136. /* be produced later when more things are checked */
  137. if ( FT_STREAM_SEEK( 0L ) )
  138. goto Exit;
  139. size = stream->size;
  140. }
  141. else
  142. parser->in_pfb = 1;
  143. /* now, try to load `size' bytes of the `base' dictionary we */
  144. /* found previously */
  145. /* if it is a memory-based resource, set up pointers */
  146. if ( !stream->read )
  147. {
  148. parser->base_dict = (FT_Byte*)stream->base + stream->pos;
  149. parser->base_len = size;
  150. parser->in_memory = 1;
  151. /* check that the `size' field is valid */
  152. if ( FT_STREAM_SKIP( size ) )
  153. goto Exit;
  154. }
  155. else
  156. {
  157. /* read segment in memory */
  158. if ( FT_ALLOC( parser->base_dict, size ) ||
  159. FT_STREAM_READ( parser->base_dict, size ) )
  160. goto Exit;
  161. parser->base_len = size;
  162. }
  163. /* Now check font format; we must see `%!PS-AdobeFont-1' */
  164. /* or `%!FontType' */
  165. {
  166. if ( size <= 16 ||
  167. ( ft_strncmp( (const char*)parser->base_dict,
  168. "%!PS-AdobeFont-1", 16 ) &&
  169. ft_strncmp( (const char*)parser->base_dict,
  170. "%!FontType", 10 ) ) )
  171. {
  172. FT_TRACE2(( "[not a Type1 font]\n" ));
  173. error = T1_Err_Unknown_File_Format;
  174. }
  175. else
  176. {
  177. parser->root.base = parser->base_dict;
  178. parser->root.cursor = parser->base_dict;
  179. parser->root.limit = parser->root.cursor + parser->base_len;
  180. }
  181. }
  182. Exit:
  183. if ( error && !parser->in_memory )
  184. FT_FREE( parser->base_dict );
  185. return error;
  186. }
  187. FT_LOCAL_DEF( void )
  188. T1_Finalize_Parser( T1_Parser parser )
  189. {
  190. FT_Memory memory = parser->root.memory;
  191. /* always free the private dictionary */
  192. FT_FREE( parser->private_dict );
  193. /* free the base dictionary only when we have a disk stream */
  194. if ( !parser->in_memory )
  195. FT_FREE( parser->base_dict );
  196. parser->root.funcs.done( &parser->root );
  197. }
  198. /* return the value of an hexadecimal digit */
  199. static int
  200. hexa_value( char c )
  201. {
  202. unsigned int d;
  203. d = (unsigned int)( c - '0' );
  204. if ( d <= 9 )
  205. return (int)d;
  206. d = (unsigned int)( c - 'a' );
  207. if ( d <= 5 )
  208. return (int)( d + 10 );
  209. d = (unsigned int)( c - 'A' );
  210. if ( d <= 5 )
  211. return (int)( d + 10 );
  212. return -1;
  213. }
  214. FT_LOCAL_DEF( FT_Error )
  215. T1_Get_Private_Dict( T1_Parser parser,
  216. PSAux_Service psaux )
  217. {
  218. FT_Stream stream = parser->stream;
  219. FT_Memory memory = parser->root.memory;
  220. FT_Error error = 0;
  221. FT_Long size;
  222. if ( parser->in_pfb )
  223. {
  224. /* in the case of the PFB format, the private dictionary can be */
  225. /* made of several segments. We thus first read the number of */
  226. /* segments to compute the total size of the private dictionary */
  227. /* then re-read them into memory. */
  228. FT_Long start_pos = FT_STREAM_POS();
  229. FT_UShort tag;
  230. parser->private_len = 0;
  231. for (;;)
  232. {
  233. error = read_pfb_tag( stream, &tag, &size );
  234. if ( error )
  235. goto Fail;
  236. if ( tag != 0x8002U )
  237. break;
  238. parser->private_len += size;
  239. if ( FT_STREAM_SKIP( size ) )
  240. goto Fail;
  241. }
  242. /* Check that we have a private dictionary there */
  243. /* and allocate private dictionary buffer */
  244. if ( parser->private_len == 0 )
  245. {
  246. FT_ERROR(( "T1_Get_Private_Dict:" ));
  247. FT_ERROR(( " invalid private dictionary section\n" ));
  248. error = T1_Err_Invalid_File_Format;
  249. goto Fail;
  250. }
  251. if ( FT_STREAM_SEEK( start_pos ) ||
  252. FT_ALLOC( parser->private_dict, parser->private_len ) )
  253. goto Fail;
  254. parser->private_len = 0;
  255. for (;;)
  256. {
  257. error = read_pfb_tag( stream, &tag, &size );
  258. if ( error || tag != 0x8002U )
  259. {
  260. error = T1_Err_Ok;
  261. break;
  262. }
  263. if ( FT_STREAM_READ( parser->private_dict + parser->private_len, size ) )
  264. goto Fail;
  265. parser->private_len += size;
  266. }
  267. }
  268. else
  269. {
  270. /* we have already `loaded' the whole PFA font file into memory; */
  271. /* if this is a memory resource, allocate a new block to hold */
  272. /* the private dict. Otherwise, simply overwrite into the base */
  273. /* dictionary block in the heap. */
  274. /* first of all, look at the `eexec' keyword */
  275. FT_Byte* cur = parser->base_dict;
  276. FT_Byte* limit = cur + parser->base_len;
  277. FT_Byte c;
  278. for (;;)
  279. {
  280. c = cur[0];
  281. if ( c == 'e' && cur + 9 < limit ) /* 9 = 5 letters for `eexec' + */
  282. /* newline + 4 chars */
  283. {
  284. if ( cur[1] == 'e' && cur[2] == 'x' &&
  285. cur[3] == 'e' && cur[4] == 'c' )
  286. {
  287. cur += 6; /* we skip the newling after the `eexec' */
  288. /* XXX: Some fonts use DOS-linefeeds, i.e. \r\n; we need to */
  289. /* skip the extra \n if we find it */
  290. if ( cur[0] == '\n' )
  291. cur++;
  292. break;
  293. }
  294. }
  295. cur++;
  296. if ( cur >= limit )
  297. {
  298. FT_ERROR(( "T1_Get_Private_Dict:" ));
  299. FT_ERROR(( " could not find `eexec' keyword\n" ));
  300. error = T1_Err_Invalid_File_Format;
  301. goto Exit;
  302. }
  303. }
  304. /* now determine where to write the _encrypted_ binary private */
  305. /* dictionary. We overwrite the base dictionary for disk-based */
  306. /* resources and allocate a new block otherwise */
  307. size = (FT_Long)( parser->base_len - ( cur - parser->base_dict ) );
  308. if ( parser->in_memory )
  309. {
  310. /* note that we allocate one more byte to put a terminating `0' */
  311. if ( FT_ALLOC( parser->private_dict, size + 1 ) )
  312. goto Fail;
  313. parser->private_len = size;
  314. }
  315. else
  316. {
  317. parser->single_block = 1;
  318. parser->private_dict = parser->base_dict;
  319. parser->private_len = size;
  320. parser->base_dict = 0;
  321. parser->base_len = 0;
  322. }
  323. /* now determine whether the private dictionary is encoded in binary */
  324. /* or hexadecimal ASCII format -- decode it accordingly */
  325. /* we need to access the next 4 bytes (after the final \r following */
  326. /* the `eexec' keyword); if they all are hexadecimal digits, then */
  327. /* we have a case of ASCII storage */
  328. if ( ( hexa_value( cur[0] ) | hexa_value( cur[1] ) |
  329. hexa_value( cur[2] ) | hexa_value( cur[3] ) ) < 0 )
  330. /* binary encoding -- `simply' copy the private dict */
  331. FT_MEM_COPY( parser->private_dict, cur, size );
  332. else
  333. {
  334. /* ASCII hexadecimal encoding */
  335. FT_Byte* write;
  336. FT_Int count;
  337. write = parser->private_dict;
  338. count = 0;
  339. for ( ;cur < limit; cur++ )
  340. {
  341. int hex1;
  342. /* check for newline */
  343. if ( cur[0] == '\r' || cur[0] == '\n' )
  344. continue;
  345. /* exit if we have a non-hexadecimal digit that isn't a newline */
  346. hex1 = hexa_value( cur[0] );
  347. if ( hex1 < 0 || cur + 1 >= limit )
  348. break;
  349. /* otherwise, store byte */
  350. *write++ = (FT_Byte)( ( hex1 << 4 ) | hexa_value( cur[1] ) );
  351. count++;
  352. cur++;
  353. }
  354. /* put a safeguard */
  355. parser->private_len = write - parser->private_dict;
  356. *write++ = 0;
  357. }
  358. }
  359. /* we now decrypt the encoded binary private dictionary */
  360. psaux->t1_decrypt( parser->private_dict, parser->private_len, 55665U );
  361. parser->root.base = parser->private_dict;
  362. parser->root.cursor = parser->private_dict;
  363. parser->root.limit = parser->root.cursor + parser->private_len;
  364. Fail:
  365. Exit:
  366. return error;
  367. }
  368. /* END */