Skip to content

Commit 089f7f5

Browse files
Dr. David Alan GilbertSteve French
authored andcommitted
fs/smb: Swing unicode common code from smb->NLS
Swing most of the inline functions and unicode tables into nls from the copy in smb/server. This is UCS-2 rather than most of the rest of the code in NLS, but it currently seems like the best place for it. The actual unicode.c implementations vary much more between server and client so they're unmoved. Signed-off-by: Dr. David Alan Gilbert <[email protected]> Reviewed-by: Dave Kleikamp <[email protected]> Signed-off-by: Steve French <[email protected]>
1 parent 9e74938 commit 089f7f5

File tree

7 files changed

+328
-284
lines changed

7 files changed

+328
-284
lines changed

fs/nls/Kconfig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,12 @@ config NLS_UTF8
617617
input/output character sets. Say Y here for the UTF-8 encoding of
618618
the Unicode/ISO9646 universal character set.
619619

620+
config NLS_UCS2_UTILS
621+
tristate "NLS UCS-2 UTILS"
622+
help
623+
Set of older UCS-2 conversion utilities and tables used by some
624+
filesystems including SMB/CIFS. This includes upper case conversion
625+
tables. This will automatically be selected when the filesystem
626+
that uses it is selected.
627+
620628
endif # NLS

fs/nls/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ obj-$(CONFIG_NLS_MAC_INUIT) += mac-inuit.o
5454
obj-$(CONFIG_NLS_MAC_ROMANIAN) += mac-romanian.o
5555
obj-$(CONFIG_NLS_MAC_ROMAN) += mac-roman.o
5656
obj-$(CONFIG_NLS_MAC_TURKISH) += mac-turkish.o
57+
obj-$(CONFIG_NLS_UCS2_UTILS) += nls_ucs2_utils.o

fs/smb/server/uniupr.h renamed to fs/nls/nls_ucs2_utils.c

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
/* SPDX-License-Identifier: GPL-2.0-or-later */
1+
// SPDX-License-Identifier: GPL-2.0-or-later
22
/*
33
* Some of the source code in this file came from fs/cifs/uniupr.h
44
* Copyright (c) International Business Machines Corp., 2000,2002
55
*
6-
* uniupr.h - Unicode compressed case ranges
6+
* Some of the source code in this file came from fs/cifs/cifs_unicode.c
7+
*
8+
* Copyright (c) International Business Machines Corp., 2000,2009
9+
* Modified by Steve French ([email protected])
10+
* Modified by Namjae Jeon ([email protected])
711
*
812
*/
9-
#ifndef __KSMBD_UNIUPR_H
10-
#define __KSMBD_UNIUPR_H
13+
#include <linux/fs.h>
14+
#include <linux/module.h>
15+
#include <linux/slab.h>
16+
#include <asm/unaligned.h>
17+
#include "nls_ucs2_utils.h"
18+
19+
MODULE_LICENSE("GPL");
1120

12-
#ifndef UNIUPR_NOUPPER
1321
/*
1422
* Latin upper case
1523
*/
16-
signed char SmbUniUpperTable[512] = {
24+
signed char NlsUniUpperTable[512] = {
1725
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 000-00f */
1826
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 010-01f */
1927
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 020-02f */
@@ -51,6 +59,7 @@ signed char SmbUniUpperTable[512] = {
5159
0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, 0, -1, /* 1e0-1ef */
5260
0, 0, -1, -2, 0, -1, 0, 0, 0, -1, 0, -1, 0, -1, 0, -1, /* 1f0-1ff */
5361
};
62+
EXPORT_SYMBOL_GPL(NlsUniUpperTable);
5463

5564
/* Upper case range - Greek */
5665
static signed char UniCaseRangeU03a0[47] = {
@@ -126,14 +135,12 @@ static signed char UniCaseRangeUff40[27] = {
126135
/*
127136
* Upper Case Range
128137
*/
129-
const struct UniCaseRange SmbUniUpperRange[] = {
138+
const struct UniCaseRange NlsUniUpperRange[] = {
130139
{0x03a0, 0x03ce, UniCaseRangeU03a0},
131140
{0x0430, 0x045f, UniCaseRangeU0430},
132141
{0x0490, 0x04cc, UniCaseRangeU0490},
133142
{0x1e00, 0x1ffc, UniCaseRangeU1e00},
134143
{0xff40, 0xff5a, UniCaseRangeUff40},
135144
{0}
136145
};
137-
#endif
138-
139-
#endif /* __KSMBD_UNIUPR_H */
146+
EXPORT_SYMBOL_GPL(NlsUniUpperRange);

fs/nls/nls_ucs2_utils.h

Lines changed: 297 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,297 @@
1+
/* SPDX-License-Identifier: GPL-2.0-or-later */
2+
/*
3+
* Some of the source code in this file came from fs/cifs/cifs_unicode.c
4+
* and then via server/unicode.c
5+
* cifs_unicode: Unicode kernel case support
6+
*
7+
* Function:
8+
* Convert a unicode character to upper or lower case using
9+
* compressed tables.
10+
*
11+
* Copyright (c) International Business Machines Corp., 2000,2009
12+
*
13+
*
14+
* Notes:
15+
* These APIs are based on the C library functions. The semantics
16+
* should match the C functions but with expanded size operands.
17+
*
18+
* The upper/lower functions are based on a table created by mkupr.
19+
* This is a compressed table of upper and lower case conversion.
20+
*
21+
*/
22+
#ifndef _NLS_UCS2_UTILS_H
23+
#define _NLS_UCS2_UTILS_H
24+
25+
#include <asm/byteorder.h>
26+
#include <linux/types.h>
27+
#include <linux/nls.h>
28+
#include <linux/unicode.h>
29+
30+
/*
31+
* Windows maps these to the user defined 16 bit Unicode range since they are
32+
* reserved symbols (along with \ and /), otherwise illegal to store
33+
* in filenames in NTFS
34+
*/
35+
#define UNI_ASTERISK ((__u16)('*' + 0xF000))
36+
#define UNI_QUESTION ((__u16)('?' + 0xF000))
37+
#define UNI_COLON ((__u16)(':' + 0xF000))
38+
#define UNI_GRTRTHAN ((__u16)('>' + 0xF000))
39+
#define UNI_LESSTHAN ((__u16)('<' + 0xF000))
40+
#define UNI_PIPE ((__u16)('|' + 0xF000))
41+
#define UNI_SLASH ((__u16)('\\' + 0xF000))
42+
43+
#ifndef UNICASERANGE_DEFINED
44+
struct UniCaseRange {
45+
wchar_t start;
46+
wchar_t end;
47+
signed char *table;
48+
};
49+
#endif /* UNICASERANGE_DEFINED */
50+
51+
#ifndef UNIUPR_NOUPPER
52+
extern signed char NlsUniUpperTable[512];
53+
extern const struct UniCaseRange NlsUniUpperRange[];
54+
#endif /* UNIUPR_NOUPPER */
55+
56+
/*
57+
* UniStrcat: Concatenate the second string to the first
58+
*
59+
* Returns:
60+
* Address of the first string
61+
*/
62+
static inline wchar_t *UniStrcat(wchar_t *ucs1, const wchar_t *ucs2)
63+
{
64+
wchar_t *anchor = ucs1; /* save a pointer to start of ucs1 */
65+
66+
while (*ucs1++)
67+
/*NULL*/; /* To end of first string */
68+
ucs1--; /* Return to the null */
69+
while ((*ucs1++ = *ucs2++))
70+
/*NULL*/; /* copy string 2 over */
71+
return anchor;
72+
}
73+
74+
/*
75+
* UniStrchr: Find a character in a string
76+
*
77+
* Returns:
78+
* Address of first occurrence of character in string
79+
* or NULL if the character is not in the string
80+
*/
81+
static inline wchar_t *UniStrchr(const wchar_t *ucs, wchar_t uc)
82+
{
83+
while ((*ucs != uc) && *ucs)
84+
ucs++;
85+
86+
if (*ucs == uc)
87+
return (wchar_t *)ucs;
88+
return NULL;
89+
}
90+
91+
/*
92+
* UniStrcmp: Compare two strings
93+
*
94+
* Returns:
95+
* < 0: First string is less than second
96+
* = 0: Strings are equal
97+
* > 0: First string is greater than second
98+
*/
99+
static inline int UniStrcmp(const wchar_t *ucs1, const wchar_t *ucs2)
100+
{
101+
while ((*ucs1 == *ucs2) && *ucs1) {
102+
ucs1++;
103+
ucs2++;
104+
}
105+
return (int)*ucs1 - (int)*ucs2;
106+
}
107+
108+
/*
109+
* UniStrcpy: Copy a string
110+
*/
111+
static inline wchar_t *UniStrcpy(wchar_t *ucs1, const wchar_t *ucs2)
112+
{
113+
wchar_t *anchor = ucs1; /* save the start of result string */
114+
115+
while ((*ucs1++ = *ucs2++))
116+
/*NULL*/;
117+
return anchor;
118+
}
119+
120+
/*
121+
* UniStrlen: Return the length of a string (in 16 bit Unicode chars not bytes)
122+
*/
123+
static inline size_t UniStrlen(const wchar_t *ucs1)
124+
{
125+
int i = 0;
126+
127+
while (*ucs1++)
128+
i++;
129+
return i;
130+
}
131+
132+
/*
133+
* UniStrnlen: Return the length (in 16 bit Unicode chars not bytes) of a
134+
* string (length limited)
135+
*/
136+
static inline size_t UniStrnlen(const wchar_t *ucs1, int maxlen)
137+
{
138+
int i = 0;
139+
140+
while (*ucs1++) {
141+
i++;
142+
if (i >= maxlen)
143+
break;
144+
}
145+
return i;
146+
}
147+
148+
/*
149+
* UniStrncat: Concatenate length limited string
150+
*/
151+
static inline wchar_t *UniStrncat(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
152+
{
153+
wchar_t *anchor = ucs1; /* save pointer to string 1 */
154+
155+
while (*ucs1++)
156+
/*NULL*/;
157+
ucs1--; /* point to null terminator of s1 */
158+
while (n-- && (*ucs1 = *ucs2)) { /* copy s2 after s1 */
159+
ucs1++;
160+
ucs2++;
161+
}
162+
*ucs1 = 0; /* Null terminate the result */
163+
return anchor;
164+
}
165+
166+
/*
167+
* UniStrncmp: Compare length limited string
168+
*/
169+
static inline int UniStrncmp(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
170+
{
171+
if (!n)
172+
return 0; /* Null strings are equal */
173+
while ((*ucs1 == *ucs2) && *ucs1 && --n) {
174+
ucs1++;
175+
ucs2++;
176+
}
177+
return (int)*ucs1 - (int)*ucs2;
178+
}
179+
180+
/*
181+
* UniStrncmp_le: Compare length limited string - native to little-endian
182+
*/
183+
static inline int
184+
UniStrncmp_le(const wchar_t *ucs1, const wchar_t *ucs2, size_t n)
185+
{
186+
if (!n)
187+
return 0; /* Null strings are equal */
188+
while ((*ucs1 == __le16_to_cpu(*ucs2)) && *ucs1 && --n) {
189+
ucs1++;
190+
ucs2++;
191+
}
192+
return (int)*ucs1 - (int)__le16_to_cpu(*ucs2);
193+
}
194+
195+
/*
196+
* UniStrncpy: Copy length limited string with pad
197+
*/
198+
static inline wchar_t *UniStrncpy(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
199+
{
200+
wchar_t *anchor = ucs1;
201+
202+
while (n-- && *ucs2) /* Copy the strings */
203+
*ucs1++ = *ucs2++;
204+
205+
n++;
206+
while (n--) /* Pad with nulls */
207+
*ucs1++ = 0;
208+
return anchor;
209+
}
210+
211+
/*
212+
* UniStrncpy_le: Copy length limited string with pad to little-endian
213+
*/
214+
static inline wchar_t *UniStrncpy_le(wchar_t *ucs1, const wchar_t *ucs2, size_t n)
215+
{
216+
wchar_t *anchor = ucs1;
217+
218+
while (n-- && *ucs2) /* Copy the strings */
219+
*ucs1++ = __le16_to_cpu(*ucs2++);
220+
221+
n++;
222+
while (n--) /* Pad with nulls */
223+
*ucs1++ = 0;
224+
return anchor;
225+
}
226+
227+
/*
228+
* UniStrstr: Find a string in a string
229+
*
230+
* Returns:
231+
* Address of first match found
232+
* NULL if no matching string is found
233+
*/
234+
static inline wchar_t *UniStrstr(const wchar_t *ucs1, const wchar_t *ucs2)
235+
{
236+
const wchar_t *anchor1 = ucs1;
237+
const wchar_t *anchor2 = ucs2;
238+
239+
while (*ucs1) {
240+
if (*ucs1 == *ucs2) {
241+
/* Partial match found */
242+
ucs1++;
243+
ucs2++;
244+
} else {
245+
if (!*ucs2) /* Match found */
246+
return (wchar_t *)anchor1;
247+
ucs1 = ++anchor1; /* No match */
248+
ucs2 = anchor2;
249+
}
250+
}
251+
252+
if (!*ucs2) /* Both end together */
253+
return (wchar_t *)anchor1; /* Match found */
254+
return NULL; /* No match */
255+
}
256+
257+
#ifndef UNIUPR_NOUPPER
258+
/*
259+
* UniToupper: Convert a unicode character to upper case
260+
*/
261+
static inline wchar_t UniToupper(register wchar_t uc)
262+
{
263+
register const struct UniCaseRange *rp;
264+
265+
if (uc < sizeof(NlsUniUpperTable)) {
266+
/* Latin characters */
267+
return uc + NlsUniUpperTable[uc]; /* Use base tables */
268+
}
269+
270+
rp = NlsUniUpperRange; /* Use range tables */
271+
while (rp->start) {
272+
if (uc < rp->start) /* Before start of range */
273+
return uc; /* Uppercase = input */
274+
if (uc <= rp->end) /* In range */
275+
return uc + rp->table[uc - rp->start];
276+
rp++; /* Try next range */
277+
}
278+
return uc; /* Past last range */
279+
}
280+
281+
/*
282+
* UniStrupr: Upper case a unicode string
283+
*/
284+
static inline __le16 *UniStrupr(register __le16 *upin)
285+
{
286+
register __le16 *up;
287+
288+
up = upin;
289+
while (*up) { /* For all characters */
290+
*up = cpu_to_le16(UniToupper(le16_to_cpu(*up)));
291+
up++;
292+
}
293+
return upin; /* Return input pointer */
294+
}
295+
#endif /* UNIUPR_NOUPPER */
296+
297+
#endif /* _NLS_UCS2_UTILS_H */

fs/smb/server/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ config SMB_SERVER
55
depends on FILE_LOCKING
66
select NLS
77
select NLS_UTF8
8+
select NLS_UCS2_UTILS
89
select CRYPTO
910
select CRYPTO_MD5
1011
select CRYPTO_HMAC

fs/smb/server/unicode.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
#include <asm/unaligned.h>
1212
#include "glob.h"
1313
#include "unicode.h"
14-
#include "uniupr.h"
1514
#include "smb_common.h"
1615

1716
/*

0 commit comments

Comments
 (0)