|
| 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 */ |
0 commit comments