-
Notifications
You must be signed in to change notification settings - Fork 0
/
qbp-rs.rs
591 lines (573 loc) · 15.7 KB
/
qbp-rs.rs
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/***********************************************************************************************************/
//Базовая реализация упаковки/распаковки файла по упрощённому алгоритму LZSS+RLE+RC32
/***********************************************************************************************************/
use std::env::args;
use std::path::Path;
use std::fs::{File,metadata};
use std::{panic,ptr,usize};
use std::io::{Read, Write};
const LZ_BUF_SIZE: u16=259;
const LZ_CAPACITY: u8=24;
const LZ_MIN_MATCH: u16=3;
#[derive(Copy, Clone)]
struct IOpntr {
i: u16,
o: u16,
}
#[derive(Copy, Clone)]
union Vocpntr {
p: IOpntr,
v: u32,
}
union U16U8 {
u: u16,
c: [u8; 2],
}
pub struct Packer {
ibuf: [u8; 0x10000],
obuf: [u8; 0x10000],
vocbuf: [u8; 0x10000],
cbuffer: [u8; (LZ_CAPACITY+1) as usize],
cntxs: [u8; (LZ_CAPACITY+1) as usize],
vocarea: [u16; 0x10000],
hashes: [u16; 0x10000],
vocindx: [Vocpntr; 0x10000],
frequency: Vec<Vec<u16>>,
fcs: [u16; 256],
icbuf: u32,
wpos: u32,
rpos: u32,
low: u32,
hlp: u32,
range: u32,
buf_size: u16,
voclast: u16,
vocroot: u16,
offset: u16,
length: u16,
symbol: u16,
hs: u16,
lowp: *mut u8,
hlpp: *mut u8,
flags: u8,
scntx: u8,
pub ifile: File,
pub ofile: File,
}
impl Packer {
pub fn new(i: &str,o: &str)->Packer {
Packer{
ibuf: [0 as u8; 0x10000],
obuf: [0 as u8; 0x10000],
vocbuf: [0 as u8; 0x10000],
cbuffer: [0 as u8; (LZ_CAPACITY+1) as usize],
cntxs: [0 as u8; (LZ_CAPACITY+1) as usize],
vocarea: [0 as u16; 0x10000],
hashes: [0 as u16; 0x10000],
vocindx: [Vocpntr{v:1 as u32,}; 0x10000],
frequency: vec![vec![0 as u16; 256]; 256],
fcs: [0 as u16; 256],
icbuf: 0,wpos: 0,rpos: 0,low: 0,hlp: 0,range: 0,
buf_size: 0,voclast: 0,vocroot: 0,offset: 0,length: 0,symbol: 0,hs: 0,
lowp: ptr::null_mut(),
hlpp: ptr::null_mut(),
flags: 0,
scntx: 0xff,
ifile:match File::open(&Path::new(i)){
Err(why)=>{
println!("Couldn't open input file: {}",why);
panic!();
},
Ok(ifile)=>ifile,
},
ofile:match File::create(&Path::new(o)){
Err(why)=>{
println!("Couldn't open output file: {}",why);
panic!();
},
Ok(ofile)=>ofile,
},
}
}
pub fn init(&mut self) {
self.buf_size=0;
self.flags=0;
self.scntx=0xff;
self.vocroot=0;
self.cbuffer[0]=0;
self.cntxs[0]=0;
self.low=0;
self.hlp=0;
self.icbuf=0;
self.wpos=0;
self.rpos=0;
self.voclast=0xfffd;
self.range=0xffffffff;
for i in 0..256{
for j in 0..256{
self.frequency[i][j]=1 as u16;
}
self.fcs[i]=256;
}
for i in 0..0x10000{
self.vocbuf[i]=0xff;
self.hashes[i]=0;
self.vocarea[i]=((i as u16)+1) as u16;
};
self.vocindx[0].p.i=0;
self.vocindx[0].p.o=0xfffc;
self.vocarea[0xfffc]=0xfffc;
self.vocarea[0xfffd]=0xfffd;
self.vocarea[0xfffe]=0xfffe;
self.vocarea[0xffff]=0xffff;
self.hs=0x00ff;
self.lowp=ptr::addr_of_mut!(self.low) as *mut u8;
unsafe { self.lowp=self.lowp.add(3) };
self.hlpp=ptr::addr_of_mut!(self.hlp) as *mut u8;
}
#[inline(always)]
fn rbuf(&mut self,c: *mut u8)->bool {
if self.rpos==self.icbuf {
self.rpos=0;
match self.ifile.read(&mut self.ibuf) {
Ok(r) => self.icbuf=r as u32,
Err(_) => return true,
}
}
if self.icbuf>0 {
unsafe { *c=self.ibuf[self.rpos as usize] };
self.rpos+=1;
}
return false;
}
#[inline(always)]
fn wbuf(&mut self,c: u8)->bool {
if self.wpos==0x10000{
self.wpos=0;
match self.ofile.write(&self.obuf) {
Ok(_) => {},
Err(_) => return true,
}
}
self.obuf[self.wpos as usize]=c;
self.wpos+=1;
return false;
}
#[inline(always)]
fn rc32_rescale(&mut self,s: u32,cntx: u8) {
self.low+=s*self.range;
self.range*=self.frequency[cntx as usize][self.symbol as usize] as u32;
self.frequency[cntx as usize][self.symbol as usize]+=1;
self.fcs[cntx as usize]+=1;
if self.fcs[cntx as usize]==0 {
let mut fc: u16=0;
for i in 0..256 {
self.frequency[cntx as usize][i]=(self.frequency[cntx as usize][i]>>1)|(self.frequency[cntx as usize][i]&1);
fc+=self.frequency[cntx as usize][i];
}
self.fcs[cntx as usize]=fc;
}
}
fn rc32_getc(&mut self,c: *mut u8,cntx: u8)->bool {
while (self.low^(self.low+self.range))<0x1000000 || self.range<0x10000 || self.hlp<self.low {
self.hlp<<=8;
if self.rbuf(self.hlpp) {
return true;
}
if self.rpos==0 {
return false;
};
self.low<<=8;
self.range<<=8;
if ((self.range+self.low) as u32) < self.low {
self.range=!self.low;
}
}
self.range/=self.fcs[cntx as usize] as u32;
let count: u32=(self.hlp-self.low)/self.range;
if count>=self.fcs[cntx as usize] as u32 {
return true;
}
let mut s: u32=0;
for i in 0..256 {
s+=self.frequency[cntx as usize][i as usize] as u32;
if s>count {
self.symbol=i as u16;
break;
};
};
s-=self.frequency[cntx as usize][self.symbol as usize] as u32;
unsafe { *c=self.symbol as u8};
self.rc32_rescale(s,cntx);
return false;
}
fn rc32_putc(&mut self,c: u8,cntx: u8)->bool {
while (self.low^(self.low+self.range))<0x1000000 || self.range<0x10000 {
if self.wbuf(unsafe { *self.lowp }) {
return true;
}
self.low<<=8;
self.range<<=8;
if ((self.range+self.low) as u32) < self.low {
self.range=!self.low;
}
}
self.symbol=c as u16;
self.range/=self.fcs[cntx as usize] as u32;
let mut s: u32=0;
for i in 0..self.symbol {
s+=self.frequency[cntx as usize][i as usize] as u32;
}
self.rc32_rescale(s,cntx);
return false;
}
pub fn pack(&mut self) {
let mut i: u16;
let mut rle: u16;
let mut rle_shift: u16=0;
let mut cnode: u16;
let mut cpos: *mut u8=ptr::addr_of_mut!(self.cbuffer).cast();
let w: *mut u8=cpos;
unsafe { cpos=cpos.add(1) };
let mut eoff: bool=false;
let mut eofs: bool=false;
let mut cntx: u8=1;
self.flags=8;
loop {
if !eoff {
if (LZ_BUF_SIZE-self.buf_size)>0 {
if self.rbuf(ptr::addr_of!(self.vocbuf[self.vocroot as usize]) as *mut u8) {
break;
}
if self.rpos==0 {
eoff=true;
continue;
}
else {
if self.vocarea[self.vocroot as usize]==self.vocroot {
self.vocindx[self.hashes[self.vocroot as usize] as usize].v=1;
}
else {
self.vocindx[self.hashes[self.vocroot as usize] as usize].p.i=self.vocarea[self.vocroot as usize];
}
self.vocarea[self.vocroot as usize]=self.vocroot;
self.hs^=self.vocbuf[self.vocroot as usize] as u16;
self.hs=(self.hs<<4)|(self.hs>>12);
self.hashes[self.voclast as usize]=self.hs;
self.hs^=self.vocbuf[self.voclast as usize] as u16;
let indx=self.hashes[self.voclast as usize] as usize;
unsafe {
if self.vocindx[indx].v==1 {
self.vocindx[indx].p.i=self.voclast;
}
else {
self.vocarea[self.vocindx[indx].p.o as usize]=self.voclast;
}
self.vocindx[indx].p.o=self.voclast;
}
self.voclast+=1;
self.vocroot+=1;
self.buf_size+=1;
continue;
}
}
}
self.cbuffer[0]<<=1;
self.symbol=self.vocroot-self.buf_size;
rle=self.symbol;
let cnv: U16U8;
if self.buf_size>0 {
rle+=1;
while rle!=self.vocroot && self.vocbuf[self.symbol as usize]==self.vocbuf[rle as usize] {
rle+=1;
}
rle-=self.symbol;
self.length=LZ_MIN_MATCH;
if self.buf_size>LZ_MIN_MATCH && rle<self.buf_size {
unsafe { cnode=self.vocindx[self.hashes[self.symbol as usize] as usize].p.i };
rle_shift=self.vocroot+LZ_BUF_SIZE-self.buf_size;
while cnode!=self.symbol {
if self.vocbuf[((self.symbol+self.length) as u16) as usize]==
self.vocbuf[((cnode+self.length) as u16) as usize] {
i=self.symbol;
let mut k: u16=cnode;
while i!=self.vocroot && self.vocbuf[i as usize]==self.vocbuf[k as usize] {
k+=1;
i+=1;
}
i-=self.symbol;
if i>=self.length {
if self.buf_size<LZ_BUF_SIZE {
if (cnode-rle_shift) as u16>0xfefe {
cnode=self.vocarea[cnode as usize];
continue;
}
}
self.offset=cnode;
self.length=i;
if i==self.buf_size {
break;
}
}
}
cnode=self.vocarea[cnode as usize];
}
}
if rle>self.length {
self.cntxs[cntx as usize]=1;
cntx+=1;
self.cntxs[cntx as usize]=2;
cntx+=1;
self.cntxs[cntx as usize]=3;
cntx+=1;
unsafe {
*cpos=(rle-LZ_MIN_MATCH-1) as u8;
cpos=cpos.add(1);
cnv.u=self.vocbuf[self.symbol as usize] as u16;
*cpos=cnv.c[0];
cpos=cpos.add(1);
*cpos=cnv.c[1];
self.buf_size-=rle;
}
}
else {
if self.length>LZ_MIN_MATCH {
self.cntxs[cntx as usize]=1;
cntx+=1;
self.cntxs[cntx as usize]=2;
cntx+=1;
self.cntxs[cntx as usize]=3;
cntx+=1;
unsafe {
*cpos=(self.length-LZ_MIN_MATCH-1) as u8;
cpos=cpos.add(1);
cnv.u=!(self.offset-rle_shift);
*cpos=cnv.c[0];
cpos=cpos.add(1);
*cpos=cnv.c[1];
self.buf_size-=self.length;
}
}
else {
self.cntxs[cntx as usize]=self.scntx;
self.scntx=self.vocbuf[self.symbol as usize];
cntx+=1;
self.cbuffer[0]|=0x01;
unsafe { *cpos=self.vocbuf[self.symbol as usize] };
self.buf_size-=1;
}
}
}
else {
self.cntxs[cntx as usize]=1;
cntx+=1;
self.cntxs[cntx as usize]=2;
cntx+=1;
self.cntxs[cntx as usize]=3;
cntx+=1;
unsafe {
cpos=cpos.add(1);
cnv.u=0x0100;
*cpos=cnv.c[0];
cpos=cpos.add(1);
*cpos=cnv.c[1];
if eoff {
eofs=true;
}
}
}
unsafe { cpos=cpos.add(1) };
self.flags-=1;
if self.flags==0 || eofs {
self.cbuffer[0]<<=self.flags;
for i in 0..(cpos as u32 - w as u32) as u16 {
if self.rc32_putc(self.cbuffer[i as usize],self.cntxs[i as usize]){
return;
}
}
cntx=1;
self.flags=8;
cpos=w;
unsafe { cpos=cpos.add(1) };
if eofs {
i=4;
while i>0 {
if self.wbuf(unsafe { *self.lowp }) {
return;
}
self.low<<=8;
i-=1;
}
match self.ofile.write(&self.obuf[..self.wpos as usize]) {
Ok(_) => break,
Err(_) => return,
}
}
}
}
}
pub fn unpack(&mut self) {
let mut cpos: *mut u8=ptr::addr_of_mut!(self.cbuffer).cast();
let mut c: u8=0;
let mut cntx: u8;
let mut cflags: u8;
let mut rle_flag: bool=false;
let mut bytes: bool=false;
for _i in 0..4 {
self.hlp<<=8;
if self.rbuf(self.hlpp) || self.rpos==0 {
return;
}
}
loop {
if self.length!=0 {
if rle_flag==false {
c=self.vocbuf[self.offset as usize];
self.offset+=1;
}
self.vocbuf[self.vocroot as usize]=c;
self.vocroot+=1;
self.length-=1;
bytes=true;
if self.vocroot==0 {
bytes=false;
match self.ofile.write(&self.vocbuf) {
Ok(_) => {},
Err(_) => return,
}
}
}
else {
if self.flags==0 {
cpos=ptr::addr_of_mut!(self.cbuffer) as *mut u8;
if self.rc32_getc(cpos,0){
return;
}
unsafe { cpos=cpos.add(1) };
cflags=self.cbuffer[0];
c=!self.cbuffer[0];
while c!=0 {
c&=c-1;
self.flags+=1;
}
cntx=8+(self.flags<<1);
let mut i: u8=0;
while i<cntx {
if cflags&0x80!=0 {
self.cntxs[i as usize]=4;
i+=1;
}
else {
self.cntxs[i as usize]=1;
i+=1;
self.cntxs[i as usize]=2;
i+=1;
self.cntxs[i as usize]=3;
i+=1;
}
cflags<<=1;
}
for i in 0..cntx {
if self.cntxs[i as usize]==4 {
if self.rc32_getc(cpos,self.scntx) {
return;
}
unsafe {
self.scntx=*cpos;
}
}
else{
if self.rc32_getc(cpos,self.cntxs[i as usize]) {
return;
}
}
unsafe { cpos=cpos.add(1) }
}
self.flags=8;
cpos=ptr::addr_of_mut!(self.cbuffer) as *mut u8;
unsafe { cpos=cpos.add(1) }
}
rle_flag=true;
if self.cbuffer[0]&0x80!=0 {
self.length=1;
c=unsafe { *cpos };
}
else {
let mut cnv: U16U8;
cnv.u=0;
unsafe {
self.length=*cpos as u16;
cpos=cpos.add(1);
self.length+=LZ_MIN_MATCH+1;
cnv.c[0]=*cpos as u8;
cpos=cpos.add(1);
cnv.c[1]=*cpos as u8;
self.offset=cnv.u;
}
if self.offset<0x0100 {
c=self.offset as u8;
}
else {
if self.offset==0x0100 {
break;
}
self.offset=!self.offset+(self.vocroot+LZ_BUF_SIZE) as u16;
rle_flag=false;
}
}
self.cbuffer[0]<<=1;
unsafe { cpos=cpos.add(1) };
self.flags-=1;
}
}
if bytes {
if self.vocroot!=0 {
match self.ofile.write(&self.vocbuf[..self.vocroot as usize]) {
Ok(_) => return,
Err(_) => return,
}
}
else {
match self.ofile.write(&self.vocbuf) {
Ok(_) => return,
Err(_) => return,
}
}
}
}
}
fn main(){
panic::set_hook(Box::new(|_| { }));
let argv: Vec<String>=args().collect();
let argc=argv.len();
if argc<4 {
println!("qbp-rs file compressor");
println!("to compress use: qbp-rs c input output");
println!("to decompress use: qbp-rs d input output");
panic!();
}
if !Path::new(&argv[2]).exists() {
println!("{} does not exists!",argv[2]);
panic!();
}
if !metadata(&argv[2]).unwrap().is_file() {
println!("{} not an ordinary file!",argv[2]);
panic!();
}
if Path::new(&argv[3]).exists() {
println!("{} already exists!",argv[3]);
panic!();
}
let cmd=argv[1].chars().nth(0).unwrap();
if cmd!='c' && cmd!='d' {
println!("Wrong argument: \'{}\'!",cmd);
panic!();
}
let mut p=Packer::new(&argv[2],&argv[3]);
p.init();
match cmd {
'c' => p.pack(),
'd' => p.unpack(),
_ => (),
}
}