home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / misc / volume35 / freeze / part03 < prev    next >
Encoding:
Text File  |  1993-02-21  |  16.7 KB  |  571 lines

  1. Newsgroups: comp.sources.misc
  2. From: leo@ipmce.su (Leonid A. Broukhis)
  3. Subject: v35i079:  freeze - Freeze/melt compression program, v2.5, Part03/03
  4. Message-ID: <1993Feb22.035731.14973@sparky.imd.sterling.com>
  5. X-Md4-Signature: fc02ccd5b0839f7e4f267703968f5747
  6. Date: Mon, 22 Feb 1993 03:57:31 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: leo@ipmce.su (Leonid A. Broukhis)
  10. Posting-number: Volume 35, Issue 79
  11. Archive-name: freeze/part03
  12. Environment: ISC, Xenix, SunOS, MS-DOS
  13. Supersedes: freeze: Volume 25, Issue 12-13
  14.  
  15. #! /bin/sh
  16. # This is a shell archive.  Remove anything before this line, then feed it
  17. # into a shell via "sh file" or similar.  To overwrite existing files,
  18. # type "sh file -c".
  19. # Contents:  bitio.c bitio.h config.h.in config.msc debug.c default.c
  20. #   patchlev.h showhuf.c
  21. # Wrapped by kent@sparky on Sat Feb 20 22:49:57 1993
  22. PATH=/bin:/usr/bin:/usr/ucb:/usr/local/bin:/usr/lbin ; export PATH
  23. echo If this archive is complete, you will see the following message:
  24. echo '          "shar: End of archive 3 (of 3)."'
  25. if test -f 'bitio.c' -a "${1}" != "-c" ; then 
  26.   echo shar: Will not clobber existing file \"'bitio.c'\"
  27. else
  28.   echo shar: Extracting \"'bitio.c'\" \(165 characters\)
  29.   sed "s/^X//" >'bitio.c' <<'END_OF_FILE'
  30. Xunsigned bitbuf = 0;    /* use native word size, it's faster */
  31. Xint     bitlen = 0, overrun = 0;
  32. Xlong    bytes_out;      /* counter of bytes processed by PutBits */
  33. END_OF_FILE
  34.   if test 165 -ne `wc -c <'bitio.c'`; then
  35.     echo shar: \"'bitio.c'\" unpacked with wrong size!
  36.   fi
  37.   # end of 'bitio.c'
  38. fi
  39. if test -f 'bitio.h' -a "${1}" != "-c" ; then 
  40.   echo shar: Will not clobber existing file \"'bitio.h'\"
  41. else
  42.   echo shar: Extracting \"'bitio.h'\" \(2223 characters\)
  43.   sed "s/^X//" >'bitio.h' <<'END_OF_FILE'
  44. X/* Some definitions for faster bit-level I/O */
  45. X/* Assumptions: local variables "loclen" and "locbuf" are defined
  46. X * via "DefBits";
  47. X * AvailBits() is called before all bit input operations, the
  48. X * maximum allowed argument for AvailBits() is bits(bitbuf) -7;
  49. X * FlushBits() is called often enough while bit output operations;
  50. X * KeepBits() is called as opposite to DefBits.
  51. X */
  52. X
  53. Xextern unsigned bitbuf; /* Bit I/O buffers */
  54. Xextern int  bitlen;     /* Number of bits actually in `bitbuf' */
  55. Xextern int  overrun;    /* at least as many bytes were read as EOF */
  56. Xextern long bytes_out;  /* we use it, we declare it */
  57. X
  58. X#define bits(x) ((int)sizeof(x)*8)
  59. X#define BYSH  (bits(bitbuf)-8)
  60. X#define BISH  (bits(bitbuf)-1)
  61. X
  62. X#define InitIO()        { overrun = bitlen = 0; bitbuf = 0; }
  63. X
  64. X#define DefBits         register unsigned locbuf = bitbuf;\
  65. Xregister int loclen = bitlen
  66. X
  67. X#define FillBits()   if (loclen <= bits(bitbuf) - 8) {\
  68. X    do {\
  69. X        locbuf |= (unsigned)(getchar() & 0xFF) << (BYSH - loclen);\
  70. X        loclen += 8;\
  71. X    } while (loclen <= bits(bitbuf) - 8);\
  72. Xif (feof(stdin)) overrun++;\
  73. X}
  74. X
  75. X#define FlushBits() if (loclen >= 8) do {\
  76. X    putchar ((int)(locbuf >> BYSH));\
  77. X    bytes_out++;\
  78. X    locbuf <<= 8;\
  79. X    loclen -= 8;\
  80. X} while (loclen >= 8)
  81. X
  82. X/* FlushTail works with global variables !! */
  83. X#define FlushTail() if (bitlen) {\
  84. X    putchar((int)(bitbuf >> BYSH));\
  85. X    bytes_out++;\
  86. X}
  87. X
  88. X#define KeepBits()      bitbuf = locbuf, bitlen = loclen
  89. X
  90. X/* GetX() macros may be used only in "var op= GetX();" statements !! */
  91. X
  92. X#define GetBit()  /* var op= */locbuf >> BISH, locbuf <<= 1, loclen--
  93. X
  94. X#define GetByte() /* var op= */locbuf >> BYSH, locbuf <<= 8, loclen -= 8
  95. X
  96. X/* NB! `n' is used more than once here! */
  97. X#define GetNBits(n) /* var op= */ locbuf >> (bits(bitbuf) - (n)),\
  98. X    locbuf <<= (n), loclen -= (n)
  99. X
  100. X/* Puts n MSBs of var to the stream, assume other bits == 0 */
  101. X#define PutNBits(var, n) locbuf |= (unsigned)(var) >> loclen, loclen += (n)
  102. X
  103. X/* Puts n LSBs of var to the stream, assume other bits == 0 */
  104. X#define PutNLowBits(var, n) locbuf |= (unsigned)(var) << (bits(bitbuf) -\
  105. X(n) - loclen), loclen += (n)
  106. X
  107. X/* Puts LSB (!) of var to the stream, isn't used now */
  108. X#define PutBit(var) locbuf |= (unsigned)((var) & 1) << (BISH - loclen), loclen++
  109. END_OF_FILE
  110.   if test 2223 -ne `wc -c <'bitio.h'`; then
  111.     echo shar: \"'bitio.h'\" unpacked with wrong size!
  112.   fi
  113.   # end of 'bitio.h'
  114. fi
  115. if test -f 'config.h.in' -a "${1}" != "-c" ; then 
  116.   echo shar: Will not clobber existing file \"'config.h.in'\"
  117. else
  118.   echo shar: Extracting \"'config.h.in'\" \(2615 characters\)
  119.   sed "s/^X//" >'config.h.in' <<'END_OF_FILE'
  120. X/* This is a configuration file prototype, copy it to config.h and      */
  121. X/* "#define" appropriate macros if you have problems with "configure".  */
  122. X
  123. X/* define as "int" or "void"; default (undefined) means "void"          */
  124. X#undef RETSIGTYPE
  125. X
  126. X/* define if your computer/system allows unaligned word access          */
  127. X#undef ALLOW_MISALIGN
  128. X
  129. X/* define if sizeof(int) == 2                                           */
  130. X#undef INT_16_BITS
  131. X
  132. X/* define if sizeof(unsigned short) > 2                                 */
  133. X#undef BIGSHORTS
  134. X
  135. X/* define if your computer cannot handle data items of more than 64K    */
  136. X#undef SEGMENTED
  137. X
  138. X/* define if filenames can be of more than 14 chars                     */
  139. X#undef HAVE_LONG_FILE_NAMES
  140. X
  141. X/* define no more than one, according to your standard #include's       */
  142. X/* if you have <dirent.h>                                               */
  143. X#undef DIRENT
  144. X/* if you have <sys/ndir.h>                                             */
  145. X#undef SYSNDIR
  146. X/* if you have <sys/dir.h>                                              */
  147. X#undef SYSDIR
  148. X
  149. X/* define if you have <sys/stdtypes.h>                                  */
  150. X#undef HAVE_SYS_STDTYPES_H
  151. X
  152. X/* define if you have "rindex" and "setlinebuf" correspondingly         */
  153. X#undef HAVE_RINDEX
  154. X#undef HAVE_SETLINEBUF
  155. X
  156. X/* define no more than one, according to your standard #include's       */
  157. X/* if you have <utime.h>                                                */
  158. X#undef UTIME
  159. X/* if you have <sys/utime.h>                                            */
  160. X#undef SYSUTIME
  161. X/* if you have "struct timeval" in <sys/time.h>                         */
  162. X#undef SYSTIME
  163. X
  164. X/* define if you want to have freeze compatible with vers. 1.0          */
  165. X#undef COMPAT
  166. X
  167. X/* define if your system has multibyte NEWLINE (as in MS-DOS) and       */
  168. X/* you want to do text conversion by default                            */
  169. X#undef TEXT_DEFAULT
  170. X
  171. X/* define if you want to build freeze in small model  (64K data)        */
  172. X/* (segmented architectures only)                                       */
  173. X#undef TINY
  174. X
  175. X/* define if you want to decrease the amount of memory but without      */
  176. X/* 64K restriction (no sense for 16-bit machines)                       */
  177. X#undef SMALL
  178. X
  179. X/* define to increase the compression speed by about 10% at the cost    */
  180. X/* of some tenths of % compression rate                                 */
  181. X#undef FASTHASH
  182. X
  183. X/* default Huffman values, define if you don't like the default        */
  184. X/* 0,0,1,2,6,19,34,0. These below are reasonably good also.            */
  185. X/* #define HUFVALUES 0,1,1,1,4,10,27,18                                */
  186. END_OF_FILE
  187.   if test 2615 -ne `wc -c <'config.h.in'`; then
  188.     echo shar: \"'config.h.in'\" unpacked with wrong size!
  189.   fi
  190.   # end of 'config.h.in'
  191. fi
  192. if test -f 'config.msc' -a "${1}" != "-c" ; then 
  193.   echo shar: Will not clobber existing file \"'config.msc'\"
  194. else
  195.   echo shar: Extracting \"'config.msc'\" \(2552 characters\)
  196.   sed "s/^X//" >'config.msc' <<'END_OF_FILE'
  197. X/* This is a configuration file prototype, copy it to config.h and      */
  198. X/* "#define" appropriate macros if you have problems with "configure".  */
  199. X
  200. X/* define as "int" or "void" */
  201. X#define RETSIGTYPE void
  202. X
  203. X/* define if your computer/system allows unaligned word access          */
  204. X#define ALLOW_MISALIGN
  205. X
  206. X/* define if sizeof(int) == 2                                           */
  207. X#define INT_16_BITS
  208. X
  209. X/* define if your computer cannot handle data items of more than 64K    */
  210. X#define SEGMENTED
  211. X
  212. X/* define if filenames can be of more than 14 chars                     */
  213. X#undef HAVE_LONG_FILE_NAMES
  214. X
  215. X/* define no more than one of, according to your standard #include's    */
  216. X/* if you have <dirent.h>                                               */
  217. X#undef DIRENT
  218. X/* if you have <sys/ndir.h>                                             */
  219. X#undef SYSNDIR
  220. X/* if you have <sys/dir.h>                                              */
  221. X#undef SYSDIR
  222. X
  223. X/* define if you have <sys/stdtypes.h>                                  */
  224. X#undef HAVE_SYS_STDTYPES_H
  225. X
  226. X/* define if you have "rindex" and "setlinebuf" correspondingly         */
  227. X#undef HAVE_RINDEX
  228. X#undef HAVE_SETLINEBUF
  229. X
  230. X/* define no more than one of, according to your standard #include's    */
  231. X/* if you have <utime.h>                                                */
  232. X#define UTIME
  233. X/* if you have <sys/utime.h>                                            */
  234. X#undef SYSUTIME
  235. X/* if you have "struct timeval" in <sys/time.h>                         */
  236. X#undef SYSTIME
  237. X
  238. X/* define if you want to have freeze compatible with vers. 1.0          */
  239. X#undef COMPAT
  240. X
  241. X/* define if your system has multibyte NEWLINE (as in MS-DOS) and       */
  242. X/* you want to do text conversion by default                            */
  243. X#undef TEXT_DEFAULT
  244. X
  245. X/* define if you want to build freeze in small model  (64K data)        */
  246. X/* (segmented architectures only)                                       */
  247. X#undef TINY
  248. X
  249. X/* define if you want to decrease the amount of memory but without      */
  250. X/* 64K restriction (no sense for 16-bit machines)                       */
  251. X#undef SMALL
  252. X
  253. X/* define to increase the compression speed by about 10% at the cost    */
  254. X/* of some tenths of % compression rate                                 */
  255. X#undef FASTHASH
  256. X
  257. X/* default Huffman values, define if you don't like the default        */
  258. X/* 0,0,1,2,6,19,34,0. These below are reasonably good also.            */
  259. X/* #define HUFVALUES 0,1,1,1,4,10,27,18                                */
  260. END_OF_FILE
  261.   if test 2552 -ne `wc -c <'config.msc'`; then
  262.     echo shar: \"'config.msc'\" unpacked with wrong size!
  263.   fi
  264.   # end of 'config.msc'
  265. fi
  266. if test -f 'debug.c' -a "${1}" != "-c" ; then 
  267.   echo shar: Will not clobber existing file \"'debug.c'\"
  268. else
  269.   echo shar: Extracting \"'debug.c'\" \(1551 characters\)
  270.   sed "s/^X//" >'debug.c' <<'END_OF_FILE'
  271. X#if defined(DEBUG)
  272. X#include "freeze.h"
  273. X#include "huf.h"
  274. X#include "bitio.h"
  275. X
  276. X          /*---------------------------*/
  277. X          /*      DEBUG      MODULE    */
  278. X          /*---------------------------*/
  279. Xvoid
  280. Xprintcodes(mode)
  281. X{
  282. X    /*
  283. X     * Just print out codes from input file.  For debugging.
  284. X     */
  285. X    register short k, c, col = 0;
  286. X
  287. X#ifdef COMPAT
  288. X    if (!mode) {
  289. X        StartHuff(N_CHAR1);
  290. X        init(Table1);
  291. X    } else
  292. X#endif
  293. X    {
  294. X        if (read_header() == EOF) {
  295. X            fprintf(stderr, "Bad header\n");
  296. X            return;
  297. X        }
  298. X        StartHuff(N_CHAR2);
  299. X        init(Table2);
  300. X    }
  301. X
  302. X    InitIO();
  303. X
  304. X    for (;;) {
  305. X
  306. X        if((c = DecodeChar()) == ENDOF)
  307. X            break;
  308. X        if (c < 256) {
  309. X        fprintf(stderr, "%5d%c", c,
  310. X            (col+=8) >= 74 ? (col = 0, '\n') : '\t' );
  311. X        } else {
  312. X        c = c - 256 + THRESHOLD;
  313. X
  314. X        k = DecodePosition();
  315. X
  316. X        fprintf(stderr, "%2d-%d%c", c, k,
  317. X            (col+=8) >= 74 ? (col = 0, '\n') : '\t' );
  318. X        }
  319. X    }
  320. X    putc( '\n', stderr );
  321. X    exit( 0 );
  322. X}
  323. X
  324. X/* for pretty char printing */
  325. X
  326. Xchar *
  327. Xpr_char(c)
  328. X    register uc_t c;
  329. X{
  330. X    static char buf[5];
  331. X    register i = 4;
  332. X    buf[4] = '\0';
  333. X    if ( (isascii((int)c) && isprint((int)c) && c != '\\') || c == ' ' ) {
  334. X        buf[--i] = c;
  335. X    } else {
  336. X        switch( c ) {
  337. X        case '\n': buf[--i] = 'n'; break;
  338. X        case '\t': buf[--i] = 't'; break;
  339. X        case '\b': buf[--i] = 'b'; break;
  340. X        case '\f': buf[--i] = 'f'; break;
  341. X        case '\r': buf[--i] = 'r'; break;
  342. X        case '\\': buf[--i] = '\\'; break;
  343. X        default:
  344. X        buf[--i] = '0' + c % 8;
  345. X        buf[--i] = '0' + (c / 8) % 8;
  346. X        buf[--i] = '0' + c / 64;
  347. X        break;
  348. X        }
  349. X        buf[--i] = '\\';
  350. X    }
  351. X    return &buf[i];
  352. X}
  353. X#endif
  354. END_OF_FILE
  355.   if test 1551 -ne `wc -c <'debug.c'`; then
  356.     echo shar: \"'debug.c'\" unpacked with wrong size!
  357.   fi
  358.   # end of 'debug.c'
  359. fi
  360. if test -f 'default.c' -a "${1}" != "-c" ; then 
  361.   echo shar: Will not clobber existing file \"'default.c'\"
  362. else
  363.   echo shar: Extracting \"'default.c'\" \(1274 characters\)
  364.   sed "s/^X//" >'default.c' <<'END_OF_FILE'
  365. X#include "freeze.h"
  366. X#include <errno.h>
  367. X
  368. X        /*-------------------------------*/
  369. X        /*     DEFAULTS FILE HANDLING    */
  370. X        /*-------------------------------*/
  371. X
  372. X#define OK      0
  373. X#define FAIL    NULL
  374. X#define NOFILE  ((FILE *) 0)
  375. X#define MAXLINE 128
  376. X
  377. Xchar            *strchr();
  378. Xstatic FILE     *defd = NOFILE;  /* defaults file stream */
  379. X
  380. Xint     defopen(fname)          /* open | reopen | close defaults file */
  381. X    char    *fname;
  382. X{
  383. X    register FILE   *fd;
  384. X
  385. X    if (!fname) {
  386. X        if (defd)
  387. X            (void) fclose(defd);
  388. X        defd = NOFILE;
  389. X        return OK;
  390. X    }
  391. X
  392. X    if (!(fd = fopen(fname, "r")))
  393. X        return errno;                   /* problems opening file */
  394. X
  395. X    defd = fd;
  396. X    return OK;
  397. X}
  398. X
  399. Xstatic char     defline[MAXLINE + 1];
  400. X
  401. Xchar    *defread(pattern)
  402. X    register char   *pattern;
  403. X{
  404. X    register        sz_patt;
  405. X    register char   *cp;
  406. X
  407. X    if (!defd)
  408. X        return FAIL;            /* defaults file not opened */
  409. X
  410. X    rewind(defd);
  411. X    sz_patt = strlen(pattern);
  412. X
  413. X    while (fgets(defline, MAXLINE, defd)) {
  414. X        if (!(cp = strchr(defline, '\n')))
  415. X            return FAIL;     /* line too long  */
  416. X        if (cp - defline < sz_patt)
  417. X            continue;       /* line too short */
  418. X        *cp = '\0';
  419. X        if (!strncmp(pattern, defline, sz_patt))
  420. X            return defline + sz_patt;       /* match found */
  421. X    }
  422. X
  423. X    return FAIL;                    /* no matching lines */
  424. X}
  425. END_OF_FILE
  426.   if test 1274 -ne `wc -c <'default.c'`; then
  427.     echo shar: \"'default.c'\" unpacked with wrong size!
  428.   fi
  429.   # end of 'default.c'
  430. fi
  431. if test -f 'patchlev.h' -a "${1}" != "-c" ; then 
  432.   echo shar: Will not clobber existing file \"'patchlev.h'\"
  433. else
  434.   echo shar: Extracting \"'patchlev.h'\" \(48 characters\)
  435.   sed "s/^X//" >'patchlev.h' <<'END_OF_FILE'
  436. X#define PATCHLEVEL 0
  437. X#define PATCHDATE "2/7/93"
  438. END_OF_FILE
  439.   if test 48 -ne `wc -c <'patchlev.h'`; then
  440.     echo shar: \"'patchlev.h'\" unpacked with wrong size!
  441.   fi
  442.   # end of 'patchlev.h'
  443. fi
  444. if test -f 'showhuf.c' -a "${1}" != "-c" ; then 
  445.   echo shar: Will not clobber existing file \"'showhuf.c'\"
  446. else
  447.   echo shar: Extracting \"'showhuf.c'\" \(1964 characters\)
  448.   sed "s/^X//" >'showhuf.c' <<'END_OF_FILE'
  449. X#include "freeze.h"
  450. X#include "huf.h"
  451. X
  452. Xuc_t Table2[9];
  453. X
  454. X/* prints out Huffman information from a frozen file, just for fun
  455. X * and testing purposes.
  456. X */
  457. X
  458. Xint main(argc, argv)
  459. Xint argc; char ** argv;
  460. X{
  461. X    if (argc != 2) {
  462. X        fprintf(stderr, "Usage: showhuf frozen_file\n");
  463. X        return 1;
  464. X    }
  465. X
  466. X    if (freopen(argv[1], "r", stdin) == NULL) {
  467. X        fprintf(stderr, "showhuf: can't open file %s", argv[1]);
  468. X        perror(" ");
  469. X        return 1;
  470. X    }
  471. X    if (getchar() != MAGIC1)
  472. X        goto reject;
  473. X
  474. X    switch (getchar()) {
  475. X    case MAGIC2_1:
  476. X        printf("%s: no Huffman table (old-style)\n", argv[1]);
  477. X        return 0;
  478. X
  479. X    case MAGIC2_2:
  480. X        break;
  481. X
  482. X    default: reject:
  483. X        printf("%s is not in frozen format\n", argv[1]);
  484. X        return 0;
  485. X    }
  486. X
  487. X    /* Now the real work begins... */
  488. X
  489. X    printf("%s: ", argv[1]);
  490. X
  491. X    if (read_header() == EOF)
  492. X        return 1;
  493. X
  494. X    printf("%d,%d,%d,%d,%d,%d,%d,%d\n",
  495. X        Table2[1], Table2[2], Table2[3], Table2[4],
  496. X        Table2[5], Table2[6], Table2[7], Table2[8]);
  497. X
  498. X    /* ... and ends */
  499. X
  500. X    return 0;
  501. X}
  502. X
  503. X/* Reconstructs `Table' from the header of the frozen file and checks
  504. X    its correctness. Returns 0 if OK, EOF otherwise.
  505. X*/
  506. X
  507. Xint read_header() {
  508. X    short i, j;
  509. X    i = getchar() & 0xFF;
  510. X    i |= (getchar() & 0xFF) << 8;
  511. X    Table2[1] = i & 1; i >>= 1;
  512. X    Table2[2] = i & 3; i >>= 2;
  513. X    Table2[3] = i & 7; i >>= 3;
  514. X    Table2[4] = i & 0xF; i >>= 4;
  515. X    Table2[5] = i & 0x1F; i >>= 5;
  516. X
  517. X    if (i & 1 || (i = getchar()) & 0xC0) {
  518. X        fprintf(stderr, "Unknown header format.\n");
  519. X        return EOF;
  520. X    }
  521. X
  522. X    Table2[6] = i & 0x3F;
  523. X
  524. X    i = Table2[1] + Table2[2] + Table2[3] + Table2[4] +
  525. X    Table2[5] + Table2[6];
  526. X
  527. X    i = 62 - i;     /* free variable length codes for 7 & 8 bits */
  528. X
  529. X    j = 128 * Table2[1] + 64 * Table2[2] + 32 * Table2[3] +
  530. X    16 * Table2[4] + 8 * Table2[5] + 4 * Table2[6];
  531. X
  532. X    j = 256 - j;    /* free byte images for these codes */
  533. X
  534. X/*      Equation:
  535. X        Table[7] + Table[8] = i
  536. X    2 * Table[7] + Table[8] = j
  537. X*/
  538. X    j -= i;
  539. X    if (j < 0 || i < j) {
  540. X        printf("Warning - bad Huffman table");
  541. X        return 0;
  542. X    }
  543. X    Table2[7] = j;
  544. X    Table2[8] = i - j;
  545. X
  546. X    return 0;
  547. X}
  548. END_OF_FILE
  549.   if test 1964 -ne `wc -c <'showhuf.c'`; then
  550.     echo shar: \"'showhuf.c'\" unpacked with wrong size!
  551.   fi
  552.   # end of 'showhuf.c'
  553. fi
  554. echo shar: End of archive 3 \(of 3\).
  555. cp /dev/null ark3isdone
  556. MISSING=""
  557. for I in 1 2 3 ; do
  558.     if test ! -f ark${I}isdone ; then
  559.     MISSING="${MISSING} ${I}"
  560.     fi
  561. done
  562. if test "${MISSING}" = "" ; then
  563.     echo You have unpacked all 3 archives.
  564.     rm -f ark[1-9]isdone
  565. else
  566.     echo You still must unpack the following archives:
  567.     echo "        " ${MISSING}
  568. fi
  569. exit 0
  570. exit 0 # Just in case...
  571.