]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Do our own isascii(), isprint(), isgraph(), and toascii().
authorGuy Harris <[email protected]>
Sun, 2 Feb 2014 23:17:06 +0000 (15:17 -0800)
committerGuy Harris <[email protected]>
Sun, 2 Feb 2014 23:17:06 +0000 (15:17 -0800)
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.

netdissect.h
parsenfsfh.c
print-ascii.c
print-isakmp.c
print-krb.c
print-rip.c
print-snmp.c
smbutil.c
tcpdump-stdinc.h
util.c

index b4c24e64b7fec6597fec2e72eb2d219d167ba237..bdf2ebdb82f6e647754423af4461c3c3316ed116 100644 (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);
 
index 923b063a5585bce8740e12c1353c839acfc7dbd2..6240e0df7f004c019206d6260d06f14248a6ffaa 100644 (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
index 9315edd2e3278fcd469d371e257d92286a8b0dd4..3ff8887b1f5a09c5c5069e6a100282e14561eae9 100644 (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) {
index a40143649efd94385bd64e31af37f6d1aca85326..bb85c47555ea2e27c79a0d2f6c0c2c0542a6d318 100644 (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) {
index d2c81239e172bb39961d64cf7508115fea079ddf..ba61a3a07cbe5f85f64d597cd533e804dc9c6570 100644 (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('^');
                }
index dbda146268c046d6b4d6e0252cfa53d789afaaed..b1931a6310c4737b21c4409ea9bc6385a9b25a9f 100644 (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));
index 4acc5b287bfe48c58188a97d0600fd0747ababbc..0724c0b01970c52c08e4be3a4ef8e5d318e994b8 100644 (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('"');
index 13333d00e471bb072ed3336b8037f79d1bbd7cc6..43783aa4b0eac8b999bdd87383d95fffb20b7a61 100644 (file)
--- 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 {
index dde6c50785bc4ad3174cf500b5570cfed0e84db7..77dccd5c8a114a7cad0a36e331aed69e8d0da2ed 100644 (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 */
diff --git a/util.c b/util.c
index c58927172f2f693598d4c18f138a8e684dae3639..17bcd9f83560e4c50233c579f566a89aba7bf977 100644 (file)
--- 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);