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.
This commit is contained in:
Guy Harris 2014-02-02 15:17:06 -08:00
parent 89e2444c8b
commit 3454732513
10 changed files with 36 additions and 32 deletions

View File

@ -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);

View File

@ -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

View File

@ -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) {

View File

@ -2110,7 +2110,7 @@ ikev2_ID_print(netdissect_options *ndo, u_char tpay,
if(dumpascii) {
ND_TCHECK2(*typedata, idtype_len);
for(i=0; i<idtype_len; i++) {
if(isprint(typedata[i])) {
if(ND_ISPRINT(typedata[i])) {
ND_PRINT((ndo, "%c", typedata[i]));
} else {
ND_PRINT((ndo, "."));
@ -2451,7 +2451,7 @@ ikev2_vid_print(netdissect_options *ndo, u_char tpay,
len = ntohs(e.len) - 4;
ND_TCHECK2(*vid, len);
for(i=0; i<len; i++) {
if(isprint(vid[i])) ND_PRINT((ndo, "%c", vid[i]));
if(ND_ISPRINT(vid[i])) ND_PRINT((ndo, "%c", vid[i]));
else ND_PRINT((ndo, "."));
}
if (2 < ndo->ndo_vflag && 4 < len) {

View File

@ -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('^');
}

View File

@ -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));

View File

@ -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('"');

View File

@ -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 {

View File

@ -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 */

20
util.c
View File

@ -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);