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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
|
/*
* $Id: encode.c,v 1.7 2004/05/31 16:08:41 andrew_belov Exp $
* ---------------------------------------------------------------------------
* The data compression procedures are located in this module.
*
*/
#include "arj.h"
#ifdef TILED
#include <dos.h> /* Weird, eh? */
#endif
DEBUGHDR(__FILE__) /* Debug information block */
static int st_n;
unsigned short *c_freq;
unsigned short FAR *c_code;
unsigned short FAR *heap;
unsigned short len_cnt[17];
unsigned short p_freq[2*NP-1];
unsigned short pt_code[NPT];
int depth;
unsigned char FAR *buf;
unsigned char output_mask;
unsigned short output_pos;
int dicbit;
unsigned short t_freq[2*NT-1];
int heapsize;
unsigned short FAR *sortptr;
unsigned char *len;
unsigned short *freq;
unsigned short fpcount;
short FAR *fpbuf;
short FAR *dtree;
unsigned short treesize;
unsigned char *tree=NULL;
unsigned short numchars;
unsigned short dicsiz_cur;
unsigned short dicpos;
unsigned short tc_passes;
short FAR *ftree;
unsigned int dic_alloc;
unsigned long encoded_bytes;
/* Inline functions */
#define encode_c(c) \
putbits(c_len[c], c_code[c])
#define encode_p(p) \
{ \
unsigned int qc, q; \
qc=0; \
q=p; \
while(q) \
{ \
q>>=1; \
qc++; \
} \
putbits(pt_len[qc], pt_code[qc]); \
if(qc>1) \
putbits(qc-1, p); \
}
/* Bitwise output routine */
void putbits(int n_c, unsigned short n_x)
{
#ifdef ASM8086
asm{
mov cl, byte ptr n_c
mov dx, n_x
mov ch, cl
sub cl, 16
neg cl
shl dx, cl
mov cl, byte ptr bitcount
mov ax, dx
shr ax, cl
or bitbuf, ax
add cl, ch
cmp cl, 8
jge chunk1
mov byte ptr bitcount, cl
jmp procend
}
chunk1:
asm{
push si
mov si, out_bytes
cmp si, out_avail
jge flush
}
acc_loop:
asm{
mov bx, out_buffer
mov ah, byte ptr bitbuf+1
mov [bx+si], ah
inc si
sub cl, 8
cmp cl, 8
jge avail_chk
#if TARGET==OS2
shl bitbuf, 8
#else
mov ah, byte ptr bitbuf
mov al, 0
mov bitbuf, ax
#endif
jmp endpoint
}
avail_chk:
asm{
cmp si, out_avail
jge r_flush
}
cpoint:
asm{
mov al, byte ptr bitbuf
mov [bx+si], al
inc si
sub cl, 8
sub ch, cl
xchg cl, ch
shl dx, cl
mov bitbuf, dx
mov cl, ch
jmp endpoint
}
flush:
asm{
push dx
push cx
}
flush_compdata();
asm{
pop cx
pop dx
mov si, out_bytes
jmp short acc_loop
}
r_flush:
asm{
mov out_bytes, si
push dx
push cx
push bx
}
flush_compdata();
asm{
pop bx
pop cx
pop dx
mov si, out_bytes
jmp short cpoint
}
endpoint:
asm{
mov out_bytes, si
pop si
mov byte ptr bitcount, cl
}
procend:;
#else
int p_n;
int bt;
p_n=n_c;
n_c=16-n_c;
n_x<<=n_c;
n_c=bitcount;
bitbuf|=(n_x>>n_c);
n_c+=p_n;
if(n_c<8)
{
bitcount=n_c;
return;
}
bt=out_bytes;
if(bt>=out_avail)
{
flush_compdata();
bt=out_bytes;
}
out_buffer[bt++]=bitbuf>>8;
n_c-=8;
if(n_c<8)
{
bitbuf<<=8;
out_bytes=bt;
bitcount=n_c;
return;
}
if(bt>=out_avail)
{
out_bytes=bt;
flush_compdata();
bt=out_bytes;
}
out_buffer[bt++]=bitbuf;
n_c-=8;
p_n-=n_c;
bitbuf=n_x<<p_n;
out_bytes=bt;
bitcount=n_c;
#endif
}
/* Quick fill routine */
static void fill_fpbuf()
{
#ifdef ASM8086
asm{
push di
cld
mov ax, 65535
les di, fpbuf
mov cx, fpcount
rep stosw
pop di
}
#else
unsigned int i;
for(i=0; i<fpcount; i++)
fpbuf[i]=-1;
#endif
}
/* Reduces the number of bits for smaller files */
void adjust_dicbit()
{
if(uncompsize<16384)
dicbit=12;
}
/* Reads data from the source file or a memory region */
#if SFX_LEVEL>=ARJ
int fetch_uncomp(char *dest, int n)
{
unsigned int fetch_size;
if(file_packing)
return(fread_crc(dest, n, encstream));
else
{
if(encmem_remain==0)
return(0);
fetch_size=min((unsigned int)n, encmem_remain);
far_memmove((char FAR *)dest, encblock_ptr, fetch_size);
crc32_for_block(dest, fetch_size);
encmem_remain-=fetch_size;
encblock_ptr+=fetch_size;
return(fetch_size);
}
}
#endif
/* Fills the length table depending on the leaf depth (call with i==root) */
static void count_len(int i)
{
static int depth=0;
if(i<st_n)
len_cnt[(depth<16)?depth:16]++;
else
{
depth++;
count_len(left[i]);
count_len(right[i]);
depth--;
}
}
/* Makes length counter table */
static void NEAR make_len(int root)
{
int i, k;
unsigned short cum;
for(i=0; i<=16; i++)
len_cnt[i]=0;
count_len(root);
cum=0;
for(i=16; i>0; i--)
cum+=len_cnt[i]<<(16-i);
while(cum!=0)
{
if(debug_enabled&&strchr(debug_opt, 'f')!=NULL)
msg_cprintf(0, M_HDF_FIX);
len_cnt[16]--;
for(i=15; i>0; i--)
{
if(len_cnt[i]!=0)
{
len_cnt[i]--;
len_cnt[i+1]+=2;
break;
}
}
cum--;
}
for(i=16; i>0; i--)
{
k=len_cnt[i];
while(--k>=0)
len[*sortptr++]=i;
}
}
/* Sends i-th entry down the heap */
static void NEAR downheap(int i)
{
int j, k;
k=heap[i];
while((j=2*i)<=heapsize)
{
if(j<heapsize&&freq[heap[j]]>freq[heap[j+1]])
j++;
if(freq[k]<=freq[heap[j]])
break;
heap[i]=heap[j];
i=j;
}
heap[i]=k;
}
/* Encodes a length table element */
static void NEAR make_code(int n, unsigned char *len, unsigned short FAR *code)
{
int i;
unsigned short start[18];
start[1]=0;
for(i=1; i<=16; i++)
start[i+1]=(start[i]+len_cnt[i])<<1;
for(i=0; i<n; i++)
code[i]=start[len[i]]++;
}
/* Makes tree, calculates len[], returns root */
static int make_tree(int nparm, unsigned short *freqparm, unsigned char *lenparm, unsigned short FAR *codeparm)
{
int i, j, k, avail;
st_n=nparm;
freq=freqparm;
len=lenparm;
avail=st_n;
heapsize=0;
heap[1]=0;
for(i=0; i<st_n; i++)
{
len[i]=0;
if(freq[i]!=0)
heap[++heapsize]=i;
}
if(heapsize<2)
{
codeparm[heap[1]]=0;
return(heap[1]);
}
for(i=heapsize>>1; i>=1; i--)
downheap(i); /* Make priority queue */
sortptr=codeparm;
/* While queue has at least two entries */
do
{
i=heap[1]; /* Take out least-freq entry */
if(i<st_n)
*(sortptr++)=i;
heap[1]=heap[heapsize--];
downheap(1);
j=heap[1]; /* Next least-freq entry */
if(j<st_n)
*(sortptr++)=j;
k=avail++; /* Generate new node */
freq[k]=freq[i]+freq[j];
heap[1]=k;
downheap(1); /* Put into queue */
left[k]=i;
right[k]=j;
} while(heapsize>1);
sortptr=codeparm;
make_len(k);
make_code(nparm, lenparm, codeparm);
return(k); /* Return root */
}
/* Counts the cumulative frequency */
void count_t_freq()
{
int i, k, n, count;
for(i=0; i<NT; i++)
t_freq[i]=0;
n=NC;
while(n>0&&c_len[n-1]==0)
n--;
i=0;
while(i<n)
{
k=c_len[i++];
if(k==0)
{
count=1;
while(i<n&&c_len[i]==0)
{
i++;
count++;
}
if(count<=2)
t_freq[0]+=count;
else if(count<=18)
t_freq[1]++;
else if(count==19)
{
t_freq[0]++;
t_freq[1]++;
}
else
t_freq[2]++;
}
else
t_freq[k+2]++;
}
}
/* Writes the encoded length */
void write_pt_len(int n, int nbit, int i_special)
{
int i, k;
while(n>0&&pt_len[n-1]==0)
n--;
putbits(nbit, n);
i=0;
while(i<n)
{
k=(int)pt_len[i++];
if(k<=6)
{
putbits(3, k);
}
else
putbits(k-3, 0xFFFE);
if(i==i_special)
{
while(i<6&&pt_len[i]==0)
i++;
putbits(2, i-3);
}
}
}
/* Writes character length */
void write_c_len()
{
int i, k, n, count;
n=NC;
while(n>0&&c_len[n-1]==0)
n--;
putbits(CBIT, n);
i=0;
while(i<n)
{
k=(int)c_len[i++];
if(k==0)
{
count=1;
while(i<n&&c_len[i]==0)
{
i++;
count++;
}
if(count<=2)
{
for(k=0; k<count; k++)
putbits(pt_len[0], pt_code[0]);
}
else if(count<=18)
{
putbits(pt_len[1], pt_code[1]);
putbits(4, count-3);
}
else if(count==19)
{
putbits(pt_len[0], pt_code[0]);
putbits(pt_len[1], pt_code[1]);
putbits(4, 15);
}
else
{
putbits(pt_len[2], pt_code[2]);
putbits(CBIT, count-20);
}
}
else
putbits(pt_len[k+2], pt_code[k+2]);
}
}
/* Encodes a block and writes it to the output file */
void send_block()
{
unsigned int i, k, flags=0, root, pos, size;
unsigned int c;
if(!unpackable)
{
root=make_tree(NC, c_freq, c_len, c_code);
size=c_freq[root];
putbits(16, size);
if(root>=NC)
{
count_t_freq();
root=make_tree(NT, t_freq, pt_len, (unsigned short FAR *)pt_code);
if(root>=NT)
write_pt_len(NT, TBIT, 3);
else
{
putbits(TBIT, 0);
putbits(TBIT, root);
}
write_c_len();
}
else
{
putbits(TBIT, 0);
putbits(TBIT, 0);
putbits(CBIT, 0);
putbits(CBIT, root);
}
root=make_tree(NP, p_freq, pt_len, (unsigned short FAR *)pt_code);
if(root>=NP)
write_pt_len(NP, PBIT, -1);
else
{
putbits(PBIT, 0);
putbits(PBIT, root);
}
pos=0;
for(i=0; i<size; i++)
{
if(unpackable)
return;
if(i%CHAR_BIT==0)
flags=buf[pos++];
else
flags<<=1;
if(flags&(1U<<(CHAR_BIT-1)))
{
c=(unsigned int)buf[pos++]+(1<<CHAR_BIT);
encode_c(c);
k=buf[pos++];
k|=(unsigned int)buf[pos++]<<CHAR_BIT;
encode_p(k);
}
else
{
c=buf[pos++];
encode_c(c);
}
}
for(i=0; i<NC; i++)
c_freq[i]=0;
for(i=0; i<NP; i++)
p_freq[i]=0;
}
}
/* Handy macro for retrieval of two bytes (don't care about the portability) */
#ifdef ALIGN_POINTERS
#define word_ptr(c) ( (((unsigned char *) (c))[0]<<8)+ ((unsigned char *) (c))[1] )
#define _diff(c1,c2) ( (c1)[0]!=(c2)[0] || (c1)[1]!=(c2)[1] )
#elif !defined(TILED)
#define word_ptr(c) (*(unsigned short *)(c))
#endif
/* Tree update routine. Possibly the most time-consuming one. */
static int upd_tree(int n_c)
{
#ifdef ASM8086
asm{
push bp
push si
push di
mov si, n_c
mov bp, 1
mov es, word ptr dtree+2
mov bx, si
mov bx, es:[bx+si]
or bx, bx
jl done_dup
}
ok:
asm{
mov di, tree
add si, di
add di, bx
mov ax, [si]
cld
mov dx, numchars
dec dx
jge ut_loop
}
done_dup:
asm{
jmp done
}
ut_fetch:
asm{
mov ax, [bp+si-1]
}
select_pos:
asm{
mov di, tree
add di, bp
}
ut_loopbody:
asm{
dec dx
jl ut_brk
shl bx, 1
mov bx, es:[bx]
or bx, bx
jl ut_brk
cmp ax, [bx+di-1]
jz ut_rcount
dec dx
shl bx, 1
mov bx, es:[bx]
or bx, bx
jl ut_brk
cmp ax, [bx+di-1]
je ut_rcount
dec dx
shl bx, 1
mov bx, es:[bx]
or bx, bx
jl ut_brk
cmp ax, [bx+di-1]
je ut_rcount
dec dx
shl bx, 1
mov bx, es:[bx]
or bx, bx
jl ut_brk
cmp ax, [bx+di-1]
je ut_rcount
dec dx
shl bx, 1
mov bx, es:[bx]
or bx, bx
jl ut_brk
cmp ax, [bx+di-1]
je ut_rcount
dec dx
shl bx, 1
mov bx, es:[bx]
or bx, bx
jl ut_brk
cmp ax, [bx+di-1]
je ut_rcount
jmp short ut_loopbody
}
ut_brk:
asm{
jmp short upd_finish
}
ut_rcount:
asm{
sub di, bp
add di, bx
}
ut_loop:
asm{
mov cx, [si]
cmp cx, [di]
jnz select_pos
mov ax, ds
mov es, ax
mov ax, di
add di, 2
add si, 2
mov cx, 128
repe cmpsw
mov cl, [di-2]
sub cl, [si-2]
xchg ax, di
sub ax, di
sub si, ax
sub cl, 1
adc ax, -2
mov es, word ptr dtree+2
cmp ax, bp
jg verify_pos
jmp ut_fetch
}
verify_pos:
asm{
mov cx, si
sub cx, di
cmp cx, dicsiz_cur
jg upd_finish
dec cx
mov dicpos, cx
mov bp, ax
cmp bp, 256
jge upd_finish
jmp ut_fetch
}
upd_finish:
asm{
cmp bp, 256
jle done
mov bp, 256
}
done:
asm{
mov ax, bp
mov tc_passes, ax
pop di
pop si
pop bp
}
return(tc_passes);
#else
short FAR *dptr;
short r_bx, r_dx;
unsigned short r_ax;
short r_bp=1;
unsigned char *tptr;
unsigned char *tdptr;
unsigned char *prev_tptr, *prev_tdptr;
int c;
char diff;
int remainder;
dptr=dtree;
if((r_bx=dptr[n_c])>=0)
{
tptr=tree+n_c;
tdptr=tree+r_bx;
r_ax=word_ptr(tptr);
r_dx=numchars;
if(--r_dx>=0)
goto ut_loop;
ut_fetch:
r_ax=word_ptr(tptr+r_bp-1);
select_pos:
tdptr=tree+r_bp;
do
{
if(--r_dx<0)
goto upd_finish;
r_bx=dptr[r_bx];
if(r_bx<0)
goto upd_finish;
} while(r_ax!=word_ptr(tdptr+r_bx-1));
tdptr+=r_bx-r_bp;
ut_loop:
#ifdef ALIGN_POINTERS
if (_diff(tptr,tdptr))
#else
if(word_ptr(tptr)!=word_ptr(tdptr))
#endif
goto select_pos;
prev_tptr=tptr;
prev_tdptr=tdptr;
tptr+=2;
tdptr+=2;
for(c=128; c>0; c--)
{
#ifdef ALIGN_POINTERS
if (_diff(tptr,tdptr))
#else
if(word_ptr(tptr)!=word_ptr(tdptr))
#endif
break;
tptr+=2;
tdptr+=2;
}
diff=*(tdptr)-*(tptr);
remainder=tdptr-prev_tdptr;
if(diff==0)
remainder+=1;
tptr=prev_tptr;
tdptr=prev_tdptr;
if(remainder<=r_bp)
goto ut_fetch;
if(tptr-tdptr<=dicsiz_cur)
{
dicpos=tptr-tdptr-1;
r_bp=remainder;
if(r_bp<256)
goto ut_fetch;
}
upd_finish:
if(r_bp>256)
r_bp=256;
}
return(tc_passes=r_bp);
#endif
}
/* Optimized output routine */
static void output(int c, unsigned short p)
{
unsigned char FAR *bptr;
unsigned char cy;
unsigned short r_dx;
unsigned short r_bx;
bptr=buf;
r_dx=cpos;
cy=output_mask&1;
output_mask=(cy?0x80:0)|((output_mask&0xFF)>>1);
if(cy)
{
if(r_dx>=bufsiz)
{
send_block();
if(unpackable)
{
cpos=bptr-buf;
return;
}
r_dx=0;
}
output_pos=r_dx;
buf[r_dx]=0;
r_dx++;
}
bptr+=r_dx;
*bptr++=c;
c_freq[c]++;
if(c>=256)
{
buf[output_pos]|=output_mask;
*bptr++=p&0xFF;
*bptr++=(p>>8);
for(r_bx=0; p!=0; p>>=1)
r_bx++;
p_freq[r_bx]++;
}
cpos=bptr-buf;
}
/* Unstubbed optimized output routine */
static void output_opt(unsigned char c)
{
unsigned char FAR *bptr, FAR *cptr;
unsigned short r_dx;
unsigned char cy;
cptr=bptr=buf;
r_dx=cpos;
cy=output_mask&1;
output_mask=(cy?0x80:0)|((output_mask&0xFF)>>1);
if(cy)
{
if(r_dx>=bufsiz)
{
send_block();
r_dx=0;
if(unpackable)
{
cpos=r_dx;
return;
}
}
output_pos=r_dx;
cptr[r_dx]=0;
r_dx++;
}
bptr+=r_dx;
*bptr++=c;
c_freq[c]++;
cpos=bptr-cptr;
}
/* Initializes memory for encoding */
void allocate_memory()
{
int i;
if((c_freq=calloc(NC*2-1, sizeof(*c_freq)))==NULL)
error(M_OUT_OF_NEAR_MEMORY);
if((c_code=farcalloc(NC, sizeof(*c_code)))==NULL)
error(M_OUT_OF_MEMORY);
if((heap=farcalloc(NC+1, sizeof(*heap)))==NULL)
error(M_OUT_OF_MEMORY);
for(i=0; i<NP; i++)
p_freq[i]=0;
depth=0;
bufsiz=current_bufsiz;
if(bufsiz>=MAX_BUFSIZ-BUFSIZ_INCREMENT)
bufsiz=MAX_BUFSIZ-1;
#ifdef FINETUNE_BUFSIZ
i=1;
#endif
/* Adjust the buffer size if there's not enough memory for it */
while((buf=farmalloc(bufsiz))==NULL)
{
#ifndef FINETUNE_BUFSIZ
bufsiz=bufsiz/10U*9U;
#else
if(i<2048)
bufsiz-=i++;
else
bufsiz=bufsiz/16U*15U;
#endif
if(bufsiz<MIN_BUFSIZ)
error(M_OUT_OF_MEMORY);
}
if(debug_enabled&&strchr(debug_opt, 'v')!=NULL)
msg_cprintf(0, M_BUFSIZ, bufsiz);
init_putbits();
output_mask=1;
output_pos=0;
cpos=0;
buf[output_pos]='\0';
bufsiz-=30;
}
/* Shutdown the encoding */
void NEAR huf_encode_end()
{
if(!unpackable)
send_block();
shutdown_putbits();
free(c_freq);
farfree(c_code);
farfree(heap);
farfree(buf);
bufsiz=0;
cpos=0;
}
/* Basic encoding routine */
static void NEAR huf_encode()
{
int hash_bits;
unsigned short fp_max;
int nchars;
int i, j, m=0;
short k, l;
unsigned short t; /* Exchange variable */
int tree_el;
unsigned int n_passes;
unsigned int f_dicpos;
unsigned int fetch;
unsigned int max_fetch; /* For comparison */
short FAR *fptr;
short FAR *dtptr;
unsigned char *tptr;
unsigned short r_cx, r_dx, r_ax;
int pm;
hash_bits=(dicbit+2)/3;
fpcount=1U<<dicbit;
fp_max=fpcount-1;
if(tree==NULL)
{
if((tree=calloc(treesize+2, sizeof(*tree)))==NULL)
error(M_OUT_OF_NEAR_MEMORY);
#ifdef ASM8086
ftree=farcalloc_based((unsigned long)treesize+16L, sizeof(*ftree));
dtree=(FP_OFF(ftree)==0)?ftree:MK_FP(FP_SEG(ftree)+((FP_OFF(ftree)+15)>>4), 0);
#else
ftree=dtree=farcalloc((unsigned long)treesize+16L, sizeof(*ftree));
#endif
fpbuf=farcalloc((unsigned long)fpcount+4L, 2L);
if(ftree==NULL||fpbuf==NULL)
error(M_OUT_OF_MEMORY);
}
if(dic_alloc<1024)
dic_alloc=1024;
allocate_memory();
nchars=(UCHAR_MAX+THRESHOLD)*2;
display_indicator(0L);
encoded_bytes=0L;
tc_passes=0;
dicpos=0;
i=j=0;
while(!unpackable)
{
tree_el=0;
k=0;
if(j!=0)
{
tree_el=dic_alloc;
if((k=j-tree_el)<=0)
{
k=0;
tree_el=j;
}
else
memmove(tree, tree+k, tree_el);
}
max_fetch=fetch=(unsigned int)(treesize-tree_el);
if(multivolume_option)
fetch=check_multivolume(fetch);
if(max_fetch!=fetch)
nchars=4;
if((fetch=fetch_uncomp(tree+tree_el, fetch))==0)
{
if(tree_el==0||k==0)
break;
memmove(tree+k, tree, tree_el);
dicsiz_cur=min(tree_el-i-1, dicsiz_cur);
break;
}
j=fetch+tree_el;
encoded_bytes+=(unsigned long)fetch;
display_indicator(encoded_bytes);
m=0;
if(k<=0)
fill_fpbuf();
else
{
fptr=fpbuf;
for(l=fpcount>>2; l>0; l--)
{
*fptr=max(*fptr-k, -1);
fptr++;
*fptr=max(*fptr-k, -1);
fptr++;
*fptr=max(*fptr-k, -1);
fptr++;
*fptr=max(*fptr-k, -1);
fptr++;
}
dtptr=dtree;
for(l=tree_el>>3; l>0; l--)
{
*dtptr=max(dtptr[k]-k, -1);
dtptr++;
*dtptr=max(dtptr[k]-k, -1);
dtptr++;
*dtptr=max(dtptr[k]-k, -1);
dtptr++;
*dtptr=max(dtptr[k]-k, -1);
dtptr++;
*dtptr=max(dtptr[k]-k, -1);
dtptr++;
*dtptr=max(dtptr[k]-k, -1);
dtptr++;
*dtptr=max(dtptr[k]-k, -1);
dtptr++;
*dtptr=max(dtptr[k]-k, -1);
dtptr++;
}
/* Store the remainder */
for(l=tree_el%8; l>0; l--)
{
*dtptr=max(dtptr[k]-k, -1);
dtptr++;
}
m+=tree_el;
if(m>=2)
m-=2;
}
tptr=&tree[m];
r_dx=(unsigned short)*(tptr++);
r_cx=(fp_max&0xFF00)|(hash_bits&0xFF);
r_dx<<=(hash_bits&0xFF);
r_dx^=(unsigned short)*(tptr++);
r_dx&=(r_cx|0xFF);
for(l=j-2; m<l; m++)
{
r_dx<<=(r_cx&0xFF);
r_dx^=*(tptr++);
r_dx&=(r_cx|0xFF);
t=fpbuf[r_dx];
fpbuf[r_dx]=m;
dtree[m]=t;
}
dtree[m]=-1;
dtree[m+1]=-1;
m=tree_el-i;
i+=fetch;
while(i>nchars)
{
i--;
pm=m;
m++;
n_passes=(unsigned int)tc_passes;
f_dicpos=(int)dicpos;
if((r_ax=upd_tree(m))>i)
r_ax=tc_passes=i;
if(n_passes<THRESHOLD||(n_passes==THRESHOLD&&f_dicpos>16384)||--r_ax>n_passes||(r_ax==n_passes&&dicpos>>1<f_dicpos))
{
output_opt(tree[pm]);
}
else
{
i-=n_passes-1;
m+=n_passes-1;
r_ax=n_passes+UCHAR_MAX-2;
output(r_ax, f_dicpos);
upd_tree(m);
if(tc_passes>i)
tc_passes=i;
}
}
}
while(i>0)
{
i--;
pm=m;
m++;
n_passes=tc_passes;
f_dicpos=dicpos;
if((r_ax=upd_tree(m))>i)
r_ax=tc_passes=i;
if(n_passes<THRESHOLD||(n_passes==THRESHOLD&&f_dicpos>16384)||--r_ax>n_passes||(r_ax==n_passes&&dicpos>>1<f_dicpos))
{
output_opt(tree[pm]);
}
else
{
i-=n_passes-1;
m+=n_passes-1;
r_ax=n_passes+UCHAR_MAX-2;
output(r_ax, f_dicpos);
if((r_ax=upd_tree(m))>i)
tc_passes=i;
}
}
huf_encode_end();
#ifdef ASM8086
farfree_based(ftree);
#else
farfree(ftree);
#endif
farfree(fpbuf);
free(tree);
tree=NULL;
}
/* Encoding stub for -m3 */
static void NEAR huf_encode_m3()
{
int hash_bits;
unsigned short fp_max;
int i, j, m;
unsigned short t; /* Exchange variable */
short k, l;
int tree_el;
unsigned int fetch;
unsigned char *tptr;
unsigned short r_cx, r_dx;
short r_ax;
hash_bits=(dicbit+2)/3;
fpcount=1U<<dicbit;
fp_max=fpcount-1;
if(tree==NULL)
{
if((tree=calloc(treesize+2, sizeof(*tree)))==NULL)
error(M_OUT_OF_NEAR_MEMORY);
#ifdef ASM8086
ftree=farcalloc_based((unsigned long)treesize+16L, sizeof(*ftree));
dtree=(FP_OFF(ftree)==0)?ftree:MK_FP(FP_SEG(ftree)+((FP_OFF(ftree)+15)>>4), 0);
#else
ftree=dtree=farcalloc((unsigned long)treesize+16L, sizeof(*ftree));
#endif
fpbuf=farcalloc((unsigned long)fpcount+4L, 2L);
if(ftree==NULL||fpbuf==NULL)
error(M_OUT_OF_MEMORY);
}
allocate_memory();
display_indicator(encoded_bytes=0L);
j=0;
while(!unpackable)
{
tree_el=0;
if(dic_alloc!=0&&j!=0)
{
tree_el=dic_alloc;
if((k=j-tree_el)<=0)
{
k=0;
tree_el=j;
}
else
memmove(tree, tree+k, tree_el);
}
fetch=(unsigned int)(treesize-tree_el);
if(multivolume_option)
fetch=check_multivolume(fetch);
if((fetch=fetch_uncomp(tree+tree_el, fetch))==0)
break;
else
{
j=fetch+tree_el;
encoded_bytes+=(unsigned long)fetch;
display_indicator(encoded_bytes);
fill_fpbuf();
l=fetch;
tptr=tree;
r_dx=(unsigned short)*(tptr++);
r_cx=(fp_max&0xFF00)|(hash_bits&0xFF);
r_dx<<=(hash_bits&0xFF);
r_dx^=(unsigned short)*(tptr++);
r_dx&=(r_cx|0xFF);
for(m=0; m<j; m++)
{
r_dx<<=(r_cx&0xFF);
r_dx^=*(tptr++);
r_dx&=(r_cx|0xFF);
t=fpbuf[r_dx];
fpbuf[r_dx]=m;
dtree[m]=t;
}
i=l;
m=tree_el;
while(i>0)
{
r_ax=upd_tree(m);
if((r_ax=upd_tree(m))>i)
r_ax=tc_passes=i;
if(r_ax<THRESHOLD)
{
r_ax=(unsigned short)tree[m];
output(r_ax, 0);
m++;
i--;
}
else
{
r_ax+=UCHAR_MAX-2;
output(r_ax, dicpos);
m+=tc_passes;
i-=tc_passes;
}
}
}
}
huf_encode_end();
#ifdef ASM8086
farfree_based(ftree);
#else
farfree(ftree);
#endif
farfree(fpbuf);
free(tree);
tree=NULL;
}
/* Encodes a single file. */
void encode(int method)
{
char *dsw; /* Debug switch (-hd) pointer */
char a;
numchars=UCHAR_MAX+5;
dicbit=14;
dic_alloc=DICSIZ;
treesize=31744;
dicsiz_cur=DICSIZ-2;
adjust_dicbit();
/* Method 0 (store) is already filtered away at this point */
switch(method)
{
case 1:
numchars=UCHAR_MAX+5;
break;
case 2:
treesize=30720;
numchars=72;
dic_alloc=20480;
break;
case 3:
treesize=30720;
numchars=32;
dic_alloc=8192;
break;
default:
error(M_UNKNOWN_METHOD);
}
switch(max_compression)
{
case 1:
numchars=3000;
dic_alloc=DICSIZ+512;
break;
case 2:
numchars=512;
dic_alloc=DICSIZ+512;
break;
case 3:
numchars=1024;
dicbit=12;
treesize=20480;
dicsiz_cur=dic_alloc=16384;
break;
case 4:
numchars=1024;
dicbit=12;
treesize=12288;
dicsiz_cur=dic_alloc=8192;
}
if(debug_enabled)
{
dsw=debug_opt;
while(*dsw!='\0')
{
a=*(dsw++);
switch(a)
{
case 'd': /* Dictionary size */
dicsiz_cur=(unsigned int)strtol(dsw, &dsw, 10);
break;
case 'g': /* G-size */
dic_alloc=(int)strtol(dsw, &dsw, 10);
break;
case 'h':
dicbit=(int)strtol(dsw, &dsw, 10);
break;
case 'm':
numchars=(int)strtol(dsw, &dsw, 10);
break;
case 'w':
treesize=(int)strtol(dsw, &dsw, 10);
break;
}
}
if(strchr(debug_opt, 'v')!=NULL)
msg_cprintf(0, M_PRECOMP_STAT, numchars, dicbit, dicsiz_cur, dic_alloc, treesize);
}
if(dicsiz_cur>DICSIZ_MAX)
error(M_LARGE_DICTIONARY);
if(dic_alloc>treesize)
error(M_LARGE_GSIZE);
if(method==3)
huf_encode_m3();
else
huf_encode();
}
/* Fast search stub for method 4 */
static void enc4_pass1(int n_c)
{
#ifdef ASM8086
asm{
mov dx, n_c
mov bx, 1
mov cx, 0
cmp dx, bx
jl cease_search
}
binsearch:
asm{
sub dx, bx
inc cx
shl bx, 1
cmp dx, bx
jl cease_search
sub dx, bx
inc cx
shl bx, 1
cmp dx, bx
jl cease_search
sub dx, bx
inc cx
shl bx, 1
cmp dx, bx
jl cease_search
sub dx, bx
inc cx
shl bx, 1
cmp dx, bx
jl cease_search
sub dx, bx
inc cx
shl bx, 1
cmp dx, bx
jl cease_search
jmp binsearch
}
cease_search:
asm{
or cx, cx
jz chk_ctr_bounds
push dx
push cx
mov ax, 65535
push ax
push cx
call far ptr putbits
add sp, 4
pop cx
pop dx
}
chk_ctr_bounds:
asm{
cmp cx, 7
jge p1exit
inc cx
}
p1exit:
asm{
push dx
push cx
call far ptr putbits
add sp, 4
}
#else
short r_bx=1;
int r_cx=0;
while(n_c>=r_bx)
{
n_c-=r_bx;
r_cx++;
r_bx<<=1;
}
if(r_cx!=0)
putbits(r_cx, -1);
if(r_cx<7)
r_cx++;
putbits(r_cx, n_c);
#endif
}
/* Dictionary position lookup */
static void enc4_pass2(int n_c)
{
#ifdef ASM8086
asm{
mov dx, n_c
mov bx, 512
mov cx, 9
cmp dx, bx
jl p2_cease_search
}
p2_bsearch:
asm{
sub dx, bx
inc cx
shl bx, 1
cmp dx, bx
jl p2_cease_search
sub dx, bx
inc cx
shl bx, 1
cmp dx, bx
jl p2_cease_search
sub dx, bx
inc cx
shl bx, 1
cmp dx, bx
jl p2_cease_search
sub dx, bx
inc cx
shl bx, 1
cmp dx, bx
jl p2_cease_search
sub dx, bx
inc cx
shl bx, 1
cmp dx, bx
jge p2_bsearch
}
p2_cease_search:
asm{
mov ch, cl
sub cl, 9
jz p2_chk_ctr
push cx
push dx
mov ax, 65535
push ax
push cx
call far ptr putbits
add sp, 4
pop dx
pop cx
}
p2_chk_ctr:
asm{
cmp ch, 13
jge p2exit
inc ch
}
p2exit:
asm{
mov cl, ch
push dx
push cx
call far ptr putbits
add sp, 4
}
#else
unsigned short r_bx=1<<9;
int r_cx=9;
while(n_c>=r_bx)
{
n_c-=r_bx;
r_cx++;
r_bx<<=1;
}
if(r_cx!=9)
putbits(r_cx-9, -1);
if(r_cx<13)
r_cx++;
putbits(r_cx, n_c);
#endif
}
/* Encodes a single file, using method 4 */
void encode_f()
{
int fetch;
int hash_bits;
unsigned short fp_max;
unsigned char *tptr;
int i, m;
unsigned short r_cx, r_dx, r_ax;
unsigned short t;
dicbit=14;
numchars=32;
dicsiz_cur=15800;
treesize=30720;
adjust_dicbit();
hash_bits=(dicbit+2)/3;
fpcount=1U<<dicbit;
fp_max=fpcount-1;
if(tree==NULL)
{
if((tree=calloc(treesize+2, sizeof(*tree)))==NULL)
error(M_OUT_OF_NEAR_MEMORY);
#ifdef ASM8086
ftree=farcalloc_based((unsigned long)treesize+16L, sizeof(*ftree));
dtree=(FP_OFF(ftree)==0)?ftree:MK_FP(FP_SEG(ftree)+((FP_OFF(ftree)+15)>>4), 0);
#else
ftree=dtree=farcalloc((unsigned long)treesize+16L, sizeof(*ftree));
#endif
fpbuf=farcalloc((unsigned long)fpcount+4L, 2L);
if(ftree==NULL||fpbuf==NULL)
error(M_OUT_OF_MEMORY);
}
init_putbits();
cpos=0;
display_indicator(encoded_bytes=0L);
while(!unpackable)
{
fetch=treesize;
if(multivolume_option)
fetch=check_multivolume(fetch);
if((fetch=fetch_uncomp(tree, fetch))==0)
break;
encoded_bytes+=(unsigned long)fetch;
display_indicator(encoded_bytes);
fill_fpbuf();
m=0;
tptr=tree;
r_dx=(unsigned short)*(tptr++);
r_cx=(fp_max&0xFF00)|(hash_bits&0xFF);
r_dx<<=(hash_bits&0xFF);
r_dx^=(unsigned short)*(tptr++);
r_dx&=(r_cx|0xFF);
for(m=0; m<fetch; m++)
{
r_dx<<=(r_cx&0xFF);
r_dx^=(unsigned char)*(tptr++);
r_dx&=(r_cx|0xFF);
t=fpbuf[r_dx];
fpbuf[r_dx]=m;
dtree[m]=t;
}
m=0;
i=fetch;
while(i>0)
{
if((r_ax=upd_tree(m))>i)
r_ax=tc_passes=i;
if(r_ax<THRESHOLD)
{
putbits(9, (unsigned char)tree[m]);
i--;
m++;
}
else
{
i-=tc_passes;
m+=tc_passes;
r_ax=tc_passes-2;
enc4_pass1(r_ax);
enc4_pass2(dicpos);
}
}
}
shutdown_putbits();
farfree(fpbuf);
#ifdef ASM8086
farfree_based(ftree);
#else
farfree(ftree);
#endif
free(tree);
tree=NULL;
}
|