| 1 | /*
|
|---|
| 2 | Unix SMB/CIFS implementation.
|
|---|
| 3 | Samba utility functions
|
|---|
| 4 | Copyright (C) Andrew Tridgell 1992-2001
|
|---|
| 5 | Copyright (C) Simo Sorce 2001
|
|---|
| 6 | Copyright (C) Jeremy Allison 2005
|
|---|
| 7 |
|
|---|
| 8 | This program is free software; you can redistribute it and/or modify
|
|---|
| 9 | it under the terms of the GNU General Public License as published by
|
|---|
| 10 | the Free Software Foundation; either version 3 of the License, or
|
|---|
| 11 | (at your option) any later version.
|
|---|
| 12 |
|
|---|
| 13 | This program is distributed in the hope that it will be useful,
|
|---|
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 16 | GNU General Public License for more details.
|
|---|
| 17 |
|
|---|
| 18 | You should have received a copy of the GNU General Public License
|
|---|
| 19 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|---|
| 20 | */
|
|---|
| 21 |
|
|---|
| 22 | #include "includes.h"
|
|---|
| 23 |
|
|---|
| 24 | #ifndef MAXUNI
|
|---|
| 25 | #define MAXUNI 1024
|
|---|
| 26 | #endif
|
|---|
| 27 |
|
|---|
| 28 | /* these 3 tables define the unicode case handling. They are loaded
|
|---|
| 29 | at startup either via mmap() or read() from the lib directory */
|
|---|
| 30 | static smb_ucs2_t *upcase_table;
|
|---|
| 31 | static smb_ucs2_t *lowcase_table;
|
|---|
| 32 | static uint8 *valid_table;
|
|---|
| 33 | static bool upcase_table_use_unmap;
|
|---|
| 34 | static bool lowcase_table_use_unmap;
|
|---|
| 35 | static bool valid_table_use_unmap;
|
|---|
| 36 | static bool initialized;
|
|---|
| 37 |
|
|---|
| 38 | /**
|
|---|
| 39 | * Destroy global objects allocated by load_case_tables()
|
|---|
| 40 | **/
|
|---|
| 41 | void gfree_case_tables(void)
|
|---|
| 42 | {
|
|---|
| 43 | if ( upcase_table ) {
|
|---|
| 44 | if ( upcase_table_use_unmap )
|
|---|
| 45 | unmap_file(upcase_table, 0x20000);
|
|---|
| 46 | else
|
|---|
| 47 | SAFE_FREE(upcase_table);
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | if ( lowcase_table ) {
|
|---|
| 51 | if ( lowcase_table_use_unmap )
|
|---|
| 52 | unmap_file(lowcase_table, 0x20000);
|
|---|
| 53 | else
|
|---|
| 54 | SAFE_FREE(lowcase_table);
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | if ( valid_table ) {
|
|---|
| 58 | if ( valid_table_use_unmap )
|
|---|
| 59 | unmap_file(valid_table, 0x10000);
|
|---|
| 60 | else
|
|---|
| 61 | SAFE_FREE(valid_table);
|
|---|
| 62 | }
|
|---|
| 63 | initialized = false;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Load or generate the case handling tables.
|
|---|
| 68 | *
|
|---|
| 69 | * The case tables are defined in UCS2 and don't depend on any
|
|---|
| 70 | * configured parameters, so they never need to be reloaded.
|
|---|
| 71 | **/
|
|---|
| 72 |
|
|---|
| 73 | void load_case_tables(void)
|
|---|
| 74 | {
|
|---|
| 75 | char *old_locale = NULL, *saved_locale = NULL;
|
|---|
| 76 | int i;
|
|---|
| 77 | TALLOC_CTX *frame = NULL;
|
|---|
| 78 |
|
|---|
| 79 | if (initialized) {
|
|---|
| 80 | return;
|
|---|
| 81 | }
|
|---|
| 82 | initialized = true;
|
|---|
| 83 |
|
|---|
| 84 | frame = talloc_stackframe();
|
|---|
| 85 |
|
|---|
| 86 | upcase_table = (smb_ucs2_t *)map_file(data_path("upcase.dat"),
|
|---|
| 87 | 0x20000);
|
|---|
| 88 | upcase_table_use_unmap = ( upcase_table != NULL );
|
|---|
| 89 |
|
|---|
| 90 | lowcase_table = (smb_ucs2_t *)map_file(data_path("lowcase.dat"),
|
|---|
| 91 | 0x20000);
|
|---|
| 92 | lowcase_table_use_unmap = ( lowcase_table != NULL );
|
|---|
| 93 |
|
|---|
| 94 | #ifdef HAVE_SETLOCALE
|
|---|
| 95 | /* Get the name of the current locale. */
|
|---|
| 96 | old_locale = setlocale(LC_ALL, NULL);
|
|---|
| 97 |
|
|---|
| 98 | if (old_locale) {
|
|---|
| 99 | /* Save it as it is in static storage. */
|
|---|
| 100 | saved_locale = SMB_STRDUP(old_locale);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /* We set back the locale to C to get ASCII-compatible toupper/lower functions. */
|
|---|
| 104 | setlocale(LC_ALL, "C");
|
|---|
| 105 | #endif
|
|---|
| 106 |
|
|---|
| 107 | /* we would like Samba to limp along even if these tables are
|
|---|
| 108 | not available */
|
|---|
| 109 | if (!upcase_table) {
|
|---|
| 110 | DEBUG(1,("creating lame upcase table\n"));
|
|---|
| 111 | upcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
|
|---|
| 112 | for (i=0;i<0x10000;i++) {
|
|---|
| 113 | smb_ucs2_t v;
|
|---|
| 114 | SSVAL(&v, 0, i);
|
|---|
| 115 | upcase_table[v] = i;
|
|---|
| 116 | }
|
|---|
| 117 | for (i=0;i<256;i++) {
|
|---|
| 118 | smb_ucs2_t v;
|
|---|
| 119 | SSVAL(&v, 0, UCS2_CHAR(i));
|
|---|
| 120 | upcase_table[v] = UCS2_CHAR(islower(i)?toupper(i):i);
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | if (!lowcase_table) {
|
|---|
| 125 | DEBUG(1,("creating lame lowcase table\n"));
|
|---|
| 126 | lowcase_table = (smb_ucs2_t *)SMB_MALLOC(0x20000);
|
|---|
| 127 | for (i=0;i<0x10000;i++) {
|
|---|
| 128 | smb_ucs2_t v;
|
|---|
| 129 | SSVAL(&v, 0, i);
|
|---|
| 130 | lowcase_table[v] = i;
|
|---|
| 131 | }
|
|---|
| 132 | for (i=0;i<256;i++) {
|
|---|
| 133 | smb_ucs2_t v;
|
|---|
| 134 | SSVAL(&v, 0, UCS2_CHAR(i));
|
|---|
| 135 | lowcase_table[v] = UCS2_CHAR(isupper(i)?tolower(i):i);
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | #ifdef HAVE_SETLOCALE
|
|---|
| 140 | /* Restore the old locale. */
|
|---|
| 141 | if (saved_locale) {
|
|---|
| 142 | setlocale (LC_ALL, saved_locale);
|
|---|
| 143 | SAFE_FREE(saved_locale);
|
|---|
| 144 | }
|
|---|
| 145 | #endif
|
|---|
| 146 | TALLOC_FREE(frame);
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | static int check_dos_char_slowly(smb_ucs2_t c)
|
|---|
| 150 | {
|
|---|
| 151 | char buf[10];
|
|---|
| 152 | smb_ucs2_t c2 = 0;
|
|---|
| 153 | int len1, len2;
|
|---|
| 154 |
|
|---|
| 155 | len1 = convert_string(CH_UTF16LE, CH_DOS, &c, 2, buf, sizeof(buf),False);
|
|---|
| 156 | if (len1 == 0) {
|
|---|
| 157 | return 0;
|
|---|
| 158 | }
|
|---|
| 159 | len2 = convert_string(CH_DOS, CH_UTF16LE, buf, len1, &c2, 2,False);
|
|---|
| 160 | if (len2 != 2) {
|
|---|
| 161 | return 0;
|
|---|
| 162 | }
|
|---|
| 163 | return (c == c2);
|
|---|
| 164 | }
|
|---|
| 165 |
|
|---|
| 166 | /**
|
|---|
| 167 | * Load the valid character map table from <tt>valid.dat</tt> or
|
|---|
| 168 | * create from the configured codepage.
|
|---|
| 169 | *
|
|---|
| 170 | * This function is called whenever the configuration is reloaded.
|
|---|
| 171 | * However, the valid character table is not changed if it's loaded
|
|---|
| 172 | * from a file, because we can't unmap files.
|
|---|
| 173 | **/
|
|---|
| 174 |
|
|---|
| 175 | void init_valid_table(void)
|
|---|
| 176 | {
|
|---|
| 177 | static int mapped_file;
|
|---|
| 178 | int i;
|
|---|
| 179 | const char *allowed = ".!#$%&'()_-@^`~";
|
|---|
| 180 | uint8 *valid_file;
|
|---|
| 181 |
|
|---|
| 182 | if (mapped_file) {
|
|---|
| 183 | /* Can't unmap files, so stick with what we have */
|
|---|
| 184 | return;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | valid_file = (uint8 *)map_file(data_path("valid.dat"), 0x10000);
|
|---|
| 188 | if (valid_file) {
|
|---|
| 189 | valid_table = valid_file;
|
|---|
| 190 | mapped_file = 1;
|
|---|
| 191 | valid_table_use_unmap = True;
|
|---|
| 192 | return;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | /* Otherwise, we're using a dynamically created valid_table.
|
|---|
| 196 | * It might need to be regenerated if the code page changed.
|
|---|
| 197 | * We know that we're not using a mapped file, so we can
|
|---|
| 198 | * free() the old one. */
|
|---|
| 199 | SAFE_FREE(valid_table);
|
|---|
| 200 |
|
|---|
| 201 | /* use free rather than unmap */
|
|---|
| 202 | valid_table_use_unmap = False;
|
|---|
| 203 |
|
|---|
| 204 | DEBUG(2,("creating default valid table\n"));
|
|---|
| 205 | valid_table = (uint8 *)SMB_MALLOC(0x10000);
|
|---|
| 206 | SMB_ASSERT(valid_table != NULL);
|
|---|
| 207 | for (i=0;i<128;i++) {
|
|---|
| 208 | valid_table[i] = isalnum(i) || strchr(allowed,i);
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | lazy_initialize_conv();
|
|---|
| 212 |
|
|---|
| 213 | for (;i<0x10000;i++) {
|
|---|
| 214 | smb_ucs2_t c;
|
|---|
| 215 | SSVAL(&c, 0, i);
|
|---|
| 216 | valid_table[i] = check_dos_char_slowly(c);
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | /*******************************************************************
|
|---|
| 221 | Write a string in (little-endian) unicode format. src is in
|
|---|
| 222 | the current DOS codepage. len is the length in bytes of the
|
|---|
| 223 | string pointed to by dst.
|
|---|
| 224 |
|
|---|
| 225 | if null_terminate is True then null terminate the packet (adds 2 bytes)
|
|---|
| 226 |
|
|---|
| 227 | the return value is the length in bytes consumed by the string, including the
|
|---|
| 228 | null termination if applied
|
|---|
| 229 | ********************************************************************/
|
|---|
| 230 |
|
|---|
| 231 | size_t dos_PutUniCode(char *dst,const char *src, size_t len, bool null_terminate)
|
|---|
| 232 | {
|
|---|
| 233 | int flags = null_terminate ? STR_UNICODE|STR_NOALIGN|STR_TERMINATE
|
|---|
| 234 | : STR_UNICODE|STR_NOALIGN;
|
|---|
| 235 | return push_ucs2(NULL, dst, src, len, flags);
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 |
|
|---|
| 239 | /*******************************************************************
|
|---|
| 240 | Skip past a unicode string, but not more than len. Always move
|
|---|
| 241 | past a terminating zero if found.
|
|---|
| 242 | ********************************************************************/
|
|---|
| 243 |
|
|---|
| 244 | char *skip_unibuf(char *src, size_t len)
|
|---|
| 245 | {
|
|---|
| 246 | char *srcend = src + len;
|
|---|
| 247 |
|
|---|
| 248 | while (src < srcend && SVAL(src,0)) {
|
|---|
| 249 | src += 2;
|
|---|
| 250 | }
|
|---|
| 251 |
|
|---|
| 252 | if(!SVAL(src,0)) {
|
|---|
| 253 | src += 2;
|
|---|
| 254 | }
|
|---|
| 255 |
|
|---|
| 256 | return src;
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | /* Converts a string from internal samba format to unicode
|
|---|
| 260 | */
|
|---|
| 261 |
|
|---|
| 262 | int rpcstr_push(void *dest, const char *src, size_t dest_len, int flags)
|
|---|
| 263 | {
|
|---|
| 264 | return push_ucs2(NULL, dest, src, dest_len, flags|STR_UNICODE|STR_NOALIGN);
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | /* Converts a string from internal samba format to unicode. Always terminates.
|
|---|
| 268 | * Actually just a wrapper round push_ucs2_talloc().
|
|---|
| 269 | */
|
|---|
| 270 |
|
|---|
| 271 | int rpcstr_push_talloc(TALLOC_CTX *ctx, smb_ucs2_t **dest, const char *src)
|
|---|
| 272 | {
|
|---|
| 273 | size_t size;
|
|---|
| 274 | if (push_ucs2_talloc(ctx, dest, src, &size))
|
|---|
| 275 | return size;
|
|---|
| 276 | else
|
|---|
| 277 | return -1;
|
|---|
| 278 | }
|
|---|
| 279 |
|
|---|
| 280 | /*******************************************************************
|
|---|
| 281 | Convert a wchar to upper case.
|
|---|
| 282 | ********************************************************************/
|
|---|
| 283 |
|
|---|
| 284 | smb_ucs2_t toupper_w(smb_ucs2_t val)
|
|---|
| 285 | {
|
|---|
| 286 | return upcase_table[SVAL(&val,0)];
|
|---|
| 287 | }
|
|---|
| 288 |
|
|---|
| 289 | /*******************************************************************
|
|---|
| 290 | Convert a wchar to lower case.
|
|---|
| 291 | ********************************************************************/
|
|---|
| 292 |
|
|---|
| 293 | smb_ucs2_t tolower_w( smb_ucs2_t val )
|
|---|
| 294 | {
|
|---|
| 295 | return lowcase_table[SVAL(&val,0)];
|
|---|
| 296 | }
|
|---|
| 297 |
|
|---|
| 298 | /*******************************************************************
|
|---|
| 299 | Determine if a character is lowercase.
|
|---|
| 300 | ********************************************************************/
|
|---|
| 301 |
|
|---|
| 302 | bool islower_w(smb_ucs2_t c)
|
|---|
| 303 | {
|
|---|
| 304 | return upcase_table[SVAL(&c,0)] != c;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /*******************************************************************
|
|---|
| 308 | Determine if a character is uppercase.
|
|---|
| 309 | ********************************************************************/
|
|---|
| 310 |
|
|---|
| 311 | bool isupper_w(smb_ucs2_t c)
|
|---|
| 312 | {
|
|---|
| 313 | return lowcase_table[SVAL(&c,0)] != c;
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | /*******************************************************************
|
|---|
| 317 | Determine if a character is valid in a 8.3 name.
|
|---|
| 318 | ********************************************************************/
|
|---|
| 319 |
|
|---|
| 320 | bool isvalid83_w(smb_ucs2_t c)
|
|---|
| 321 | {
|
|---|
| 322 | return valid_table[SVAL(&c,0)] != 0;
|
|---|
| 323 | }
|
|---|
| 324 |
|
|---|
| 325 | /*******************************************************************
|
|---|
| 326 | Count the number of characters in a smb_ucs2_t string.
|
|---|
| 327 | ********************************************************************/
|
|---|
| 328 |
|
|---|
| 329 | size_t strlen_w(const smb_ucs2_t *src)
|
|---|
| 330 | {
|
|---|
| 331 | size_t len;
|
|---|
| 332 | smb_ucs2_t c;
|
|---|
| 333 |
|
|---|
| 334 | for(len = 0; *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
|
|---|
| 335 | ;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | return len;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | /*******************************************************************
|
|---|
| 342 | Count up to max number of characters in a smb_ucs2_t string.
|
|---|
| 343 | ********************************************************************/
|
|---|
| 344 |
|
|---|
| 345 | size_t strnlen_w(const smb_ucs2_t *src, size_t max)
|
|---|
| 346 | {
|
|---|
| 347 | size_t len;
|
|---|
| 348 | smb_ucs2_t c;
|
|---|
| 349 |
|
|---|
| 350 | for(len = 0; (len < max) && *(COPY_UCS2_CHAR(&c,src)); src++, len++) {
|
|---|
| 351 | ;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | return len;
|
|---|
| 355 | }
|
|---|
| 356 |
|
|---|
| 357 | /*******************************************************************
|
|---|
| 358 | Wide strchr().
|
|---|
| 359 | ********************************************************************/
|
|---|
| 360 |
|
|---|
| 361 | smb_ucs2_t *strchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
|
|---|
| 362 | {
|
|---|
| 363 | smb_ucs2_t cp;
|
|---|
| 364 | while (*(COPY_UCS2_CHAR(&cp,s))) {
|
|---|
| 365 | if (c == cp) {
|
|---|
| 366 | return (smb_ucs2_t *)s;
|
|---|
| 367 | }
|
|---|
| 368 | s++;
|
|---|
| 369 | }
|
|---|
| 370 | if (c == cp) {
|
|---|
| 371 | return (smb_ucs2_t *)s;
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | return NULL;
|
|---|
| 375 | }
|
|---|
| 376 |
|
|---|
| 377 | smb_ucs2_t *strchr_wa(const smb_ucs2_t *s, char c)
|
|---|
| 378 | {
|
|---|
| 379 | return strchr_w(s, UCS2_CHAR(c));
|
|---|
| 380 | }
|
|---|
| 381 |
|
|---|
| 382 | /*******************************************************************
|
|---|
| 383 | Wide strrchr().
|
|---|
| 384 | ********************************************************************/
|
|---|
| 385 |
|
|---|
| 386 | smb_ucs2_t *strrchr_w(const smb_ucs2_t *s, smb_ucs2_t c)
|
|---|
| 387 | {
|
|---|
| 388 | smb_ucs2_t cp;
|
|---|
| 389 | const smb_ucs2_t *p = s;
|
|---|
| 390 | int len = strlen_w(s);
|
|---|
| 391 |
|
|---|
| 392 | if (len == 0) {
|
|---|
| 393 | return NULL;
|
|---|
| 394 | }
|
|---|
| 395 | p += (len - 1);
|
|---|
| 396 | do {
|
|---|
| 397 | if (c == *(COPY_UCS2_CHAR(&cp,p))) {
|
|---|
| 398 | return (smb_ucs2_t *)p;
|
|---|
| 399 | }
|
|---|
| 400 | } while (p-- != s);
|
|---|
| 401 | return NULL;
|
|---|
| 402 | }
|
|---|
| 403 |
|
|---|
| 404 | /*******************************************************************
|
|---|
| 405 | Wide version of strrchr that returns after doing strrchr 'n' times.
|
|---|
| 406 | ********************************************************************/
|
|---|
| 407 |
|
|---|
| 408 | smb_ucs2_t *strnrchr_w(const smb_ucs2_t *s, smb_ucs2_t c, unsigned int n)
|
|---|
| 409 | {
|
|---|
| 410 | smb_ucs2_t cp;
|
|---|
| 411 | const smb_ucs2_t *p = s;
|
|---|
| 412 | int len = strlen_w(s);
|
|---|
| 413 |
|
|---|
| 414 | if (len == 0 || !n) {
|
|---|
| 415 | return NULL;
|
|---|
| 416 | }
|
|---|
| 417 | p += (len - 1);
|
|---|
| 418 | do {
|
|---|
| 419 | if (c == *(COPY_UCS2_CHAR(&cp,p))) {
|
|---|
| 420 | n--;
|
|---|
| 421 | }
|
|---|
| 422 |
|
|---|
| 423 | if (!n) {
|
|---|
| 424 | return (smb_ucs2_t *)p;
|
|---|
| 425 | }
|
|---|
| 426 | } while (p-- != s);
|
|---|
| 427 | return NULL;
|
|---|
| 428 | }
|
|---|
| 429 |
|
|---|
| 430 | /*******************************************************************
|
|---|
| 431 | Wide strstr().
|
|---|
| 432 | ********************************************************************/
|
|---|
| 433 |
|
|---|
| 434 | smb_ucs2_t *strstr_w(const smb_ucs2_t *s, const smb_ucs2_t *ins)
|
|---|
| 435 | {
|
|---|
| 436 | smb_ucs2_t *r;
|
|---|
| 437 | size_t inslen;
|
|---|
| 438 |
|
|---|
| 439 | if (!s || !*s || !ins || !*ins) {
|
|---|
| 440 | return NULL;
|
|---|
| 441 | }
|
|---|
| 442 |
|
|---|
| 443 | inslen = strlen_w(ins);
|
|---|
| 444 | r = (smb_ucs2_t *)s;
|
|---|
| 445 |
|
|---|
| 446 | while ((r = strchr_w(r, *ins))) {
|
|---|
| 447 | if (strncmp_w(r, ins, inslen) == 0) {
|
|---|
| 448 | return r;
|
|---|
| 449 | }
|
|---|
| 450 | r++;
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | return NULL;
|
|---|
| 454 | }
|
|---|
| 455 |
|
|---|
| 456 | /*******************************************************************
|
|---|
| 457 | Convert a string to lower case.
|
|---|
| 458 | return True if any char is converted
|
|---|
| 459 | ********************************************************************/
|
|---|
| 460 |
|
|---|
| 461 | bool strlower_w(smb_ucs2_t *s)
|
|---|
| 462 | {
|
|---|
| 463 | smb_ucs2_t cp;
|
|---|
| 464 | bool ret = False;
|
|---|
| 465 |
|
|---|
| 466 | while (*(COPY_UCS2_CHAR(&cp,s))) {
|
|---|
| 467 | smb_ucs2_t v = tolower_w(cp);
|
|---|
| 468 | if (v != cp) {
|
|---|
| 469 | COPY_UCS2_CHAR(s,&v);
|
|---|
| 470 | ret = True;
|
|---|
| 471 | }
|
|---|
| 472 | s++;
|
|---|
| 473 | }
|
|---|
| 474 | return ret;
|
|---|
| 475 | }
|
|---|
| 476 |
|
|---|
| 477 | /*******************************************************************
|
|---|
| 478 | Convert a string to upper case.
|
|---|
| 479 | return True if any char is converted
|
|---|
| 480 | ********************************************************************/
|
|---|
| 481 |
|
|---|
| 482 | bool strupper_w(smb_ucs2_t *s)
|
|---|
| 483 | {
|
|---|
| 484 | smb_ucs2_t cp;
|
|---|
| 485 | bool ret = False;
|
|---|
| 486 | while (*(COPY_UCS2_CHAR(&cp,s))) {
|
|---|
| 487 | smb_ucs2_t v = toupper_w(cp);
|
|---|
| 488 | if (v != cp) {
|
|---|
| 489 | COPY_UCS2_CHAR(s,&v);
|
|---|
| 490 | ret = True;
|
|---|
| 491 | }
|
|---|
| 492 | s++;
|
|---|
| 493 | }
|
|---|
| 494 | return ret;
|
|---|
| 495 | }
|
|---|
| 496 |
|
|---|
| 497 | /*******************************************************************
|
|---|
| 498 | Convert a string to "normal" form.
|
|---|
| 499 | ********************************************************************/
|
|---|
| 500 |
|
|---|
| 501 | void strnorm_w(smb_ucs2_t *s, int case_default)
|
|---|
| 502 | {
|
|---|
| 503 | if (case_default == CASE_UPPER) {
|
|---|
| 504 | strupper_w(s);
|
|---|
| 505 | } else {
|
|---|
| 506 | strlower_w(s);
|
|---|
| 507 | }
|
|---|
| 508 | }
|
|---|
| 509 |
|
|---|
| 510 | int strcmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
|
|---|
| 511 | {
|
|---|
| 512 | smb_ucs2_t cpa, cpb;
|
|---|
| 513 |
|
|---|
| 514 | while ((*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
|
|---|
| 515 | a++;
|
|---|
| 516 | b++;
|
|---|
| 517 | }
|
|---|
| 518 | return (*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b)));
|
|---|
| 519 | /* warning: if *a != *b and both are not 0 we return a random
|
|---|
| 520 | greater or lesser than 0 number not realted to which
|
|---|
| 521 | string is longer */
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | int strncmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
|
|---|
| 525 | {
|
|---|
| 526 | smb_ucs2_t cpa, cpb;
|
|---|
| 527 | size_t n = 0;
|
|---|
| 528 |
|
|---|
| 529 | while ((n < len) && (*(COPY_UCS2_CHAR(&cpb,b))) && (*(COPY_UCS2_CHAR(&cpa,a)) == cpb)) {
|
|---|
| 530 | a++;
|
|---|
| 531 | b++;
|
|---|
| 532 | n++;
|
|---|
| 533 | }
|
|---|
| 534 | return (len - n)?(*(COPY_UCS2_CHAR(&cpa,a)) - *(COPY_UCS2_CHAR(&cpb,b))):0;
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | /*******************************************************************
|
|---|
| 538 | Case insensitive string comparison.
|
|---|
| 539 | ********************************************************************/
|
|---|
| 540 |
|
|---|
| 541 | int strcasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b)
|
|---|
| 542 | {
|
|---|
| 543 | smb_ucs2_t cpa, cpb;
|
|---|
| 544 |
|
|---|
| 545 | while ((*COPY_UCS2_CHAR(&cpb,b)) && toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb)) {
|
|---|
| 546 | a++;
|
|---|
| 547 | b++;
|
|---|
| 548 | }
|
|---|
| 549 | return (tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b))));
|
|---|
| 550 | }
|
|---|
| 551 |
|
|---|
| 552 | /*******************************************************************
|
|---|
| 553 | Case insensitive string comparison, length limited.
|
|---|
| 554 | ********************************************************************/
|
|---|
| 555 |
|
|---|
| 556 | int strncasecmp_w(const smb_ucs2_t *a, const smb_ucs2_t *b, size_t len)
|
|---|
| 557 | {
|
|---|
| 558 | smb_ucs2_t cpa, cpb;
|
|---|
| 559 | size_t n = 0;
|
|---|
| 560 |
|
|---|
| 561 | while ((n < len) && *COPY_UCS2_CHAR(&cpb,b) && (toupper_w(*(COPY_UCS2_CHAR(&cpa,a))) == toupper_w(cpb))) {
|
|---|
| 562 | a++;
|
|---|
| 563 | b++;
|
|---|
| 564 | n++;
|
|---|
| 565 | }
|
|---|
| 566 | return (len - n)?(tolower_w(*(COPY_UCS2_CHAR(&cpa,a))) - tolower_w(*(COPY_UCS2_CHAR(&cpb,b)))):0;
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | /*******************************************************************
|
|---|
| 570 | Compare 2 strings.
|
|---|
| 571 | ********************************************************************/
|
|---|
| 572 |
|
|---|
| 573 | bool strequal_w(const smb_ucs2_t *s1, const smb_ucs2_t *s2)
|
|---|
| 574 | {
|
|---|
| 575 | if (s1 == s2) {
|
|---|
| 576 | return(True);
|
|---|
| 577 | }
|
|---|
| 578 | if (!s1 || !s2) {
|
|---|
| 579 | return(False);
|
|---|
| 580 | }
|
|---|
| 581 |
|
|---|
| 582 | return(strcasecmp_w(s1,s2)==0);
|
|---|
| 583 | }
|
|---|
| 584 |
|
|---|
| 585 | /*******************************************************************
|
|---|
| 586 | Compare 2 strings up to and including the nth char.
|
|---|
| 587 | ******************************************************************/
|
|---|
| 588 |
|
|---|
| 589 | bool strnequal_w(const smb_ucs2_t *s1,const smb_ucs2_t *s2,size_t n)
|
|---|
| 590 | {
|
|---|
| 591 | if (s1 == s2) {
|
|---|
| 592 | return(True);
|
|---|
| 593 | }
|
|---|
| 594 | if (!s1 || !s2 || !n) {
|
|---|
| 595 | return(False);
|
|---|
| 596 | }
|
|---|
| 597 |
|
|---|
| 598 | return(strncasecmp_w(s1,s2,n)==0);
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 | /*******************************************************************
|
|---|
| 602 | Duplicate string.
|
|---|
| 603 | ********************************************************************/
|
|---|
| 604 |
|
|---|
| 605 | smb_ucs2_t *strdup_w(const smb_ucs2_t *src)
|
|---|
| 606 | {
|
|---|
| 607 | return strndup_w(src, 0);
|
|---|
| 608 | }
|
|---|
| 609 |
|
|---|
| 610 | /* if len == 0 then duplicate the whole string */
|
|---|
| 611 |
|
|---|
| 612 | smb_ucs2_t *strndup_w(const smb_ucs2_t *src, size_t len)
|
|---|
| 613 | {
|
|---|
| 614 | smb_ucs2_t *dest;
|
|---|
| 615 |
|
|---|
| 616 | if (!len) {
|
|---|
| 617 | len = strlen_w(src);
|
|---|
| 618 | }
|
|---|
| 619 | dest = SMB_MALLOC_ARRAY(smb_ucs2_t, len + 1);
|
|---|
| 620 | if (!dest) {
|
|---|
| 621 | DEBUG(0,("strdup_w: out of memory!\n"));
|
|---|
| 622 | return NULL;
|
|---|
| 623 | }
|
|---|
| 624 |
|
|---|
| 625 | memcpy(dest, src, len * sizeof(smb_ucs2_t));
|
|---|
| 626 | dest[len] = 0;
|
|---|
| 627 | return dest;
|
|---|
| 628 | }
|
|---|
| 629 |
|
|---|
| 630 | /*******************************************************************
|
|---|
| 631 | Copy a string with max len.
|
|---|
| 632 | ********************************************************************/
|
|---|
| 633 |
|
|---|
| 634 | smb_ucs2_t *strncpy_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
|
|---|
| 635 | {
|
|---|
| 636 | smb_ucs2_t cp;
|
|---|
| 637 | size_t len;
|
|---|
| 638 |
|
|---|
| 639 | if (!dest || !src) {
|
|---|
| 640 | return NULL;
|
|---|
| 641 | }
|
|---|
| 642 |
|
|---|
| 643 | for (len = 0; (*COPY_UCS2_CHAR(&cp,(src+len))) && (len < max); len++) {
|
|---|
| 644 | cp = *COPY_UCS2_CHAR(dest+len,src+len);
|
|---|
| 645 | }
|
|---|
| 646 | cp = 0;
|
|---|
| 647 | for ( /*nothing*/ ; len < max; len++ ) {
|
|---|
| 648 | cp = *COPY_UCS2_CHAR(dest+len,&cp);
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | return dest;
|
|---|
| 652 | }
|
|---|
| 653 |
|
|---|
| 654 | /*******************************************************************
|
|---|
| 655 | Append a string of len bytes and add a terminator.
|
|---|
| 656 | ********************************************************************/
|
|---|
| 657 |
|
|---|
| 658 | smb_ucs2_t *strncat_w(smb_ucs2_t *dest, const smb_ucs2_t *src, const size_t max)
|
|---|
| 659 | {
|
|---|
| 660 | size_t start;
|
|---|
| 661 | size_t len;
|
|---|
| 662 | smb_ucs2_t z = 0;
|
|---|
| 663 |
|
|---|
| 664 | if (!dest || !src) {
|
|---|
| 665 | return NULL;
|
|---|
| 666 | }
|
|---|
| 667 |
|
|---|
| 668 | start = strlen_w(dest);
|
|---|
| 669 | len = strnlen_w(src, max);
|
|---|
| 670 |
|
|---|
| 671 | memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
|
|---|
| 672 | z = *COPY_UCS2_CHAR(dest+start+len,&z);
|
|---|
| 673 |
|
|---|
| 674 | return dest;
|
|---|
| 675 | }
|
|---|
| 676 |
|
|---|
| 677 | smb_ucs2_t *strcat_w(smb_ucs2_t *dest, const smb_ucs2_t *src)
|
|---|
| 678 | {
|
|---|
| 679 | size_t start;
|
|---|
| 680 | size_t len;
|
|---|
| 681 | smb_ucs2_t z = 0;
|
|---|
| 682 |
|
|---|
| 683 | if (!dest || !src) {
|
|---|
| 684 | return NULL;
|
|---|
| 685 | }
|
|---|
| 686 |
|
|---|
| 687 | start = strlen_w(dest);
|
|---|
| 688 | len = strlen_w(src);
|
|---|
| 689 |
|
|---|
| 690 | memcpy(&dest[start], src, len*sizeof(smb_ucs2_t));
|
|---|
| 691 | z = *COPY_UCS2_CHAR(dest+start+len,&z);
|
|---|
| 692 |
|
|---|
| 693 | return dest;
|
|---|
| 694 | }
|
|---|
| 695 |
|
|---|
| 696 |
|
|---|
| 697 | /*******************************************************************
|
|---|
| 698 | Replace any occurence of oldc with newc in unicode string.
|
|---|
| 699 | ********************************************************************/
|
|---|
| 700 |
|
|---|
| 701 | void string_replace_w(smb_ucs2_t *s, smb_ucs2_t oldc, smb_ucs2_t newc)
|
|---|
| 702 | {
|
|---|
| 703 | smb_ucs2_t cp;
|
|---|
| 704 |
|
|---|
| 705 | for(;*(COPY_UCS2_CHAR(&cp,s));s++) {
|
|---|
| 706 | if(cp==oldc) {
|
|---|
| 707 | COPY_UCS2_CHAR(s,&newc);
|
|---|
| 708 | }
|
|---|
| 709 | }
|
|---|
| 710 | }
|
|---|
| 711 |
|
|---|
| 712 | /*******************************************************************
|
|---|
| 713 | Trim unicode string.
|
|---|
| 714 | ********************************************************************/
|
|---|
| 715 |
|
|---|
| 716 | bool trim_string_w(smb_ucs2_t *s, const smb_ucs2_t *front,
|
|---|
| 717 | const smb_ucs2_t *back)
|
|---|
| 718 | {
|
|---|
| 719 | bool ret = False;
|
|---|
| 720 | size_t len, front_len, back_len;
|
|---|
| 721 |
|
|---|
| 722 | if (!s) {
|
|---|
| 723 | return False;
|
|---|
| 724 | }
|
|---|
| 725 |
|
|---|
| 726 | len = strlen_w(s);
|
|---|
| 727 |
|
|---|
| 728 | if (front && *front) {
|
|---|
| 729 | front_len = strlen_w(front);
|
|---|
| 730 | while (len && strncmp_w(s, front, front_len) == 0) {
|
|---|
| 731 | memmove(s, (s + front_len), (len - front_len + 1) * sizeof(smb_ucs2_t));
|
|---|
| 732 | len -= front_len;
|
|---|
| 733 | ret = True;
|
|---|
| 734 | }
|
|---|
| 735 | }
|
|---|
| 736 |
|
|---|
| 737 | if (back && *back) {
|
|---|
| 738 | back_len = strlen_w(back);
|
|---|
| 739 | while (len && strncmp_w((s + (len - back_len)), back, back_len) == 0) {
|
|---|
| 740 | s[len - back_len] = 0;
|
|---|
| 741 | len -= back_len;
|
|---|
| 742 | ret = True;
|
|---|
| 743 | }
|
|---|
| 744 | }
|
|---|
| 745 |
|
|---|
| 746 | return ret;
|
|---|
| 747 | }
|
|---|
| 748 |
|
|---|
| 749 | /*
|
|---|
| 750 | The *_wa() functions take a combination of 7 bit ascii
|
|---|
| 751 | and wide characters They are used so that you can use string
|
|---|
| 752 | functions combining C string constants with ucs2 strings
|
|---|
| 753 |
|
|---|
| 754 | The char* arguments must NOT be multibyte - to be completely sure
|
|---|
| 755 | of this only pass string constants */
|
|---|
| 756 |
|
|---|
| 757 | int strcmp_wa(const smb_ucs2_t *a, const char *b)
|
|---|
| 758 | {
|
|---|
| 759 | smb_ucs2_t cp = 0;
|
|---|
| 760 |
|
|---|
| 761 | while (*b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
|
|---|
| 762 | a++;
|
|---|
| 763 | b++;
|
|---|
| 764 | }
|
|---|
| 765 | return (*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b));
|
|---|
| 766 | }
|
|---|
| 767 |
|
|---|
| 768 | int strncmp_wa(const smb_ucs2_t *a, const char *b, size_t len)
|
|---|
| 769 | {
|
|---|
| 770 | smb_ucs2_t cp = 0;
|
|---|
| 771 | size_t n = 0;
|
|---|
| 772 |
|
|---|
| 773 | while ((n < len) && *b && *(COPY_UCS2_CHAR(&cp,a)) == UCS2_CHAR(*b)) {
|
|---|
| 774 | a++;
|
|---|
| 775 | b++;
|
|---|
| 776 | n++;
|
|---|
| 777 | }
|
|---|
| 778 | return (len - n)?(*(COPY_UCS2_CHAR(&cp,a)) - UCS2_CHAR(*b)):0;
|
|---|
| 779 | }
|
|---|
| 780 |
|
|---|
| 781 | smb_ucs2_t *strpbrk_wa(const smb_ucs2_t *s, const char *p)
|
|---|
| 782 | {
|
|---|
| 783 | smb_ucs2_t cp;
|
|---|
| 784 |
|
|---|
| 785 | while (*(COPY_UCS2_CHAR(&cp,s))) {
|
|---|
| 786 | int i;
|
|---|
| 787 | for (i=0; p[i] && cp != UCS2_CHAR(p[i]); i++)
|
|---|
| 788 | ;
|
|---|
| 789 | if (p[i]) {
|
|---|
| 790 | return (smb_ucs2_t *)s;
|
|---|
| 791 | }
|
|---|
| 792 | s++;
|
|---|
| 793 | }
|
|---|
| 794 | return NULL;
|
|---|
| 795 | }
|
|---|
| 796 |
|
|---|
| 797 | smb_ucs2_t *strstr_wa(const smb_ucs2_t *s, const char *ins)
|
|---|
| 798 | {
|
|---|
| 799 | smb_ucs2_t *r;
|
|---|
| 800 | size_t inslen;
|
|---|
| 801 |
|
|---|
| 802 | if (!s || !ins) {
|
|---|
| 803 | return NULL;
|
|---|
| 804 | }
|
|---|
| 805 |
|
|---|
| 806 | inslen = strlen(ins);
|
|---|
| 807 | r = (smb_ucs2_t *)s;
|
|---|
| 808 |
|
|---|
| 809 | while ((r = strchr_w(r, UCS2_CHAR(*ins)))) {
|
|---|
| 810 | if (strncmp_wa(r, ins, inslen) == 0)
|
|---|
| 811 | return r;
|
|---|
| 812 | r++;
|
|---|
| 813 | }
|
|---|
| 814 |
|
|---|
| 815 | return NULL;
|
|---|
| 816 | }
|
|---|
| 817 |
|
|---|
| 818 | /*************************************************************
|
|---|
| 819 | ascii only toupper - saves the need for smbd to be in C locale.
|
|---|
| 820 | *************************************************************/
|
|---|
| 821 |
|
|---|
| 822 | int toupper_ascii(int c)
|
|---|
| 823 | {
|
|---|
| 824 | smb_ucs2_t uc = toupper_w(UCS2_CHAR(c));
|
|---|
| 825 | return UCS2_TO_CHAR(uc);
|
|---|
| 826 | }
|
|---|
| 827 |
|
|---|
| 828 | /*************************************************************
|
|---|
| 829 | ascii only tolower - saves the need for smbd to be in C locale.
|
|---|
| 830 | *************************************************************/
|
|---|
| 831 |
|
|---|
| 832 | int tolower_ascii(int c)
|
|---|
| 833 | {
|
|---|
| 834 | smb_ucs2_t uc = tolower_w(UCS2_CHAR(c));
|
|---|
| 835 | return UCS2_TO_CHAR(uc);
|
|---|
| 836 | }
|
|---|
| 837 |
|
|---|
| 838 | /*************************************************************
|
|---|
| 839 | ascii only isupper - saves the need for smbd to be in C locale.
|
|---|
| 840 | *************************************************************/
|
|---|
| 841 |
|
|---|
| 842 | int isupper_ascii(int c)
|
|---|
| 843 | {
|
|---|
| 844 | return isupper_w(UCS2_CHAR(c));
|
|---|
| 845 | }
|
|---|
| 846 |
|
|---|
| 847 | /*************************************************************
|
|---|
| 848 | ascii only islower - saves the need for smbd to be in C locale.
|
|---|
| 849 | *************************************************************/
|
|---|
| 850 |
|
|---|
| 851 | int islower_ascii(int c)
|
|---|
| 852 | {
|
|---|
| 853 | return islower_w(UCS2_CHAR(c));
|
|---|
| 854 | }
|
|---|