LZHUF.C 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /* Catacomb Apocalypse Source Code
  2. * Copyright (C) 1993-2014 Flat Rock Software
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. //===========================================================================
  19. //
  20. // LZHUFF COMPRESSION ROUTINES
  21. // VERSION 1.0
  22. //
  23. // Compression algrythim by Haruhiko OKUMURA
  24. // Implementation by Jim T. Row
  25. //
  26. //
  27. // Copyright (c) 1992 - Softdisk Publishing inc. - All rights reserved
  28. //
  29. //===========================================================================
  30. //
  31. // Compiler #ifdef switches
  32. //
  33. // LZHUFF_COMPRESSION & LZHUFF_DECOMPRESSION - not yet functional!
  34. //
  35. // Usage Explanition :
  36. //
  37. // if LZHUFF_COMPRESSION is defined then the compression code & data is
  38. // compiled and so-forth for the decompression code.
  39. //
  40. //---------------------------------------------------------------------------
  41. #include <fcntl.h>
  42. #include <io.h>
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47. #include <alloc.h>
  48. #include <dos.h>
  49. #include "lzhuff.h"
  50. #include "jam_io.h"
  51. //===========================================================================
  52. //
  53. // SWITCHES
  54. //
  55. // NOTE : Make sure the appropriate switches are set in SOFT.c for Softlib
  56. // archive support.
  57. //
  58. //===========================================================================
  59. #define INCLUDE_LZH_COMP 0
  60. #define INCLUDE_LZH_DECOMP 1
  61. //===========================================================================
  62. //
  63. // DEFINES
  64. //
  65. //===========================================================================
  66. #define EXIT_OK 0
  67. #define EXIT_FAILED -1
  68. /* LZSS Parameters */
  69. #define N 4096 /* Size of string buffer */
  70. #define F 30 /* Size of look-ahead buffer */
  71. #define THRESHOLD 2
  72. #define NIL N /* End of tree's node */
  73. /* Huffman coding parameters */
  74. #define N_CHAR (256 - THRESHOLD + F) /* character code (= 0..N_CHAR-1) */
  75. #define T (N_CHAR * 2 - 1) /* Size of table */
  76. #define R (T - 1) /* root position */
  77. #define MAX_FREQ 0x8000 /* update when cumulative frequency */
  78. /* reaches to this value */
  79. //==========================================================================
  80. //
  81. // LOCAL PROTOTYPES
  82. //
  83. //==========================================================================
  84. static void StartHuff();
  85. static void reconst();
  86. static void update(int c);
  87. static void DeleteNode(int p); /* Deleting node from the tree */
  88. static void InsertNode(int r); /* Inserting node to the tree */
  89. static void InitTree(void); /* Initializing tree */
  90. static void Putcode(long outfile_ptr, int l, unsigned c,unsigned PtrTypes); /* output c bits */
  91. static void EncodeChar(long outfile_ptr, unsigned c, unsigned PtrTypes);
  92. static void EncodePosition(long outfile_ptr, unsigned c, unsigned PtrTypes);
  93. static void EncodeEnd(long outfile_ptr,unsigned PtrTypes);
  94. static int GetByte(long infile_ptr, unsigned long *CompressLength, unsigned PtrTypes);
  95. static int GetBit(long infile_ptr, unsigned long *CompressLength, unsigned PtrTypes); /* get one bit */
  96. static int DecodeChar(long infile_ptr, unsigned long *CompressLength, unsigned PtrTypes);
  97. static int DecodePosition(long infile_ptr,unsigned long *CompressLength, unsigned PtrTypes);
  98. //==========================================================================
  99. //
  100. // USER AVAILABLE VECTORS
  101. //
  102. //==========================================================================
  103. //---------------------------------------------------------------------------
  104. //
  105. // LZHUFF DISPLAY VECTORS
  106. //
  107. // These vectors allow you to hook up any form of display you desire for
  108. // displaying the compression/decompression status.
  109. //
  110. // These routines are called inside of the compression/decompression routines
  111. // and pass the orginal size of data and current position within that
  112. // data. This allows for any kind of "% Done" messages.
  113. //
  114. // Your functions MUST have the following parameters in this order...
  115. //
  116. // void VectorRoutine(unsigned long OrginalSize,unsigned long CurPosition)
  117. //
  118. //
  119. #if INCLUDE_LZH_COMP
  120. void (*LZH_CompressDisplayVector)() = NULL;
  121. #endif
  122. #if INCLUDE_LZH_DECOMP
  123. void (*LZH_DecompressDisplayVector)() = NULL;
  124. #endif
  125. //===========================================================================
  126. //
  127. // GLOBAL VARIABLES
  128. //
  129. //===========================================================================
  130. /* pointing children nodes (son[], son[] + 1)*/
  131. int far son[T];
  132. unsigned code, len;
  133. //
  134. // pointing parent nodes.
  135. // area [T..(T + N_CHAR - 1)] are pointers for leaves
  136. //
  137. int far prnt[T + N_CHAR];
  138. unsigned far freq[T + 1]; /* cumulative freq table */
  139. unsigned long textsize = 0, codesize = 0, printcount = 0,datasize;
  140. unsigned char far text_buf[N + F - 1];
  141. //
  142. // COMPRESSION VARIABLES
  143. //
  144. #if INCLUDE_LZH_COMP
  145. static int match_position,match_length, lson[N + 1], rson[N + 257], dad[N + 1];
  146. unsigned putbuf = 0;
  147. unsigned char putlen = 0;
  148. //
  149. // Tables for encoding/decoding upper 6 bits of
  150. // sliding dictionary pointer
  151. //
  152. //
  153. // encoder table
  154. //
  155. unsigned char far p_len[64] = {
  156. 0x03, 0x04, 0x04, 0x04, 0x05, 0x05, 0x05, 0x05,
  157. 0x05, 0x05, 0x05, 0x05, 0x06, 0x06, 0x06, 0x06,
  158. 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  159. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  160. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  161. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  162. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  163. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
  164. };
  165. unsigned char far p_code[64] = {
  166. 0x00, 0x20, 0x30, 0x40, 0x50, 0x58, 0x60, 0x68,
  167. 0x70, 0x78, 0x80, 0x88, 0x90, 0x94, 0x98, 0x9C,
  168. 0xA0, 0xA4, 0xA8, 0xAC, 0xB0, 0xB4, 0xB8, 0xBC,
  169. 0xC0, 0xC2, 0xC4, 0xC6, 0xC8, 0xCA, 0xCC, 0xCE,
  170. 0xD0, 0xD2, 0xD4, 0xD6, 0xD8, 0xDA, 0xDC, 0xDE,
  171. 0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEC, 0xEE,
  172. 0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  173. 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
  174. };
  175. #endif
  176. //
  177. // DECOMPRESSION VARIABLES
  178. //
  179. //
  180. // decoder table
  181. //
  182. #if INCLUDE_LZH_DECOMP
  183. unsigned char far d_code[256] = {
  184. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  185. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  186. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  187. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  188. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  189. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  190. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  191. 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  192. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  193. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  194. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  195. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  196. 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  197. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  198. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  199. 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
  200. 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A, 0x0A,
  201. 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B,
  202. 0x0C, 0x0C, 0x0C, 0x0C, 0x0D, 0x0D, 0x0D, 0x0D,
  203. 0x0E, 0x0E, 0x0E, 0x0E, 0x0F, 0x0F, 0x0F, 0x0F,
  204. 0x10, 0x10, 0x10, 0x10, 0x11, 0x11, 0x11, 0x11,
  205. 0x12, 0x12, 0x12, 0x12, 0x13, 0x13, 0x13, 0x13,
  206. 0x14, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15, 0x15,
  207. 0x16, 0x16, 0x16, 0x16, 0x17, 0x17, 0x17, 0x17,
  208. 0x18, 0x18, 0x19, 0x19, 0x1A, 0x1A, 0x1B, 0x1B,
  209. 0x1C, 0x1C, 0x1D, 0x1D, 0x1E, 0x1E, 0x1F, 0x1F,
  210. 0x20, 0x20, 0x21, 0x21, 0x22, 0x22, 0x23, 0x23,
  211. 0x24, 0x24, 0x25, 0x25, 0x26, 0x26, 0x27, 0x27,
  212. 0x28, 0x28, 0x29, 0x29, 0x2A, 0x2A, 0x2B, 0x2B,
  213. 0x2C, 0x2C, 0x2D, 0x2D, 0x2E, 0x2E, 0x2F, 0x2F,
  214. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  215. 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
  216. };
  217. unsigned char far d_len[256] = {
  218. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  219. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  220. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  221. 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  222. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  223. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  224. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  225. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  226. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  227. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  228. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  229. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  230. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  231. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  232. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  233. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  234. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  235. 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05, 0x05,
  236. 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  237. 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  238. 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  239. 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  240. 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  241. 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
  242. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  243. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  244. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  245. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  246. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  247. 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07,
  248. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  249. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  250. };
  251. unsigned getbuf = 0;
  252. unsigned char getlen = 0;
  253. #endif
  254. //===========================================================================
  255. //
  256. // COMPRESSION & DECOMPRESSION ROUTINES
  257. //
  258. //===========================================================================
  259. //---------------------------------------------------------------------------
  260. // StartHuff /* initialize freq tree */
  261. //---------------------------------------------------------------------------
  262. static void StartHuff()
  263. {
  264. int i, j;
  265. for (i = 0; i < N_CHAR; i++) {
  266. freq[i] = 1;
  267. son[i] = i + T;
  268. prnt[i + T] = i;
  269. }
  270. i = 0; j = N_CHAR;
  271. while (j <= R) {
  272. freq[j] = freq[i] + freq[i + 1];
  273. son[j] = i;
  274. prnt[i] = prnt[i + 1] = j;
  275. i += 2; j++;
  276. }
  277. freq[T] = 0xffff;
  278. prnt[R] = 0;
  279. }
  280. //---------------------------------------------------------------------------
  281. // reconst /* reconstruct freq tree */
  282. //---------------------------------------------------------------------------
  283. static void reconst()
  284. {
  285. int i, j, k;
  286. unsigned f, l;
  287. /* halven cumulative freq for leaf nodes */
  288. j = 0;
  289. for (i = 0; i < T; i++)
  290. {
  291. if (son[i] >= T)
  292. {
  293. freq[j] = (freq[i] + 1) / 2;
  294. son[j] = son[i];
  295. j++;
  296. }
  297. }
  298. /* make a tree : first, connect children nodes */
  299. for (i = 0, j = N_CHAR; j < T; i += 2, j++)
  300. {
  301. k = i + 1;
  302. f = freq[j] = freq[i] + freq[k];
  303. for (k = j - 1;f < freq[k]; k--);
  304. k++;
  305. l = (j - k) * 2;
  306. (void)memmove(&freq[k + 1], &freq[k], l);
  307. freq[k] = f;
  308. (void)memmove(&son[k + 1], &son[k], l);
  309. son[k] = i;
  310. }
  311. /* connect parent nodes */
  312. for (i = 0; i < T; i++)
  313. {
  314. if ((k = son[i]) >= T)
  315. {
  316. prnt[k] = i;
  317. }
  318. else
  319. {
  320. prnt[k] = prnt[k + 1] = i;
  321. }
  322. }
  323. }
  324. //---------------------------------------------------------------------------
  325. // update() update freq tree
  326. //---------------------------------------------------------------------------
  327. static void update(int c)
  328. {
  329. int i, j, k, l;
  330. if (freq[R] == MAX_FREQ)
  331. {
  332. reconst();
  333. }
  334. c = prnt[c + T];
  335. do {
  336. k = ++freq[c];
  337. //
  338. // swap nodes to keep the tree freq-ordered
  339. //
  340. if (k > freq[l = c + 1])
  341. {
  342. while (k > freq[++l]);
  343. l--;
  344. freq[c] = freq[l];
  345. freq[l] = k;
  346. i = son[c];
  347. prnt[i] = l;
  348. if (i < T)
  349. prnt[i + 1] = l;
  350. j = son[l];
  351. son[l] = i;
  352. prnt[j] = c;
  353. if (j < T)
  354. prnt[j + 1] = c;
  355. son[c] = j;
  356. c = l;
  357. }
  358. } while ((c = prnt[c]) != 0); /* do it until reaching the root */
  359. }
  360. //===========================================================================
  361. //
  362. // COMPRESSION ROUTINES
  363. //
  364. //===========================================================================
  365. #if INCLUDE_LZH_COMP
  366. //---------------------------------------------------------------------------
  367. // DeleteNode
  368. //---------------------------------------------------------------------------
  369. static void DeleteNode(int p) /* Deleting node from the tree */
  370. {
  371. int q;
  372. if (dad[p] == NIL)
  373. return; /* unregistered */
  374. if (rson[p] == NIL)
  375. q = lson[p];
  376. else
  377. if (lson[p] == NIL)
  378. q = rson[p];
  379. else
  380. {
  381. q = lson[p];
  382. if (rson[q] != NIL)
  383. {
  384. do {
  385. q = rson[q];
  386. } while (rson[q] != NIL);
  387. rson[dad[q]] = lson[q];
  388. dad[lson[q]] = dad[q];
  389. lson[q] = lson[p];
  390. dad[lson[p]] = q;
  391. }
  392. rson[q] = rson[p];
  393. dad[rson[p]] = q;
  394. }
  395. dad[q] = dad[p];
  396. if (rson[dad[p]] == p)
  397. rson[dad[p]] = q;
  398. else
  399. lson[dad[p]] = q;
  400. dad[p] = NIL;
  401. }
  402. //---------------------------------------------------------------------------
  403. // InsertNode
  404. //---------------------------------------------------------------------------
  405. static void InsertNode(int r) /* Inserting node to the tree */
  406. {
  407. int i, p, cmp;
  408. unsigned char *key;
  409. unsigned c;
  410. cmp = 1;
  411. key = &text_buf[r];
  412. p = N + 1 + key[0];
  413. rson[r] = lson[r] = NIL;
  414. match_length = 0;
  415. for ( ; ; )
  416. {
  417. if (cmp >= 0)
  418. {
  419. if (rson[p] != NIL)
  420. p = rson[p];
  421. else
  422. {
  423. rson[p] = r;
  424. dad[r] = p;
  425. return;
  426. }
  427. }
  428. else
  429. {
  430. if (lson[p] != NIL)
  431. p = lson[p];
  432. else
  433. {
  434. lson[p] = r;
  435. dad[r] = p;
  436. return;
  437. }
  438. }
  439. for (i = 1; i < F; i++)
  440. if ((cmp = key[i] - text_buf[p + i]) != 0)
  441. break;
  442. if (i > THRESHOLD)
  443. {
  444. if (i > match_length)
  445. {
  446. match_position = ((r - p) & (N - 1)) - 1;
  447. if ((match_length = i) >= F)
  448. break;
  449. }
  450. if (i == match_length)
  451. {
  452. if ((c = ((r - p) & (N - 1)) - 1) < match_position)
  453. {
  454. match_position = c;
  455. }
  456. }
  457. }
  458. }
  459. dad[r] = dad[p];
  460. lson[r] = lson[p];
  461. rson[r] = rson[p];
  462. dad[lson[p]] = r;
  463. dad[rson[p]] = r;
  464. if (rson[dad[p]] == p)
  465. rson[dad[p]] = r;
  466. else
  467. lson[dad[p]] = r;
  468. dad[p] = NIL; /* remove p */
  469. }
  470. //---------------------------------------------------------------------------
  471. // InitTree
  472. //---------------------------------------------------------------------------
  473. static void InitTree(void) /* Initializing tree */
  474. {
  475. int i;
  476. for (i = N + 1; i <= N + 256; i++)
  477. rson[i] = NIL; /* root */
  478. for (i = 0; i < N; i++)
  479. dad[i] = NIL; /* node */
  480. }
  481. //---------------------------------------------------------------------------
  482. // Putcode
  483. //---------------------------------------------------------------------------
  484. static void Putcode(long outfile_ptr, int l, unsigned c,unsigned PtrTypes) /* output c bits */
  485. {
  486. putbuf |= c >> putlen;
  487. if ((putlen += l) >= 8)
  488. {
  489. WritePtr(outfile_ptr, putbuf >> 8, PtrTypes);
  490. codesize++;
  491. if ((putlen -= 8) >= 8)
  492. {
  493. WritePtr(outfile_ptr, putbuf, PtrTypes);
  494. codesize++;
  495. putlen -= 8;
  496. putbuf = c << (l - putlen);
  497. }
  498. else
  499. {
  500. putbuf <<= 8;
  501. }
  502. }
  503. }
  504. //---------------------------------------------------------------------------
  505. // EncodeChar
  506. //---------------------------------------------------------------------------
  507. static void EncodeChar(long outfile_ptr, unsigned c, unsigned PtrTypes)
  508. {
  509. unsigned i;
  510. int j, k;
  511. i = 0;
  512. j = 0;
  513. k = prnt[c + T];
  514. /* search connections from leaf node to the root */
  515. do {
  516. i >>= 1;
  517. //
  518. // if node's address is odd, output 1 else output 0
  519. //
  520. if (k & 1)
  521. i += 0x8000;
  522. j++;
  523. } while ((k = prnt[k]) != R);
  524. Putcode(outfile_ptr, j, i, PtrTypes);
  525. code = i;
  526. len = j;
  527. update(c);
  528. }
  529. //---------------------------------------------------------------------------
  530. // EncodePosition
  531. //---------------------------------------------------------------------------
  532. static void EncodePosition(long outfile_ptr, unsigned c, unsigned PtrTypes)
  533. {
  534. unsigned i;
  535. //
  536. // output upper 6 bits with encoding
  537. //
  538. i = c >> 6;
  539. Putcode(outfile_ptr, p_len[i], (unsigned)p_code[i] << 8,PtrTypes);
  540. //
  541. // output lower 6 bits directly
  542. //
  543. Putcode(outfile_ptr, 6, (c & 0x3f) << 10,PtrTypes);
  544. }
  545. //---------------------------------------------------------------------------
  546. // EncodeEnd
  547. //---------------------------------------------------------------------------
  548. static void EncodeEnd(long outfile_ptr,unsigned PtrTypes)
  549. {
  550. if (putlen)
  551. {
  552. WritePtr(outfile_ptr,(putbuf >> 8),PtrTypes);
  553. codesize++;
  554. }
  555. }
  556. #endif
  557. //===========================================================================
  558. //
  559. // DECOMPRESSION ROUTINES
  560. //
  561. //===========================================================================
  562. #if INCLUDE_LZH_DECOMP
  563. //---------------------------------------------------------------------------
  564. // GetByte
  565. //---------------------------------------------------------------------------
  566. static int GetByte(long infile_ptr, unsigned long *CompressLength, unsigned PtrTypes)
  567. {
  568. unsigned i;
  569. while (getlen <= 8)
  570. {
  571. if (*CompressLength)
  572. {
  573. i = ReadPtr(infile_ptr,PtrTypes);
  574. (*CompressLength)--;
  575. }
  576. else
  577. i = 0;
  578. getbuf |= i << (8 - getlen);
  579. getlen += 8;
  580. }
  581. i = getbuf;
  582. getbuf <<= 8;
  583. getlen -= 8;
  584. return i>>8;
  585. }
  586. //---------------------------------------------------------------------------
  587. // GetBit
  588. //---------------------------------------------------------------------------
  589. static int GetBit(long infile_ptr, unsigned long *CompressLength, unsigned PtrTypes) /* get one bit */
  590. {
  591. int i;
  592. while (getlen <= 8)
  593. {
  594. if (*CompressLength)
  595. {
  596. i = ReadPtr(infile_ptr,PtrTypes);
  597. (*CompressLength)--;
  598. }
  599. else
  600. i = 0;
  601. getbuf |= i << (8 - getlen);
  602. getlen += 8;
  603. }
  604. i = getbuf;
  605. getbuf <<= 1;
  606. getlen--;
  607. return (i < 0);
  608. }
  609. //---------------------------------------------------------------------------
  610. // DecodeChar
  611. //---------------------------------------------------------------------------
  612. static int DecodeChar(long infile_ptr, unsigned long *CompressLength, unsigned PtrTypes)
  613. {
  614. unsigned c;
  615. c = son[R];
  616. /*
  617. * start searching tree from the root to leaves.
  618. * choose node #(son[]) if input bit == 0
  619. * else choose #(son[]+1) (input bit == 1)
  620. */
  621. while (c < T)
  622. {
  623. c += GetBit(infile_ptr,CompressLength,PtrTypes);
  624. c = son[c];
  625. }
  626. c -= T;
  627. update(c);
  628. return c;
  629. }
  630. //---------------------------------------------------------------------------
  631. // DecodePosition
  632. //---------------------------------------------------------------------------
  633. static int DecodePosition(long infile_ptr,unsigned long *CompressLength, unsigned PtrTypes)
  634. {
  635. unsigned i, j, c;
  636. //
  637. // decode upper 6 bits from given table
  638. //
  639. i = GetByte(infile_ptr, CompressLength, PtrTypes);
  640. c = (unsigned)d_code[i] << 6;
  641. j = d_len[i];
  642. //
  643. // input lower 6 bits directly
  644. //
  645. j -= 2;
  646. while (j--)
  647. {
  648. i = (i << 1) + GetBit(infile_ptr, CompressLength, PtrTypes);
  649. }
  650. return c | i & 0x3f;
  651. }
  652. #endif
  653. //===========================================================================
  654. //
  655. // EXTERNAL REFERENCED
  656. // COMPRESSION & DECOMPRESSION
  657. // ROUTINES
  658. //
  659. //===========================================================================
  660. #if INCLUDE_LZH_DECOMP
  661. //---------------------------------------------------------------------------
  662. // lzhDecompress()
  663. //---------------------------------------------------------------------------
  664. long lzhDecompress(void far *infile, void far *outfile, unsigned long OrginalLength, unsigned long CompressLength, unsigned PtrTypes)
  665. {
  666. int i, j, k, r, c;
  667. long count;
  668. datasize = textsize = OrginalLength;
  669. getbuf = 0;
  670. getlen = 0;
  671. if (textsize == 0)
  672. return;
  673. StartHuff();
  674. for (i = 0; i < N - F; i++)
  675. text_buf[i] = ' ';
  676. r = N - F;
  677. for (count = 0; count < textsize; )
  678. {
  679. c = DecodeChar((long)&infile,&CompressLength,PtrTypes);
  680. if (c < 256)
  681. {
  682. WritePtr((long)&outfile,c,PtrTypes);
  683. datasize--; // Dec # of bytes to write
  684. text_buf[r++] = c;
  685. r &= (N - 1);
  686. count++; // inc count of bytes written
  687. }
  688. else
  689. {
  690. i = (r - DecodePosition((long)&infile,&CompressLength,PtrTypes) - 1) & (N - 1);
  691. j = c - 255 + THRESHOLD;
  692. for (k = 0; k < j; k++)
  693. {
  694. c = text_buf[(i + k) & (N - 1)];
  695. WritePtr((long)&outfile,c,PtrTypes);
  696. datasize--; // dec count of bytes to write
  697. text_buf[r++] = c;
  698. r &= (N - 1);
  699. count++; // inc count of bytes written
  700. }
  701. }
  702. if (LZH_DecompressDisplayVector && (count > printcount))
  703. {
  704. LZH_DecompressDisplayVector(OrginalLength,OrginalLength-datasize);
  705. printcount += 1024;
  706. }
  707. }
  708. // printf("%12ld\n", count);
  709. return(count);
  710. }
  711. #endif
  712. #if INCLUDE_LZH_COMP
  713. //---------------------------------------------------------------------------
  714. // lzhCompress()
  715. //---------------------------------------------------------------------------
  716. long lzhCompress(void far *infile, void far *outfile,unsigned long DataLength,unsigned PtrTypes)
  717. {
  718. int i, c, len, r, s, last_match_length;
  719. textsize = DataLength;
  720. if (textsize == 0)
  721. return;
  722. getbuf = 0;
  723. getlen = 0;
  724. textsize = 0; /* rewind and rescan */
  725. codesize = 0;
  726. datasize = 0; // Init our counter of ReadData...
  727. StartHuff();
  728. InitTree();
  729. s = 0;
  730. r = N - F;
  731. for (i = s; i < r; i++)
  732. text_buf[i] = ' ';
  733. for (len = 0; len < F && (DataLength > datasize); len++)
  734. {
  735. c = ReadPtr((long)&infile,PtrTypes);
  736. datasize++; // Dec num of bytes to compress
  737. text_buf[r + len] = c;
  738. }
  739. textsize = len;
  740. for (i = 1; i <= F; i++)
  741. InsertNode(r - i);
  742. InsertNode(r);
  743. do {
  744. if (match_length > len)
  745. match_length = len;
  746. if (match_length <= THRESHOLD)
  747. {
  748. match_length = 1;
  749. EncodeChar((long)&outfile,text_buf[r],PtrTypes);
  750. }
  751. else
  752. {
  753. EncodeChar((long)&outfile, 255 - THRESHOLD + match_length,PtrTypes);
  754. EncodePosition((long)&outfile, match_position,PtrTypes);
  755. }
  756. last_match_length = match_length;
  757. for (i = 0; i < last_match_length && (DataLength > datasize); i++)
  758. {
  759. c = ReadPtr((long)&infile,PtrTypes);
  760. datasize++;
  761. DeleteNode(s);
  762. text_buf[s] = c;
  763. if (s < F - 1)
  764. text_buf[s + N] = c;
  765. s = (s + 1) & (N - 1);
  766. r = (r + 1) & (N - 1);
  767. InsertNode(r);
  768. }
  769. if (LZH_CompressDisplayVector && ((textsize += i) > printcount))
  770. {
  771. LZH_CompressDisplayVector(DataLength,datasize);
  772. printcount += 1024;
  773. }
  774. while (i++ < last_match_length)
  775. {
  776. DeleteNode(s);
  777. s = (s + 1) & (N - 1);
  778. r = (r + 1) & (N - 1);
  779. if (--len)
  780. InsertNode(r);
  781. }
  782. } while (len > 0);
  783. EncodeEnd((long)&outfile,PtrTypes);
  784. return(codesize);
  785. }
  786. #endif