summaryrefslogtreecommitdiff
path: root/usr/src/common/smbsrv/smb_string.c
blob: 292f58d4faaf4383d5b10b85dad3452f48e7cf3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifdef _KERNEL
#include <sys/types.h>
#include <sys/sunddi.h>
#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#endif
#include <sys/u8_textprep.h>
#include <smbsrv/alloc.h>
#include <smbsrv/string.h>
#include <smbsrv/cp_usascii.h>
#include <smbsrv/cp_unicode.h>

#define	UNICODE_N_ENTRIES	(sizeof (a_unicode) / sizeof (a_unicode[0]))

/*
 * Global pointer to the current codepage: defaults to ASCII,
 * and a flag indicating whether the codepage is Unicode or ASCII.
 */
static smb_codepage_t *current_codepage = usascii_codepage;
static boolean_t is_unicode = B_FALSE;

static smb_codepage_t *smb_unicode_init(void);

/*
 * strsubst
 *
 * Scan a string replacing all occurrences of orgchar with newchar.
 * Returns a pointer to s, or null of s is null.
 */
char *
strsubst(char *s, char orgchar, char newchar)
{
	char *p = s;

	if (p == 0)
		return (0);

	while (*p) {
		if (*p == orgchar)
			*p = newchar;
		++p;
	}

	return (s);
}

/*
 * strcanon
 *
 * Normalize a string by reducing all the repeated characters in
 * buf as defined by class. For example;
 *
 *		char *buf = strdup("/d1//d2//d3\\\\d4\\\\f1.txt");
 *		strcanon(buf, "/\\");
 *
 * Would result in buf containing the following string:
 *
 *		/d1/d2/d3\d4\f1.txt
 *
 * This function modifies the contents of buf in place and returns
 * a pointer to buf.
 */
char *
strcanon(char *buf, const char *class)
{
	char *p = buf;
	char *q = buf;
	char *r;

	while (*p) {
		*q++ = *p;

		if ((r = strchr(class, *p)) != 0) {
			while (*p == *r)
				++p;
		} else
			++p;
	}

	*q = '\0';
	return (buf);
}

void
smb_codepage_init(void)
{
	smb_codepage_t *cp;

	if (is_unicode)
		return;

	if ((cp = smb_unicode_init()) != NULL) {
		current_codepage = cp;
		is_unicode = B_TRUE;
	} else {
		current_codepage = usascii_codepage;
		is_unicode = B_FALSE;
	}
}

/*
 * Determine whether or not a character is an uppercase character.
 * This function operates on the current codepage table. Returns
 * non-zero if the character is uppercase. Otherwise returns zero.
 */
int
smb_isupper(int c)
{
	uint16_t mask = is_unicode ? 0xffff : 0xff;

	return (current_codepage[c & mask].ctype & CODEPAGE_ISUPPER);
}

/*
 * Determine whether or not a character is an lowercase character.
 * This function operates on the current codepage table. Returns
 * non-zero if the character is lowercase. Otherwise returns zero.
 */
int
smb_islower(int c)
{
	uint16_t mask = is_unicode ? 0xffff : 0xff;

	return (current_codepage[c & mask].ctype & CODEPAGE_ISLOWER);
}

/*
 * Convert individual characters to their uppercase equivalent value.
 * If the specified character is lowercase, the uppercase value will
 * be returned. Otherwise the original value will be returned.
 */
int
smb_toupper(int c)
{
	uint16_t mask = is_unicode ? 0xffff : 0xff;

	return (current_codepage[c & mask].upper);
}

/*
 * Convert individual characters to their lowercase equivalent value.
 * If the specified character is uppercase, the lowercase value will
 * be returned. Otherwise the original value will be returned.
 */
int
smb_tolower(int c)
{
	uint16_t mask = is_unicode ? 0xffff : 0xff;

	return (current_codepage[c & mask].lower);
}

/*
 * Convert a string to uppercase using the appropriate codepage. The
 * string is converted in place. A pointer to the string is returned.
 * There is an assumption here that uppercase and lowercase values
 * always result encode to the same length.
 */
char *
smb_strupr(char *s)
{
	smb_wchar_t c;
	char *p = s;

	while (*p) {
		if (smb_isascii(*p)) {
			*p = smb_toupper(*p);
			p++;
		} else {
			if (smb_mbtowc(&c, p, MTS_MB_CHAR_MAX) < 0)
				return (0);

			if (c == 0)
				break;

			c = smb_toupper(c);
			p += smb_wctomb(p, c);
		}
	}

	return (s);
}

/*
 * Convert a string to lowercase using the appropriate codepage. The
 * string is converted in place. A pointer to the string is returned.
 * There is an assumption here that uppercase and lowercase values
 * always result encode to the same length.
 */
char *
smb_strlwr(char *s)
{
	smb_wchar_t c;
	char *p = s;

	while (*p) {
		if (smb_isascii(*p)) {
			*p = smb_tolower(*p);
			p++;
		} else {
			if (smb_mbtowc(&c, p, MTS_MB_CHAR_MAX) < 0)
				return (0);

			if (c == 0)
				break;

			c = smb_tolower(c);
			p += smb_wctomb(p, c);
		}
	}

	return (s);
}

/*
 * Returns 1 if string contains NO uppercase chars 0 otherwise. However,
 * -1 is returned if "s" is not a valid multi-byte string.
 */
int
smb_isstrlwr(const char *s)
{
	smb_wchar_t c;
	int n;
	const char *p = s;

	while (*p) {
		if (smb_isascii(*p) && smb_isupper(*p))
			return (0);
		else {
			if ((n = smb_mbtowc(&c, p, MTS_MB_CHAR_MAX)) < 0)
				return (-1);

			if (c == 0)
				break;

			if (smb_isupper(c))
				return (0);

			p += n;
		}
	}

	return (1);
}

/*
 * Returns 1 if string contains NO lowercase chars 0 otherwise. However,
 * -1 is returned if "s" is not a valid multi-byte string.
 */
int
smb_isstrupr(const char *s)
{
	smb_wchar_t c;
	int n;
	const char *p = s;

	while (*p) {
		if (smb_isascii(*p) && smb_islower(*p))
			return (0);
		else {
			if ((n = smb_mbtowc(&c, p, MTS_MB_CHAR_MAX)) < 0)
				return (-1);

			if (c == 0)
				break;

			if (smb_islower(c))
				return (0);

			p += n;
		}
	}

	return (1);
}

/*
 * Compare the null-terminated strings s1 and s2 and return an integer
 * greater than, equal to or less than 0 dependent on whether s1 is
 * lexicographically greater than, equal to or less than s2 after
 * translation of each character to lowercase.  The original strings
 * are not modified.
 *
 * If n is non-zero, at most n bytes are compared.  Otherwise, the strings
 * are compared until a null terminator is encountered.
 *
 * Out:    0 if strings are equal
 *       < 0 if first string < second string
 *       > 0 if first string > second string
 */
int
smb_strcasecmp(const char *s1, const char *s2, size_t n)
{
	int	err = 0;
	int	rc;

	rc = u8_strcmp(s1, s2, n, U8_STRCMP_CI_LOWER, U8_UNICODE_LATEST, &err);
	if (err != 0)
		return (-1);
	return (rc);
}

/*
 * First build a codepage based on cp_unicode.h.  Then build the unicode
 * codepage from this interim codepage by copying the entries over while
 * fixing them and filling in the gaps.
 */
static smb_codepage_t *
smb_unicode_init(void)
{
	smb_codepage_t	*unicode;
	uint32_t	a = 0;
	uint32_t	b = 0;

	unicode = MEM_ZALLOC("unicode", sizeof (smb_codepage_t) << 16);
	if (unicode == NULL)
		return (NULL);

	while (b != 0xffff) {
		/*
		 * If there is a gap in the standard,
		 * fill in the gap with no-case entries.
		 */
		if (UNICODE_N_ENTRIES <= a || a_unicode[a].val > b) {
			unicode[b].ctype = CODEPAGE_ISNONE;
			unicode[b].upper = (smb_wchar_t)b;
			unicode[b].lower = (smb_wchar_t)b;
			b++;
			continue;
		}

		/*
		 * Copy the entry and fixup as required.
		 */
		switch (a_unicode[a].ctype) {
		case CODEPAGE_ISNONE:
			/*
			 * Replace 0xffff in upper/lower fields with its val.
			 */
			unicode[b].ctype = CODEPAGE_ISNONE;
			unicode[b].upper = (smb_wchar_t)b;
			unicode[b].lower = (smb_wchar_t)b;
			break;
		case CODEPAGE_ISUPPER:
			/*
			 * Some characters may have case yet not have
			 * case conversion.  Treat them as no-case.
			 */
			if (a_unicode[a].lower == 0xffff) {
				unicode[b].ctype = CODEPAGE_ISNONE;
				unicode[b].upper = (smb_wchar_t)b;
				unicode[b].lower = (smb_wchar_t)b;
			} else {
				unicode[b].ctype = CODEPAGE_ISUPPER;
				unicode[b].upper = (smb_wchar_t)b;
				unicode[b].lower = a_unicode[a].lower;
			}
			break;
		case CODEPAGE_ISLOWER:
			/*
			 * Some characters may have case yet not have
			 * case conversion.  Treat them as no-case.
			 */
			if (a_unicode[a].upper == 0xffff) {
				unicode[b].ctype = CODEPAGE_ISNONE;
				unicode[b].upper = (smb_wchar_t)b;
				unicode[b].lower = (smb_wchar_t)b;
			} else {
				unicode[b].ctype = CODEPAGE_ISLOWER;
				unicode[b].upper = a_unicode[a].upper;
				unicode[b].lower = (smb_wchar_t)b;
			}
			break;
		default:
			MEM_FREE("unicode", unicode);
			return (NULL);
		}

		a++;
		b++;
	};

	return (unicode);
}