/src/freetype/src/lzw/ftzopen.h

https://bitbucket.org/cabalistic/ogredeps/ · C++ Header · 171 lines · 60 code · 32 blank · 79 comment · 0 complexity · 07a957315bd99dcc8a1fada6419f712b MD5 · raw file

  1. /***************************************************************************/
  2. /* */
  3. /* ftzopen.h */
  4. /* */
  5. /* FreeType support for .Z compressed files. */
  6. /* */
  7. /* This optional component relies on NetBSD's zopen(). It should mainly */
  8. /* be used to parse compressed PCF fonts, as found with many X11 server */
  9. /* distributions. */
  10. /* */
  11. /* Copyright 2005, 2006, 2007, 2008 by David Turner. */
  12. /* */
  13. /* This file is part of the FreeType project, and may only be used, */
  14. /* modified, and distributed under the terms of the FreeType project */
  15. /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
  16. /* this file you indicate that you have read the license and */
  17. /* understand and accept it fully. */
  18. /* */
  19. /***************************************************************************/
  20. #ifndef __FT_ZOPEN_H__
  21. #define __FT_ZOPEN_H__
  22. #include <ft2build.h>
  23. #include FT_FREETYPE_H
  24. /*
  25. * This is a complete re-implementation of the LZW file reader,
  26. * since the old one was incredibly badly written, using
  27. * 400 KByte of heap memory before decompressing anything.
  28. *
  29. */
  30. #define FT_LZW_IN_BUFF_SIZE 64
  31. #define FT_LZW_DEFAULT_STACK_SIZE 64
  32. #define LZW_INIT_BITS 9
  33. #define LZW_MAX_BITS 16
  34. #define LZW_CLEAR 256
  35. #define LZW_FIRST 257
  36. #define LZW_BIT_MASK 0x1f
  37. #define LZW_BLOCK_MASK 0x80
  38. #define LZW_MASK( n ) ( ( 1U << (n) ) - 1U )
  39. typedef enum FT_LzwPhase_
  40. {
  41. FT_LZW_PHASE_START = 0,
  42. FT_LZW_PHASE_CODE,
  43. FT_LZW_PHASE_STACK,
  44. FT_LZW_PHASE_EOF
  45. } FT_LzwPhase;
  46. /*
  47. * state of LZW decompressor
  48. *
  49. * small technical note
  50. * --------------------
  51. *
  52. * We use a few tricks in this implementation that are explained here to
  53. * ease debugging and maintenance.
  54. *
  55. * - First of all, the `prefix' and `suffix' arrays contain the suffix
  56. * and prefix for codes over 256; this means that
  57. *
  58. * prefix_of(code) == state->prefix[code-256]
  59. * suffix_of(code) == state->suffix[code-256]
  60. *
  61. * Each prefix is a 16-bit code, and each suffix an 8-bit byte.
  62. *
  63. * Both arrays are stored in a single memory block, pointed to by
  64. * `state->prefix'. This means that the following equality is always
  65. * true:
  66. *
  67. * state->suffix == (FT_Byte*)(state->prefix + state->prefix_size)
  68. *
  69. * Of course, state->prefix_size is the number of prefix/suffix slots
  70. * in the arrays, corresponding to codes 256..255+prefix_size.
  71. *
  72. * - `free_ent' is the index of the next free entry in the `prefix'
  73. * and `suffix' arrays. This means that the corresponding `next free
  74. * code' is really `256+free_ent'.
  75. *
  76. * Moreover, `max_free' is the maximum value that `free_ent' can reach.
  77. *
  78. * `max_free' corresponds to `(1 << max_bits) - 256'. Note that this
  79. * value is always <= 0xFF00, which means that both `free_ent' and
  80. * `max_free' can be stored in an FT_UInt variable, even on 16-bit
  81. * machines.
  82. *
  83. * If `free_ent == max_free', you cannot add new codes to the
  84. * prefix/suffix table.
  85. *
  86. * - `num_bits' is the current number of code bits, starting at 9 and
  87. * growing each time `free_ent' reaches the value of `free_bits'. The
  88. * latter is computed as follows
  89. *
  90. * if num_bits < max_bits:
  91. * free_bits = (1 << num_bits)-256
  92. * else:
  93. * free_bits = max_free + 1
  94. *
  95. * Since the value of `max_free + 1' can never be reached by
  96. * `free_ent', `num_bits' cannot grow larger than `max_bits'.
  97. */
  98. typedef struct FT_LzwStateRec_
  99. {
  100. FT_LzwPhase phase;
  101. FT_Int in_eof;
  102. FT_Byte buf_tab[16];
  103. FT_Int buf_offset;
  104. FT_Int buf_size;
  105. FT_Bool buf_clear;
  106. FT_Offset buf_total;
  107. FT_UInt max_bits; /* max code bits, from file header */
  108. FT_Int block_mode; /* block mode flag, from file header */
  109. FT_UInt max_free; /* (1 << max_bits) - 256 */
  110. FT_UInt num_bits; /* current code bit number */
  111. FT_UInt free_ent; /* index of next free entry */
  112. FT_UInt free_bits; /* if reached by free_ent, increment num_bits */
  113. FT_UInt old_code;
  114. FT_UInt old_char;
  115. FT_UInt in_code;
  116. FT_UShort* prefix; /* always dynamically allocated / reallocated */
  117. FT_Byte* suffix; /* suffix = (FT_Byte*)(prefix + prefix_size) */
  118. FT_UInt prefix_size; /* number of slots in `prefix' or `suffix' */
  119. FT_Byte* stack; /* character stack */
  120. FT_UInt stack_top;
  121. FT_Offset stack_size;
  122. FT_Byte stack_0[FT_LZW_DEFAULT_STACK_SIZE]; /* minimize heap alloc */
  123. FT_Stream source; /* source stream */
  124. FT_Memory memory;
  125. } FT_LzwStateRec, *FT_LzwState;
  126. FT_LOCAL( void )
  127. ft_lzwstate_init( FT_LzwState state,
  128. FT_Stream source );
  129. FT_LOCAL( void )
  130. ft_lzwstate_done( FT_LzwState state );
  131. FT_LOCAL( void )
  132. ft_lzwstate_reset( FT_LzwState state );
  133. FT_LOCAL( FT_ULong )
  134. ft_lzwstate_io( FT_LzwState state,
  135. FT_Byte* buffer,
  136. FT_ULong out_size );
  137. /* */
  138. #endif /* __FT_ZOPEN_H__ */
  139. /* END */