home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / CONTRIB / MBASE / MBASE51.TAR / mbase51 / src / mb_comp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-04  |  3.5 KB  |  177 lines

  1. /*
  2.  * METALBASE 5.1
  3.  *
  4.  * Released January 1st, 1993 by Huan-Ti [ t-richj@microsoft.com ]
  5.  *
  6.  */
  7.  
  8. #include <mbase.h>
  9.  
  10.  
  11. static char bufIn[MAX_CBUF], bufOut[MAX_CBUF];
  12.  
  13. /*
  14.  * PROTOTYPES -----------------------------------------------------------------
  15.  *
  16.  */
  17.  
  18.    static long     _bufCompress    XARGS( (long) );
  19.    static long     _bufDecompress  XARGS( (long) );
  20.  
  21.  
  22. /*
  23.  * DECOMPRESS -----------------------------------------------------------------
  24.  *
  25.  */
  26.  
  27. #define sigCOMPRESSED '+'
  28. #define sigFINISHED   '-'
  29.  
  30. long
  31. _fhDecompress (fhOut, fhIn) /* Decompress enough text for one page, ret size */
  32. file           fhOut, fhIn;
  33. {
  34.    char  sig;
  35.    long  nRead;
  36.  
  37.    readx (fhIn, &sig, 1);
  38.    if (sig == sigFINISHED)
  39.       return  0L;
  40.    if (sig != sigCOMPRESSED)
  41.       return -1L;
  42.  
  43.  
  44.    readx (fhIn, &nRead, 4);    /* Find out how much we'll need to decompress */
  45.    readx (fhIn, bufIn, nRead); /* And read enough text for the upcoming page */
  46.  
  47.    if (nRead == 0L)
  48.       return -1L;
  49.  
  50. /*
  51.  * Decompress bufIn->bufOut, set nRead=size of data, decompressed
  52.  *
  53.  */
  54.    nRead = _bufDecompress (nRead);
  55.  
  56.  
  57. /*
  58.  * Now that we've decompressed it, store the data in the output file and
  59.  * return the decompressed length:
  60.  *
  61.  */
  62.  
  63.    writx (fhOut, bufOut, nRead);
  64.  
  65.    return nRead;
  66. }
  67.  
  68.  
  69. /*
  70.  * COMPRESS -------------------------------------------------------------------
  71.  *
  72.  */
  73.  
  74. long
  75. _fhCompress (fhOut, fhIn)     /* Compress one page; return compressed length */
  76. file         fhOut, fhIn;
  77. {
  78.    long  nRead;
  79.    char  sig;
  80.  
  81.  
  82.    if ((nRead = readx (fhIn, bufIn, MAX_CBUF)) == 0L)
  83.       {
  84.       sig = sigFINISHED;
  85.       writx (fhOut, &sig, 1);   /* We have to use at least 4 bytes here! */
  86.       writx (fhOut, &sig, 1);
  87.       writx (fhOut, &sig, 1);
  88.       writx (fhOut, &sig, 1);
  89.       return 4;
  90.       }
  91.  
  92. /*
  93.  * Compress bufIn->bufOut, store in fhOut, set nRead=new size
  94.  *
  95.  */
  96.    nRead = _bufCompress (nRead);
  97.  
  98.    sig = sigCOMPRESSED;
  99.    writx (fhOut, &sig, 1);
  100.    writx (fhOut, &nRead, 4);        /* Number of chars needed to decomp page */
  101.    writx (fhOut, bufOut, nRead);    /* Actual data to decompress, later      */
  102.  
  103.    return 5+nRead;
  104. }
  105.  
  106.  
  107. /*
  108.  * SERVICE ROUTINES -----------------------------------------------------------
  109.  *
  110.  */
  111.  
  112. long
  113. _fhCopy (fhOut, fhIn, n)     /* Copy one page; return number of bytes copied */
  114. file     fhOut, fhIn;
  115. long                  n;
  116. {
  117.    long  nRead;
  118.    long  nWrote;
  119.  
  120.    if ((nRead = readx (fhIn, bufIn, n)) != 0L)
  121.       {
  122.       if ((nWrote = writx (fhOut, bufIn, nRead)) != nRead)
  123.          return nWrote;
  124.       }
  125.  
  126.    return nRead;
  127. }
  128.  
  129.  
  130. /*
  131.  * Decompress bufIn->bufOut, return uncompressed length
  132.  *
  133.  */
  134.  
  135. static long
  136. _bufDecompress (n)
  137. long            n;
  138. {
  139.  
  140.    /*
  141.     * This routine will house the decompression routine; when called, 'bufIn'
  142.     * contains 'n' bytes which need to be decompressed--depending on the
  143.     * routine I eventually put in here, the first bytes of bufIn may contain
  144.     * information necessary for decompressing this page.  Niether bufIn nor
  145.     * bufOut should ever exceed MAX_CBUF.
  146.     *
  147.     */
  148.    numcpy (bufOut, bufIn, (int)n);
  149.  
  150.    return n;
  151. }
  152.  
  153.  
  154. /*
  155.  * Compress bufIn->bufOut, return compressed length
  156.  *
  157.  */
  158.  
  159. static long
  160. _bufCompress (n)
  161. long          n;
  162. {
  163.  
  164.    /*
  165.     * This routine will house the compression routine; when called, 'bufIn'
  166.     * contains 'n' bytes which need to be compressed, and 'bufOut' should be
  167.     * filled with compressed text, possibly with whatever headers are
  168.     * necessary for decompression at the front.  Niether bufIn nor bufOut
  169.     * should ever exceed MAX_CBUF.
  170.     *
  171.     */
  172.    numcpy (bufOut, bufIn, (int)n);
  173.  
  174.    return n;
  175. }
  176.  
  177.