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

  1. /*
  2.  * File: zmr.c 07-30-1989
  3.  * Copyright 1988, 1989 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
  52.  
  53. /* Send data subpacket RLE encoded with 32 bit FCS */
  54. zsdar32(buf, length, frameend)
  55. char *buf;
  56. {
  57.     register int c, l, n;
  58. #ifdef m6809
  59.     register long crc;
  60. #else
  61.     register unsigned long crc;
  62. #endif
  63.  
  64.     crc = 0xFFFFFFFFL;  l = *buf++ & 0377;
  65.     if (length == 1) {
  66.         zsendline(l); crc = UPDC32(l, crc);
  67.         if (l == ZRESC) {
  68.             zsendline(1); crc = UPDC32(1, crc);
  69.         }
  70.     } else {
  71.         for (n = 0; --length >= 0; ++buf) {
  72.             if ((c = *buf & 0377) == l && n < 126 && length>0) {
  73.                 ++n;  continue;
  74.             }
  75.             switch (n) {
  76.             case 0:
  77.                 zsendline(l);
  78.                 crc = UPDC32(l, crc);
  79.                 if (l == ZRESC) {
  80.                     zsendline(0100); crc = UPDC32(0100, crc);
  81.                 }
  82.                 l = c; break;
  83.             case 1:
  84.                 if (l != ZRESC) {
  85.                     zsendline(l); zsendline(l);
  86.                     crc = UPDC32(l, crc);
  87.                     crc = UPDC32(l, crc);
  88.                     n = 0; l = c; break;
  89.                 }
  90.                 /* **** FALL THRU TO **** */
  91.             default:
  92.                 zsendline(ZRESC); crc = UPDC32(ZRESC, crc);
  93.                 if (l == 040 && n < 34) {
  94.                     n += 036;
  95.                     zsendline(n); crc = UPDC32(n, crc);
  96.                 }
  97.                 else {
  98.                     n += 0101;
  99.                     zsendline(n); crc = UPDC32(n, crc);
  100.                     zsendline(l); crc = UPDC32(l, crc);
  101.                 }
  102.                 n = 0; l = c; break;
  103.             }
  104.         }
  105.     }
  106.     xsendline(ZDLE); xsendline(frameend);
  107.     crc = UPDC32(frameend, crc);
  108.  
  109.     crc = ~crc;
  110.     for (length=4; --length >= 0;) {
  111.         zsendline((int)crc);  crc >>= 8;
  112.     }
  113. }
  114.  
  115.  
  116. /* Receive data subpacket RLE encoded with 32 bit FCS */
  117. zrdatr32(buf, length)
  118. register char *buf;
  119. {
  120.     register int c;
  121. #ifdef m6809
  122.     register long crc;
  123. #else
  124.     register unsigned long crc;
  125. #endif
  126.     register char *end;
  127.     register int d;
  128.  
  129.     crc = 0xFFFFFFFFL;  Rxcount = 0;  end = buf + length;
  130.     d = 0;    /* Use for RLE decoder state */
  131.     while (buf <= end) {
  132.         if ((c = zdlread()) & ~0377) {
  133. crcfoo:
  134.             switch (c) {
  135.             case GOTCRCE:
  136.             case GOTCRCG:
  137.             case GOTCRCQ:
  138.             case GOTCRCW:
  139.                 d = c;  c &= 0377;
  140.                 crc = UPDC32(c, crc);
  141.                 if ((c = zdlread()) & ~0377)
  142.                     goto crcfoo;
  143.                 crc = UPDC32(c, crc);
  144.                 if ((c = zdlread()) & ~0377)
  145.                     goto crcfoo;
  146.                 crc = UPDC32(c, crc);
  147.                 if ((c = zdlread()) & ~0377)
  148.                     goto crcfoo;
  149.                 crc = UPDC32(c, crc);
  150.                 if ((c = zdlread()) & ~0377)
  151.                     goto crcfoo;
  152.                 crc = UPDC32(c, crc);
  153.                 if (crc != 0xDEBB20E3) {
  154.                     zperr(badcrc);
  155.                     return ERROR;
  156.                 }
  157.                 Rxcount = length - (end - buf);
  158. #ifndef DSZ
  159.                 vfile("zrdatr32: %d %s", Rxcount,
  160.                   Zendnames[d-GOTCRCE&3]);
  161. #endif
  162.                 return d;
  163.             case GOTCAN:
  164.                 zperr("Sender Canceled");
  165.                 return ZCAN;
  166.             case TIMEOUT:
  167.                 zperr("TIMEOUT");
  168.                 return c;
  169.             default:
  170.                 zperr("Bad data subpacket");
  171.                 return c;
  172.             }
  173.         }
  174.         crc = UPDC32(c, crc);
  175.         switch (d) {
  176.         case 0:
  177.             if (c == ZRESC) {
  178.                 d = -1;  continue;
  179.             }
  180.             *buf++ = c;  continue;
  181.         case -1:
  182.             if (c >= 040 && c < 0100) {
  183.                 d = c - 035; c = 040;  goto spaces;
  184.             }
  185.             if (c == 0100) {
  186.                 d = 0;
  187.                 *buf++ = ZRESC;  continue;
  188.             }
  189.             d = c;  continue;
  190.         default:
  191.             d -= 0100;
  192.             if (d < 1)
  193.                 goto badpkt;
  194. spaces:
  195.             if ((buf + d) > end)
  196.                 goto badpkt;
  197.             while ( --d >= 0)
  198.                 *buf++ = c;
  199.             d = 0;  continue;
  200.         }
  201.     }
  202. badpkt:
  203.     zperr("Data subpacket too long");
  204.     return ERROR;
  205. }
  206.  
  207. /* End of zmr.c */
  208.