FireBirdLib - Topfield TMS PVR TAP Programming Library
FBLib_compression.h
Go to the documentation of this file.
1#ifndef FBLIB_COMPRESSION_H
2#define FBLIB_COMPRESSION_H
3
4#include <limits.h>
5#include "type.h"
6#include "libFireBird.h"
7
8//Uncomment or pass compiler option to get a call history on the serial console
9//#define ARCALLTRACE
10
11/* variables for hash */
12typedef struct
13{
14 dword pos;
15 int too_flag; /* if 1, matching candidate is too many */
16} HASH;
17
18typedef struct
19{
20 int len;
21 dword off;
22} MATCHDATA;
23
24
25#define LOAD_WORD(x) (((x)[0] << 8) | (x)[1])
26#define LOAD_WORDLE(x) (((x)[1] << 8) | (x)[0])
27#define STORE_WORD(x,y) (x)[0] = (y) >> 8;(x)[1] = (y) & 0xff
28#define STORE_WORDLE(x,y) (x)[1] = (y) >> 8;(x)[0] = (y) & 0xff
29
30#define LZHUFF5_METHOD_NUM 5
31#define LZHUFF6_METHOD_NUM 6
32#define LZHUFF7_METHOD_NUM 7
33
34#define LZHUFF4_DICBIT 12 /* 2^12 = 4KB sliding dictionary */
35#define LZHUFF5_DICBIT 13 /* 2^13 = 8KB sliding dictionary */
36#define LZHUFF6_DICBIT 15 /* 2^15 = 32KB sliding dictionary */
37#define LZHUFF7_DICBIT 16 /* 2^16 = 64KB sliding dictionary */
38
39#define NIL 0
40
41/* hash function: it represents 3 letters from `pos' on `text' */
42#define INIT_HASH(pos) \
43 ((( (ArData->text[(pos)] << 5) \
44 ^ ArData->text[(pos) + 1] ) << 5) \
45 ^ ArData->text[(pos) + 2] ) & (unsigned)(HSHSIZ - 1);
46#define NEXT_HASH(hash,pos) \
47 (((hash) << 5) \
48 ^ ArData->text[(pos) + 2] ) & (unsigned)(HSHSIZ - 1);
49
50
51#define peekbits(n) (ArData->bitbuf >> (sizeof(ArData->bitbuf)*8 - (n)))
52#define NC (UCHAR_MAX + MAXMATCH + 2 - THRESHOLD)
53#define MAXMATCH 256 /* formerly F (not more than UCHAR_MAX + 1) */
54#define THRESHOLD 3 /* choose optimal value */
55#define MAX_DICBIT LZHUFF7_DICBIT /* lh7 use 16bits */
56#define MAX_DICSIZ (1L << MAX_DICBIT)
57#define USHRT_BIT 16 /* (CHAR_BIT * sizeof(ushort)) */
58#define NP (MAX_DICBIT + 1)
59#define NT (USHRT_BIT + 3)
60#define NPT 0x80 /* #if NT > NP #define NPT NT #else #define NPT NP #endif */
61#define TBIT 5 /* smallest integer such that (1 << TBIT) > * NT */
62#define CBIT 9 /* smallest integer such that (1 << CBIT) > * NC */
63#define HSHSIZ (((dword)1) <<15)
64#define TXTSIZ (MAX_DICSIZ * 2L + MAXMATCH)
65#define LIMIT 0x100 /* limit of hash chain */
66
67typedef struct
68{
69 byte *InFile, *OutFile; //provided by the main program
70 dword OrigSize;
71 dword CompSize;
72
73 dword InFileRemaining; //io
75 word bitbuf;
76 byte bitcount, subbitbuf;
77 int dicbit;
78 int pbit, np;
79
80 word pt_code [NPT]; //huf
81 word c_code [NC];
82
83 word c_freq [2 * NC - 1];
84 word p_freq [2 * NP - 1];
85 word t_freq [2 * NT - 1];
86
87 word left[2 * NC - 1];
88 word right[2 * NC - 1];
91 byte *buf;
92 byte c_len[NC];
93 byte pt_len [NPT];
94 dword bufsiz;
95
96 HASH *hash; //maketree
97 word depth;
98
99 byte *text; //encode
100 dword *prev;
101 word cpos;
103
104 dword txtsiz;
105 dword dicsiz;
107
108 word blocksize; //decode
109 word c_table[4096];
110 word pt_table[256];
111
112 dword Magic;
113
114} ARDATA;
115
116#endif
#define NPT
#define NC
#define NT
#define NP
word output_pos
HASH * hash
dword CompSize
dword InFileRemaining
byte * buf
byte * text
dword OrigSize
word output_mask
dword * prev
byte * InFile
dword bufsiz
dword pos
int too_flag