rtlstr.c 63.6 KB
Newer Older
Juergen Schmied's avatar
Juergen Schmied committed
1
/*
2
 * Rtl string functions
Juergen Schmied's avatar
Juergen Schmied committed
3
 *
4 5
 * Copyright (C) 1996-1998 Marcus Meissner
 * Copyright (C) 2000      Alexandre Julliard
6
 * Copyright (C) 2003      Thomas Mertes
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Juergen Schmied's avatar
Juergen Schmied committed
21 22
 */

23 24
#include "config.h"

25
#include <assert.h>
26
#include <stdarg.h>
Juergen Schmied's avatar
Juergen Schmied committed
27 28
#include <stdlib.h>
#include <string.h>
29

30 31
#include "ntstatus.h"
#define WIN32_NO_STATUS
32
#include "windef.h"
33
#include "winnt.h"
34
#include "winternl.h"
35
#include "wine/unicode.h"
36
#include "wine/debug.h"
37
#include "ntdll_misc.h"
38

39
WINE_DEFAULT_DEBUG_CHANNEL(ntdll);
40

41 42
#define GUID_STRING_LENGTH    38

43
UINT NlsAnsiCodePage = 0;
44 45 46 47 48
BYTE NlsMbCodePageTag = 0;
BYTE NlsMbOemCodePageTag = 0;

static const union cptable *ansi_table;
static const union cptable *oem_table;
49 50
static const union cptable* unix_table; /* NULL if UTF8 */

51 52 53 54 55 56

/**************************************************************************
 *	__wine_init_codepages   (NTDLL.@)
 *
 * Set the code page once kernel32 is loaded. Should be done differently.
 */
57 58
void CDECL __wine_init_codepages( const union cptable *ansi, const union cptable *oem,
                                  const union cptable *ucp)
59 60 61
{
    ansi_table = ansi;
    oem_table = oem;
62
    unix_table = ucp;
63 64 65
    NlsAnsiCodePage = ansi->info.codepage;
}

66
int ntdll_umbstowcs(DWORD flags, const char* src, int srclen, WCHAR* dst, int dstlen)
67 68
{
#ifdef __APPLE__
69 70
    /* work around broken Mac OS X filesystem that enforces decomposed Unicode */
    if (!unix_table) flags |= MB_COMPOSITE;
71
#endif
72 73 74 75 76 77 78
    return (unix_table) ?
        wine_cp_mbstowcs( unix_table, flags, src, srclen, dst, dstlen ) :
        wine_utf8_mbstowcs( flags, src, srclen, dst, dstlen );
}

int ntdll_wcstoumbs(DWORD flags, const WCHAR* src, int srclen, char* dst, int dstlen,
                    const char* defchar, int *used )
79 80 81 82
{
    if (unix_table)
        return wine_cp_wcstombs( unix_table, flags, src, srclen, dst, dstlen, defchar, used );
    if (used) *used = 0;  /* all chars are valid for UTF-8 */
83
    return wine_utf8_wcstombs( flags, src, srclen, dst, dstlen );
84
}
85 86

/**************************************************************************
87 88 89 90 91 92 93 94 95 96 97
 *      RtlInitAnsiString   (NTDLL.@)
 *
 * Initializes a buffered ansi string.
 *
 * RETURNS
 *  Nothing.
 *
 * NOTES
 *  Assigns source to target->Buffer. The length of source is assigned to
 *  target->Length and target->MaximumLength. If source is NULL the length
 *  of source is assumed to be 0.
Juergen Schmied's avatar
Juergen Schmied committed
98
 */
99 100 101
void WINAPI RtlInitAnsiString(
    PANSI_STRING target, /* [I/O] Buffered ansi string to be initialized */
    PCSZ source)         /* [I]   '\0' terminated string used to initialize target */
102
{
103
    if ((target->Buffer = (PCHAR) source))
104 105 106 107 108 109 110
    {
        target->Length = strlen(source);
        target->MaximumLength = target->Length + 1;
    }
    else target->Length = target->MaximumLength = 0;
}

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
/**************************************************************************
 *      RtlInitAnsiStringEx   (NTDLL.@)
 *
 * Initializes a buffered ansi string.
 *
 * RETURNS
 *  An appropriate NTSTATUS value.
 *
 * NOTES
 *  Assigns source to target->Buffer. The length of source is assigned to
 *  target->Length and target->MaximumLength. If source is NULL the length
 *  of source is assumed to be 0.
 */
NTSTATUS WINAPI RtlInitAnsiStringEx(PANSI_STRING target, PCSZ source)
{
    if (source)
    {
        unsigned int len = strlen(source);
        if (len+1 > 0xffff)
            return STATUS_NAME_TOO_LONG;

        target->Buffer = (PCHAR) source;
        target->Length = len;
        target->MaximumLength = len + 1;
    }
    else
    {
        target->Buffer = NULL;
        target->Length = 0;
        target->MaximumLength = 0;
    }
    return STATUS_SUCCESS;
}
144

145
/**************************************************************************
146 147 148 149 150 151 152 153 154 155 156
 *      RtlInitString   (NTDLL.@)
 *
 * Initializes a buffered string.
 *
 * RETURNS
 *  Nothing.
 *
 * NOTES
 *  Assigns source to target->Buffer. The length of source is assigned to
 *  target->Length and target->MaximumLength. If source is NULL the length
 *  of source is assumed to be 0.
157
 */
158 159 160
void WINAPI RtlInitString(
    PSTRING target, /* [I/O] Buffered string to be initialized */
    PCSZ source)    /* [I]   '\0' terminated string used to initialize target */
161
{
162
    RtlInitAnsiString( target, source );
163 164 165
}


166 167 168 169 170
/**************************************************************************
 *	RtlFreeAnsiString   (NTDLL.@)
 */
void WINAPI RtlFreeAnsiString( PSTRING str )
{
171 172 173 174 175
    if (str->Buffer)
    {
        RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
        RtlZeroMemory( str, sizeof(*str) );
    }
176
}
177

Juergen Schmied's avatar
Juergen Schmied committed
178 179

/**************************************************************************
180
 *	RtlFreeOemString   (NTDLL.@)
Juergen Schmied's avatar
Juergen Schmied committed
181
 */
182
void WINAPI RtlFreeOemString( PSTRING str )
Juergen Schmied's avatar
Juergen Schmied committed
183
{
184 185 186
    RtlFreeAnsiString( str );
}

187

188 189 190 191 192 193 194 195 196 197 198 199
/**************************************************************************
 *	RtlCopyString   (NTDLL.@)
 */
void WINAPI RtlCopyString( STRING *dst, const STRING *src )
{
    if (src)
    {
        unsigned int len = min( src->Length, dst->MaximumLength );
        memcpy( dst->Buffer, src->Buffer, len );
        dst->Length = len;
    }
    else dst->Length = 0;
Juergen Schmied's avatar
Juergen Schmied committed
200 201
}

202

Juergen Schmied's avatar
Juergen Schmied committed
203
/**************************************************************************
204 205 206 207 208 209 210 211 212 213 214
 *      RtlInitUnicodeString   (NTDLL.@)
 *
 * Initializes a buffered unicode string.
 *
 * RETURNS
 *  Nothing.
 *
 * NOTES
 *  Assigns source to target->Buffer. The length of source is assigned to
 *  target->Length and target->MaximumLength. If source is NULL the length
 *  of source is assumed to be 0.
215
 */
216 217 218
void WINAPI RtlInitUnicodeString(
    PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
    PCWSTR source)          /* [I]   '\0' terminated unicode string used to initialize target */
219
{
220
    if ((target->Buffer = (PWSTR) source))
221
    {
222 223 224 225
        unsigned int length = strlenW(source) * sizeof(WCHAR);
        if (length > 0xfffc)
            length = 0xfffc;
        target->Length = length;
226 227 228 229 230
        target->MaximumLength = target->Length + sizeof(WCHAR);
    }
    else target->Length = target->MaximumLength = 0;
}

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
/**************************************************************************
 *      RtlInitUnicodeStringEx   (NTDLL.@)
 *
 * Initializes a buffered unicode string.
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. target is initialized.
 *  Failure: STATUS_NAME_TOO_LONG, if the source string is larger than 65532 bytes.
 *
 * NOTES
 *  Assigns source to target->Buffer. The length of source is assigned to
 *  target->Length and target->MaximumLength. If source is NULL the length
 *  of source is assumed to be 0.
 */
NTSTATUS WINAPI RtlInitUnicodeStringEx(
    PUNICODE_STRING target, /* [I/O] Buffered unicode string to be initialized */
    PCWSTR source)          /* [I]   '\0' terminated unicode string used to initialize target */
{
    if (source != NULL) {
        unsigned int len = strlenW(source) * sizeof(WCHAR);

        if (len > 0xFFFC) {
            return STATUS_NAME_TOO_LONG;
        } else {
            target->Length = len;
            target->MaximumLength = len + sizeof(WCHAR);
            target->Buffer = (PWSTR) source;
        } /* if */
    } else {
        target->Length = 0;
        target->MaximumLength = 0;
        target->Buffer = NULL;
    } /* if */
    return STATUS_SUCCESS;
}


269 270
/**************************************************************************
 *	RtlCreateUnicodeString   (NTDLL.@)
271 272 273 274 275 276
 *
 * Creates a UNICODE_STRING from a null-terminated Unicode string.
 *
 * RETURNS
 *     Success: TRUE
 *     Failure: FALSE
277 278 279 280
 */
BOOLEAN WINAPI RtlCreateUnicodeString( PUNICODE_STRING target, LPCWSTR src )
{
    int len = (strlenW(src) + 1) * sizeof(WCHAR);
281
    if (!(target->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) return FALSE;
282 283
    memcpy( target->Buffer, src, len );
    target->MaximumLength = len;
Dmitry Timoshkov's avatar
Dmitry Timoshkov committed
284
    target->Length = len - sizeof(WCHAR);
285
    return TRUE;
286 287 288
}


289 290
/**************************************************************************
 *	RtlCreateUnicodeStringFromAsciiz   (NTDLL.@)
291 292 293 294 295 296
 *
 * Creates a UNICODE_STRING from a null-terminated Ascii string.
 *
 * RETURNS
 *     Success: TRUE
 *     Failure: FALSE
297 298 299 300 301 302 303
 */
BOOLEAN WINAPI RtlCreateUnicodeStringFromAsciiz( PUNICODE_STRING target, LPCSTR src )
{
    STRING ansi;
    RtlInitAnsiString( &ansi, src );
    return !RtlAnsiStringToUnicodeString( target, &ansi, TRUE );
}
304 305 306


/**************************************************************************
307
 *	RtlFreeUnicodeString   (NTDLL.@)
308 309 310 311 312 313
 *
 * Frees a UNICODE_STRING created with RtlCreateUnicodeString() or 
 * RtlCreateUnicodeStringFromAsciiz().
 *
 * RETURNS
 *     nothing
Juergen Schmied's avatar
Juergen Schmied committed
314
 */
315
void WINAPI RtlFreeUnicodeString( PUNICODE_STRING str )
Juergen Schmied's avatar
Juergen Schmied committed
316
{
317 318 319 320 321
    if (str->Buffer)
    {
        RtlFreeHeap( GetProcessHeap(), 0, str->Buffer );
        RtlZeroMemory( str, sizeof(*str) );
    }
Juergen Schmied's avatar
Juergen Schmied committed
322
}
323

324

Juergen Schmied's avatar
Juergen Schmied committed
325
/**************************************************************************
326
 *	RtlCopyUnicodeString   (NTDLL.@)
327 328 329 330 331
 *
 * Copies from one UNICODE_STRING to another.
 *
 * RETURNS
 *     nothing
Juergen Schmied's avatar
Juergen Schmied committed
332
 */
333
void WINAPI RtlCopyUnicodeString( UNICODE_STRING *dst, const UNICODE_STRING *src )
Juergen Schmied's avatar
Juergen Schmied committed
334
{
335 336 337 338 339
    if (src)
    {
        unsigned int len = min( src->Length, dst->MaximumLength );
        memcpy( dst->Buffer, src->Buffer, len );
        dst->Length = len;
340
        /* append terminating '\0' if enough space */
341 342 343 344 345
        if (len < dst->MaximumLength) dst->Buffer[len / sizeof(WCHAR)] = 0;
    }
    else dst->Length = 0;
}

346

347 348 349
/**************************************************************************
 *      RtlDuplicateUnicodeString   (NTDLL.@)
 *
350
 * Duplicates a unicode string.
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
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. destination contains the duplicated unicode string.
 *  Failure: STATUS_INVALID_PARAMETER, if one of the parameters is illegal.
 *           STATUS_NO_MEMORY, if the allocation fails.
 *
 * NOTES
 *  For add_nul there are several possible values:
 *  0 = destination will not be '\0' terminated,
 *  1 = destination will be '\0' terminated,
 *  3 = like 1 but for an empty source string produce '\0' terminated empty
 *     Buffer instead of assigning NULL to the Buffer.
 *  Other add_nul values are invalid.
 */
NTSTATUS WINAPI RtlDuplicateUnicodeString(
    int add_nul,                  /* [I] flag */
    const UNICODE_STRING *source, /* [I] Unicode string to be duplicated */
    UNICODE_STRING *destination)  /* [O] destination for the duplicated unicode string */
{
    if (source == NULL || destination == NULL ||
        source->Length > source->MaximumLength ||
        (source->Length == 0 && source->MaximumLength > 0 && source->Buffer == NULL) ||
        add_nul == 2 || add_nul >= 4 || add_nul < 0) {
        return STATUS_INVALID_PARAMETER;
    } else {
        if (source->Length == 0 && add_nul != 3) {
            destination->Length = 0;
            destination->MaximumLength = 0;
            destination->Buffer = NULL;
        } else {
            unsigned int destination_max_len = source->Length;

            if (add_nul) {
                destination_max_len += sizeof(WCHAR);
            } /* if */
386
            destination->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, destination_max_len);
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
            if (destination->Buffer == NULL) {
                return STATUS_NO_MEMORY;
            } else {
                memcpy(destination->Buffer, source->Buffer, source->Length);
                destination->Length = source->Length;
                destination->MaximumLength = source->Length;
                /* append terminating '\0' if enough space */
                if (add_nul) {
                    destination->MaximumLength = destination_max_len;
                    destination->Buffer[destination->Length / sizeof(WCHAR)] = 0;
                } /* if */
            } /* if */
        } /* if */
    } /* if */
    return STATUS_SUCCESS;
}


405 406
/**************************************************************************
 *	RtlEraseUnicodeString   (NTDLL.@)
407 408 409 410 411
 *
 * Overwrites a UNICODE_STRING with zeros.
 *
 * RETURNS
 *     nothing
412 413 414 415 416 417 418 419
 */
void WINAPI RtlEraseUnicodeString( UNICODE_STRING *str )
{
    if (str->Buffer)
    {
        memset( str->Buffer, 0, str->MaximumLength );
        str->Length = 0;
    }
Juergen Schmied's avatar
Juergen Schmied committed
420 421
}

422

423
/*
424
    COMPARISON FUNCTIONS
425 426
*/

427

428 429
/******************************************************************************
 *	RtlCompareString   (NTDLL.@)
Juergen Schmied's avatar
Juergen Schmied committed
430
 */
431
LONG WINAPI RtlCompareString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
Juergen Schmied's avatar
Juergen Schmied committed
432
{
433 434 435 436 437 438 439 440 441 442
    unsigned int len;
    LONG ret = 0;
    LPCSTR p1, p2;

    len = min(s1->Length, s2->Length);
    p1 = s1->Buffer;
    p2 = s2->Buffer;

    if (CaseInsensitive)
    {
443
        while (!ret && len--) ret = RtlUpperChar(*p1++) - RtlUpperChar(*p2++);
444 445 446 447 448 449 450 451
    }
    else
    {
        while (!ret && len--) ret = *p1++ - *p2++;
    }
    if (!ret) ret = s1->Length - s2->Length;
    return ret;
}
452 453


454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
/******************************************************************************
 *	RtlCompareUnicodeString   (NTDLL.@)
 */
LONG WINAPI RtlCompareUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
                                     BOOLEAN CaseInsensitive )
{
    unsigned int len;
    LONG ret = 0;
    LPCWSTR p1, p2;

    len = min(s1->Length, s2->Length) / sizeof(WCHAR);
    p1 = s1->Buffer;
    p2 = s2->Buffer;

    if (CaseInsensitive)
    {
        while (!ret && len--) ret = toupperW(*p1++) - toupperW(*p2++);
    }
    else
    {
        while (!ret && len--) ret = *p1++ - *p2++;
    }
    if (!ret) ret = s1->Length - s2->Length;
    return ret;
478 479
}

480 481 482

/**************************************************************************
 *	RtlEqualString   (NTDLL.@)
483 484 485 486 487 488 489 490 491 492
 *
 * Determine if two strings are equal.
 *
 * PARAMS
 *  s1              [I] Source string
 *  s2              [I] String to compare to s1
 *  CaseInsensitive [I] TRUE = Case insensitive, FALSE = Case sensitive
 *
 * RETURNS
 *  Non-zero if s1 is equal to s2, 0 otherwise.
493
 */
494
BOOLEAN WINAPI RtlEqualString( const STRING *s1, const STRING *s2, BOOLEAN CaseInsensitive )
495
{
496 497 498
    if (s1->Length != s2->Length) return FALSE;
    return !RtlCompareString( s1, s2, CaseInsensitive );
}
499 500


501 502
/**************************************************************************
 *	RtlEqualUnicodeString   (NTDLL.@)
503 504
 *
 * Unicode version of RtlEqualString.
505 506 507 508 509 510 511 512
 */
BOOLEAN WINAPI RtlEqualUnicodeString( const UNICODE_STRING *s1, const UNICODE_STRING *s2,
                                      BOOLEAN CaseInsensitive )
{
    if (s1->Length != s2->Length) return FALSE;
    return !RtlCompareUnicodeString( s1, s2, CaseInsensitive );
}

513

514 515 516
/**************************************************************************
 *	RtlPrefixString   (NTDLL.@)
 *
517 518 519 520 521 522 523 524 525
 * Determine if one string is a prefix of another.
 *
 * PARAMS
 *  s1          [I] Prefix to look for in s2
 *  s2          [I] String that may contain s1 as a prefix
 *  ignore_case [I] TRUE = Case insensitive, FALSE = Case sensitive
 *
 * RETURNS
 *  TRUE if s2 contains s1 as a prefix, FALSE otherwise.
526 527 528 529 530 531 532 533 534
 */
BOOLEAN WINAPI RtlPrefixString( const STRING *s1, const STRING *s2, BOOLEAN ignore_case )
{
    unsigned int i;

    if (s1->Length > s2->Length) return FALSE;
    if (ignore_case)
    {
        for (i = 0; i < s1->Length; i++)
535
            if (RtlUpperChar(s1->Buffer[i]) != RtlUpperChar(s2->Buffer[i])) return FALSE;
536 537 538 539 540 541 542
    }
    else
    {
        for (i = 0; i < s1->Length; i++)
            if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
    }
    return TRUE;
Juergen Schmied's avatar
Juergen Schmied committed
543 544
}

545 546 547 548

/**************************************************************************
 *	RtlPrefixUnicodeString   (NTDLL.@)
 *
549
 * Unicode version of RtlPrefixString.
550 551 552 553 554 555 556 557 558 559 560
 */
BOOLEAN WINAPI RtlPrefixUnicodeString( const UNICODE_STRING *s1,
                                       const UNICODE_STRING *s2,
                                       BOOLEAN ignore_case )
{
    unsigned int i;

    if (s1->Length > s2->Length) return FALSE;
    if (ignore_case)
    {
        for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
561
            if (toupperW(s1->Buffer[i]) != toupperW(s2->Buffer[i])) return FALSE;
562 563 564 565 566 567 568 569 570 571
    }
    else
    {
        for (i = 0; i < s1->Length / sizeof(WCHAR); i++)
            if (s1->Buffer[i] != s2->Buffer[i]) return FALSE;
    }
    return TRUE;
}


572 573 574 575 576 577 578 579 580 581 582 583 584
/**************************************************************************
 *	RtlEqualComputerName   (NTDLL.@)
 *
 * Determine if two computer names are the same.
 *
 * PARAMS
 *  left  [I] First computer name
 *  right [I] Second computer name
 *
 * RETURNS
 *  0 if the names are equal, non-zero otherwise.
 *
 * NOTES
585
 *  The comparison is case insensitive.
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
 */
NTSTATUS WINAPI RtlEqualComputerName(const UNICODE_STRING *left,
                                     const UNICODE_STRING *right)
{
    NTSTATUS ret;
    STRING upLeft, upRight;

    if (!(ret = RtlUpcaseUnicodeStringToOemString( &upLeft, left, TRUE )))
    {
       if (!(ret = RtlUpcaseUnicodeStringToOemString( &upRight, right, TRUE )))
       {
         ret = RtlEqualString( &upLeft, &upRight, FALSE );
         RtlFreeOemString( &upRight );
       }
       RtlFreeOemString( &upLeft );
    }
    return ret;
}

605

606 607 608 609 610 611 612 613 614 615 616 617 618
/**************************************************************************
 *	RtlEqualDomainName   (NTDLL.@)
 *
 * Determine if two domain names are the same.
 *
 * PARAMS
 *  left  [I] First domain name
 *  right [I] Second domain name
 *
 * RETURNS
 *  0 if the names are equal, non-zero otherwise.
 *
 * NOTES
619
 *  The comparison is case insensitive.
620 621 622 623 624 625 626
 */
NTSTATUS WINAPI RtlEqualDomainName(const UNICODE_STRING *left,
                                   const UNICODE_STRING *right)
{
    return RtlEqualComputerName(left, right);
}

627

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
/**************************************************************************
 *      RtlAnsiCharToUnicodeChar   (NTDLL.@)
 *
 * Converts the first ansi character to a unicode character.
 *
 * PARAMS
 *  ansi [I/O] Pointer to the ansi string.
 *
 * RETURNS
 *  Unicode representation of the first character in the ansi string.
 *
 * NOTES
 *  Upon successful completion, the char pointer ansi points to is
 *  incremented by the size of the character.
 */
WCHAR WINAPI RtlAnsiCharToUnicodeChar(LPSTR *ansi)
{
    WCHAR str;
    DWORD charSize = sizeof(CHAR);

648
    if (wine_is_dbcs_leadbyte(ansi_table, **ansi))
649 650 651 652 653 654 655 656
        charSize++;

    RtlMultiByteToUnicodeN(&str, sizeof(WCHAR), NULL, *ansi, charSize);
    *ansi += charSize;

    return str;
}

657
/*
658 659
        COPY BETWEEN ANSI_STRING or UNICODE_STRING
        there is no parameter checking, it just crashes
660 661
*/

662

Juergen Schmied's avatar
Juergen Schmied committed
663
/**************************************************************************
664 665
 *      RtlAnsiStringToUnicodeString   (NTDLL.@)
 *
666
 * Converts an ansi string to a unicode string.
667 668 669 670 671 672
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. uni contains the converted string
 *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
 *           STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
 *           STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
673
 *
674
 * NOTES
675
 *  This function always writes a terminating '\0'.
Juergen Schmied's avatar
Juergen Schmied committed
676
 */
677 678 679 680
NTSTATUS WINAPI RtlAnsiStringToUnicodeString(
    PUNICODE_STRING uni, /* [I/O] Destination for the unicode string */
    PCANSI_STRING ansi,  /* [I]   Ansi string to be converted */
    BOOLEAN doalloc)     /* [I]   TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
Juergen Schmied's avatar
Juergen Schmied committed
681
{
682
    DWORD total = RtlAnsiStringToUnicodeSize( ansi );
683 684

    if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
685
    uni->Length = total - sizeof(WCHAR);
686 687 688
    if (doalloc)
    {
        uni->MaximumLength = total;
689
        if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
690
            return STATUS_NO_MEMORY;
691 692 693
    }
    else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;

694 695
    RtlMultiByteToUnicodeN( uni->Buffer, uni->Length, NULL, ansi->Buffer, ansi->Length );
    uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
696
    return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
697
}
698

699

Juergen Schmied's avatar
Juergen Schmied committed
700
/**************************************************************************
701
 *	RtlOemStringToUnicodeString   (NTDLL.@)
702
 *
703
 * Converts an oem string to a unicode string.
704 705 706 707 708 709 710
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. uni contains the converted string
 *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
 *           STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
 *           STATUS_INVALID_PARAMETER_2, if the unicode string would be larger than 65535.
 *
711
 * NOTES
712
 *  This function always writes a terminating '\0'.
Juergen Schmied's avatar
Juergen Schmied committed
713
 */
714 715 716 717
NTSTATUS WINAPI RtlOemStringToUnicodeString(
    UNICODE_STRING *uni, /* [I/O] Destination for the unicode string */
    const STRING *oem,   /* [I]   Oem string to be converted */
    BOOLEAN doalloc)     /* [I]   TRUE=Allocate new buffer for uni, FALSE=Use existing buffer */
Juergen Schmied's avatar
Juergen Schmied committed
718
{
719
    DWORD total = RtlOemStringToUnicodeSize( oem );
720 721

    if (total > 0xffff) return STATUS_INVALID_PARAMETER_2;
722
    uni->Length = total - sizeof(WCHAR);
723 724 725
    if (doalloc)
    {
        uni->MaximumLength = total;
726
        if (!(uni->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, total )))
727
            return STATUS_NO_MEMORY;
728 729 730
    }
    else if (total > uni->MaximumLength) return STATUS_BUFFER_OVERFLOW;

731 732
    RtlOemToUnicodeN( uni->Buffer, uni->Length, NULL, oem->Buffer, oem->Length );
    uni->Buffer[uni->Length / sizeof(WCHAR)] = 0;
733
    return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
734 735
}

736

Juergen Schmied's avatar
Juergen Schmied committed
737
/**************************************************************************
738
 *	RtlUnicodeStringToAnsiString   (NTDLL.@)
739
 *
740
 * Converts a unicode string to an ansi string.
741 742 743
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. ansi contains the converted string
744 745
 *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and ansi is too small.
 *           STATUS_NO_MEMORY, if doalloc is TRUE and the allocation fails.
746
 *
747
 * NOTES
748 749
 *  This function always writes a terminating '\0'.
 *  It performs a partial copy if ansi is too small.
Juergen Schmied's avatar
Juergen Schmied committed
750
 */
751
NTSTATUS WINAPI RtlUnicodeStringToAnsiString(
752
    STRING *ansi,              /* [I/O] Destination for the ansi string */
753 754
    const UNICODE_STRING *uni, /* [I]   Unicode string to be converted */
    BOOLEAN doalloc)           /* [I]   TRUE=Allocate new buffer for ansi, FALSE=Use existing buffer */
Juergen Schmied's avatar
Juergen Schmied committed
755
{
756 757 758
    NTSTATUS ret = STATUS_SUCCESS;
    DWORD len = RtlUnicodeStringToAnsiSize( uni );

759
    ansi->Length = len - 1;
760 761
    if (doalloc)
    {
762
        ansi->MaximumLength = len;
763
        if (!(ansi->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
764
            return STATUS_NO_MEMORY;
765
    }
766
    else if (ansi->MaximumLength < len)
767 768 769 770 771 772
    {
        if (!ansi->MaximumLength) return STATUS_BUFFER_OVERFLOW;
        ansi->Length = ansi->MaximumLength - 1;
        ret = STATUS_BUFFER_OVERFLOW;
    }

773
    RtlUnicodeToMultiByteN( ansi->Buffer, ansi->Length, NULL, uni->Buffer, uni->Length );
774 775
    ansi->Buffer[ansi->Length] = 0;
    return ret;
Juergen Schmied's avatar
Juergen Schmied committed
776 777
}

778

Juergen Schmied's avatar
Juergen Schmied committed
779
/**************************************************************************
780
 *	RtlUnicodeStringToOemString   (NTDLL.@)
781
 *
782
 * Converts a Rtl Unicode string to an OEM string.
783 784 785 786 787 788 789 790
 *
 * PARAMS
 *  oem     [O] Destination for OEM string
 *  uni     [I] Source Unicode string
 *  doalloc [I] TRUE=Allocate new buffer for oem,FALSE=Use existing buffer
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. oem contains the converted string
791 792
 *  Failure: STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and oem is too small.
 *           STATUS_NO_MEMORY, if doalloc is TRUE and allocation fails.
793
 *
794
 * NOTES
795
 *   If doalloc is TRUE, the length allocated is uni->Length + 1.
796
 *   This function always '\0' terminates the string returned.
Juergen Schmied's avatar
Juergen Schmied committed
797
 */
798 799 800
NTSTATUS WINAPI RtlUnicodeStringToOemString( STRING *oem,
                                             const UNICODE_STRING *uni,
                                             BOOLEAN doalloc )
Juergen Schmied's avatar
Juergen Schmied committed
801
{
802 803 804
    NTSTATUS ret = STATUS_SUCCESS;
    DWORD len = RtlUnicodeStringToOemSize( uni );

805
    oem->Length = len - 1;
806 807
    if (doalloc)
    {
808
        oem->MaximumLength = len;
809
        if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
810
            return STATUS_NO_MEMORY;
811
    }
812
    else if (oem->MaximumLength < len)
813 814 815 816 817 818
    {
        if (!oem->MaximumLength) return STATUS_BUFFER_OVERFLOW;
        oem->Length = oem->MaximumLength - 1;
        ret = STATUS_BUFFER_OVERFLOW;
    }

819
    RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, uni->Buffer, uni->Length );
820 821
    oem->Buffer[oem->Length] = 0;
    return ret;
Juergen Schmied's avatar
Juergen Schmied committed
822 823
}

824

Juergen Schmied's avatar
Juergen Schmied committed
825
/**************************************************************************
826
 *	RtlMultiByteToUnicodeN   (NTDLL.@)
827
 *
828 829 830 831 832
 * Converts a multi-byte string to a Unicode string.
 *
 * RETURNS
 *  NTSTATUS code
 *
833
 * NOTES
834
 *  Performs a partial copy if dst is too small.
Juergen Schmied's avatar
Juergen Schmied committed
835
 */
836 837
NTSTATUS WINAPI RtlMultiByteToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
                                        LPCSTR src, DWORD srclen )
Juergen Schmied's avatar
Juergen Schmied committed
838
{
839

840
    int ret = wine_cp_mbstowcs( ansi_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
841
    if (reslen)
842
        *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
843 844
    return STATUS_SUCCESS;
}
845 846


847 848
/**************************************************************************
 *	RtlOemToUnicodeN   (NTDLL.@)
849 850 851 852 853
 *
 * Converts a multi-byte string in the OEM code page to a Unicode string.
 *
 * RETURNS
 *  NTSTATUS code
854 855 856 857
 */
NTSTATUS WINAPI RtlOemToUnicodeN( LPWSTR dst, DWORD dstlen, LPDWORD reslen,
                                  LPCSTR src, DWORD srclen )
{
858
    int ret = wine_cp_mbstowcs( oem_table, 0, src, srclen, dst, dstlen/sizeof(WCHAR) );
859
    if (reslen)
860
        *reslen = (ret >= 0) ? ret*sizeof(WCHAR) : dstlen; /* overflow -> we filled up to dstlen */
861 862 863
    return STATUS_SUCCESS;
}

864

865 866
/**************************************************************************
 *	RtlUnicodeToMultiByteN   (NTDLL.@)
867 868 869 870 871
 *
 * Converts a Unicode string to a multi-byte string in the ANSI code page.
 *
 * RETURNS
 *  NTSTATUS code
872 873 874 875
 */
NTSTATUS WINAPI RtlUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
                                        LPCWSTR src, DWORD srclen )
{
876
    int ret = wine_cp_wcstombs( ansi_table, 0, src, srclen / sizeof(WCHAR),
877
                                dst, dstlen, NULL, NULL );
878
    if (reslen)
879
        *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
880
    return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
881 882
}

883

884
/**************************************************************************
885
 *	RtlUnicodeToOemN   (NTDLL.@)
886 887 888 889 890
 *
 * Converts a Unicode string to a multi-byte string in the OEM code page.
 *
 * RETURNS
 *  NTSTATUS code
891 892 893 894
 */
NTSTATUS WINAPI RtlUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
                                  LPCWSTR src, DWORD srclen )
{
895
    int ret = wine_cp_wcstombs( oem_table, 0, src, srclen / sizeof(WCHAR),
896
                                dst, dstlen, NULL, NULL );
897
    if (reslen)
898
        *reslen = (ret >= 0) ? ret : dstlen; /* overflow -> we filled up to dstlen */
899
    return STATUS_SUCCESS;
900
}
Juergen Schmied's avatar
Juergen Schmied committed
901

902

903
/*
904
     CASE CONVERSIONS
905
*/
906

907 908 909

/**************************************************************************
 *	RtlUpperChar   (NTDLL.@)
910
 *
911
 * Converts an Ascii character to uppercase.
912 913 914 915 916 917
 *
 * PARAMS
 *  ch [I] Character to convert
 *
 * RETURNS
 *  The uppercase character value.
918 919 920 921 922
 *
 * NOTES
 *  For the input characters from 'a' .. 'z' it returns 'A' .. 'Z'.
 *  All other input characters are returned unchanged. The locale and
 *  multibyte characters are not taken into account (as native DLL).
923 924 925 926 927 928 929
 */
CHAR WINAPI RtlUpperChar( CHAR ch )
{
    if (ch >= 'a' && ch <= 'z') {
        return ch - 'a' + 'A';
    } else {
        return ch;
930
    } /* if */
931 932 933
}


Juergen Schmied's avatar
Juergen Schmied committed
934
/**************************************************************************
935
 *	RtlUpperString   (NTDLL.@)
936
 *
937
 * Converts an Ascii string to uppercase.
938 939 940 941 942 943 944
 *
 * PARAMS
 *  dst [O] Destination for converted string
 *  src [I] Source string to convert
 *
 * RETURNS
 *  Nothing.
945 946 947 948 949 950 951
 *
 * NOTES
 *  For the src characters from 'a' .. 'z' it assigns 'A' .. 'Z' to dst.
 *  All other src characters are copied unchanged to dst. The locale and
 *  multibyte characters are not taken into account (as native DLL).
 *  The number of character copied is the minimum of src->Length and
 *  the dst->MaximumLength.
Juergen Schmied's avatar
Juergen Schmied committed
952
 */
953
void WINAPI RtlUpperString( STRING *dst, const STRING *src )
Juergen Schmied's avatar
Juergen Schmied committed
954
{
955
    unsigned int i, len = min(src->Length, dst->MaximumLength);
956

957
    for (i = 0; i < len; i++) dst->Buffer[i] = RtlUpperChar(src->Buffer[i]);
958 959
    dst->Length = len;
}
960 961


962 963
/**************************************************************************
 *	RtlUpcaseUnicodeChar   (NTDLL.@)
964
 *
965
 * Converts a Unicode character to uppercase.
966 967 968 969 970 971
 *
 * PARAMS
 *  wch [I] Character to convert
 *
 * RETURNS
 *  The uppercase character value.
972 973 974 975 976 977
 */
WCHAR WINAPI RtlUpcaseUnicodeChar( WCHAR wch )
{
    return toupperW(wch);
}

978

979 980 981
/**************************************************************************
 *	RtlDowncaseUnicodeChar   (NTDLL.@)
 *
982
 * Converts a Unicode character to lowercase.
983 984 985 986 987 988 989 990 991 992 993
 *
 * PARAMS
 *  wch [I] Character to convert
 *
 * RETURNS
 *  The lowercase character value.
 */
WCHAR WINAPI RtlDowncaseUnicodeChar(WCHAR wch)
{
    return tolowerW(wch);
}
994

995

996 997 998
/**************************************************************************
 *	RtlUpcaseUnicodeString   (NTDLL.@)
 *
999
 * Converts a Unicode string to uppercase.
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
 *
 * PARAMS
 *  dest    [O] Destination for converted string
 *  src     [I] Source string to convert
 *  doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. dest contains the converted string.
 *  Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
 *           STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
 *
 * NOTES
1012 1013
 *  dest is never '\0' terminated because it may be equal to src, and src
 *  might not be '\0' terminated. dest->Length is only set upon success.
1014 1015 1016
 */
NTSTATUS WINAPI RtlUpcaseUnicodeString( UNICODE_STRING *dest,
                                        const UNICODE_STRING *src,
1017
                                        BOOLEAN doalloc)
1018 1019 1020 1021 1022 1023
{
    DWORD i, len = src->Length;

    if (doalloc)
    {
        dest->MaximumLength = len;
1024
        if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1025
            return STATUS_NO_MEMORY;
1026 1027 1028 1029 1030 1031
    }
    else if (len > dest->MaximumLength) return STATUS_BUFFER_OVERFLOW;

    for (i = 0; i < len/sizeof(WCHAR); i++) dest->Buffer[i] = toupperW(src->Buffer[i]);
    dest->Length = len;
    return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
1032 1033
}

1034

1035 1036 1037
/**************************************************************************
 *	RtlDowncaseUnicodeString   (NTDLL.@)
 *
1038
 * Converts a Unicode string to lowercase.
1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
 *
 * PARAMS
 *  dest    [O] Destination for converted string
 *  src     [I] Source string to convert
 *  doalloc [I] TRUE=Allocate a buffer for dest if it doesn't have one
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. dest contains the converted string.
 *  Failure: STATUS_NO_MEMORY, if doalloc is TRUE and memory allocation fails, or
 *           STATUS_BUFFER_OVERFLOW, if doalloc is FALSE and dest is too small.
 *
 * NOTES
1051 1052
 *  dest is never '\0' terminated because it may be equal to src, and src
 *  might not be '\0' terminated. dest->Length is only set upon success.
1053 1054
 */
NTSTATUS WINAPI RtlDowncaseUnicodeString(
1055 1056 1057
    UNICODE_STRING *dest,
    const UNICODE_STRING *src,
    BOOLEAN doalloc)
1058 1059 1060 1061 1062 1063
{
    DWORD i;
    DWORD len = src->Length;

    if (doalloc) {
        dest->MaximumLength = len;
1064
        if (!(dest->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len ))) {
1065 1066
            return STATUS_NO_MEMORY;
        } /* if */
1067
    } else if (len > dest->MaximumLength) {
1068
        return STATUS_BUFFER_OVERFLOW;
1069 1070 1071
    } /* if */

    for (i = 0; i < len/sizeof(WCHAR); i++) {
1072
        dest->Buffer[i] = tolowerW(src->Buffer[i]);
1073 1074 1075 1076 1077 1078
    } /* for */
    dest->Length = len;
    return STATUS_SUCCESS;
}


Juergen Schmied's avatar
Juergen Schmied committed
1079
/**************************************************************************
1080 1081
 *	RtlUpcaseUnicodeStringToAnsiString   (NTDLL.@)
 *
1082 1083 1084 1085 1086
 * Converts a Unicode string to the equivalent ANSI upper-case representation.
 *
 * RETURNS
 *  NTSTATUS code
 *
1087 1088
 * NOTES
 *  writes terminating 0
Juergen Schmied's avatar
Juergen Schmied committed
1089
 */
1090 1091 1092
NTSTATUS WINAPI RtlUpcaseUnicodeStringToAnsiString( STRING *dst,
                                                    const UNICODE_STRING *src,
                                                    BOOLEAN doalloc )
Juergen Schmied's avatar
Juergen Schmied committed
1093
{
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
    NTSTATUS ret;
    UNICODE_STRING upcase;

    if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
    {
        ret = RtlUnicodeStringToAnsiString( dst, &upcase, doalloc );
        RtlFreeUnicodeString( &upcase );
    }
    return ret;
}
1104 1105


1106 1107 1108
/**************************************************************************
 *	RtlUpcaseUnicodeStringToOemString   (NTDLL.@)
 *
1109 1110 1111 1112 1113 1114
 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
 * stored in STRING format.
 *
 * RETURNS
 *  NTSTATUS code
 *
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131
 * NOTES
 *  writes terminating 0
 */
NTSTATUS WINAPI RtlUpcaseUnicodeStringToOemString( STRING *dst,
                                                   const UNICODE_STRING *src,
                                                   BOOLEAN doalloc )
{
    NTSTATUS ret;
    UNICODE_STRING upcase;

    if (!(ret = RtlUpcaseUnicodeString( &upcase, src, TRUE )))
    {
        ret = RtlUnicodeStringToOemString( dst, &upcase, doalloc );
        RtlFreeUnicodeString( &upcase );
    }
    return ret;
}
1132 1133


1134 1135 1136
/**************************************************************************
 *	RtlUpcaseUnicodeStringToCountedOemString   (NTDLL.@)
 *
1137 1138 1139 1140 1141 1142
 * Converts a UNICODE_STRING to the equivalent OEM upper-case representation
 * stored in STRING format.
 *
 * RETURNS
 *  NTSTATUS code
 *
1143 1144 1145 1146 1147 1148 1149 1150 1151
 * NOTES
 *  Same as RtlUpcaseUnicodeStringToOemString but doesn't write terminating null
 */
NTSTATUS WINAPI RtlUpcaseUnicodeStringToCountedOemString( STRING *oem,
                                                          const UNICODE_STRING *uni,
                                                          BOOLEAN doalloc )
{
    NTSTATUS ret;
    UNICODE_STRING upcase;
1152
    WCHAR tmp[32];
1153

1154 1155 1156 1157 1158 1159
    upcase.Buffer = tmp;
    upcase.MaximumLength = sizeof(tmp);
    ret = RtlUpcaseUnicodeString( &upcase, uni, FALSE );
    if (ret == STATUS_BUFFER_OVERFLOW) ret = RtlUpcaseUnicodeString( &upcase, uni, TRUE );

    if (!ret)
1160 1161 1162 1163 1164 1165
    {
        DWORD len = RtlUnicodeStringToOemSize( &upcase ) - 1;
        oem->Length = len;
        if (doalloc)
        {
            oem->MaximumLength = len;
1166
            if (!(oem->Buffer = RtlAllocateHeap( GetProcessHeap(), 0, len )))
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
            {
                ret = STATUS_NO_MEMORY;
                goto done;
            }
        }
        else if (oem->MaximumLength < len)
        {
            ret = STATUS_BUFFER_OVERFLOW;
            oem->Length = oem->MaximumLength;
            if (!oem->MaximumLength) goto done;
        }
        RtlUnicodeToOemN( oem->Buffer, oem->Length, NULL, upcase.Buffer, upcase.Length );
    done:
1180
        if (upcase.Buffer != tmp) RtlFreeUnicodeString( &upcase );
1181 1182 1183 1184 1185
    }
    return ret;
}


1186 1187
/**************************************************************************
 *	RtlUpcaseUnicodeToMultiByteN   (NTDLL.@)
1188 1189 1190 1191 1192
 *
 * Converts a Unicode string to the equivalent ANSI upper-case representation.
 *
 * RETURNS
 *  NTSTATUS code
1193 1194 1195 1196 1197 1198 1199 1200
 */
NTSTATUS WINAPI RtlUpcaseUnicodeToMultiByteN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
                                              LPCWSTR src, DWORD srclen )
{
    NTSTATUS ret;
    LPWSTR upcase;
    DWORD i;

1201
    if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1202 1203
    for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
    ret = RtlUnicodeToMultiByteN( dst, dstlen, reslen, upcase, srclen );
1204
    RtlFreeHeap( GetProcessHeap(), 0, upcase );
1205
    return ret;
Juergen Schmied's avatar
Juergen Schmied committed
1206 1207
}

1208

Juergen Schmied's avatar
Juergen Schmied committed
1209
/**************************************************************************
1210
 *	RtlUpcaseUnicodeToOemN   (NTDLL.@)
1211 1212 1213 1214 1215
 *
 * Converts a Unicode string to the equivalent OEM upper-case representation.
 *
 * RETURNS
 *  NTSTATUS code
Juergen Schmied's avatar
Juergen Schmied committed
1216
 */
1217 1218
NTSTATUS WINAPI RtlUpcaseUnicodeToOemN( LPSTR dst, DWORD dstlen, LPDWORD reslen,
                                        LPCWSTR src, DWORD srclen )
Juergen Schmied's avatar
Juergen Schmied committed
1219
{
1220 1221 1222 1223
    NTSTATUS ret;
    LPWSTR upcase;
    DWORD i;

1224
    if (!(upcase = RtlAllocateHeap( GetProcessHeap(), 0, srclen ))) return STATUS_NO_MEMORY;
1225 1226
    for (i = 0; i < srclen/sizeof(WCHAR); i++) upcase[i] = toupperW(src[i]);
    ret = RtlUnicodeToOemN( dst, dstlen, reslen, upcase, srclen );
1227
    RtlFreeHeap( GetProcessHeap(), 0, upcase );
1228 1229
    return ret;
}
1230 1231


1232
/*
1233
        STRING SIZE
1234
*/
1235

1236

1237 1238
/**************************************************************************
 *      RtlOemStringToUnicodeSize   (NTDLL.@)
Patrik Stridvall's avatar
Patrik Stridvall committed
1239
 *      RtlxOemStringToUnicodeSize  (NTDLL.@)
1240
 *
1241
 * Calculate the size in bytes necessary for the Unicode conversion of str,
1242
 * including the terminating '\0'.
1243 1244 1245 1246 1247 1248
 *
 * PARAMS
 *  str [I] String to calculate the size of
 *
 * RETURNS
 *  The calculated size.
1249
 */
1250
UINT WINAPI RtlOemStringToUnicodeSize( const STRING *str )
1251
{
1252
    int ret = wine_cp_mbstowcs( oem_table, 0, str->Buffer, str->Length, NULL, 0 );
1253
    return (ret + 1) * sizeof(WCHAR);
Juergen Schmied's avatar
Juergen Schmied committed
1254 1255
}

1256

Juergen Schmied's avatar
Juergen Schmied committed
1257
/**************************************************************************
1258
 *      RtlAnsiStringToUnicodeSize   (NTDLL.@)
Patrik Stridvall's avatar
Patrik Stridvall committed
1259
 *      RtlxAnsiStringToUnicodeSize  (NTDLL.@)
1260
 *
1261
 * Calculate the size in bytes necessary for the Unicode conversion of str,
1262
 * including the terminating '\0'.
1263 1264 1265 1266 1267 1268
 *
 * PARAMS
 *  str [I] String to calculate the size of
 *
 * RETURNS
 *  The calculated size.
Juergen Schmied's avatar
Juergen Schmied committed
1269
 */
1270
DWORD WINAPI RtlAnsiStringToUnicodeSize( const STRING *str )
1271
{
1272 1273 1274
    DWORD ret;
    RtlMultiByteToUnicodeSize( &ret, str->Buffer, str->Length );
    return ret + sizeof(WCHAR);
1275
}
1276 1277


1278 1279 1280
/**************************************************************************
 *      RtlMultiByteToUnicodeSize   (NTDLL.@)
 *
1281
 * Compute the size in bytes necessary for the Unicode conversion of str,
1282
 * without the terminating '\0'.
1283 1284 1285 1286 1287 1288 1289 1290
 *
 * PARAMS
 *  size [O] Destination for size
 *  str  [I] String to calculate the size of
 *  len  [I] Length of str
 *
 * RETURNS
 *  STATUS_SUCCESS.
1291 1292 1293
 */
NTSTATUS WINAPI RtlMultiByteToUnicodeSize( DWORD *size, LPCSTR str, UINT len )
{
1294
    *size = wine_cp_mbstowcs( ansi_table, 0, str, len, NULL, 0 ) * sizeof(WCHAR);
1295
    return STATUS_SUCCESS;
1296
}
1297 1298


1299 1300 1301
/**************************************************************************
 *      RtlUnicodeToMultiByteSize   (NTDLL.@)
 *
1302
 * Calculate the size in bytes necessary for the multibyte conversion of str,
1303
 * without the terminating '\0'.
1304 1305 1306 1307 1308 1309 1310 1311
 *
 * PARAMS
 *  size [O] Destination for size
 *  str  [I] String to calculate the size of
 *  len  [I] Length of str
 *
 * RETURNS
 *  STATUS_SUCCESS.
1312
 */
1313
NTSTATUS WINAPI RtlUnicodeToMultiByteSize( PULONG size, LPCWSTR str, ULONG len )
1314
{
1315
    *size = wine_cp_wcstombs( ansi_table, 0, str, len / sizeof(WCHAR), NULL, 0, NULL, NULL );
1316
    return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
1317 1318
}

1319

Juergen Schmied's avatar
Juergen Schmied committed
1320
/**************************************************************************
1321
 *      RtlUnicodeStringToAnsiSize   (NTDLL.@)
Patrik Stridvall's avatar
Patrik Stridvall committed
1322
 *      RtlxUnicodeStringToAnsiSize  (NTDLL.@)
1323
 *
1324
 * Calculate the size in bytes necessary for the Ansi conversion of str,
1325
 * including the terminating '\0'.
1326 1327 1328 1329 1330 1331
 *
 * PARAMS
 *  str [I] String to calculate the size of
 *
 * RETURNS
 *  The calculated size.
Juergen Schmied's avatar
Juergen Schmied committed
1332
 */
1333
DWORD WINAPI RtlUnicodeStringToAnsiSize( const UNICODE_STRING *str )
Juergen Schmied's avatar
Juergen Schmied committed
1334
{
1335 1336 1337
    DWORD ret;
    RtlUnicodeToMultiByteSize( &ret, str->Buffer, str->Length );
    return ret + 1;
1338
}
Juergen Schmied's avatar
Juergen Schmied committed
1339 1340


1341 1342
/**************************************************************************
 *      RtlUnicodeStringToOemSize   (NTDLL.@)
Patrik Stridvall's avatar
Patrik Stridvall committed
1343
 *      RtlxUnicodeStringToOemSize  (NTDLL.@)
1344
 *
1345
 * Calculate the size in bytes necessary for the OEM conversion of str,
1346
 * including the terminating '\0'.
1347 1348 1349 1350 1351 1352
 *
 * PARAMS
 *  str [I] String to calculate the size of
 *
 * RETURNS
 *  The calculated size.
1353 1354 1355
 */
DWORD WINAPI RtlUnicodeStringToOemSize( const UNICODE_STRING *str )
{
1356
    return wine_cp_wcstombs( oem_table, 0, str->Buffer, str->Length / sizeof(WCHAR),
1357
                             NULL, 0, NULL, NULL ) + 1;
1358
}
1359 1360


1361
/**************************************************************************
1362 1363 1364 1365 1366 1367 1368
 *      RtlAppendAsciizToString   (NTDLL.@)
 *
 * Concatenates a buffered character string and a '\0' terminated character
 * string
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. src is appended to dest.
1369
 *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1370 1371 1372 1373 1374
 *                  to hold the concatenated string.
 *
 * NOTES
 *  if src is NULL dest is unchanged.
 *  dest is never '\0' terminated.
1375
 */
1376 1377 1378
NTSTATUS WINAPI RtlAppendAsciizToString(
    STRING *dest, /* [I/O] Buffered character string to which src is concatenated */
    LPCSTR src)   /* [I]   '\0' terminated character string to be concatenated */
1379
{
1380 1381 1382 1383 1384 1385 1386 1387
    if (src != NULL) {
        unsigned int src_len = strlen(src);
        unsigned int dest_len  = src_len + dest->Length;

        if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
        memcpy(dest->Buffer + dest->Length, src, src_len);
        dest->Length = dest_len;
    } /* if */
1388
    return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
1389 1390
}

1391

Juergen Schmied's avatar
Juergen Schmied committed
1392
/**************************************************************************
1393 1394 1395 1396 1397 1398
 *      RtlAppendStringToString   (NTDLL.@)
 *
 * Concatenates two buffered character strings
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. src is appended to dest.
1399
 *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1400 1401 1402 1403 1404
 *                  to hold the concatenated string.
 *
 * NOTES
 *  if src->length is zero dest is unchanged.
 *  dest is never '\0' terminated.
Juergen Schmied's avatar
Juergen Schmied committed
1405
 */
1406 1407 1408
NTSTATUS WINAPI RtlAppendStringToString(
    STRING *dest,       /* [I/O] Buffered character string to which src is concatenated */
    const STRING *src)  /* [I]   Buffered character string to be concatenated */
Juergen Schmied's avatar
Juergen Schmied committed
1409
{
1410
    if (src->Length != 0) {
1411
        unsigned int dest_len = src->Length + dest->Length;
1412

1413 1414 1415
        if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
        memcpy(dest->Buffer + dest->Length, src->Buffer, src->Length);
        dest->Length = dest_len;
1416
    } /* if */
1417
    return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
1418 1419
}

1420

Juergen Schmied's avatar
Juergen Schmied committed
1421
/**************************************************************************
1422
 *      RtlAppendUnicodeToString   (NTDLL.@)
1423
 *
1424
 * Concatenates a buffered unicode string and a '\0' terminated unicode 
1425 1426 1427 1428
 * string
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. src is appended to dest.
1429
 *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1430 1431 1432 1433
 *                  to hold the concatenated string.
 *
 * NOTES
 *  if src is NULL dest is unchanged.
1434 1435
 *  dest is '\0' terminated when the MaximumLength allows it.
 *  When dest fits exactly in MaximumLength characters the '\0' is omitted.
1436 1437 1438 1439
 *
 * DIFFERENCES
 *  Does not write in the src->Buffer beyond MaximumLength when
 *  MaximumLength is odd as the native function does.
Juergen Schmied's avatar
Juergen Schmied committed
1440
 */
1441 1442 1443
NTSTATUS WINAPI RtlAppendUnicodeToString(
    UNICODE_STRING *dest, /* [I/O] Buffered unicode string to which src is concatenated */
    LPCWSTR src)          /* [I]   '\0' terminated unicode string to be concatenated */
Juergen Schmied's avatar
Juergen Schmied committed
1444
{
1445 1446 1447 1448 1449 1450 1451
    if (src != NULL) {
        unsigned int src_len = strlenW(src) * sizeof(WCHAR);
        unsigned int dest_len  = src_len + dest->Length;

        if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
        memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src, src_len);
        dest->Length = dest_len;
1452
        /* append terminating '\0' if enough space */
1453
        if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
1454 1455
            dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
        } /* if */
1456
    } /* if */
1457
    return STATUS_SUCCESS;
Juergen Schmied's avatar
Juergen Schmied committed
1458 1459
}

1460

Juergen Schmied's avatar
Juergen Schmied committed
1461
/**************************************************************************
1462
 *      RtlAppendUnicodeStringToString   (NTDLL.@)
1463 1464 1465 1466 1467
 *
 * Concatenates two buffered unicode strings
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. src is appended to dest.
1468
 *  Failure: STATUS_BUFFER_TOO_SMALL, if the buffer of dest is too small
1469 1470 1471 1472
 *                  to hold the concatenated string.
 *
 * NOTES
 *  if src->length is zero dest is unchanged.
1473 1474
 *  dest is '\0' terminated when the MaximumLength allows it.
 *  When dest fits exactly in MaximumLength characters the '\0' is omitted.
1475 1476 1477 1478
 *
 * DIFFERENCES
 *  Does not write in the src->Buffer beyond MaximumLength when
 *  MaximumLength is odd as the native function does.
1479
 */
1480 1481 1482
NTSTATUS WINAPI RtlAppendUnicodeStringToString(
    UNICODE_STRING *dest,      /* [I/O] Buffered unicode string to which src is concatenated */
    const UNICODE_STRING *src) /* [I]   Buffered unicode string to be concatenated */
1483
{
1484
    if (src->Length != 0) {
1485 1486 1487 1488 1489 1490 1491 1492 1493
        unsigned int dest_len = src->Length + dest->Length;

        if (dest_len > dest->MaximumLength) return STATUS_BUFFER_TOO_SMALL;
        memcpy(dest->Buffer + dest->Length/sizeof(WCHAR), src->Buffer, src->Length);
        dest->Length = dest_len;
        /* append terminating '\0' if enough space */
        if (dest_len + sizeof(WCHAR) <= dest->MaximumLength) {
            dest->Buffer[dest_len / sizeof(WCHAR)] = 0;
        } /* if */
1494
    } /* if */
1495
    return STATUS_SUCCESS;
1496 1497
}

1498

1499 1500 1501
/**************************************************************************
 *      RtlFindCharInUnicodeString   (NTDLL.@)
 *
1502
 * Searches for one of several unicode characters in a unicode string.
1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. pos contains the position after the character found.
 *  Failure: STATUS_NOT_FOUND, if none of the search_chars are in main_str.
 */
NTSTATUS WINAPI RtlFindCharInUnicodeString(
    int flags,                          /* [I] Flags */
    const UNICODE_STRING *main_str,     /* [I] Unicode string in which one or more characters are searched */
    const UNICODE_STRING *search_chars, /* [I] Unicode string which contains the characters to search for */
    USHORT *pos)                        /* [O] Position of the first character found + 2 */
{
1514
    unsigned int main_idx, search_idx;
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528

    switch (flags) {
        case 0:
            for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
                for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
                    if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
                        *pos = (main_idx + 1) * sizeof(WCHAR);
                        return STATUS_SUCCESS;
                    }
                }
            }
            *pos = 0;
            return STATUS_NOT_FOUND;
        case 1:
1529 1530
            main_idx = main_str->Length / sizeof(WCHAR);
            while (main_idx-- > 0) {
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554
                for (search_idx = 0; search_idx < search_chars->Length / sizeof(WCHAR); search_idx++) {
                    if (main_str->Buffer[main_idx] == search_chars->Buffer[search_idx]) {
                        *pos = main_idx * sizeof(WCHAR);
                        return STATUS_SUCCESS;
                    }
                }
            }
            *pos = 0;
            return STATUS_NOT_FOUND;
        case 2:
            for (main_idx = 0; main_idx < main_str->Length / sizeof(WCHAR); main_idx++) {
                search_idx = 0;
                while (search_idx < search_chars->Length / sizeof(WCHAR) &&
                         main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
                    search_idx++;
                }
                if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
                    *pos = (main_idx + 1) * sizeof(WCHAR);
                    return STATUS_SUCCESS;
                }
            }
            *pos = 0;
            return STATUS_NOT_FOUND;
        case 3:
1555 1556
            main_idx = main_str->Length / sizeof(WCHAR);
            while (main_idx-- > 0) {
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
                search_idx = 0;
                while (search_idx < search_chars->Length / sizeof(WCHAR) &&
                         main_str->Buffer[main_idx] != search_chars->Buffer[search_idx]) {
                    search_idx++;
                }
                if (search_idx >= search_chars->Length / sizeof(WCHAR)) {
                    *pos = main_idx * sizeof(WCHAR);
                    return STATUS_SUCCESS;
                }
            }
            *pos = 0;
            return STATUS_NOT_FOUND;
    } /* switch */
    return STATUS_NOT_FOUND;
}


1574
/*
1575
        MISC
1576
*/
1577

1578
/**************************************************************************
1579
 *	RtlIsTextUnicode (NTDLL.@)
Juergen Schmied's avatar
Juergen Schmied committed
1580
 *
1581 1582 1583 1584 1585 1586 1587 1588
 * Attempt to guess whether a text buffer is Unicode.
 *
 * PARAMS
 *  buf [I] Text buffer to test
 *  len [I] Length of buf
 *  pf  [O] Destination for test results
 *
 * RETURNS
1589
 *  TRUE if the buffer is likely Unicode, FALSE otherwise.
1590 1591 1592
 *
 * FIXME
 *  Should implement more tests.
Juergen Schmied's avatar
Juergen Schmied committed
1593
 */
1594
BOOLEAN WINAPI RtlIsTextUnicode( LPCVOID buf, INT len, INT *pf )
Juergen Schmied's avatar
Juergen Schmied committed
1595
{
1596
    static const WCHAR std_control_chars[] = {'\r','\n','\t',' ',0x3000,0};
1597
    static const WCHAR byterev_control_chars[] = {0x0d00,0x0a00,0x0900,0x2000,0};
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
    const WCHAR *s = buf;
    int i;
    unsigned int flags = ~0U, out_flags = 0;

    if (len < sizeof(WCHAR))
    {
        /* FIXME: MSDN documents IS_TEXT_UNICODE_BUFFER_TOO_SMALL but there is no such thing... */
        if (pf) *pf = 0;
        return FALSE;
    }
    if (pf)
        flags = *pf;
    /*
     * Apply various tests to the text string. According to the
     * docs, each test "passed" sets the corresponding flag in
     * the output flags. But some of the tests are mutually
     * exclusive, so I don't see how you could pass all tests ...
     */

    /* Check for an odd length ... pass if even. */
    if (len & 1) out_flags |= IS_TEXT_UNICODE_ODD_LENGTH;

1620 1621 1622
    if (((char *)buf)[len - 1] == 0)
        len--;  /* Windows seems to do something like that to avoid e.g. false IS_TEXT_UNICODE_NULL_BYTES  */

1623 1624 1625 1626 1627 1628 1629
    len /= sizeof(WCHAR);
    /* Windows only checks the first 256 characters */
    if (len > 256) len = 256;

    /* Check for the special byte order unicode marks. */
    if (*s == 0xFEFF) out_flags |= IS_TEXT_UNICODE_SIGNATURE;
    if (*s == 0xFFFE) out_flags |= IS_TEXT_UNICODE_REVERSE_SIGNATURE;
1630 1631 1632 1633

    /* apply some statistical analysis */
    if (flags & IS_TEXT_UNICODE_STATISTICS)
    {
1634
        int stats = 0;
1635
        /* FIXME: checks only for ASCII characters in the unicode stream */
1636
        for (i = 0; i < len; i++)
1637 1638 1639
        {
            if (s[i] <= 255) stats++;
        }
1640
        if (stats > len / 2)
1641 1642 1643 1644 1645 1646
            out_flags |= IS_TEXT_UNICODE_STATISTICS;
    }

    /* Check for unicode NULL chars */
    if (flags & IS_TEXT_UNICODE_NULL_BYTES)
    {
1647
        for (i = 0; i < len; i++)
1648
        {
1649
            if (!(s[i] & 0xff) || !(s[i] >> 8))
1650 1651 1652 1653 1654 1655 1656
            {
                out_flags |= IS_TEXT_UNICODE_NULL_BYTES;
                break;
            }
        }
    }

1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
    if (flags & IS_TEXT_UNICODE_CONTROLS)
    {
        for (i = 0; i < len; i++)
        {
            if (strchrW(std_control_chars, s[i]))
            {
                out_flags |= IS_TEXT_UNICODE_CONTROLS;
                break;
            }
        }
    }

    if (flags & IS_TEXT_UNICODE_REVERSE_CONTROLS)
    {
        for (i = 0; i < len; i++)
        {
            if (strchrW(byterev_control_chars, s[i]))
            {
                out_flags |= IS_TEXT_UNICODE_REVERSE_CONTROLS;
                break;
            }
        }
    }

1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
    if (pf)
    {
        out_flags &= *pf;
        *pf = out_flags;
    }
    /* check for flags that indicate it's definitely not valid Unicode */
    if (out_flags & (IS_TEXT_UNICODE_REVERSE_MASK | IS_TEXT_UNICODE_NOT_UNICODE_MASK)) return FALSE;
    /* now check for invalid ASCII, and assume Unicode if so */
    if (out_flags & IS_TEXT_UNICODE_NOT_ASCII_MASK) return TRUE;
    /* now check for Unicode flags */
    if (out_flags & IS_TEXT_UNICODE_UNICODE_MASK) return TRUE;
    /* no flags set */
    return FALSE;
Juergen Schmied's avatar
Juergen Schmied committed
1694
}
1695

1696 1697 1698 1699

/**************************************************************************
 *      RtlCharToInteger   (NTDLL.@)
 *
1700
 * Converts a character string into its integer equivalent.
1701
 *
1702 1703 1704 1705
 * RETURNS
 *  Success: STATUS_SUCCESS. value contains the converted number
 *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
 *           STATUS_ACCESS_VIOLATION, if value is NULL.
1706
 *
1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
 * NOTES
 *  For base 0 it uses 10 as base and the string should be in the format
 *      "{whitespace} [+|-] [0[x|o|b]] {digits}".
 *  For other bases the string should be in the format
 *      "{whitespace} [+|-] {digits}".
 *  No check is made for value overflow, only the lower 32 bits are assigned.
 *  If str is NULL it crashes, as the native function does.
 *
 * DIFFERENCES
 *  This function does not read garbage behind '\0' as the native version does.
1717 1718
 */
NTSTATUS WINAPI RtlCharToInteger(
1719 1720 1721
    PCSZ str,      /* [I] '\0' terminated single-byte string containing a number */
    ULONG base,    /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
    ULONG *value)  /* [O] Destination for the converted value */
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
{
    CHAR chCurrent;
    int digit;
    ULONG RunningTotal = 0;
    char bMinus = 0;

    while (*str != '\0' && *str <= ' ') {
	str++;
    } /* while */

    if (*str == '+') {
	str++;
    } else if (*str == '-') {
	bMinus = 1;
	str++;
    } /* if */

    if (base == 0) {
	base = 10;
	if (str[0] == '0') {
	    if (str[1] == 'b') {
		str += 2;
		base = 2;
	    } else if (str[1] == 'o') {
		str += 2;
		base = 8;
	    } else if (str[1] == 'x') {
		str += 2;
		base = 16;
	    } /* if */
	} /* if */
    } else if (base != 2 && base != 8 && base != 10 && base != 16) {
	return STATUS_INVALID_PARAMETER;
    } /* if */

    if (value == NULL) {
	return STATUS_ACCESS_VIOLATION;
    } /* if */

    while (*str != '\0') {
	chCurrent = *str;
	if (chCurrent >= '0' && chCurrent <= '9') {
	    digit = chCurrent - '0';
	} else if (chCurrent >= 'A' && chCurrent <= 'Z') {
	    digit = chCurrent - 'A' + 10;
	} else if (chCurrent >= 'a' && chCurrent <= 'z') {
	    digit = chCurrent - 'a' + 10;
	} else {
	    digit = -1;
	} /* if */
	if (digit < 0 || digit >= base) {
	    *value = bMinus ? -RunningTotal : RunningTotal;
	    return STATUS_SUCCESS;
	} /* if */

	RunningTotal = RunningTotal * base + digit;
	str++;
    } /* while */

    *value = bMinus ? -RunningTotal : RunningTotal;
    return STATUS_SUCCESS;
}


1786
/**************************************************************************
1787
 *      RtlIntegerToChar   (NTDLL.@)
1788
 *
1789 1790 1791 1792 1793 1794 1795
 * Converts an unsigned integer to a character string.
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. str contains the converted number
 *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
 *           STATUS_BUFFER_OVERFLOW, if str would be larger than length.
 *           STATUS_ACCESS_VIOLATION, if str is NULL.
1796
 *
1797
 * NOTES
1798 1799
 *  Instead of base 0 it uses 10 as base.
 *  Writes at most length characters to the string str.
1800 1801
 *  Str is '\0' terminated when length allows it.
 *  When str fits exactly in length characters the '\0' is omitted.
1802 1803
 */
NTSTATUS WINAPI RtlIntegerToChar(
1804 1805 1806 1807
    ULONG value,   /* [I] Value to be converted */
    ULONG base,    /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
    ULONG length,  /* [I] Length of the str buffer in bytes */
    PCHAR str)     /* [O] Destination for the converted value */
1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850
{
    CHAR buffer[33];
    PCHAR pos;
    CHAR digit;
    ULONG len;

    if (base == 0) {
	base = 10;
    } else if (base != 2 && base != 8 && base != 10 && base != 16) {
	return STATUS_INVALID_PARAMETER;
    } /* if */

    pos = &buffer[32];
    *pos = '\0';

    do {
	pos--;
	digit = value % base;
	value = value / base;
	if (digit < 10) {
	    *pos = '0' + digit;
	} else {
	    *pos = 'A' + digit - 10;
	} /* if */
    } while (value != 0L);

    len = &buffer[32] - pos;
    if (len > length) {
	return STATUS_BUFFER_OVERFLOW;
    } else if (str == NULL) {
	return STATUS_ACCESS_VIOLATION;
    } else if (len == length) {
	memcpy(str, pos, len);
    } else {
	memcpy(str, pos, len + 1);
    } /* if */
    return STATUS_SUCCESS;
}


/**************************************************************************
 *      RtlUnicodeStringToInteger (NTDLL.@)
 *
1851
 * Converts a unicode string into its integer equivalent.
1852 1853 1854 1855 1856
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. value contains the converted number
 *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
 *           STATUS_ACCESS_VIOLATION, if value is NULL.
1857
 *
1858
 * NOTES
1859 1860 1861 1862 1863 1864 1865 1866 1867 1868
 *  For base 0 it uses 10 as base and the string should be in the format
 *      "{whitespace} [+|-] [0[x|o|b]] {digits}".
 *  For other bases the string should be in the format
 *      "{whitespace} [+|-] {digits}".
 *  No check is made for value overflow, only the lower 32 bits are assigned.
 *  If str is NULL it crashes, as the native function does.
 *
 * DIFFERENCES
 *  This function does not read garbage on string length 0 as the native
 *  version does.
1869 1870
 */
NTSTATUS WINAPI RtlUnicodeStringToInteger(
1871 1872 1873
    const UNICODE_STRING *str, /* [I] Unicode string to be converted */
    ULONG base,                /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
    ULONG *value)              /* [O] Destination for the converted value */
1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911
{
    LPWSTR lpwstr = str->Buffer;
    USHORT CharsRemaining = str->Length / sizeof(WCHAR);
    WCHAR wchCurrent;
    int digit;
    ULONG RunningTotal = 0;
    char bMinus = 0;

    while (CharsRemaining >= 1 && *lpwstr <= ' ') {
	lpwstr++;
	CharsRemaining--;
    } /* while */

    if (CharsRemaining >= 1) {
	if (*lpwstr == '+') {
	    lpwstr++;
	    CharsRemaining--;
	} else if (*lpwstr == '-') {
	    bMinus = 1;
	    lpwstr++;
	    CharsRemaining--;
	} /* if */
    } /* if */

    if (base == 0) {
	base = 10;
	if (CharsRemaining >= 2 && lpwstr[0] == '0') {
	    if (lpwstr[1] == 'b') {
		lpwstr += 2;
		CharsRemaining -= 2;
		base = 2;
	    } else if (lpwstr[1] == 'o') {
		lpwstr += 2;
		CharsRemaining -= 2;
		base = 8;
	    } else if (lpwstr[1] == 'x') {
		lpwstr += 2;
		CharsRemaining -= 2;
1912
		base = 16;
1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
	    } /* if */
	} /* if */
    } else if (base != 2 && base != 8 && base != 10 && base != 16) {
	return STATUS_INVALID_PARAMETER;
    } /* if */

    if (value == NULL) {
	return STATUS_ACCESS_VIOLATION;
    } /* if */

    while (CharsRemaining >= 1) {
	wchCurrent = *lpwstr;
	if (wchCurrent >= '0' && wchCurrent <= '9') {
	    digit = wchCurrent - '0';
	} else if (wchCurrent >= 'A' && wchCurrent <= 'Z') {
	    digit = wchCurrent - 'A' + 10;
	} else if (wchCurrent >= 'a' && wchCurrent <= 'z') {
	    digit = wchCurrent - 'a' + 10;
	} else {
	    digit = -1;
	} /* if */
	if (digit < 0 || digit >= base) {
	    *value = bMinus ? -RunningTotal : RunningTotal;
	    return STATUS_SUCCESS;
	} /* if */

	RunningTotal = RunningTotal * base + digit;
	lpwstr++;
	CharsRemaining--;
    } /* while */

    *value = bMinus ? -RunningTotal : RunningTotal;
    return STATUS_SUCCESS;
}


/**************************************************************************
 *	RtlIntegerToUnicodeString (NTDLL.@)
 *
1952 1953 1954 1955 1956 1957 1958 1959 1960
 * Converts an unsigned integer to a '\0' terminated unicode string.
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. str contains the converted number
 *  Failure: STATUS_INVALID_PARAMETER, if base is not 0, 2, 8, 10 or 16.
 *           STATUS_BUFFER_OVERFLOW, if str is too small to hold the string
 *                  (with the '\0' termination). In this case str->Length
 *                  is set to the length, the string would have (which can
 *                  be larger than the MaximumLength).
1961
 *
1962
 * NOTES
1963 1964 1965 1966 1967 1968 1969
 *  Instead of base 0 it uses 10 as base.
 *  If str is NULL it crashes, as the native function does.
 *
 * DIFFERENCES
 *  Do not return STATUS_BUFFER_OVERFLOW when the string is long enough.
 *  The native function does this when the string would be longer than 16
 *  characters even when the string parameter is long enough.
1970 1971
 */
NTSTATUS WINAPI RtlIntegerToUnicodeString(
1972 1973 1974
    ULONG value,         /* [I] Value to be converted */
    ULONG base,          /* [I] Number base for conversion (allowed 0, 2, 8, 10 or 16) */
    UNICODE_STRING *str) /* [O] Destination for the converted value */
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003
{
    WCHAR buffer[33];
    PWCHAR pos;
    WCHAR digit;

    if (base == 0) {
	base = 10;
    } else if (base != 2 && base != 8 && base != 10 && base != 16) {
	return STATUS_INVALID_PARAMETER;
    } /* if */

    pos = &buffer[32];
    *pos = '\0';

    do {
	pos--;
	digit = value % base;
	value = value / base;
	if (digit < 10) {
	    *pos = '0' + digit;
	} else {
	    *pos = 'A' + digit - 10;
	} /* if */
    } while (value != 0L);

    str->Length = (&buffer[32] - pos) * sizeof(WCHAR);
    if (str->Length >= str->MaximumLength) {
	return STATUS_BUFFER_OVERFLOW;
    } else {
2004
	memcpy(str->Buffer, pos, str->Length + sizeof(WCHAR));
2005 2006
    } /* if */
    return STATUS_SUCCESS;
2007
}
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025


/*************************************************************************
 * RtlGUIDFromString (NTDLL.@)
 *
 * Convert a string representation of a GUID into a GUID.
 *
 * PARAMS
 *  str  [I] String representation in the format "{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}"
 *  guid [O] Destination for the converted GUID
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. guid contains the converted value.
 *  Failure: STATUS_INVALID_PARAMETER, if str is not in the expected format.
 *
 * SEE ALSO
 *  See RtlStringFromGUID.
 */
2026
NTSTATUS WINAPI RtlGUIDFromString(PUNICODE_STRING str, GUID* guid)
2027 2028 2029 2030 2031 2032 2033 2034 2035 2036
{
  int i = 0;
  const WCHAR *lpszCLSID = str->Buffer;
  BYTE* lpOut = (BYTE*)guid;

  TRACE("(%s,%p)\n", debugstr_us(str), guid);

  /* Convert string: {XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
   * to memory:       DWORD... WORD WORD BYTES............
   */
2037
  while (i <= 37)
2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126
  {
    switch (i)
    {
    case 0:
      if (*lpszCLSID != '{')
        return STATUS_INVALID_PARAMETER;
      break;

    case 9: case 14: case 19: case 24:
      if (*lpszCLSID != '-')
        return STATUS_INVALID_PARAMETER;
      break;

    case 37:
      if (*lpszCLSID != '}')
        return STATUS_INVALID_PARAMETER;
      break;

    default:
      {
        WCHAR ch = *lpszCLSID, ch2 = lpszCLSID[1];
        unsigned char byte;

        /* Read two hex digits as a byte value */
        if      (ch >= '0' && ch <= '9') ch = ch - '0';
        else if (ch >= 'a' && ch <= 'f') ch = ch - 'a' + 10;
        else if (ch >= 'A' && ch <= 'F') ch = ch - 'A' + 10;
        else return STATUS_INVALID_PARAMETER;

        if      (ch2 >= '0' && ch2 <= '9') ch2 = ch2 - '0';
        else if (ch2 >= 'a' && ch2 <= 'f') ch2 = ch2 - 'a' + 10;
        else if (ch2 >= 'A' && ch2 <= 'F') ch2 = ch2 - 'A' + 10;
        else return STATUS_INVALID_PARAMETER;

        byte = ch << 4 | ch2;

        switch (i)
        {
#ifndef WORDS_BIGENDIAN
        /* For Big Endian machines, we store the data such that the
         * dword/word members can be read as DWORDS and WORDS correctly. */
        /* Dword */
        case 1:  lpOut[3] = byte; break;
        case 3:  lpOut[2] = byte; break;
        case 5:  lpOut[1] = byte; break;
        case 7:  lpOut[0] = byte; lpOut += 4;  break;
        /* Word */
        case 10: case 15: lpOut[1] = byte; break;
        case 12: case 17: lpOut[0] = byte; lpOut += 2; break;
#endif
        /* Byte */
        default: lpOut[0] = byte; lpOut++; break;
        }
        lpszCLSID++; /* Skip 2nd character of byte */
        i++;
      }
    }
    lpszCLSID++;
    i++;
  }

  return STATUS_SUCCESS;
}

/*************************************************************************
 * RtlStringFromGUID (NTDLL.@)
 *
 * Convert a GUID into a string representation of a GUID.
 *
 * PARAMS
 *  guid [I] GUID to convert
 *  str  [O] Destination for the converted string
 *
 * RETURNS
 *  Success: STATUS_SUCCESS. str contains the converted value.
 *  Failure: STATUS_NO_MEMORY, if memory for str cannot be allocated.
 *
 * SEE ALSO
 *  See RtlGUIDFromString.
 */
NTSTATUS WINAPI RtlStringFromGUID(const GUID* guid, UNICODE_STRING *str)
{
  static const WCHAR szFormat[] = { '{','%','0','8','l','X','-',
    '%','0','4','X','-',  '%','0','4','X','-','%','0','2','X','%','0','2','X',
    '-',   '%','0','2','X','%','0','2','X','%','0','2','X','%','0','2','X',
    '%','0','2','X','%','0','2','X','}','\0' };

  TRACE("(%p,%p)\n", guid, str);

2127 2128
  str->Length = GUID_STRING_LENGTH * sizeof(WCHAR);
  str->MaximumLength = str->Length + sizeof(WCHAR);
2129
  str->Buffer = RtlAllocateHeap(GetProcessHeap(), 0, str->MaximumLength);
2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140
  if (!str->Buffer)
  {
    str->Length = str->MaximumLength = 0;
    return STATUS_NO_MEMORY;
  }
  sprintfW(str->Buffer, szFormat, guid->Data1, guid->Data2, guid->Data3,
          guid->Data4[0], guid->Data4[1], guid->Data4[2], guid->Data4[3],
          guid->Data4[4], guid->Data4[5], guid->Data4[6], guid->Data4[7]);

  return STATUS_SUCCESS;
}