OLZW.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. // Filename : OLZW.H
  21. // Description : header file of LZW compression/decompression
  22. #ifndef __OLZW_H
  23. #define __OLZW_H
  24. struct Dictionary;
  25. class File;
  26. class BitStream // for reading token less than 16 bits
  27. {
  28. public:
  29. long bit_offset;
  30. public:
  31. BitStream();
  32. virtual ~BitStream();
  33. virtual unsigned short input_bits(unsigned stringLen);
  34. virtual void output_bits(unsigned short stringCode, unsigned stringLen);
  35. };
  36. class BitMemStream : public BitStream
  37. {
  38. protected:
  39. unsigned char *bytePtr;
  40. public:
  41. BitMemStream(unsigned char *p);
  42. unsigned short input_bits(unsigned stringLen);
  43. void output_bits(unsigned short stringCode, unsigned stringLen);
  44. };
  45. class BitFileRead : public BitStream
  46. {
  47. protected:
  48. File *filePtr;
  49. long last_offset;
  50. unsigned long residue; // always the 4 bytes at last_offset
  51. public:
  52. BitFileRead(File *f);
  53. unsigned short input_bits(unsigned stringLen);
  54. void output_bits(unsigned short stringCode, unsigned stringLen);
  55. };
  56. class BitFileWrite : public BitStream
  57. {
  58. protected:
  59. File *filePtr;
  60. long residue_len;
  61. unsigned long residue; // always the 4 bytes at last_offset
  62. public:
  63. BitFileWrite(File *f);
  64. ~BitFileWrite();
  65. unsigned short input_bits(unsigned stringLen);
  66. void output_bits(unsigned short stringCode, unsigned stringLen);
  67. };
  68. class Lzw
  69. {
  70. private:
  71. Dictionary *dict;
  72. unsigned char *decode_stack;
  73. unsigned short next_code;
  74. unsigned current_code_bits;
  75. unsigned short next_bump_code;
  76. public:
  77. Lzw();
  78. ~Lzw();
  79. void initialize_storage();
  80. void free_storage();
  81. // nul output, to find output size
  82. long compress( unsigned char *inPtr, long inByteLen);
  83. // compressed data in memory
  84. long compress( unsigned char *inPtr, long inByteLen, unsigned char *outPtr);
  85. long expand( unsigned char *inPtr, long inBitLen, unsigned char *outPtr);
  86. // compressed data in file
  87. long compress( unsigned char *inPtr, long inByteLen, File *outFile);
  88. long expand( File *inFile, unsigned char *outPtr);
  89. long basic_compress( unsigned char *inPtr, long inByteLen, BitStream *outStream);
  90. long basic_expand( BitStream *inStream, unsigned char *outPtr);
  91. private:
  92. void initialize_dictionary();
  93. unsigned short find_child_node( unsigned short parent_code, unsigned char child_character );
  94. unsigned int decode_string( unsigned int count, unsigned short code );
  95. };
  96. #endif