Line data Source code
1 : /*
2 : Unix SMB/CIFS implementation.
3 : Samba utility functions
4 :
5 : Copyright (C) Andrew Tridgell 1992-2001
6 : Copyright (C) Simo Sorce 2001-2002
7 : Copyright (C) Martin Pool 2003
8 : Copyright (C) James Peach 2005
9 :
10 : This program is free software; you can redistribute it and/or modify
11 : it under the terms of the GNU General Public License as published by
12 : the Free Software Foundation; either version 3 of the License, or
13 : (at your option) any later version.
14 :
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 :
20 : You should have received a copy of the GNU General Public License
21 : along with this program. If not, see <http://www.gnu.org/licenses/>.
22 : */
23 :
24 : #include "includes.h"
25 :
26 : /**
27 : Do a case-insensitive, whitespace-ignoring ASCII string compare.
28 : **/
29 5542977040 : _PUBLIC_ int strwicmp(const char *psz1, const char *psz2)
30 : {
31 : /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
32 : /* appropriate value. */
33 5542977040 : if (psz1 == psz2)
34 3028 : return (0);
35 5542839144 : else if (psz1 == NULL)
36 0 : return (-1);
37 5542839144 : else if (psz2 == NULL)
38 0 : return (1);
39 :
40 : /* sync the strings on first non-whitespace */
41 : while (1) {
42 12574950532 : while (isspace((int)*psz1))
43 36117500 : psz1++;
44 11896982150 : while (isspace((int)*psz2))
45 33826921 : psz2++;
46 :
47 : /*
48 : * This does not do a genuine multi-byte comparison,
49 : * instead it just uses the fast-path for ASCII in
50 : * these common routines
51 : */
52 6302384459 : if (toupper_m((unsigned char)*psz1) != toupper_m((unsigned char)*psz2)
53 779815935 : || *psz1 == '\0'
54 759545315 : || *psz2 == '\0')
55 : break;
56 759545315 : psz1++;
57 759545315 : psz2++;
58 : }
59 5542839144 : return (*psz1 - *psz2);
60 : }
61 :
62 : /**
63 : String replace.
64 : NOTE: oldc and newc must be 7 bit characters
65 : **/
66 70671 : void string_replace( char *s, char oldc, char newc )
67 : {
68 2041754 : while (*s != '\0') {
69 : size_t c_size;
70 1904899 : next_codepoint(s, &c_size);
71 :
72 1904899 : if (c_size == 1) {
73 1904899 : if (*s == oldc) {
74 109739 : *s = newc;
75 : }
76 : }
77 1904899 : s += c_size;
78 : }
79 70671 : }
80 :
81 :
82 : /**
83 : Paranoid strcpy into a buffer of given length (includes terminating
84 : zero. Strips out all but 'a-Z0-9' and the character in other_safe_chars
85 : and replaces with '_'. Deliberately does *NOT* check for multibyte
86 : characters. Treats src as an array of bytes, not as a multibyte
87 : string. Any byte >0x7f is automatically converted to '_'.
88 : other_safe_chars must also contain an ascii string (bytes<0x7f).
89 : **/
90 :
91 94990 : char *alpha_strcpy(char *dest,
92 : const char *src,
93 : const char *other_safe_chars,
94 : size_t maxlength)
95 : {
96 : size_t len, i;
97 :
98 94990 : if (!dest) {
99 0 : smb_panic("ERROR: NULL dest in alpha_strcpy");
100 : }
101 :
102 94990 : if (!src) {
103 0 : *dest = 0;
104 0 : return dest;
105 : }
106 :
107 94990 : len = strlen(src);
108 94990 : if (len >= maxlength)
109 0 : len = maxlength - 1;
110 :
111 94990 : if (!other_safe_chars)
112 56 : other_safe_chars = "";
113 :
114 863124 : for(i = 0; i < len; i++) {
115 768134 : int val = (src[i] & 0xff);
116 768134 : if (val > 0x7f) {
117 0 : dest[i] = '_';
118 0 : continue;
119 : }
120 1029809 : if (isupper(val) || islower(val) ||
121 379473 : isdigit(val) || strchr(other_safe_chars, val))
122 766420 : dest[i] = src[i];
123 : else
124 1714 : dest[i] = '_';
125 : }
126 :
127 94990 : dest[i] = '\0';
128 :
129 94990 : return dest;
130 : }
131 :
132 48944 : char *talloc_alpha_strcpy(TALLOC_CTX *mem_ctx,
133 : const char *src,
134 : const char *other_safe_chars)
135 : {
136 48944 : char *dest = NULL;
137 : size_t slen;
138 :
139 48944 : if (src == NULL) {
140 0 : return NULL;
141 : }
142 :
143 48944 : slen = strlen(src);
144 :
145 48944 : dest = talloc_zero_size(mem_ctx, slen + 1);
146 48944 : if (dest == NULL) {
147 0 : return NULL;
148 : }
149 :
150 48944 : alpha_strcpy(dest, src, other_safe_chars, slen + 1);
151 48944 : return dest;
152 : }
|