1 /***************************************************************************/ 2 /* */ 3 /* ftgzip.c */ 4 /* */ 5 /* FreeType support for .gz compressed files. */ 6 /* */ 7 /* This optional component relies on zlib. It should mainly be used to */ 8 /* parse compressed PCF fonts, as found with many X11 server */ 9 /* distributions. */ 10 /* */ 11 /* Copyright 2002-2015 by */ 12 /* David Turner, Robert Wilhelm, and Werner Lemberg. */ 13 /* */ 14 /* This file is part of the FreeType project, and may only be used, */ 15 /* modified, and distributed under the terms of the FreeType project */ 16 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */ 17 /* this file you indicate that you have read the license and */ 18 /* understand and accept it fully. */ 19 /* */ 20 /***************************************************************************/ 21 22 23 #include <ft2build.h> 24 #include FT_INTERNAL_MEMORY_H 25 #include FT_INTERNAL_STREAM_H 26 #include FT_INTERNAL_DEBUG_H 27 #include FT_GZIP_H 28 #include FT_CONFIG_STANDARD_LIBRARY_H 29 30 31 #include FT_MODULE_ERRORS_H 32 33 #undef __FTERRORS_H__ 34 35 #undef FT_ERR_PREFIX 36 #define FT_ERR_PREFIX Gzip_Err_ 37 #define FT_ERR_BASE FT_Mod_Err_Gzip 38 39 #include FT_ERRORS_H 40 41 42 #ifdef FT_CONFIG_OPTION_USE_ZLIB 43 44 #ifdef FT_CONFIG_OPTION_PIC 45 #error "gzip code does not support PIC yet" 46 #endif 47 48 #ifdef FT_CONFIG_OPTION_SYSTEM_ZLIB 49 50 #include <zlib.h> 51 52 #else /* !FT_CONFIG_OPTION_SYSTEM_ZLIB */ 53 54 /* In this case, we include our own modified sources of the ZLib */ 55 /* within the "ftgzip" component. The modifications were necessary */ 56 /* to #include all files without conflicts, as well as preventing */ 57 /* the definition of "extern" functions that may cause linking */ 58 /* conflicts when a program is linked with both FreeType and the */ 59 /* original ZLib. */ 60 61 #ifndef USE_ZLIB_ZCALLOC 62 #define MY_ZCALLOC /* prevent all zcalloc() & zfree() in zutils.c */ 63 #endif 64 65 #include "zlib.h" 66 67 #undef SLOW 68 #define SLOW 1 /* we can't use asm-optimized sources here! */ 69 70 #if defined( _MSC_VER ) /* Visual C++ (and Intel C++) */ 71 /* We disable the warning `conversion from XXX to YYY, */ 72 /* possible loss of data' in order to compile cleanly with */ 73 /* the maximum level of warnings: zlib is non-FreeType */ 74 /* code. */ 75 #pragma warning( push ) 76 #pragma warning( disable : 4244 ) 77 #endif /* _MSC_VER */ 78 79 /* Urgh. `inflate_mask' must not be declared twice -- C++ doesn't like 80 this. We temporarily disable it and load all necessary header files. */ 81 #define NO_INFLATE_MASK 82 #include "zutil.h" 83 #include "inftrees.h" 84 #include "infblock.h" 85 #include "infcodes.h" 86 #include "infutil.h" 87 #undef NO_INFLATE_MASK 88 89 /* infutil.c must be included before infcodes.c */ 90 #include "zutil.c" 91 #include "inftrees.c" 92 #include "infutil.c" 93 #include "infcodes.c" 94 #include "infblock.c" 95 #include "inflate.c" 96 #include "adler32.c" 97 98 #if defined( _MSC_VER ) 99 #pragma warning( pop ) 100 #endif 101 102 #endif /* !FT_CONFIG_OPTION_SYSTEM_ZLIB */ 103 104 105 /***************************************************************************/ 106 /***************************************************************************/ 107 /***** *****/ 108 /***** Z L I B M E M O R Y M A N A G E M E N T *****/ 109 /***** *****/ 110 /***************************************************************************/ 111 /***************************************************************************/ 112 113 /* it is better to use FreeType memory routines instead of raw 114 'malloc/free' */ 115 116 static voidpf ft_gzip_alloc(FT_Memory memory,uInt items,uInt size)117 ft_gzip_alloc( FT_Memory memory, 118 uInt items, 119 uInt size ) 120 { 121 FT_ULong sz = (FT_ULong)size * items; 122 FT_Error error; 123 FT_Pointer p = NULL; 124 125 126 (void)FT_ALLOC( p, sz ); 127 return p; 128 } 129 130 131 static void ft_gzip_free(FT_Memory memory,voidpf address)132 ft_gzip_free( FT_Memory memory, 133 voidpf address ) 134 { 135 FT_MEM_FREE( address ); 136 } 137 138 139 #if !defined( FT_CONFIG_OPTION_SYSTEM_ZLIB ) && !defined( USE_ZLIB_ZCALLOC ) 140 141 local voidpf zcalloc(voidpf opaque,unsigned items,unsigned size)142 zcalloc ( voidpf opaque, 143 unsigned items, 144 unsigned size ) 145 { 146 return ft_gzip_alloc( (FT_Memory)opaque, items, size ); 147 } 148 149 local void zcfree(voidpf opaque,voidpf ptr)150 zcfree( voidpf opaque, 151 voidpf ptr ) 152 { 153 ft_gzip_free( (FT_Memory)opaque, ptr ); 154 } 155 156 #endif /* !SYSTEM_ZLIB && !USE_ZLIB_ZCALLOC */ 157 158 159 /***************************************************************************/ 160 /***************************************************************************/ 161 /***** *****/ 162 /***** Z L I B F I L E D E S C R I P T O R *****/ 163 /***** *****/ 164 /***************************************************************************/ 165 /***************************************************************************/ 166 167 #define FT_GZIP_BUFFER_SIZE 4096 168 169 typedef struct FT_GZipFileRec_ 170 { 171 FT_Stream source; /* parent/source stream */ 172 FT_Stream stream; /* embedding stream */ 173 FT_Memory memory; /* memory allocator */ 174 z_stream zstream; /* zlib input stream */ 175 176 FT_ULong start; /* starting position, after .gz header */ 177 FT_Byte input[FT_GZIP_BUFFER_SIZE]; /* input read buffer */ 178 179 FT_Byte buffer[FT_GZIP_BUFFER_SIZE]; /* output buffer */ 180 FT_ULong pos; /* position in output */ 181 FT_Byte* cursor; 182 FT_Byte* limit; 183 184 } FT_GZipFileRec, *FT_GZipFile; 185 186 187 /* gzip flag byte */ 188 #define FT_GZIP_ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ 189 #define FT_GZIP_HEAD_CRC 0x02 /* bit 1 set: header CRC present */ 190 #define FT_GZIP_EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ 191 #define FT_GZIP_ORIG_NAME 0x08 /* bit 3 set: original file name present */ 192 #define FT_GZIP_COMMENT 0x10 /* bit 4 set: file comment present */ 193 #define FT_GZIP_RESERVED 0xE0 /* bits 5..7: reserved */ 194 195 196 /* check and skip .gz header - we don't support `transparent' compression */ 197 static FT_Error ft_gzip_check_header(FT_Stream stream)198 ft_gzip_check_header( FT_Stream stream ) 199 { 200 FT_Error error; 201 FT_Byte head[4]; 202 203 204 if ( FT_STREAM_SEEK( 0 ) || 205 FT_STREAM_READ( head, 4 ) ) 206 goto Exit; 207 208 /* head[0] && head[1] are the magic numbers; */ 209 /* head[2] is the method, and head[3] the flags */ 210 if ( head[0] != 0x1F || 211 head[1] != 0x8B || 212 head[2] != Z_DEFLATED || 213 (head[3] & FT_GZIP_RESERVED) ) 214 { 215 error = FT_THROW( Invalid_File_Format ); 216 goto Exit; 217 } 218 219 /* skip time, xflags and os code */ 220 (void)FT_STREAM_SKIP( 6 ); 221 222 /* skip the extra field */ 223 if ( head[3] & FT_GZIP_EXTRA_FIELD ) 224 { 225 FT_UInt len; 226 227 228 if ( FT_READ_USHORT_LE( len ) || 229 FT_STREAM_SKIP( len ) ) 230 goto Exit; 231 } 232 233 /* skip original file name */ 234 if ( head[3] & FT_GZIP_ORIG_NAME ) 235 for (;;) 236 { 237 FT_UInt c; 238 239 240 if ( FT_READ_BYTE( c ) ) 241 goto Exit; 242 243 if ( c == 0 ) 244 break; 245 } 246 247 /* skip .gz comment */ 248 if ( head[3] & FT_GZIP_COMMENT ) 249 for (;;) 250 { 251 FT_UInt c; 252 253 254 if ( FT_READ_BYTE( c ) ) 255 goto Exit; 256 257 if ( c == 0 ) 258 break; 259 } 260 261 /* skip CRC */ 262 if ( head[3] & FT_GZIP_HEAD_CRC ) 263 if ( FT_STREAM_SKIP( 2 ) ) 264 goto Exit; 265 266 Exit: 267 return error; 268 } 269 270 271 static FT_Error ft_gzip_file_init(FT_GZipFile zip,FT_Stream stream,FT_Stream source)272 ft_gzip_file_init( FT_GZipFile zip, 273 FT_Stream stream, 274 FT_Stream source ) 275 { 276 z_stream* zstream = &zip->zstream; 277 FT_Error error = FT_Err_Ok; 278 279 280 zip->stream = stream; 281 zip->source = source; 282 zip->memory = stream->memory; 283 284 zip->limit = zip->buffer + FT_GZIP_BUFFER_SIZE; 285 zip->cursor = zip->limit; 286 zip->pos = 0; 287 288 /* check and skip .gz header */ 289 { 290 stream = source; 291 292 error = ft_gzip_check_header( stream ); 293 if ( error ) 294 goto Exit; 295 296 zip->start = FT_STREAM_POS(); 297 } 298 299 /* initialize zlib -- there is no zlib header in the compressed stream */ 300 zstream->zalloc = (alloc_func)ft_gzip_alloc; 301 zstream->zfree = (free_func) ft_gzip_free; 302 zstream->opaque = stream->memory; 303 304 zstream->avail_in = 0; 305 zstream->next_in = zip->buffer; 306 307 if ( inflateInit2( zstream, -MAX_WBITS ) != Z_OK || 308 zstream->next_in == NULL ) 309 error = FT_THROW( Invalid_File_Format ); 310 311 Exit: 312 return error; 313 } 314 315 316 static void ft_gzip_file_done(FT_GZipFile zip)317 ft_gzip_file_done( FT_GZipFile zip ) 318 { 319 z_stream* zstream = &zip->zstream; 320 321 322 inflateEnd( zstream ); 323 324 /* clear the rest */ 325 zstream->zalloc = NULL; 326 zstream->zfree = NULL; 327 zstream->opaque = NULL; 328 zstream->next_in = NULL; 329 zstream->next_out = NULL; 330 zstream->avail_in = 0; 331 zstream->avail_out = 0; 332 333 zip->memory = NULL; 334 zip->source = NULL; 335 zip->stream = NULL; 336 } 337 338 339 static FT_Error ft_gzip_file_reset(FT_GZipFile zip)340 ft_gzip_file_reset( FT_GZipFile zip ) 341 { 342 FT_Stream stream = zip->source; 343 FT_Error error; 344 345 346 if ( !FT_STREAM_SEEK( zip->start ) ) 347 { 348 z_stream* zstream = &zip->zstream; 349 350 351 inflateReset( zstream ); 352 353 zstream->avail_in = 0; 354 zstream->next_in = zip->input; 355 zstream->avail_out = 0; 356 zstream->next_out = zip->buffer; 357 358 zip->limit = zip->buffer + FT_GZIP_BUFFER_SIZE; 359 zip->cursor = zip->limit; 360 zip->pos = 0; 361 } 362 363 return error; 364 } 365 366 367 static FT_Error ft_gzip_file_fill_input(FT_GZipFile zip)368 ft_gzip_file_fill_input( FT_GZipFile zip ) 369 { 370 z_stream* zstream = &zip->zstream; 371 FT_Stream stream = zip->source; 372 FT_ULong size; 373 374 375 if ( stream->read ) 376 { 377 size = stream->read( stream, stream->pos, zip->input, 378 FT_GZIP_BUFFER_SIZE ); 379 if ( size == 0 ) 380 { 381 zip->limit = zip->cursor; 382 return FT_THROW( Invalid_Stream_Operation ); 383 } 384 } 385 else 386 { 387 size = stream->size - stream->pos; 388 if ( size > FT_GZIP_BUFFER_SIZE ) 389 size = FT_GZIP_BUFFER_SIZE; 390 391 if ( size == 0 ) 392 { 393 zip->limit = zip->cursor; 394 return FT_THROW( Invalid_Stream_Operation ); 395 } 396 397 FT_MEM_COPY( zip->input, stream->base + stream->pos, size ); 398 } 399 stream->pos += size; 400 401 zstream->next_in = zip->input; 402 zstream->avail_in = size; 403 404 return FT_Err_Ok; 405 } 406 407 408 static FT_Error ft_gzip_file_fill_output(FT_GZipFile zip)409 ft_gzip_file_fill_output( FT_GZipFile zip ) 410 { 411 z_stream* zstream = &zip->zstream; 412 FT_Error error = FT_Err_Ok; 413 414 415 zip->cursor = zip->buffer; 416 zstream->next_out = zip->cursor; 417 zstream->avail_out = FT_GZIP_BUFFER_SIZE; 418 419 while ( zstream->avail_out > 0 ) 420 { 421 int err; 422 423 424 if ( zstream->avail_in == 0 ) 425 { 426 error = ft_gzip_file_fill_input( zip ); 427 if ( error ) 428 break; 429 } 430 431 err = inflate( zstream, Z_NO_FLUSH ); 432 433 if ( err == Z_STREAM_END ) 434 { 435 zip->limit = zstream->next_out; 436 if ( zip->limit == zip->cursor ) 437 error = FT_THROW( Invalid_Stream_Operation ); 438 break; 439 } 440 else if ( err != Z_OK ) 441 { 442 zip->limit = zip->cursor; 443 error = FT_THROW( Invalid_Stream_Operation ); 444 break; 445 } 446 } 447 448 return error; 449 } 450 451 452 /* fill output buffer; `count' must be <= FT_GZIP_BUFFER_SIZE */ 453 static FT_Error ft_gzip_file_skip_output(FT_GZipFile zip,FT_ULong count)454 ft_gzip_file_skip_output( FT_GZipFile zip, 455 FT_ULong count ) 456 { 457 FT_Error error = FT_Err_Ok; 458 FT_ULong delta; 459 460 461 for (;;) 462 { 463 delta = (FT_ULong)( zip->limit - zip->cursor ); 464 if ( delta >= count ) 465 delta = count; 466 467 zip->cursor += delta; 468 zip->pos += delta; 469 470 count -= delta; 471 if ( count == 0 ) 472 break; 473 474 error = ft_gzip_file_fill_output( zip ); 475 if ( error ) 476 break; 477 } 478 479 return error; 480 } 481 482 483 static FT_ULong ft_gzip_file_io(FT_GZipFile zip,FT_ULong pos,FT_Byte * buffer,FT_ULong count)484 ft_gzip_file_io( FT_GZipFile zip, 485 FT_ULong pos, 486 FT_Byte* buffer, 487 FT_ULong count ) 488 { 489 FT_ULong result = 0; 490 FT_Error error; 491 492 493 /* Reset inflate stream if we're seeking backwards. */ 494 /* Yes, that is not too efficient, but it saves memory :-) */ 495 if ( pos < zip->pos ) 496 { 497 error = ft_gzip_file_reset( zip ); 498 if ( error ) 499 goto Exit; 500 } 501 502 /* skip unwanted bytes */ 503 if ( pos > zip->pos ) 504 { 505 error = ft_gzip_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) ); 506 if ( error ) 507 goto Exit; 508 } 509 510 if ( count == 0 ) 511 goto Exit; 512 513 /* now read the data */ 514 for (;;) 515 { 516 FT_ULong delta; 517 518 519 delta = (FT_ULong)( zip->limit - zip->cursor ); 520 if ( delta >= count ) 521 delta = count; 522 523 FT_MEM_COPY( buffer, zip->cursor, delta ); 524 buffer += delta; 525 result += delta; 526 zip->cursor += delta; 527 zip->pos += delta; 528 529 count -= delta; 530 if ( count == 0 ) 531 break; 532 533 error = ft_gzip_file_fill_output( zip ); 534 if ( error ) 535 break; 536 } 537 538 Exit: 539 return result; 540 } 541 542 543 /***************************************************************************/ 544 /***************************************************************************/ 545 /***** *****/ 546 /***** G Z E M B E D D I N G S T R E A M *****/ 547 /***** *****/ 548 /***************************************************************************/ 549 /***************************************************************************/ 550 551 static void ft_gzip_stream_close(FT_Stream stream)552 ft_gzip_stream_close( FT_Stream stream ) 553 { 554 FT_GZipFile zip = (FT_GZipFile)stream->descriptor.pointer; 555 FT_Memory memory = stream->memory; 556 557 558 if ( zip ) 559 { 560 /* finalize gzip file descriptor */ 561 ft_gzip_file_done( zip ); 562 563 FT_FREE( zip ); 564 565 stream->descriptor.pointer = NULL; 566 } 567 568 if ( !stream->read ) 569 FT_FREE( stream->base ); 570 } 571 572 573 static unsigned long ft_gzip_stream_io(FT_Stream stream,unsigned long offset,unsigned char * buffer,unsigned long count)574 ft_gzip_stream_io( FT_Stream stream, 575 unsigned long offset, 576 unsigned char* buffer, 577 unsigned long count ) 578 { 579 FT_GZipFile zip = (FT_GZipFile)stream->descriptor.pointer; 580 581 582 return ft_gzip_file_io( zip, offset, buffer, count ); 583 } 584 585 586 static FT_ULong ft_gzip_get_uncompressed_size(FT_Stream stream)587 ft_gzip_get_uncompressed_size( FT_Stream stream ) 588 { 589 FT_Error error; 590 FT_ULong old_pos; 591 FT_ULong result = 0; 592 593 594 old_pos = stream->pos; 595 if ( !FT_Stream_Seek( stream, stream->size - 4 ) ) 596 { 597 result = FT_Stream_ReadULongLE( stream, &error ); 598 if ( error ) 599 result = 0; 600 601 (void)FT_Stream_Seek( stream, old_pos ); 602 } 603 604 return result; 605 } 606 607 608 /* documentation is in ftgzip.h */ 609 610 FT_EXPORT_DEF( FT_Error ) FT_Stream_OpenGzip(FT_Stream stream,FT_Stream source)611 FT_Stream_OpenGzip( FT_Stream stream, 612 FT_Stream source ) 613 { 614 FT_Error error; 615 FT_Memory memory; 616 FT_GZipFile zip = NULL; 617 618 619 if ( !stream || !source ) 620 { 621 error = FT_THROW( Invalid_Stream_Handle ); 622 goto Exit; 623 } 624 625 memory = source->memory; 626 627 /* 628 * check the header right now; this prevents allocating un-necessary 629 * objects when we don't need them 630 */ 631 error = ft_gzip_check_header( source ); 632 if ( error ) 633 goto Exit; 634 635 FT_ZERO( stream ); 636 stream->memory = memory; 637 638 if ( !FT_QNEW( zip ) ) 639 { 640 error = ft_gzip_file_init( zip, stream, source ); 641 if ( error ) 642 { 643 FT_FREE( zip ); 644 goto Exit; 645 } 646 647 stream->descriptor.pointer = zip; 648 } 649 650 /* 651 * We use the following trick to try to dramatically improve the 652 * performance while dealing with small files. If the original stream 653 * size is less than a certain threshold, we try to load the whole font 654 * file into memory. This saves us from using the 32KB buffer needed 655 * to inflate the file, plus the two 4KB intermediate input/output 656 * buffers used in the `FT_GZipFile' structure. 657 */ 658 { 659 FT_ULong zip_size = ft_gzip_get_uncompressed_size( source ); 660 661 662 if ( zip_size != 0 && zip_size < 40 * 1024 ) 663 { 664 FT_Byte* zip_buff = NULL; 665 666 667 if ( !FT_ALLOC( zip_buff, zip_size ) ) 668 { 669 FT_ULong count; 670 671 672 count = ft_gzip_file_io( zip, 0, zip_buff, zip_size ); 673 if ( count == zip_size ) 674 { 675 ft_gzip_file_done( zip ); 676 FT_FREE( zip ); 677 678 stream->descriptor.pointer = NULL; 679 680 stream->size = zip_size; 681 stream->pos = 0; 682 stream->base = zip_buff; 683 stream->read = NULL; 684 stream->close = ft_gzip_stream_close; 685 686 goto Exit; 687 } 688 689 ft_gzip_file_io( zip, 0, NULL, 0 ); 690 FT_FREE( zip_buff ); 691 } 692 error = FT_Err_Ok; 693 } 694 695 if ( zip_size ) 696 stream->size = zip_size; 697 else 698 stream->size = 0x7FFFFFFFL; /* don't know the real size! */ 699 } 700 701 stream->pos = 0; 702 stream->base = NULL; 703 stream->read = ft_gzip_stream_io; 704 stream->close = ft_gzip_stream_close; 705 706 Exit: 707 return error; 708 } 709 710 711 /* documentation is in ftgzip.h */ 712 713 FT_EXPORT_DEF( FT_Error ) FT_Gzip_Uncompress(FT_Memory memory,FT_Byte * output,FT_ULong * output_len,const FT_Byte * input,FT_ULong input_len)714 FT_Gzip_Uncompress( FT_Memory memory, 715 FT_Byte* output, 716 FT_ULong* output_len, 717 const FT_Byte* input, 718 FT_ULong input_len ) 719 { 720 z_stream stream; 721 int err; 722 723 724 /* check for `input' delayed to `inflate' */ 725 726 if ( !memory || ! output_len || !output ) 727 return FT_THROW( Invalid_Argument ); 728 729 /* this function is modeled after zlib's `uncompress' function */ 730 731 stream.next_in = (Bytef*)input; 732 stream.avail_in = (uInt)input_len; 733 734 stream.next_out = output; 735 stream.avail_out = (uInt)*output_len; 736 737 stream.zalloc = (alloc_func)ft_gzip_alloc; 738 stream.zfree = (free_func) ft_gzip_free; 739 stream.opaque = memory; 740 741 err = inflateInit2( &stream, MAX_WBITS ); 742 if ( err != Z_OK ) 743 return FT_THROW( Invalid_Argument ); 744 745 err = inflate( &stream, Z_FINISH ); 746 if ( err != Z_STREAM_END ) 747 { 748 inflateEnd( &stream ); 749 if ( err == Z_OK ) 750 err = Z_BUF_ERROR; 751 } 752 else 753 { 754 *output_len = stream.total_out; 755 756 err = inflateEnd( &stream ); 757 } 758 759 if ( err == Z_MEM_ERROR ) 760 return FT_THROW( Out_Of_Memory ); 761 762 if ( err == Z_BUF_ERROR ) 763 return FT_THROW( Array_Too_Large ); 764 765 if ( err == Z_DATA_ERROR ) 766 return FT_THROW( Invalid_Table ); 767 768 return FT_Err_Ok; 769 } 770 771 772 #else /* !FT_CONFIG_OPTION_USE_ZLIB */ 773 774 FT_EXPORT_DEF( FT_Error ) FT_Stream_OpenGzip(FT_Stream stream,FT_Stream source)775 FT_Stream_OpenGzip( FT_Stream stream, 776 FT_Stream source ) 777 { 778 FT_UNUSED( stream ); 779 FT_UNUSED( source ); 780 781 return FT_THROW( Unimplemented_Feature ); 782 } 783 784 785 FT_EXPORT_DEF( FT_Error ) FT_Gzip_Uncompress(FT_Memory memory,FT_Byte * output,FT_ULong * output_len,const FT_Byte * input,FT_ULong input_len)786 FT_Gzip_Uncompress( FT_Memory memory, 787 FT_Byte* output, 788 FT_ULong* output_len, 789 const FT_Byte* input, 790 FT_ULong input_len ) 791 { 792 FT_UNUSED( memory ); 793 FT_UNUSED( output ); 794 FT_UNUSED( output_len ); 795 FT_UNUSED( input ); 796 FT_UNUSED( input_len ); 797 798 return FT_THROW( Unimplemented_Feature ); 799 } 800 801 #endif /* !FT_CONFIG_OPTION_USE_ZLIB */ 802 803 804 /* END */ 805