huffencode.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227
  1. /*
  2. ** Command & Conquer Generals Zero Hour(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. // Copyright (C) Electronic Arts Canada Inc. 1995-2002. All rights reserved.
  19. #ifndef __HUFWRITE
  20. #define __HUFWRITE 1
  21. #include <string.h>
  22. #include "codex.h"
  23. #include "huffcodex.h"
  24. /****************************************************************/
  25. /* Internal Functions */
  26. /****************************************************************/
  27. struct HUFFMemStruct
  28. {
  29. char *ptr;
  30. int len;
  31. };
  32. #define HUFFBIGNUM 32000
  33. #define HUFFTREESIZE 520
  34. #define HUFFCODES 256
  35. #define HUFFMAXBITS 16
  36. #define HUFFREPTBL 252
  37. struct HuffEncodeContext
  38. {
  39. char qleapcode[HUFFCODES];
  40. unsigned int count[768];
  41. unsigned int bitnum[HUFFMAXBITS+1];
  42. unsigned int repbits[HUFFREPTBL];
  43. unsigned int repbase[HUFFREPTBL];
  44. unsigned int tree_left[HUFFTREESIZE];
  45. unsigned int tree_right[HUFFTREESIZE];
  46. unsigned int bitsarray[HUFFCODES];
  47. unsigned int patternarray[HUFFCODES];
  48. unsigned int masks[17];
  49. unsigned int packbits;
  50. unsigned int workpattern;
  51. unsigned char *buffer;
  52. unsigned char *bufptr;
  53. int flen;
  54. unsigned int csum;
  55. unsigned int mostbits;
  56. unsigned int codes;
  57. unsigned int chainused;
  58. unsigned int clue;
  59. unsigned int dclue;
  60. unsigned int clues;
  61. unsigned int dclues;
  62. int mindelta;
  63. int maxdelta;
  64. unsigned int plen;
  65. unsigned int ulen;
  66. unsigned int sortptr[HUFFCODES];
  67. };
  68. static void HUFF_deltabytes(const void *source,void *dest,int len)
  69. {
  70. const unsigned char *s = (const unsigned char *) source;
  71. unsigned char *d = (unsigned char *) dest;
  72. unsigned char c;
  73. unsigned char c1;
  74. const unsigned char *send;
  75. c = '\0';
  76. send = s+len;
  77. while (s<send)
  78. {
  79. c1 = *s++;
  80. *d++ = (unsigned char)(c1-c);
  81. c = c1;
  82. }
  83. }
  84. static void HUFF_writebits(struct HuffEncodeContext *EC,
  85. struct HUFFMemStruct *dest,
  86. unsigned int bitpattern,
  87. unsigned int len)
  88. {
  89. if (len > 16)
  90. {
  91. HUFF_writebits(EC,dest,(unsigned int) (bitpattern>>16), len-16);
  92. HUFF_writebits(EC,dest,(unsigned int) bitpattern, 16);
  93. }
  94. else
  95. {
  96. EC->packbits += len;
  97. EC->workpattern += (bitpattern & EC->masks[len]) << (24-EC->packbits);
  98. while (EC->packbits > 7)
  99. {
  100. *(dest->ptr+dest->len) = (unsigned char) (EC->workpattern >> 16);
  101. ++dest->len;
  102. EC->workpattern = EC->workpattern << 8;
  103. EC->packbits -= 8;
  104. ++EC->plen;
  105. }
  106. }
  107. }
  108. static void HUFF_treechase(struct HuffEncodeContext *EC,
  109. unsigned int node,
  110. unsigned int bits)
  111. {
  112. if (node < HUFFCODES)
  113. EC->bitsarray[node] = bits;
  114. else
  115. { HUFF_treechase(EC,EC->tree_left[node], bits+1);
  116. HUFF_treechase(EC,EC->tree_right[node], bits+1);
  117. }
  118. }
  119. static void HUFF_maketree(struct HuffEncodeContext *EC)
  120. {
  121. unsigned int i, i1;
  122. unsigned int ptr1;
  123. unsigned int val1, val2;
  124. unsigned int ptr2, nodes;
  125. unsigned int list_count[HUFFCODES+2];
  126. unsigned int list_ptr[HUFFCODES+2];
  127. /* initialize tree */
  128. /* registers vars usage
  129. i - code
  130. i1 - code index (number of unjoined codes)
  131. i2 -
  132. i3 -
  133. */
  134. nodes = HUFFCODES;
  135. i1 = 0;
  136. list_count[i1++] = 0L;
  137. for (i=0; i<HUFFCODES; ++i)
  138. { EC->bitsarray[i] = 99;
  139. if (EC->count[i])
  140. { list_count[i1] = (unsigned int) EC->count[i];
  141. list_ptr[i1++] = i;
  142. }
  143. }
  144. EC->codes = i1-1;
  145. /* make tree */
  146. /* registers vars usage
  147. i - code index/temp
  148. i1 - number of unjoined codes
  149. i2 -
  150. i3 -
  151. */
  152. if (i1>2)
  153. {
  154. while (i1>2)
  155. {
  156. /* get 2 smallest node counts */
  157. i = i1;
  158. val1 = list_count[--i]; /* initialize 2 values */
  159. ptr1 = i;
  160. val2 = list_count[--i];
  161. ptr2 = i;
  162. if (val1 < val2)
  163. { val2 = val1;
  164. ptr2 = ptr1;
  165. val1 = list_count[i];
  166. ptr1 = i;
  167. }
  168. while (i)
  169. { --i;
  170. while (list_count[i] > val1)
  171. --i;
  172. if (i)
  173. { val1 = list_count[i];
  174. ptr1 = i;
  175. if (val1 <= val2)
  176. { val1 = val2;
  177. ptr1 = ptr2;
  178. val2 = list_count[i];
  179. ptr2 = i;
  180. }
  181. }
  182. }
  183. EC->tree_left[nodes] = list_ptr[ptr1];
  184. EC->tree_right[nodes] = list_ptr[ptr2];
  185. list_count[ptr1] = val1 + val2;
  186. list_ptr[ptr1] = nodes;
  187. list_count[ptr2] = list_count[--i1];
  188. list_ptr[ptr2] = list_ptr[i1];
  189. ++nodes;
  190. }
  191. /* traverse tree */
  192. HUFF_treechase(EC,nodes-1, 0);
  193. }
  194. else
  195. {
  196. /* traverse tree */
  197. HUFF_treechase(EC,list_ptr[EC->codes], 1);
  198. }
  199. }
  200. static int HUFF_minrep(struct HuffEncodeContext *EC,
  201. unsigned int remaining,
  202. unsigned int r)
  203. {
  204. int min, min1, use, newremaining;
  205. if (r)
  206. { min = HUFF_minrep(EC,remaining, r-1);
  207. if (EC->count[EC->clue+r])
  208. { use = remaining/r;
  209. newremaining = remaining-(use*r);
  210. min1 = HUFF_minrep(EC,newremaining, r-1)+EC->bitsarray[EC->clue+r]*use;
  211. if (min1<min)
  212. min = min1;
  213. }
  214. }
  215. else
  216. { min = 0;
  217. if (remaining)
  218. { min = 20;
  219. if (remaining<HUFFREPTBL)
  220. min = EC->bitsarray[EC->clue]+3+EC->repbits[remaining]*2;
  221. }
  222. }
  223. return(min);
  224. }
  225. static void HUFF_writenum(struct HuffEncodeContext *EC,
  226. struct HUFFMemStruct *dest,
  227. unsigned int num)
  228. {
  229. unsigned int dphuf;
  230. unsigned int dbase;
  231. if (num<HUFFREPTBL)
  232. { dphuf = EC->repbits[(unsigned int) num];
  233. dbase = (unsigned int) EC->repbase[(unsigned int) num];
  234. }
  235. else
  236. { if (num<508L)
  237. { dphuf = 6;
  238. dbase = 252L;
  239. }
  240. else if (num<1020L)
  241. { dphuf = 7;
  242. dbase = 508L;
  243. }
  244. else if (num<2044L)
  245. { dphuf = 8;
  246. dbase = 1020L;
  247. }
  248. else if (num<4092L)
  249. { dphuf = 9;
  250. dbase = 2044L;
  251. }
  252. else if (num<8188L)
  253. { dphuf = 10;
  254. dbase = 4092L;
  255. }
  256. else if (num<16380L)
  257. { dphuf = 11;
  258. dbase = 8188L;
  259. }
  260. else if (num<32764L)
  261. { dphuf = 12;
  262. dbase = 16380L;
  263. }
  264. else if (num<65532L)
  265. { dphuf = 13;
  266. dbase = 32764L;
  267. }
  268. else if (num<131068L)
  269. { dphuf = 14;
  270. dbase = 65532L;
  271. }
  272. else if (num<262140L)
  273. { dphuf = 15;
  274. dbase = 131068L;
  275. }
  276. else if (num<524288L)
  277. { dphuf = 16;
  278. dbase = 262140L;
  279. }
  280. else if (num<1048576L)
  281. { dphuf = 17;
  282. dbase = 524288L;
  283. }
  284. else
  285. { dphuf = 18;
  286. dbase = 1048576L;
  287. }
  288. }
  289. HUFF_writebits(EC,dest,(unsigned int) 0x00000001, dphuf+1);
  290. HUFF_writebits(EC,dest,(unsigned int) (num - dbase), dphuf+2);
  291. }
  292. /* write explicite byte ([clue] 0gn [0] [byte]) */
  293. static void HUFF_writeexp(struct HuffEncodeContext *EC,
  294. struct HUFFMemStruct *dest,
  295. unsigned int code)
  296. {
  297. HUFF_writebits(EC,dest,EC->patternarray[EC->clue], EC->bitsarray[EC->clue]);
  298. HUFF_writenum(EC,dest,0L);
  299. HUFF_writebits(EC,dest,(unsigned int) code, 9);
  300. }
  301. static void HUFF_writecode(struct HuffEncodeContext *EC,
  302. struct HUFFMemStruct *dest,
  303. unsigned int code)
  304. {
  305. if (code==EC->clue)
  306. HUFF_writeexp(EC,dest,code);
  307. else
  308. HUFF_writebits(EC,dest,EC->patternarray[code], EC->bitsarray[code]);
  309. }
  310. static void HUFF_init(struct HuffEncodeContext *EC)
  311. {
  312. unsigned int i;
  313. /* precalculate repeat field lengths */
  314. /* registers vars usage
  315. i - num
  316. i1 -
  317. i2 -
  318. i3 -
  319. */
  320. i = 0;
  321. while (i<4)
  322. { EC->repbits[i] = 0;
  323. EC->repbase[i++] = 0L;
  324. }
  325. while (i<12)
  326. { EC->repbits[i] = 1;
  327. EC->repbase[i++] = 4L;
  328. }
  329. while (i<28)
  330. { EC->repbits[i] = 2;
  331. EC->repbase[i++] = 12L;
  332. }
  333. while (i<60)
  334. { EC->repbits[i] = 3;
  335. EC->repbase[i++] = 28L;
  336. }
  337. while (i<124)
  338. { EC->repbits[i] = 4;
  339. EC->repbase[i++] = 60L;
  340. }
  341. while (i<252)
  342. { EC->repbits[i] = 5;
  343. EC->repbase[i++] = 124L;
  344. }
  345. }
  346. static void HUFF_analysis(struct HuffEncodeContext *EC,
  347. unsigned int opt,
  348. unsigned int chainsaw)
  349. {
  350. unsigned char *bptr1;
  351. unsigned char *bptr2;
  352. unsigned int i;
  353. unsigned int i1;
  354. unsigned int i2=0;
  355. unsigned int i3;
  356. unsigned int thres=0;
  357. int di;
  358. unsigned int pattern;
  359. unsigned int rep1;
  360. unsigned int repn;
  361. unsigned int ncode;
  362. unsigned int irep;
  363. unsigned int remaining;
  364. unsigned int count2[HUFFCODES];
  365. unsigned int dcount[HUFFCODES];
  366. /* count file (pass 1) */
  367. /* registers vars usage
  368. i - current byte
  369. i1 - previous byte
  370. i2 - rep
  371. i3 - checksum
  372. */
  373. for (i=0; i<768; ++i)
  374. EC->count[i] = 0;
  375. bptr1 = EC->buffer;
  376. i1 = 256;
  377. i3 = 0;
  378. while (bptr1<EC->bufptr)
  379. { i = (unsigned int) *bptr1++;
  380. i3 = i3 + i;
  381. if (i == i1)
  382. {
  383. i2 = 0;
  384. bptr2 = bptr1+30000;
  385. if (bptr2>(EC->bufptr))
  386. bptr2 = EC->bufptr;
  387. while ((i == i1) && (bptr1 < bptr2))
  388. { ++i2;
  389. i = (unsigned int) *bptr1++;
  390. i3 = i3 + i;
  391. }
  392. if (i2 < 255)
  393. ++EC->count[512+i2];
  394. else
  395. ++EC->count[512];
  396. }
  397. ++EC->count[i];
  398. ++EC->count[((i+256-i1)&255)+256];
  399. i1 = i;
  400. }
  401. EC->csum = i3;
  402. if (!EC->count[512])
  403. ++EC->count[512];
  404. /* find clue bytes */
  405. /* registers vars usage
  406. i - code
  407. i1 - cluebyte
  408. i2 - clues
  409. i3 - best forced clue
  410. */
  411. EC->clues = 0;
  412. EC->dclues = 0;
  413. i3 = 0;
  414. for (i=0;i<HUFFCODES; ++i)
  415. { i1 = i;
  416. i2 = 0;
  417. if (EC->count[i]<EC->count[i3])
  418. i3 = i;
  419. while (!EC->count[i] && (i<256))
  420. { ++i2;
  421. ++i;
  422. }
  423. if (i2 >= EC->dclues)
  424. { EC->dclue = i1;
  425. EC->dclues = i2;
  426. if (EC->dclues >= EC->clues)
  427. { EC->dclue = EC->clue;
  428. EC->dclues = EC->clues;
  429. EC->clue = i1;
  430. EC->clues = i2;
  431. }
  432. }
  433. }
  434. /* force a clue byte */
  435. if (opt & 32)
  436. { if (!EC->clues)
  437. { EC->clues = 1;
  438. EC->clue = i3;
  439. }
  440. }
  441. /* disable & split clue bytes */
  442. if ((~opt) & 2)
  443. { if (EC->clues>1)
  444. EC->clues = 1;
  445. if ((~opt) & 1)
  446. EC->clues = 0;
  447. }
  448. if ((~opt) & 4)
  449. EC->dclues = 0;
  450. else
  451. { if (EC->dclues > 10)
  452. { i1 = EC->clue;
  453. i2 = EC->clues;
  454. EC->clue = EC->dclue;
  455. EC->clues = EC->dclues;
  456. EC->dclue = i1;
  457. EC->dclues = i2;
  458. }
  459. if ((EC->clues*4) < EC->dclues)
  460. { EC->clues = EC->dclues/4;
  461. EC->dclues = EC->dclues-EC->clues;
  462. EC->clue = EC->dclue+EC->dclues;
  463. }
  464. }
  465. /* copy delta clue bytes */
  466. if (EC->dclues)
  467. {
  468. EC->mindelta = -((int)EC->dclues/2);
  469. EC->maxdelta = EC->dclues+EC->mindelta;
  470. thres = (int) (EC->ulen/25);
  471. for (i=1;i<=(unsigned int)EC->maxdelta;++i)
  472. if (EC->count[256+i] > thres)
  473. EC->count[EC->dclue+(i-1)*2] = EC->count[256+i];
  474. for (i=1;i<=(unsigned int)(-EC->mindelta);++i)
  475. if (EC->count[512-i] > thres)
  476. EC->count[EC->dclue+(i-1)*2+1] = EC->count[512-i];
  477. /* adjust delta clues */
  478. for (i=0, i2=0;i<EC->dclues;++i)
  479. if (EC->count[EC->dclue+i])
  480. i2 = i;
  481. di = EC->dclues-i2-1;
  482. EC->dclues -= di;
  483. if (EC->clue == (EC->dclue+EC->dclues+di))
  484. { EC->clue -= di;
  485. EC->clues += di;
  486. }
  487. EC->mindelta = -((int)EC->dclues/2);
  488. EC->maxdelta = EC->dclues+EC->mindelta;
  489. }
  490. /* copy rep clue bytes */
  491. if (EC->clues)
  492. {
  493. for (i=0;i<EC->clues;++i)
  494. EC->count[EC->clue+i] = EC->count[512+i];
  495. }
  496. /* make a first approximation tree */
  497. HUFF_maketree(EC);
  498. /* remove implied rep clues */
  499. /* registers vars usage
  500. i - clues
  501. i1 - minrep
  502. i2 -
  503. i3 -
  504. */
  505. if (EC->clues>1)
  506. {
  507. for (i=1; i<EC->clues; ++i)
  508. { i1 = i-1;
  509. if (i1>8)
  510. i1 = 8;
  511. if (EC->count[EC->clue+i])
  512. { i1 = HUFF_minrep(EC,i,i1);
  513. if ((i1 <= EC->bitsarray[EC->clue+i])
  514. || (EC->count[EC->clue+i]*(i1-EC->bitsarray[EC->clue+i])<(i/2)))
  515. { EC->count[EC->clue+i] = 0;
  516. }
  517. }
  518. }
  519. }
  520. /* count file (pass 2) */
  521. /* registers vars usage
  522. i - current byte
  523. i1 - previous byte
  524. i2 - rep
  525. i3 - rep2
  526. */
  527. for (i=0; i<HUFFCODES; ++i)
  528. { count2[i] = EC->count[i];
  529. dcount[i] = 0;
  530. EC->count[i] = 0;
  531. EC->count[256+i] = 0;
  532. EC->count[512+i] = 0;
  533. }
  534. i = 1;
  535. i1 = 256;
  536. bptr1 = EC->buffer;
  537. while (bptr1<EC->bufptr)
  538. { i = (unsigned int) *bptr1++;
  539. if (i == i1)
  540. {
  541. i2 = 0;
  542. bptr2 = bptr1+30000;
  543. if (bptr2>(EC->bufptr))
  544. bptr2 = EC->bufptr;
  545. while ((i == i1) && (bptr1 < bptr2))
  546. { i = (unsigned int) *bptr1++;
  547. ++i2;
  548. }
  549. repn = HUFFBIGNUM;
  550. irep = HUFFBIGNUM;
  551. ncode = i2*EC->bitsarray[i1];
  552. if (EC->clues)
  553. {
  554. if (count2[EC->clue])
  555. { repn = 20;
  556. if (i2 < HUFFREPTBL)
  557. repn = EC->bitsarray[EC->clue]+3+EC->repbits[i2]*2;
  558. }
  559. if (EC->clues>1)
  560. {
  561. remaining = i2;
  562. irep = 0;
  563. i3=EC->clues-1;
  564. while (i3)
  565. { if (count2[EC->clue+i3])
  566. { rep1 = remaining/i3;
  567. irep = irep+rep1*EC->bitsarray[EC->clue+i3];
  568. remaining = remaining-rep1*i3;
  569. }
  570. --i3;
  571. }
  572. if (remaining)
  573. irep=HUFFBIGNUM;
  574. }
  575. }
  576. if ((ncode<=repn) && (ncode<=irep))
  577. EC->count[i1] += i2;
  578. else
  579. { if (repn < irep)
  580. ++EC->count[EC->clue];
  581. else
  582. {
  583. remaining = i2;
  584. irep = 0;
  585. i3=EC->clues-1;
  586. while (i3)
  587. { if (count2[EC->clue+i3])
  588. { rep1 = remaining/i3;
  589. irep = irep+rep1*EC->bitsarray[EC->clue+i3];
  590. remaining = remaining-rep1*i3;
  591. EC->count[EC->clue+i3] += rep1;
  592. }
  593. --i3;
  594. }
  595. }
  596. }
  597. }
  598. if (EC->dclues)
  599. { i3 = 0;
  600. di = i-i1;
  601. if (di <= EC->maxdelta)
  602. { if (di >= EC->mindelta)
  603. { di = (i-i1-1)*2+EC->dclue;
  604. if (i<i1)
  605. di = (i1-i-1)*2+EC->dclue+1;
  606. if (count2[di]>thres)
  607. { if (count2[i]<4)
  608. ++i3;
  609. if (EC->bitsarray[di] < EC->bitsarray[i])
  610. ++i3;
  611. if (EC->bitsarray[di] == EC->bitsarray[i])
  612. if (EC->count[di] > EC->count[i])
  613. ++i3;
  614. }
  615. }
  616. }
  617. if (i3)
  618. ++EC->count[di];
  619. else
  620. ++EC->count[i];
  621. }
  622. else
  623. ++EC->count[i];
  624. i1 = i;
  625. }
  626. /* force a clue byte */
  627. if (opt & 32)
  628. ++EC->count[EC->clue];
  629. /* make a second approximation tree */
  630. HUFF_maketree(EC);
  631. /* chainsaw IV branch clipping algorithm */
  632. /* - maintains perfect tree
  633. - find intest code
  634. - find intest branch thats shorter than maximum bits
  635. - graft one branch to the shorter branch
  636. - shorten the other code by 1
  637. */
  638. /* registers vars usage
  639. i - code
  640. i1 - codes lengths
  641. i2 - int code1 ptr
  642. i3 - int code2 ptr
  643. */
  644. EC->chainused = 0;
  645. i1 = 99;
  646. while (i1>chainsaw)
  647. { i1 = 0;
  648. for (i=0; i<HUFFCODES; ++i) /* find intest code */
  649. { if (EC->count[i])
  650. { if (EC->bitsarray[i]>=i1)
  651. { i3 = i2;
  652. i2 = i;
  653. i1 = EC->bitsarray[i];
  654. }
  655. }
  656. }
  657. if (i1>chainsaw)
  658. {
  659. i1 = 0;
  660. while (i1<HUFFCODES)
  661. { if (EC->count[i1])
  662. {
  663. if (EC->bitsarray[i1]<chainsaw)
  664. break;
  665. }
  666. ++i1;
  667. }
  668. for (i=i1; i<HUFFCODES; ++i) /* find code to graft to */
  669. { if (EC->count[i])
  670. {
  671. if ((EC->bitsarray[i]<chainsaw) && (EC->bitsarray[i]>EC->bitsarray[i1]))
  672. i1 = i;
  673. }
  674. }
  675. i = EC->bitsarray[i1]+1; /* graft to short code */
  676. EC->bitsarray[i1] = i;
  677. EC->bitsarray[i2] = i;
  678. EC->bitsarray[i3] = EC->bitsarray[i3]-1;/* shorten other code by 1 */
  679. EC->chainused = chainsaw;
  680. i1 = 99;
  681. }
  682. }
  683. /* if huffman inhibited make all codes 8 bits */
  684. /* registers vars usage
  685. i - code
  686. i1 - 8
  687. i2 -
  688. i3 -
  689. */
  690. if ((~opt) & 8)
  691. {
  692. i1 = 8;
  693. for (i=0; i<HUFFCODES; ++i)
  694. EC->bitsarray[i] = i1;
  695. }
  696. /* count bitnums */
  697. /* registers vars usage
  698. i - code/bits
  699. i1 -
  700. i2 -
  701. i3 -
  702. */
  703. for (i=0; i<=HUFFMAXBITS; ++i)
  704. EC->bitnum[i] = 0;
  705. for (i=0; i<HUFFCODES; ++i)
  706. { if (EC->bitsarray[i]<=HUFFMAXBITS)
  707. ++EC->bitnum[EC->bitsarray[i]];
  708. }
  709. /* sort codes */
  710. /* registers vars usage
  711. i - next sorted ptr
  712. i1 - code
  713. i2 - bits
  714. i3 - EC->mostbits
  715. */
  716. i=0;
  717. i3=0;
  718. for (i2=1; i2<=HUFFMAXBITS; ++i2)
  719. { if (EC->bitnum[i2])
  720. { for (i1=0; i1<HUFFCODES; ++i1)
  721. if (EC->bitsarray[i1] == i2)
  722. EC->sortptr[i++] = i1;
  723. i3 = i2;
  724. }
  725. }
  726. EC->mostbits = i3;
  727. /* assign bit patterns */
  728. /* registers vars usage
  729. i - code index
  730. i1 - code
  731. i2 - bits
  732. i3 -
  733. */
  734. pattern = 0L;
  735. i2 = 0;
  736. for (i=0; i<EC->codes; ++i)
  737. { i1 = EC->sortptr[i];
  738. while (i2 < EC->bitsarray[i1])
  739. { ++i2;
  740. pattern = pattern << 1;
  741. }
  742. EC->patternarray[i1] = pattern;
  743. ++pattern;
  744. }
  745. }
  746. static void HUFF_pack(struct HuffEncodeContext *EC,
  747. struct HUFFMemStruct *dest,
  748. unsigned int opt)
  749. {
  750. unsigned char *bptr1;
  751. unsigned char *bptr2;
  752. unsigned int i;
  753. unsigned int i1;
  754. unsigned int i2;
  755. unsigned int i3;
  756. int uptype;
  757. unsigned int hlen, ibits, rladjust;
  758. int di, firstcode, firstbits;
  759. unsigned int rep1, repn, ncode, irep, remaining,curpc;
  760. /* write header */
  761. curpc = 0L;
  762. uptype = 38;
  763. rladjust = 1;
  764. if (uptype==38)
  765. {
  766. if (uptype==34)
  767. {
  768. HUFF_writenum(EC,dest,(unsigned int) EC->ulen);
  769. ibits = 0;
  770. if ((opt & 16) && (!EC->chainused))
  771. ibits = 1;
  772. i = 0; /* write options field */
  773. if (EC->clues)
  774. i = 1;
  775. if (ibits)
  776. i += 2;
  777. if (EC->dclues)
  778. i += 4;
  779. HUFF_writenum(EC,dest,(unsigned int) i);
  780. if (EC->clues)
  781. { HUFF_writenum(EC,dest,(unsigned int) EC->clue);
  782. HUFF_writenum(EC,dest,(unsigned int) EC->clues);
  783. }
  784. if (EC->dclues)
  785. { HUFF_writenum(EC,dest,(unsigned int) EC->dclue);
  786. HUFF_writenum(EC,dest,(unsigned int) EC->dclues);
  787. }
  788. if (!ibits)
  789. HUFF_writenum(EC,dest,(unsigned int) EC->mostbits);
  790. }
  791. else
  792. {
  793. HUFF_writebits(EC,dest,(unsigned int) EC->clue, 8); /* clue */
  794. rladjust = 0;
  795. }
  796. for (i=1; i <= EC->mostbits; ++i)
  797. HUFF_writenum(EC,dest,(unsigned int) EC->bitnum[i]);
  798. for (i=0; i<HUFFCODES; ++i)
  799. EC->qleapcode[i] = 0;
  800. i = 0;
  801. i2 = 255;
  802. firstbits = 0;
  803. firstcode = -1;
  804. while (i<EC->codes)
  805. {
  806. i1 = EC->sortptr[i];
  807. #if 0
  808. if (EC->bitsarray[i1]!=firstbits)
  809. {
  810. i2 = firstcode;
  811. firstcode = i1;
  812. firstbits = EC->bitsarray[i2];
  813. }
  814. #endif
  815. /* calculate leapfrog delta */
  816. di = -1;
  817. do
  818. {
  819. i2 = (i2+1)&255;
  820. if (!EC->qleapcode[i2])
  821. ++di;
  822. } while (i1!=i2);
  823. EC->qleapcode[i2] = 1;
  824. HUFF_writenum(EC,dest,(unsigned int) di);
  825. ++i;
  826. }
  827. }
  828. hlen = EC->plen+1;
  829. if (!EC->clues)
  830. EC->clue = HUFFBIGNUM;
  831. /* write packed file */
  832. /* registers vars usage
  833. i - current byte
  834. i1 - previous byte
  835. i2 - rep
  836. i3 - rep2
  837. */
  838. i = 1;
  839. i1 = 256;
  840. bptr1 = EC->buffer;
  841. while (bptr1<EC->bufptr)
  842. { i = (unsigned int) *bptr1++;
  843. if (i == i1)
  844. {
  845. i2 = 0;
  846. bptr2 = bptr1+30000;
  847. if (bptr2>(EC->bufptr))
  848. bptr2 = EC->bufptr;
  849. while ((i == i1) && (bptr1 < bptr2))
  850. { i = (unsigned int) *bptr1++;
  851. ++i2;
  852. }
  853. repn = HUFFBIGNUM;
  854. irep = HUFFBIGNUM;
  855. ncode = i2*EC->bitsarray[i1];
  856. if (EC->clues)
  857. {
  858. if (EC->count[EC->clue])
  859. { repn = 20;
  860. if (i2 < HUFFREPTBL)
  861. repn = EC->bitsarray[EC->clue]+3+EC->repbits[i2]*2;
  862. }
  863. if (EC->clues>1)
  864. {
  865. remaining = i2;
  866. irep = 0;
  867. i3=EC->clues-1;
  868. while (i3)
  869. { if (EC->count[EC->clue+i3])
  870. { rep1 = remaining/i3;
  871. irep = irep+rep1*EC->bitsarray[EC->clue+i3];
  872. remaining = remaining-rep1*i3;
  873. }
  874. --i3;
  875. }
  876. if (remaining)
  877. irep = HUFFBIGNUM;
  878. }
  879. }
  880. if ((ncode<=repn) && (ncode<=irep))
  881. {
  882. while (i2--)
  883. HUFF_writecode(EC,dest,i1);
  884. }
  885. else
  886. { if (repn < irep)
  887. {
  888. HUFF_writebits(EC,dest,EC->patternarray[EC->clue], EC->bitsarray[EC->clue]);
  889. HUFF_writenum(EC,dest,(unsigned int) (i2-rladjust));
  890. }
  891. else
  892. {
  893. remaining = i2;
  894. irep = 0;
  895. i3=EC->clues-1;
  896. while (i3)
  897. { if (EC->count[EC->clue+i3])
  898. { rep1 = remaining/i3;
  899. irep = irep+rep1*EC->bitsarray[EC->clue+i3];
  900. remaining = remaining-rep1*i3;
  901. while (rep1--)
  902. HUFF_writecode(EC,dest,EC->clue+i3);
  903. }
  904. --i3;
  905. }
  906. }
  907. }
  908. }
  909. i3 = 0;
  910. if (EC->dclues)
  911. {
  912. di = i-i1;
  913. if ((di <= EC->maxdelta) && (di >= EC->mindelta))
  914. { di = (i-i1-1)*2+EC->dclue;
  915. if (i<i1)
  916. di = (i1-i-1)*2+EC->dclue+1;
  917. if (EC->bitsarray[di] < EC->bitsarray[i])
  918. { HUFF_writebits(EC,dest,EC->patternarray[di], EC->bitsarray[di]);
  919. ++i3;
  920. }
  921. }
  922. }
  923. i1 = i;
  924. if (!i3)
  925. HUFF_writecode(EC,dest,i);
  926. if (((int) bptr1- (int) EC->buffer) >= (int)(EC->plen+curpc))
  927. curpc = (int) bptr1 - (int) EC->buffer - EC->plen;
  928. }
  929. /* write EOF ([clue] 0gn [10]) */
  930. HUFF_writebits(EC,dest,EC->patternarray[EC->clue], EC->bitsarray[EC->clue]);
  931. HUFF_writenum(EC,dest,0L);
  932. HUFF_writebits(EC,dest,(unsigned int) 2, 2);
  933. /* flush bits */
  934. HUFF_writebits(EC,dest,(unsigned int) 0,7);
  935. curpc += 2;
  936. }
  937. static int HUFF_packfile(struct HuffEncodeContext *EC,
  938. struct HUFFMemStruct *infile,
  939. struct HUFFMemStruct *outfile,
  940. int ulen,
  941. int deltaed)
  942. {
  943. unsigned int i;
  944. unsigned int uptype=0;
  945. unsigned int chainsaw;
  946. unsigned int opt;
  947. /* set defaults */
  948. EC->packbits = 0;
  949. EC->workpattern = 0L;
  950. chainsaw = 15;
  951. EC->masks[0] = 0;
  952. for (i=1;i<17;++i)
  953. EC->masks[i] = (EC->masks[i-1] << 1) + 1;
  954. /* initialize huffman vars */
  955. HUFF_init(EC);
  956. /* read in a source file */
  957. EC->buffer = (unsigned char *) (infile->ptr);
  958. EC->flen = infile->len;
  959. EC->ulen = EC->flen;
  960. EC->bufptr = EC->buffer + EC->flen;
  961. /* pack a file */
  962. outfile->ptr = outfile->ptr;
  963. outfile->len = 0L;
  964. EC->packbits = 0;
  965. EC->workpattern = 0L;
  966. EC->plen = 0L;
  967. opt = 57 | 49;
  968. HUFF_analysis(EC,opt, chainsaw);
  969. /* write standard header stuff (type/signature/ulen/adjust) */
  970. if (ulen>0xffffff) // 32 bit header required
  971. {
  972. /* simple fb6 header */
  973. if (ulen==infile->len)
  974. {
  975. if (deltaed==0) uptype = 0xb0fb;
  976. else if (deltaed==1) uptype = 0xb2fb;
  977. else if (deltaed==2) uptype = 0xb4fb;
  978. HUFF_writebits(EC,outfile,(unsigned int) uptype, 16);
  979. HUFF_writebits(EC,outfile,(unsigned int) infile->len, 32);
  980. }
  981. /* composite fb4 header */
  982. else
  983. {
  984. if (deltaed==0) uptype = 0xb1fb;
  985. else if (deltaed==1) uptype = 0xb3fb;
  986. else if (deltaed==2) uptype = 0xb5fb;
  987. HUFF_writebits(EC,outfile,(unsigned int) uptype, 16);
  988. HUFF_writebits(EC,outfile,(unsigned int) ulen, 32);
  989. HUFF_writebits(EC,outfile,(unsigned int) infile->len, 32);
  990. }
  991. }
  992. else
  993. {
  994. /* simple fb6 header */
  995. if (ulen==infile->len)
  996. {
  997. if (deltaed==0) uptype = 0x30fb;
  998. else if (deltaed==1) uptype = 0x32fb;
  999. else if (deltaed==2) uptype = 0x34fb;
  1000. HUFF_writebits(EC,outfile,(unsigned int) uptype, 16);
  1001. HUFF_writebits(EC,outfile,(unsigned int) infile->len, 24);
  1002. }
  1003. /* composite fb4 header */
  1004. else
  1005. {
  1006. if (deltaed==0) uptype = 0x31fb;
  1007. else if (deltaed==1) uptype = 0x33fb;
  1008. else if (deltaed==2) uptype = 0x35fb;
  1009. HUFF_writebits(EC,outfile,(unsigned int) uptype, 16);
  1010. HUFF_writebits(EC,outfile,(unsigned int) ulen, 24);
  1011. HUFF_writebits(EC,outfile,(unsigned int) infile->len, 24);
  1012. }
  1013. }
  1014. HUFF_pack(EC,outfile, opt);
  1015. return(outfile->len);
  1016. }
  1017. /****************************************************************/
  1018. /* Encode Function */
  1019. /****************************************************************/
  1020. int GCALL HUFF_encode(void *compresseddata, const void *source, int sourcesize, int *opts)
  1021. {
  1022. int plen=0;
  1023. struct HUFFMemStruct infile;
  1024. struct HUFFMemStruct outfile;
  1025. struct HuffEncodeContext *EC=0;
  1026. void *deltabuf=0;
  1027. int opt=0;
  1028. if (opts)
  1029. opt = opts[0];
  1030. EC = (struct HuffEncodeContext *)galloc(sizeof(struct HuffEncodeContext));
  1031. if (EC)
  1032. {
  1033. switch (opt)
  1034. {
  1035. default:
  1036. case 0:
  1037. infile.ptr = (char *)source;
  1038. break;
  1039. case 1:
  1040. deltabuf = galloc(sourcesize);
  1041. HUFF_deltabytes(source,deltabuf,sourcesize);
  1042. infile.ptr = (char *) deltabuf;
  1043. break;
  1044. case 2:
  1045. deltabuf = galloc(sourcesize);
  1046. HUFF_deltabytes(source,deltabuf,sourcesize);
  1047. HUFF_deltabytes(deltabuf,deltabuf,sourcesize);
  1048. infile.ptr = (char *) deltabuf;
  1049. break;
  1050. }
  1051. infile.len = sourcesize;
  1052. outfile.ptr = (char *)compresseddata;
  1053. outfile.len = sourcesize;
  1054. plen = HUFF_packfile(EC,&infile, &outfile, sourcesize, opt);
  1055. if (deltabuf) gfree(deltabuf);
  1056. gfree(EC);
  1057. }
  1058. return(plen);
  1059. }
  1060. #endif