1 /**************************************************************************** 2 * 3 * ftbzip2.c 4 * 5 * FreeType support for .bz2 compressed files. 6 * 7 * This optional component relies on libbz2. It should mainly be used to 8 * parse compressed PCF fonts, as found with many X11 server 9 * distributions. 10 * 11 * Copyright (C) 2010-2023 by 12 * Joel Klinghed. 13 * 14 * based on `src/gzip/ftgzip.c' 15 * 16 * This file is part of the FreeType project, and may only be used, 17 * modified, and distributed under the terms of the FreeType project 18 * license, LICENSE.TXT. By continuing to use, modify, or distribute 19 * this file you indicate that you have read the license and 20 * understand and accept it fully. 21 * 22 */ 23 24 25 #include <freetype/internal/ftmemory.h> 26 #include <freetype/internal/ftstream.h> 27 #include <freetype/internal/ftdebug.h> 28 #include <freetype/ftbzip2.h> 29 #include FT_CONFIG_STANDARD_LIBRARY_H 30 31 32 #include <freetype/ftmoderr.h> 33 34 #undef FTERRORS_H_ 35 36 #undef FT_ERR_PREFIX 37 #define FT_ERR_PREFIX Bzip2_Err_ 38 #define FT_ERR_BASE FT_Mod_Err_Bzip2 39 40 #include <freetype/fterrors.h> 41 42 43 #ifdef FT_CONFIG_OPTION_USE_BZIP2 44 45 #define BZ_NO_STDIO /* Do not need FILE */ 46 #include <bzlib.h> 47 48 49 /***************************************************************************/ 50 /***************************************************************************/ 51 /***** *****/ 52 /***** B Z I P 2 M E M O R Y M A N A G E M E N T *****/ 53 /***** *****/ 54 /***************************************************************************/ 55 /***************************************************************************/ 56 57 /* it is better to use FreeType memory routines instead of raw 58 'malloc/free' */ 59 60 typedef void* (*alloc_func)( void*, int, int ); 61 typedef void (*free_func) ( void*, void* ); 62 63 64 static void* ft_bzip2_alloc(FT_Memory memory,int items,int size)65 ft_bzip2_alloc( FT_Memory memory, 66 int items, 67 int size ) 68 { 69 FT_ULong sz = (FT_ULong)size * (FT_ULong)items; 70 FT_Error error; 71 FT_Pointer p = NULL; 72 73 74 FT_MEM_QALLOC( p, sz ); 75 return p; 76 } 77 78 79 static void ft_bzip2_free(FT_Memory memory,void * address)80 ft_bzip2_free( FT_Memory memory, 81 void* address ) 82 { 83 FT_MEM_FREE( address ); 84 } 85 86 87 /***************************************************************************/ 88 /***************************************************************************/ 89 /***** *****/ 90 /***** B Z I P 2 F I L E D E S C R I P T O R *****/ 91 /***** *****/ 92 /***************************************************************************/ 93 /***************************************************************************/ 94 95 #define FT_BZIP2_BUFFER_SIZE 4096 96 97 typedef struct FT_BZip2FileRec_ 98 { 99 FT_Stream source; /* parent/source stream */ 100 FT_Stream stream; /* embedding stream */ 101 FT_Memory memory; /* memory allocator */ 102 bz_stream bzstream; /* bzlib input stream */ 103 104 FT_Byte input[FT_BZIP2_BUFFER_SIZE]; /* input read buffer */ 105 106 FT_Byte buffer[FT_BZIP2_BUFFER_SIZE]; /* output buffer */ 107 FT_ULong pos; /* position in output */ 108 FT_Byte* cursor; 109 FT_Byte* limit; 110 FT_Bool reset; /* reset before next read */ 111 112 } FT_BZip2FileRec, *FT_BZip2File; 113 114 115 /* check and skip .bz2 header - we don't support `transparent' compression */ 116 static FT_Error ft_bzip2_check_header(FT_Stream stream)117 ft_bzip2_check_header( FT_Stream stream ) 118 { 119 FT_Error error = FT_Err_Ok; 120 FT_Byte head[4]; 121 122 123 if ( FT_STREAM_SEEK( 0 ) || 124 FT_STREAM_READ( head, 4 ) ) 125 goto Exit; 126 127 /* head[0] && head[1] are the magic numbers; */ 128 /* head[2] is the version, and head[3] the blocksize */ 129 if ( head[0] != 0x42 || 130 head[1] != 0x5A || 131 head[2] != 0x68 ) /* only support bzip2 (huffman) */ 132 { 133 error = FT_THROW( Invalid_File_Format ); 134 goto Exit; 135 } 136 137 Exit: 138 return error; 139 } 140 141 142 static FT_Error ft_bzip2_file_init(FT_BZip2File zip,FT_Stream stream,FT_Stream source)143 ft_bzip2_file_init( FT_BZip2File zip, 144 FT_Stream stream, 145 FT_Stream source ) 146 { 147 bz_stream* bzstream = &zip->bzstream; 148 FT_Error error = FT_Err_Ok; 149 150 151 zip->stream = stream; 152 zip->source = source; 153 zip->memory = stream->memory; 154 155 zip->limit = zip->buffer + FT_BZIP2_BUFFER_SIZE; 156 zip->cursor = zip->limit; 157 zip->pos = 0; 158 zip->reset = 0; 159 160 /* check .bz2 header */ 161 { 162 stream = source; 163 164 error = ft_bzip2_check_header( stream ); 165 if ( error ) 166 goto Exit; 167 168 if ( FT_STREAM_SEEK( 0 ) ) 169 goto Exit; 170 } 171 172 /* initialize bzlib */ 173 bzstream->bzalloc = (alloc_func)ft_bzip2_alloc; 174 bzstream->bzfree = (free_func) ft_bzip2_free; 175 bzstream->opaque = zip->memory; 176 177 bzstream->avail_in = 0; 178 bzstream->next_in = (char*)zip->buffer; 179 180 if ( BZ2_bzDecompressInit( bzstream, 0, 0 ) != BZ_OK || 181 !bzstream->next_in ) 182 error = FT_THROW( Invalid_File_Format ); 183 184 Exit: 185 return error; 186 } 187 188 189 static void ft_bzip2_file_done(FT_BZip2File zip)190 ft_bzip2_file_done( FT_BZip2File zip ) 191 { 192 bz_stream* bzstream = &zip->bzstream; 193 194 195 BZ2_bzDecompressEnd( bzstream ); 196 197 /* clear the rest */ 198 bzstream->bzalloc = NULL; 199 bzstream->bzfree = NULL; 200 bzstream->opaque = NULL; 201 bzstream->next_in = NULL; 202 bzstream->next_out = NULL; 203 bzstream->avail_in = 0; 204 bzstream->avail_out = 0; 205 206 zip->memory = NULL; 207 zip->source = NULL; 208 zip->stream = NULL; 209 } 210 211 212 static FT_Error ft_bzip2_file_reset(FT_BZip2File zip)213 ft_bzip2_file_reset( FT_BZip2File zip ) 214 { 215 FT_Stream stream = zip->source; 216 FT_Error error; 217 218 219 if ( !FT_STREAM_SEEK( 0 ) ) 220 { 221 bz_stream* bzstream = &zip->bzstream; 222 223 224 BZ2_bzDecompressEnd( bzstream ); 225 226 bzstream->avail_in = 0; 227 bzstream->next_in = (char*)zip->input; 228 bzstream->avail_out = 0; 229 bzstream->next_out = (char*)zip->buffer; 230 231 zip->limit = zip->buffer + FT_BZIP2_BUFFER_SIZE; 232 zip->cursor = zip->limit; 233 zip->pos = 0; 234 zip->reset = 0; 235 236 BZ2_bzDecompressInit( bzstream, 0, 0 ); 237 } 238 239 return error; 240 } 241 242 243 static FT_Error ft_bzip2_file_fill_input(FT_BZip2File zip)244 ft_bzip2_file_fill_input( FT_BZip2File zip ) 245 { 246 bz_stream* bzstream = &zip->bzstream; 247 FT_Stream stream = zip->source; 248 FT_ULong size; 249 250 251 if ( stream->read ) 252 { 253 size = stream->read( stream, stream->pos, zip->input, 254 FT_BZIP2_BUFFER_SIZE ); 255 if ( size == 0 ) 256 { 257 zip->limit = zip->cursor; 258 return FT_THROW( Invalid_Stream_Operation ); 259 } 260 } 261 else 262 { 263 size = stream->size - stream->pos; 264 if ( size > FT_BZIP2_BUFFER_SIZE ) 265 size = FT_BZIP2_BUFFER_SIZE; 266 267 if ( size == 0 ) 268 { 269 zip->limit = zip->cursor; 270 return FT_THROW( Invalid_Stream_Operation ); 271 } 272 273 FT_MEM_COPY( zip->input, stream->base + stream->pos, size ); 274 } 275 stream->pos += size; 276 277 bzstream->next_in = (char*)zip->input; 278 bzstream->avail_in = size; 279 280 return FT_Err_Ok; 281 } 282 283 284 static FT_Error ft_bzip2_file_fill_output(FT_BZip2File zip)285 ft_bzip2_file_fill_output( FT_BZip2File zip ) 286 { 287 bz_stream* bzstream = &zip->bzstream; 288 FT_Error error = FT_Err_Ok; 289 290 291 zip->cursor = zip->buffer; 292 bzstream->next_out = (char*)zip->cursor; 293 bzstream->avail_out = FT_BZIP2_BUFFER_SIZE; 294 295 while ( bzstream->avail_out > 0 ) 296 { 297 int err; 298 299 300 if ( bzstream->avail_in == 0 ) 301 { 302 error = ft_bzip2_file_fill_input( zip ); 303 if ( error ) 304 break; 305 } 306 307 err = BZ2_bzDecompress( bzstream ); 308 309 if ( err != BZ_OK ) 310 { 311 zip->reset = 1; 312 313 if ( err == BZ_STREAM_END ) 314 { 315 zip->limit = (FT_Byte*)bzstream->next_out; 316 if ( zip->limit == zip->cursor ) 317 error = FT_THROW( Invalid_Stream_Operation ); 318 break; 319 } 320 else 321 { 322 zip->limit = zip->cursor; 323 error = FT_THROW( Invalid_Stream_Operation ); 324 break; 325 } 326 } 327 } 328 329 return error; 330 } 331 332 333 /* fill output buffer; `count' must be <= FT_BZIP2_BUFFER_SIZE */ 334 static FT_Error ft_bzip2_file_skip_output(FT_BZip2File zip,FT_ULong count)335 ft_bzip2_file_skip_output( FT_BZip2File zip, 336 FT_ULong count ) 337 { 338 FT_Error error = FT_Err_Ok; 339 340 341 for (;;) 342 { 343 FT_ULong delta = (FT_ULong)( zip->limit - zip->cursor ); 344 345 346 if ( delta >= count ) 347 delta = count; 348 349 zip->cursor += delta; 350 zip->pos += delta; 351 352 count -= delta; 353 if ( count == 0 ) 354 break; 355 356 error = ft_bzip2_file_fill_output( zip ); 357 if ( error ) 358 break; 359 } 360 361 return error; 362 } 363 364 365 static FT_ULong ft_bzip2_file_io(FT_BZip2File zip,FT_ULong pos,FT_Byte * buffer,FT_ULong count)366 ft_bzip2_file_io( FT_BZip2File zip, 367 FT_ULong pos, 368 FT_Byte* buffer, 369 FT_ULong count ) 370 { 371 FT_ULong result = 0; 372 FT_Error error; 373 374 375 /* Reset inflate stream if seeking backwards or bzip reported an error. */ 376 /* Yes, that is not too efficient, but it saves memory :-) */ 377 if ( pos < zip->pos || zip->reset ) 378 { 379 error = ft_bzip2_file_reset( zip ); 380 if ( error ) 381 goto Exit; 382 } 383 384 /* skip unwanted bytes */ 385 if ( pos > zip->pos ) 386 { 387 error = ft_bzip2_file_skip_output( zip, (FT_ULong)( pos - zip->pos ) ); 388 if ( error ) 389 goto Exit; 390 } 391 392 if ( count == 0 ) 393 goto Exit; 394 395 /* now read the data */ 396 for (;;) 397 { 398 FT_ULong delta; 399 400 401 delta = (FT_ULong)( zip->limit - zip->cursor ); 402 if ( delta >= count ) 403 delta = count; 404 405 FT_MEM_COPY( buffer, zip->cursor, delta ); 406 buffer += delta; 407 result += delta; 408 zip->cursor += delta; 409 zip->pos += delta; 410 411 count -= delta; 412 if ( count == 0 ) 413 break; 414 415 error = ft_bzip2_file_fill_output( zip ); 416 if ( error ) 417 break; 418 } 419 420 Exit: 421 return result; 422 } 423 424 425 /***************************************************************************/ 426 /***************************************************************************/ 427 /***** *****/ 428 /***** B Z E M B E D D I N G S T R E A M *****/ 429 /***** *****/ 430 /***************************************************************************/ 431 /***************************************************************************/ 432 433 static void ft_bzip2_stream_close(FT_Stream stream)434 ft_bzip2_stream_close( FT_Stream stream ) 435 { 436 FT_BZip2File zip = (FT_BZip2File)stream->descriptor.pointer; 437 FT_Memory memory = stream->memory; 438 439 440 if ( zip ) 441 { 442 /* finalize bzip file descriptor */ 443 ft_bzip2_file_done( zip ); 444 445 FT_FREE( zip ); 446 447 stream->descriptor.pointer = NULL; 448 } 449 } 450 451 452 static unsigned long ft_bzip2_stream_io(FT_Stream stream,unsigned long offset,unsigned char * buffer,unsigned long count)453 ft_bzip2_stream_io( FT_Stream stream, 454 unsigned long offset, 455 unsigned char* buffer, 456 unsigned long count ) 457 { 458 FT_BZip2File zip = (FT_BZip2File)stream->descriptor.pointer; 459 460 461 return ft_bzip2_file_io( zip, offset, buffer, count ); 462 } 463 464 465 FT_EXPORT_DEF( FT_Error ) FT_Stream_OpenBzip2(FT_Stream stream,FT_Stream source)466 FT_Stream_OpenBzip2( FT_Stream stream, 467 FT_Stream source ) 468 { 469 FT_Error error; 470 FT_Memory memory; 471 FT_BZip2File zip = NULL; 472 473 474 if ( !stream || !source ) 475 { 476 error = FT_THROW( Invalid_Stream_Handle ); 477 goto Exit; 478 } 479 480 memory = source->memory; 481 482 /* 483 * check the header right now; this prevents allocating unnecessary 484 * objects when we don't need them 485 */ 486 error = ft_bzip2_check_header( source ); 487 if ( error ) 488 goto Exit; 489 490 FT_ZERO( stream ); 491 stream->memory = memory; 492 493 if ( !FT_QNEW( zip ) ) 494 { 495 error = ft_bzip2_file_init( zip, stream, source ); 496 if ( error ) 497 { 498 FT_FREE( zip ); 499 goto Exit; 500 } 501 502 stream->descriptor.pointer = zip; 503 } 504 505 stream->size = 0x7FFFFFFFL; /* don't know the real size! */ 506 stream->pos = 0; 507 stream->base = NULL; 508 stream->read = ft_bzip2_stream_io; 509 stream->close = ft_bzip2_stream_close; 510 511 Exit: 512 return error; 513 } 514 515 #else /* !FT_CONFIG_OPTION_USE_BZIP2 */ 516 517 FT_EXPORT_DEF( FT_Error ) FT_Stream_OpenBzip2(FT_Stream stream,FT_Stream source)518 FT_Stream_OpenBzip2( FT_Stream stream, 519 FT_Stream source ) 520 { 521 FT_UNUSED( stream ); 522 FT_UNUSED( source ); 523 524 return FT_THROW( Unimplemented_Feature ); 525 } 526 527 #endif /* !FT_CONFIG_OPTION_USE_BZIP2 */ 528 529 530 /* END */ 531