* src/bdf/bdflib.c: various improvements to the bdf loader, mainly used to reduce the amount of heap size required to only test wether we're dealing with a BDF font (the old code allocated 64 Kb right before any test). * src/lzw/ftlzw.c (FT_Stream_OpenLZW): modified the function to check the LZW header before doing anything else. This helps avoid un-necessary heap allocations (400 Kb of heap memory for the LZW decoder ! Oh my !) * src/gzip/ftgzip.c (FT_Stream_OpenGZip): ditto for the .gz decoder, though the code savings is smaller.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
diff --git a/ChangeLog b/ChangeLog
index de3a765..0095ce4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2005-03-15 David Turner <david@freetype.org>
+
+ * src/lzw/ftlzw.c (FT_Stream_OpenLZW): modified the function to check
+ the LZW header before doing anything else. This helps avoid un-necessary
+ heap allocations (400 Kb of heap memory for the LZW decoder ! Oh my !)
+
+ * src/gzip/ftgzip.c (FT_Stream_OpenGZip): ditto for the .gz decoder,
+ though the code savings is smaller.
+
2005-03-10 David Turner <david@freetype.org>
* src/tools/glnames.py: Add comment to explain the compression
diff --git a/include/freetype/internal/ftstream.h b/include/freetype/internal/ftstream.h
index 9833cd0..b5c7066 100644
--- a/include/freetype/internal/ftstream.h
+++ b/include/freetype/internal/ftstream.h
@@ -343,7 +343,7 @@ FT_BEGIN_HEADER
/* free a stream */
FT_BASE( void )
- FT_Stream_Free( FT_Stream stream,
+ FT_Stream_Free( FT_Stream stream,
FT_Int external );
/* initialize a stream for reading in-memory data */
@@ -385,6 +385,14 @@ FT_BEGIN_HEADER
FT_Byte* buffer,
FT_ULong count );
+ /* try to read bytes at the end of a stream, return
+ * the number of bytes really available
+ */
+ FT_BASE( FT_ULong )
+ FT_Stream_TryRead( FT_Stream stream,
+ FT_Byte* buffer,
+ FT_ULong count );
+
/* Enter a frame of `count' consecutive bytes in a stream. Returns an */
/* error if the frame could not be read/accessed. The caller can use */
/* the FT_Stream_Get_XXX functions to retrieve frame data without */
diff --git a/src/base/ftstream.c b/src/base/ftstream.c
index 4fb933e..2ff29cb 100644
--- a/src/base/ftstream.c
+++ b/src/base/ftstream.c
@@ -153,6 +153,35 @@
}
+ FT_BASE_DEF( FT_ULong )
+ FT_Stream_TryRead( FT_Stream stream,
+ FT_Byte* buffer,
+ FT_ULong count )
+ {
+ FT_ULong read_bytes = 0;
+
+
+ if ( stream->pos >= stream->size )
+ goto Exit;
+
+ if ( stream->read )
+ read_bytes = stream->read( stream, stream->pos, buffer, count );
+ else
+ {
+ read_bytes = stream->size - stream->pos;
+ if ( read_bytes > count )
+ read_bytes = count;
+
+ FT_MEM_COPY( buffer, stream->base + stream->pos, read_bytes );
+ }
+
+ stream->pos += read_bytes;
+
+ Exit:
+ return read_bytes;
+ }
+
+
FT_BASE_DEF( FT_Error )
FT_Stream_ExtractFrame( FT_Stream stream,
FT_ULong count,
diff --git a/src/bdf/bdflib.c b/src/bdf/bdflib.c
index 825385e..9ac50be 100644
--- a/src/bdf/bdflib.c
+++ b/src/bdf/bdflib.c
@@ -224,7 +224,6 @@
if ( FT_NEW_ARRAY( ht->table, ht->size ) )
goto Exit;
- FT_MEM_ZERO( ht->table, sizeof ( hashnode ) * ht->size );
for ( i = 0, bp = obp; i < sz; i++, bp++ )
{
@@ -255,7 +254,6 @@
if ( FT_NEW_ARRAY( ht->table, sz ) )
goto Exit;
- FT_MEM_ZERO( ht->table, sizeof ( hashnode ) * sz );
Exit:
return error;
@@ -351,6 +349,7 @@
char** field;
unsigned long size;
unsigned long used;
+ FT_Memory memory;
} _bdf_list_t;
@@ -389,20 +388,123 @@
#define sbitset( m, cc ) ( m[(cc) >> 3] & ( 1 << ( (cc) & 7 ) ) )
+ static void
+ _bdf_list_init( _bdf_list_t* list,
+ FT_Memory memory )
+ {
+ FT_ZERO(list);
+ list->memory = memory;
+ }
+
+ static void
+ _bdf_list_done( _bdf_list_t* list )
+ {
+ FT_Memory memory = list->memory;
+
+ if ( memory )
+ {
+ FT_FREE( list->field );
+ FT_ZERO(list);
+ }
+ }
+
+
+ static FT_Error
+ _bdf_list_ensure( _bdf_list_t* list,
+ int num_items )
+ {
+ FT_Error error = 0;
+
+ if ( num_items > list->size )
+ {
+ int oldsize = list->size;
+ int newsize = oldsize + (oldsize >> 1) + 4;
+ int bigsize = FT_INT_MAX / sizeof(char*);
+ FT_Memory memory = list->memory;
+
+ if ( oldsize == bigsize )
+ {
+ error = FT_Err_Out_Of_Memory;
+ goto Exit;
+ }
+ else if ( newsize < oldsize || newsize > bigsize )
+ newsize = bigsize;
+
+ if ( FT_RENEW_ARRAY( list->field, oldsize, newsize ) )
+ goto Exit;
+
+ list->size = newsize;
+ }
+ Exit:
+ return error;
+ }
+
+
+ static void
+ _bdf_list_shift( _bdf_list_t* list,
+ unsigned long n )
+ {
+ unsigned long i, u;
+
+
+ if ( list == 0 || list->used == 0 || n == 0 )
+ return;
+
+ if ( n >= list->used )
+ {
+ list->used = 0;
+ return;
+ }
+
+ for ( u = n, i = 0; u < list->used; i++, u++ )
+ list->field[i] = list->field[u];
+ list->used -= n;
+ }
+
+
+ static char *
+ _bdf_list_join( _bdf_list_t* list,
+ int c,
+ unsigned long *alen )
+ {
+ unsigned long i, j;
+ char *fp, *dp;
+
+
+ *alen = 0;
+
+ if ( list == 0 || list->used == 0 )
+ return 0;
+
+ dp = list->field[0];
+ for ( i = j = 0; i < list->used; i++ )
+ {
+ fp = list->field[i];
+ while ( *fp )
+ dp[j++] = *fp++;
+
+ if ( i + 1 < list->used )
+ dp[j++] = (char)c;
+ }
+ dp[j] = 0;
+
+ *alen = j;
+ return dp;
+ }
+
+
+
/* An empty string for empty fields. */
static const char empty[1] = { 0 }; /* XXX eliminate this */
- /* Assume the line is NULL-terminated and that the `list' parameter */
- /* was initialized the first time it was used. */
static FT_Error
- _bdf_split( char* separators,
- char* line,
- unsigned long linelen,
- _bdf_list_t* list,
- FT_Memory memory )
+ _bdf_list_split( _bdf_list_t* list,
+ char* separators,
+ char* line,
+ unsigned long linelen )
{
int mult, final_empty;
char *sp, *ep, *end;
@@ -451,20 +553,9 @@
/* Resize the list if necessary. */
if ( list->used == list->size )
{
- if ( list->size == 0 )
- {
- if ( FT_NEW_ARRAY( list->field, 5 ) )
- goto Exit;
- }
- else
- {
- if ( FT_RENEW_ARRAY ( list->field ,
- list->size,
- list->size + 5 ) )
- goto Exit;
- }
-
- list->size += 5;
+ error = _bdf_list_ensure( list, list->used+1 );
+ if ( error )
+ goto Exit;
}
/* Assign the field appropriately. */
@@ -489,48 +580,16 @@
}
/* Finally, NULL-terminate the list. */
- if ( list->used + final_empty + 1 >= list->size )
+ if ( list->used + final_empty >= list->size )
{
- if ( list->used == list->size )
- {
- if ( list->size == 0 )
- {
- if ( FT_NEW_ARRAY( list->field, 5 ) )
- goto Exit;
- }
- else
- {
- if ( FT_RENEW_ARRAY( list->field,
- list->size,
- list->size + 5 ) )
- goto Exit;
- }
-
- list->size += 5;
- }
+ error = _bdf_list_ensure( list, list->used+final_empty+1 );
+ if ( error )
+ goto Exit;
}
if ( final_empty )
list->field[list->used++] = (char*)empty;
- if ( list->used == list->size )
- {
- if ( list->size == 0 )
- {
- if ( FT_NEW_ARRAY( list->field, 5 ) )
- goto Exit;
- }
- else
- {
- if ( FT_RENEW_ARRAY( list->field,
- list->size,
- list->size + 5 ) )
- goto Exit;
- }
-
- list->size += 5;
- }
-
list->field[list->used] = 0;
Exit:
@@ -538,100 +597,8 @@
}
- static void
- _bdf_shift( unsigned long n,
- _bdf_list_t* list )
- {
- unsigned long i, u;
-
-
- if ( list == 0 || list->used == 0 || n == 0 )
- return;
-
- if ( n >= list->used )
- {
- list->used = 0;
- return;
- }
-
- for ( u = n, i = 0; u < list->used; i++, u++ )
- list->field[i] = list->field[u];
- list->used -= n;
- }
-
-
- static char *
- _bdf_join( int c,
- unsigned long* len,
- _bdf_list_t* list )
- {
- unsigned long i, j;
- char *fp, *dp;
-
-
- if ( list == 0 || list->used == 0 )
- return 0;
-
- *len = 0;
-
- dp = list->field[0];
- for ( i = j = 0; i < list->used; i++ )
- {
- fp = list->field[i];
- while ( *fp )
- dp[j++] = *fp++;
-
- if ( i + 1 < list->used )
- dp[j++] = (char)c;
- }
- dp[j] = 0;
-
- *len = j;
- return dp;
- }
-
-
- /* High speed file reader that passes each line to a callback. */
- static FT_Error
- bdf_internal_readstream( FT_Stream stream,
- char* buffer,
- int count,
- int *read_bytes )
- {
- int rbytes;
- unsigned long pos = stream->pos;
- FT_Error error = BDF_Err_Ok;
-
-
- if ( pos > stream->size )
- {
- FT_ERROR(( "bdf_internal_readstream:" ));
- FT_ERROR(( " invalid i/o; pos = 0x%lx, size = 0x%lx\n",
- pos, stream->size ));
- error = BDF_Err_Invalid_Stream_Operation;
- goto Exit;
- }
-
- if ( stream->read )
- rbytes = stream->read( stream, pos,
- (unsigned char *)buffer, count );
- else
- {
- rbytes = stream->size - pos;
- if ( rbytes > count )
- rbytes = count;
-
- FT_MEM_COPY( buffer, stream->base + pos, rbytes );
- }
-
- stream->pos = pos + rbytes;
-
- *read_bytes = rbytes;
-
- Exit:
- return error;
- }
+#define NO_SKIP 256 /* this value cannot be stored in a 'char' */
static FT_Error
_bdf_readstream( FT_Stream stream,
@@ -640,10 +607,10 @@
unsigned long *lno )
{
_bdf_line_func_t cb;
- unsigned long lineno;
- int n, done, refill, bytes, hold;
- char *ls, *le, *pp, *pe, *hp;
- char *buf = 0;
+ unsigned long lineno, buf_size;
+ int refill, bytes, hold, to_skip;
+ int start, end, cursor, avail;
+ char* buf = 0;
FT_Memory memory = stream->memory;
FT_Error error = BDF_Err_Ok;
@@ -654,85 +621,115 @@
goto Exit;
}
- if ( FT_NEW_ARRAY( buf, 65536L ) )
- goto Exit;
+ /* initial size and allocation of the input buffer
+ */
+ buf_size = 1024;
- cb = callback;
- lineno = 1;
- buf[0] = 0;
-
- done = 0;
- pp = ls = le = buf;
+ if ( FT_NEW_ARRAY( buf, buf_size ) )
+ goto Exit;
- bytes = 65536L;
+ cb = callback;
+ lineno = 1;
+ buf[0] = 0;
+ start = 0;
+ end = 0;
+ avail = 0;
+ cursor = 0;
+ refill = 1;
+ to_skip = NO_SKIP;
- while ( !done )
+ for (;;)
{
- error = bdf_internal_readstream( stream, pp, bytes, &n );
- if ( error )
- goto Exit;
+ if ( refill )
+ {
+ bytes = (int) FT_Stream_TryRead( stream, buf + cursor,
+ (FT_ULong)(buf_size - cursor) );
+ avail = cursor + bytes;
+ cursor = 0;
+ refill = 0;
+ }
- if ( n == 0 )
- break;
+ end = start;
- /* Determine the new end of the buffer pages. */
- pe = pp + n;
+ /* should we skip an optional character like \n or \r ? */
+ if ( start < avail && buf[start] == to_skip )
+ {
+ start += 1;
+ to_skip = NO_SKIP;
+ continue;
+ }
- for ( refill = 0; done == 0 && refill == 0; )
+ /* try to find the end of the line */
+ while ( end < avail && buf[end] != '\n' && buf[end] != '\r' )
+ end++;
+
+ /* if we hit the end of the buffer, try shifting its content
+ * or even resizing it
+ */
+ if ( end >= avail )
{
- while ( le < pe && *le != '\n' && *le != '\r' )
- le++;
+ if ( bytes == 0 ) /* last line in file doesn't end in \r or \n */
+ break; /* ignore it then exit */
- if ( le == pe )
+ if ( start == 0 )
{
- /* Hit the end of the last page in the buffer. Need to find */
- /* out how many pages to shift and how many pages need to be */
- /* read in. Adjust the line start and end pointers down to */
- /* point to the right places in the pages. */
-
- pp = buf + ( ( ( ls - buf ) >> 13 ) << 13 );
- n = pp - buf;
- ls -= n;
- le -= n;
- n = pe - pp;
-
- FT_MEM_MOVE( buf, pp, n );
-
- pp = buf + n;
- bytes = 65536L - n;
- refill = 1;
+ /* this line is definitely too long, try resizing the input buffer
+ * a bit to handle it.
+ */
+ FT_ULong new_size;
+
+ if ( buf_size >= 65536UL ) /* limit ourselves to 64 Kb */
+ {
+ error = BDF_Err_Invalid_Argument;
+ goto Exit;
+ }
+
+ new_size = buf_size*2;
+ if ( FT_RENEW_ARRAY( buf, buf_size, new_size ) )
+ goto Exit;
+
+ cursor = buf_size;
+ buf_size = new_size;
}
else
{
- /* Temporarily NULL-terminate the line. */
- hp = le;
- hold = *le;
- *le = 0;
-
- /* XXX: Use encoding independent value for 0x1a */
- if ( *ls != '#' && *ls != 0x1a &&
- le > ls &&
- ( error = (*cb)( ls, le - ls, lineno, (void *)&cb,
- client_data ) ) != BDF_Err_Ok )
- done = 1;
- else
- {
- ls = ++le;
- /* Handle the case of DOS crlf sequences. */
- if ( le < pe && hold == '\n' && *le =='\r' )
- ls = ++le;
- }
+ bytes = avail - start;
- /* Increment the line number. */
- lineno++;
+ FT_MEM_COPY( buf, buf+start, bytes );
- /* Restore the character at the end of the line. */
- *hp = (char)hold;
+ cursor = bytes;
+ avail -= bytes;
+ start = 0;
}
+ refill = 1;
+ continue;
+ }
+
+ /* Temporarily NUL-terminate the line. */
+ hold = buf[end];
+ buf[end] = 0;
+
+ /* XXX: Use encoding independent value for 0x1a */
+ if ( buf[start] != '#' && buf[start] != 0x1a && end > start )
+ {
+ error = (*cb)( buf+start, end-start, lineno, (void*)&cb, client_data );
+ if ( error )
+ break;
}
+
+ lineno += 1;
+ buf[end] = (char)hold;
+ start = end+1;
+
+ if ( hold == '\n' )
+ to_skip = '\r';
+ else if ( hold == '\r' )
+ to_skip = '\n';
+ else
+ to_skip = NO_SKIP;
}
- *lno = lineno;
+ *lno = lineno;
Exit:
FT_FREE( buf );
@@ -955,7 +952,8 @@
if ( c1->encoding < c2->encoding )
return -1;
- else if ( c1->encoding > c2->encoding )
+
+ if ( c1->encoding > c2->encoding )
return 1;
return 0;
@@ -979,23 +977,16 @@
if ( hash_lookup( name, &(font->proptbl) ) )
goto Exit;
- if ( font->nuser_props == 0 )
- {
- if ( FT_NEW_ARRAY( font->user_props, 1 ) )
- goto Exit;
- }
- else
- {
- if ( FT_RENEW_ARRAY( font->user_props,
- font->nuser_props,
- font->nuser_props + 1 ) )
- goto Exit;
- }
+ if ( FT_RENEW_ARRAY( font->user_props,
+ font->nuser_props,
+ font->nuser_props + 1 ) )
+ goto Exit;
p = font->user_props + font->nuser_props;
- FT_MEM_ZERO( p, sizeof ( bdf_property_t ) );
+ FT_ZERO( p );
n = (unsigned long)( ft_strlen( name ) + 1 );
+
if ( FT_NEW_ARRAY( p->name, n ) )
goto Exit;
@@ -1110,23 +1101,16 @@
FT_Error error = BDF_Err_Ok;
- if ( font->comments_len == 0 )
- {
- if ( FT_NEW_ARRAY( font->comments, len + 1 ) )
- goto Exit;
- }
- else
- {
- if ( FT_RENEW_ARRAY( font->comments,
- font->comments_len,
- font->comments_len + len + 1 ) )
- goto Exit;
- }
+ if ( FT_RENEW_ARRAY( font->comments,
+ font->comments_len,
+ font->comments_len + len + 1 ) )
+ goto Exit;
cp = font->comments + font->comments_len;
+
FT_MEM_COPY( cp, comment, len );
- cp += len;
- *cp++ = '\n';
+ cp[len] = '\n';
+
font->comments_len += len + 1;
Exit:
@@ -1155,16 +1139,16 @@
memory = font->memory;
+ _bdf_list_init( &list, memory );
+
font->spacing = opts->font_spacing;
len = (unsigned long)( ft_strlen( font->name ) + 1 );
FT_MEM_COPY( name, font->name, len );
- list.size = list.used = 0;
-
- error = _bdf_split( (char *)"-", name, len, &list, memory );
+ error = _bdf_list_split( &list, (char *)"-", name, len );
if ( error )
- goto Exit;
+ goto Fail;
if ( list.used == 15 )
{
@@ -1185,7 +1169,8 @@
}
}
- FT_FREE( list.field );
+ Fail:
+ _bdf_list_done( &list );
Exit:
return error;
@@ -1484,7 +1469,7 @@
goto Exit;
}
- error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
+ error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
if ( error )
goto Exit;
p->cnt = font->glyphs_size = _bdf_atoul( p->list.field[1], 0, 10 );
@@ -1538,15 +1523,17 @@
/* encoding can be checked for an unencoded character. */
FT_FREE( p->glyph_name );
- error = _bdf_split( (char *)" +", line, linelen, &p->list,memory );
+ error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
if ( error )
goto Exit;
- _bdf_shift( 1, &p->list );
- s = _bdf_join( ' ', &slen, &p->list );
+ _bdf_list_shift( &p->list, 1 );
+
+ s = _bdf_list_join( &p->list, ' ', &slen );
if ( FT_NEW_ARRAY( p->glyph_name, slen + 1 ) )
goto Exit;
+
FT_MEM_COPY( p->glyph_name, s, slen + 1 );
p->flags |= _BDF_GLYPH;
@@ -1565,9 +1552,10 @@
goto Exit;
}
- error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
+ error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
if ( error )
goto Exit;
+
p->glyph_enc = _bdf_atol( p->list.field[1], 0, 10 );
/* Check to see whether this encoding has already been encountered. */
@@ -1598,8 +1586,7 @@
font->glyphs_size,
font->glyphs_size + 64 ) )
goto Exit;
- FT_MEM_ZERO( font->glyphs + font->glyphs_size,
- sizeof ( bdf_glyph_t ) * 64 ); /* FZ inutile */
+
font->glyphs_size += 64;
}
@@ -1619,18 +1606,11 @@
/* Allocate the next unencoded glyph. */
if ( font->unencoded_used == font->unencoded_size )
{
- if ( font->unencoded_size == 0 )
- {
- if ( FT_NEW_ARRAY( font->unencoded, 4 ) )
- goto Exit;
- }
- else
- {
- if ( FT_RENEW_ARRAY( font->unencoded ,
- font->unencoded_size,
- font->unencoded_size + 4 ) )
- goto Exit;
- }
+ if ( FT_RENEW_ARRAY( font->unencoded ,
+ font->unencoded_size,
+ font->unencoded_size + 4 ) )
+ goto Exit;
+
font->unencoded_size += 4;
}
@@ -1719,9 +1699,10 @@
goto Exit;
}
- error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
+ error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
if ( error )
goto Exit;
+
glyph->swidth = (unsigned short)_bdf_atoul( p->list.field[1], 0, 10 );
p->flags |= _BDF_SWIDTH;
@@ -1731,9 +1712,10 @@
/* Expect the DWIDTH (scalable width) field next. */
if ( ft_memcmp( line, "DWIDTH", 6 ) == 0 )
{
- error = _bdf_split( (char *)" +", line, linelen, &p->list,memory );
+ error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
if ( error )
goto Exit;
+
glyph->dwidth = (unsigned short)_bdf_atoul( p->list.field[1], 0, 10 );
if ( !( p->flags & _BDF_SWIDTH ) )
@@ -1755,7 +1737,7 @@
/* Expect the BBX field next. */
if ( ft_memcmp( line, "BBX", 3 ) == 0 )
{
- error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
+ error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
if ( error )
goto Exit;
@@ -1936,13 +1918,13 @@
}
else
{
- error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
+ error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
if ( error )
goto Exit;
name = p->list.field[0];
- _bdf_shift( 1, &p->list );
- value = _bdf_join( ' ', &vlen, &p->list );
+ _bdf_list_shift( &p->list, 1 );
+ value = _bdf_list_join( &p->list, ' ', &vlen );
error = _bdf_add_property( p->font, name, value );
if ( error )
@@ -2057,7 +2039,7 @@
/* Check for the start of the properties. */
if ( ft_memcmp( line, "STARTPROPERTIES", 15 ) == 0 )
{
- error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
+ error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
if ( error )
goto Exit;
p->cnt = p->font->props_size = _bdf_atoul( p->list.field[1], 0, 10 );
@@ -2082,7 +2064,7 @@
goto Exit;
}
- error = _bdf_split( (char *)" +", line, linelen, &p->list , memory );
+ error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
if ( error )
goto Exit;
@@ -2105,12 +2087,12 @@
/* The next thing to check for is the FONT field. */
if ( ft_memcmp( line, "FONT", 4 ) == 0 )
{
- error = _bdf_split( (char *)" +", line, linelen, &p->list , memory );
+ error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
if ( error )
goto Exit;
- _bdf_shift( 1, &p->list );
+ _bdf_list_shift( &p->list, 1 );
- s = _bdf_join( ' ', &slen, &p->list );
+ s = _bdf_list_join( &p->list, ' ', &slen );
if ( FT_NEW_ARRAY( p->font->name, slen + 1 ) )
goto Exit;
FT_MEM_COPY( p->font->name, s, slen + 1 );
@@ -2137,7 +2119,7 @@
goto Exit;
}
- error = _bdf_split( (char *)" +", line, linelen, &p->list, memory );
+ error = _bdf_list_split( &p->list, (char *)" +", line, linelen );
if ( error )
goto Exit;
@@ -2207,7 +2189,7 @@
FT_Error error = BDF_Err_Ok;
- if ( FT_ALLOC( p, sizeof ( _bdf_parse_t ) ) )
+ if ( FT_NEW( p ) )
goto Exit;
memory = NULL;
@@ -2215,6 +2197,8 @@
p->minlb = 32767;
p->memory = extmemory; /* only during font creation */
+ _bdf_list_init( &p->list, extmemory );
+
error = _bdf_readstream( stream, _bdf_parse_start,
(void *)p, &lineno );
if ( error )
@@ -2301,10 +2285,6 @@
}
}
- /* Free up the list used during the parsing. */
- if ( memory != NULL )
- FT_FREE( p->list.field );
-
if ( p->font != 0 )
{
/* Make sure the comments are NULL terminated if they exist. */
@@ -2327,7 +2307,10 @@
Exit:
if ( p )
{
+ _bdf_list_done( &p->list );
+
memory = extmemory;
+
FT_FREE( p );
}
diff --git a/src/gzip/ftgzip.c b/src/gzip/ftgzip.c
index a78c195..c2ae1a0 100644
--- a/src/gzip/ftgzip.c
+++ b/src/gzip/ftgzip.c
@@ -561,6 +561,12 @@
FT_Memory memory = source->memory;
FT_GZipFile zip;
+ /* check the header right now, this prevents allocating un-necessary
+ * objects when we don't need them
+ */
+ error = ft_gzip_check_header( source );
+ if ( error )
+ goto Exit;
FT_ZERO( stream );
stream->memory = memory;
diff --git a/src/lzw/ftlzw.c b/src/lzw/ftlzw.c
index a0e4cc5..bf52b77 100644
--- a/src/lzw/ftlzw.c
+++ b/src/lzw/ftlzw.c
@@ -414,6 +414,16 @@
FT_Memory memory = source->memory;
FT_LZWFile zip;
+ /* check the header right now, this will prevent us from
+ * allocating a huge LZWFile object (400 Kb of heap memory !!)
+ * when not necessary.
+ *
+ * Did I mention that you should never use .Z compressed font
+ * file ?
+ */
+ error = ft_lzw_check_header( source );
+ if ( error )
+ goto Exit;
FT_ZERO( stream );
stream->memory = memory;