4 * Copyright 1993, 1994, 1995 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Warning: The code assumes that LocalAlloc() returns a block aligned
23 * on a 4-bytes boundary (because of the shifting done in
24 * HANDLETOATOM). If this is not the case, the allocation code will
29 #include "wine/port.h"
40 #include "wine/server.h"
41 #include "wine/unicode.h"
42 #include "wine/winbase16.h"
45 #include "stackframe.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(atom);
51 #define DEFAULT_ATOMTABLE_SIZE 37
52 #define MAX_ATOM_LEN 255
54 #define ATOMTOHANDLE(atom) ((HANDLE16)(atom) << 2)
55 #define HANDLETOATOM(handle) ((ATOM)(0xc000 | ((handle) >> 2)))
72 /***********************************************************************
75 * Return a pointer to the atom table of a given segment, creating
79 * Pointer to table: Success
82 static ATOMTABLE *ATOM_GetTable( BOOL create /* [in] Create */ )
84 INSTANCEDATA *ptr = MapSL( MAKESEGPTR( CURRENT_DS, 0 ) );
87 ATOMTABLE *table = (ATOMTABLE *)((char *)ptr + ptr->atomtable);
88 if (table->size) return table;
90 if (!create) return NULL;
91 if (!InitAtomTable16( 0 )) return NULL;
92 /* Reload ptr in case it moved in linear memory */
93 ptr = MapSL( MAKESEGPTR( CURRENT_DS, 0 ) );
94 return (ATOMTABLE *)((char *)ptr + ptr->atomtable);
98 /***********************************************************************
101 * The hash value for the input string
103 static WORD ATOM_Hash(
104 WORD entries, /* [in] Total number of entries */
105 LPCSTR str, /* [in] Pointer to string to hash */
106 WORD len /* [in] Length of string */
110 TRACE("%x, %s, %x\n", entries, str, len);
112 for (i = 0; i < len; i++) hash ^= toupper(str[i]) + i;
113 return hash % entries;
117 /***********************************************************************
120 static BOOL ATOM_IsIntAtomA(LPCSTR atomstr,WORD *atomid)
123 if (!HIWORD(atomstr)) atom = LOWORD(atomstr);
126 if (*atomstr++ != '#') return FALSE;
127 while (*atomstr >= '0' && *atomstr <= '9')
129 atom = atom * 10 + *atomstr - '0';
132 if (*atomstr) return FALSE;
134 if (atom >= MAXINTATOM)
136 SetLastError( ERROR_INVALID_PARAMETER );
144 /***********************************************************************
147 static BOOL ATOM_IsIntAtomW(LPCWSTR atomstr,WORD *atomid)
150 if (!HIWORD(atomstr)) atom = LOWORD(atomstr);
153 if (*atomstr++ != '#') return FALSE;
154 while (*atomstr >= '0' && *atomstr <= '9')
156 atom = atom * 10 + *atomstr - '0';
159 if (*atomstr) return FALSE;
161 if (atom >= MAXINTATOM)
163 SetLastError( ERROR_INVALID_PARAMETER );
171 /***********************************************************************
174 * Make an ATOMENTRY pointer from a handle (obtained from GetAtomHandle()).
176 static inline ATOMENTRY *ATOM_MakePtr( HANDLE16 handle /* [in] Handle */ )
178 return MapSL( MAKESEGPTR( CURRENT_DS, handle ) );
182 /***********************************************************************
183 * InitAtomTable (KERNEL.68)
185 WORD WINAPI InitAtomTable16( WORD entries )
191 /* Allocate the table */
193 if (!entries) entries = DEFAULT_ATOMTABLE_SIZE; /* sanity check */
194 handle = LocalAlloc16( LMEM_FIXED, sizeof(ATOMTABLE) + (entries-1) * sizeof(HANDLE16) );
195 if (!handle) return 0;
196 table = MapSL( MAKESEGPTR( CURRENT_DS, handle ) );
197 table->size = entries;
198 for (i = 0; i < entries; i++) table->entries[i] = 0;
200 /* Store a pointer to the table in the instance data */
202 ((INSTANCEDATA *)MapSL( MAKESEGPTR( CURRENT_DS, 0 )))->atomtable = handle;
206 /***********************************************************************
207 * GetAtomHandle (KERNEL.73)
209 HANDLE16 WINAPI GetAtomHandle16( ATOM atom )
211 if (atom < MAXINTATOM) return 0;
212 return ATOMTOHANDLE( atom );
216 /***********************************************************************
217 * AddAtom (KERNEL.70)
219 * Windows DWORD aligns the atom entry size.
220 * The remaining unused string space created by the alignment
221 * gets padded with '\0's in a certain way to ensure
222 * that at least one trailing '\0' remains.
228 ATOM WINAPI AddAtom16( LPCSTR str )
230 char buffer[MAX_ATOM_LEN+1];
233 ATOMENTRY * entryPtr;
238 if (ATOM_IsIntAtomA( str, &iatom )) return iatom;
240 TRACE("%s\n",debugstr_a(buffer));
242 /* Make a copy of the string to be sure it doesn't move in linear memory. */
243 lstrcpynA( buffer, str, sizeof(buffer) );
245 len = strlen( buffer );
246 if (!(table = ATOM_GetTable( TRUE ))) return 0;
248 hash = ATOM_Hash( table->size, buffer, len );
249 entry = table->entries[hash];
252 entryPtr = ATOM_MakePtr( entry );
253 if ((entryPtr->length == len) &&
254 (!strncasecmp( entryPtr->str, buffer, len )))
256 entryPtr->refCount++;
257 TRACE("-- existing 0x%x\n", entry);
258 return HANDLETOATOM( entry );
260 entry = entryPtr->next;
263 ae_len = (sizeof(ATOMENTRY)+len+3) & ~3;
264 entry = LocalAlloc16( LMEM_FIXED, ae_len );
265 if (!entry) return 0;
266 /* Reload the table ptr in case it moved in linear memory */
267 table = ATOM_GetTable( FALSE );
268 entryPtr = ATOM_MakePtr( entry );
269 entryPtr->next = table->entries[hash];
270 entryPtr->refCount = 1;
271 entryPtr->length = len;
272 /* Some applications _need_ the '\0' padding provided by this strncpy */
273 strncpy( entryPtr->str, buffer, ae_len - sizeof(ATOMENTRY) + 1 );
274 entryPtr->str[ae_len - sizeof(ATOMENTRY)] = '\0';
275 table->entries[hash] = entry;
276 TRACE("-- new 0x%x\n", entry);
277 return HANDLETOATOM( entry );
281 /***********************************************************************
282 * DeleteAtom (KERNEL.71)
284 ATOM WINAPI DeleteAtom16( ATOM atom )
286 ATOMENTRY * entryPtr;
288 HANDLE16 entry, *prevEntry;
291 if (atom < MAXINTATOM) return 0; /* Integer atom */
293 TRACE("0x%x\n",atom);
295 if (!(table = ATOM_GetTable( FALSE ))) return 0;
296 entry = ATOMTOHANDLE( atom );
297 entryPtr = ATOM_MakePtr( entry );
299 /* Find previous atom */
300 hash = ATOM_Hash( table->size, entryPtr->str, entryPtr->length );
301 prevEntry = &table->entries[hash];
302 while (*prevEntry && *prevEntry != entry)
304 ATOMENTRY * prevEntryPtr = ATOM_MakePtr( *prevEntry );
305 prevEntry = &prevEntryPtr->next;
307 if (!*prevEntry) return atom;
310 if (--entryPtr->refCount == 0)
312 *prevEntry = entryPtr->next;
313 LocalFree16( entry );
319 /***********************************************************************
320 * FindAtom (KERNEL.69)
322 ATOM WINAPI FindAtom16( LPCSTR str )
329 TRACE("%s\n",debugstr_a(str));
331 if (ATOM_IsIntAtomA( str, &iatom )) return iatom;
332 if ((len = strlen( str )) > 255) len = 255;
333 if (!(table = ATOM_GetTable( FALSE ))) return 0;
334 hash = ATOM_Hash( table->size, str, len );
335 entry = table->entries[hash];
338 ATOMENTRY * entryPtr = ATOM_MakePtr( entry );
339 if ((entryPtr->length == len) &&
340 (!strncasecmp( entryPtr->str, str, len )))
342 TRACE("-- found %x\n", entry);
343 return HANDLETOATOM( entry );
345 entry = entryPtr->next;
347 TRACE("-- not found\n");
352 /***********************************************************************
353 * GetAtomName (KERNEL.72)
355 UINT16 WINAPI GetAtomName16( ATOM atom, LPSTR buffer, INT16 count )
358 ATOMENTRY * entryPtr;
366 if (!count) return 0;
367 if (atom < MAXINTATOM)
369 sprintf( text, "#%d", atom );
375 if (!(table = ATOM_GetTable( FALSE ))) return 0;
376 entry = ATOMTOHANDLE( atom );
377 entryPtr = ATOM_MakePtr( entry );
378 len = entryPtr->length;
379 strPtr = entryPtr->str;
381 if (len >= count) len = count-1;
382 memcpy( buffer, strPtr, len );
387 /***********************************************************************
388 * InitAtomTable (KERNEL32.@)
390 BOOL WINAPI InitAtomTable( DWORD entries )
393 SERVER_START_REQ( init_atom_table )
395 req->entries = entries;
396 ret = !wine_server_call_err( req );
403 static ATOM ATOM_AddAtomA( LPCSTR str, BOOL local )
406 if (!ATOM_IsIntAtomA( str, &atom ))
408 WCHAR buffer[MAX_ATOM_LEN];
410 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, strlen(str), buffer, MAX_ATOM_LEN );
413 SetLastError( ERROR_INVALID_PARAMETER );
416 SERVER_START_REQ( add_atom )
418 wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
420 if (!wine_server_call_err(req)) atom = reply->atom;
424 TRACE( "(%s) %s -> %x\n", local ? "local" : "global", debugstr_a(str), atom );
429 /***********************************************************************
430 * GlobalAddAtomA (KERNEL32.@)
432 * Adds a character string to the global atom table and returns a unique
433 * value identifying the string.
439 ATOM WINAPI GlobalAddAtomA( LPCSTR str /* [in] Pointer to string to add */ )
441 return ATOM_AddAtomA( str, FALSE );
445 /***********************************************************************
446 * AddAtomA (KERNEL32.@)
447 * Adds a string to the atom table and returns the atom identifying the
454 ATOM WINAPI AddAtomA( LPCSTR str /* [in] Pointer to string to add */ )
456 return ATOM_AddAtomA( str, TRUE );
460 static ATOM ATOM_AddAtomW( LPCWSTR str, BOOL local )
463 if (!ATOM_IsIntAtomW( str, &atom ))
465 DWORD len = strlenW(str);
466 if (len > MAX_ATOM_LEN)
468 SetLastError( ERROR_INVALID_PARAMETER );
471 SERVER_START_REQ( add_atom )
474 wine_server_add_data( req, str, len * sizeof(WCHAR) );
475 if (!wine_server_call_err(req)) atom = reply->atom;
479 TRACE( "(%s) %s -> %x\n", local ? "local" : "global", debugstr_w(str), atom );
484 /***********************************************************************
485 * GlobalAddAtomW (KERNEL32.@)
487 ATOM WINAPI GlobalAddAtomW( LPCWSTR str )
489 return ATOM_AddAtomW( str, FALSE );
493 /***********************************************************************
494 * AddAtomW (KERNEL32.@)
496 ATOM WINAPI AddAtomW( LPCWSTR str )
498 return ATOM_AddAtomW( str, TRUE );
502 static ATOM ATOM_DeleteAtom( ATOM atom, BOOL local)
504 TRACE( "(%s) %x\n", local ? "local" : "global", atom );
505 if (atom >= MAXINTATOM)
507 SERVER_START_REQ( delete_atom )
511 wine_server_call_err( req );
519 /***********************************************************************
520 * GlobalDeleteAtom (KERNEL32.@)
521 * Decrements the reference count of a string atom. If the count is
522 * zero, the string associated with the atom is removed from the table.
528 ATOM WINAPI GlobalDeleteAtom( ATOM atom /* [in] Atom to delete */ )
530 return ATOM_DeleteAtom( atom, FALSE);
534 /***********************************************************************
535 * DeleteAtom (KERNEL32.@)
536 * Decrements the reference count of a string atom. If count becomes
537 * zero, the string associated with the atom is removed from the table.
543 ATOM WINAPI DeleteAtom( ATOM atom /* [in] Atom to delete */ )
545 return ATOM_DeleteAtom( atom, TRUE );
549 static ATOM ATOM_FindAtomA( LPCSTR str, BOOL local )
552 if (!ATOM_IsIntAtomA( str, &atom ))
554 WCHAR buffer[MAX_ATOM_LEN];
556 DWORD len = MultiByteToWideChar( CP_ACP, 0, str, strlen(str), buffer, MAX_ATOM_LEN );
559 SetLastError( ERROR_INVALID_PARAMETER );
562 SERVER_START_REQ( find_atom )
565 wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
566 if (!wine_server_call_err(req)) atom = reply->atom;
570 TRACE( "(%s) %s -> %x\n", local ? "local" : "global", debugstr_a(str), atom );
575 /***********************************************************************
576 * GlobalFindAtomA (KERNEL32.@)
578 * Searches the atom table for the string and returns the atom
579 * associated with it.
585 ATOM WINAPI GlobalFindAtomA( LPCSTR str /* [in] Pointer to string to search for */ )
587 return ATOM_FindAtomA( str, FALSE );
590 /***********************************************************************
591 * FindAtomA (KERNEL32.@)
592 * Searches the local atom table for the string and returns the atom
593 * associated with that string.
599 ATOM WINAPI FindAtomA( LPCSTR str /* [in] Pointer to string to find */ )
601 return ATOM_FindAtomA( str, TRUE );
605 static ATOM ATOM_FindAtomW( LPCWSTR str, BOOL local )
608 if (!ATOM_IsIntAtomW( str, &atom ))
610 DWORD len = strlenW(str);
611 if (len > MAX_ATOM_LEN)
613 SetLastError( ERROR_INVALID_PARAMETER );
616 SERVER_START_REQ( find_atom )
618 wine_server_add_data( req, str, len * sizeof(WCHAR) );
620 if (!wine_server_call_err( req )) atom = reply->atom;
624 TRACE( "(%s) %s -> %x\n", local ? "local" : "global", debugstr_w(str), atom );
629 /***********************************************************************
630 * GlobalFindAtomW (KERNEL32.@)
632 ATOM WINAPI GlobalFindAtomW( LPCWSTR str )
634 return ATOM_FindAtomW( str, FALSE );
638 /***********************************************************************
639 * FindAtomW (KERNEL32.@)
641 ATOM WINAPI FindAtomW( LPCWSTR str )
643 return ATOM_FindAtomW( str, TRUE );
647 static UINT ATOM_GetAtomNameA( ATOM atom, LPSTR buffer, INT count, BOOL local )
653 SetLastError( ERROR_MORE_DATA );
656 if (atom < MAXINTATOM)
661 SetLastError( ERROR_INVALID_PARAMETER );
664 len = sprintf( name, "#%d", atom );
665 lstrcpynA( buffer, name, count );
669 WCHAR full_name[MAX_ATOM_LEN];
672 SERVER_START_REQ( get_atom_name )
676 wine_server_set_reply( req, full_name, sizeof(full_name) );
677 if (!wine_server_call_err( req ))
679 len = WideCharToMultiByte( CP_ACP, 0, full_name,
680 wine_server_reply_size(reply) / sizeof(WCHAR),
681 buffer, count - 1, NULL, NULL );
682 if (!len) len = count; /* overflow */
683 else buffer[len] = 0;
689 if (len && count <= len)
691 SetLastError( ERROR_MORE_DATA );
695 TRACE( "(%s) %x -> %s\n", local ? "local" : "global", atom, debugstr_a(buffer) );
700 /***********************************************************************
701 * GlobalGetAtomNameA (KERNEL32.@)
703 * Retrieves a copy of the string associated with an atom.
706 * Length of string in characters: Success
709 UINT WINAPI GlobalGetAtomNameA(
710 ATOM atom, /* [in] Atom identifier */
711 LPSTR buffer, /* [out] Pointer to buffer for atom string */
712 INT count ) /* [in] Size of buffer */
714 return ATOM_GetAtomNameA( atom, buffer, count, FALSE );
718 /***********************************************************************
719 * GetAtomNameA (KERNEL32.@)
720 * Retrieves a copy of the string associated with the atom.
723 * Length of string: Success
726 UINT WINAPI GetAtomNameA(
727 ATOM atom, /* [in] Atom */
728 LPSTR buffer, /* [out] Pointer to string for atom string */
729 INT count) /* [in] Size of buffer */
731 return ATOM_GetAtomNameA( atom, buffer, count, TRUE );
735 static UINT ATOM_GetAtomNameW( ATOM atom, LPWSTR buffer, INT count, BOOL local )
741 SetLastError( ERROR_MORE_DATA );
744 if (atom < MAXINTATOM)
749 SetLastError( ERROR_INVALID_PARAMETER );
752 sprintf( name, "#%d", atom );
753 len = MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, count );
754 if (!len) buffer[count-1] = 0; /* overflow */
758 WCHAR full_name[MAX_ATOM_LEN];
761 SERVER_START_REQ( get_atom_name )
765 wine_server_set_reply( req, full_name, sizeof(full_name) );
766 if (!wine_server_call_err( req ))
768 len = wine_server_reply_size(reply) / sizeof(WCHAR);
769 if (count > len) count = len + 1;
770 memcpy( buffer, full_name, (count-1) * sizeof(WCHAR) );
777 TRACE( "(%s) %x -> %s\n", local ? "local" : "global", atom, debugstr_w(buffer) );
782 /***********************************************************************
783 * GlobalGetAtomNameW (KERNEL32.@)
785 UINT WINAPI GlobalGetAtomNameW( ATOM atom, LPWSTR buffer, INT count )
787 return ATOM_GetAtomNameW( atom, buffer, count, FALSE);
791 /***********************************************************************
792 * GetAtomNameW (KERNEL32.@)
794 UINT WINAPI GetAtomNameW( ATOM atom, LPWSTR buffer, INT count )
796 return ATOM_GetAtomNameW( atom, buffer, count, TRUE );