Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / modules / freetype2 / src / cff / cffparse.c
blobd6d77dd08da329bd15a86fb9980b659db2d221fb
1 /***************************************************************************/
2 /* */
3 /* cffparse.c */
4 /* */
5 /* CFF token stream parser (body) */
6 /* */
7 /* Copyright 1996-2001, 2002, 2003, 2004, 2007, 2008 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 /***************************************************************************/
19 #include <ft2build.h>
20 #include "cffparse.h"
21 #include FT_INTERNAL_STREAM_H
22 #include FT_INTERNAL_DEBUG_H
24 #include "cfferrs.h"
27 /*************************************************************************/
28 /* */
29 /* The macro FT_COMPONENT is used in trace mode. It is an implicit */
30 /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log */
31 /* messages during execution. */
32 /* */
33 #undef FT_COMPONENT
34 #define FT_COMPONENT trace_cffparse
37 enum
39 cff_kind_none = 0,
40 cff_kind_num,
41 cff_kind_fixed,
42 cff_kind_fixed_thousand,
43 cff_kind_string,
44 cff_kind_bool,
45 cff_kind_delta,
46 cff_kind_callback,
48 cff_kind_max /* do not remove */
52 /* now generate handlers for the most simple fields */
53 typedef FT_Error (*CFF_Field_Reader)( CFF_Parser parser );
55 typedef struct CFF_Field_Handler_
57 int kind;
58 int code;
59 FT_UInt offset;
60 FT_Byte size;
61 CFF_Field_Reader reader;
62 FT_UInt array_max;
63 FT_UInt count_offset;
65 } CFF_Field_Handler;
68 FT_LOCAL_DEF( void )
69 cff_parser_init( CFF_Parser parser,
70 FT_UInt code,
71 void* object )
73 FT_MEM_ZERO( parser, sizeof ( *parser ) );
75 parser->top = parser->stack;
76 parser->object_code = code;
77 parser->object = object;
81 /* read an integer */
82 static FT_Long
83 cff_parse_integer( FT_Byte* start,
84 FT_Byte* limit )
86 FT_Byte* p = start;
87 FT_Int v = *p++;
88 FT_Long val = 0;
91 if ( v == 28 )
93 if ( p + 2 > limit )
94 goto Bad;
96 val = (FT_Short)( ( (FT_Int)p[0] << 8 ) | p[1] );
97 p += 2;
99 else if ( v == 29 )
101 if ( p + 4 > limit )
102 goto Bad;
104 val = ( (FT_Long)p[0] << 24 ) |
105 ( (FT_Long)p[1] << 16 ) |
106 ( (FT_Long)p[2] << 8 ) |
107 p[3];
108 p += 4;
110 else if ( v < 247 )
112 val = v - 139;
114 else if ( v < 251 )
116 if ( p + 1 > limit )
117 goto Bad;
119 val = ( v - 247 ) * 256 + p[0] + 108;
120 p++;
122 else
124 if ( p + 1 > limit )
125 goto Bad;
127 val = -( v - 251 ) * 256 - p[0] - 108;
128 p++;
131 Exit:
132 return val;
134 Bad:
135 val = 0;
136 goto Exit;
140 static const FT_Long power_tens[] =
143 10L,
144 100L,
145 1000L,
146 10000L,
147 100000L,
148 1000000L,
149 10000000L,
150 100000000L,
151 1000000000L
155 /* read a real */
156 static FT_Fixed
157 cff_parse_real( FT_Byte* start,
158 FT_Byte* limit,
159 FT_Int power_ten,
160 FT_Int* scaling )
162 FT_Byte* p = start;
163 FT_UInt nib;
164 FT_UInt phase;
166 FT_Long result, number, rest, exponent;
167 FT_Int sign = 0, exponent_sign = 0;
168 FT_Int exponent_add, integer_length, fraction_length;
171 if ( scaling )
172 *scaling = 0;
174 result = 0;
176 number = 0;
177 rest = 0;
178 exponent = 0;
180 exponent_add = 0;
181 integer_length = 0;
182 fraction_length = 0;
184 /* First of all, read the integer part. */
185 phase = 4;
187 for (;;)
189 /* If we entered this iteration with phase == 4, we need to */
190 /* read a new byte. This also skips past the initial 0x1E. */
191 if ( phase )
193 p++;
195 /* Make sure we don't read past the end. */
196 if ( p >= limit )
197 goto Exit;
200 /* Get the nibble. */
201 nib = ( p[0] >> phase ) & 0xF;
202 phase = 4 - phase;
204 if ( nib == 0xE )
205 sign = 1;
206 else if ( nib > 9 )
207 break;
208 else
210 /* Increase exponent if we can't add the digit. */
211 if ( number >= 0xCCCCCCCL )
212 exponent_add++;
213 /* Skip leading zeros. */
214 else if ( nib || number )
216 integer_length++;
217 number = number * 10 + nib;
222 /* Read fraction part, if any. */
223 if ( nib == 0xa )
224 for (;;)
226 /* If we entered this iteration with phase == 4, we need */
227 /* to read a new byte. */
228 if ( phase )
230 p++;
232 /* Make sure we don't read past the end. */
233 if ( p >= limit )
234 goto Exit;
237 /* Get the nibble. */
238 nib = ( p[0] >> phase ) & 0xF;
239 phase = 4 - phase;
240 if ( nib >= 10 )
241 break;
243 /* Skip leading zeros if possible. */
244 if ( !nib && !number )
245 exponent_add--;
246 /* Only add digit if we don't overflow. */
247 else if ( number < 0xCCCCCCCL )
249 fraction_length++;
250 number = number * 10 + nib;
254 /* Read exponent, if any. */
255 if ( nib == 12 )
257 exponent_sign = 1;
258 nib = 11;
261 if ( nib == 11 )
263 for (;;)
265 /* If we entered this iteration with phase == 4, */
266 /* we need to read a new byte. */
267 if ( phase )
269 p++;
271 /* Make sure we don't read past the end. */
272 if ( p >= limit )
273 goto Exit;
276 /* Get the nibble. */
277 nib = ( p[0] >> phase ) & 0xF;
278 phase = 4 - phase;
279 if ( nib >= 10 )
280 break;
282 exponent = exponent * 10 + nib;
284 /* Arbitrarily limit exponent. */
285 if ( exponent > 1000 )
286 goto Exit;
289 if ( exponent_sign )
290 exponent = -exponent;
293 /* We don't check `power_ten' and `exponent_add'. */
294 exponent += power_ten + exponent_add;
296 if ( scaling )
298 /* Only use `fraction_length'. */
299 fraction_length += integer_length;
300 exponent += integer_length;
302 if ( fraction_length <= 5 )
304 if ( number > 0x7FFFL )
306 result = FT_DivFix( number, 10 );
307 *scaling = exponent - fraction_length + 1;
309 else
311 if ( exponent > 0 )
313 FT_Int new_fraction_length, shift;
316 /* Make `scaling' as small as possible. */
317 new_fraction_length = FT_MIN( exponent, 5 );
318 exponent -= new_fraction_length;
319 shift = new_fraction_length - fraction_length;
321 number *= power_tens[shift];
322 if ( number > 0x7FFFL )
324 number /= 10;
325 exponent += 1;
328 else
329 exponent -= fraction_length;
331 result = number << 16;
332 *scaling = exponent;
335 else
337 if ( ( number / power_tens[fraction_length - 5] ) > 0x7FFFL )
339 result = FT_DivFix( number, power_tens[fraction_length - 4] );
340 *scaling = exponent - 4;
342 else
344 result = FT_DivFix( number, power_tens[fraction_length - 5] );
345 *scaling = exponent - 5;
349 else
351 integer_length += exponent;
352 fraction_length -= exponent;
354 /* Check for overflow and underflow. */
355 if ( FT_ABS( integer_length ) > 5 )
356 goto Exit;
358 /* Convert into 16.16 format. */
359 if ( fraction_length > 0 )
361 if ( ( number / power_tens[fraction_length] ) > 0x7FFFL )
362 goto Exit;
364 result = FT_DivFix( number, power_tens[fraction_length] );
366 else
368 number *= power_tens[-fraction_length];
370 if ( number > 0x7FFFL )
371 goto Exit;
373 result = number << 16;
377 if ( sign )
378 result = -result;
380 Exit:
381 return result;
385 /* read a number, either integer or real */
386 static FT_Long
387 cff_parse_num( FT_Byte** d )
389 return **d == 30 ? ( cff_parse_real( d[0], d[1], 0, NULL ) >> 16 )
390 : cff_parse_integer( d[0], d[1] );
394 /* read a floating point number, either integer or real */
395 static FT_Fixed
396 cff_parse_fixed( FT_Byte** d )
398 return **d == 30 ? cff_parse_real( d[0], d[1], 0, NULL )
399 : cff_parse_integer( d[0], d[1] ) << 16;
403 /* read a floating point number, either integer or real, */
404 /* but return `10^scaling' times the number read in */
405 static FT_Fixed
406 cff_parse_fixed_scaled( FT_Byte** d,
407 FT_Int scaling )
409 return **d ==
410 30 ? cff_parse_real( d[0], d[1], scaling, NULL )
411 : (FT_Fixed)FT_MulFix( cff_parse_integer( d[0], d[1] ) << 16,
412 power_tens[scaling] );
416 /* read a floating point number, either integer or real, */
417 /* and return it as precise as possible -- `scaling' returns */
418 /* the scaling factor (as a power of 10) */
419 static FT_Fixed
420 cff_parse_fixed_dynamic( FT_Byte** d,
421 FT_Int* scaling )
423 FT_ASSERT( scaling );
425 if ( **d == 30 )
426 return cff_parse_real( d[0], d[1], 0, scaling );
427 else
429 FT_Long number;
430 FT_Int integer_length;
433 number = cff_parse_integer( d[0], d[1] );
435 if ( number > 0x7FFFL )
437 for ( integer_length = 5; integer_length < 10; integer_length++ )
438 if ( number < power_tens[integer_length] )
439 break;
441 if ( ( number / power_tens[integer_length - 5] ) > 0x7FFFL )
443 *scaling = integer_length - 4;
444 return FT_DivFix( number, power_tens[integer_length - 4] );
446 else
448 *scaling = integer_length - 5;
449 return FT_DivFix( number, power_tens[integer_length - 5] );
452 else
454 *scaling = 0;
455 return number << 16;
461 static FT_Error
462 cff_parse_font_matrix( CFF_Parser parser )
464 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
465 FT_Matrix* matrix = &dict->font_matrix;
466 FT_Vector* offset = &dict->font_offset;
467 FT_ULong* upm = &dict->units_per_em;
468 FT_Byte** data = parser->stack;
469 FT_Error error = CFF_Err_Stack_Underflow;
472 if ( parser->top >= parser->stack + 6 )
474 FT_Int scaling;
477 error = CFF_Err_Ok;
479 /* We expect a well-formed font matrix, this is, the matrix elements */
480 /* `xx' and `yy' are of approximately the same magnitude. To avoid */
481 /* loss of precision, we use the magnitude of element `xx' to scale */
482 /* all other elements. The scaling factor is then contained in the */
483 /* `units_per_em' value. */
485 matrix->xx = cff_parse_fixed_dynamic( data++, &scaling );
487 scaling = -scaling;
489 if ( scaling < 0 || scaling > 9 )
491 /* Return default matrix in case of unlikely values. */
492 matrix->xx = 0x10000L;
493 matrix->yx = 0;
494 matrix->yx = 0;
495 matrix->yy = 0x10000L;
496 offset->x = 0;
497 offset->y = 0;
498 *upm = 1;
500 goto Exit;
503 matrix->yx = cff_parse_fixed_scaled( data++, scaling );
504 matrix->xy = cff_parse_fixed_scaled( data++, scaling );
505 matrix->yy = cff_parse_fixed_scaled( data++, scaling );
506 offset->x = cff_parse_fixed_scaled( data++, scaling );
507 offset->y = cff_parse_fixed_scaled( data, scaling );
509 *upm = power_tens[scaling];
512 Exit:
513 return error;
517 static FT_Error
518 cff_parse_font_bbox( CFF_Parser parser )
520 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
521 FT_BBox* bbox = &dict->font_bbox;
522 FT_Byte** data = parser->stack;
523 FT_Error error;
526 error = CFF_Err_Stack_Underflow;
528 if ( parser->top >= parser->stack + 4 )
530 bbox->xMin = FT_RoundFix( cff_parse_fixed( data++ ) );
531 bbox->yMin = FT_RoundFix( cff_parse_fixed( data++ ) );
532 bbox->xMax = FT_RoundFix( cff_parse_fixed( data++ ) );
533 bbox->yMax = FT_RoundFix( cff_parse_fixed( data ) );
534 error = CFF_Err_Ok;
537 return error;
541 static FT_Error
542 cff_parse_private_dict( CFF_Parser parser )
544 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
545 FT_Byte** data = parser->stack;
546 FT_Error error;
549 error = CFF_Err_Stack_Underflow;
551 if ( parser->top >= parser->stack + 2 )
553 dict->private_size = cff_parse_num( data++ );
554 dict->private_offset = cff_parse_num( data );
555 error = CFF_Err_Ok;
558 return error;
562 static FT_Error
563 cff_parse_cid_ros( CFF_Parser parser )
565 CFF_FontRecDict dict = (CFF_FontRecDict)parser->object;
566 FT_Byte** data = parser->stack;
567 FT_Error error;
570 error = CFF_Err_Stack_Underflow;
572 if ( parser->top >= parser->stack + 3 )
574 dict->cid_registry = (FT_UInt)cff_parse_num ( data++ );
575 dict->cid_ordering = (FT_UInt)cff_parse_num ( data++ );
576 dict->cid_supplement = (FT_ULong)cff_parse_num( data );
577 error = CFF_Err_Ok;
580 return error;
584 #define CFF_FIELD_NUM( code, name ) \
585 CFF_FIELD( code, name, cff_kind_num )
586 #define CFF_FIELD_FIXED( code, name ) \
587 CFF_FIELD( code, name, cff_kind_fixed )
588 #define CFF_FIELD_FIXED_1000( code, name ) \
589 CFF_FIELD( code, name, cff_kind_fixed_thousand )
590 #define CFF_FIELD_STRING( code, name ) \
591 CFF_FIELD( code, name, cff_kind_string )
592 #define CFF_FIELD_BOOL( code, name ) \
593 CFF_FIELD( code, name, cff_kind_bool )
594 #define CFF_FIELD_DELTA( code, name, max ) \
595 CFF_FIELD( code, name, cff_kind_delta )
597 #define CFF_FIELD_CALLBACK( code, name ) \
599 cff_kind_callback, \
600 code | CFFCODE, \
601 0, 0, \
602 cff_parse_ ## name, \
603 0, 0 \
606 #undef CFF_FIELD
607 #define CFF_FIELD( code, name, kind ) \
609 kind, \
610 code | CFFCODE, \
611 FT_FIELD_OFFSET( name ), \
612 FT_FIELD_SIZE( name ), \
613 0, 0, 0 \
616 #undef CFF_FIELD_DELTA
617 #define CFF_FIELD_DELTA( code, name, max ) \
619 cff_kind_delta, \
620 code | CFFCODE, \
621 FT_FIELD_OFFSET( name ), \
622 FT_FIELD_SIZE_DELTA( name ), \
623 0, \
624 max, \
625 FT_FIELD_OFFSET( num_ ## name ) \
628 #define CFFCODE_TOPDICT 0x1000
629 #define CFFCODE_PRIVATE 0x2000
631 static const CFF_Field_Handler cff_field_handlers[] =
634 #include "cfftoken.h"
636 { 0, 0, 0, 0, 0, 0, 0 }
640 FT_LOCAL_DEF( FT_Error )
641 cff_parser_run( CFF_Parser parser,
642 FT_Byte* start,
643 FT_Byte* limit )
645 FT_Byte* p = start;
646 FT_Error error = CFF_Err_Ok;
649 parser->top = parser->stack;
650 parser->start = start;
651 parser->limit = limit;
652 parser->cursor = start;
654 while ( p < limit )
656 FT_UInt v = *p;
659 if ( v >= 27 && v != 31 )
661 /* it's a number; we will push its position on the stack */
662 if ( parser->top - parser->stack >= CFF_MAX_STACK_DEPTH )
663 goto Stack_Overflow;
665 *parser->top ++ = p;
667 /* now, skip it */
668 if ( v == 30 )
670 /* skip real number */
671 p++;
672 for (;;)
674 if ( p >= limit )
675 goto Syntax_Error;
676 v = p[0] >> 4;
677 if ( v == 15 )
678 break;
679 v = p[0] & 0xF;
680 if ( v == 15 )
681 break;
682 p++;
685 else if ( v == 28 )
686 p += 2;
687 else if ( v == 29 )
688 p += 4;
689 else if ( v > 246 )
690 p += 1;
692 else
694 /* This is not a number, hence it's an operator. Compute its code */
695 /* and look for it in our current list. */
697 FT_UInt code;
698 FT_UInt num_args = (FT_UInt)
699 ( parser->top - parser->stack );
700 const CFF_Field_Handler* field;
703 *parser->top = p;
704 code = v;
705 if ( v == 12 )
707 /* two byte operator */
708 p++;
709 if ( p >= limit )
710 goto Syntax_Error;
712 code = 0x100 | p[0];
714 code = code | parser->object_code;
716 for ( field = cff_field_handlers; field->kind; field++ )
718 if ( field->code == (FT_Int)code )
720 /* we found our field's handler; read it */
721 FT_Long val;
722 FT_Byte* q = (FT_Byte*)parser->object + field->offset;
725 /* check that we have enough arguments -- except for */
726 /* delta encoded arrays, which can be empty */
727 if ( field->kind != cff_kind_delta && num_args < 1 )
728 goto Stack_Underflow;
730 switch ( field->kind )
732 case cff_kind_bool:
733 case cff_kind_string:
734 case cff_kind_num:
735 val = cff_parse_num( parser->stack );
736 goto Store_Number;
738 case cff_kind_fixed:
739 val = cff_parse_fixed( parser->stack );
740 goto Store_Number;
742 case cff_kind_fixed_thousand:
743 val = cff_parse_fixed_scaled( parser->stack, 3 );
745 Store_Number:
746 switch ( field->size )
748 case (8 / FT_CHAR_BIT):
749 *(FT_Byte*)q = (FT_Byte)val;
750 break;
752 case (16 / FT_CHAR_BIT):
753 *(FT_Short*)q = (FT_Short)val;
754 break;
756 case (32 / FT_CHAR_BIT):
757 *(FT_Int32*)q = (FT_Int)val;
758 break;
760 default: /* for 64-bit systems */
761 *(FT_Long*)q = val;
763 break;
765 case cff_kind_delta:
767 FT_Byte* qcount = (FT_Byte*)parser->object +
768 field->count_offset;
770 FT_Byte** data = parser->stack;
773 if ( num_args > field->array_max )
774 num_args = field->array_max;
776 /* store count */
777 *qcount = (FT_Byte)num_args;
779 val = 0;
780 while ( num_args > 0 )
782 val += cff_parse_num( data++ );
783 switch ( field->size )
785 case (8 / FT_CHAR_BIT):
786 *(FT_Byte*)q = (FT_Byte)val;
787 break;
789 case (16 / FT_CHAR_BIT):
790 *(FT_Short*)q = (FT_Short)val;
791 break;
793 case (32 / FT_CHAR_BIT):
794 *(FT_Int32*)q = (FT_Int)val;
795 break;
797 default: /* for 64-bit systems */
798 *(FT_Long*)q = val;
801 q += field->size;
802 num_args--;
805 break;
807 default: /* callback */
808 error = field->reader( parser );
809 if ( error )
810 goto Exit;
812 goto Found;
816 /* this is an unknown operator, or it is unsupported; */
817 /* we will ignore it for now. */
819 Found:
820 /* clear stack */
821 parser->top = parser->stack;
823 p++;
826 Exit:
827 return error;
829 Stack_Overflow:
830 error = CFF_Err_Invalid_Argument;
831 goto Exit;
833 Stack_Underflow:
834 error = CFF_Err_Invalid_Argument;
835 goto Exit;
837 Syntax_Error:
838 error = CFF_Err_Invalid_Argument;
839 goto Exit;
843 /* END */