source: vendor/current/lib/util/util_str.c@ 740

Last change on this file since 740 was 740, checked in by Silvan Scherrer, 12 years ago

Samba Server: update vendor to 3.6.0

File size: 7.3 KB
RevLine 
[740]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#include "system/locale.h"
26#undef strncasecmp
27#undef strcasemp
28
29/**
30 * @file
31 * @brief String utilities.
32 **/
33
34/**
35 Safe string copy into a known length string. maxlength does not
36 include the terminating zero.
37**/
38_PUBLIC_ char *safe_strcpy(char *dest,const char *src, size_t maxlength)
39{
40 size_t len;
41
42 if (!dest) {
43 DEBUG(0,("ERROR: NULL dest in safe_strcpy\n"));
44 return NULL;
45 }
46
47#ifdef DEVELOPER
48 /* We intentionally write out at the extremity of the destination
49 * string. If the destination is too short (e.g. pstrcpy into mallocd
50 * or fstring) then this should cause an error under a memory
51 * checker. */
52 dest[maxlength] = '\0';
53 if (PTR_DIFF(&len, dest) > 0) { /* check if destination is on the stack, ok if so */
54 log_suspicious_usage("safe_strcpy", src);
55 }
56#endif
57
58 if (!src) {
59 *dest = 0;
60 return dest;
61 }
62
63 len = strlen(src);
64
65 if (len > maxlength) {
66 DEBUG(0,("ERROR: string overflow by %u (%u - %u) in safe_strcpy [%.50s]\n",
67 (unsigned int)(len-maxlength), (unsigned)len, (unsigned)maxlength, src));
68 len = maxlength;
69 }
70
71 memmove(dest, src, len);
72 dest[len] = 0;
73 return dest;
74}
75
76/**
77 Safe string cat into a string. maxlength does not
78 include the terminating zero.
79**/
80_PUBLIC_ char *safe_strcat(char *dest, const char *src, size_t maxlength)
81{
82 size_t src_len, dest_len;
83
84 if (!dest) {
85 DEBUG(0,("ERROR: NULL dest in safe_strcat\n"));
86 return NULL;
87 }
88
89 if (!src)
90 return dest;
91
92#ifdef DEVELOPER
93 if (PTR_DIFF(&src_len, dest) > 0) { /* check if destination is on the stack, ok if so */
94 log_suspicious_usage("safe_strcat", src);
95 }
96#endif
97 src_len = strlen(src);
98 dest_len = strlen(dest);
99
100 if (src_len + dest_len > maxlength) {
101 DEBUG(0,("ERROR: string overflow by %d in safe_strcat [%.50s]\n",
102 (int)(src_len + dest_len - maxlength), src));
103 if (maxlength > dest_len) {
104 memcpy(&dest[dest_len], src, maxlength - dest_len);
105 }
106 dest[maxlength] = 0;
107 return NULL;
108 }
109
110 memcpy(&dest[dest_len], src, src_len);
111 dest[dest_len + src_len] = 0;
112 return dest;
113}
114
115/**
116 format a string into length-prefixed dotted domain format, as used in NBT
117 and in some ADS structures
118**/
119_PUBLIC_ const char *str_format_nbt_domain(TALLOC_CTX *mem_ctx, const char *s)
120{
121 char *ret;
122 int i;
123 if (!s || !*s) {
124 return talloc_strdup(mem_ctx, "");
125 }
126 ret = talloc_array(mem_ctx, char, strlen(s)+2);
127 if (!ret) {
128 return ret;
129 }
130
131 memcpy(ret+1, s, strlen(s)+1);
132 ret[0] = '.';
133
134 for (i=0;ret[i];i++) {
135 if (ret[i] == '.') {
136 char *p = strchr(ret+i+1, '.');
137 if (p) {
138 ret[i] = p-(ret+i+1);
139 } else {
140 ret[i] = strlen(ret+i+1);
141 }
142 }
143 }
144
145 talloc_set_name_const(ret, ret);
146
147 return ret;
148}
149
150/**
151 * Add a string to an array of strings.
152 *
153 * num should be a pointer to an integer that holds the current
154 * number of elements in strings. It will be updated by this function.
155 */
156_PUBLIC_ bool add_string_to_array(TALLOC_CTX *mem_ctx,
157 const char *str, const char ***strings, int *num)
158{
159 char *dup_str = talloc_strdup(mem_ctx, str);
160
161 *strings = talloc_realloc(mem_ctx,
162 *strings,
163 const char *, ((*num)+1));
164
165 if ((*strings == NULL) || (dup_str == NULL))
166 return false;
167
168 (*strings)[*num] = dup_str;
169 *num += 1;
170
171 return true;
172}
173
174/**
175 * Parse a string containing a boolean value.
176 *
177 * val will be set to the read value.
178 *
179 * @retval true if a boolean value was parsed, false otherwise.
180 */
181_PUBLIC_ bool conv_str_bool(const char * str, bool * val)
182{
183 char * end = NULL;
184 long lval;
185
186 if (str == NULL || *str == '\0') {
187 return false;
188 }
189
190 lval = strtol(str, &end, 10 /* base */);
191 if (end == NULL || *end != '\0' || end == str) {
192 return set_boolean(str, val);
193 }
194
195 *val = (lval) ? true : false;
196 return true;
197}
198
199/**
200 * Convert a size specification like 16K into an integral number of bytes.
201 **/
202_PUBLIC_ bool conv_str_size(const char * str, uint64_t * val)
203{
204 char * end = NULL;
205 unsigned long long lval;
206
207 if (str == NULL || *str == '\0') {
208 return false;
209 }
210
211 lval = strtoull(str, &end, 10 /* base */);
212 if (end == NULL || end == str) {
213 return false;
214 }
215
216 if (*end) {
217 if (strwicmp(end, "K") == 0) {
218 lval *= 1024ULL;
219 } else if (strwicmp(end, "M") == 0) {
220 lval *= (1024ULL * 1024ULL);
221 } else if (strwicmp(end, "G") == 0) {
222 lval *= (1024ULL * 1024ULL * 1024ULL);
223 } else if (strwicmp(end, "T") == 0) {
224 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL);
225 } else if (strwicmp(end, "P") == 0) {
226 lval *= (1024ULL * 1024ULL * 1024ULL * 1024ULL * 1024ULL);
227 } else {
228 return false;
229 }
230 }
231
232 *val = (uint64_t)lval;
233 return true;
234}
235
236/**
237 * Parse a uint64_t value from a string
238 *
239 * val will be set to the value read.
240 *
241 * @retval true if parsing was successful, false otherwise
242 */
243_PUBLIC_ bool conv_str_u64(const char * str, uint64_t * val)
244{
245 char * end = NULL;
246 unsigned long long lval;
247
248 if (str == NULL || *str == '\0') {
249 return false;
250 }
251
252 lval = strtoull(str, &end, 10 /* base */);
253 if (end == NULL || *end != '\0' || end == str) {
254 return false;
255 }
256
257 *val = (uint64_t)lval;
258 return true;
259}
260
261/**
262Do a case-insensitive, whitespace-ignoring string compare.
263**/
264_PUBLIC_ int strwicmp(const char *psz1, const char *psz2)
265{
266 /* if BOTH strings are NULL, return TRUE, if ONE is NULL return */
267 /* appropriate value. */
268 if (psz1 == psz2)
269 return (0);
270 else if (psz1 == NULL)
271 return (-1);
272 else if (psz2 == NULL)
273 return (1);
274
275 /* sync the strings on first non-whitespace */
276 while (1) {
277 while (isspace((int)*psz1))
278 psz1++;
279 while (isspace((int)*psz2))
280 psz2++;
281 if (toupper((unsigned char)*psz1) != toupper((unsigned char)*psz2)
282 || *psz1 == '\0'
283 || *psz2 == '\0')
284 break;
285 psz1++;
286 psz2++;
287 }
288 return (*psz1 - *psz2);
289}
290
291/**
292 * Compare 2 strings.
293 *
294 * @note The comparison is case-insensitive.
295 **/
296_PUBLIC_ bool strequal(const char *s1, const char *s2)
297{
298 if (s1 == s2)
299 return true;
300 if (!s1 || !s2)
301 return false;
302
303 return strcasecmp(s1,s2) == 0;
304}
305
306_PUBLIC_ size_t ucs2_align(const void *base_ptr, const void *p, int flags)
307{
308 if (flags & (STR_NOALIGN|STR_ASCII))
309 return 0;
310 return PTR_DIFF(p, base_ptr) & 1;
311}
312
313/**
314 String replace.
315**/
316_PUBLIC_ void string_replace(char *s, char oldc, char newc)
317{
318 while (*s) {
319 if (*s == oldc) *s = newc;
320 s++;
321 }
322}
323
324
Note: See TracBrowser for help on using the repository browser.