home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / rzsz_3_36_3_src.lzh / zmr.c < prev    next >
Text File  |  1996-01-08  |  5KB  |  215 lines

  1. /*
  2.  * File: zmr.c
  3.  * Copyright 1988, 1994 Omen Technology Inc All Rights Reserved
  4.  *
  5.  *
  6.  * 
  7.  * This module implements ZMODEM Run Length Encoding, an
  8.  * extension that was not funded by the original Telenet
  9.  * development contract.
  10.  * 
  11.  *  This software may be freely used for educational (didactic
  12.  *  only) purposes.  This software may also be freely used to
  13.  *  support file transfer operations to or from licensed Omen
  14.  *  Technology products.  Use with other commercial or shareware
  15.  *  programs (Crosstalk, Procomm, etc.) REQUIRES REGISTRATION.
  16.  *
  17.  *  Any programs which use part or all of this software must be
  18.  *  provided in source form with this notice intact except by
  19.  *  written permission from Omen Technology Incorporated.
  20.  * 
  21.  * Use of this software for commercial or administrative purposes
  22.  * except when exclusively limited to interfacing Omen Technology
  23.  * products requires a per port license payment of $20.00 US per
  24.  * port (less in quantity).  Use of this code by inclusion,
  25.  * decompilation, reverse engineering or any other means
  26.  * constitutes agreement to these conditions and acceptance of
  27.  * liability to license the materials and payment of reasonable
  28.  * legal costs necessary to enforce this license agreement.
  29.  *
  30.  *
  31.  *        Omen Technology Inc
  32.  *        Post Office Box 4681
  33.  *        Portland OR 97208
  34.  *
  35.  *    This code is made available in the hope it will be useful,
  36.  *    BUT WITHOUT ANY WARRANTY OF ANY KIND OR LIABILITY FOR ANY
  37.  *    DAMAGES OF ANY KIND.
  38.  *
  39.  *    ZMODEM RLE compression and decompression functions
  40.  */
  41.  
  42. #ifdef OS9
  43. #include <stdio.h>
  44. #include "os9.h"
  45. #include "zmodem.h"
  46. /* from rbsb.c */
  47. extern FILE *Ttystream;
  48. /* from zm.c */
  49. extern char *Zendnames[];
  50. extern char badcrc[];
  51. #endif /* OS9 */
  52.  
  53. /* All use of a lowercase el has been made uppercase due to difficulty
  54.    of telling it from a 1 (one)  either in print, or on screen GH */
  55.  
  56. /* Send data subpacket RLE encoded with 32 bit FCS */
  57. zsdar32(buf, length, frameend)
  58. char *buf;
  59. {
  60. #ifndef m6809
  61.     register int c, L, n;
  62. #else
  63.     int c,L,n;
  64. #endif
  65. #ifdef m6809
  66.     long crc;
  67. #else
  68.     register unsigned long crc;
  69. #endif
  70.  
  71.     crc = 0xFFFFFFFFL;  L = *buf++ & 0377;
  72.     if (length == 1) {
  73.         zsendline(L); crc = UPDC32(L, crc);
  74.         if (L == ZRESC) {
  75.             zsendline(1); crc = UPDC32(1, crc);
  76.         }
  77.     } else {
  78.         for (n = 0; --length >= 0; ++buf) {
  79.             if ((c = *buf & 0377) == L && n < 126 && length>0) {
  80.                 ++n;  continue;
  81.             }
  82.             switch (n) {
  83.             case 0:
  84.                 zsendline(L);
  85.                 crc = UPDC32(L, crc);
  86.                 if (L == ZRESC) {
  87.                     zsendline(0100); crc = UPDC32(0100, crc);
  88.                 }
  89.                 L = c; break;
  90.             case 1:
  91.                 if (L != ZRESC) {
  92.                     zsendline(L); zsendline(L);
  93.                     crc = UPDC32(L, crc);
  94.                     crc = UPDC32(L, crc);
  95.                     n = 0; L = c; break;
  96.                 }
  97.                 /* **** FALL THRU TO **** */
  98.             default:
  99.                 zsendline(ZRESC); crc = UPDC32(ZRESC, crc);
  100.                 if (L == 040 && n < 34) {
  101.                     n += 036;
  102.                     zsendline(n); crc = UPDC32(n, crc);
  103.                 }
  104.                 else {
  105.                     n += 0101;
  106.                     zsendline(n); crc = UPDC32(n, crc);
  107.                     zsendline(L); crc = UPDC32(L, crc);
  108.                 }
  109.                 n = 0; L = c; break;
  110.             }
  111.         }
  112.     }
  113.     xsendline(ZDLE); xsendline(frameend);
  114.     crc = UPDC32(frameend, crc);
  115.  
  116.     crc = ~crc;
  117.     for (length=4; --length >= 0;) {
  118.         zsendline((int)crc);  crc >>= 8;
  119.     }
  120. }
  121.  
  122.  
  123. /* Receive data subpacket RLE encoded with 32 bit FCS */
  124. zrdatr32(buf, length)
  125. register char *buf;
  126. {
  127.     register int c;
  128. #ifdef m6809
  129.     register long crc;
  130. #else
  131.     register unsigned long crc;
  132. #endif
  133.     register char *end;
  134.     register int d;
  135.  
  136.     crc = 0xFFFFFFFFL;  Rxcount = 0;  end = buf + length;
  137.     d = 0;    /* Use for RLE decoder state */
  138.     while (buf <= end) {
  139.         if ((c = zdlread()) & ~0377) {
  140. crcfoo:
  141.             switch (c) {
  142.             case GOTCRCE:
  143.             case GOTCRCG:
  144.             case GOTCRCQ:
  145.             case GOTCRCW:
  146.                 d = c;  c &= 0377;
  147.                 crc = UPDC32(c, crc);
  148.                 if ((c = zdlread()) & ~0377)
  149.                     goto crcfoo;
  150.                 crc = UPDC32(c, crc);
  151.                 if ((c = zdlread()) & ~0377)
  152.                     goto crcfoo;
  153.                 crc = UPDC32(c, crc);
  154.                 if ((c = zdlread()) & ~0377)
  155.                     goto crcfoo;
  156.                 crc = UPDC32(c, crc);
  157.                 if ((c = zdlread()) & ~0377)
  158.                     goto crcfoo;
  159.                 crc = UPDC32(c, crc);
  160.                 if (crc != 0xDEBB20E3) {
  161.                     zperr(badcrc);
  162.                     return ERROR;
  163.                 }
  164.                 Rxcount = length - (end - buf);
  165. #ifndef DSZ
  166.                 vfile("zrdatr32: %d %s", Rxcount,
  167.                   Zendnames[d-GOTCRCE&3]);
  168. #endif
  169.                 return d;
  170.             case GOTCAN:
  171.                 zperr("Sender Canceled");
  172.                 return ZCAN;
  173.             case TIMEOUT:
  174.                 zperr("TIMEOUT");
  175.                 return c;
  176.             default:
  177.                 zperr("Bad data subpacket");
  178.                 return c;
  179.             }
  180.         }
  181.         crc = UPDC32(c, crc);
  182.         switch (d) {
  183.         case 0:
  184.             if (c == ZRESC) {
  185.                 d = -1;  continue;
  186.             }
  187.             *buf++ = c;  continue;
  188.         case -1:
  189.             if (c >= 040 && c < 0100) {
  190.                 d = c - 035; c = 040;  goto spaces;
  191.             }
  192.             if (c == 0100) {
  193.                 d = 0;
  194.                 *buf++ = ZRESC;  continue;
  195.             }
  196.             d = c;  continue;
  197.         default:
  198.             d -= 0100;
  199.             if (d < 1)
  200.                 goto badpkt;
  201. spaces:
  202.             if ((buf + d) > end)
  203.                 goto badpkt;
  204.             while ( --d >= 0)
  205.                 *buf++ = c;
  206.             d = 0;  continue;
  207.         }
  208.     }
  209. badpkt:
  210.     zperr("Data subpacket too long");
  211.     return ERROR;
  212. }
  213.  
  214. /* End of zmr.c */
  215.