PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/freetype2/src/cff/cffparse.c

https://bitbucket.org/soko/mozilla-central
C | 1112 lines | 801 code | 231 blank | 80 comment | 117 complexity | 64e2113637bcf711232cd640d5fc8153 MD5 | raw file
Possible License(s): GPL-2.0, JSON, 0BSD, LGPL-3.0, AGPL-1.0, MIT, MPL-2.0-no-copyleft-exception, BSD-3-Clause, LGPL-2.1, Apache-2.0
  1. /***************************************************************************/
  2. /* */
  3. /* cffparse.c */
  4. /* */
  5. /* CFF token stream parser (body) */
  6. /* */
  7. /* Copyright 1996-2004, 2007-2011 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. #include <ft2build.h>
  18. #include "cffparse.h"
  19. #include FT_INTERNAL_STREAM_H
  20. #include FT_INTERNAL_DEBUG_H
  21. #include "cfferrs.h"
  22. #include "cffpic.h"
  23. /*************************************************************************/
  24. /* */
  25. /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
  26. /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
  27. /* messages during execution. */
  28. /* */
  29. #undef FT_COMPONENT
  30. #define FT_COMPONENT trace_cffparse
  31. FT_LOCAL_DEF( void )
  32. cff_parser_init( CFF_Parser parser,
  33. FT_UInt code,
  34. void* object,
  35. FT_Library library)
  36. {
  37. FT_MEM_ZERO( parser, sizeof ( *parser ) );
  38. parser->top = parser->stack;
  39. parser->object_code = code;
  40. parser->object = object;
  41. parser->library = library;
  42. }
  43. /* read an integer */
  44. static FT_Long
  45. cff_parse_integer( FT_Byte* start,
  46. FT_Byte* limit )
  47. {
  48. FT_Byte* p = start;
  49. FT_Int v = *p++;
  50. FT_Long val = 0;
  51. if ( v == 28 )
  52. {
  53. if ( p + 2 > limit )
  54. goto Bad;
  55. val = (FT_Short)( ( (FT_Int)p[0] << 8 ) | p[1] );
  56. p += 2;
  57. }
  58. else if ( v == 29 )
  59. {
  60. if ( p + 4 > limit )
  61. goto Bad;
  62. val = ( (FT_Long)p[0] << 24 ) |
  63. ( (FT_Long)p[1] << 16 ) |
  64. ( (FT_Long)p[2] << 8 ) |
  65. p[3];
  66. p += 4;
  67. }
  68. else if ( v < 247 )
  69. {
  70. val = v - 139;
  71. }
  72. else if ( v < 251 )
  73. {
  74. if ( p + 1 > limit )
  75. goto Bad;
  76. val = ( v - 247 ) * 256 + p[0] + 108;
  77. p++;
  78. }
  79. else
  80. {
  81. if ( p + 1 > limit )
  82. goto Bad;
  83. val = -( v - 251 ) * 256 - p[0] - 108;
  84. p++;
  85. }
  86. Exit:
  87. return val;
  88. Bad:
  89. val = 0;
  90. goto Exit;
  91. }
  92. static const FT_Long power_tens[] =
  93. {
  94. 1L,
  95. 10L,
  96. 100L,
  97. 1000L,
  98. 10000L,
  99. 100000L,
  100. 1000000L,
  101. 10000000L,
  102. 100000000L,
  103. 1000000000L
  104. };
  105. /* read a real */
  106. static FT_Fixed
  107. cff_parse_real( FT_Byte* start,
  108. FT_Byte* limit,
  109. FT_Long power_ten,
  110. FT_Long* scaling )
  111. {
  112. FT_Byte* p = start;
  113. FT_UInt nib;
  114. FT_UInt phase;
  115. FT_Long result, number, exponent;
  116. FT_Int sign = 0, exponent_sign = 0;
  117. FT_Long exponent_add, integer_length, fraction_length;
  118. if ( scaling )
  119. *scaling = 0;
  120. result = 0;
  121. number = 0;
  122. exponent = 0;
  123. exponent_add = 0;
  124. integer_length = 0;
  125. fraction_length = 0;
  126. /* First of all, read the integer part. */
  127. phase = 4;
  128. for (;;)
  129. {
  130. /* If we entered this iteration with phase == 4, we need to */
  131. /* read a new byte. This also skips past the initial 0x1E. */
  132. if ( phase )
  133. {
  134. p++;
  135. /* Make sure we don't read past the end. */
  136. if ( p >= limit )
  137. goto Exit;
  138. }
  139. /* Get the nibble. */
  140. nib = ( p[0] >> phase ) & 0xF;
  141. phase = 4 - phase;
  142. if ( nib == 0xE )
  143. sign = 1;
  144. else if ( nib > 9 )
  145. break;
  146. else
  147. {
  148. /* Increase exponent if we can't add the digit. */
  149. if ( number >= 0xCCCCCCCL )
  150. exponent_add++;
  151. /* Skip leading zeros. */
  152. else if ( nib || number )
  153. {
  154. integer_length++;
  155. number = number * 10 + nib;
  156. }
  157. }
  158. }
  159. /* Read fraction part, if any. */
  160. if ( nib == 0xa )
  161. for (;;)
  162. {
  163. /* If we entered this iteration with phase == 4, we need */
  164. /* to read a new byte. */
  165. if ( phase )
  166. {
  167. p++;
  168. /* Make sure we don't read past the end. */
  169. if ( p >= limit )
  170. goto Exit;
  171. }
  172. /* Get the nibble. */
  173. nib = ( p[0] >> phase ) & 0xF;
  174. phase = 4 - phase;
  175. if ( nib >= 10 )
  176. break;
  177. /* Skip leading zeros if possible. */
  178. if ( !nib && !number )
  179. exponent_add--;
  180. /* Only add digit if we don't overflow. */
  181. else if ( number < 0xCCCCCCCL && fraction_length < 9 )
  182. {
  183. fraction_length++;
  184. number = number * 10 + nib;
  185. }
  186. }
  187. /* Read exponent, if any. */
  188. if ( nib == 12 )
  189. {
  190. exponent_sign = 1;
  191. nib = 11;
  192. }
  193. if ( nib == 11 )
  194. {
  195. for (;;)
  196. {
  197. /* If we entered this iteration with phase == 4, */
  198. /* we need to read a new byte. */
  199. if ( phase )
  200. {
  201. p++;
  202. /* Make sure we don't read past the end. */
  203. if ( p >= limit )
  204. goto Exit;
  205. }
  206. /* Get the nibble. */
  207. nib = ( p[0] >> phase ) & 0xF;
  208. phase = 4 - phase;
  209. if ( nib >= 10 )
  210. break;
  211. exponent = exponent * 10 + nib;
  212. /* Arbitrarily limit exponent. */
  213. if ( exponent > 1000 )
  214. goto Exit;
  215. }
  216. if ( exponent_sign )
  217. exponent = -exponent;
  218. }
  219. /* We don't check `power_ten' and `exponent_add'. */
  220. exponent += power_ten + exponent_add;
  221. if ( scaling )
  222. {
  223. /* Only use `fraction_length'. */
  224. fraction_length += integer_length;
  225. exponent += integer_length;
  226. if ( fraction_length <= 5 )
  227. {
  228. if ( number > 0x7FFFL )
  229. {
  230. result = FT_DivFix( number, 10 );
  231. *scaling = exponent - fraction_length + 1;
  232. }
  233. else
  234. {
  235. if ( exponent > 0 )
  236. {
  237. FT_Long new_fraction_length, shift;
  238. /* Make `scaling' as small as possible. */
  239. new_fraction_length = FT_MIN( exponent, 5 );
  240. exponent -= new_fraction_length;
  241. shift = new_fraction_length - fraction_length;
  242. number *= power_tens[shift];
  243. if ( number > 0x7FFFL )
  244. {
  245. number /= 10;
  246. exponent += 1;
  247. }
  248. }
  249. else
  250. exponent -= fraction_length;
  251. result = number << 16;
  252. *scaling = exponent;
  253. }
  254. }
  255. else
  256. {
  257. if ( ( number / power_tens[fraction_length - 5] ) > 0x7FFFL )
  258. {
  259. result = FT_DivFix( number, power_tens[fraction_length - 4] );
  260. *scaling = exponent - 4;
  261. }
  262. else
  263. {
  264. result = FT_DivFix( number, power_tens[fraction_length - 5] );
  265. *scaling = exponent - 5;
  266. }
  267. }
  268. }
  269. else
  270. {
  271. integer_length += exponent;
  272. fraction_length -= exponent;
  273. /* Check for overflow and underflow. */
  274. if ( FT_ABS( integer_length ) > 5 )
  275. goto Exit;
  276. /* Remove non-significant digits. */
  277. if ( integer_length < 0 )
  278. {
  279. number /= power_tens[-integer_length];
  280. fraction_length += integer_length;
  281. }
  282. /* this can only happen if exponent was non-zero */
  283. if ( fraction_length == 10 )
  284. {
  285. number /= 10;
  286. fraction_length -= 1;
  287. }
  288. /* Convert into 16.16 format. */
  289. if ( fraction_length > 0 )
  290. {
  291. if ( ( number / power_tens[fraction_length] ) > 0x7FFFL )
  292. goto Exit;
  293. result = FT_DivFix( number, power_tens[fraction_length] );
  294. }
  295. else
  296. {
  297. number *= power_tens[-fraction_length];
  298. if ( number > 0x7FFFL )
  299. goto Exit;
  300. result = number << 16;
  301. }
  302. }
  303. if ( sign )
  304. result = -result;
  305. Exit:
  306. return result;
  307. }
  308. /* read a number, either integer or real */
  309. static FT_Long
  310. cff_parse_num( FT_Byte** d )
  311. {
  312. return **d == 30 ? ( cff_parse_real( d[0], d[1], 0, NULL ) >> 16 )
  313. : cff_parse_integer( d[0], d[1] );
  314. }
  315. /* read a floating point number, either integer or real */
  316. static FT_Fixed
  317. cff_parse_fixed( FT_Byte** d )
  318. {
  319. return **d == 30 ? cff_parse_real( d[0], d[1], 0, NULL )
  320. : cff_parse_integer( d[0], d[1] ) << 16;
  321. }
  322. /* read a floating point number, either integer or real, */
  323. /* but return `10^scaling' times the number read in */
  324. static FT_Fixed
  325. cff_parse_fixed_scaled( FT_Byte** d,
  326. FT_Long scaling )
  327. {
  328. return **d == 30 ? cff_parse_real( d[0], d[1], scaling, NULL )
  329. : ( cff_parse_integer( d[0], d[1] ) *
  330. power_tens[scaling] ) << 16;
  331. }
  332. /* read a floating point number, either integer or real, */
  333. /* and return it as precise as possible -- `scaling' returns */
  334. /* the scaling factor (as a power of 10) */
  335. static FT_Fixed
  336. cff_parse_fixed_dynamic( FT_Byte** d,
  337. FT_Long* scaling )
  338. {
  339. FT_ASSERT( scaling );
  340. if ( **d == 30 )
  341. return cff_parse_real( d[0], d[1], 0, scaling );
  342. else
  343. {
  344. FT_Long number;
  345. FT_Int integer_length;
  346. number = cff_parse_integer( d[0], d[1] );
  347. if ( number > 0x7FFFL )
  348. {
  349. for ( integer_length = 5; integer_length < 10; integer_length++ )
  350. if ( number < power_tens[integer_length] )
  351. break;
  352. if ( ( number / power_tens[integer_length - 5] ) > 0x7FFFL )
  353. {
  354. *scaling = integer_length - 4;
  355. return FT_DivFix( number, power_tens[integer_length - 4] );
  356. }
  357. else
  358. {
  359. *scaling = integer_length - 5;
  360. return FT_DivFix( number, power_tens[integer_length - 5] );
  361. }
  362. }
  363. else
  364. {
  365. *scaling = 0;
  366. return number << 16;
  367. }
  368. }
  369. }
  370. static FT_Error
  371. cff_parse_font_matrix( CFF_Parser parser )
  372. {
  373. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  374. FT_Matrix* matrix = &dict->font_matrix;
  375. FT_Vector* offset = &dict->font_offset;
  376. FT_ULong* upm = &dict->units_per_em;
  377. FT_Byte** data = parser->stack;
  378. FT_Error error = CFF_Err_Stack_Underflow;
  379. if ( parser->top >= parser->stack + 6 )
  380. {
  381. FT_Long scaling;
  382. error = CFF_Err_Ok;
  383. dict->has_font_matrix = TRUE;
  384. /* We expect a well-formed font matrix, this is, the matrix elements */
  385. /* `xx' and `yy' are of approximately the same magnitude. To avoid */
  386. /* loss of precision, we use the magnitude of element `xx' to scale */
  387. /* all other elements. The scaling factor is then contained in the */
  388. /* `units_per_em' value. */
  389. matrix->xx = cff_parse_fixed_dynamic( data++, &scaling );
  390. scaling = -scaling;
  391. if ( scaling < 0 || scaling > 9 )
  392. {
  393. /* Return default matrix in case of unlikely values. */
  394. FT_TRACE1(( "cff_parse_font_matrix:"
  395. " strange scaling value for xx element (%d),\n"
  396. " "
  397. " using default matrix\n", scaling ));
  398. matrix->xx = 0x10000L;
  399. matrix->yx = 0;
  400. matrix->xy = 0;
  401. matrix->yy = 0x10000L;
  402. offset->x = 0;
  403. offset->y = 0;
  404. *upm = 1;
  405. goto Exit;
  406. }
  407. matrix->yx = cff_parse_fixed_scaled( data++, scaling );
  408. matrix->xy = cff_parse_fixed_scaled( data++, scaling );
  409. matrix->yy = cff_parse_fixed_scaled( data++, scaling );
  410. offset->x = cff_parse_fixed_scaled( data++, scaling );
  411. offset->y = cff_parse_fixed_scaled( data, scaling );
  412. *upm = power_tens[scaling];
  413. FT_TRACE4(( " [%f %f %f %f %f %f]\n",
  414. (double)matrix->xx / *upm / 65536,
  415. (double)matrix->xy / *upm / 65536,
  416. (double)matrix->yx / *upm / 65536,
  417. (double)matrix->yy / *upm / 65536,
  418. (double)offset->x / *upm / 65536,
  419. (double)offset->y / *upm / 65536 ));
  420. }
  421. Exit:
  422. return error;
  423. }
  424. static FT_Error
  425. cff_parse_font_bbox( CFF_Parser parser )
  426. {
  427. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  428. FT_BBox* bbox = &dict->font_bbox;
  429. FT_Byte** data = parser->stack;
  430. FT_Error error;
  431. error = CFF_Err_Stack_Underflow;
  432. if ( parser->top >= parser->stack + 4 )
  433. {
  434. bbox->xMin = FT_RoundFix( cff_parse_fixed( data++ ) );
  435. bbox->yMin = FT_RoundFix( cff_parse_fixed( data++ ) );
  436. bbox->xMax = FT_RoundFix( cff_parse_fixed( data++ ) );
  437. bbox->yMax = FT_RoundFix( cff_parse_fixed( data ) );
  438. error = CFF_Err_Ok;
  439. FT_TRACE4(( " [%d %d %d %d]\n",
  440. bbox->xMin / 65536,
  441. bbox->yMin / 65536,
  442. bbox->xMax / 65536,
  443. bbox->yMax / 65536 ));
  444. }
  445. return error;
  446. }
  447. static FT_Error
  448. cff_parse_private_dict( CFF_Parser parser )
  449. {
  450. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  451. FT_Byte** data = parser->stack;
  452. FT_Error error;
  453. error = CFF_Err_Stack_Underflow;
  454. if ( parser->top >= parser->stack + 2 )
  455. {
  456. dict->private_size = cff_parse_num( data++ );
  457. dict->private_offset = cff_parse_num( data );
  458. FT_TRACE4(( " %lu %lu\n",
  459. dict->private_size, dict->private_offset ));
  460. error = CFF_Err_Ok;
  461. }
  462. return error;
  463. }
  464. static FT_Error
  465. cff_parse_cid_ros( CFF_Parser parser )
  466. {
  467. CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
  468. FT_Byte** data = parser->stack;
  469. FT_Error error;
  470. error = CFF_Err_Stack_Underflow;
  471. if ( parser->top >= parser->stack + 3 )
  472. {
  473. dict->cid_registry = (FT_UInt)cff_parse_num( data++ );
  474. dict->cid_ordering = (FT_UInt)cff_parse_num( data++ );
  475. if ( **data == 30 )
  476. FT_TRACE1(( "cff_parse_cid_ros: real supplement is rounded\n" ));
  477. dict->cid_supplement = cff_parse_num( data );
  478. if ( dict->cid_supplement < 0 )
  479. FT_TRACE1(( "cff_parse_cid_ros: negative supplement %d is found\n",
  480. dict->cid_supplement ));
  481. error = CFF_Err_Ok;
  482. FT_TRACE4(( " %d %d %d\n",
  483. dict->cid_registry,
  484. dict->cid_ordering,
  485. dict->cid_supplement ));
  486. }
  487. return error;
  488. }
  489. #define CFF_FIELD_NUM( code, name, id ) \
  490. CFF_FIELD( code, name, id, cff_kind_num )
  491. #define CFF_FIELD_FIXED( code, name, id ) \
  492. CFF_FIELD( code, name, id, cff_kind_fixed )
  493. #define CFF_FIELD_FIXED_1000( code, name, id ) \
  494. CFF_FIELD( code, name, id, cff_kind_fixed_thousand )
  495. #define CFF_FIELD_STRING( code, name, id ) \
  496. CFF_FIELD( code, name, id, cff_kind_string )
  497. #define CFF_FIELD_BOOL( code, name, id ) \
  498. CFF_FIELD( code, name, id, cff_kind_bool )
  499. #define CFFCODE_TOPDICT 0x1000
  500. #define CFFCODE_PRIVATE 0x2000
  501. #ifndef FT_CONFIG_OPTION_PIC
  502. #undef CFF_FIELD
  503. #undef CFF_FIELD_DELTA
  504. #ifndef FT_DEBUG_LEVEL_TRACE
  505. #define CFF_FIELD_CALLBACK( code, name, id ) \
  506. { \
  507. cff_kind_callback, \
  508. code | CFFCODE, \
  509. 0, 0, \
  510. cff_parse_ ## name, \
  511. 0, 0 \
  512. },
  513. #define CFF_FIELD( code, name, id, kind ) \
  514. { \
  515. kind, \
  516. code | CFFCODE, \
  517. FT_FIELD_OFFSET( name ), \
  518. FT_FIELD_SIZE( name ), \
  519. 0, 0, 0 \
  520. },
  521. #define CFF_FIELD_DELTA( code, name, max, id ) \
  522. { \
  523. cff_kind_delta, \
  524. code | CFFCODE, \
  525. FT_FIELD_OFFSET( name ), \
  526. FT_FIELD_SIZE_DELTA( name ), \
  527. 0, \
  528. max, \
  529. FT_FIELD_OFFSET( num_ ## name ) \
  530. },
  531. static const CFF_Field_Handler cff_field_handlers[] =
  532. {
  533. #include "cfftoken.h"
  534. { 0, 0, 0, 0, 0, 0, 0 }
  535. };
  536. #else /* FT_DEBUG_LEVEL_TRACE */
  537. #define CFF_FIELD_CALLBACK( code, name, id ) \
  538. { \
  539. cff_kind_callback, \
  540. code | CFFCODE, \
  541. 0, 0, \
  542. cff_parse_ ## name, \
  543. 0, 0, \
  544. id \
  545. },
  546. #define CFF_FIELD( code, name, id, kind ) \
  547. { \
  548. kind, \
  549. code | CFFCODE, \
  550. FT_FIELD_OFFSET( name ), \
  551. FT_FIELD_SIZE( name ), \
  552. 0, 0, 0, \
  553. id \
  554. },
  555. #define CFF_FIELD_DELTA( code, name, max, id ) \
  556. { \
  557. cff_kind_delta, \
  558. code | CFFCODE, \
  559. FT_FIELD_OFFSET( name ), \
  560. FT_FIELD_SIZE_DELTA( name ), \
  561. 0, \
  562. max, \
  563. FT_FIELD_OFFSET( num_ ## name ), \
  564. id \
  565. },
  566. static const CFF_Field_Handler cff_field_handlers[] =
  567. {
  568. #include "cfftoken.h"
  569. { 0, 0, 0, 0, 0, 0, 0, 0 }
  570. };
  571. #endif /* FT_DEBUG_LEVEL_TRACE */
  572. #else /* FT_CONFIG_OPTION_PIC */
  573. void
  574. FT_Destroy_Class_cff_field_handlers( FT_Library library,
  575. CFF_Field_Handler* clazz )
  576. {
  577. FT_Memory memory = library->memory;
  578. if ( clazz )
  579. FT_FREE( clazz );
  580. }
  581. FT_Error
  582. FT_Create_Class_cff_field_handlers( FT_Library library,
  583. CFF_Field_Handler** output_class )
  584. {
  585. CFF_Field_Handler* clazz;
  586. FT_Error error;
  587. FT_Memory memory = library->memory;
  588. int i = 0;
  589. #undef CFF_FIELD
  590. #define CFF_FIELD( code, name, id, kind ) i++;
  591. #undef CFF_FIELD_DELTA
  592. #define CFF_FIELD_DELTA( code, name, max, id ) i++;
  593. #undef CFF_FIELD_CALLBACK
  594. #define CFF_FIELD_CALLBACK( code, name, id ) i++;
  595. #include "cfftoken.h"
  596. i++; /* { 0, 0, 0, 0, 0, 0, 0 } */
  597. if ( FT_ALLOC( clazz, sizeof ( CFF_Field_Handler ) * i ) )
  598. return error;
  599. i = 0;
  600. #ifndef FT_DEBUG_LEVEL_TRACE
  601. #undef CFF_FIELD_CALLBACK
  602. #define CFF_FIELD_CALLBACK( code_, name_, id_ ) \
  603. clazz[i].kind = cff_kind_callback; \
  604. clazz[i].code = code_ | CFFCODE; \
  605. clazz[i].offset = 0; \
  606. clazz[i].size = 0; \
  607. clazz[i].reader = cff_parse_ ## name_; \
  608. clazz[i].array_max = 0; \
  609. clazz[i].count_offset = 0; \
  610. i++;
  611. #undef CFF_FIELD
  612. #define CFF_FIELD( code_, name_, id_, kind_ ) \
  613. clazz[i].kind = kind_; \
  614. clazz[i].code = code_ | CFFCODE; \
  615. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  616. clazz[i].size = FT_FIELD_SIZE( name_ ); \
  617. clazz[i].reader = 0; \
  618. clazz[i].array_max = 0; \
  619. clazz[i].count_offset = 0; \
  620. i++; \
  621. #undef CFF_FIELD_DELTA
  622. #define CFF_FIELD_DELTA( code_, name_, max_, id_ ) \
  623. clazz[i].kind = cff_kind_delta; \
  624. clazz[i].code = code_ | CFFCODE; \
  625. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  626. clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
  627. clazz[i].reader = 0; \
  628. clazz[i].array_max = max_; \
  629. clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
  630. i++;
  631. #include "cfftoken.h"
  632. clazz[i].kind = 0;
  633. clazz[i].code = 0;
  634. clazz[i].offset = 0;
  635. clazz[i].size = 0;
  636. clazz[i].reader = 0;
  637. clazz[i].array_max = 0;
  638. clazz[i].count_offset = 0;
  639. #else /* FT_DEBUG_LEVEL_TRACE */
  640. #undef CFF_FIELD_CALLBACK
  641. #define CFF_FIELD_CALLBACK( code_, name_, id_ ) \
  642. clazz[i].kind = cff_kind_callback; \
  643. clazz[i].code = code_ | CFFCODE; \
  644. clazz[i].offset = 0; \
  645. clazz[i].size = 0; \
  646. clazz[i].reader = cff_parse_ ## name_; \
  647. clazz[i].array_max = 0; \
  648. clazz[i].count_offset = 0; \
  649. clazz[i].id = id_; \
  650. i++;
  651. #undef CFF_FIELD
  652. #define CFF_FIELD( code_, name_, id_, kind_ ) \
  653. clazz[i].kind = kind_; \
  654. clazz[i].code = code_ | CFFCODE; \
  655. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  656. clazz[i].size = FT_FIELD_SIZE( name_ ); \
  657. clazz[i].reader = 0; \
  658. clazz[i].array_max = 0; \
  659. clazz[i].count_offset = 0; \
  660. clazz[i].id = id_; \
  661. i++; \
  662. #undef CFF_FIELD_DELTA
  663. #define CFF_FIELD_DELTA( code_, name_, max_, id_ ) \
  664. clazz[i].kind = cff_kind_delta; \
  665. clazz[i].code = code_ | CFFCODE; \
  666. clazz[i].offset = FT_FIELD_OFFSET( name_ ); \
  667. clazz[i].size = FT_FIELD_SIZE_DELTA( name_ ); \
  668. clazz[i].reader = 0; \
  669. clazz[i].array_max = max_; \
  670. clazz[i].count_offset = FT_FIELD_OFFSET( num_ ## name_ ); \
  671. clazz[i].id = id_; \
  672. i++;
  673. #include "cfftoken.h"
  674. clazz[i].kind = 0;
  675. clazz[i].code = 0;
  676. clazz[i].offset = 0;
  677. clazz[i].size = 0;
  678. clazz[i].reader = 0;
  679. clazz[i].array_max = 0;
  680. clazz[i].count_offset = 0;
  681. clazz[i].id = 0;
  682. #endif /* FT_DEBUG_LEVEL_TRACE */
  683. *output_class = clazz;
  684. return CFF_Err_Ok;
  685. }
  686. #endif /* FT_CONFIG_OPTION_PIC */
  687. FT_LOCAL_DEF( FT_Error )
  688. cff_parser_run( CFF_Parser parser,
  689. FT_Byte* start,
  690. FT_Byte* limit )
  691. {
  692. FT_Byte* p = start;
  693. FT_Error error = CFF_Err_Ok;
  694. FT_Library library = parser->library;
  695. FT_UNUSED( library );
  696. parser->top = parser->stack;
  697. parser->start = start;
  698. parser->limit = limit;
  699. parser->cursor = start;
  700. while ( p < limit )
  701. {
  702. FT_UInt v = *p;
  703. if ( v >= 27 && v != 31 )
  704. {
  705. /* it's a number; we will push its position on the stack */
  706. if ( parser->top - parser->stack >= CFF_MAX_STACK_DEPTH )
  707. goto Stack_Overflow;
  708. *parser->top ++ = p;
  709. /* now, skip it */
  710. if ( v == 30 )
  711. {
  712. /* skip real number */
  713. p++;
  714. for (;;)
  715. {
  716. /* An unterminated floating point number at the */
  717. /* end of a dictionary is invalid but harmless. */
  718. if ( p >= limit )
  719. goto Exit;
  720. v = p[0] >> 4;
  721. if ( v == 15 )
  722. break;
  723. v = p[0] & 0xF;
  724. if ( v == 15 )
  725. break;
  726. p++;
  727. }
  728. }
  729. else if ( v == 28 )
  730. p += 2;
  731. else if ( v == 29 )
  732. p += 4;
  733. else if ( v > 246 )
  734. p += 1;
  735. }
  736. else
  737. {
  738. /* This is not a number, hence it's an operator. Compute its code */
  739. /* and look for it in our current list. */
  740. FT_UInt code;
  741. FT_UInt num_args = (FT_UInt)
  742. ( parser->top - parser->stack );
  743. const CFF_Field_Handler* field;
  744. *parser->top = p;
  745. code = v;
  746. if ( v == 12 )
  747. {
  748. /* two byte operator */
  749. p++;
  750. if ( p >= limit )
  751. goto Syntax_Error;
  752. code = 0x100 | p[0];
  753. }
  754. code = code | parser->object_code;
  755. for ( field = FT_CFF_FIELD_HANDLERS_GET; field->kind; field++ )
  756. {
  757. if ( field->code == (FT_Int)code )
  758. {
  759. /* we found our field's handler; read it */
  760. FT_Long val;
  761. FT_Byte* q = (FT_Byte*)parser->object + field->offset;
  762. #ifdef FT_DEBUG_LEVEL_TRACE
  763. FT_TRACE4(( " %s", field->id ));
  764. #endif
  765. /* check that we have enough arguments -- except for */
  766. /* delta encoded arrays, which can be empty */
  767. if ( field->kind != cff_kind_delta && num_args < 1 )
  768. goto Stack_Underflow;
  769. switch ( field->kind )
  770. {
  771. case cff_kind_bool:
  772. case cff_kind_string:
  773. case cff_kind_num:
  774. val = cff_parse_num( parser->stack );
  775. goto Store_Number;
  776. case cff_kind_fixed:
  777. val = cff_parse_fixed( parser->stack );
  778. goto Store_Number;
  779. case cff_kind_fixed_thousand:
  780. val = cff_parse_fixed_scaled( parser->stack, 3 );
  781. Store_Number:
  782. switch ( field->size )
  783. {
  784. case (8 / FT_CHAR_BIT):
  785. *(FT_Byte*)q = (FT_Byte)val;
  786. break;
  787. case (16 / FT_CHAR_BIT):
  788. *(FT_Short*)q = (FT_Short)val;
  789. break;
  790. case (32 / FT_CHAR_BIT):
  791. *(FT_Int32*)q = (FT_Int)val;
  792. break;
  793. default: /* for 64-bit systems */
  794. *(FT_Long*)q = val;
  795. }
  796. #ifdef FT_DEBUG_LEVEL_TRACE
  797. switch ( field->kind )
  798. {
  799. case cff_kind_bool:
  800. FT_TRACE4(( " %s\n", val ? "true" : "false" ));
  801. break;
  802. case cff_kind_string:
  803. FT_TRACE4(( " %ld (SID)\n", val ));
  804. break;
  805. case cff_kind_num:
  806. FT_TRACE4(( " %ld\n", val ));
  807. break;
  808. case cff_kind_fixed:
  809. FT_TRACE4(( " %f\n", (double)val / 65536 ));
  810. break;
  811. case cff_kind_fixed_thousand:
  812. FT_TRACE4(( " %f\n", (double)val / 65536 / 1000 ));
  813. default:
  814. ; /* never reached */
  815. }
  816. #endif
  817. break;
  818. case cff_kind_delta:
  819. {
  820. FT_Byte* qcount = (FT_Byte*)parser->object +
  821. field->count_offset;
  822. FT_Byte** data = parser->stack;
  823. if ( num_args > field->array_max )
  824. num_args = field->array_max;
  825. FT_TRACE4(( " [" ));
  826. /* store count */
  827. *qcount = (FT_Byte)num_args;
  828. val = 0;
  829. while ( num_args > 0 )
  830. {
  831. val += cff_parse_num( data++ );
  832. switch ( field->size )
  833. {
  834. case (8 / FT_CHAR_BIT):
  835. *(FT_Byte*)q = (FT_Byte)val;
  836. break;
  837. case (16 / FT_CHAR_BIT):
  838. *(FT_Short*)q = (FT_Short)val;
  839. break;
  840. case (32 / FT_CHAR_BIT):
  841. *(FT_Int32*)q = (FT_Int)val;
  842. break;
  843. default: /* for 64-bit systems */
  844. *(FT_Long*)q = val;
  845. }
  846. FT_TRACE4(( " %ld", val ));
  847. q += field->size;
  848. num_args--;
  849. }
  850. FT_TRACE4(( "]\n" ));
  851. }
  852. break;
  853. default: /* callback */
  854. error = field->reader( parser );
  855. if ( error )
  856. goto Exit;
  857. }
  858. goto Found;
  859. }
  860. }
  861. /* this is an unknown operator, or it is unsupported; */
  862. /* we will ignore it for now. */
  863. Found:
  864. /* clear stack */
  865. parser->top = parser->stack;
  866. }
  867. p++;
  868. }
  869. Exit:
  870. return error;
  871. Stack_Overflow:
  872. error = CFF_Err_Invalid_Argument;
  873. goto Exit;
  874. Stack_Underflow:
  875. error = CFF_Err_Invalid_Argument;
  876. goto Exit;
  877. Syntax_Error:
  878. error = CFF_Err_Invalid_Argument;
  879. goto Exit;
  880. }
  881. /* END */