xref: /freebsd/contrib/ntp/sntp/libopts/streqvcmp.c (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
1 
2 /**
3  * \file streqvcmp.c
4  *
5  *  String Equivalence Comparison
6  *
7  *  These routines allow any character to be mapped to any other
8  *  character before comparison.  In processing long option names,
9  *  the characters "-", "_" and "^" all need to be equivalent
10  *  (because they are treated so by different development environments).
11  *
12  * @addtogroup autoopts
13  * @{
14  */
15 /*
16  *  This file is part of AutoOpts, a companion to AutoGen.
17  *  AutoOpts is free software.
18  *  AutoOpts is Copyright (C) 1992-2015 by Bruce Korb - all rights reserved
19  *
20  *  AutoOpts is available under any one of two licenses.  The license
21  *  in use must be one of these two and the choice is under the control
22  *  of the user of the license.
23  *
24  *   The GNU Lesser General Public License, version 3 or later
25  *      See the files "COPYING.lgplv3" and "COPYING.gplv3"
26  *
27  *   The Modified Berkeley Software Distribution License
28  *      See the file "COPYING.mbsd"
29  *
30  *  These files have the following sha256 sums:
31  *
32  *  8584710e9b04216a394078dc156b781d0b47e1729104d666658aecef8ee32e95  COPYING.gplv3
33  *  4379e7444a0e2ce2b12dd6f5a52a27a4d02d39d247901d3285c88cf0d37f477b  COPYING.lgplv3
34  *  13aa749a5b0a454917a944ed8fffc530b784f5ead522b1aacaf4ec8aa55a6239  COPYING.mbsd
35  *
36  * This array is designed for mapping upper and lower case letter
37  * together for a case independent comparison.  The mappings are
38  * based upon ascii character sequences.
39  */
40 static unsigned char charmap[] = {
41     NUL,  0x01, 0x02, 0x03,  0x04, 0x05, 0x06, '\a',
42     '\b', '\t', NL,   '\v',  '\f', '\r', 0x0E, 0x0F,
43     0x10, 0x11, 0x12, 0x13,  0x14, 0x15, 0x16, 0x17,
44     0x18, 0x19, 0x1A, 0x1B,  0x1C, 0x1D, 0x1E, 0x1F,
45 
46     ' ',  '!',  '"',  '#',   '$',  '%',  '&',  '\'',
47     '(',  ')',  '*',  '+',   ',',  '-',  '.',  '/',
48     '0',  '1',  '2',  '3',   '4',  '5',  '6',  '7',
49     '8',  '9',  ':',  ';',   '<',  '=',  '>',  '?',
50 
51     '@',  'a',  'b',  'c',   'd',  'e',  'f',  'g',
52     'h',  'i',  'j',  'k',   'l',  'm',  'n',  'o',
53     'p',  'q',  'r',  's',   't',  'u',  'v',  'w',
54     'x',  'y',  'z',  '[',   '\\', ']',  '^',  '_',
55     '`',  'a',  'b',  'c',   'd',  'e',  'f',  'g',
56     'h',  'i',  'j',  'k',   'l',  'm',  'n',  'o',
57     'p',  'q',  'r',  's',   't',  'u',  'v',  'w',
58     'x',  'y',  'z',  '{',   '|',  '}',  '~',  0x7f,
59 
60     0x80, 0x81, 0x82, 0x83,  0x84, 0x85, 0x86, 0x87,
61     0x88, 0x89, 0x8A, 0x8B,  0x8C, 0x8D, 0x8E, 0x8F,
62     0x90, 0x91, 0x92, 0x93,  0x94, 0x95, 0x96, 0x97,
63     0x98, 0x99, 0x9A, 0x9B,  0x9C, 0x9D, 0x9E, 0x9F,
64     0xA0, 0xA1, 0xA2, 0xA3,  0xA4, 0xA5, 0xA6, 0xA7,
65     0xA8, 0xA9, 0xAA, 0xAB,  0xAC, 0xAD, 0xAE, 0xAF,
66     0xB0, 0xB1, 0xB2, 0xB3,  0xB4, 0xB5, 0xB6, 0xB7,
67     0xB8, 0xB9, 0xBA, 0xBB,  0xBC, 0xBD, 0xBE, 0xBF,
68 
69     0xC0, 0xC1, 0xC2, 0xC3,  0xC4, 0xC5, 0xC6, 0xC7,
70     0xC8, 0xC9, 0xCA, 0xCB,  0xCC, 0xCD, 0xCE, 0xCF,
71     0xD0, 0xD1, 0xD2, 0xD3,  0xD4, 0xD5, 0xD6, 0xD7,
72     0xD8, 0xD9, 0xDA, 0xDB,  0xDC, 0xDD, 0xDE, 0xDF,
73     0xE0, 0xE1, 0xE2, 0xE3,  0xE4, 0xE5, 0xE6, 0xE7,
74     0xE8, 0xE9, 0xEA, 0xEB,  0xEC, 0xED, 0xEE, 0xEF,
75     0xF0, 0xF1, 0xF2, 0xF3,  0xF4, 0xF5, 0xF6, 0xF7,
76     0xF8, 0xF9, 0xFA, 0xFB,  0xFC, 0xFD, 0xFE, 0xFF,
77 };
78 
79 
80 /*=export_func strneqvcmp
81  *
82  * what: compare two strings with an equivalence mapping
83  *
84  * arg:  + char const * + str1 + first string +
85  * arg:  + char const * + str2 + second string +
86  * arg:  + int          + ct   + compare length +
87  *
88  * ret_type:  int
89  * ret_desc:  the difference between two differing characters
90  *
91  * doc:
92  *
93  * Using a character mapping, two strings are compared for "equivalence".
94  * Each input character is mapped to a comparison character and the
95  * mapped-to characters are compared for the two NUL terminated input strings.
96  * The comparison is limited to @code{ct} bytes.
97  * This function name is mapped to option_strneqvcmp so as to not conflict
98  * with the POSIX name space.
99  *
100  * err:  none checked.  Caller responsible for seg faults.
101 =*/
102 int
103 strneqvcmp(char const * s1, char const * s2, int ct)
104 {
105     for (; ct > 0; --ct) {
106         unsigned char u1 = (unsigned char) *s1++;
107         unsigned char u2 = (unsigned char) *s2++;
108         int dif;
109         if (u1 == u2) {
110             if (u1 == NUL)
111                 return 0;
112             continue;
113         }
114 
115         dif = charmap[ u1 ] - charmap[ u2 ];
116 
117         if (dif != 0)
118             return dif;
119 
120         if (u1 == NUL)
121             return 0;
122     }
123 
124     return 0;
125 }
126 
127 
128 /*=export_func streqvcmp
129  *
130  * what: compare two strings with an equivalence mapping
131  *
132  * arg:  + char const * + str1 + first string +
133  * arg:  + char const * + str2 + second string +
134  *
135  * ret_type:  int
136  * ret_desc:  the difference between two differing characters
137  *
138  * doc:
139  *
140  * Using a character mapping, two strings are compared for "equivalence".
141  * Each input character is mapped to a comparison character and the
142  * mapped-to characters are compared for the two NUL terminated input strings.
143  * This function name is mapped to option_streqvcmp so as to not conflict
144  * with the POSIX name space.
145  *
146  * err:  none checked.  Caller responsible for seg faults.
147 =*/
148 int
149 streqvcmp(char const * s1, char const * s2)
150 {
151     for (;;) {
152         unsigned char u1 = (unsigned char) *s1++;
153         unsigned char u2 = (unsigned char) *s2++;
154         int dif;
155         if (u1 == u2) {
156             if (u1 == NUL)
157                 return 0;
158             continue;
159         }
160 
161         dif = charmap[ u1 ] - charmap[ u2 ];
162 
163         if (dif != 0)
164             return dif;
165 
166         if (u1 == NUL)
167             return 0;
168     }
169 }
170 
171 
172 /*=export_func streqvmap
173  *
174  * what: Set the character mappings for the streqv functions
175  *
176  * arg:  + char + from + Input character +
177  * arg:  + char + to   + Mapped-to character +
178  * arg:  + int  + ct   + compare length +
179  *
180  * doc:
181  *
182  * Set the character mapping.  If the count (@code{ct}) is set to zero, then
183  * the map is cleared by setting all entries in the map to their index
184  * value.  Otherwise, the "@code{From}" character is mapped to the "@code{To}"
185  * character.  If @code{ct} is greater than 1, then @code{From} and @code{To}
186  * are incremented and the process repeated until @code{ct} entries have been
187  * set. For example,
188  * @example
189  *    streqvmap('a', 'A', 26);
190  * @end example
191  * @noindent
192  * will alter the mapping so that all English lower case letters
193  * will map to upper case.
194  *
195  * This function name is mapped to option_streqvmap so as to not conflict
196  * with the POSIX name space.
197  *
198  * err:  none.
199 =*/
200 void
201 streqvmap(char from, char to, int ct)
202 {
203     if (ct == 0) {
204         ct = sizeof(charmap) - 1;
205         do  {
206             charmap[ct] = (unsigned char)ct;
207         } while (--ct >= 0);
208     }
209 
210     else {
211         unsigned int i_to   = (int)to   & 0xFF;
212         unsigned int i_from = (int)from & 0xFF;
213 
214         do  {
215             charmap[i_from] = (unsigned char)i_to;
216             i_from++;
217             i_to++;
218             if ((i_from >= sizeof(charmap)) || (i_to >= sizeof(charmap)))
219                 break;
220         } while (--ct > 0);
221     }
222 }
223 
224 
225 /*=export_func strequate
226  *
227  * what: map a list of characters to the same value
228  *
229  * arg:  + char const * + ch_list + characters to equivalence +
230  *
231  * doc:
232  *
233  * Each character in the input string get mapped to the first character
234  * in the string.
235  * This function name is mapped to option_strequate so as to not conflict
236  * with the POSIX name space.
237  *
238  * err:  none.
239 =*/
240 void
241 strequate(char const * s)
242 {
243     if ((s != NULL) && (*s != NUL)) {
244         unsigned char equiv = (unsigned char)*s;
245         while (*s != NUL)
246             charmap[(unsigned char)*(s++)] = equiv;
247     }
248 }
249 
250 
251 /*=export_func strtransform
252  *
253  * what: convert a string into its mapped-to value
254  *
255  * arg:  + char *       + dest + output string +
256  * arg:  + char const * + src  + input string +
257  *
258  * doc:
259  *
260  * Each character in the input string is mapped and the mapped-to
261  * character is put into the output.
262  * This function name is mapped to option_strtransform so as to not conflict
263  * with the POSIX name space.
264  *
265  * The source and destination may be the same.
266  *
267  * err:  none.
268 =*/
269 void
270 strtransform(char * d, char const * s)
271 {
272     do  {
273         *(d++) = (char)charmap[(unsigned char)*s];
274     } while (*(s++) != NUL);
275 }
276 
277 /** @}
278  *
279  * Local Variables:
280  * mode: C
281  * c-file-style: "stroustrup"
282  * indent-tabs-mode: nil
283  * End:
284  * end of autoopts/streqvcmp.c */
285