From: Guy Harris Date: Sun, 2 Feb 2014 23:17:06 +0000 (-0800) Subject: Do our own isascii(), isprint(), isgraph(), and toascii(). X-Git-Tag: tcpdump-4.6.0~239 X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/3454732513abdbd1490c6107a94c6474f71a74d2 Do our own isascii(), isprint(), isgraph(), and toascii(). We do *not* want the behavior of isprint() and isgraph() to be locale-dependent - we want both of them to return "true" only for ASCII characters. We have to do our own isascii() and toascii() on non-UN*X systems anyway, so let's just do all of them ourselves. --- diff --git a/netdissect.h b/netdissect.h index b4c24e64..bdf2ebdb 100644 --- a/netdissect.h +++ b/netdissect.h @@ -267,6 +267,17 @@ extern char *read_infile(netdissect_options *, char *); extern char *copy_argv(netdissect_options *, char **); #endif +/* + * Locale-independent macros for testing character properties and + * stripping the 8th bit from characters. Assumed to be handed + * a value between 0 and 255, i.e. don't hand them a char, as + * those might be in the range -128 to 127. + */ +#define ND_ISASCII(c) (!((c) & 0x80)) /* value is an ASCII code point */ +#define ND_ISPRINT(c) ((c) >= 0x20 && (c) <= 0x7E) +#define ND_ISGRAPH(c) ((c) > 0x20 && (c) <= 0x7E) +#define ND_TOASCII(c) ((c) & 0x7F) + extern void safeputchar(int); extern void safeputs(const char *, int); diff --git a/parsenfsfh.c b/parsenfsfh.c index 923b063a..6240e0df 100644 --- a/parsenfsfh.c +++ b/parsenfsfh.c @@ -452,7 +452,7 @@ const unsigned char *fhp; int seen_null = 0; for (i = 1; i < 14; i++) { - if (isprint(fhp[i])) { + if (ND_ISPRINT(fhp[i])) { if (seen_null) return(0); else diff --git a/print-ascii.c b/print-ascii.c index 9315edd2..3ff8887b 100644 --- a/print-ascii.c +++ b/print-ascii.c @@ -62,7 +62,7 @@ ascii_print(register const u_char *cp, register u_int length) while (length > 0) { s = *cp++; length--; - if (!isgraph(s) && + if (!ND_ISGRAPH(s) && (s != '\t' && s != ' ' && s != '\n' && s != '\r')) putchar('.'); else @@ -89,8 +89,8 @@ hex_and_ascii_print_with_offset(register const char *ident, (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff), " %02x%02x", s1, s2); hsp += HEXDUMP_HEXSTUFF_PER_SHORT; - *(asp++) = (isgraph(s1) ? s1 : '.'); - *(asp++) = (isgraph(s2) ? s2 : '.'); + *(asp++) = (ND_ISGRAPH(s1) ? s1 : '.'); + *(asp++) = (ND_ISGRAPH(s2) ? s2 : '.'); i++; if (i >= HEXDUMP_SHORTS_PER_LINE) { *hsp = *asp = '\0'; @@ -106,7 +106,7 @@ hex_and_ascii_print_with_offset(register const char *ident, (void)snprintf(hsp, sizeof(hexstuff) - (hsp - hexstuff), " %02x", s1); hsp += 3; - *(asp++) = (isgraph(s1) ? s1 : '.'); + *(asp++) = (ND_ISGRAPH(s1) ? s1 : '.'); ++i; } if (i > 0) { diff --git a/print-isakmp.c b/print-isakmp.c index a4014364..bb85c475 100644 --- a/print-isakmp.c +++ b/print-isakmp.c @@ -2110,7 +2110,7 @@ ikev2_ID_print(netdissect_options *ndo, u_char tpay, if(dumpascii) { ND_TCHECK2(*typedata, idtype_len); for(i=0; indo_vflag && 4 < len) { diff --git a/print-krb.c b/print-krb.c index d2c81239..ba61a3a0 100644 --- a/print-krb.c +++ b/print-krb.c @@ -107,12 +107,12 @@ c_print(register const u_char *s, register const u_char *ep) flag = 0; break; } - if (!isascii(c)) { - c = toascii(c); + if (!ND_ISASCII(c)) { + c = ND_TOASCII(c); putchar('M'); putchar('-'); } - if (!isprint(c)) { + if (!ND_ISPRINT(c)) { c ^= 0x40; /* DEL to ?, others to alpha */ putchar('^'); } diff --git a/print-rip.c b/print-rip.c index dbda1462..b1931a63 100644 --- a/print-rip.c +++ b/print-rip.c @@ -135,7 +135,7 @@ rip_entry_print_v2(register const struct rip_netinfo *ni, const unsigned remaini u_int i = 0; printf("\n\t Simple Text Authentication data: "); for (; i < RIP_AUTHLEN; p++, i++) - putchar (isprint(*p) ? *p : '.'); + putchar (ND_ISPRINT(*p) ? *p : '.'); } else if (auth_type == 3) { printf("\n\t Auth header:"); printf(" Packet Len %u,", EXTRACT_16BITS((u_int8_t *)ni + 4)); diff --git a/print-snmp.c b/print-snmp.c index 4acc5b28..0724c0b0 100644 --- a/print-snmp.c +++ b/print-snmp.c @@ -779,7 +779,7 @@ asn1_print(struct be *elem) const u_char *p = elem->data.str; TCHECK2(*p, asnlen); for (i = asnlen; printable && i-- > 0; p++) - printable = isprint(*p) || isspace(*p); + printable = ND_ISPRINT(*p); p = elem->data.str; if (printable) { putchar('"'); diff --git a/smbutil.c b/smbutil.c index 13333d00..43783aa4 100644 --- a/smbutil.c +++ b/smbutil.c @@ -329,7 +329,7 @@ write_bits(unsigned int val, const char *fmt) } } -/* convert a UCS2 string into iso-8859-1 string */ +/* convert a UCS-2 string into an ASCII string */ #define MAX_UNISTR_SIZE 1000 static const char * unistr(const u_char *s, u_int32_t *len, int use_unicode) @@ -384,7 +384,7 @@ unistr(const u_char *s, u_int32_t *len, int use_unicode) TCHECK(s[0]); if (l >= MAX_UNISTR_SIZE) break; - if (isprint(s[0])) + if (ND_ISPRINT(s[0])) buf[l] = s[0]; else { if (s[0] == 0) @@ -400,7 +400,7 @@ unistr(const u_char *s, u_int32_t *len, int use_unicode) TCHECK2(s[0], 2); if (l >= MAX_UNISTR_SIZE) break; - if (s[1] == 0 && isprint(s[0])) { + if (s[1] == 0 && ND_ISPRINT(s[0])) { /* It's a printable ASCII character */ buf[l] = s[0]; } else { diff --git a/tcpdump-stdinc.h b/tcpdump-stdinc.h index dde6c507..77dccd5c 100644 --- a/tcpdump-stdinc.h +++ b/tcpdump-stdinc.h @@ -57,9 +57,6 @@ #endif #if !defined(__MINGW32__) && !defined(__WATCOMC__) -#undef toascii -#define isascii __isascii -#define toascii __toascii #define stat _stat #define open _open #define fstat _fstat @@ -90,10 +87,6 @@ extern int inet_aton (const char *cp, struct in_addr *addr); #define INET6_ADDRSTRLEN 46 #endif -#ifndef toascii -#define toascii(c) ((c) & 0x7f) -#endif - #ifndef caddr_t typedef char* caddr_t; #endif /* caddr_t */ diff --git a/util.c b/util.c index c5892717..17bcd9f8 100644 --- a/util.c +++ b/util.c @@ -59,12 +59,12 @@ fn_print(register const u_char *s, register const u_char *ep) ret = 0; break; } - if (!isascii(c)) { - c = toascii(c); + if (!ND_ISASCII(c)) { + c = ND_TOASCII(c); putchar('M'); putchar('-'); } - if (!isprint(c)) { + if (!ND_ISPRINT(c)) { c ^= 0x40; /* DEL to ?, others to alpha */ putchar('^'); } @@ -87,12 +87,12 @@ fn_printn(register const u_char *s, register u_int n, while (n > 0 && (ep == NULL || s < ep)) { n--; c = *s++; - if (!isascii(c)) { - c = toascii(c); + if (!ND_ISASCII(c)) { + c = ND_TOASCII(c); putchar('M'); putchar('-'); } - if (!isprint(c)) { + if (!ND_ISPRINT(c)) { c ^= 0x40; /* DEL to ?, others to alpha */ putchar('^'); } @@ -121,12 +121,12 @@ fn_printzp(register const u_char *s, register u_int n, ret = 0; break; } - if (!isascii(c)) { - c = toascii(c); + if (!ND_ISASCII(c)) { + c = ND_TOASCII(c); putchar('M'); putchar('-'); } - if (!isprint(c)) { + if (!ND_ISPRINT(c)) { c ^= 0x40; /* DEL to ?, others to alpha */ putchar('^'); } @@ -596,7 +596,7 @@ safeputchar(int c) unsigned char ch; ch = (unsigned char)(c & 0xff); - if (ch < 0x80 && isprint(ch)) + if (ch < 0x80 && ND_ISPRINT(ch)) printf("%c", ch); else printf("\\0x%02x", ch);