-
Notifications
You must be signed in to change notification settings - Fork 0
/
unqbp.c
147 lines (139 loc) · 3.69 KB
/
unqbp.c
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
#include <stdint.h>
#include <stdio.h>
#include <fcntl.h>
#ifndef _WIN32
#include <unistd.h>
#endif
/***********************************************************************************************************/
//Базовая реализация распаковки файла по упрощённому алгоритму LZSS+RLE+RC32
/***********************************************************************************************************/
#define LZ_BUF_SIZE 259
#define LZ_CAPACITY 24
#define LZ_MIN_MATCH 3
uint8_t ibuf[0x10000];
uint8_t vocbuf[0x10000];
uint32_t icbuf;
uint32_t rpos;
uint16_t frequency[256][256];
uint16_t fcs[256];
uint32_t low;
uint32_t hlp;
uint32_t range;
uint8_t *hlpp;
void rbuf(uint8_t *c,FILE *ifile){
if(rpos==icbuf&&!(rpos=0,icbuf=fread(ibuf,1,0x10000,ifile))) return;
*c=ibuf[rpos++];
}
int rc32_getc(uint8_t *c,FILE *ifile,uint8_t cntx){
uint16_t *f=frequency[cntx],fc=fcs[cntx];
uint32_t s=0,i;
while(hlp<low||(low^(low+range))<0x1000000||range<0x10000){
hlp<<=8;
if(!(rbuf(hlpp,ifile),rpos)) return 0;
low<<=8;
range<<=8;
if((uint32_t)(range+low)<low) range=~low;
};
if((i=(hlp-low)/(range/=fc))<fc){
while((s+=*f)<=i) f++;
low+=(s-*f)*range;
*c=f-frequency[cntx];
range*=(*f)++;
if(!++fc){
f=frequency[cntx];
for(s=0;s<256;s++) fc+=(*f=((*f)>>1)|(*f&1)),f++;
};
fcs[cntx]=fc;
return 0;
}
else return 1;
}
void unpack_file(FILE *ifile, FILE *ofile){
uint8_t flags=0,c,rle_flag=0,bytes=0,cntx=0,cflags=0,scntx=0xff;
uint8_t cbuffer[LZ_CAPACITY+1],*cpos=cbuffer;
uint8_t cntxs[LZ_CAPACITY+2];
uint16_t vocroot=0,offset=0,length=0;
low=hlp=icbuf=rpos=0;
range=0xffffffff;
hlpp=&((uint8_t *)&hlp)[0];
for(int i=0;i<256;i++){
for(int j=0;j<256;j++) frequency[i][j]=1;
fcs[i]=256;
};
for(int i=0;i<0x10000;i++) vocbuf[i]=0xff;
for(c=0;c<4;c++){
hlp<<=8;
if(!(rbuf(hlpp,ifile),rpos)) return;
}
for(;;){
if(length){
if(!rle_flag) c=vocbuf[offset++];
vocbuf[vocroot++]=c;
length--;
bytes=1;
if(!vocroot&&(bytes=0,fwrite(vocbuf,1,0x10000,ofile)<0x10000)) 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(rc32_getc(cpos++,ifile,0)) break;
for(c=~*cbuffer;c;flags++) c&=c-1;
cflags=*cbuffer;
cntx=8+(flags<<1);
for(c=0;c<(cntx=8+(flags<<1));){
if(cflags&0x80) cntxs[c++]=4;
else{
*(uint32_t*)(cntxs+c)=0x00030201;
c+=3;
};
cflags<<=1;
};
for(c=0;c<cntx;c++)
if(cntxs[c]==4){
if(rc32_getc(cpos,ifile,scntx)) return;
scntx=*cpos++;
}
else{
if(rc32_getc(cpos++,ifile,cntxs[c])) return;
};
cpos=&cbuffer[1];
flags=8;
};
if(bytes) fwrite(vocbuf,1,vocroot?vocroot:0x10000,ofile);
}
/***********************************************************************************************************/
int main(int argc, char *argv[]){
if(argc<3){
printf("qbp file decompressor\n\nto decompress use: unqbp input output\n");
goto rpoint00;
};
if(access(argv[2],0)==0){
printf("Error: output file already exists!\n");
goto rpoint00;
};
FILE *ifile,*ofile;
if(!(ifile=fopen(argv[1],"rb"))) goto rpoint00;
if(!(ofile=fopen(argv[2],"wb"))) goto rpoint01;
unpack_file(ifile,ofile);
fclose(ofile);
rpoint01:
fclose(ifile);
rpoint00:
return 0;
}