]> The Tcpdump Group git mirrors - tcpdump/commitdiff
Do case-insensitive comparisons assuming ASCII strings.
authorGuy Harris <[email protected]>
Thu, 11 Jun 2015 22:47:44 +0000 (15:47 -0700)
committerGuy Harris <[email protected]>
Thu, 11 Jun 2015 22:47:44 +0000 (15:47 -0700)
Do the case-insensitive comparisons in a locale-independent fashion that
only maps ASCII letters, in the standard English-language fashion; that
way, we don't get bitten by, for example, Turkish having separate "i
with dot" and "i without dot" letters, with lower-case "i with dot" being
mapped to upper-case "I with dot" rather than being mapped to "I".

16 files changed:
INSTALL.txt
Makefile.in
ascii_strcasecmp.c [moved from strcasecmp.c with 69% similarity]
ascii_strcasecmp.h [new file with mode: 0644]
config.h.in
configure
configure.in
lbl/os-solaris2.h
lbl/os-sunos4.h
lbl/os-ultrix4.h
missing/dlnames.c
print-esp.c
tcpdump.c
util-print.c
win32/prj/WinDump.dsp
win32/prj/WinDump.vcproj

index 2abadd6eaff8b2286a5863ce157dec9f7f2133e2..5aefc458388329d79fb6305ee5b196237c9266f0 100644 (file)
@@ -49,6 +49,8 @@ addrtoname.c  - address to hostname routines
 addrtoname.h   - address to hostname definitions
 ah.h           - IPSEC Authentication Header definitions
 appletalk.h    - AppleTalk definitions
+ascii_strcasecmp.c - locale-independent case-independent string comparison
+               routines
 atime.awk      - TCP ack awk script
 atm.h          - ATM traffic type definitions
 bpf_dump.c     - BPF program printing routines, in case libpcap doesn't
@@ -202,7 +204,6 @@ slcompress.h        - SLIP/PPP Van Jacobson compression (RFC1144) definitions
 smb.h          - SMB/CIFS definitions
 smbutil.c      - SMB/CIFS utility routines
 stime.awk      - TCP send awk script
-strcasecmp.c   - missing routine
 tcp.h          - TCP definitions
 tcpdump.1      - manual entry
 tcpdump.c      - main program
index 70f7085f6d15d9f6ea01f1fade69e420088c7333..7e4f7b1106cc9be9978e9839c93bb2cca8b29ebc 100644 (file)
@@ -75,6 +75,7 @@ CSRC =        setsignal.c tcpdump.c util.c
 LIBNETDISSECT_SRC=\
        addrtoname.c \
        af.c \
+       ascii_strcasecmp.c \
        checksum.c \
        cpack.c \
        gmpls.c \
@@ -237,6 +238,7 @@ HDR = \
        af.h \
        ah.h \
        appletalk.h \
+       ascii_strcasecmp.h \
        atm.h \
        chdlc.h \
        cpack.h \
@@ -346,7 +348,6 @@ EXTRA_DIST = \
        send-ack.awk \
        smbutil.c \
        stime.awk \
-       strcasecmp.c \
        tcpdump.1.in \
        vfprintf.c \
        win32/Include/w32_fzs.h \
similarity index 69%
rename from strcasecmp.c
rename to ascii_strcasecmp.c
index 7ee4e383deca4ae94f61033168f0c25688ea71e1..05e587d716674eaeb1e2dd097978be6f50c26fc1 100644 (file)
  * is provided ``as is'' without express or implied warranty.
  */
 
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
-
-#include <tcpdump-stdinc.h>
-
-#include "interface.h"
+#include "ascii_strcasecmp.h"
 
 /*
- * This array is designed for mapping upper and lower case letter
+ * This array is designed for mapping upper and lower case letters
  * together for a case independent comparison.  The mappings are
- * based upon ascii character sequences.
+ * based upon ASCII character sequences; all values other than
+ * ASCII letters are mapped to themselves, so this is locale-
+ * independent and intended to be locale-independent, to avoid
+ * issues with, for example, "i" and "I" not being lower-case
+ * and upper-case versions of the same letter in Turkish, where
+ * there are separate "i with dot" and "i without dot" letters.
  */
-static const u_char charmap[] = {
+static const unsigned char charmap[] = {
        '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
        '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
        '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
@@ -59,12 +58,12 @@ static const u_char charmap[] = {
 };
 
 int
-strcasecmp(s1, s2)
+ascii_strcasecmp(s1, s2)
        const char *s1, *s2;
 {
-       register const u_char *cm = charmap,
-                       *us1 = (u_char *)s1,
-                       *us2 = (u_char *)s2;
+       register const unsigned char *cm = charmap,
+                       *us1 = (const unsigned char *)s1,
+                       *us2 = (const unsigned char *)s2;
 
        while (cm[*us1] == cm[*us2++])
                if (*us1++ == '\0')
@@ -73,16 +72,39 @@ strcasecmp(s1, s2)
 }
 
 int
-strncasecmp(s1, s2, n)
+ascii_strncasecmp(s1, s2, n)
        const char *s1, *s2;
-       register int n;
+       register size_t n;
 {
-       register const u_char *cm = charmap,
-                       *us1 = (u_char *)s1,
-                       *us2 = (u_char *)s2;
+       register const unsigned char *cm = charmap,
+                       *us1 = (const unsigned char *)s1,
+                       *us2 = (const unsigned char *)s2;
 
-       while (--n >= 0 && cm[*us1] == cm[*us2++])
-               if (*us1++ == '\0')
+       for (;;) {
+               if (n == 0) {
+                       /*
+                        * We've run out of characters that we should
+                        * compare, and they've all been equal; return
+                        * 0, to indicate that the prefixes are the
+                        * same.
+                        */
                        return(0);
-       return(n < 0 ? 0 : cm[*us1] - cm[*--us2]);
+               }
+               if (cm[*us1] != cm[*us2++]) {
+                       /*
+                        * We've found a mismatch.
+                        */
+                       break;
+               }
+               if (*us1++ == '\0') {
+                       /*
+                        * We've run out of characters *to* compare,
+                        * and they've all been equal; return 0, to
+                        * indicate that the strings are the same.
+                        */
+                       return(0);
+               }
+               n--;
+       }
+       return(cm[*us1] - cm[*--us2]);
 }
diff --git a/ascii_strcasecmp.h b/ascii_strcasecmp.h
new file mode 100644 (file)
index 0000000..0d42c59
--- /dev/null
@@ -0,0 +1,33 @@
+/*
+ * Copyright (c) 1988-1997
+ *     The Regents of the University of California.  All rights reserved.
+ *
+ * Copyright (c) 1998-2012  Michael Richardson <[email protected]>
+ *      The TCPDUMP project
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that: (1) source code distributions
+ * retain the above copyright notice and this paragraph in its entirety, (2)
+ * distributions including binary code include the above copyright notice and
+ * this paragraph in its entirety in the documentation or other materials
+ * provided with the distribution, and (3) all advertising materials mentioning
+ * features or use of this software display the following acknowledgement:
+ * ``This product includes software developed by the University of California,
+ * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
+ * the University nor the names of its contributors may be used to endorse
+ * or promote products derived from this software without specific prior
+ * written permission.
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ */
+
+#ifndef tcpdump_ascii_strcasecmp_h
+#define tcpdump_ascii_strcasecmp_h
+
+#include <stddef.h>
+
+extern int ascii_strcasecmp(const char *, const char *);
+extern int ascii_strncasecmp(const char *, const char *, size_t);
+
+#endif /* tcpdump_ascii_strcasecmp_h */
index fa38d391189a25472e805ef19d13c2fd3c656a50..ee3c1fec10ff965e5fc2fd76c910e01524c52cd6 100644 (file)
 /* Define to 1 if you have the <stdlib.h> header file. */
 #undef HAVE_STDLIB_H
 
-/* Define to 1 if you have the `strcasecmp' function. */
-#undef HAVE_STRCASECMP
-
 /* Define to 1 if you have the `strdup' function. */
 #undef HAVE_STRDUP
 
index 7f67725c3c2c52d4520fa48b2ac29cee04ca51e4..2b0e1b549239c276820f807f5617002ee300d5c2 100755 (executable)
--- a/configure
+++ b/configure
@@ -5447,19 +5447,6 @@ esac
 
 fi
 
-ac_fn_c_check_func "$LINENO" "strcasecmp" "ac_cv_func_strcasecmp"
-if test "x$ac_cv_func_strcasecmp" = xyes; then :
-  $as_echo "#define HAVE_STRCASECMP 1" >>confdefs.h
-
-else
-  case " $LIBOBJS " in
-  *" strcasecmp.$ac_objext "* ) ;;
-  *) LIBOBJS="$LIBOBJS strcasecmp.$ac_objext"
- ;;
-esac
-
-fi
-
 ac_fn_c_check_func "$LINENO" "strlcat" "ac_cv_func_strlcat"
 if test "x$ac_cv_func_strlcat" = xyes; then :
   $as_echo "#define HAVE_STRLCAT 1" >>confdefs.h
index 7ee957de8cfb9d25245997452e7d2741a59990c5..0640f5a8a5cdfd92ddb251ce534999aeae72e1cf 100644 (file)
@@ -573,7 +573,7 @@ if test "$ac_cv_namereqd" = no; then
        missing_includes=yes
 fi
 
-AC_REPLACE_FUNCS(vfprintf strcasecmp strlcat strlcpy strdup strsep getopt_long)
+AC_REPLACE_FUNCS(vfprintf strlcat strlcpy strdup strsep getopt_long)
 AC_CHECK_FUNCS(fork vfork strftime)
 AC_CHECK_FUNCS(setlinebuf alarm)
 
index 9f94da97658f3744bbe1f3319e38b3ee3f76b6a8..aedba4ef3c7a363f31d6c52397feedaf485c6777 100644 (file)
@@ -25,4 +25,3 @@ int   setlinebuf(FILE *);
 #endif
 char    *strerror(int);
 int    snprintf(char *, size_t, const char *, ...);
-int    strcasecmp(const char *, const char *);
index b73585706cd55efef0427c066d1d65eb9f7bca30..0e63270b789bd413ad7f8c1ff9f4dade04982e21 100644 (file)
@@ -166,12 +166,10 @@ int       sscanf(char *, const char *, ...);
 int    stat(const char *, struct stat *);
 int    statfs(char *, struct statfs *);
 char   *strerror(int);
-int    strcasecmp(const char *, const char *);
 #ifdef __STDC__
 struct tm;
 #endif
 int    strftime(char *, int, char *, struct tm *);
-int    strncasecmp(const char *, const char *, int);
 long   strtol(const char *, char **, int);
 void   sync(void);
 void   syslog(int, const char *, ...);
index fa1f770f8d440bb9a04aac2bd04bf5212b5fde5a..891def2b39f65d9159039c7d67cd0cb400fd3160 100644 (file)
@@ -34,4 +34,3 @@ int   ioctl(int, int, caddr_t);
 int    pfopen(char *, int);
 int    setlinebuf(FILE *);
 int    socket(int, int, int);
-int    strcasecmp(const char *, const char *);
index a10cd398d8936cbf3edb85459a50f3255b83aaa2..c1795b3dca37e96b63c7616e3125d3c27653c5b9 100644 (file)
@@ -41,6 +41,7 @@
 #include <string.h>
 
 #include "pcap-missing.h"
+#include "ascii_strcasecmp.h"
 
 struct dlt_choice {
        const char *name;
@@ -137,7 +138,7 @@ pcap_datalink_name_to_val(const char *name)
        int i;
 
        for (i = 0; dlt_choices[i].name != NULL; i++) {
-               if (strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
+               if (ascii_strcasecmp(dlt_choices[i].name + sizeof("DLT_") - 1,
                    name) == 0)
                        return (dlt_choices[i].dlt);
        }
index 3086928e57935459f7e7aec38bf7796e45045641..bcd6094d8b9e61af3c84f70fdfc8b68a39e504ef 100644 (file)
@@ -50,6 +50,8 @@
 #include "interface.h"
 #include "extract.h"
 
+#include "ascii_strcasecmp.h"
+
 /*
  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
  * All rights reserved.
@@ -331,8 +333,8 @@ espprint_decode_authalgo(netdissect_options *ndo,
        }
        *colon = '\0';
 
-       if(strcasecmp(colon,"sha1") == 0 ||
-          strcasecmp(colon,"md5") == 0) {
+       if(ascii_strcasecmp(colon,"sha1") == 0 ||
+          ascii_strcasecmp(colon,"md5") == 0) {
                sa->authlen = 12;
        }
        return 1;
@@ -426,7 +428,7 @@ static void esp_print_decode_onesecret(netdissect_options *ndo, char *line,
        } else
                decode = line;
 
-       if (spikey && strcasecmp(spikey, "file") == 0) {
+       if (spikey && ascii_strcasecmp(spikey, "file") == 0) {
                /* open file and read it */
                FILE *secretfile;
                char  fileline[1024];
@@ -456,7 +458,7 @@ static void esp_print_decode_onesecret(netdissect_options *ndo, char *line,
                return;
        }
 
-       if (spikey && strcasecmp(spikey, "ikev2") == 0) {
+       if (spikey && ascii_strcasecmp(spikey, "ikev2") == 0) {
                esp_print_decode_ikeline(ndo, line, file, lineno);
                return;
        }
index 0d6cf7af84c476f211587917d0b9825e5e01273e..13becf7ee69a9b31a5614c65b47da91f02a31011 100644 (file)
--- a/tcpdump.c
+++ b/tcpdump.c
@@ -58,7 +58,6 @@ The Regents of the University of California.  All rights reserved.\n";
 
 #ifdef WIN32
 #include "w32_fzs.h"
-extern int strcasecmp (const char *__s1, const char *__s2);
 extern int SIZE_BUF;
 #define off_t long
 #define uint UINT
@@ -121,6 +120,7 @@ extern int SIZE_BUF;
 #include "setsignal.h"
 #include "gmt2local.h"
 #include "pcap-missing.h"
+#include "ascii_strcasecmp.h"
 
 #include "print.h"
 
@@ -996,11 +996,11 @@ main(int argc, char **argv)
 
 #ifdef HAVE_PCAP_SETDIRECTION
                case 'Q':
-                       if (strcasecmp(optarg, "in") == 0)
+                       if (ascii_strcasecmp(optarg, "in") == 0)
                                Qflag = PCAP_D_IN;
-                       else if (strcasecmp(optarg, "out") == 0)
+                       else if (ascii_strcasecmp(optarg, "out") == 0)
                                Qflag = PCAP_D_OUT;
-                       else if (strcasecmp(optarg, "inout") == 0)
+                       else if (ascii_strcasecmp(optarg, "inout") == 0)
                                Qflag = PCAP_D_INOUT;
                        else
                                error("unknown capture direction `%s'", optarg);
@@ -1033,37 +1033,37 @@ main(int argc, char **argv)
                        break;
 
                case 'T':
-                       if (strcasecmp(optarg, "vat") == 0)
+                       if (ascii_strcasecmp(optarg, "vat") == 0)
                                ndo->ndo_packettype = PT_VAT;
-                       else if (strcasecmp(optarg, "wb") == 0)
+                       else if (ascii_strcasecmp(optarg, "wb") == 0)
                                ndo->ndo_packettype = PT_WB;
-                       else if (strcasecmp(optarg, "rpc") == 0)
+                       else if (ascii_strcasecmp(optarg, "rpc") == 0)
                                ndo->ndo_packettype = PT_RPC;
-                       else if (strcasecmp(optarg, "rtp") == 0)
+                       else if (ascii_strcasecmp(optarg, "rtp") == 0)
                                ndo->ndo_packettype = PT_RTP;
-                       else if (strcasecmp(optarg, "rtcp") == 0)
+                       else if (ascii_strcasecmp(optarg, "rtcp") == 0)
                                ndo->ndo_packettype = PT_RTCP;
-                       else if (strcasecmp(optarg, "snmp") == 0)
+                       else if (ascii_strcasecmp(optarg, "snmp") == 0)
                                ndo->ndo_packettype = PT_SNMP;
-                       else if (strcasecmp(optarg, "cnfp") == 0)
+                       else if (ascii_strcasecmp(optarg, "cnfp") == 0)
                                ndo->ndo_packettype = PT_CNFP;
-                       else if (strcasecmp(optarg, "tftp") == 0)
+                       else if (ascii_strcasecmp(optarg, "tftp") == 0)
                                ndo->ndo_packettype = PT_TFTP;
-                       else if (strcasecmp(optarg, "aodv") == 0)
+                       else if (ascii_strcasecmp(optarg, "aodv") == 0)
                                ndo->ndo_packettype = PT_AODV;
-                       else if (strcasecmp(optarg, "carp") == 0)
+                       else if (ascii_strcasecmp(optarg, "carp") == 0)
                                ndo->ndo_packettype = PT_CARP;
-                       else if (strcasecmp(optarg, "radius") == 0)
+                       else if (ascii_strcasecmp(optarg, "radius") == 0)
                                ndo->ndo_packettype = PT_RADIUS;
-                       else if (strcasecmp(optarg, "zmtp1") == 0)
+                       else if (ascii_strcasecmp(optarg, "zmtp1") == 0)
                                ndo->ndo_packettype = PT_ZMTP1;
-                       else if (strcasecmp(optarg, "vxlan") == 0)
+                       else if (ascii_strcasecmp(optarg, "vxlan") == 0)
                                ndo->ndo_packettype = PT_VXLAN;
-                       else if (strcasecmp(optarg, "pgm") == 0)
+                       else if (ascii_strcasecmp(optarg, "pgm") == 0)
                                ndo->ndo_packettype = PT_PGM;
-                       else if (strcasecmp(optarg, "pgm_zmtp1") == 0)
+                       else if (ascii_strcasecmp(optarg, "pgm_zmtp1") == 0)
                                ndo->ndo_packettype = PT_PGM_ZMTP1;
-                       else if (strcasecmp(optarg, "lmp") == 0)
+                       else if (ascii_strcasecmp(optarg, "lmp") == 0)
                                ndo->ndo_packettype = PT_LMP;
                        else
                                error("unknown packet type `%s'", optarg);
index 2c3eb6597feef9ab593cb8c492ed04ac0521c2e6..a46e12c6a6cca0f6fe20a466265c999117ed59b2 100644 (file)
@@ -52,6 +52,7 @@
 #include <string.h>
 
 #include "interface.h"
+#include "ascii_strcasecmp.h"
 
 int32_t thiszone;              /* seconds offset from gmt to local time */
 
@@ -664,7 +665,7 @@ txtproto_print(netdissect_options *ndo, const u_char *pptr, u_int len,
                if (idx != 0) {
                        /* Is this a valid request name? */
                        while ((cmd = *cmds++) != NULL) {
-                               if (strcasecmp((const char *)token, cmd) == 0) {
+                               if (ascii_strcasecmp((const char *)token, cmd) == 0) {
                                        /* Yes. */
                                        is_reqresp = 1;
                                        break;
index acafddd4ca739d11ef39fdad315958af459d096c..5000cb4b7475b24157ee20f6a7c6955c5d098889 100644 (file)
@@ -93,6 +93,10 @@ SOURCE=..\..\af.c
 # End Source File
 # Begin Source File
 
+SOURCE=..\..\ascii_strcasecmp.c
+# End Source File
+# Begin Source File
+
 SOURCE=..\..\bpf_dump.c
 # End Source File
 # Begin Source File
@@ -613,10 +617,6 @@ SOURCE=..\..\smbutil.c
 # End Source File
 # Begin Source File
 
-SOURCE=..\..\strcasecmp.c
-# End Source File
-# Begin Source File
-
 SOURCE=..\..\missing\strlcat.c
 # End Source File
 # Begin Source File
index 3b998868e9258d931d1a76e29e95aeb9478c0df1..13f115a518e49f83f04be703a594ea364768fa70 100644 (file)
                                />
                        </FileConfiguration>
                </File>
+               <File
+                       RelativePath="..\..\ascii_strcasecmp.c"
+                       >
+                       <FileConfiguration
+                               Name="Debug|Win32"
+                               >
+                               <Tool
+                                       Name="VCCLCompilerTool"
+                                       AdditionalIncludeDirectories=""
+                                       PreprocessorDefinitions=""
+                               />
+                       </FileConfiguration>
+                       <FileConfiguration
+                               Name="Release|Win32"
+                               >
+                               <Tool
+                                       Name="VCCLCompilerTool"
+                                       AdditionalIncludeDirectories=""
+                                       PreprocessorDefinitions=""
+                               />
+                       </FileConfiguration>
+               </File>
                <File
                        RelativePath="..\..\bpf_dump.c"
                        >
                                />
                        </FileConfiguration>
                </File>
-               <File
-                       RelativePath="..\..\strcasecmp.c"
-                       >
-                       <FileConfiguration
-                               Name="Debug|Win32"
-                               >
-                               <Tool
-                                       Name="VCCLCompilerTool"
-                                       AdditionalIncludeDirectories=""
-                                       PreprocessorDefinitions=""
-                               />
-                       </FileConfiguration>
-                       <FileConfiguration
-                               Name="Release|Win32"
-                               >
-                               <Tool
-                                       Name="VCCLCompilerTool"
-                                       AdditionalIncludeDirectories=""
-                                       PreprocessorDefinitions=""
-                               />
-                       </FileConfiguration>
-               </File>
                <File
                        RelativePath="..\..\missing\strlcat.c"
                        >