summaryrefslogtreecommitdiffstats
path: root/debian/htdig/htdig-3.2.0b6/htword/WordBitCompress.h
blob: 19f2c336be71c8983543f0589ab38e7b56e920ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
//
// WordBitCompress.h
//
// BitStream: put and get bits into a buffer
//           *tagging:  add tags to keep track of the position of data 
//                      inside the bitstream for debuging purposes.
//           *freezing: saves current position. further inserts in the BitStream
//                      aren't really done. This way you can try different
//                      compression algorithms and chose the best.
//
// Compressor: BitStream with extended fuctionalities
//
//
// Part of the ht://Dig package   <http://www.htdig.org/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: WordBitCompress.h,v 1.7 2004/05/28 13:15:26 lha Exp $
//

#ifndef   _WordBitCompress_h
#define  _WordBitCompress_h

#include<stdio.h>
#include<stdlib.h>
#include"HtVector_int.h"
#include"HtMaxMin.h"

typedef unsigned char byte;
// ******** HtVector_byte (header)
#define GType byte
#define HtVectorGType HtVector_byte
#include "HtVectorGeneric.h"

typedef char * charptr;
// ******** HtVector_charptr (header)
#define GType charptr
#define HtVectorGType HtVector_charptr
#include "HtVectorGeneric.h"


// ******** Utility inline functions and macros

// error checking
#define FATAL_ABORT fflush(stdout);fprintf(stderr,"FATAL ERROR at file:%s line:%d !!!\n",__FILE__,__LINE__);fflush(stderr);(*(int *)NULL)=1
#define errr(s) {fprintf(stderr,"FATAL ERROR:%s\n",s);FATAL_ABORT;}
#define CHECK_MEM(p) if(!p) errr("mifluz: Out of memory!");
// max/min of 2 values
#define TMax(a,b) (((a)>(b)) ? (a) : (b))
#define TMin(a,b) (((a)<(b)) ? (a) : (b))

// compute integer log2
// == minimum number of bits needed to code value 
inline int
num_bits(unsigned int maxval )
{
    unsigned int mv=maxval;
    int nbits;
    for(nbits=0;mv;nbits++){mv>>=1;}
    return(nbits);
}
// compute 2^x
#define pow2(x) (1<<(x))


// function declarations
char *label_str(const char *s,int n);
void  show_bits(int v,int n=16);

//  unsigned short max_v(unsigned short *vals,int n);
//  unsigned int   max_v(unsigned int   *vals,int n);
//  unsigned short min_v(unsigned short *vals,int n);
//  unsigned int   min_v(unsigned int   *vals,int n);





// **************************************************
// *************** BitStream  ***********************
// **************************************************
//  compression is done in Compressor not in BitStream
class BitStream
{
protected:

    // the buffer were the bitstream is stored
    HtVector_byte buff;

    // current bit position within the buffer
    int bitpos;

    // tags for debuging
    HtVector_int tagpos;
    HtVector_charptr tags;
    int use_tags;

    // freezing the bitstream
    HtVector_int freeze_stack;
    int freezeon;
public:
    void freeze();
    int unfreeze();

    // puts a bit into the bitstream
    inline void put(unsigned int v)
    {
	// SPEED CRITICAL SECTION
	if(freezeon){bitpos++;return;}
	if(v){buff.back()|=pow2(bitpos & 0x07);}
	bitpos++;
	if(!(bitpos & 0x07))// new byte
	{
	    buff.push_back(0);
	}
    }	
    inline void put(unsigned int v,const char *tag)
    {
	if(!freezeon){add_tag(tag);}
	put(v);
    }

    // gets a bit from the bitstream
    inline byte get(const char *tag=(char*)NULL)
    {
	// SPEED CRITICAL SECTION
	if(check_tag(tag)==NOTOK){errr("BitStream::get() check_tag failed");}
	if(bitpos>=(buff.size()<<3)){errr("BitStream::get reading past end of BitStream!");}
	byte res=buff[bitpos>>3] & pow2(bitpos & 0x07);
//  	printf("get:res:%d bitpos:%5d/%d buff[%3d]=%x\n",res,bitpos,bitpos%8,bitpos/8,buff[bitpos/8]);
	bitpos++;
	return(res);
    }

    // get/put an integer using n bits
    void         put_uint(unsigned int v,int n,const char *tag=(char*)"NOTAG");
    unsigned int get_uint(               int n,const char *tag=(char*)NULL);

    // get/put n bits of data stored in vals
    void put_zone(byte *vals,int n,const char *tag);
    void get_zone(byte *vals,int n,const char *tag);

    // 
    inline void add_tag(const char *tag)
    {
	if(!use_tags || !tag || freezeon){return;}
	add_tag1(tag);
    }
    void add_tag1(const char *tag);
    inline int  check_tag(const char *tag,int pos=-1)
    {
	if(!use_tags || !tag){return OK;}	
	return(check_tag1(tag,pos));
    }
    int  check_tag1(const char *tag,int pos);
    void set_use_tags(){use_tags=1;}
    int  find_tag(const char *tag);
    int  find_tag(int pos,int posaftertag=1);

    void show_bits(int a,int n);
    void show(int a=0,int n=-1);

    // position accesors
    int size(){return(bitpos);}
    int buffsize(){return(buff.size());}

    // get a copy of the buffer
    byte *get_data();
    // set the buffer from outside data (current buffer must be empty)
    void set_data(const byte *nbuff,int nbits);
      
    // use this for reading a BitStream after you have written in it 
    // (generally for debuging)
    void rewind(){bitpos=0;}

    ~BitStream()
    {
	int i;
	for(i=0;i<tags.size();i++){free(tags[i]);}
    }
    BitStream(int size0)
    {
	buff.reserve((size0+7)/8);
	init();
    }
    BitStream()
    {
	init();
    }
 private:
    void init()
    {
	bitpos=0;
	buff.push_back(0);
	freezeon=0;
	use_tags=0;
    }
};


// **************************************************
// *************** Compressor ***********************
// **************************************************

// Constants used by Compressor
// number of bits to code the number of values in an array 
#define NBITS_NVALS 16
// number of bits to code the values in an unsigned int array (=sizeof(unsigned int))
#define NBITS_VAL 32
// number of bits to code he number of bits used by an unsigned int value
#define NBITS_NBITS_VAL  5
// number of bits to code the number of bits used by a byte value
#define NBITS_NBITS_CHARVAL 4

class Compressor : public BitStream
{
public:
    int verbose;
    // get/put an integer using a variable number of bits
    void         put_uint_vl(unsigned int v,int maxn,const char *tag=(char*)"NOTAG");
    unsigned int get_uint_vl(               int maxn,const char *tag=(char*)NULL);

    // get/put an integer checking for an expected value
    void         put_uint_ex(unsigned int v,unsigned int ex,int maxn,const char *tag=(char*)"NOTAG")
	{
	    if(v==ex){put(1,tag);}
	    else{put(0,tag);put_uint(v,maxn,(char*)NULL);}
	}
    unsigned int get_uint_ex(               unsigned int ex,int maxn,const char *tag=(char*)NULL)
	{
	    if(get(tag)){return ex;}
	    else{return get_uint(maxn,(char*)NULL);}
	}


    // compress/decompress an array of unsigned ints (choosing best method)
    int put_vals(unsigned int *vals,int n,const char *tag);
    int get_vals(unsigned int **pres,const char *tag=(char*)"BADTAG!");

    // compress/decompress an array of bytes (very simple)
    int put_fixedbitl(byte *vals,int n,const char *tag);    
    int get_fixedbitl(byte **pres,const char *tag=(char*)"BADTAG!");

    // compress/decompress an array of unsigned ints (very simple)
    void get_fixedbitl(unsigned int *res,int n);
    void put_fixedbitl(unsigned int *vals,int n);

    // compress/decompress an array of unsigned ints (sophisticated)
    void get_decr(unsigned int *res,int n);
    void put_decr(unsigned int *vals,int n);

    Compressor():BitStream()
	{
	    verbose=0;
	}
    Compressor(int size0):BitStream(size0)
	{
	    verbose=0;
	}

};



#endif