atom.c 13.9 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3
/*
 * Atom table functions
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
4
 * Copyright 1993, 1994, 1995 Alexandre Julliard
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19 20
 */

21
#include "config.h"
22
#include "wine/port.h"
23

Alexandre Julliard's avatar
Alexandre Julliard committed
24
#include <stdlib.h>
25
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
26
#include <stdio.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
27
#include <string.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
28
#include <ctype.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
29

30
#include "windef.h"
31
#include "winbase.h"
32
#include "winerror.h"
33 34
#include "winreg.h"
#include "winternl.h"
35

36 37
#include "wine/exception.h"
#include "excpt.h"
38
#include "wine/unicode.h"
39
#include "kernel_private.h"
40

41
#define MAX_ATOM_LEN 255
42

43 44 45 46 47 48 49
/******************************************************************
 *		get_local_table
 *
 * Returns the local atom table for this process (and create it if doesn't
 * exist yet)
 */
static RTL_ATOM_TABLE get_local_table(DWORD entries)
50
{
51
    static RTL_ATOM_TABLE local_table;
52 53 54

    if (!local_table)
    {
55 56
        NTSTATUS        status;
        RTL_ATOM_TABLE  table = NULL;
57

58
        if ((status = RtlCreateAtomTable( entries, &table )))
59 60
            SetLastError( RtlNtStatusToDosError( status ) );
        else if (InterlockedCompareExchangePointer((void*)&local_table, table, NULL) != NULL)
61
            RtlDestroyAtomTable( table );
62 63
    }

64
    return local_table;
65 66 67
}


Alexandre Julliard's avatar
Alexandre Julliard committed
68
/***********************************************************************
69
 *           InitAtomTable   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
70 71 72 73 74 75 76 77 78
 *
 * Initialise the global atom table.
 *
 * PARAMS
 *  entries [I] The number of entries to reserve in the table.
 *
 * RETURNS
 *  Success: TRUE.
 *  Failure: FALSE.
Alexandre Julliard's avatar
Alexandre Julliard committed
79
 */
80 81
BOOL WINAPI InitAtomTable( DWORD entries )
{
82
    return get_local_table( entries ) ? TRUE : FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
83 84
}

85 86 87 88 89 90 91
/******************************************************************
 *		check_integral_atom
 *
 * Check if a string (ANSI or UNICODE) is in fact an integral atom
 * (but doesn't check the "#1234" form)
 */
static inline BOOL check_integral_atom( const void* ptr, ATOM* patom)
Alexandre Julliard's avatar
Alexandre Julliard committed
92
{
93 94
    if (HIWORD( ptr )) return FALSE;
    if ((*patom = LOWORD( ptr )) >= MAXINTATOM)
95
    {
96 97
        SetLastError( ERROR_INVALID_PARAMETER );
        *patom = 0;
98
    }
99
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
100 101
}

Alexandre Julliard's avatar
Alexandre Julliard committed
102
/***********************************************************************
103
 *           GlobalAddAtomA   (KERNEL32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
104
 *
Jon Griffiths's avatar
Jon Griffiths committed
105 106
 * Add a character string to the global atom table and return a unique
 * value identifying it.
Alexandre Julliard's avatar
Alexandre Julliard committed
107 108
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
109 110
 *	Success: The atom allocated to str.
 *	Failure: 0.
Alexandre Julliard's avatar
Alexandre Julliard committed
111
 */
Jon Griffiths's avatar
Jon Griffiths committed
112
ATOM WINAPI GlobalAddAtomA( LPCSTR str /* [in] String to add */ )
Alexandre Julliard's avatar
Alexandre Julliard committed
113
{
114
    ATOM atom = 0;
115 116
    __TRY
    {
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        if (!check_integral_atom( str, &atom ))
	{
	    WCHAR buffer[MAX_ATOM_LEN];
	    DWORD len = MultiByteToWideChar( CP_ACP, 0, str, strlen(str), buffer, MAX_ATOM_LEN );
	    if (!len) SetLastError( ERROR_INVALID_PARAMETER );
	    else
	    {
	        NTSTATUS status = NtAddAtom( buffer, len * sizeof(WCHAR), &atom );
		if (status)
		{
		    SetLastError( RtlNtStatusToDosError( status ) );
		    atom = 0;
		}
	    }
	}
132
    }
133
    __EXCEPT_PAGE_FAULT
134 135
    {
        SetLastError( ERROR_INVALID_PARAMETER );
136
        atom = 0;
137 138
    }
    __ENDTRY
139
    return atom;
Alexandre Julliard's avatar
Alexandre Julliard committed
140 141 142 143
}


/***********************************************************************
144
 *           AddAtomA   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
145 146 147
 *
 * Add a character string to the global atom table and return a unique
 * value identifying it.
Alexandre Julliard's avatar
Alexandre Julliard committed
148 149
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
150 151
 *      Success: The atom allocated to str.
 *      Failure: 0.
Alexandre Julliard's avatar
Alexandre Julliard committed
152
 */
Jon Griffiths's avatar
Jon Griffiths committed
153
ATOM WINAPI AddAtomA( LPCSTR str /* [in] String to add */ )
154 155
{
    ATOM atom = 0;
156 157

    if (!check_integral_atom( str, &atom ))
158
    {
159 160 161 162 163 164 165
        WCHAR           buffer[MAX_ATOM_LEN + 1];
        DWORD           len;
        RTL_ATOM_TABLE  table;

        len = MultiByteToWideChar( CP_ACP, 0, str, -1, buffer, MAX_ATOM_LEN + 1 );
        if (!len) SetLastError( ERROR_INVALID_PARAMETER );
        else if ((table = get_local_table( 0 )))
166
        {
167 168 169 170 171 172
            NTSTATUS status = RtlAddAtomToAtomTable( table, buffer, &atom );
            if (status)
            {
                SetLastError( RtlNtStatusToDosError( status ) );
                atom = 0;
            }
173
        }
174 175
    }
    return atom;
Alexandre Julliard's avatar
Alexandre Julliard committed
176 177 178
}

/***********************************************************************
179
 *           GlobalAddAtomW   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
180 181
 *
 * Unicode version of GlobalAddAtomA.
Alexandre Julliard's avatar
Alexandre Julliard committed
182
 */
183
ATOM WINAPI GlobalAddAtomW( LPCWSTR str )
Alexandre Julliard's avatar
Alexandre Julliard committed
184
{
185 186 187 188 189 190 191 192 193 194
    ATOM        atom = 0;
    NTSTATUS    status;

    if (!check_integral_atom( str, &atom ) && 
        (status = NtAddAtom( str, strlenW( str ) * sizeof(WCHAR), &atom )))
    {
        SetLastError( RtlNtStatusToDosError( status ) );
        atom = 0;
    }
    return atom;
195 196 197 198
}


/***********************************************************************
199
 *           AddAtomW   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
200 201
 *
 * Unicode version of AddAtomA.          
202 203 204
 */
ATOM WINAPI AddAtomW( LPCWSTR str )
{
205 206
    ATOM                atom = 0;
    RTL_ATOM_TABLE      table;
207

208
    if (!check_integral_atom( str, &atom ) && (table = get_local_table( 0 )))
209
    {
210 211
        NTSTATUS status = RtlAddAtomToAtomTable( table, str, &atom );
        if (status)
212
        {
213 214
            SetLastError( RtlNtStatusToDosError( status ) );
            atom = 0;
215
        }
216
    }
217
    return atom;
Alexandre Julliard's avatar
Alexandre Julliard committed
218 219 220 221
}


/***********************************************************************
222
 *           GlobalDeleteAtom   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
223 224
 *
 * Decrement the reference count of a string atom.  If the count is
Alexandre Julliard's avatar
Alexandre Julliard committed
225 226 227
 * zero, the string associated with the atom is removed from the table.
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
228 229
 *	Success: 0.
 *	Failure: atom.
Alexandre Julliard's avatar
Alexandre Julliard committed
230
 */
231
ATOM WINAPI GlobalDeleteAtom( ATOM atom /* [in] Atom to delete */ )
Alexandre Julliard's avatar
Alexandre Julliard committed
232
{
233 234 235 236 237 238 239 240 241 242
    if (atom >= MAXINTATOM)
    {
        NTSTATUS status = NtDeleteAtom( atom );
        if (status)
        {
            SetLastError( RtlNtStatusToDosError( status ) );
            return atom;
        }
    }
    return 0;
243 244 245 246
}


/***********************************************************************
247
 *           DeleteAtom   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
248 249
 *
 * Decrement the reference count of a string atom.  If the count becomes
250 251 252
 * zero, the string associated with the atom is removed from the table.
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
253 254
 *	Success: 0.
 *	Failure: atom
255 256 257
 */
ATOM WINAPI DeleteAtom( ATOM atom /* [in] Atom to delete */ )
{
258 259
    NTSTATUS            status;
    RTL_ATOM_TABLE      table;
260

261
    if (atom >= MAXINTATOM)
262
    {
263 264 265
        if (!(table = get_local_table( 0 ))) return atom;
        status = RtlDeleteAtomFromAtomTable( table, atom );
        if (status)
266
        {
267 268
            SetLastError( RtlNtStatusToDosError( status ) );
            return atom;
269
        }
270
    }
271
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
272 273 274 275
}


/***********************************************************************
276
 *           GlobalFindAtomA   (KERNEL32.@)
277
 *
Jon Griffiths's avatar
Jon Griffiths committed
278
 * Get the atom associated with a string.
Alexandre Julliard's avatar
Alexandre Julliard committed
279 280
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
281 282
 *	Success: The associated atom.
 *	Failure: 0.
Alexandre Julliard's avatar
Alexandre Julliard committed
283
 */
284
ATOM WINAPI GlobalFindAtomA( LPCSTR str /* [in] Pointer to string to search for */ )
285
{
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    ATOM atom = 0;

    if (!check_integral_atom( str, &atom ))
    {
        WCHAR buffer[MAX_ATOM_LEN];
        DWORD len = MultiByteToWideChar( CP_ACP, 0, str, strlen(str), buffer, MAX_ATOM_LEN );

        if (!len) SetLastError( ERROR_INVALID_PARAMETER );
        else
        {
            NTSTATUS status = NtFindAtom( buffer, len * sizeof(WCHAR), &atom );
            if (status)
            {
                SetLastError( RtlNtStatusToDosError( status ) );
                atom = 0;
            }
        }
    }
    return atom;
305 306 307
}

/***********************************************************************
308
 *           FindAtomA   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
309 310
 *
 * Get the atom associated with a string.
311 312
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
313 314
 *	Success: The associated atom.
 *	Failure: 0.
315 316
 */
ATOM WINAPI FindAtomA( LPCSTR str /* [in] Pointer to string to find */ )
Alexandre Julliard's avatar
Alexandre Julliard committed
317
{
318
    ATOM atom = 0;
319 320

    if (!check_integral_atom( str, &atom ))
321
    {
322 323 324 325 326 327 328
        WCHAR           buffer[MAX_ATOM_LEN + 1];
        DWORD           len;
        RTL_ATOM_TABLE  table;

        len = MultiByteToWideChar( CP_ACP, 0, str, -1, buffer, MAX_ATOM_LEN + 1 );
        if (!len) SetLastError( ERROR_INVALID_PARAMETER );
        else if ((table = get_local_table( 0 )))
329
        {
330 331 332 333 334 335
            NTSTATUS status = RtlLookupAtomInAtomTable( table, buffer, &atom );
            if (status)
            {
                SetLastError( RtlNtStatusToDosError( status ) );
                atom = 0;
            }
336
        }
337 338
    }
    return atom;
Alexandre Julliard's avatar
Alexandre Julliard committed
339 340 341 342
}


/***********************************************************************
343
 *           GlobalFindAtomW   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
344 345
 *
 * Unicode version of GlobalFindAtomA.
Alexandre Julliard's avatar
Alexandre Julliard committed
346
 */
347
ATOM WINAPI GlobalFindAtomW( LPCWSTR str )
Alexandre Julliard's avatar
Alexandre Julliard committed
348
{
349 350 351 352 353 354 355 356 357 358 359 360
    ATOM atom = 0;

    if (!check_integral_atom( str, &atom ))
    {
        NTSTATUS status = NtFindAtom( str, strlenW( str ) * sizeof(WCHAR), &atom );
        if (status)
        {
            SetLastError( RtlNtStatusToDosError( status ) );
            atom = 0;
        }
    }
    return atom;
Alexandre Julliard's avatar
Alexandre Julliard committed
361 362 363 364
}


/***********************************************************************
365
 *           FindAtomW   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
366 367
 *
 * Unicode version of FindAtomA.
Alexandre Julliard's avatar
Alexandre Julliard committed
368
 */
369 370
ATOM WINAPI FindAtomW( LPCWSTR str )
{
371 372 373 374 375 376 377 378 379 380 381 382 383 384
    ATOM                atom = 0;
    NTSTATUS            status;
    RTL_ATOM_TABLE      table;

    if ((table = get_local_table( 0 )))
    {
        status = RtlLookupAtomInAtomTable( table, str, &atom );
        if (status)
        {
            SetLastError( RtlNtStatusToDosError( status ) );
            atom = 0;
        }
    }
    return atom;
385 386 387
}


Alexandre Julliard's avatar
Alexandre Julliard committed
388
/***********************************************************************
389
 *           GlobalGetAtomNameA   (KERNEL32.@)
390
 *
Jon Griffiths's avatar
Jon Griffiths committed
391
 * Get a copy of the string associated with an atom.
392 393
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
394 395
 *	Success: The length of the returned string in characters.
 *	Failure: 0.
Alexandre Julliard's avatar
Alexandre Julliard committed
396
 */
397 398 399 400 401
UINT WINAPI GlobalGetAtomNameA(
              ATOM atom,    /* [in]  Atom identifier */
              LPSTR buffer, /* [out] Pointer to buffer for atom string */
              INT count )   /* [in]  Size of buffer */
{
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
    WCHAR       tmpW[MAX_ATOM_LEN + 1];
    UINT        wlen, len = 0, c;

    if (count <= 0) SetLastError( ERROR_MORE_DATA );
    else if ((wlen = GlobalGetAtomNameW( atom, tmpW, MAX_ATOM_LEN + 1 )))
    {
        char    tmp[MAX_ATOM_LEN + 1];

        len = WideCharToMultiByte( CP_ACP, 0, tmpW, wlen, tmp, MAX_ATOM_LEN + 1, NULL, NULL );
        c = min(len, count - 1);
        memcpy(buffer, tmp, c);
        buffer[c] = '\0';
        if (len >= count)
        {
            len = 0;
            SetLastError( ERROR_MORE_DATA );
        }
    }
    return len;
421 422 423 424
}


/***********************************************************************
425
 *           GetAtomNameA   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
426 427
 *
 * Get a copy of the string associated with an atom.
428 429
 *
 * RETURNS
Jon Griffiths's avatar
Jon Griffiths committed
430 431
 *	Success: The length of the returned string in characters.
 *	Failure: 0.
432 433 434 435 436 437
 */
UINT WINAPI GetAtomNameA(
              ATOM atom,    /* [in]  Atom */
              LPSTR buffer, /* [out] Pointer to string for atom string */
              INT count)    /* [in]  Size of buffer */
{
438 439
    WCHAR       tmpW[MAX_ATOM_LEN + 1];
    UINT        wlen, len = 0, c;
440

441 442
    if (count <= 0) SetLastError( ERROR_MORE_DATA );
    else if ((wlen = GetAtomNameW( atom, tmpW, MAX_ATOM_LEN + 1 )))
443
    {
444
        char    tmp[MAX_ATOM_LEN + 1];
445

446 447 448 449 450
        len = WideCharToMultiByte( CP_ACP, 0, tmpW, wlen, tmp, MAX_ATOM_LEN + 1, NULL, NULL );
        c = min(len, count - 1);
        memcpy(buffer, tmp, c);
        buffer[c] = '\0';
        if (len >= count)
451
        {
452 453
            len = c;
            SetLastError( ERROR_MORE_DATA );
454
        }
455 456
    }
    return len;
Alexandre Julliard's avatar
Alexandre Julliard committed
457
}
458 459 460


/***********************************************************************
461
 *           GlobalGetAtomNameW   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
462 463
 *
 * Unicode version of GlobalGetAtomNameA.
464 465 466
 */
UINT WINAPI GlobalGetAtomNameW( ATOM atom, LPWSTR buffer, INT count )
{
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
    char        ptr[sizeof(ATOM_BASIC_INFORMATION) + MAX_ATOM_LEN * sizeof(WCHAR)];
    ATOM_BASIC_INFORMATION*     abi = (ATOM_BASIC_INFORMATION*)ptr;
    ULONG       ptr_size = sizeof(ATOM_BASIC_INFORMATION) + MAX_ATOM_LEN * sizeof(WCHAR);
    NTSTATUS    status;
    UINT        length = 0;

    if (count <= 0)
    {
        SetLastError( ERROR_MORE_DATA );
        return 0;
    }
    status = NtQueryInformationAtom( atom, AtomBasicInformation, (void*)ptr, ptr_size, NULL );
    if (status) SetLastError( RtlNtStatusToDosError( status ) );
    else
    {
        length = min( abi->NameLength / sizeof(WCHAR), count);
        memcpy( buffer, abi->Name, length * sizeof(WCHAR) );
484 485 486
        /* yes, the string will not be null terminated if the passed buffer
         * is one WCHAR too small (and it's not an error)
         */
487 488 489 490 491
        if (length < abi->NameLength / sizeof(WCHAR))
        {
            SetLastError( ERROR_MORE_DATA );
            length = count;
        }
492
        else if (length < count) buffer[length] = '\0';
493 494
    }
    return length;
495 496 497 498
}


/***********************************************************************
499
 *           GetAtomNameW   (KERNEL32.@)
Jon Griffiths's avatar
Jon Griffiths committed
500 501
 *
 * Unicode version of GetAtomNameA.
502 503 504
 */
UINT WINAPI GetAtomNameW( ATOM atom, LPWSTR buffer, INT count )
{
505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
    NTSTATUS            status;
    RTL_ATOM_TABLE      table;
    DWORD               length;
    WCHAR               tmp[MAX_ATOM_LEN + 1];

    if (count <= 0)
    {
        SetLastError( ERROR_MORE_DATA );
        return 0;
    }
    if (!(table = get_local_table( 0 ))) return 0;
    length = sizeof(tmp);
    status = RtlQueryAtomInAtomTable( table, atom, NULL, NULL, tmp, &length );
    if (status)
    {
        SetLastError( RtlNtStatusToDosError( status ) );
        return 0;
    }
    length = min(length, (count - 1) * sizeof(WCHAR));
    if (length) memcpy(buffer, tmp, length);
    else SetLastError( ERROR_INSUFFICIENT_BUFFER );
    length /= sizeof(WCHAR);
    buffer[length] = '\0';
    return length;
529
}