]>
git.saurik.com Git - wxWidgets.git/blob - utils/Install/packace/uac_dcpr.c
1 /* ------------------------------------------------------------------------ */
3 /* These are the decompression algorithms. */
4 /* Don't change here anything (apart from memory allocation perhaps). */
5 /* Any changes will very likely cause bugs! */
7 /* ------------------------------------------------------------------------ */
12 #include <string.h> // mem*()
14 #if defined(DOS) || defined(WIN16) || defined(WINNT) || defined(OS2) || defined(UNIX)
15 #if !defined(__CYGWIN__)
16 #include <mem.h> // mem*()
20 #include <stdio.h> // pipeit()
21 #include <stdlib.h> // malloc()
35 //------------------------------ QUICKSORT ---------------------------------//
36 #define xchg_def(v1,v2) {INT dummy;\
41 void sortrange(INT left
, INT right
)
47 hyphen
= sort_freq
[right
];
49 //divides by hyphen the given range into 2 parts
52 while (sort_freq
[zl
] > hyphen
)
54 while (sort_freq
[zr
] < hyphen
)
56 //found a too small (left side) and
57 //a too big (right side) element-->exchange them
60 xchg_def(sort_freq
[zl
], sort_freq
[zr
]);
61 xchg_def(sort_org
[zl
], sort_org
[zr
]);
68 //sort partial ranges - when very small, sort directly
73 else if (sort_freq
[left
] < sort_freq
[zr
])
75 xchg_def(sort_freq
[left
], sort_freq
[zr
]);
76 xchg_def(sort_org
[left
], sort_org
[zr
]);
84 else if (sort_freq
[zl
] < sort_freq
[right
])
86 xchg_def(sort_freq
[zl
], sort_freq
[right
]);
87 xchg_def(sort_org
[zl
], sort_org
[right
]);
101 //------------------------------ read bits ---------------------------------//
106 i
= (size_rdb
- 2) << 2;
107 rpos
-= size_rdb
- 2;
108 buf_rd
[0] = buf_rd
[size_rdb
- 2];
109 buf_rd
[1] = buf_rd
[size_rdb
- 1];
110 read_adds_blk((CHAR
*) & buf_rd
[2], i
);
111 #ifdef HI_LO_BYTE_ORDER
114 i
>>=2; // count LONGs not BYTEs
125 #define addbits(bits) \
127 rpos+=(bits_rd+=bits)>>5; \
129 if (rpos==(size_rdb-2)) readdat(); \
130 code_rd=(buf_rd[rpos] << bits_rd) \
131 +((buf_rd[rpos+1] >> (32-bits_rd))&(!bits_rd-1)); \
135 //---------------------- COMMENT DECOMPRESSION -----------------------------//
137 #define comm_cpr_hf(a,b) (a+b)
139 void dcpr_comm_init(void)
143 i
= comm_cpr_size
> size_rdb
* sizeof(LONG
) ? size_rdb
* sizeof(LONG
) : comm_cpr_size
;
145 memcpy(buf_rd
, comm
, i
);
146 #ifdef HI_LO_BYTE_ORDER
149 i
>>=2; // count LONGs not BYTEs
162 void dcpr_comm(INT comm_size
)
164 SHORT hash
[comm_cpr_hf(255, 255) + 1];
171 memset(&hash
, 0, sizeof(hash
));
175 len
= code_rd
>> (32 - 15);
177 if (len
>= comm_size
)
179 if (read_wd(maxwd_mn
, dcpr_code_mn
, dcpr_wd_mn
, max_cd_mn
))
184 pos
= hash
[hs
= comm_cpr_hf(comm
[dpos
- 1], comm
[dpos
- 2])];
187 addbits(dcpr_wd_mn
[(c
= dcpr_code_mn
[code_rd
>> (32 - maxwd_mn
)])]);
188 if (rpos
== size_rdb
- 3)
195 comm
[dpos
++] = comm
[pos
++];
207 //------------------------- LZW1 DECOMPRESSION -----------------------------//
212 dcpr_text
[dcpr_dpos
] = ch
;
214 dcpr_dpos
&= dcpr_dican
;
217 void copystr(LONG d
, INT l
)
223 mpos
= dcpr_dpos
- d
;
226 if ((mpos
>= dcpr_dicsiz
- maxlength
) || (dcpr_dpos
>= dcpr_dicsiz
- maxlength
))
230 dcpr_text
[dcpr_dpos
] = dcpr_text
[mpos
];
232 dcpr_dpos
&= dcpr_dican
;
240 dcpr_text
[dcpr_dpos
++] = dcpr_text
[mpos
++];
241 dcpr_dpos
&= dcpr_dican
;
245 void decompress(void)
253 while (dcpr_do
< dcpr_do_max
)
259 addbits(dcpr_wd_mn
[(c
= dcpr_code_mn
[code_rd
>> (32 - maxwd_mn
)])]);
267 dist
= (code_rd
>> (33 - c
)) + (1L << (c
- 1));
272 dcpr_olddist
[(dcpr_oldnum
= (dcpr_oldnum
+ 1) & 3)] = dist
;
283 dist
= dcpr_olddist
[(dcpr_oldnum
- (c
&= 255)) & 3];
284 for (k
= c
+ 1; k
--;)
285 dcpr_olddist
[(dcpr_oldnum
- k
) & 3] = dcpr_olddist
[(dcpr_oldnum
- k
+ 1) & 3];
286 dcpr_olddist
[dcpr_oldnum
] = dist
;
291 addbits(dcpr_wd_lg
[(lg
= dcpr_code_lg
[code_rd
>> (32 - maxwd_lg
)])]);
301 //-------------------------- HUFFMAN ROUTINES ------------------------------//
302 INT
makecode(UINT maxwd
, UINT size1_t
, UCHAR
* wd
, USHORT
* code
)
311 memcpy(&sort_freq
, wd
, (size1_t
+ 1) * sizeof(CHAR
));
316 sort_freq
[size1_t
+ 1] = size2_t
= c
= 0;
317 while (sort_freq
[size2_t
])
323 size2_t
+= (size2_t
== 0);
327 max_make_code
= 1 << maxwd
;
328 for (i
= size2_t
+ 1; i
-- && c
< max_make_code
;)
330 maxc
= 1 << (maxwd
- sort_freq
[i
]);
332 if (c
+ maxc
> max_make_code
)
334 dcpr_do
= dcpr_do_max
;
337 memset16(&code
[c
], l
, maxc
);
343 INT
read_wd(UINT maxwd
, USHORT
* code
, UCHAR
* wd
, INT max_el
)
353 memset(wd
, 0, max_el
* sizeof(CHAR
));
354 memset(code
, 0, (1 << maxwd
) * sizeof(SHORT
));
356 num_el
= code_rd
>> (32 - 9);
361 lolim
= code_rd
>> (32 - 4);
363 uplim
= code_rd
>> (32 - 4);
366 for (i
= -1; ++i
<= uplim
;)
368 wd_svwd
[i
] = code_rd
>> (32 - 3);
371 if (!makecode(maxwd_svwd
, uplim
, wd_svwd
, code
))
376 c
= code
[code_rd
>> (32 - maxwd_svwd
)];
382 l
= (code_rd
>> 28) + 4;
384 while (l
-- && j
<= num_el
)
389 for (i
= 0; ++i
<= num_el
;)
390 wd
[i
] = (wd
[i
] + wd
[i
- 1]) % uplim
;
391 for (i
= -1; ++i
<= num_el
;)
395 return (makecode(maxwd
, num_el
, wd
, code
));
399 INT
calc_dectabs(void)
401 if (!read_wd(maxwd_mn
, dcpr_code_mn
, dcpr_wd_mn
, max_cd_mn
)
402 || !read_wd(maxwd_lg
, dcpr_code_lg
, dcpr_wd_lg
, max_cd_lg
))
405 blocksize
= code_rd
>> (32 - 15);
411 //---------------------------- BLOCK ROUTINES ------------------------------//
412 INT
decompress_blk(CHAR
* buf
, UINT len
)
414 LONG old_pos
= dcpr_dpos
;
418 if ((dcpr_do_max
= len
- maxlength
) > dcpr_size
)
419 dcpr_do_max
= dcpr_size
;
420 if ((LONG
) dcpr_size
> 0 && dcpr_do_max
)
423 if (old_pos
+ dcpr_do
> dcpr_dicsiz
)
425 i
= dcpr_dicsiz
- old_pos
;
426 memcpy(buf
, &dcpr_text
[old_pos
], i
);
427 memcpy(&buf
[i
], dcpr_text
, dcpr_do
- i
);
430 memcpy(buf
, &dcpr_text
[old_pos
], dcpr_do
);
432 dcpr_size
-= dcpr_do
;
436 INT
unstore(CHAR
* buf
, UINT len
)
443 len
= crypt_len(len
- 8); /* because of decryption */
446 while ((i
= read_adds_blk((CHAR
*) buf_rd
, (INT
) ((i
= ((len
> dcpr_size
) ? dcpr_size
: len
)) > size_rdb
? size_rdb
: i
))) != 0)
450 memcpy(&buf
[pos
], buf_rd
, i
);
454 for (i
= 0; i
< rd
; i
++)
456 dcpr_text
[dcpr_dpos
] = buf
[i
];
458 dcpr_dpos
&= dcpr_dican
;
463 INT
dcpr_adds_blk(CHAR
* buf
, UINT len
)
467 switch (fhead
.TECH
.TYPE
)
470 r
= unstore(buf
, len
);
473 r
= decompress_blk(buf
, len
);
476 pipeit("\nFile compressed with unknown method. Decompression not possible.\n");
480 rd_crc
= getcrc(rd_crc
, (UCHAR
*)buf
, r
);
485 //----------------------------- INIT ROUTINES ------------------------------//
491 while ((dcpr_text
= malloc(dcpr_dicsiz
= (LONG
) 1 << dcpr_dic
))==NULL
)
493 dcpr_dican
= dcpr_dicsiz
- 1;
496 void dcpr_init_file(void)
506 if (head
.HEAD_FLAGS
& ACE_PASSW
)
508 pipeit("\nFound passworded file. Decryption not supported.\n");
516 dcpr_size
= fhead
.SIZE
;
517 if (fhead
.TECH
.TYPE
== TYPE_LZW1
)
519 if ((fhead
.TECH
.PARM
& 15) + 10 > dcpr_dic
)
521 pipeit("\nNot enough memory or dictionary of archive too large.\n");
526 i
= size_rdb
* sizeof(LONG
);
527 read_adds_blk((CHAR
*) buf_rd
, i
);
528 #ifdef HI_LO_BYTE_ORDER
531 i
>>=2; // count LONGs not BYTEs
545 if (!adat
.sol
|| dcpr_frst_file
)
549 memset(&dcpr_olddist
, 0, sizeof(dcpr_olddist
));