| 1 |
|
| /*
|
| 2 |
|
| XS for Archive::Lha
|
| 3 |
|
|
|
| 4 |
|
| This XS is, though largely modified, based on LHa for UNIX.
|
| 5 |
|
| See lib/Archive/Lha.pm for Authors/Copyright/License information.
|
| 6 |
|
| */
|
| 7 |
|
| #include "EXTERN.h"
|
| 8 |
|
| #include "perl.h"
|
| 9 |
|
| #include "XSUB.h"
|
| 10 |
|
| #include "ppport.h"
|
| 11 |
|
| #include "Lha.h"
|
| 12 |
|
| #include <time.h>
|
| 13 |
|
|
|
| 14 |
|
| /*
|
| 15 |
|
| these are not from LHa for UNIX
|
| 16 |
|
| */
|
| 17 |
|
|
|
| 18 |
|
| void
|
| 19 |
19 |
| destroy_stash(LhaStash * stash)
|
| 20 |
|
| {
|
| 21 |
19 |
| Safefree(stash->tree->left);
|
| 22 |
19 |
| Safefree(stash->tree->right);
|
| 23 |
19 |
| Safefree(stash->tree);
|
| 24 |
19 |
| Safefree(stash->pt->table);
|
| 25 |
19 |
| Safefree(stash->pt->length);
|
| 26 |
19 |
| Safefree(stash->pt);
|
| 27 |
19 |
| Safefree(stash->c->table);
|
| 28 |
19 |
| Safefree(stash->c->length);
|
| 29 |
19 |
| Safefree(stash->c);
|
| 30 |
19 |
| Safefree(stash->bit);
|
| 31 |
19 |
| Safefree(stash->queue);
|
| 32 |
19 |
| Safefree(stash);
|
| 33 |
19 |
| }
|
| 34 |
|
|
|
| 35 |
|
| void
|
| 36 |
0 |
| safe_croak(LhaStash * stash, char * dying_message)
|
| 37 |
|
| {
|
| 38 |
0 |
| destroy_stash(stash);
|
| 39 |
0 |
| croak("%s", dying_message);
|
| 40 |
|
| }
|
| 41 |
|
|
|
| 42 |
|
| void
|
| 43 |
19 |
| output(LhaStash * stash, unsigned char * queue, int len)
|
| 44 |
|
| {
|
| 45 |
19 |
| dSP;
|
| 46 |
19 |
| ENTER;
|
| 47 |
19 |
| SAVETMPS;
|
| 48 |
19 |
▶ | PUSHMARK(SP);
|
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP);
|
0 |
19 |
19 |
|
| 49 |
19 |
▶ | XPUSHs(sv_2mortal(newSVpv(queue, len)));
|
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSVpv(queue, len)));
|
0 |
19 |
19 |
|
| 50 |
19 |
| PUTBACK;
|
| 51 |
19 |
| call_sv(stash->write, G_VOID);
|
| 52 |
19 |
| SPAGAIN;
|
| 53 |
19 |
| PUTBACK;
|
| 54 |
19 |
▶ | FREETMPS;
|
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS;
|
19 |
0 |
19 |
|
| 55 |
19 |
| LEAVE;
|
| 56 |
19 |
| }
|
| 57 |
|
|
|
| 58 |
|
| void
|
| 59 |
19 |
| input(LhaStash * stash, int len)
|
| 60 |
|
| {
|
| 61 |
|
| int n;
|
| 62 |
|
| SV *sv;
|
| 63 |
|
| STRLEN got;
|
| 64 |
|
| const char *ptr;
|
| 65 |
19 |
| dSP;
|
| 66 |
19 |
| ENTER;
|
| 67 |
19 |
| SAVETMPS;
|
| 68 |
19 |
▶ | PUSHMARK(SP);
|
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP);
|
0 |
19 |
19 |
|
| 69 |
19 |
▶ | XPUSHs(sv_2mortal(newSViv(len)));
|
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(len)));
|
0 |
19 |
19 |
|
| 70 |
19 |
| PUTBACK;
|
| 71 |
19 |
| n = call_sv(stash->read, G_SCALAR);
|
| 72 |
19 |
| SPAGAIN;
|
| 73 |
19 |
▶ | if (n != 1)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (n != 1)
|
0 |
19 |
19 |
|
| 74 |
0 |
| safe_croak(stash, "There's something wrong in 'read' callback");
|
| 75 |
19 |
| sv = POPs;
|
| 76 |
19 |
| ptr = SvPVbyte(sv, got);
|
| 77 |
19 |
| Copy(ptr, stash->bit->readbuf, (STRLEN)len <= got ? (STRLEN)len : got, unsigned char);
|
| 78 |
19 |
| PUTBACK;
|
| 79 |
19 |
▶ | FREETMPS;
|
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS;
|
19 |
0 |
19 |
|
| 80 |
19 |
| LEAVE;
|
| 81 |
19 |
| }
|
| 82 |
|
|
|
| 83 |
|
| /*
|
| 84 |
|
| modified from LHa for UNIX: bitio.c ver 1.14
|
| 85 |
|
| original authors:
|
| 86 |
|
| Source All chagned 1995.01.14 N.Watazaki
|
| 87 |
|
| Separated from crcio.c 2002.10.26 Koji Arai
|
| 88 |
|
| */
|
| 89 |
|
|
|
| 90 |
|
| unsigned short
|
| 91 |
9638 |
| shiftbits(LhaBitstream * bit, unsigned char n)
|
| 92 |
|
| {
|
| 93 |
9638 |
| return (bit->value << n)
|
| 94 |
9638 |
| + (bit->buf >> (char_bit - n));
|
| 95 |
|
| }
|
| 96 |
|
|
|
| 97 |
|
| void
|
| 98 |
6192 |
| fillbuf(LhaStash * stash, unsigned char n)
|
| 99 |
|
| {
|
| 100 |
|
| unsigned short len;
|
| 101 |
6192 |
| LhaBitstream * bit = stash->bit;
|
| 102 |
9638 |
▶ | while (n > bit->pos) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while (n > bit->pos) {
|
3446 |
6192 |
9638 |
|
| 103 |
3446 |
| n -= bit->pos;
|
| 104 |
3446 |
| bit->value = shiftbits(bit, bit->pos);
|
| 105 |
3446 |
▶ | if (stash->encoded_size > 0) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (stash->encoded_size > 0) {
|
3408 |
38 |
3446 |
|
| 106 |
3408 |
▶ | if (bit->readpos == 0) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (bit->readpos == 0) {
|
19 |
3389 |
3408 |
|
| 107 |
19 |
▶ | if (stash->encoded_size > readbuf_size)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (stash->encoded_size > readbuf_size)
|
0 |
19 |
19 |
|
| 108 |
0 |
| len = readbuf_size;
|
| 109 |
|
| else
|
| 110 |
19 |
| len = stash->encoded_size;
|
| 111 |
19 |
| input(stash, len);
|
| 112 |
|
| }
|
| 113 |
3408 |
| bit->buf = bit->readbuf[bit->readpos++];
|
| 114 |
3408 |
▶ | if (bit->readpos == readbuf_size)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (bit->readpos == readbuf_size)
|
0 |
3408 |
3408 |
|
| 115 |
0 |
| bit->readpos = 0;
|
| 116 |
3408 |
| stash->encoded_size--;
|
| 117 |
|
| }
|
| 118 |
|
| else
|
| 119 |
38 |
| bit->buf = 0;
|
| 120 |
3446 |
| bit->pos = char_bit;
|
| 121 |
|
| }
|
| 122 |
6192 |
| bit->pos -= n;
|
| 123 |
6192 |
| bit->value = shiftbits(bit, n);
|
| 124 |
6192 |
| bit->buf <<= n;
|
| 125 |
6192 |
| }
|
| 126 |
|
|
|
| 127 |
|
| unsigned short
|
| 128 |
6173 |
| peekbits(LhaStash * stash, unsigned char n)
|
| 129 |
|
| {
|
| 130 |
6173 |
| return (stash->bit->value >> (ushort_bit - n));
|
| 131 |
|
| }
|
| 132 |
|
|
|
| 133 |
|
| unsigned short
|
| 134 |
727 |
| getbits(LhaStash * stash, unsigned char n)
|
| 135 |
|
| {
|
| 136 |
727 |
| unsigned short bits = peekbits(stash, n);
|
| 137 |
727 |
| fillbuf(stash, n);
|
| 138 |
727 |
| return bits;
|
| 139 |
|
| }
|
| 140 |
|
|
|
| 141 |
|
| void
|
| 142 |
19 |
| init_bitstream(LhaStash * stash)
|
| 143 |
|
| {
|
| 144 |
|
| LhaBitstream * bitstream;
|
| 145 |
|
|
|
| 146 |
19 |
| Newxz(bitstream, 1, LhaBitstream);
|
| 147 |
19 |
| stash->bit = bitstream;
|
| 148 |
|
|
|
| 149 |
19 |
| stash->bit->blocksize = 0;
|
| 150 |
19 |
| stash->bit->readpos = 0;
|
| 151 |
19 |
| stash->bit->value = 0;
|
| 152 |
19 |
| stash->bit->buf = 0;
|
| 153 |
19 |
| stash->bit->pos = 0;
|
| 154 |
19 |
| fillbuf(stash, ushort_bit);
|
| 155 |
19 |
| }
|
| 156 |
|
|
|
| 157 |
|
| /*
|
| 158 |
|
| modified from LHa for UNIX: maketbl.c ver 1.14
|
| 159 |
|
| original author(s):
|
| 160 |
|
| Source All chagned 1995.01.14 N.Watazaki
|
| 161 |
|
| */
|
| 162 |
|
|
|
| 163 |
|
| void
|
| 164 |
56 |
| make_table(LhaStash * stash, LhaTable * table, unsigned short nchar)
|
| 165 |
|
| {
|
| 166 |
|
| unsigned short count[ushort_bit + 1];
|
| 167 |
|
| unsigned short weight[ushort_bit + 1];
|
| 168 |
|
| unsigned short start[ushort_bit + 1];
|
| 169 |
|
| unsigned short total, avail;
|
| 170 |
|
| unsigned short from, to;
|
| 171 |
|
| unsigned char bits_to_shift, bit;
|
| 172 |
|
| unsigned int i, j, l;
|
| 173 |
|
| char n;
|
| 174 |
|
| unsigned short *p;
|
| 175 |
|
|
|
| 176 |
56 |
▶ | if (table->bit > ushort_bit) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (table->bit > ushort_bit) {
|
0 |
56 |
56 |
|
| 177 |
0 |
| safe_croak(stash, "Table is broken: table bit is too large");
|
| 178 |
|
| }
|
| 179 |
|
|
|
| 180 |
952 |
▶ | for(i = 1; i <= ushort_bit; i++) {
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 1; i <= ushort_bit; i++) {
|
896 |
56 |
952 |
|
| 181 |
896 |
| count[i] = 0;
|
| 182 |
896 |
| weight[i] = 1u << (ushort_bit - i);
|
| 183 |
|
| }
|
| 184 |
|
|
|
| 185 |
10383 |
▶ | for(i = 0; i < nchar; i++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < nchar; i++)
|
10327 |
56 |
10383 |
|
| 186 |
10327 |
▶ | if (table->length[i] > ushort_bit) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (table->length[i] > ushort_bit) {
|
0 |
10327 |
10327 |
|
| 187 |
0 |
| safe_croak(stash, "Table is broken: bit length is too large");
|
| 188 |
|
| }
|
| 189 |
|
| else
|
| 190 |
10327 |
| count[table->length[i]]++;
|
| 191 |
|
|
|
| 192 |
56 |
| total = 0;
|
| 193 |
952 |
▶ | for(i = 1; i <= ushort_bit; i++) {
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 1; i <= ushort_bit; i++) {
|
896 |
56 |
952 |
|
| 194 |
896 |
| start[i] = total;
|
| 195 |
896 |
| total += weight[i] * count[i];
|
| 196 |
|
| }
|
| 197 |
56 |
▶ | if (total & ushort_max) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (total & ushort_max) {
|
0 |
56 |
56 |
|
| 198 |
0 |
| safe_croak(stash, "Table is broken: total mismatch");
|
| 199 |
|
| }
|
| 200 |
|
|
|
| 201 |
56 |
| bits_to_shift = ushort_bit - table->bit;
|
| 202 |
580 |
▶ | for(i = 1; i <= table->bit; i++) {
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 1; i <= table->bit; i++) {
|
524 |
56 |
580 |
|
| 203 |
524 |
| start[i] >>= bits_to_shift;
|
| 204 |
524 |
| weight[i] >>= bits_to_shift;
|
| 205 |
|
| }
|
| 206 |
|
|
|
| 207 |
56 |
| from = start[table->bit + 1] >> bits_to_shift;
|
| 208 |
56 |
| to = min(1u << table->bit, table->size);
|
| 209 |
56 |
▶ | if (from)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (from)
|
0 |
56 |
56 |
|
| 210 |
0 |
▶ | for(i = from; i < to; i++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i = from; i < to; i++)
|
0 |
0 |
0 |
|
| 211 |
0 |
| table->table[i] = 0;
|
| 212 |
|
|
|
| 213 |
56 |
| avail = nchar;
|
| 214 |
10383 |
▶ | for(i = 0; i < nchar; i++) {
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < nchar; i++) {
|
10327 |
56 |
10383 |
|
| 215 |
10327 |
| bit = table->length[i];
|
| 216 |
10327 |
▶ | if (bit == 0)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (bit == 0)
|
9087 |
1240 |
10327 |
|
| 217 |
9087 |
| continue;
|
| 218 |
1240 |
| l = start[bit] + weight[bit];
|
| 219 |
1240 |
▶ | if (bit <= table->bit) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (bit <= table->bit) {
|
1240 |
0 |
1240 |
|
| 220 |
1240 |
| l = min(l, table->size);
|
| 221 |
88536 |
▶ | for(j = start[bit]; j < l; j++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for(j = start[bit]; j < l; j++)
|
87296 |
1240 |
88536 |
|
| 222 |
87296 |
| table->table[j] = i;
|
| 223 |
|
| }
|
| 224 |
|
| else {
|
| 225 |
0 |
| j = start[bit];
|
| 226 |
0 |
▶ | if ((j >> bits_to_shift) > table->size) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if ((j >> bits_to_shift) > table->size) {
|
0 |
0 |
0 |
|
| 227 |
0 |
| safe_croak(stash, "Table is broken");
|
| 228 |
|
| }
|
| 229 |
0 |
| p = &(table->table[j >> bits_to_shift]);
|
| 230 |
0 |
| j <<= table->bit;
|
| 231 |
0 |
| n = bit - table->bit;
|
| 232 |
0 |
▶ | while (--n >= 0) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while (--n >= 0) {
|
0 |
0 |
0 |
|
| 233 |
0 |
▶ | if (*p == 0) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (*p == 0) {
|
0 |
0 |
0 |
|
| 234 |
0 |
| stash->tree->right[avail] = stash->tree->left[avail] = 0;
|
| 235 |
0 |
| *p = avail++;
|
| 236 |
|
| }
|
| 237 |
0 |
▶ | if (j & ushort_center)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (j & ushort_center)
|
0 |
0 |
0 |
|
| 238 |
0 |
| p = &(stash->tree->right[*p]);
|
| 239 |
|
| else
|
| 240 |
0 |
| p = &(stash->tree->left[*p]);
|
| 241 |
0 |
| j <<= 1;
|
| 242 |
|
| }
|
| 243 |
0 |
| *p = i;
|
| 244 |
|
| }
|
| 245 |
1240 |
| start[bit] = l;
|
| 246 |
|
| }
|
| 247 |
56 |
| }
|
| 248 |
|
|
|
| 249 |
|
| /*
|
| 250 |
|
| modified from LHa for UNIX: huf.c ver 1.14
|
| 251 |
|
| original authors:
|
| 252 |
|
| Source All chagned 1995.01.14 N.Watazaki
|
| 253 |
|
| Support LH7 & Bug Fixed 2000.10. 6 t.okamoto
|
| 254 |
|
| */
|
| 255 |
|
|
|
| 256 |
|
| void
|
| 257 |
38 |
| read_pt_len(LhaStash * stash, short nn, unsigned char nbit, short threshold)
|
| 258 |
|
| {
|
| 259 |
|
| int i, c;
|
| 260 |
|
| unsigned short n, mask;
|
| 261 |
|
|
|
| 262 |
38 |
| n = getbits(stash, nbit);
|
| 263 |
38 |
▶ | if (n == 0) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) {
|
1 |
37 |
38 |
|
| 264 |
1 |
| c = getbits(stash, nbit);
|
| 265 |
15 |
▶ | for(i = 0; i < nn; i++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < nn; i++)
|
14 |
1 |
15 |
|
| 266 |
14 |
| stash->pt->length[i] = 0;
|
| 267 |
257 |
▶ | for(i = 0; i < stash->pt->size; i++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < stash->pt->size; i++)
|
256 |
1 |
257 |
|
| 268 |
256 |
| stash->pt->table[i] = c;
|
| 269 |
|
| }
|
| 270 |
|
| else {
|
| 271 |
37 |
| i = 0;
|
| 272 |
383 |
▶ | while (i < min(n, stash->pt->length_size)) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while (i < min(n, stash->pt->length_size)) {
|
309 |
37 |
346 |
|
| 273 |
309 |
| c = peekbits(stash, 3);
|
| 274 |
309 |
▶ | if (c != 7)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (c != 7)
|
307 |
2 |
309 |
|
| 275 |
307 |
| fillbuf(stash, 3);
|
| 276 |
|
| else {
|
| 277 |
2 |
| mask = create_mask(3);
|
| 278 |
2 |
▶ | while (stash->bit->value & mask) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while (stash->bit->value & mask) {
|
0 |
2 |
2 |
|
| 279 |
0 |
| mask >>= 1;
|
| 280 |
0 |
| c++;
|
| 281 |
|
| }
|
| 282 |
2 |
| fillbuf(stash, (unsigned char)(c - 3));
|
| 283 |
|
| }
|
| 284 |
309 |
| stash->pt->length[i++] = c;
|
| 285 |
309 |
▶ | if (i == threshold) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (i == threshold) {
|
290 |
19 |
309 |
|
| 286 |
19 |
| c = getbits(stash, 2);
|
| 287 |
60 |
▶ | while (--c >= 0 && i < stash->pt->length_size)
|
Branch: 2 branches
| Branch | True | False | Total |
| while (--c >= 0 && i < stash->pt->length_size)
|
41 |
19 |
60 |
| while (--c >= 0 && i < stash->pt->length_size)
|
41 |
0 |
41 |
|
| 288 |
41 |
| stash->pt->length[i++] = 0;
|
| 289 |
|
| }
|
| 290 |
|
| }
|
| 291 |
324 |
▶ | while (i < nn)
|
Branch: 1 branch
| Branch | True | False | Total |
| while (i < nn)
|
287 |
37 |
324 |
|
| 292 |
287 |
| stash->pt->length[i++] = 0;
|
| 293 |
37 |
| make_table(stash, stash->pt, nn);
|
| 294 |
|
| }
|
| 295 |
38 |
| }
|
| 296 |
|
|
|
| 297 |
|
| void
|
| 298 |
19 |
| read_c_len(LhaStash * stash)
|
| 299 |
|
| {
|
| 300 |
|
| short i, c, n;
|
| 301 |
|
| unsigned short mask;
|
| 302 |
|
|
|
| 303 |
19 |
| n = getbits(stash, stash->CBIT);
|
| 304 |
19 |
▶ | if (n == 0) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) {
|
0 |
19 |
19 |
|
| 305 |
0 |
| c = getbits(stash, stash->CBIT);
|
| 306 |
0 |
▶ | for(i = 0; i < stash->c->length_size; i++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < stash->c->length_size; i++)
|
0 |
0 |
0 |
|
| 307 |
0 |
| stash->c->length[i] = 0;
|
| 308 |
0 |
▶ | for(i = 0; i < stash->c->size; i++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < stash->c->size; i++)
|
0 |
0 |
0 |
|
| 309 |
0 |
| stash->c->table[i] = c;
|
| 310 |
|
| }
|
| 311 |
|
| else {
|
| 312 |
19 |
| i = 0;
|
| 313 |
1583 |
▶ | while (i < min(n, stash->c->length_size)) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while (i < min(n, stash->c->length_size)) {
|
1564 |
19 |
1583 |
|
| 314 |
1564 |
| c = stash->pt->table[peekbits(stash, stash->pt->bit)];
|
| 315 |
1564 |
▶ | if (c >= stash->NT) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (c >= stash->NT) {
|
0 |
1564 |
1564 |
|
| 316 |
0 |
| mask = create_mask(stash->pt->bit);
|
| 317 |
|
| do {
|
| 318 |
0 |
▶ | if (stash->bit->value & mask)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (stash->bit->value & mask)
|
0 |
0 |
0 |
|
| 319 |
0 |
| c = stash->tree->right[c];
|
| 320 |
|
| else
|
| 321 |
0 |
| c = stash->tree->left[c];
|
| 322 |
0 |
| mask >>= 1;
|
| 323 |
0 |
▶ | } while (c >= stash->NT && (mask || c != stash->tree->left[c]));
|
Branch: 3 branches
| Branch | True | False | Total |
| } while (c >= stash->NT && (mask || c != stash->tree->left[c]));
|
0 |
0 |
0 |
| } while (c >= stash->NT && (mask || c != stash->tree->left[c]));
|
0 |
0 |
0 |
| } while (c >= stash->NT && (mask || c != stash->tree->left[c]));
|
0 |
0 |
0 |
|
| 324 |
|
| }
|
| 325 |
1564 |
| fillbuf(stash, stash->pt->length[c]);
|
| 326 |
1564 |
▶ | if (c <= 2) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (c <= 2) {
|
566 |
998 |
1564 |
|
| 327 |
566 |
▶ | if (c == 0)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (c == 0)
|
376 |
190 |
566 |
|
| 328 |
376 |
| c = 1;
|
| 329 |
190 |
▶ | else if (c == 1)
|
Branch: 1 branch
| Branch | True | False | Total |
| else if (c == 1)
|
164 |
26 |
190 |
|
| 330 |
164 |
| c = getbits(stash, 4) + 3;
|
| 331 |
|
| else
|
| 332 |
26 |
| c = getbits(stash, stash->CBIT) + 20;
|
| 333 |
5238 |
▶ | while (--c >= 0)
|
Branch: 1 branch
| Branch | True | False | Total |
| while (--c >= 0)
|
4672 |
566 |
5238 |
|
| 334 |
4672 |
| stash->c->length[i++] = 0;
|
| 335 |
|
| }
|
| 336 |
|
| else
|
| 337 |
998 |
| stash->c->length[i++] = c - 2;
|
| 338 |
|
| }
|
| 339 |
4039 |
▶ | while (i < stash->c->length_size)
|
Branch: 1 branch
| Branch | True | False | Total |
| while (i < stash->c->length_size)
|
4020 |
19 |
4039 |
|
| 340 |
4020 |
| stash->c->length[i++] = 0;
|
| 341 |
19 |
| make_table(stash, stash->c, stash->c->length_size);
|
| 342 |
|
| }
|
| 343 |
19 |
| }
|
| 344 |
|
|
|
| 345 |
|
| unsigned short
|
| 346 |
3130 |
| decode_c(LhaStash * stash)
|
| 347 |
|
| {
|
| 348 |
|
| unsigned short j, mask;
|
| 349 |
|
|
|
| 350 |
3130 |
▶ | if (stash->bit->blocksize == 0) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (stash->bit->blocksize == 0) {
|
19 |
3111 |
3130 |
|
| 351 |
19 |
| stash->bit->blocksize = getbits(stash, ushort_bit);
|
| 352 |
19 |
| read_pt_len(stash, stash->NT, stash->TBIT, 3);
|
| 353 |
19 |
| read_c_len(stash);
|
| 354 |
19 |
| read_pt_len(stash, stash->NP, stash->PBIT, -1);
|
| 355 |
|
| }
|
| 356 |
3130 |
| stash->bit->blocksize--;
|
| 357 |
3130 |
| j = stash->c->table[peekbits(stash, stash->c->bit)];
|
| 358 |
3130 |
▶ | if (j < stash->c->length_size)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (j < stash->c->length_size)
|
3130 |
0 |
3130 |
|
| 359 |
3130 |
| fillbuf(stash, stash->c->length[j]);
|
| 360 |
|
| else {
|
| 361 |
0 |
| fillbuf(stash, stash->c->bit);
|
| 362 |
0 |
| mask = create_mask(0);
|
| 363 |
|
| do {
|
| 364 |
0 |
▶ | if (stash->bit->value & mask)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (stash->bit->value & mask)
|
0 |
0 |
0 |
|
| 365 |
0 |
| j = stash->tree->right[j];
|
| 366 |
|
| else
|
| 367 |
0 |
| j = stash->tree->left[j];
|
| 368 |
0 |
| mask >>= 1;
|
| 369 |
0 |
▶ | } while (j >= stash->c->length_size && (mask || j != stash->tree->left[j]));
|
Branch: 3 branches
| Branch | True | False | Total |
| } while (j >= stash->c->length_size && (mask || j != stash->tree->left[j]));
|
0 |
0 |
0 |
| } while (j >= stash->c->length_size && (mask || j != stash->tree->left[j]));
|
0 |
0 |
0 |
| } while (j >= stash->c->length_size && (mask || j != stash->tree->left[j]));
|
0 |
0 |
0 |
|
| 370 |
0 |
| fillbuf(stash, (unsigned char)(stash->c->length[j] - stash->c->bit));
|
| 371 |
|
| }
|
| 372 |
3130 |
| return j;
|
| 373 |
|
| }
|
| 374 |
|
|
|
| 375 |
|
| unsigned short
|
| 376 |
443 |
| decode_p(LhaStash * stash)
|
| 377 |
|
| {
|
| 378 |
|
| unsigned short j, mask;
|
| 379 |
|
|
|
| 380 |
443 |
| j = stash->pt->table[peekbits(stash, stash->pt->bit)];
|
| 381 |
443 |
▶ | if (j < stash->NP)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (j < stash->NP)
|
443 |
0 |
443 |
|
| 382 |
443 |
| fillbuf(stash, stash->pt->length[j]);
|
| 383 |
|
| else {
|
| 384 |
0 |
| fillbuf(stash, stash->pt->bit);
|
| 385 |
0 |
| mask = create_mask(0);
|
| 386 |
|
| do {
|
| 387 |
0 |
▶ | if (stash->bit->value & mask)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (stash->bit->value & mask)
|
0 |
0 |
0 |
|
| 388 |
0 |
| j = stash->tree->right[j];
|
| 389 |
|
| else
|
| 390 |
0 |
| j = stash->tree->left[j];
|
| 391 |
0 |
| mask >>= 1;
|
| 392 |
0 |
▶ | } while (j >= stash->NP && (mask || j != stash->tree->left[j]));
|
Branch: 3 branches
| Branch | True | False | Total |
| } while (j >= stash->NP && (mask || j != stash->tree->left[j]));
|
0 |
0 |
0 |
| } while (j >= stash->NP && (mask || j != stash->tree->left[j]));
|
0 |
0 |
0 |
| } while (j >= stash->NP && (mask || j != stash->tree->left[j]));
|
0 |
0 |
0 |
|
| 393 |
0 |
| fillbuf(stash, (unsigned char)(stash->pt->length[j] - stash->pt->bit));
|
| 394 |
|
| }
|
| 395 |
443 |
▶ | if (j != 0)
|
Branch: 1 branch
| Branch | True | False | Total |
| if (j != 0)
|
441 |
2 |
443 |
|
| 396 |
441 |
| j = (1u << (j - 1)) + getbits(stash, (unsigned char)(j - 1));
|
| 397 |
443 |
| return j;
|
| 398 |
|
| }
|
| 399 |
|
|
|
| 400 |
|
| /*
|
| 401 |
|
| modified from LHa for UNIX: crcio.c ver 1.14
|
| 402 |
|
| original author(s):
|
| 403 |
|
| Source All chagned 1995.01.14 N.Watazaki
|
| 404 |
|
| */
|
| 405 |
|
|
|
| 406 |
|
| unsigned short
|
| 407 |
27 |
| calc_crc16(unsigned short crc, unsigned char * str, unsigned int len)
|
| 408 |
|
| {
|
| 409 |
12210 |
▶ | while (len-- > 0)
|
Branch: 1 branch
| Branch | True | False | Total |
| while (len-- > 0)
|
12183 |
27 |
12210 |
|
| 410 |
12183 |
| crc = crctable[(crc ^ *str++) & uchar_max] ^ (crc >> char_bit);
|
| 411 |
27 |
| return crc;
|
| 412 |
|
| }
|
| 413 |
|
|
|
| 414 |
|
| /*
|
| 415 |
|
| this is not from LHa for UNIX
|
| 416 |
|
| */
|
| 417 |
|
|
|
| 418 |
|
| void
|
| 419 |
19 |
| init_tables(HV * self, LhaStash * stash)
|
| 420 |
|
| {
|
| 421 |
|
| LhaTable * pt_table;
|
| 422 |
|
| LhaTable * c_table;
|
| 423 |
|
| LhaTree * tree;
|
| 424 |
|
|
|
| 425 |
19 |
| stash->NPT = self_ushort("NPT");
|
| 426 |
19 |
| stash->NP = self_ushort("NP");
|
| 427 |
19 |
| stash->NT = self_ushort("NT");
|
| 428 |
19 |
| stash->NC = self_ushort("NC");
|
| 429 |
19 |
| stash->PBIT = self_uchar("PBIT");
|
| 430 |
19 |
| stash->TBIT = self_uchar("TBIT");
|
| 431 |
19 |
| stash->CBIT = self_uchar("CBIT");
|
| 432 |
|
|
|
| 433 |
19 |
| Newxz(pt_table, 1, LhaTable);
|
| 434 |
19 |
| Newxz(c_table, 1, LhaTable);
|
| 435 |
19 |
| Newxz(tree, 1, LhaTree);
|
| 436 |
|
|
|
| 437 |
19 |
| pt_table->bit = self_uchar("PT_TABLE_BIT");
|
| 438 |
19 |
| pt_table->size = self_ushort("PT_TABLE_SIZE");
|
| 439 |
19 |
| pt_table->length_size = stash->NPT;
|
| 440 |
|
|
|
| 441 |
19 |
| Newxz(pt_table->table, pt_table->size, unsigned short);
|
| 442 |
19 |
| Newxz(pt_table->length, pt_table->length_size, unsigned char);
|
| 443 |
|
|
|
| 444 |
19 |
| c_table->bit = self_uchar("C_TABLE_BIT");
|
| 445 |
19 |
| c_table->size = self_ushort("C_TABLE_SIZE");
|
| 446 |
19 |
| c_table->length_size = stash->NC;
|
| 447 |
|
|
|
| 448 |
19 |
| Newxz(c_table->table, c_table->size, unsigned short);
|
| 449 |
19 |
| Newxz(c_table->length, c_table->length_size, unsigned char);
|
| 450 |
|
|
|
| 451 |
19 |
| Newxz(tree->left, 2 * stash->NC - 1, unsigned short);
|
| 452 |
19 |
| Newxz(tree->right, 2 * stash->NC - 1, unsigned short);
|
| 453 |
|
|
|
| 454 |
19 |
| stash->tree = tree;
|
| 455 |
19 |
| stash->pt = pt_table;
|
| 456 |
19 |
| stash->c = c_table;
|
| 457 |
19 |
| }
|
| 458 |
|
|
|
| 459 |
|
| MODULE = Archive::Lha PACKAGE = Archive::Lha::Decode::Base PREFIX = xs_
|
| 460 |
|
|
|
| 461 |
|
| PROTOTYPES: DISABLE
|
| 462 |
|
|
|
| 463 |
|
| #/*
|
| 464 |
|
| # modified from LHa for UNIX: slide.c ver 1.14
|
| 465 |
|
| # original authors:
|
| 466 |
|
| # Modified Nobutaka Watazaki
|
| 467 |
|
| # Ver. 1.14d Exchanging a search algorithm 1997.01.11 T.Okamoto
|
| 468 |
|
| #*/
|
| 469 |
|
|
|
| 470 |
|
| unsigned short
|
| 471 |
|
| xs_decode(hashref)
|
| 472 |
|
| SV * hashref;
|
| 473 |
|
| CODE:
|
| 474 |
|
| HV * self;
|
| 475 |
|
| unsigned char * queue;
|
| 476 |
|
| LhaStash * stash;
|
| 477 |
|
| unsigned short crc16;
|
| 478 |
|
| int i, c, matchlen, matchoff, matchpos, adjust;
|
| 479 |
|
| unsigned int dicsize, dicsize1, total, loc;
|
| 480 |
|
|
|
| 481 |
19 |
| dSP;
|
| 482 |
19 |
| self = (HV *) SvRV(hashref);
|
| 483 |
|
|
|
| 484 |
19 |
▶ | if ( !hash_exists(self, "read") )
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( !hash_exists(self, "read") )
|
0 |
19 |
19 |
|
| 485 |
0 |
| croak("'read' callback is missing");
|
| 486 |
19 |
▶ | if ( !hash_exists(self, "write") )
|
Branch: 1 branch
| Branch | True | False | Total |
| if ( !hash_exists(self, "write") )
|
0 |
19 |
19 |
|
| 487 |
0 |
| croak("'write' callback is missing");
|
| 488 |
|
|
|
| 489 |
19 |
| dicsize = self_uint("DICSIZE");
|
| 490 |
19 |
| dicsize1 = dicsize - 1;
|
| 491 |
|
|
|
| 492 |
19 |
| Newxz(queue, dicsize, unsigned char);
|
| 493 |
19 |
| Newxz(stash, 1, LhaStash);
|
| 494 |
|
|
|
| 495 |
19 |
| stash->queue = queue;
|
| 496 |
|
|
|
| 497 |
19 |
| stash->read = self_sv("read");
|
| 498 |
19 |
| stash->write = self_sv("write");
|
| 499 |
19 |
| stash->original_size = self_uint("original_size");
|
| 500 |
19 |
| stash->encoded_size = self_uint("encoded_size");
|
| 501 |
|
|
|
| 502 |
19 |
| init_tables(self, stash);
|
| 503 |
19 |
| init_bitstream(stash);
|
| 504 |
|
|
|
| 505 |
19 |
| adjust = (1u << uchar_bit) - self_uchar("THRESHOLD");
|
| 506 |
19 |
| crc16 = 0;
|
| 507 |
19 |
| loc = 0;
|
| 508 |
19 |
| total = 0;
|
| 509 |
3149 |
▶ | while ( total < stash->original_size ) {
|
Branch: 1 branch
| Branch | True | False | Total |
| while ( total < stash->original_size ) {
|
3130 |
19 |
3149 |
|
| 510 |
3130 |
| c = decode_c(stash);
|
| 511 |
3130 |
▶ | if (c <= uchar_max) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (c <= uchar_max) {
|
2687 |
443 |
3130 |
|
| 512 |
2687 |
| queue[loc++] = c;
|
| 513 |
2687 |
▶ | if (loc == dicsize) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (loc == dicsize) {
|
0 |
2687 |
2687 |
|
| 514 |
0 |
| output(stash, queue, dicsize);
|
| 515 |
0 |
| crc16 = calc_crc16(crc16, queue, dicsize);
|
| 516 |
0 |
| loc = 0;
|
| 517 |
|
| }
|
| 518 |
2687 |
| total++;
|
| 519 |
|
| }
|
| 520 |
|
| else {
|
| 521 |
443 |
| matchlen = c - adjust;
|
| 522 |
443 |
| matchoff = decode_p(stash) + 1;
|
| 523 |
443 |
| matchpos = (loc - matchoff) & dicsize1;
|
| 524 |
443 |
| total += matchlen;
|
| 525 |
5655 |
▶ | for(i = 0; i < matchlen; i++) {
|
Branch: 1 branch
| Branch | True | False | Total |
| for(i = 0; i < matchlen; i++) {
|
5212 |
443 |
5655 |
|
| 526 |
5212 |
| queue[loc++] = queue[(matchpos + i) & dicsize1];
|
| 527 |
5212 |
▶ | if (loc == dicsize) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (loc == dicsize) {
|
0 |
5212 |
5212 |
|
| 528 |
0 |
| output(stash, queue, dicsize);
|
| 529 |
0 |
| crc16 = calc_crc16(crc16, queue, dicsize);
|
| 530 |
0 |
| loc = 0;
|
| 531 |
|
| }
|
| 532 |
|
| }
|
| 533 |
|
| }
|
| 534 |
|
| }
|
| 535 |
19 |
▶ | if (loc) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (loc) {
|
19 |
0 |
19 |
|
| 536 |
19 |
| output(stash, queue, loc);
|
| 537 |
19 |
| crc16 = calc_crc16(crc16, queue, loc);
|
| 538 |
|
| }
|
| 539 |
|
|
|
| 540 |
19 |
| destroy_stash(stash);
|
| 541 |
|
|
|
| 542 |
19 |
▶ | RETVAL = crc16;
|
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = crc16;
|
18 |
1 |
19 |
|
| 543 |
|
|
|
| 544 |
|
| OUTPUT:
|
| 545 |
|
| RETVAL
|
| 546 |
|
|
|
| 547 |
|
| MODULE = Archive::Lha PACKAGE = Archive::Lha::CRC PREFIX = xs_
|
| 548 |
|
|
|
| 549 |
|
| PROTOTYPES: DISABLE
|
| 550 |
|
|
|
| 551 |
|
| #/* this is not from LHa for UNIX */
|
| 552 |
|
|
|
| 553 |
|
| unsigned short
|
| 554 |
|
| xs_update(unsigned short crc, SV * str, STRLEN len)
|
| 555 |
|
| CODE:
|
| 556 |
16 |
| RETVAL = calc_crc16(crc, SvPV(str, len), len);
|
| 557 |
|
|
|
| 558 |
|
| OUTPUT:
|
| 559 |
|
| RETVAL
|
| 560 |
|
|
|
| 561 |
|
| MODULE = Archive::Lha PACKAGE = Archive::Lha::Header::Utils PREFIX = xs_
|
| 562 |
|
|
|
| 563 |
|
| PROTOTYPES: DISABLE
|
| 564 |
|
|
|
| 565 |
|
| unsigned char
|
| 566 |
|
| xs__checksum(SV * buf, STRLEN offset)
|
| 567 |
|
| CODE:
|
| 568 |
|
| STRLEN len;
|
| 569 |
239 |
| unsigned char * s = (unsigned char *) SvPV(buf, len);
|
| 570 |
239 |
| unsigned char sum = 0;
|
| 571 |
|
| STRLEN i;
|
| 572 |
9342 |
▶ | for (i = offset; i < len; i++)
|
Branch: 1 branch
| Branch | True | False | Total |
| for (i = offset; i < len; i++)
|
9103 |
239 |
9342 |
|
| 573 |
9103 |
| sum += s[i];
|
| 574 |
239 |
▶ | RETVAL = sum;
|
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = sum;
|
232 |
7 |
239 |
|
| 575 |
|
| OUTPUT:
|
| 576 |
|
| RETVAL
|
| 577 |
|
|
|
| 578 |
|
| IV
|
| 579 |
|
| xs__dostime2utime(U32 v)
|
| 580 |
|
| CODE:
|
| 581 |
|
| struct tm t;
|
| 582 |
|
| time_t result;
|
| 583 |
5 |
▶ | if (v == 0) {
|
Branch: 1 branch
| Branch | True | False | Total |
| if (v == 0) {
|
3 |
2 |
5 |
|
| 584 |
3 |
| RETVAL = 0;
|
| 585 |
|
| } else {
|
| 586 |
2 |
| t.tm_sec = (v & 0x1F) * 2;
|
| 587 |
2 |
| t.tm_min = (v >> 5) & 0x3F;
|
| 588 |
2 |
| t.tm_hour = (v >> 11) & 0x1F;
|
| 589 |
2 |
| t.tm_mday = (v >> 16) & 0x1F;
|
| 590 |
2 |
| t.tm_mon = ((v >> 21) & 0x0F) - 1;
|
| 591 |
2 |
| t.tm_year = ((v >> 25) & 0x7F) + 80;
|
| 592 |
2 |
| t.tm_isdst = -1;
|
| 593 |
2 |
| result = mktime(&t);
|
| 594 |
2 |
▶ | RETVAL = (result == (time_t)-1) ? 0 : (IV)result;
|
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (result == (time_t)-1) ? 0 : (IV)result;
|
2 |
0 |
2 |
|
| 595 |
|
| }
|
| 596 |
|
| OUTPUT:
|
| 597 |
|
| RETVAL
|
| 598 |
|
|
|
| 599 |
|
| MODULE = Archive::Lha PACKAGE = Archive::Lha PREFIX = xs_
|
| 600 |
|
|
|
| 601 |
|
| PROTOTYPES: DISABLE
|