-
Notifications
You must be signed in to change notification settings - Fork 0
/
lz16-cc.cc
239 lines (225 loc) · 6.17 KB
/
lz16-cc.cc
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
/***********************************************************************************************************/
//Базовая реализация упаковки/распаковки файла по упрощённому алгоритму LZSS+RLE
/***********************************************************************************************************/
#include <cstdint>
#include <algorithm>
#include <iostream>
#include <fstream>
using namespace std;
#define LZ_BUF_SIZE 259
#define LZ_CAPACITY 24
#define LZ_MIN_MATCH 3
typedef union{
struct{
uint16_t in;
uint16_t out;
};
uint32_t val;
} vocpntr;
class packer{
private:
uint8_t *ibuf,*obuf,*cbuffer,*vocbuf,flags;
uint16_t *vocarea,*hashes,buf_size,voclast,vocroot,offset,length;
uint32_t icbuf,wpos,rpos;
vocpntr *vocindx;
template <class T,class V> void del(T& p,uint32_t s,V v);
inline void wbuf(uint8_t c);
inline void rbuf(uint8_t *c);
public:
ifstream ifile;
ofstream ofile;
void init();
void pack();
void unpack();
packer();
~packer();
};
template <class T,class V> void packer::del(T& p,uint32_t s,V v){
if(p){
fill(p,p+s,v);
delete[] p;
};
p=NULL;
}
packer::packer(){
ibuf=new uint8_t[0x10000];
obuf=new uint8_t[0x10000];
vocbuf=new uint8_t[0x10000];
cbuffer=new uint8_t[LZ_CAPACITY+1];
vocarea=new uint16_t[0x10000];
hashes=new uint16_t[0x10000];
vocindx=new vocpntr[0x10000];
}
packer::~packer(){
del(ibuf,0x10000,(uint8_t)0);
del(obuf,0x10000,(uint8_t)0);
del(vocbuf,0x10000,(uint8_t)0);
del(cbuffer,LZ_CAPACITY+1,(uint8_t)0);
del(vocarea,0x10000,(uint16_t)0);
del(hashes,0x10000,(uint16_t)0);
del(vocindx,0x10000,(vocpntr){0,0});
flags=buf_size=vocroot=voclast=icbuf=wpos=rpos=0;
}
void packer::init(){
buf_size=flags=vocroot=icbuf=wpos=rpos=0;
voclast=0xfffc;
for(uint32_t i=0;i<0x10000;i++){
vocbuf[i]=0xff;
hashes[i]=0;
vocindx[i].val=1;
vocarea[i]=(uint16_t)(i+1);
};
vocindx[0].in=0;
vocindx[0].out=0xfffc;
vocarea[0xfffc]=0xfffc;
vocarea[0xfffd]=0xfffd;
vocarea[0xfffe]=0xfffe;
vocarea[0xffff]=0xffff;
}
inline void packer::wbuf(uint8_t c){
if(wpos==0x10000&&(wpos=0,ofile.write((char *)obuf,0x10000).bad())) return;
obuf[wpos++]=c;
}
inline void packer::rbuf(uint8_t *c){
if(rpos==icbuf&&!(rpos=0,ifile.read((char *)ibuf,0x10000),(icbuf=ifile.gcount()))) return;
*c=ibuf[rpos++];
}
void packer::pack(){
uint8_t *cpos=&cbuffer[1],eoff=0;
flags=8;
for(;;){
if(!eoff&&buf_size!=LZ_BUF_SIZE){
if((rbuf(&vocbuf[vocroot]),rpos)){
if(vocarea[vocroot]==vocroot) vocindx[hashes[vocroot]].val=1;
else vocindx[hashes[vocroot]].in=vocarea[vocroot];
vocarea[vocroot]=vocroot;
uint16_t hs=hashes[voclast]^vocbuf[voclast]^vocbuf[vocroot++];
vocpntr *indx=&vocindx[(hashes[++voclast]=(hs<<4)|(hs>>12))];
if(indx->val==1) indx->in=voclast;
else vocarea[indx->out]=voclast;
indx->out=voclast;
buf_size++;
}
else eoff=1;
continue;
};
*cbuffer<<=1;
if(buf_size){
uint16_t symbol=vocroot-buf_size,rle=symbol,rle_shift=symbol+LZ_BUF_SIZE;
while(rle!=vocroot&&vocbuf[++rle]==vocbuf[symbol]);
rle-=symbol;
if(buf_size>(length=LZ_MIN_MATCH)&&rle!=buf_size){
uint16_t cnode=vocindx[hashes[symbol]].in;
while(cnode!=symbol){
if(vocbuf[(uint16_t)(symbol+length)]==vocbuf[(uint16_t)(cnode+length)]){
uint16_t i=symbol,j=cnode;
while(i!=vocroot&&vocbuf[i]==vocbuf[j++]) i++;
if((i-=symbol)>=length){
if(buf_size!=LZ_BUF_SIZE&&(uint16_t)(cnode-rle_shift)>0xfefe) cnode=vocarea[cnode];
else{
offset=cnode;
if((length=i)==buf_size) break;
};
};
};
cnode=vocarea[cnode];
};
};
if(rle>length){
length=rle;
offset=vocbuf[symbol];
}
else offset=~(uint16_t)(offset-rle_shift);
uint16_t i=length-LZ_MIN_MATCH;
if(i){
*cpos++=--i;
*(uint16_t*)cpos++=offset;
buf_size-=length;
}
else{
*cbuffer|=1;
*cpos=vocbuf[symbol];
buf_size--;
};
}
else{
length=0;
cpos++;
*(uint16_t*)cpos++=0x0100;
};
cpos++;
if(--flags&&length) continue;
*cbuffer<<=flags;
for(int i=0;i<cpos-cbuffer;i++)
if(!(wbuf(cbuffer[i]),wpos)) return;
if(!length) break;
cpos=&cbuffer[1];
flags=8;
};
ofile.write((char*)obuf,wpos);
}
void packer::unpack(){
uint8_t *cpos=NULL,c,rle_flag=0,bytes=0;
length=0;
for(;;){
if(length){
if(!rle_flag) c=vocbuf[offset++];
vocbuf[vocroot++]=c;
length--;
bytes=1;
if(!vocroot&&(bytes=0,ofile.write((char *)vocbuf,0x10000).bad())) break;
continue;
};
if(flags){
length=rle_flag=1;
if(*cbuffer&0x80) c=*cpos;
else{
length+=LZ_MIN_MATCH+*cpos++;
if((offset=*(uint16_t*)cpos++)<0x0100) c=offset;
else{
if(offset==0x0100) break;
offset=~offset+vocroot+LZ_BUF_SIZE;
rle_flag=0;
};
};
*cbuffer<<=1;
cpos++;
flags--;
continue;
};
cpos=cbuffer;
if(!(rbuf(cpos++),rpos)) break;
for(c=~*cbuffer;c;flags++) c&=c-1;
for(c=8+(flags<<1);c;c--)
if(!(rbuf(cpos++),rpos)) break;
cpos=&cbuffer[1];
flags=8;
};
if(bytes) ofile.write((char *)vocbuf,vocroot?vocroot:0x10000);
}
int main(int argc, char *argv[]){
packer *p;
if((argc<4)||((argv[1][0]!='c')&&(argv[1][0]!='d'))){
cout<<"lz16-cc file compressor\n\nto compress use: lz16-cc c input output\nto decompress use: lz16-cc d input output\n";
goto rpoint00;
};
if(ifstream(argv[3]).good()){
cout<<"Error: output file already exists!\n";
goto rpoint00;
};
p=new packer();
p->ifile.open(argv[2],fstream::in|fstream::binary);
if(!p->ifile.is_open()) goto rpoint01;
p->ofile.open(argv[3],fstream::out|fstream::binary);
if(!p->ofile.is_open()) goto rpoint02;
p->init();
if(argv[1][0]=='c') p->pack();
else p->unpack();
p->ofile.close();
rpoint02:
p->ifile.close();
rpoint01:
delete p;
rpoint00:
return 0;
}