⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zsend.c

📁 zmodem文件传输协议的源码
💻 C
📖 第 1 页 / 共 3 页
字号:



/*--------------------------------------------------------------------------*/
/* ZS SEND BINARY HEADER                                                    */
/* Send ZMODEM binary header hdr of type type                               */
/*--------------------------------------------------------------------------*/
static void pascal ZS_SendBinaryHeader(type, hdr)
   unsigned short type;
   byte          *hdr;
   begin
      register byte          *hptr;
      register unsigned short crc;
      int                     n;

      SENDBYTE(ZPAD);
      SENDBYTE(ZDLE);

      SENDBYTE(ZBIN);
      ZS_SendByte(type);

      crc = Z_UpdateCRC(type, 0);

      hptr  = hdr;
      for (n=4; --n >= 0;)
         begin
            ZS_SendByte(*hptr);
            crc = Z_UpdateCRC(((unsigned short)(*hptr++)), crc);
         end

      crc = Z_UpdateCRC(0,crc);
      crc = Z_UpdateCRC(0,crc);

      ZS_SendByte(crc>>8);
      ZS_SendByte(crc);

      if (type != ZDATA) wait_for_clear();

   end /* ZS_SendBinaryHeader */





/*--------------------------------------------------------------------------*/
/* ZS SEND DATA                                                             */
/* Send binary array buf with ending ZDLE sequence frameend                 */
/*--------------------------------------------------------------------------*/
static void pascal ZS_SendData(buf, length, frameend)
   byte          *buf;
   int            length;
   unsigned short frameend;
   begin

      register unsigned short crc;
      int                     t;

      crc = 0;
      for (;--length >= 0;)
         begin
            ZS_SendByte(*buf);
            crc = Z_UpdateCRC(((unsigned short)(*buf++)), crc);
         end

      SENDBYTE(ZDLE);
      SENDBYTE(frameend);
      crc = Z_UpdateCRC(frameend, crc);

      crc = Z_UpdateCRC(0,crc);
      crc = Z_UpdateCRC(0,crc);
      ZS_SendByte(crc>>8);
      ZS_SendByte(crc);

      if (frameend == ZCRCW)
         begin
            SENDBYTE(XON);
            wait_for_clear();
         end

   end /* ZS_SendData */






/*--------------------------------------------------------------------------*/
/* ZS SEND BYTE                                                             */
/* Send character c with ZMODEM escape sequence encoding.                   */
/* Escape XON, XOFF. Escape CR following @ (Telenet net escape)             */
/*--------------------------------------------------------------------------*/
static void pascal ZS_SendByte(c)
   register byte c;
   begin
      static byte lastsent;

      switch (c)
         begin
            case 015:
            case 0215:  /*--------------------------------------------------*/
                        /*                                                  */
                        /*--------------------------------------------------*/
                        if ((lastsent & 0x7F) != '@') goto SendIt;
            case 020:
            case 021:
            case 023:
            case 0220:
            case 0221:
            case 0223:
            case ZDLE:  /*--------------------------------------------------*/
                        /* Quoted characters                                */
                        /*--------------------------------------------------*/
                        SENDBYTE(ZDLE);
                        c ^= 0x40;

            default:    /*--------------------------------------------------*/
                        /* Normal character output                          */
SendIt:                 /*--------------------------------------------------*/
                        SENDBYTE(lastsent = c);

         end /* switch */

   end /* ZS_SendByte */






/*--------------------------------------------------------------------------*/
/* ZS GET RECEIVER INFO                                                     */
/* Get the receiver's init parameters                                       */
/*--------------------------------------------------------------------------*/
static int pascal ZS_GetReceiverInfo()
   begin
      int   n;
      int   rxflags;

      for (n=10; --n>=0; )
         begin

            switch ( Z_GetHeader(Rxhdr) )
               begin
                  case ZCHALLENGE:  /*--------------------------------------*/
                                    /* Echo receiver's challenge number     */
                                    /*--------------------------------------*/
                                    Z_PutLongIntoHeader(Rxpos);
                                    Z_SendHexHeader(ZACK, Txhdr);
                                    continue;

                  case ZCOMMAND:    /*--------------------------------------*/
                                    /* They didn't see our ZRQINIT          */
                                    /*--------------------------------------*/
                                    Z_PutLongIntoHeader(0L);
                                    Z_SendHexHeader(ZRQINIT, Txhdr);
                                    continue;

                  case ZRINIT:      /*--------------------------------------*/
                                    /*                                      */
                                    /*--------------------------------------*/
                                    Rxbuflen = ((word )Rxhdr[ZP1]<<8)|Rxhdr[ZP0];
                                    return OK;

                  case ZCAN:
                  case RCDO:
                  case TIMEOUT:     /*--------------------------------------*/
                                    /*                                      */
                                    /*--------------------------------------*/
                                    return ERROR;

                  case ZRQINIT:     /*--------------------------------------*/
                                    /*                                      */
                                    /*--------------------------------------*/
                                    if (Rxhdr[ZF0] == ZCOMMAND) continue;

                  default:          /*--------------------------------------*/
                                    /*                                      */
                                    /*--------------------------------------*/
                                    Z_SendHexHeader(ZNAK, Txhdr);
                                    continue;
               end /* switch */

         end /* for */

      return ERROR;

   end /* ZS_GetReceiverInfo */





/*--------------------------------------------------------------------------*/
/* ZS SEND FILE                                                             */
/* Send ZFILE frame and begin sending ZDATA frame                           */
/*--------------------------------------------------------------------------*/
static int pascal ZS_SendFile(blen, wazoo)
   int blen;
   int wazoo;
   begin
      register int c;

      while(1)

         begin

            if (((KEYPRESS()) and (READKB()==27)))
               begin
                  send_can();
                  z_log( KBD_msg );
                  return ERROR;
               end
            else if (!CARRIER) return ERROR;

   
            Txhdr[ZF0] = LZCONV;    /* Default file conversion mode */
            Txhdr[ZF1] = LZMANAG;   /* Default file management mode */
            Txhdr[ZF2] = LZTRANS;   /* Default file transport mode */
            Txhdr[ZF3] = 0;
            ZS_SendBinaryHeader(ZFILE, Txhdr);
            ZS_SendData(Txbuf, blen, ZCRCW);
   
Again:
            switch (c = Z_GetHeader(Rxhdr))
               begin

                  case ZRINIT:   /*-----------------------------------------*/
                                 /*                                         */
                                 /*-----------------------------------------*/
                                 goto Again;

                  case ZCAN:     
                  case ZCRC:     
                  case RCDO:     
                  case TIMEOUT:  
                  case ZFIN:
                  case ZABORT:   
                                 /*-----------------------------------------*/
                                 /*                                         */
                                 /*-----------------------------------------*/
                                 return ERROR;
   
                  case ZSKIP:    /*-----------------------------------------*/
                                 /* Other system wants to skip this file    */
                                 /*-----------------------------------------*/
                                 return c;
   
                  case ZRPOS:    /*-----------------------------------------*/
                                 /* Resend from this position...            */
                                 /*-----------------------------------------*/
                                 fseek(Infile, Rxpos, SEEK_SET);
                                 Strtpos = Txpos = Rxpos;
                                 CLEAR_INBOUND();
                                 return ZS_SendFileData(wazoo);
               end /* switch */

         end /* while */

   end /* ZS_SendFile */




/*--------------------------------------------------------------------------*/
/* ZS SEND FILE DATA                                                        */
/* Send the data in the file                                                */
/*--------------------------------------------------------------------------*/
static int pascal ZS_SendFileData(wazoo)
   int wazoo;
   begin

      register int c, e;
      word         newcnt, blklen, maxblklen, goodblks, goodneeded = 1;
      byte        *p;


      maxblklen = (cur_baud<300) ? 128 : cur_baud/300*256;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -