1 /***************************************************************************
2   che3.h  -  HE3 compression and decompression (for old format filelists)
3                              -------------------
4     begin                : ?
5     copyright            : (C) 2001 Eric Prevoteau
6     copyright            : (C) 2002-2005 Mathias Küster
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef __HE3_H__
19 #define __HE3_H__
20 
21 /**
22  * This class compresses and decompresses ".DcLst" old format
23  * filelists, which are based on Huffman encoding.
24  *
25  * The compression is probably worse than zlib, plus it
26  * seems to be orientated on bits rather than bytes making
27  * it horrible to implement.
28  */
29 #include <dclib/dcos.h>
30 
31 class CByteArray;
32 class CString;
33 
34 typedef struct hufnode
35 {
36 	unsigned long occur;
37 	struct hufnode *left,*right;		/* son nodes (left and right are both either not null or either null) */
38 	unsigned char val;					/* if left&right!=NULL, val is the encoded character, else, it has no usage */
39 } HUFNODE;
40 
41 typedef struct
42 {
43 	unsigned int bits_len;				/* number of bits in the bitfield used to encode the value */
44 	unsigned long bits;					/* bitfield containing the encoding pattern */
45 												/* a N bits_len pattern is stored inside bits from bit N-1 to bit 0 */
46 } HUFENCODE;
47 
48 class CHE3 {
49 public:
50 	/**************************************************/
51 	/* decompress data compressed using HE3 algorithm */
52 	/**********************************************************/
53 	/* input: a GByteArray containing HE3 compressed data     */
54 	/* output: a GString containing uncompressed data or NULL */
55 	/**********************************************************/
56 	CString *decode_he3_data(CByteArray *data);
57 
58 	/*****************************************************************************************/
59 	/* compress data compressed using an Huffman algorithm and store it in HE3 usable format */
60 	/*****************************************************************************************/
61 	/* input: a GString containing a string to compress        */
62 	/* output: a GByteArray containing compressed data or NULL */
63 	/***********************************************************/
64 	CByteArray *encode_he3_data(const CString *str);
65 
66 private:
67 	unsigned long get_bit(unsigned char *data, unsigned long *cur_pos);
68 	unsigned long get_bits(unsigned char *data, unsigned long *cur_pos, int nb_bit);
69 	static int huf_insert_glist(void * a, void * b);
70 	void use_hufnode(HUFENCODE tbl_enc[256], HUFNODE *node,unsigned int bits_len, unsigned long bits);
71 	void free_hufnode(HUFNODE *node);
72 	CByteArray *add_bit(CByteArray *data, unsigned long *bit_pos, unsigned char bit_value);
73 	CByteArray *add_bits(CByteArray *data, unsigned long *bit_pos, unsigned long pattern, unsigned int pattern_length);
74 };
75 
76 #endif
77