home *** CD-ROM | disk | FTP | other *** search
/ ftp.barnyard.co.uk / 2015.02.ftp.barnyard.co.uk.tar / ftp.barnyard.co.uk / cpm / walnut-creek-CDROM / CPM / UTILS / SQUSQ / UTR.C < prev    next >
C/C++ Source or Header  |  2000-06-30  |  2KB  |  89 lines

  1. #include <bdscio.h>
  2. #include <dio.h>
  3. #include "sqcom.h"
  4. #include "usq.h"
  5.  
  6. /* initialize decoding functions */
  7.  
  8. init_cr()
  9. {
  10.     repct = 0;
  11. }
  12.  
  13. init_huff()
  14. {
  15.     bpos = 99;    /* force initial read */
  16. }
  17.  
  18. /* Get bytes with decoding - this decodes repetition,
  19.  * calls getuhuff to decode file stream into byte
  20.  * level code with only repetition encoding.
  21.  *
  22.  * The code is simple passing through of bytes except
  23.  * that DLE is encoded as DLE-zero and other values
  24.  * repeated more than twice are encoded as value-DLE-count.
  25.  */
  26.  
  27. int
  28. getcr(ib)
  29. struct _buf *ib;
  30. {
  31.     int c;
  32.  
  33.     if(repct > 0) {
  34.         /* Expanding a repeated char */
  35.         --repct;
  36.         return value;
  37.     } else {
  38.         /* Nothing unusual */
  39.         if((c = getuhuff(ib)) != DLE) {
  40.             /* It's not the special delimiter */
  41.             value = c;
  42.             if(value == EOF)
  43.                 repct = LARGE;
  44.             return value;
  45.         } else {
  46.             /* Special token */
  47.             if((repct = getuhuff(ib)) == 0)
  48.                 /* DLE, zero represents DLE */
  49.                 return DLE;
  50.             else {
  51.                 /* Begin expanding repetition */
  52.                 repct -= 2;    /* 2nd time */
  53.                 return value;
  54.             }
  55.         }
  56.     }
  57. }
  58.  
  59. /* Decode file stream into a byte level code with only
  60.  * repetition encoding remaining.
  61.  */
  62.  
  63. int
  64. getuhuff(ib)
  65. struct _buf *ib;
  66. {
  67.     int i;
  68.     int bitval;
  69.  
  70.     /* Follow bit stream in tree to a leaf*/
  71.     i = 0;    /* Start at root of tree */
  72.     do {
  73.         if(++bpos > 7) {
  74.             if((curin = getc(ib)) == ERROR)
  75.                 return ERROR;
  76.             bpos = 0;
  77.             /* move a level deeper in tree */
  78.             i = dnode[i].children[1 & curin];
  79.         } else
  80.             i = dnode[i].children[1 & (curin >>= 1)];
  81.     } while(i >= 0);
  82.  
  83.     /* Decode fake node index to original data value */
  84.     i = -(i + 1);
  85.     /* Decode special endfile token to normal EOF */
  86.     i = (i == SPEOF) ? EOF : i;
  87.     return i;
  88. }
  89.