/src/freetype/src/type1/t1parse.c

https://bitbucket.org/cabalistic/ogredeps/ · C · 494 lines · 288 code · 94 blank · 112 comment · 71 complexity · 183ad04ee764ebeeb4da559d08b1e18d MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* t1parse.c */
  4. /* */
  5. /* Type 1 parser (body). */
  6. /* */
  7. /* Copyright 1996-2005, 2008, 2009, 2012 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_STREAM_H
  35. #include FT_INTERNAL_POSTSCRIPT_AUX_H
  36. #include "t1parse.h"
  37. #include "t1errors.h"
  38. /*************************************************************************/
  39. /* */
  40. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  41. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  42. /* messages during execution. */
  43. /* */
  44. #undef FT_COMPONENT
  45. #define FT_COMPONENT trace_t1parse
  46. /*************************************************************************/
  47. /*************************************************************************/
  48. /*************************************************************************/
  49. /***** *****/
  50. /***** INPUT STREAM PARSER *****/
  51. /***** *****/
  52. /*************************************************************************/
  53. /*************************************************************************/
  54. /*************************************************************************/
  55. /* see Adobe Technical Note 5040.Download_Fonts.pdf */
  56. static FT_Error
  57. read_pfb_tag( FT_Stream stream,
  58. FT_UShort *atag,
  59. FT_ULong *asize )
  60. {
  61. FT_Error error;
  62. FT_UShort tag;
  63. FT_ULong size;
  64. *atag = 0;
  65. *asize = 0;
  66. if ( !FT_READ_USHORT( tag ) )
  67. {
  68. if ( tag == 0x8001U || tag == 0x8002U )
  69. {
  70. if ( !FT_READ_ULONG_LE( size ) )
  71. *asize = size;
  72. }
  73. *atag = tag;
  74. }
  75. return error;
  76. }
  77. static FT_Error
  78. check_type1_format( FT_Stream stream,
  79. const char* header_string,
  80. size_t header_length )
  81. {
  82. FT_Error error;
  83. FT_UShort tag;
  84. FT_ULong dummy;
  85. if ( FT_STREAM_SEEK( 0 ) )
  86. goto Exit;
  87. error = read_pfb_tag( stream, &tag, &dummy );
  88. if ( error )
  89. goto Exit;
  90. /* We assume that the first segment in a PFB is always encoded as */
  91. /* text. This might be wrong (and the specification doesn't insist */
  92. /* on that), but we have never seen a counterexample. */
  93. if ( tag != 0x8001U && FT_STREAM_SEEK( 0 ) )
  94. goto Exit;
  95. if ( !FT_FRAME_ENTER( header_length ) )
  96. {
  97. error = T1_Err_Ok;
  98. if ( ft_memcmp( stream->cursor, header_string, header_length ) != 0 )
  99. error = T1_Err_Unknown_File_Format;
  100. FT_FRAME_EXIT();
  101. }
  102. Exit:
  103. return error;
  104. }
  105. FT_LOCAL_DEF( FT_Error )
  106. T1_New_Parser( T1_Parser parser,
  107. FT_Stream stream,
  108. FT_Memory memory,
  109. PSAux_Service psaux )
  110. {
  111. FT_Error error;
  112. FT_UShort tag;
  113. FT_ULong size;
  114. psaux->ps_parser_funcs->init( &parser->root, 0, 0, memory );
  115. parser->stream = stream;
  116. parser->base_len = 0;
  117. parser->base_dict = 0;
  118. parser->private_len = 0;
  119. parser->private_dict = 0;
  120. parser->in_pfb = 0;
  121. parser->in_memory = 0;
  122. parser->single_block = 0;
  123. /* check the header format */
  124. error = check_type1_format( stream, "%!PS-AdobeFont", 14 );
  125. if ( error )
  126. {
  127. if ( error != T1_Err_Unknown_File_Format )
  128. goto Exit;
  129. error = check_type1_format( stream, "%!FontType", 10 );
  130. if ( error )
  131. {
  132. FT_TRACE2(( " not a Type 1 font\n" ));
  133. goto Exit;
  134. }
  135. }
  136. /******************************************************************/
  137. /* */
  138. /* Here a short summary of what is going on: */
  139. /* */
  140. /* When creating a new Type 1 parser, we try to locate and load */
  141. /* the base dictionary if this is possible (i.e., for PFB */
  142. /* files). Otherwise, we load the whole font into memory. */
  143. /* */
  144. /* When `loading' the base dictionary, we only setup pointers */
  145. /* in the case of a memory-based stream. Otherwise, we */
  146. /* allocate and load the base dictionary in it. */
  147. /* */
  148. /* parser->in_pfb is set if we are in a binary (`.pfb') font. */
  149. /* parser->in_memory is set if we have a memory stream. */
  150. /* */
  151. /* try to compute the size of the base dictionary; */
  152. /* look for a Postscript binary file tag, i.e., 0x8001 */
  153. if ( FT_STREAM_SEEK( 0L ) )
  154. goto Exit;
  155. error = read_pfb_tag( stream, &tag, &size );
  156. if ( error )
  157. goto Exit;
  158. if ( tag != 0x8001U )
  159. {
  160. /* assume that this is a PFA file for now; an error will */
  161. /* be produced later when more things are checked */
  162. if ( FT_STREAM_SEEK( 0L ) )
  163. goto Exit;
  164. size = stream->size;
  165. }
  166. else
  167. parser->in_pfb = 1;
  168. /* now, try to load `size' bytes of the `base' dictionary we */
  169. /* found previously */
  170. /* if it is a memory-based resource, set up pointers */
  171. if ( !stream->read )
  172. {
  173. parser->base_dict = (FT_Byte*)stream->base + stream->pos;
  174. parser->base_len = size;
  175. parser->in_memory = 1;
  176. /* check that the `size' field is valid */
  177. if ( FT_STREAM_SKIP( size ) )
  178. goto Exit;
  179. }
  180. else
  181. {
  182. /* read segment in memory -- this is clumsy, but so does the format */
  183. if ( FT_ALLOC( parser->base_dict, size ) ||
  184. FT_STREAM_READ( parser->base_dict, size ) )
  185. goto Exit;
  186. parser->base_len = size;
  187. }
  188. parser->root.base = parser->base_dict;
  189. parser->root.cursor = parser->base_dict;
  190. parser->root.limit = parser->root.cursor + parser->base_len;
  191. Exit:
  192. if ( error && !parser->in_memory )
  193. FT_FREE( parser->base_dict );
  194. return error;
  195. }
  196. FT_LOCAL_DEF( void )
  197. T1_Finalize_Parser( T1_Parser parser )
  198. {
  199. FT_Memory memory = parser->root.memory;
  200. /* always free the private dictionary */
  201. FT_FREE( parser->private_dict );
  202. /* free the base dictionary only when we have a disk stream */
  203. if ( !parser->in_memory )
  204. FT_FREE( parser->base_dict );
  205. parser->root.funcs.done( &parser->root );
  206. }
  207. FT_LOCAL_DEF( FT_Error )
  208. T1_Get_Private_Dict( T1_Parser parser,
  209. PSAux_Service psaux )
  210. {
  211. FT_Stream stream = parser->stream;
  212. FT_Memory memory = parser->root.memory;
  213. FT_Error error = T1_Err_Ok;
  214. FT_ULong size;
  215. if ( parser->in_pfb )
  216. {
  217. /* in the case of the PFB format, the private dictionary can be */
  218. /* made of several segments. We thus first read the number of */
  219. /* segments to compute the total size of the private dictionary */
  220. /* then re-read them into memory. */
  221. FT_Long start_pos = FT_STREAM_POS();
  222. FT_UShort tag;
  223. parser->private_len = 0;
  224. for (;;)
  225. {
  226. error = read_pfb_tag( stream, &tag, &size );
  227. if ( error )
  228. goto Fail;
  229. if ( tag != 0x8002U )
  230. break;
  231. parser->private_len += size;
  232. if ( FT_STREAM_SKIP( size ) )
  233. goto Fail;
  234. }
  235. /* Check that we have a private dictionary there */
  236. /* and allocate private dictionary buffer */
  237. if ( parser->private_len == 0 )
  238. {
  239. FT_ERROR(( "T1_Get_Private_Dict:"
  240. " invalid private dictionary section\n" ));
  241. error = T1_Err_Invalid_File_Format;
  242. goto Fail;
  243. }
  244. if ( FT_STREAM_SEEK( start_pos ) ||
  245. FT_ALLOC( parser->private_dict, parser->private_len ) )
  246. goto Fail;
  247. parser->private_len = 0;
  248. for (;;)
  249. {
  250. error = read_pfb_tag( stream, &tag, &size );
  251. if ( error || tag != 0x8002U )
  252. {
  253. error = T1_Err_Ok;
  254. break;
  255. }
  256. if ( FT_STREAM_READ( parser->private_dict + parser->private_len,
  257. size ) )
  258. goto Fail;
  259. parser->private_len += size;
  260. }
  261. }
  262. else
  263. {
  264. /* We have already `loaded' the whole PFA font file into memory; */
  265. /* if this is a memory resource, allocate a new block to hold */
  266. /* the private dict. Otherwise, simply overwrite into the base */
  267. /* dictionary block in the heap. */
  268. /* first of all, look at the `eexec' keyword */
  269. FT_Byte* cur = parser->base_dict;
  270. FT_Byte* limit = cur + parser->base_len;
  271. FT_Byte c;
  272. Again:
  273. for (;;)
  274. {
  275. c = cur[0];
  276. if ( c == 'e' && cur + 9 < limit ) /* 9 = 5 letters for `eexec' + */
  277. /* newline + 4 chars */
  278. {
  279. if ( cur[1] == 'e' &&
  280. cur[2] == 'x' &&
  281. cur[3] == 'e' &&
  282. cur[4] == 'c' )
  283. break;
  284. }
  285. cur++;
  286. if ( cur >= limit )
  287. {
  288. FT_ERROR(( "T1_Get_Private_Dict:"
  289. " could not find `eexec' keyword\n" ));
  290. error = T1_Err_Invalid_File_Format;
  291. goto Exit;
  292. }
  293. }
  294. /* check whether `eexec' was real -- it could be in a comment */
  295. /* or string (as e.g. in u003043t.gsf from ghostscript) */
  296. parser->root.cursor = parser->base_dict;
  297. parser->root.limit = cur + 9;
  298. cur = parser->root.cursor;
  299. limit = parser->root.limit;
  300. while ( cur < limit )
  301. {
  302. if ( *cur == 'e' && ft_strncmp( (char*)cur, "eexec", 5 ) == 0 )
  303. goto Found;
  304. T1_Skip_PS_Token( parser );
  305. if ( parser->root.error )
  306. break;
  307. T1_Skip_Spaces ( parser );
  308. cur = parser->root.cursor;
  309. }
  310. /* we haven't found the correct `eexec'; go back and continue */
  311. /* searching */
  312. cur = limit;
  313. limit = parser->base_dict + parser->base_len;
  314. goto Again;
  315. /* now determine where to write the _encrypted_ binary private */
  316. /* dictionary. We overwrite the base dictionary for disk-based */
  317. /* resources and allocate a new block otherwise */
  318. Found:
  319. parser->root.limit = parser->base_dict + parser->base_len;
  320. T1_Skip_PS_Token( parser );
  321. cur = parser->root.cursor;
  322. /* according to the Type1 spec, the first cipher byte must not be */
  323. /* an ASCII whitespace character code (blank, tab, carriage return */
  324. /* or line feed). We have seen Type 1 fonts with two line feed */
  325. /* characters... So skip now all whitespace character codes. */
  326. while ( cur < limit &&
  327. ( *cur == ' ' ||
  328. *cur == '\t' ||
  329. *cur == '\r' ||
  330. *cur == '\n' ) )
  331. ++cur;
  332. if ( cur >= limit )
  333. {
  334. FT_ERROR(( "T1_Get_Private_Dict:"
  335. " `eexec' not properly terminated\n" ));
  336. error = T1_Err_Invalid_File_Format;
  337. goto Exit;
  338. }
  339. size = parser->base_len - ( cur - parser->base_dict );
  340. if ( parser->in_memory )
  341. {
  342. /* note that we allocate one more byte to put a terminating `0' */
  343. if ( FT_ALLOC( parser->private_dict, size + 1 ) )
  344. goto Fail;
  345. parser->private_len = size;
  346. }
  347. else
  348. {
  349. parser->single_block = 1;
  350. parser->private_dict = parser->base_dict;
  351. parser->private_len = size;
  352. parser->base_dict = 0;
  353. parser->base_len = 0;
  354. }
  355. /* now determine whether the private dictionary is encoded in binary */
  356. /* or hexadecimal ASCII format -- decode it accordingly */
  357. /* we need to access the next 4 bytes (after the final \r following */
  358. /* the `eexec' keyword); if they all are hexadecimal digits, then */
  359. /* we have a case of ASCII storage */
  360. if ( ft_isxdigit( cur[0] ) && ft_isxdigit( cur[1] ) &&
  361. ft_isxdigit( cur[2] ) && ft_isxdigit( cur[3] ) )
  362. {
  363. /* ASCII hexadecimal encoding */
  364. FT_Long len;
  365. parser->root.cursor = cur;
  366. (void)psaux->ps_parser_funcs->to_bytes( &parser->root,
  367. parser->private_dict,
  368. parser->private_len,
  369. &len,
  370. 0 );
  371. parser->private_len = len;
  372. /* put a safeguard */
  373. parser->private_dict[len] = '\0';
  374. }
  375. else
  376. /* binary encoding -- copy the private dict */
  377. FT_MEM_MOVE( parser->private_dict, cur, size );
  378. }
  379. /* we now decrypt the encoded binary private dictionary */
  380. psaux->t1_decrypt( parser->private_dict, parser->private_len, 55665U );
  381. if ( parser->private_len < 4 )
  382. {
  383. FT_ERROR(( "T1_Get_Private_Dict:"
  384. " invalid private dictionary section\n" ));
  385. error = T1_Err_Invalid_File_Format;
  386. goto Fail;
  387. }
  388. /* replace the four random bytes at the beginning with whitespace */
  389. parser->private_dict[0] = ' ';
  390. parser->private_dict[1] = ' ';
  391. parser->private_dict[2] = ' ';
  392. parser->private_dict[3] = ' ';
  393. parser->root.base = parser->private_dict;
  394. parser->root.cursor = parser->private_dict;
  395. parser->root.limit = parser->root.cursor + parser->private_len;
  396. Fail:
  397. Exit:
  398. return error;
  399. }
  400. /* END */