]> The Tcpdump Group git mirrors - tcpdump/blob - ascii_strcasecmp.c
Add program_name field in the netdissect_options structure
[tcpdump] / ascii_strcasecmp.c
1 /*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that this notice is preserved and that due credit is given
7 * to the University of California at Berkeley. The name of the University
8 * may not be used to endorse or promote products derived from this
9 * software without specific written prior permission. This software
10 * is provided ``as is'' without express or implied warranty.
11 */
12
13 #include "ascii_strcasecmp.h"
14
15 /*
16 * This array maps upper-case ASCII letters to their lower-case
17 * equivalents; all other byte values are mapped to themselves,
18 * so this is locale-independent and intended to be locale-independent,
19 * to avoid issues with, for example, "i" and "I" not being lower-case
20 * and upper-case versions of the same letter in Turkish, where
21 * there are separate "i with dot" and "i without dot" letters.
22 */
23 static const unsigned char charmap[] = {
24 '\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
25 '\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
26 '\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
27 '\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
28 '\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
29 '\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
30 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
31 '\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
32 '\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
33 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
34 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
35 '\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
36 '\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
37 '\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
38 '\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
39 '\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
40 '\200', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
41 '\210', '\211', '\212', '\213', '\214', '\215', '\216', '\217',
42 '\220', '\221', '\222', '\223', '\224', '\225', '\226', '\227',
43 '\230', '\231', '\232', '\233', '\234', '\235', '\236', '\237',
44 '\240', '\241', '\242', '\243', '\244', '\245', '\246', '\247',
45 '\250', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
46 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
47 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
48 '\300', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
49 '\310', '\311', '\312', '\313', '\314', '\315', '\316', '\317',
50 '\320', '\321', '\322', '\323', '\324', '\325', '\326', '\327',
51 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
52 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
53 '\350', '\351', '\352', '\353', '\354', '\355', '\356', '\357',
54 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
55 '\370', '\371', '\372', '\373', '\374', '\375', '\376', '\377',
56 };
57
58 int
59 ascii_strcasecmp(s1, s2)
60 const char *s1, *s2;
61 {
62 register const unsigned char *cm = charmap,
63 *us1 = (const unsigned char *)s1,
64 *us2 = (const unsigned char *)s2;
65
66 while (cm[*us1] == cm[*us2++])
67 if (*us1++ == '\0')
68 return(0);
69 return(cm[*us1] - cm[*--us2]);
70 }
71
72 int
73 ascii_strncasecmp(s1, s2, n)
74 const char *s1, *s2;
75 register size_t n;
76 {
77 register const unsigned char *cm = charmap,
78 *us1 = (const unsigned char *)s1,
79 *us2 = (const unsigned char *)s2;
80
81 for (;;) {
82 if (n == 0) {
83 /*
84 * We've run out of characters that we should
85 * compare, and they've all been equal; return
86 * 0, to indicate that the prefixes are the
87 * same.
88 */
89 return(0);
90 }
91 if (cm[*us1] != cm[*us2++]) {
92 /*
93 * We've found a mismatch.
94 */
95 break;
96 }
97 if (*us1++ == '\0') {
98 /*
99 * We've run out of characters *to* compare,
100 * and they've all been equal; return 0, to
101 * indicate that the strings are the same.
102 */
103 return(0);
104 }
105 n--;
106 }
107 return(cm[*us1] - cm[*--us2]);
108 }