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

📄 ckcfns.c

📁 C-Kermit源码。是使用串口/Modem和网络通讯的程序
💻 C
📖 第 1 页 / 共 5 页
字号:
#ifdef CALIBRATE		if (calibrate) {		    ffc++;		    continue;		}#endif /* CALIBRATE *//* Note: The Unicode and Kanji sections can probably be combined now; *//* the Unicode method (xpnbyte()) covers Kanji too. */#ifdef UNICODE		if (!binary && xlatype == XLA_UNICODE)		  t = xpnbyte((unsigned)((unsigned)a & 0xff),			      tcharset,			      fcharset,			      fn			      );		else#endif /* UNICODE */#ifdef KANJI		if (!binary && tcharset == TC_JEUC &&		    fcharset != FC_JEUC) { /* Translating from J-EUC */		    if (ffc == 0L) xkanjf();		    if (xkanji(a,fn) < 0)  /* to something else? */		      return(-1);		    else t = 1;		} else#endif /* KANJI */		{#ifdef OS2		      if (xflg && !remfile) { /* Write to virtual screen */			  char _a;			  _a = a & fmask;			  t = conoc(_a);			  if (t < 1)			    t = -1;		      } else#endif /* OS2 */			t = zmchout(a & fmask); /* zmchout is a macro */		}		if (t < 0) {		    debug(F101,"decode write errno","",errno);		    return(-1);		}#ifdef UNICODE		if (xlatype != XLA_UNICODE || binary) {		    ffc++;		/* Count the character */		    if (docrc && !xflg && !remfile) { /* Update file CRC */			c = a;		/* Force conversion to unsigned char */			z = crc16 ^ (long)c;			crc16 = (crc16 >> 8) ^			  (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);		    }		}#endif /* UNICODE */	    }	} else {			/* Output to something else. */	    a &= fmask;			/* Apply file mask */	    for (; rpt > 0; rpt--) {	/* Output the char RPT times */#ifdef CALIBRATE		if (calibrate) {		    ffc++;		    continue;		}#endif /* CALIBRATE */		if ((*fn)((char) a) < 0) return(-1); /* Send to output func. */	    }	}#ifdef CK_CTRLZ	lastchar = a;#endif /* CK_CTRLZ */    }    return(0);}/*  G E T P K T -- Fill a packet data field  *//*  Gets characters from the current source -- file or memory string.  Encodes the data into the packet, filling the packet optimally.  Set first = 1 when calling for the first time on a given input stream  (string or file).  Call with:    bufmax -- current send-packet size    xlate  -- flag: 0 to skip character-set translation, 1 to translate  Uses global variables:    t     -- current character.    first -- flag: 1 to start up, 0 for input in progress, -1 for EOF.    next  -- next character (not used any more).    data  -- pointer to the packet data buffer.    size  -- number of characters in the data buffer.    memstr - flag that input is coming from a memory string instead of a file.    memptr - pointer to string in memory.    (*sx)()  character set translation function  Returns:    The size as value of the function, and also sets global "size",    and fills (and null-terminates) the global data array.    Returns:      0 on EOF.     -1 on fatal (internal) error.     -3 on timeout (e.g. when reading data from a pipe).  Rewritten by Paul W. Placeway (PWP) of Ohio State University, March 1989.  Incorporates old getchx() and encode() inline to reduce function calls,  uses buffered input for much-improved efficiency, and clears up some  confusion with line termination (CRLF vs LF vs CR).  Rewritten again by Frank da Cruz to incorporate locking shift mechanism,  May 1991.  And again in 1998 for efficiency, etc, with a separate  bgetpkt() split out for binary-mode no-parity transfers.*//*  Note: Separate Kanji support dates from circa 1991 and now (1999) can most  likely be combined with the the Unicode support: the xgnbyte()/xpnbyte()  mechanism works for both Unicode and Kanji.*/#ifdef KANJIintkgetf(#ifdef CK_ANSIC      VOID#endif /* CK_ANSIC */      ) {    if (funcstr)      return((*funcptr)());    else      return(zminchar());}intkgetm(#ifdef CK_ANSIC      VOID#endif /* CK_ANSIC */      ) {    int x;    if (x = *memptr++) return(x);    else return(-1);}#endif /* KANJI *//*  Lookahead function to decide whether locking shift is worth it.  Looks at  the next four input characters to see if all of their 8th bits match the  argument.  Call with 0 or 0200.  Returns 1 on match, 0 if they don't match.  If we don't happen to have at least 4 more characters waiting in the input  buffer, returns 1.  Note that zinptr points two characters ahead of the  current character because of repeat-count lookahead.*/intlslook(b) unsigned int b; {		/* Locking Shift Lookahead */    int i;    if (zincnt < 3)			/* If not enough chars in buffer, */      return(1);			/* force shift-state switch. */    b &= 0200;				/* Force argument to proper form. */    for (i = -1; i < 3; i++)		/* Look at next 5 characters to */      if (((*(zinptr+i)) & 0200) != b)	/* see if all their 8th bits match.  */	return(0);			/* They don't. */    return(1);				/* They do. */}/* Routine to compute maximum data length for packet to be filled */intmaxdata() {				/* Get maximum data length */    int n, len;    debug(F101,"maxdata spsiz 1","",spsiz);    if (spsiz < 0)			/* How could this happen? */      spsiz = DSPSIZ;    debug(F101,"maxdata spsiz 2","",spsiz);    n = spsiz - 5;			/* Space for Data and Checksum */    if (n > 92 && n < 96) n = 92;	/* "Short" Long packets don't pay */    if (n > 92 && lpcapu == 0)		/* If long packets needed, */      n = 92;				/* make sure they've been negotiated */    len = n - bctl;			/* Space for data */    if (n > 92) len -= 3;		/* Long packet needs header chksum */    debug(F101,"maxdata len 1","",len);    if (len < 0) len = 10;    debug(F101,"maxdata len 2","",len);    return(len);}static CHAR leftover[9] = { '\0','\0','\0','\0','\0','\0','\0','\0','\0' };static int nleft = 0;#ifdef CKTUNING/*  When CKTUNING is defined we use this special trimmed-down version of getpkt  to speed up binary-mode no-parity transfers.  When CKTUNING is not defined,  or for text-mode or parity transfers, we use the regular getpkt() function.  Call just like getpkt() but test first for transfer mode and parity.  NOTE:  This routine is only to be called when sending a real file -- not for  filenames, server responses, etc, because it only reads from the input file.  See getpkt() for more detailed commentary.*/static intbgetpkt(bufmax) int bufmax; {    register CHAR rt = t, rnext;    register CHAR *dp, *odp, *p1, *p2;    register int x = 0, a7;    CHAR xxrc, xxcq;			/* Pieces of prefixed sequence */    long z;				/* A long worker (for CRC) */    if (!binary || parity || memstr)	/* JUST IN CASE caller didn't test */      return(getpkt(bufmax,!binary));    if (!data) {	debug(F100,"SERIOUS ERROR: bgetpkt data == NULL","",0);	return(-1);    }    dp = data;				/* Point to packet data buffer */    size = 0;				/* And initialize its size */    bufmax = maxdata();			/* Get maximum data length */#ifdef DEBUG    if (deblog)      debug(F101,"bgetpkt bufmax","",bufmax);#endif /* DEBUG */    if (first == 1) {			/* If first character of this file.. */	ffc = 0L;			/* reset file character counter */#ifdef COMMENT/* Moved to below */	first = 0;			/* Next character won't be first */#endif /* COMMENT */	*leftover = '\0';		/* Discard any interrupted leftovers */	nleft = 0;	/* Get first character of file into rt, watching out for null file */#ifdef CALIBRATE	if (calibrate) {#ifdef NORANDOM	    rt = 17;#else	    rt = cal_a[rand() & 0xff];#endif /* NORANDOM */	    first = 0;	} else#endif /* CALIBRATE */	if ((x = zminchar()) < 0) {	/* EOF or error */	    if (x == -3) {		/* Timeout. */		size = (dp - data);		debug(F101,"bgetpkt timeout size","",size);		return((size == 0) ? x : size);	    }	    first = -1;	    size = 0;	    if (x == -2) {		/* Error */		debug(F100,"bgetpkt: input error","",0);		cxseen = 1;		/* Interrupt the file transfer */	    } else {		debug(F100,"bgetpkt empty file","",0);	    }	    return(0);	}	first = 0;			/* Next char will not be the first */	ffc++;				/* Count a file character */	rt = (CHAR) x;			/* Convert int to char */	if (docrc && what == W_SEND) {	/* Accumulate file crc */	    z = crc16 ^ (long)rt;	    crc16 = (crc16 >> 8) ^	      (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);	}	rt &= fmask;			/* Apply SET FILE BYTESIZE mask */    } else if (first == -1 && nleft == 0) { /* EOF from last time */        return(size = 0);    }/*  Here we handle characters that were encoded for the last packet but  did not fit, and so were saved in the "leftover" array.*/    if (nleft) {	for (p1 = leftover; nleft > 0; nleft--) /* Copy leftovers */	  *dp++ = *p1++;	*leftover = '\0';		/* Delete leftovers */	nleft = 0;    }    if (first == -1)			/* Handle EOF */      return(size = (dp - data));/* Now fill up the rest of the packet. */    rpt = 0;				/* Initialize character repeat count */    while (first > -1) {		/* Until EOF... */#ifdef CALIBRATE	if (calibrate) {		/* We generate our own "file" */	    if (ffc >= calibrate) {	/* EOF */		first = -1;		ffc--;	    } else {			/* Generate next character */		if (cal_j > CAL_M * ffc)		  cal_j = cal_a[ffc & 0xff];		x = (unsigned)cal_a[(cal_j & 0xff)];		if (x == rt) x ^= 2;	    }	    ffc++;	    cal_j += (unsigned int)(ffc + CAL_O);	} else#endif /* CALIBRATE */	if ((x = zminchar()) < 0) {	/* Check for EOF */	    if (x == -3) {		/* Timeout. */		t = rt;		size = (dp-data);		debug(F101,"bgetpkt timeout size","",size);		return((size == 0) ? x : size);	    }	    first = -1;			/* Flag eof for next time. */	    if (x == -2) cxseen = 1;	/* If error, cancel this file. */	} else {	    ffc++;			/* Count the character */	    if (docrc && what == W_SEND) { /* Accumulate file crc */		z = crc16 ^ (long)((CHAR)x & 0xff);		crc16 = (crc16 >> 8) ^		  (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);	    }	}	rnext = (CHAR) (x & fmask);	/* Apply file mask *//*  At this point, the character we just read is in rnext,  and the character we are about to encode into the packet is in rt.*/	odp = dp;			/* Remember where we started. */	xxrc = xxcq = NUL;		/* Clear these. *//*  Now encode the character according to the options that are in effect:    ctlp[]: whether this control character needs prefixing.    rptflg: repeat counts enabled.    Other options don't apply in this routine.*/	if (rptflg && rt == rnext && first == 0) { /* Got a run... */	    if (++rpt < 94) {		/* Below max, just count */		continue;		/* go back and get another */	    } else if (rpt == 94) {	/* Reached max, must dump */		xxrc = (CHAR) tochar(rpt); /* Put the repeat count here */		rptn += rpt;		/* Accumulate it for statistics */		rpt = 0;		/* And reset it */	    }	} else if (rpt > 0) {		/* End of run */	    xxrc = (CHAR)tochar(++rpt); /* The count */	    rptn += rpt;		/* For stats */	    rpt = 0;			/* Reset repeat count */	}	a7 = rt & 0177;			/* Get low 7 bits of character */	if (#ifdef CK_SPEED	    ctlp[(unsigned)(rt & 0xff)]	/* Lop off any "sign" extension */#else	    (a7 < SP) || (a7 == DEL)#endif /* CK_SPEED */	    ) {				/* Do control prefixing if necessary */	    xxcq = myctlq;		/* The prefix */	    ccp++;			/* Count it */	    rt = (CHAR) ctl(rt);	/* Uncontrollify the character */	}#ifdef CK_SPEED	else if ((a7 < SP) || (a7 == DEL)) /* Count an unprefixed one */	  ccu++;#endif /* CK_SPEED */	if (a7 == myctlq)		/* Always prefix the control prefix */	  xxcq = myctlq;	if ((rptflg) && (a7 == rptq))	/* If it's the repeat prefix, */	  xxcq = myctlq;		/* prefix it if doing repeat counts *//* Now construct the prefixed sequence */	if (xxrc) {			/* Repeat count */	    if (xxrc == (CHAR) '"' && !xxcq) { /* 2 in a row & not prefixed */		*dp++ = rt;		/* So just do this */	    } else {			/* More than two or prefixed */		*dp++ = (CHAR) rptq; *dp++ = xxrc; /* Emit repeat sequence */	    }	}	if (xxcq) { *dp++ = myctlq; }	/* Control prefix */	*dp++ = rt;			/* Finally, the character itself */	rt = rnext;			/* Next character is now current. *//* Done encoding the character.  Now take care of packet buffer overflow. */	size = dp - data;		/* How many bytes we put in buffer. */	if (size >= bufmax) {		/* If too big, save some for next. */	    *dp = '\0';			/* Mark the end. */	    if (size > bufmax) {	/* if packet is overfull */		/* Copy the part that doesn't fit into the leftover buffer, */		/* taking care not to split a prefixed sequence. */		int i;		nleft = dp - odp;		p1 = leftover;		p2 = odp;		for (i = 0; i < nleft; i++)		  *p1++ = *p2++;		size = odp - data;	/* Return truncated packet. */		*odp = '\0';		/* Mark the new end */	    }	    t = rt;			/* Save for next time */	    return(size);	}    }					/* Otherwise, keep filling. */    size = dp - data;			/* End of file */    *dp = '\0';				/* Mark the end of the data. */    return(size);		     /* Return partially filled last packet. */}#endif /* CKTUNING */VOIDdofilcrc(c) int c; {			/* Accumulate file crc */    long z;    z = crc16 ^ (long)c;    crc16 = (crc16 >> 8) ^      (crcta[(z & 0xF0) >> 4] ^ crctb[z & 0x0F]);}/* For SENDing from an array... */intagnbyte() {				/* Get next byte from array */#ifndef NOSPL    char c;

⌨️ 快捷键说明

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