1 /***************************************************************************/ 2 /* */ 3 /* ftgzip.c */ 4 /* */ 5 /* FreeType support for .gz compressed fileds */ 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 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 #include <ft2build.h> 23 #include FT_INTERNAL_MEMORY_H 24 #include FT_INTERNAL_STREAM_H 25 #include FT_INTERNAL_DEBUG_H 26 #include <string.h> 27 28 #ifdef FT_CONFIG_OPTION_USE_ZLIB 29 30 #ifdef FT_CONFIG_OPTION_SYSTEM_ZLIB 31 32 # include "zlib.h" 33 34 #else /* !SYSTEM_ZLIB */ 35 36 /* in this case, we include our own modified sources of the ZLib */ 37 /* within the "ftgzip" component. The modifications were necessary */ 38 /* to #include all files without conflicts, as well as preventing */ 39 /* the definition of "extern" functions that may cause linking */ 40 /* conflicts when a program is linked with both FreeType and the */ 41 /* original ZLib */ 42 43 # define NO_DUMMY_DECL 44 # define BUILDFIXED /* save code size */ 45 # define MY_ZCALLOC 46 47 # include "zlib.h" 48 49 # undef SLOW 50 # define SLOW 1 /* we can't use asm-optimized sources here !! */ 51 52 # include "zutil.c" 53 # include "inftrees.c" 54 # include "infcodes.c" 55 # include "infutil.c" 56 # include "infblock.c" 57 # include "inflate.c" 58 # include "adler32.c" 59 60 #endif /* !SYSTEM_ZLIB */ 61 62 63 /***************************************************************************/ 64 /***************************************************************************/ 65 /***** *****/ 66 /***** Z L I B M E M O R Y M A N A G E M E N T *****/ 67 /***** *****/ 68 /***************************************************************************/ 69 /***************************************************************************/ 70 71 /* it's better to use FreeType memory routines instead of raw 'malloc/free' */ 72 73 74 static voidpf ft_gzip_alloc(FT_Memory memory,uInt items,uInt size)75 ft_gzip_alloc( FT_Memory memory, 76 uInt items, 77 uInt size ) 78 { 79 FT_ULong sz = (FT_ULong)size * items; 80 FT_Pointer p; 81 82 FT_MEM_ALLOC( p, sz ); 83 84 return (voidpf) p; 85 } 86 87 88 static void ft_gzip_free(FT_Memory memory,voidpf address)89 ft_gzip_free( FT_Memory memory, 90 voidpf address ) 91 { 92 FT_MEM_FREE( address ); 93 } 94 95 96 #ifndef FT_CONFIG_OPTION_SYSTEM_ZLIB 97 98 local voidpf zcalloc(opaque,items,size)99 zcalloc (opaque, items, size) 100 voidpf opaque; 101 unsigned items; 102 unsigned size; 103 { 104 return ft_gzip_alloc( opaque, items, size ); 105 } 106 107 local void zcfree(voidpf opaque,voidpf ptr)108 zcfree( voidpf opaque, 109 voidpf ptr ) 110 { 111 ft_gzip_free( opaque, ptr ); 112 } 113 114 #endif /* !SYSTEM_ZLIB */ 115 116 117 /***************************************************************************/ 118 /***************************************************************************/ 119 /***** *****/ 120 /***** Z L I B F I L E D E S C R I P T O R *****/ 121 /***** *****/ 122 /***************************************************************************/ 123 /***************************************************************************/ 124 125 #define FT_GZIP_BUFFER_SIZE 4096 126 127 typedef struct FT_GZipFileRec_ 128 { 129 FT_Stream source; /* parent/source stream */ 130 FT_Stream stream; /* embedding stream */ 131 FT_Memory memory; /* memory allocator */ 132 z_stream zstream; /* zlib input stream */ 133 134 FT_ULong start; /* starting position, after .gz header */ 135 FT_Byte input[ FT_GZIP_BUFFER_SIZE ]; /* input read buffer */ 136 137 FT_Byte buffer[ FT_GZIP_BUFFER_SIZE ]; /* output buffer */ 138 FT_ULong pos; /* position in output */ 139 FT_Byte* cursor; 140 FT_Byte* limit; 141 142 } FT_GZipFileRec, *FT_GZipFile; 143 144 145 /* gzip flag byte */ 146 #define FT_GZIP_ASCII_FLAG 0x01 /* bit 0 set: file probably ascii text */ 147 #define FT_GZIP_HEAD_CRC 0x02 /* bit 1 set: header CRC present */ 148 #define FT_GZIP_EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ 149 #define FT_GZIP_ORIG_NAME 0x08 /* bit 3 set: original file name present */ 150 #define FT_GZIP_COMMENT 0x10 /* bit 4 set: file comment present */ 151 #define FT_GZIP_RESERVED 0xE0 /* bits 5..7: reserved */ 152 153 154 /* check and skip .gz header - we don't support "transparent" compression */ 155 static FT_Error ft_gzip_check_header(FT_Stream stream)156 ft_gzip_check_header( FT_Stream stream ) 157 { 158 FT_Error error; 159 FT_Byte head[4]; 160 161 if ( FT_STREAM_SEEK( 0 ) || 162 FT_STREAM_READ( head, 4 ) ) 163 goto Exit; 164 165 /* head[0] && head[1] are the magic numbers */ 166 /* head[2] is the method, and head[3] the flags */ 167 if ( head[0] != 0x1f || 168 head[1] != 0x8b || 169 head[2] != Z_DEFLATED || 170 (head[3] & FT_GZIP_RESERVED) ) 171 { 172 error = FT_Err_Invalid_File_Format; 173 goto Exit; 174 } 175 176 /* skip time, xflags and os code */ 177 (void)FT_STREAM_SKIP( 6 ); 178 179 /* skip the extra field */ 180 if ( head[3] && FT_GZIP_EXTRA_FIELD ) 181 { 182 FT_UInt len; 183 184 if ( FT_READ_USHORT_LE( len ) || 185 FT_STREAM_SKIP( len ) ) 186 goto Exit; 187 } 188 189 /* skip original file name */ 190 if ( head[3] && FT_GZIP_ORIG_NAME ) 191 for (;;) 192 { 193 FT_UInt c; 194 195 if ( FT_READ_BYTE( c) ) 196 goto Exit; 197 198 if ( c == 0 ) 199 break; 200 } 201 202 /* skip .gz comment */ 203 if ( head[3] & FT_GZIP_COMMENT ) 204 for (;;) 205 { 206 FT_UInt c; 207 208 if ( FT_READ_BYTE( c) ) 209 goto Exit; 210 211 if ( c == 0 ) 212 break; 213 } 214 215 /* skip CRC */ 216 if ( head[3] & FT_GZIP_HEAD_CRC ) 217 if ( FT_STREAM_SKIP( 2 ) ) 218 goto Exit; 219 220 Exit: 221 return error; 222 } 223 224 225 226 static FT_Error ft_gzip_file_init(FT_GZipFile zip,FT_Stream stream,FT_Stream source)227 ft_gzip_file_init( FT_GZipFile zip, 228 FT_Stream stream, 229 FT_Stream source ) 230 { 231 z_stream* zstream = &zip->zstream; 232 FT_Error error = 0; 233 234 zip->stream = stream; 235 zip->source = source; 236 zip->memory = stream->memory; 237 238 zip->limit = zip->buffer + FT_GZIP_BUFFER_SIZE; 239 zip->cursor = zip->limit; 240 zip->pos = 0; 241 242 /* check and skip .gz header */ 243 { 244 stream = source; 245 246 error = ft_gzip_check_header( stream ); 247 if (error) 248 goto Exit; 249 250 zip->start = FT_STREAM_POS(); 251 } 252 253 /* initialize zlib - there is no zlib header in the compressed stream */ 254 zstream->zalloc = (alloc_func) ft_gzip_alloc; 255 zstream->zfree = (free_func) ft_gzip_free; 256 zstream->opaque = stream->memory; 257 258 zstream->avail_in = 0; 259 zstream->next_in = zip->buffer; 260 261 if ( inflateInit2( zstream, -MAX_WBITS ) != Z_OK || 262 zstream->next_in == NULL ) 263 { 264 error = FT_Err_Invalid_File_Format; 265 goto Exit; 266 } 267 268 Exit: 269 return error; 270 } 271 272 273 274 static void ft_gzip_file_done(FT_GZipFile zip)275 ft_gzip_file_done( FT_GZipFile zip ) 276 { 277 z_stream* zstream = &zip->zstream; 278 279 /* clear the rest */ 280 zstream->zalloc = NULL; 281 zstream->zfree = NULL; 282 zstream->opaque = NULL; 283 zstream->next_in = NULL; 284 zstream->next_out = NULL; 285 zstream->avail_in = 0; 286 zstream->avail_out = 0; 287 288 zip->memory = NULL; 289 zip->source = NULL; 290 zip->stream = NULL; 291 } 292 293 294 static FT_Error ft_gzip_file_reset(FT_GZipFile zip)295 ft_gzip_file_reset( FT_GZipFile zip ) 296 { 297 FT_Stream stream = zip->source; 298 FT_Error error; 299 300 if ( !FT_STREAM_SEEK( zip->start ) ) 301 { 302 z_stream* zstream = &zip->zstream; 303 304 inflateReset( zstream ); 305 306 zstream->avail_in = 0; 307 zstream->next_in = zip->input; 308 zstream->avail_out = 0; 309 zstream->next_out = zip->buffer; 310 311 zip->limit = zip->buffer + FT_GZIP_BUFFER_SIZE; 312 zip->cursor = zip->limit; 313 zip->pos = 0; 314 } 315 return error; 316 } 317 318 319 static FT_Error ft_gzip_file_fill_input(FT_GZipFile zip)320 ft_gzip_file_fill_input( FT_GZipFile zip ) 321 { 322 z_stream* zstream = &zip->zstream; 323 FT_Stream stream = zip->source; 324 FT_ULong size; 325 326 if ( stream->read ) 327 { 328 size = stream->read( stream, stream->pos, zip->input, FT_GZIP_BUFFER_SIZE ); 329 if ( size == 0 ) 330 return FT_Err_Invalid_Stream_Operation; 331 } 332 else 333 { 334 size = stream->size - stream->pos; 335 if ( size > FT_GZIP_BUFFER_SIZE ) 336 size = FT_GZIP_BUFFER_SIZE; 337 338 if ( size == 0 ) 339 return FT_Err_Invalid_Stream_Operation; 340 341 FT_MEM_COPY( zip->input, stream->base + stream->pos, size ); 342 } 343 stream->pos += size; 344 345 zstream->next_in = zip->input; 346 zstream->avail_in = size; 347 348 return 0; 349 } 350 351 352 353 static FT_Error ft_gzip_file_fill_output(FT_GZipFile zip)354 ft_gzip_file_fill_output( FT_GZipFile zip ) 355 { 356 z_stream* zstream = &zip->zstream; 357 FT_Error error = 0; 358 359 zip->cursor = zip->buffer; 360 zstream->next_out = zip->cursor; 361 zstream->avail_out = FT_GZIP_BUFFER_SIZE; 362 363 while ( zstream->avail_out > 0 ) 364 { 365 int err; 366 367 if ( zstream->avail_in == 0 ) 368 { 369 error = ft_gzip_file_fill_input( zip ); 370 if ( error ) 371 break; 372 } 373 374 err = inflate( zstream, Z_NO_FLUSH ); 375 376 if ( err == Z_STREAM_END ) 377 { 378 zip->limit = zstream->next_out; 379 break; 380 } 381 else if ( err != Z_OK ) 382 { 383 error = FT_Err_Invalid_Stream_Operation; 384 break; 385 } 386 } 387 return error; 388 } 389 390 391 /* fill output buffer, 'count' must be <= FT_GZIP_BUFFER_SIZE */ 392 static FT_Error ft_gzip_file_skip_output(FT_GZipFile zip,FT_ULong count)393 ft_gzip_file_skip_output( FT_GZipFile zip, 394 FT_ULong count ) 395 { 396 FT_Error error = 0; 397 FT_ULong delta; 398 399 for (;;) 400 { 401 delta = (FT_ULong)( zip->limit - zip->cursor ); 402 if ( delta >= count ) 403 delta = count; 404 405 zip->cursor += delta; 406 zip->pos += delta; 407 408 count -= delta; 409 if ( count == 0 ) 410 break; 411 412 error = ft_gzip_file_fill_output( zip ); 413 if ( error ) 414 break; 415 } 416 417 return error; 418 } 419 420 421 static FT_ULong ft_gzip_file_io(FT_GZipFile zip,FT_ULong pos,FT_Byte * buffer,FT_ULong count)422 ft_gzip_file_io( FT_GZipFile zip, 423 FT_ULong pos, 424 FT_Byte* buffer, 425 FT_ULong count ) 426 { 427 FT_ULong result = 0; 428 FT_Error error; 429 430 /* reset inflate stream if we're seeking backwards */ 431 /* yes, that's not too efficient, but it saves memory :-) */ 432 if ( pos < zip->pos ) 433 { 434 error = ft_gzip_file_reset( zip ); 435 if ( error ) goto Exit; 436 } 437 438 /* skip unwanted bytes */ 439 if ( pos > zip->pos ) 440 { 441 error = ft_gzip_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) ); 442 if (error) 443 goto Exit; 444 } 445 446 if ( count == 0 ) 447 goto Exit; 448 449 /* now read the data */ 450 for (;;) 451 { 452 FT_ULong delta; 453 454 delta = (FT_ULong)( zip->limit - zip->cursor ); 455 if ( delta >= count ) 456 delta = count; 457 458 FT_MEM_COPY( buffer, zip->cursor, delta ); 459 buffer += delta; 460 result += delta; 461 zip->cursor += delta; 462 zip->pos += delta; 463 464 count -= delta; 465 if ( count == 0 ) 466 break; 467 468 error = ft_gzip_file_fill_output( zip ); 469 if (error) 470 break; 471 } 472 473 Exit: 474 return result; 475 } 476 477 478 /***************************************************************************/ 479 /***************************************************************************/ 480 /***** *****/ 481 /***** G Z E M B E D D I N G S T R E A M *****/ 482 /***** *****/ 483 /***************************************************************************/ 484 /***************************************************************************/ 485 486 static void ft_gzip_stream_close(FT_Stream stream)487 ft_gzip_stream_close( FT_Stream stream ) 488 { 489 FT_GZipFile zip = stream->descriptor.pointer; 490 FT_Memory memory = stream->memory; 491 492 if ( zip ) 493 { 494 /* finalize gzip file descriptor */ 495 ft_gzip_file_done( zip ); 496 497 FT_FREE( zip ); 498 499 stream->descriptor.pointer = NULL; 500 } 501 } 502 503 504 static FT_ULong ft_gzip_stream_io(FT_Stream stream,FT_ULong pos,FT_Byte * buffer,FT_ULong count)505 ft_gzip_stream_io( FT_Stream stream, 506 FT_ULong pos, 507 FT_Byte* buffer, 508 FT_ULong count ) 509 { 510 FT_GZipFile zip = stream->descriptor.pointer; 511 512 return ft_gzip_file_io( zip, pos, buffer, count ); 513 } 514 515 516 FT_EXPORT_DEF( FT_Error ) FT_Stream_OpenGzip(FT_Stream stream,FT_Stream source)517 FT_Stream_OpenGzip( FT_Stream stream, 518 FT_Stream source ) 519 { 520 FT_Error error; 521 FT_Memory memory = source->memory; 522 FT_GZipFile zip; 523 524 FT_ZERO( stream ); 525 stream->memory = memory; 526 527 if ( !FT_NEW( zip ) ) 528 { 529 error = ft_gzip_file_init( zip, stream, source ); 530 if ( error ) 531 { 532 FT_FREE( zip ); 533 goto Exit; 534 } 535 536 stream->descriptor.pointer = zip; 537 } 538 539 stream->size = 0x7FFFFFFF; /* don't know the real size !! */ 540 stream->pos = 0; 541 stream->base = 0; 542 stream->read = ft_gzip_stream_io; 543 stream->close = ft_gzip_stream_close; 544 545 Exit: 546 return error; 547 } 548 549 #else /* !FT_CONFIG_OPTION_USE_ZLIB */ 550 551 FT_EXPORT_DEF( FT_Error ) FT_Stream_OpenGzip(FT_Stream stream,FT_Stream source)552 FT_Stream_OpenGzip( FT_Stream stream, 553 FT_Stream source ) 554 { 555 FT_UNUSED( stream ); 556 FT_UNUSED( source ); 557 558 return FT_Err_Unimplemented_Feature; 559 } 560 561 #endif /* !FT_CONFIG_OPTION_USE_ZLIB */ 562