棘!如何实现com口用zmodem协议进行文件传输?(100分)

  • 主题发起人 主题发起人 cctt
  • 开始时间 开始时间
C

cctt

Unregistered / Unconfirmed
GUEST, unregistred user!
请列位神经百战的大侠出首指点一二,如哪位有
1.zmodem 协议
2.带源码控件
请email cctt.@263.net
最后向历次人民解放战争中奋勇争先的各路大侠致以虫高的敬礼 $:)
 
老天,这个东西我都发过无数次了,怎么还是有人要?你是哪个学校的?
 
g622,你好!
为了解放全人类,再发一次吧,最好改mail到 longta@up369.com.
俺这旮耷学校是HIT.
 
呵呵,声援cctt,我也需要,g622顺便给我发一份吧:hf2000@263.net
 
我只能发到国内的邮箱
 
g622,你发给我,我再转发给cctt吧,谢谢!!
 
g622,我down了a pro3.1,正在试.你用过那种?结果如何?如有经验可否指点一下?
你知道邮箱是国内的,发吧
 
/*
* Z M O D E M . H Manifest constants for ZMODEM
* application to application file transfer protocol
* 9-06-86 Chuck Forsberg Omen Technology Inc
*/
#define ZPAD '*' /* 052 Padding character begins frames */
#define ZDLE 030 /* Ctrl-X Zmodem escape - `ala BISYNC DLE */
#define ZDLEE (ZDLE^0100) /* Escaped ZDLE as transmitted */
#define ZBIN 'A' /* Binary frame indicator */
#define ZHEX 'B' /* HEX frame indicator */

/* Frame types (see array "frametypes" in zm.c) */
#define ZRQINIT 0 /* Request receive init */
#define ZRINIT 1 /* Receive init */
#define ZSINIT 2 /* Send init sequence (optional) */
#define ZACK 3 /* ACK to above */
#define ZFILE 4 /* File name from sender */
#define ZSKIP 5 /* To sender: skip this file */
#define ZNAK 6 /* Last packet was garbled */
#define ZABORT 7 /* Abort batch transfers */
#define ZFIN 8 /* Finish session */
#define ZRPOS 9 /* Resume data trans at this position */
#define ZDATA 10 /* Data packet(s) follow */
#define ZEOF 11 /* End of file */
#define ZFERR 12 /* Fatal Read or Write error Detected */
#define ZCRC 13 /* Request for file CRC and response */
#define ZCHALLENGE 14 /* Receiver's Challenge */
#define ZCOMPL 15 /* Request is complete */
#define ZCAN 16 /* Other end canned session with CAN*5 */
#define ZFREECNT 17 /* Request for free bytes on filesystem */
#define ZCOMMAND 18 /* Command from sending program */
#define ZSTDERR 19 /* Output to standard error, data follows */

/* ZDLE sequences */
#define ZCRCE 'h' /* CRC next, frame ends, header packet follows */
#define ZCRCG 'i' /* CRC next, frame continues nonstop */
#define ZCRCQ 'j' /* CRC next, frame continues, ZACK expected */
#define ZCRCW 'k' /* CRC next, ZACK expected, end of frame */
#define ZRUB0 'l' /* Translate to rubout 0177 */
#define ZRUB1 'm' /* Translate to rubout 0377 */

/* zdlread return values (internal) */
/* -1 is general error, -2 is timeout */
#define GOTOR 0400
#define GOTCRCE (ZCRCE|GOTOR) /* ZDLE-ZCRCE received */
#define GOTCRCG (ZCRCG|GOTOR) /* ZDLE-ZCRCG received */
#define GOTCRCQ (ZCRCQ|GOTOR) /* ZDLE-ZCRCQ received */
#define GOTCRCW (ZCRCW|GOTOR) /* ZDLE-ZCRCW received */
#define GOTCAN (GOTOR|030) /* CAN*5 seen */

/* Byte positions within header array */
#define ZF0 3 /* First flags byte */
#define ZF1 2
#define ZF2 1
#define ZF3 0
#define ZP0 0 /* Low order 8 bits of position */
#define ZP1 1
#define ZP2 2
#define ZP3 3 /* High order 8 bits of file position */

/* Bit Masks for ZRINIT flags byte ZF0 */
#define CANFDX 01 /* Rx can send and receive true FDX */
#define CANOVIO 02 /* Rx can receive data during disk I/O */
#define CANBRK 04 /* Rx can send a break signal */
#define CANCRY 010 /* Receiver can decrypt */
#define CANLZW 020 /* Receiver can uncompress */

/* Parameters for ZSINIT frame */
#define ZATTNLEN 32 /* Max length of attention string */

/* Parameters for ZFILE frame */
/* Conversion options one of these in ZF0 */
#define ZCBIN 1 /* Binary transfer - inhibit conversion */
#define ZCNL 2 /* Convert NL to local end of line convention */
#define ZCRESUM 3 /* Resume interrupted file transfer */
/* Management options, one of these in ZF1 */
#define ZMNEW 1 /* Transfer if source newer or longer */
#define ZMCRC 2 /* Transfer if different file CRC or length */
#define ZMAPND 3 /* Append contents to existing file (if any) */
#define ZMCLOB 4 /* Replace existing file */
#define ZMSPARS 5 /* Encoding for sparse file */
#define ZMDIFF 6 /* Transfer if dates or lengths different */
#define ZMPROT 7 /* Protect destination file */
/* Transport options, one of these in ZF2 */
#define ZTLZW 1 /* Lempel-Ziv compression */
#define ZTCRYPT 2 /* Encryption */
#define ZTRLE 3 /* Run Length encoding */

/* Parameters for ZCOMMAND frame ZF0 (otherwise 0) */
#define ZCACK1 1 /* Acknowledge, then do command */

long rclhdr();

/* Globals used by ZMODEM functions */
int Rxframeind; /* ZBIN or ZHEX indicates type of frame received */
int Rxtype; /* Type of header received */
int Rxcount; /* Count of data bytes received */
extern Rxtimeout; /* Tenths of seconds to wait for something */
char Rxhdr[4]; /* Received header */
char Txhdr[4]; /* Transmitted header */
long Rxpos; /* Received file position */
long Txpos; /* Transmitted file position */
int Znulls; /* Number of nulls to send at beginning of ZDATA hdr */
char Attn[ZATTNLEN+1]; /* Attention string rx sends to tx on err */
* Z M . C
* ZMODEM protocol primitives
* 8-16-86 Chuck Forsberg Omen Technology Inc
* Entry point Functions:
* zsbhdr(type, hdr) send binary header
* zshhdr(type, hdr) send hex header
* zgethdr(hdr, eflag) receive header - binary or hex
* zsdata(buf, len, frameend) send data
* zrdata(buf, len) receive data
* stohdr(pos) store position data in Txhdr
* long rclhdr(hdr) recover position offset from header

#ifndef CANFDX
#include "zmodem.h"
int Rxtimeout = 100; /* Tenths of seconds to wait for something */
#endif
static char *frametypes[] = {
"Carrier Lost", /* -3 */
"TIMEOUT", /* -2 */
"ERROR", /* -1 */
#define FTOFFSET 3
"ZRQINIT",
"ZRINIT",
"ZSINIT",
"ZACK",
"ZFILE",
"ZSKIP",
"ZNAK",
"ZABORT",
"ZFIN",
"ZRPOS",
"ZDATA",
"ZEOF",
"ZFERR",
"ZCRC",
"ZCHALLENGE",
"ZCOMPL",
"ZCAN",
"ZFREECNT",
"ZCOMMAND",
"ZSTDERR",
"xxxxx"
#define FRTYPES 22 /* Total number of frame types in this array */
/* not including psuedo negative entries */
};

/* Send ZMODEM binary header hdr of type type */
zsbhdr(type, hdr)
register char *hdr;
{
register n;
register unsigned short crc;

vfile("zsbhdr: %s %lx", frametypes[type+FTOFFSET], rclhdr(hdr));
if (type == ZDATA)
for (n = Znulls; --n >=0; )
zsendline(0);

xsendline(ZPAD); xsendline(ZDLE); xsendline(ZBIN);

zsendline(type); crc = updcrc(type, 0);

for (n=4; --n >= 0;) {
zsendline(*hdr);
crc = updcrc((0377& *hdr++), crc);
}
crc = updcrc(0,updcrc(0,crc));
zsendline(crc>>8);
zsendline(crc);
if (type != ZDATA)
flushmo();
}
/* Send ZMODEM HEX header hdr of type type */
zshhdr(type, hdr)
register char *hdr;
{
register n;
register unsigned short crc;

vfile("zshhdr: %s %lx", frametypes[type+FTOFFSET], rclhdr(hdr));
sendline(ZPAD); sendline(ZPAD); sendline(ZDLE); sendline(ZHEX);
zputhex(type);

crc = updcrc(type, 0);
for (n=4; --n >= 0;) {
zputhex(*hdr); crc = updcrc((0377& *hdr++), crc);
}
crc = updcrc(0,updcrc(0,crc));
zputhex(crc>>8); zputhex(crc);

/* Make it printable on remote machine */
sendline(015); sendline(012);
/*
* Uncork the remote in case a fake XOFF has stopped data flow
*/
if (type != ZFIN)
sendline(021);
flushmo();
}
/*
* Send binary array buf of length length, with ending ZDLE sequence frameend
*/
zsdata(buf, length, frameend)
register char *buf;
{
register unsigned short crc;

vfile("zsdata: length=%d end=%x", length, frameend);
crc = 0;
for (;--length >= 0;) {
zsendline(*buf);
crc = updcrc((0377& *buf++), crc);
}
xsendline(ZDLE); xsendline(frameend);
crc = updcrc(frameend, crc);

crc = updcrc(0,updcrc(0,crc));
zsendline(crc>>8);
zsendline(crc);
if (frameend == ZCRCW) {
xsendline(XON); flushmo();
}
}
/*
* Receive array buf of max length with ending ZDLE sequence
* and CRC. Returns the ending character or error code.
*/
zrdata(buf, length)
register char *buf;
{
register c;
register unsigned short crc;
register d;

crc = Rxcount = 0;
for (;;) {
if ((c = zdlread()) & ~0377) {
crcfoo:
switch (c) {
case GOTCRCE:
case GOTCRCG:
case GOTCRCQ:
case GOTCRCW:
crc = updcrc((d=c)&0377, crc);
if ((c = zdlread()) & ~0377)
goto crcfoo;
crc = updcrc(c, crc);
if ((c = zdlread()) & ~0377)
goto crcfoo;
crc = updcrc(c, crc);
if (crc & 0xFFFF) {
zperr("Bad data CRC %x", crc);
return ERROR;
}
vfile("zrdata: cnt = %d ret = %x", Rxcount, d);
return d;
case GOTCAN:
zperr("ZMODEM: Sender Canceled");
return ZCAN;
case TIMEOUT:
zperr("ZMODEM data TIMEOUT");
return c;
default:
zperr("ZMODEM bad data subpacket ret=%x", c);
return c;
}
}
if (--length < 0) {
zperr("ZMODEM data subpacket too long");
return ERROR;
}
++Rxcount;
*buf++ = c;
crc = updcrc(c, crc);
continue;
}
}

/*
* Read a ZMODEM header to hdr, either binary or hex.
* eflag controls local display of non zmodem characters:
* 0: no display
* 1: display printing characters only
* 2: display all non ZMODEM characters
* On success, set Zmodem to 1 and return type of header.
* Otherwise return negative on error
*/
zgethdr(hdr, eflag)
char *hdr;
{
register c, n, cancount;

n = Baudrate; /* Max characters before start of frame */
cancount = 5;
again:
Rxframeind = Rxtype = 0;
switch (c = noxread7()) {
case RCDO:
case TIMEOUT:
goto fifi;
case CAN:
if (--cancount <= 0) {
c = ZCAN; goto fifi;
}
/* **** FALL THRU TO **** */
default:
agn2:
if ( --n == 0) {
zperr("ZMODEM Garbage count exceeded");
return(ERROR);
}
if (eflag &amp;&amp; ((c &amp;= 0177) &amp; 0140))
bttyout(c);
else if (eflag > 1)
bttyout(c);
if (c != CAN)
cancount = 5;
goto again;
case ZPAD: /* This is what we want. */
break;
}
cancount = 5;
splat:
switch (c = noxread7()) {
case ZPAD:
goto splat;
case RCDO:
case TIMEOUT:
goto fifi;
default:
goto agn2;
case ZDLE: /* This is what we want. */
break;
}

switch (c = noxread7()) {
case RCDO:
case TIMEOUT:
goto fifi;
case ZBIN:
Rxframeind = ZBIN;
c = zrbhdr(hdr);
break;
case ZHEX:
Rxframeind = ZHEX;
c = zrhhdr(hdr);
break;
case CAN:
if (--cancount <= 0) {
c = ZCAN; goto fifi;
}
goto agn2;
default:
goto agn2;
}
Rxpos = hdr[ZP3] &amp; 0377;
Rxpos = (Rxpos<<8) + (hdr[ZP2] &amp; 0377);
Rxpos = (Rxpos<<8) + (hdr[ZP1] &amp; 0377);
Rxpos = (Rxpos<<8) + (hdr[ZP0] &amp; 0377);
fifi:
switch (c) {
case GOTCAN:
c = ZCAN;
/* **** FALL THRU TO **** */
case ZNAK:
case ZCAN:
case ERROR:
case TIMEOUT:
case RCDO:
zperr("ZMODEM: Got %s %s", frametypes[c+FTOFFSET],
(c >= 0) ? "header" : "error");
/* **** FALL THRU TO **** */
default:
if (c >= -3 &amp;&amp; c <= FRTYPES)
vfile("zgethdr: %s %lx", frametypes[c+FTOFFSET], Rxpos);
else
vfile("zgethdr: %d %lx", c, Rxpos);
}
return c;
}

/* Receive a binary style header (type and position) */
zrbhdr(hdr)
register char *hdr;
{
register c, n;
register unsigned short crc;

if ((c = zdlread()) &amp; ~0377)
return c;
Rxtype = c;
crc = updcrc(c, 0);

for (n=4; --n >= 0;) {
if ((c = zdlread()) &amp; ~0377)
return c;
crc = updcrc(c, crc);
*hdr++ = c;
}
if ((c = zdlread()) &amp; ~0377)
return c;
crc = updcrc(c, crc);
if ((c = zdlread()) &amp; ~0377)
return c;
crc = updcrc(c, crc);
if (crc &amp; 0xFFFF) {
zperr("Bad Header CRC"); return ERROR;
}
Zmodem = 1;
return Rxtype;
}

/* Receive a hex style header (type and position) */
zrhhdr(hdr)
char *hdr;
{
register c;
register unsigned short crc;
register n;

if ((c = zgethex()) < 0)
return c;
Rxtype = c;
crc = updcrc(c, 0);

for (n=4; --n >= 0;) {
if ((c = zgethex()) < 0)
return c;
crc = updcrc(c, crc);
*hdr++ = c;
}
if ((c = zgethex()) < 0)
return c;
crc = updcrc(c, crc);
if ((c = zgethex()) < 0)
return c;
crc = updcrc(c, crc);
if (crc &amp; 0xFFFF) {
zperr("Bad Header CRC"); return ERROR;
}
if (readline(1) == '/r') /* Throw away possible cr/lf */
readline(1);
Zmodem = 1; return Rxtype;
}

/* Send a byte as two hex digits */
zputhex(c)
register c;
{
static char digits[] = "0123456789abcdef";

if (Verbose>4)
vfile("zputhex: %x", c);
sendline(digits[(c&amp;0xF0)>>4]);
sendline(digits[(c)&amp;0xF]);
}

/*
* Send character c with ZMODEM escape sequence encoding.
* Escape XON, XOFF. Escape CR following @ (Telenet net escape)
*/
zsendline(c)
register c;
{
static lastsent;

switch (c &amp; 0377) {
case ZDLE:
xsendline(ZDLE);
xsendline (lastsent = (c ^= 0100));
break;
case 015:
case 0215:
if ((lastsent &amp; 0177) != '@')
goto sendit;
/* **** FALL THRU TO **** */
case 020:
case 021:
case 023:
case 0220:
case 0221:
case 0223:
#ifdef ZKER
if (Zctlesc<0)
goto sendit;
#endif
xsendline(ZDLE);
c ^= 0100;
sendit:
xsendline(lastsent = c);
break;
default:
#ifdef ZKER
if (Zctlesc>0 &amp;&amp; ! (c &amp; 0140)) {
xsendline(ZDLE);
c ^= 0100;
}
#endif
xsendline(lastsent = c);
}
}

/* Decode two lower case hex digits into an 8 bit byte value */
zgethex()
{
register c;

c = zgeth1();
if (Verbose>4)
vfile("zgethex: %x", c);
return c;
}
zgeth1()
{
register c, n;

if ((c = noxread7()) < 0)
return c;
n = c - '0';
if (n > 9)
n -= ('a' - ':');
if (n &amp; ~0xF)
return ERROR;
if ((c = noxread7()) < 0)
return c;
c -= '0';
if (c > 9)
c -= ('a' - ':');
if (c &amp; ~0xF)
return ERROR;
c += (n<<4);
return c;
}

/*
* Read a byte, checking for ZMODEM escape encoding
* including CAN*5 which represents a quick abort
*/
zdlread()
{
register c;

if ((c = readline(Rxtimeout)) != ZDLE)
return c;
if ((c = readline(Rxtimeout)) < 0)
return c;
if (c == CAN &amp;&amp; (c = readline(Rxtimeout)) < 0)
return c;
if (c == CAN &amp;&amp; (c = readline(Rxtimeout)) < 0)
return c;
if (c == CAN &amp;&amp; (c = readline(Rxtimeout)) < 0)
return c;
switch (c) {
case CAN:
return GOTCAN;
case ZCRCE:
case ZCRCG:
case ZCRCQ:
case ZCRCW:
return (c | GOTOR);
case ZRUB0:
return 0177;
case ZRUB1:
return 0377;
default:
if ((c &amp; 0140) == 0100)
return (c ^ 0100);
break;
}
zperr("Got bad ZMODEM escape sequence %x", c);
return ERROR;
}

/*
* Read a character from the modem line with timeout.
* Eat parity, XON and XOFF characters.
*/
noxread7()
{
register c;

for (;;) {
if ((c = readline(Rxtimeout)) < 0)
return c;
switch (c &amp;= 0177) {
case XON:
case XOFF:
continue;
default:
return c;
}
}
}

/* Store long integer pos in Txhdr */
stohdr(pos)
long pos;
{
Txhdr[ZP0] = pos;
Txhdr[ZP1] = pos>>8;
Txhdr[ZP2] = pos>>16;
Txhdr[ZP3] = pos>>24;
}

/* Recover a long integer from a header */
long
rclhdr(hdr)
register char *hdr;
{
register long l;

l = (hdr[ZP3] &amp; 0377);
l = (l << 8) | (hdr[ZP2] &amp; 0377);
l = (l << 8) | (hdr[ZP1] &amp; 0377);
l = (l << 8) | (hdr[ZP0] &amp; 0377);
return l;
}

 
前面的是h,c文件,文档大致有1000行,我实在放不上来.英文的要不要?
 
感谢 g622 ! c码程序感觉挺精巧简洁的,肯定对我消化pro3.1大有帮助.
我要好好研究一下,弄懂后马上付分.
盈吻也要!要!
 
cc.tt@263.net不是你的邮箱?我早就发了!
 
g622:你好!
出差两天,今天才看到邮件.
借助你的帮助,我已经解决了问题,多谢!
 
后退
顶部