string.c

来自「ftam等标准协议服务器和客户端的源代码。」· C语言 代码 · 共 1,405 行 · 第 1/2 页

C
1,405
字号
char * cryptstring (str)char * str;{register char * p;	/* This is a SIMPLE HACK to prevent passwords being revealed */	/* at a glance.  It buys virtually no extra security */#define CRYPT_MASK 0x23 	/* could tailor this */	for (p=str; *p != 0; p++)		if (*p != CRYPT_MASK)			*p ^= CRYPT_MASK; 	return (str);}char * cryptparse (str)char * str;{extern char crypt_flag;char * octparse ();	if (crypt_flag) {		crypt_flag = FALSE;  /* recognised it !!! */		return (cryptstring(octparse (str)));   	} else 		return (octparse (str));}sfree (x)char *x;{	free (x);}pstrcmp (a,b)register char * a, *b;{    while (*a == *b) {	if (*a++ == NULL)	    return (0);	b++;    }    if (*a > *b)	return (1);    else	return (-1);}static tpstrcmp (a,b)register char *a, *b;{	if (*a == T61_MARK)		a++;	if (*b == T61_MARK)		b++;    while (*a == *b) {	if (*a++ == NULL)	    return (0);	b++;    }    if (*a > *b)	return (1);    else	return (-1);}static tlexequ (a,b)register char *a, *b;{    /* lexequ with T.61 knowledge */	if (*a == T61_MARK)		a++;	if (*b == T61_MARK)		b++;    while (chrcnv[*a] == chrcnv[*b]) {	if (*a++ == NULL)	    return (0);	b++;    }    if (chrcnv[*a] > chrcnv[*b])	return (1);    else	return (-1);}passwdcmp (a,b)char * a, *b;{	if (strcmp (a,b) == 0)		return (0);	else		return (2);}telcmp (a, b)char   *a,       *b;{    register char c1,		  c2;    for (;;) {	while (c1 = *a++)	    if (c1 != ' ' && c1 != '-')		break;	while (c2 = *b++)	    if (c2 != ' ' && c2 != '-')		break;	if (c1 == NULL)	    return (c2 ? -1 : 0);	else	    if (c2 == NULL)		return 1;	if (c1 > c2)	    return 1;	else	    if (c1 < c2)		return -1;    }}telstrlen( s )char	*s;{	int	len;	for ( len = 0; *s; s++ )		if ( *s != ' ' && *s != '-' )			len++;	return( len );}telncmp (a, b, len)char   *a,       *b;int	len;{    register char c1,		  c2;    for (;;) {	while (c1 = *a++)	    if (c1 != ' ' && c1 != '-')		break;	while (c2 = *b++)	    if (c2 != ' ' && c2 != '-')		break;	if (c1 == NULL)	    return (c2 ? -1 : 0);	else	    if (c2 == NULL)		return 1;	if (c1 > c2)	    return 1;	else	    if (c1 < c2)		return -1;	if (--len == 0)		return(0);    }}strprint (ps,str,format)PS ps;char * str;int format;{	if (*str == T61_MARK) {		if (format != READOUT) {			ps_print (ps,"{T.61}");			octprint (ps,++str,format);		} else if (ch_set == 1) 			iso8859print(ps,++str);		else 			octprint(ps,++str,format);	} else {		if ((*str == ' ') && (*(str+1) == 0)) 			if (format == READOUT)				ps_print (ps," (single space)");			else 				ps_print (ps,"\\20");		else 			ps_print (ps,str);	}}cryptprint (ps,str,format)PS ps;char * str;int format;{char ptr [LINESIZE];extern char dsa_mode;	if (format == READOUT) 		ps_print (ps,"Read but not displayed");	else {		if (! dsa_mode) {			octprint (ps,str,format);			return;		}		ps_print (ps,"{CRYPT}");		(void) strcpy (ptr,str);		octprint (ps,cryptstring(ptr),format);	}}#define MAXLINE		75	/* maximum line length */#define BUFSLOP		5	/* space to allow over run an unexpanded char */r_octprint (ps, qb, format)PS ps;struct qbuf * qb;int format;{    char 	buf[MAXLINE + BUFSLOP];    register	char *str;    register 	char *ptr = buf;    register char	*pend = buf + MAXLINE;    register char	*optr = buf;    register int	cnt;    register struct qbuf *qp;    for (qp = qb->qb_forw; qp != qb; qp = qp->qb_forw) {	for (str = qp->qb_data, cnt = qp->qb_len; cnt > 0; str++, cnt--) {	again:	    if ((isascii(*str)) && (isprint(*str))) {		if (format != READOUT)		switch (*str) {		    case '&': /* used as seperators */		    case '#':		    case '$':		    case '%':		    case '@':#ifdef TIDY_STRING		    case ' ':#endif			ptr = quotechar (*str,ptr);			break;		    default:			*ptr++ = *str;			if (*str == '\\')			    *ptr++ = *str;		}		    else 			*ptr++ = *str;	    } else		ptr = quotechar (*str,ptr);	    if (ptr >= pend) {		if (format != READOUT)		    *optr++ = '\\';	/* Continuation character */		*optr++ = '\n';		(void) ps_write(ps, (PElementData)buf, optr - buf);		optr = ptr = buf;		goto again;	    }	    optr = ptr;	}    }    if (ptr > buf)	(void) ps_write(ps, (PElementData)buf, ptr - buf);}/* ARGSUSED */octprint (ps,str,format)PS ps;char * str;int format;{    		char	buf[MAXLINE + 4];    register	char	*ptr = buf;    register	char	*optr = buf;    register	char	*pend = buf + MAXLINE;    if ((*str == ' ') && (*(str+1) == 0)) {	    ps_print (ps,"\\20");	    return;    }    while (*str) {	if (isascii((*str) & 0xff) && isprint((*str) & 0xff)) {	    if (format != READOUT)		switch (*str) {		    case '&': /* used as seperators */		    case '#':		    case '$':		    case '%':		    case '@':			ptr = quotechar (*str,ptr);			break;		    default:			*ptr++ = *str;			if (*str == '\\')			    *ptr++ = *str;		}	    else 		*ptr++ = *str;	} else		ptr = quotechar (*str,ptr);	if (ptr >= pend) {	    if (format != READOUT) {		*optr++ = '\\';		*optr++ = '\n';	    }	    (void) ps_write (ps, (PElementData)buf, optr - buf);	    ptr = optr = buf;	    continue;	}	optr = ptr;	str++;    }    if (optr > buf)	(void) ps_write (ps, (PElementData)buf, optr - buf);}/* * Compare two possible null qbuf lists and return non zero if they are * different * Pretty complex to allow for all sorts of weird cases * Doesn't work for a qbuf which doesn't have a head ! Don't really know what * is the proper form of a queue buf. Marshall's doco doesn't say */qb_cmp(qb1, qb2)struct	qbuf	*qb1, *qb2;{    struct	qbuf	*qp1, *qp2;    register char	*po1, *po2;    register int	len1, len2;    register int	i;    if (qb1 == NULL && qb2 == NULL)	return (0);        if (qb1 == NULL)	return (-1);	    if (qb2 == NULL)	return (1);    qp1 = qb1->qb_forw;    qp2 = qb2->qb_forw;    po1 = qp1->qb_data;    po2 = qp2->qb_data;    len1 = qp1->qb_len;    len2 = qp2->qb_len;    while (qp1 != qb1 && qp2 != qb2) {	if (len1 < len2) {	    if ((i = nbcmp(po1, po2, len1)))		    return (i);	    len2 -= len1;	    po2 += len1;	    qp1 = qp1->qb_forw;	    po1 = qp1->qb_data;	    len1 = qp1->qb_len;	} else {	    if ((i = nbcmp(po1, po2, len2)))		    return (i);	    len1 -= len2;	    po1 += len2;	    qp2 = qp2->qb_forw;	    po2 = qp2->qb_data;	    len2 = qp2->qb_len;	}    }    if (len1 == 0)	    qp1 = qp1->qb_forw;    if (len2 == 0)	    qp2 = qp2->qb_forw;    while (qp1 != qb1 && qp1->qb_len == 0)	    qp1 = qp1->qb_forw;    while (qp2 != qb2 && qp2->qb_len == 0)	    qp2 = qp2->qb_forw;    if (qp1 == qb1 && qp2 == qb2)	return (0);	/* perfect match */    if (qp1 == qb1)	return (-1);    return (1);}/* * new bcmp * actually compares them and returns 1, 0, -1 depending on wether the  * len characters of string1 are greater, equal or less than string2 */nbcmp(string1, string2, len)register char	*string1;register char	*string2;register int 	len;{    while (len-- > 0) {	if (*string1++ == *string2++)	    continue;	if (*--string1 > *--string2)	    return (1);	return (-1);    }    return (0);}#define SIZEOFQB(qb)  (sizeof (struct qbuf) +  (qb && qb->qb_data ? qb->qb_len \				: 0))struct qbuf *qb_cpy(qb)struct qbuf	*qb;{    struct qbuf	*qp;    struct qbuf	*nqb;    struct qbuf	*nqp;    struct qbuf	*pred;    if (qb == (struct qbuf *)0)	return ((struct qbuf *)0);    nqb = (struct qbuf *) smalloc(SIZEOFQB(qb));    nqb->qb_len = qb->qb_len;    if (qb->qb_data) {	nqb->qb_data = nqb->qb_base;	bcopy(qb->qb_data, nqb->qb_data, qb->qb_len);    } else	nqb->qb_data = NULLCP;    nqb->qb_forw = nqb;    nqb->qb_back = nqb;    pred = nqb;    for (qp = qb->qb_forw; qp != qb; qp = qp->qb_forw) {	nqp = (struct qbuf *) smalloc(SIZEOFQB(qp));	nqp->qb_len = qp->qb_len;	if (qp->qb_data) {	    nqp->qb_data = nqp->qb_base;	    bcopy(qp->qb_data, nqp->qb_data, qp->qb_len);	} else	    nqp->qb_data = NULLCP;	insque(nqp, pred);	pred = nqp;    }    return (nqb);}/* * output the string to the PS - including a delimiter on the end */part_print (ps, p, len)PS ps;char	*p;	/* string to be output (may contain nulls) */int	len;	/* number of characters in string */{    char 	buf[MAXLINE + BUFSLOP];    register	char *str;    register 	char *ptr = buf;    register char	*pend = buf + MAXLINE;    register char	*optr = buf;    for (str = p; len > 0; len--, str++) {    again:	if (isascii(*str) && isprint(*str)) {	    switch (*str) {		case '&': /* used as seperators */		case '#':		case '$':		case '%':		case '@':#ifdef TIDY_STRING		case ' ':#endif		    ptr = quotechar (*str,ptr);		    break;		default:		    *ptr++ = *str;		    if (*str == '\\')			*ptr++ = *str;	    }	} else	    ptr = quotechar (*str,ptr);	if (ptr >= pend) {	    *optr++ = '\\';	/* Continuation character */	    *optr++ = '\n';	    (void) ps_write(ps, (PElementData)buf, optr - buf);	    optr = ptr = buf;	    goto again;	}	optr = ptr;    }      /* Add the delimiter */    *ptr++ = '\\';    *ptr++ = 'Z';    if (ptr > buf)	(void) ps_write(ps, (PElementData)buf, ptr - buf);}/* * extract the next string that was written using out_print * note we return the string in a temporary buffer - don't try to free it * it takes the address of a pointer so we can reset the pointer to * where we get up to in the string. It also needs to set the length * as we support binary strings. But we always terminate out strings with * a '\0' for the convience of non binary string users */char *part_parse (pstr, plen)char **pstr;		/* address of pointer to string */int	*plen;		/* address of integer we set the length to */{    static char 	*buf;    static int 		buflen = 0;    register char	*str;    register char	*ptr;    int			left;    int			curlen;    str = *pstr;    left = buflen;    ptr = buf;    for (; *str != 0; str++) {	if ( left <= MINBUF) {            if (buflen <= 0) {                left = buflen = PARSE_INCR;                ptr = buf = smalloc(PARSE_INCR);            } else {                buflen += PARSE_INCR;                left += PARSE_INCR;		curlen = ptr - buf;                buf = srealloc(buf, buflen);                ptr = buf + curlen;            }        }	if (*str != '\\')	    *ptr++ = *str;	else if (str[1] == 'Z') { /* Ahha ! found the delimiter */	    *ptr = '\0';	    str += 2;	    *pstr = str;	    *plen = ptr - buf;	    return (buf);	} else {	    str++;	    str = unquotechar (str,ptr);	    ptr++;	}	left--;    }    *ptr = '\0';	/* for users convience */    *pstr = ++str;    *plen = ptr - buf;    return (buf);}case_exact_match (sntx) short sntx;{	if ((sntx < exct) || (sntx > (exct + 3)))		return (FALSE);	else		return (TRUE);}approx_string (sntx)short sntx;{	if ((sntx < exct) || (sntx > (exct + 7)))		return (FALSE);	else		return (TRUE);}sub_string (sntx)short sntx;{	if ((sntx < exct) || (sntx > (exct + 8)))		return (FALSE);	else		return (TRUE);}telephone_match (sntx) short sntx;{	return( sntx == tel_sntx );}string_syntaxes (){	/* Don't change ordering here unless you know 	   the side effects !!! */	/* 1-4 Exact string */	/* 1-7 Approx       */	exct = add_attribute_syntax ("caseexactstring",		(IFP) strenc,	(IFP) t61dec,		(IFP) t61parse,	strprint,		(IFP) strdup,	tpstrcmp,		sfree,		NULLCP,		soundex_match,	TRUE);	tel_sntx = add_attribute_syntax ("TelephoneNumber",		(IFP) strenc,	(IFP) prtsdec,		(IFP) prtparse,	strprint,		(IFP) strdup,	telcmp,		sfree,		NULLCP,		soundex_match,	TRUE);	(void) add_attribute_syntax ("printablestring",		(IFP) strenc,	(IFP) prtsdec,		(IFP) prtparse,	strprint,		(IFP) strdup,	pstrcmp,		sfree,		NULLCP,		soundex_match,	TRUE);	(void) add_attribute_syntax ("ia5string",		(IFP) ia5enc,	(IFP) ia5sdec,		(IFP) octparse,	octprint,		(IFP) strdup,	pstrcmp,		sfree,		NULLCP,		soundex_match,	TRUE);	/* 5-8 ignore strings */	(void) add_attribute_syntax ("countrystring",		(IFP) strenc,	(IFP) cntydec,		(IFP) cntyparse,strprint,		(IFP) strdup,	lexequ,		sfree,		NULLCP,		soundex_match,	TRUE);	(void) add_attribute_syntax ("DestinationString",		(IFP) strenc,	(IFP) prtsdec,		(IFP) prtparse,	strprint,		(IFP) strdup,	lexequ,		sfree,		NULLCP,		soundex_match,	TRUE);	(void) add_attribute_syntax ("caseignorestring",		(IFP) strenc,	(IFP) t61dec,		(IFP) t61parse,	strprint,		(IFP) strdup,	tlexequ,		sfree,		NULLCP,		soundex_match,	TRUE);	(void) add_attribute_syntax ("caseIgnoreIa5string",		(IFP) ia5enc,	(IFP) ia5sdec,		(IFP) octparse,	octprint,		(IFP) strdup,	lexequ,		sfree,		NULLCP,		soundex_match,	TRUE);	/* 1-9 -> substrings */	(void) add_attribute_syntax ("numericstring",		(IFP) nstrenc,	(IFP) numsdec,		(IFP) strdup,	strprint,		(IFP) strdup,	pstrcmp,		sfree,		NULLCP,		NULLIFP,	FALSE);	/* Not really strings at all (yet!) */	(void) add_attribute_syntax ("octetstring",		(IFP) r_octenc,	(IFP) r_octsdec,		(IFP) r_octparse, r_octprint,		(IFP) qb_cpy,	qb_cmp,		qb_free,		NULLCP,		NULLIFP,	TRUE);	(void) add_attribute_syntax ("password",		(IFP) octenc,	(IFP) octsdec,		(IFP) cryptparse,	cryptprint,		(IFP) strdup,	passwdcmp,		sfree,		NULLCP,		NULLIFP,	TRUE);}

⌨️ 快捷键说明

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