ftgzip.c 14 KB

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