Fixed import directory limits (spotted by Jan Kratochvil).
[wine] / memory / atom.c
1 /*
2  * Atom table functions
3  *
4  * Copyright 1993, 1994, 1995 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 /*
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
25  * have to be changed.
26  */
27
28 #include "config.h"
29 #include "wine/port.h"
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
35
36 #include "windef.h"
37 #include "winbase.h"
38 #include "winerror.h"
39
40 #include "wine/server.h"
41 #include "wine/unicode.h"
42 #include "wine/winbase16.h"
43 #include "global.h"
44 #include "instance.h"
45 #include "stackframe.h"
46
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(atom);
50
51 #define DEFAULT_ATOMTABLE_SIZE    37
52 #define MAX_ATOM_LEN              255
53
54 #define ATOMTOHANDLE(atom)        ((HANDLE16)(atom) << 2)
55 #define HANDLETOATOM(handle)      ((ATOM)(0xc000 | ((handle) >> 2)))
56
57 typedef struct
58 {
59     HANDLE16    next;
60     WORD        refCount;
61     BYTE        length;
62     BYTE        str[1];
63 } ATOMENTRY;
64
65 typedef struct
66 {
67     WORD        size;
68     HANDLE16    entries[1];
69 } ATOMTABLE;
70
71
72 /***********************************************************************
73  *           ATOM_GetTable
74  *
75  * Return a pointer to the atom table of a given segment, creating
76  * it if necessary.
77  *
78  * RETURNS
79  *      Pointer to table: Success
80  *      NULL: Failure
81  */
82 static ATOMTABLE *ATOM_GetTable( BOOL create  /* [in] Create */ )
83 {
84     INSTANCEDATA *ptr = MapSL( MAKESEGPTR( CURRENT_DS, 0 ) );
85     if (ptr->atomtable)
86     {
87         ATOMTABLE *table = (ATOMTABLE *)((char *)ptr + ptr->atomtable);
88         if (table->size) return table;
89     }
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);
95 }
96
97
98 /***********************************************************************
99  *           ATOM_Hash
100  * RETURNS
101  *      The hash value for the input string
102  */
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 */
107 ) {
108     WORD i, hash = 0;
109
110     TRACE("%x, %s, %x\n", entries, str, len);
111
112     for (i = 0; i < len; i++) hash ^= toupper(str[i]) + i;
113     return hash % entries;
114 }
115
116
117 /***********************************************************************
118  *           ATOM_IsIntAtomA
119  */
120 static BOOL ATOM_IsIntAtomA(LPCSTR atomstr,WORD *atomid)
121 {
122     UINT atom = 0;
123     if (!HIWORD(atomstr)) atom = LOWORD(atomstr);
124     else
125     {
126         if (*atomstr++ != '#') return FALSE;
127         while (*atomstr >= '0' && *atomstr <= '9')
128         {
129             atom = atom * 10 + *atomstr - '0';
130             atomstr++;
131         }
132         if (*atomstr) return FALSE;
133     }
134     if (!atom || (atom >= MAXINTATOM))
135     {
136         SetLastError( ERROR_INVALID_PARAMETER );
137         atom = 0;
138     }
139     *atomid = atom;
140     return TRUE;
141 }
142
143
144 /***********************************************************************
145  *           ATOM_IsIntAtomW
146  */
147 static BOOL ATOM_IsIntAtomW(LPCWSTR atomstr,WORD *atomid)
148 {
149     UINT atom = 0;
150     if (!HIWORD(atomstr)) atom = LOWORD(atomstr);
151     else
152     {
153         if (*atomstr++ != '#') return FALSE;
154         while (*atomstr >= '0' && *atomstr <= '9')
155         {
156             atom = atom * 10 + *atomstr - '0';
157             atomstr++;
158         }
159         if (*atomstr) return FALSE;
160     }
161     if (!atom || (atom >= MAXINTATOM))
162     {
163         SetLastError( ERROR_INVALID_PARAMETER );
164         atom = 0;
165     }
166     *atomid = atom;
167     return TRUE;
168 }
169
170
171 /***********************************************************************
172  *           ATOM_MakePtr
173  *
174  * Make an ATOMENTRY pointer from a handle (obtained from GetAtomHandle()).
175  */
176 static inline ATOMENTRY *ATOM_MakePtr( HANDLE16 handle /* [in] Handle */ )
177 {
178     return MapSL( MAKESEGPTR( CURRENT_DS, handle ) );
179 }
180
181
182 /***********************************************************************
183  *           InitAtomTable   (KERNEL.68)
184  */
185 WORD WINAPI InitAtomTable16( WORD entries )
186 {
187     int i;
188     HANDLE16 handle;
189     ATOMTABLE *table;
190
191       /* Allocate the table */
192
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;
199
200       /* Store a pointer to the table in the instance data */
201
202     ((INSTANCEDATA *)MapSL( MAKESEGPTR( CURRENT_DS, 0 )))->atomtable = handle;
203     return handle;
204 }
205
206 /***********************************************************************
207  *           GetAtomHandle   (KERNEL.73)
208  */
209 HANDLE16 WINAPI GetAtomHandle16( ATOM atom )
210 {
211     if (atom < MAXINTATOM) return 0;
212     return ATOMTOHANDLE( atom );
213 }
214
215
216 /***********************************************************************
217  *           AddAtom   (KERNEL.70)
218  *
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.
223  *
224  * RETURNS
225  *      Atom: Success
226  *      0: Failure
227  */
228 ATOM WINAPI AddAtom16( LPCSTR str )
229 {
230     char buffer[MAX_ATOM_LEN+1];
231     WORD hash;
232     HANDLE16 entry;
233     ATOMENTRY * entryPtr;
234     ATOMTABLE * table;
235     int len, ae_len;
236     WORD        iatom;
237
238     if (ATOM_IsIntAtomA( str, &iatom )) return iatom;
239
240     TRACE("%s\n",debugstr_a(buffer));
241
242     /* Make a copy of the string to be sure it doesn't move in linear memory. */
243     lstrcpynA( buffer, str, sizeof(buffer) );
244
245     len = strlen( buffer );
246     if (!(table = ATOM_GetTable( TRUE ))) return 0;
247
248     hash = ATOM_Hash( table->size, buffer, len );
249     entry = table->entries[hash];
250     while (entry)
251     {
252         entryPtr = ATOM_MakePtr( entry );
253         if ((entryPtr->length == len) &&
254             (!strncasecmp( entryPtr->str, buffer, len )))
255         {
256             entryPtr->refCount++;
257             TRACE("-- existing 0x%x\n", entry);
258             return HANDLETOATOM( entry );
259         }
260         entry = entryPtr->next;
261     }
262
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 );
278 }
279
280
281 /***********************************************************************
282  *           DeleteAtom   (KERNEL.71)
283  */
284 ATOM WINAPI DeleteAtom16( ATOM atom )
285 {
286     ATOMENTRY * entryPtr;
287     ATOMTABLE * table;
288     HANDLE16 entry, *prevEntry;
289     WORD hash;
290
291     if (atom < MAXINTATOM) return 0;  /* Integer atom */
292
293     TRACE("0x%x\n",atom);
294
295     if (!(table = ATOM_GetTable( FALSE ))) return 0;
296     entry = ATOMTOHANDLE( atom );
297     entryPtr = ATOM_MakePtr( entry );
298
299     /* Find previous atom */
300     hash = ATOM_Hash( table->size, entryPtr->str, entryPtr->length );
301     prevEntry = &table->entries[hash];
302     while (*prevEntry && *prevEntry != entry)
303     {
304         ATOMENTRY * prevEntryPtr = ATOM_MakePtr( *prevEntry );
305         prevEntry = &prevEntryPtr->next;
306     }
307     if (!*prevEntry) return atom;
308
309     /* Delete atom */
310     if (--entryPtr->refCount == 0)
311     {
312         *prevEntry = entryPtr->next;
313         LocalFree16( entry );
314     }
315     return 0;
316 }
317
318
319 /***********************************************************************
320  *           FindAtom   (KERNEL.69)
321  */
322 ATOM WINAPI FindAtom16( LPCSTR str )
323 {
324     ATOMTABLE * table;
325     WORD hash,iatom;
326     HANDLE16 entry;
327     int len;
328
329     TRACE("%s\n",debugstr_a(str));
330
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];
336     while (entry)
337     {
338         ATOMENTRY * entryPtr = ATOM_MakePtr( entry );
339         if ((entryPtr->length == len) &&
340             (!strncasecmp( entryPtr->str, str, len )))
341         {
342             TRACE("-- found %x\n", entry);
343             return HANDLETOATOM( entry );
344         }
345         entry = entryPtr->next;
346     }
347     TRACE("-- not found\n");
348     return 0;
349 }
350
351
352 /***********************************************************************
353  *           GetAtomName   (KERNEL.72)
354  */
355 UINT16 WINAPI GetAtomName16( ATOM atom, LPSTR buffer, INT16 count )
356 {
357     ATOMTABLE * table;
358     ATOMENTRY * entryPtr;
359     HANDLE16 entry;
360     char * strPtr;
361     UINT len;
362     char text[8];
363
364     TRACE("%x\n",atom);
365
366     if (!count) return 0;
367     if (atom < MAXINTATOM)
368     {
369         sprintf( text, "#%d", atom );
370         len = strlen(text);
371         strPtr = text;
372     }
373     else
374     {
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;
380     }
381     if (len >= count) len = count-1;
382     memcpy( buffer, strPtr, len );
383     buffer[len] = '\0';
384     return len;
385 }
386
387 /***********************************************************************
388  *           InitAtomTable   (KERNEL32.@)
389  */
390 BOOL WINAPI InitAtomTable( DWORD entries )
391 {
392     BOOL ret;
393     SERVER_START_REQ( init_atom_table )
394     {
395         req->entries = entries;
396         ret = !wine_server_call_err( req );
397     }
398     SERVER_END_REQ;
399     return ret;
400 }
401
402
403 static ATOM ATOM_AddAtomA( LPCSTR str, BOOL local )
404 {
405     ATOM atom = 0;
406     if (!ATOM_IsIntAtomA( str, &atom ))
407     {
408         WCHAR buffer[MAX_ATOM_LEN];
409
410         DWORD len = MultiByteToWideChar( CP_ACP, 0, str, strlen(str), buffer, MAX_ATOM_LEN );
411         if (!len)
412         {
413             SetLastError( ERROR_INVALID_PARAMETER );
414             return 0;
415         }
416         SERVER_START_REQ( add_atom )
417         {
418             wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
419             req->local = local;
420             if (!wine_server_call_err(req)) atom = reply->atom;
421         }
422         SERVER_END_REQ;
423     }
424     TRACE( "(%s) %s -> %x\n", local ? "local" : "global", debugstr_a(str), atom );
425     return atom;
426 }
427
428
429 /***********************************************************************
430  *           GlobalAddAtomA   (KERNEL32.@)
431  *
432  * Adds a character string to the global atom table and returns a unique
433  * value identifying the string.
434  *
435  * RETURNS
436  *      Atom: Success
437  *      0: Failure
438  */
439 ATOM WINAPI GlobalAddAtomA( LPCSTR str /* [in] Pointer to string to add */ )
440 {
441     return ATOM_AddAtomA( str, FALSE );
442 }
443
444
445 /***********************************************************************
446  *           AddAtomA   (KERNEL32.@)
447  * Adds a string to the atom table and returns the atom identifying the
448  * string.
449  *
450  * RETURNS
451  *      Atom: Success
452  *      0: Failure
453  */
454 ATOM WINAPI AddAtomA( LPCSTR str /* [in] Pointer to string to add */ )
455 {
456     return ATOM_AddAtomA( str, TRUE );
457 }
458
459
460 static ATOM ATOM_AddAtomW( LPCWSTR str, BOOL local )
461 {
462     ATOM atom = 0;
463     if (!ATOM_IsIntAtomW( str, &atom ))
464     {
465         DWORD len = strlenW(str);
466         if (len > MAX_ATOM_LEN)
467         {
468             SetLastError( ERROR_INVALID_PARAMETER );
469             return 0;
470         }
471         SERVER_START_REQ( add_atom )
472         {
473             req->local = local;
474             wine_server_add_data( req, str, len * sizeof(WCHAR) );
475             if (!wine_server_call_err(req)) atom = reply->atom;
476         }
477         SERVER_END_REQ;
478     }
479     TRACE( "(%s) %s -> %x\n", local ? "local" : "global", debugstr_w(str), atom );
480     return atom;
481 }
482
483
484 /***********************************************************************
485  *           GlobalAddAtomW   (KERNEL32.@)
486  */
487 ATOM WINAPI GlobalAddAtomW( LPCWSTR str )
488 {
489     return ATOM_AddAtomW( str, FALSE );
490 }
491
492
493 /***********************************************************************
494  *           AddAtomW   (KERNEL32.@)
495  */
496 ATOM WINAPI AddAtomW( LPCWSTR str )
497 {
498     return ATOM_AddAtomW( str, TRUE );
499 }
500
501
502 static ATOM ATOM_DeleteAtom( ATOM atom,  BOOL local)
503 {
504     TRACE( "(%s) %x\n", local ? "local" : "global", atom );
505     if (atom < MAXINTATOM) atom = 0;
506     else
507     {
508         SERVER_START_REQ( delete_atom )
509         {
510             req->atom = atom;
511             req->local = local;
512             if (!wine_server_call_err( req )) atom = 0;
513         }
514         SERVER_END_REQ;
515     }
516     return atom;
517 }
518
519
520 /***********************************************************************
521  *           GlobalDeleteAtom   (KERNEL32.@)
522  * Decrements the reference count of a string atom.  If the count is
523  * zero, the string associated with the atom is removed from the table.
524  *
525  * RETURNS
526  *      0: Success
527  *      Atom: Failure
528  */
529 ATOM WINAPI GlobalDeleteAtom( ATOM atom /* [in] Atom to delete */ )
530 {
531     return ATOM_DeleteAtom( atom, FALSE);
532 }
533
534
535 /***********************************************************************
536  *           DeleteAtom   (KERNEL32.@)
537  * Decrements the reference count of a string atom.  If count becomes
538  * zero, the string associated with the atom is removed from the table.
539  *
540  * RETURNS
541  *      0: Success
542  *      Atom: Failure
543  */
544 ATOM WINAPI DeleteAtom( ATOM atom /* [in] Atom to delete */ )
545 {
546     return ATOM_DeleteAtom( atom, TRUE );
547 }
548
549
550 static ATOM ATOM_FindAtomA( LPCSTR str, BOOL local )
551 {
552     ATOM atom = 0;
553     if (!ATOM_IsIntAtomA( str, &atom ))
554     {
555         WCHAR buffer[MAX_ATOM_LEN];
556
557         DWORD len = MultiByteToWideChar( CP_ACP, 0, str, strlen(str), buffer, MAX_ATOM_LEN );
558         if (!len)
559         {
560             SetLastError( ERROR_INVALID_PARAMETER );
561             return 0;
562         }
563         SERVER_START_REQ( find_atom )
564         {
565             req->local = local;
566             wine_server_add_data( req, buffer, len * sizeof(WCHAR) );
567             if (!wine_server_call_err(req)) atom = reply->atom;
568         }
569         SERVER_END_REQ;
570     }
571     TRACE( "(%s) %s -> %x\n", local ? "local" : "global", debugstr_a(str), atom );
572     return atom;
573 }
574
575
576 /***********************************************************************
577  *           GlobalFindAtomA   (KERNEL32.@)
578  *
579  * Searches the atom table for the string and returns the atom
580  * associated with it.
581  *
582  * RETURNS
583  *      Atom: Success
584  *      0: Failure
585  */
586 ATOM WINAPI GlobalFindAtomA( LPCSTR str /* [in] Pointer to string to search for */ )
587 {
588     return ATOM_FindAtomA( str, FALSE );
589 }
590
591 /***********************************************************************
592  *           FindAtomA   (KERNEL32.@)
593  * Searches the local atom table for the string and returns the atom
594  * associated with that string.
595  *
596  * RETURNS
597  *      Atom: Success
598  *      0: Failure
599  */
600 ATOM WINAPI FindAtomA( LPCSTR str /* [in] Pointer to string to find */ )
601 {
602     return ATOM_FindAtomA( str, TRUE );
603 }
604
605
606 static ATOM ATOM_FindAtomW( LPCWSTR str, BOOL local )
607 {
608     ATOM atom = 0;
609     if (!ATOM_IsIntAtomW( str, &atom ))
610     {
611         DWORD len = strlenW(str);
612         if (len > MAX_ATOM_LEN)
613         {
614             SetLastError( ERROR_INVALID_PARAMETER );
615             return 0;
616         }
617         SERVER_START_REQ( find_atom )
618         {
619             wine_server_add_data( req, str, len * sizeof(WCHAR) );
620             req->local = local;
621             if (!wine_server_call_err( req )) atom = reply->atom;
622         }
623         SERVER_END_REQ;
624     }
625     TRACE( "(%s) %s -> %x\n", local ? "local" : "global", debugstr_w(str), atom );
626     return atom;
627 }
628
629
630 /***********************************************************************
631  *           GlobalFindAtomW   (KERNEL32.@)
632  */
633 ATOM WINAPI GlobalFindAtomW( LPCWSTR str )
634 {
635     return ATOM_FindAtomW( str, FALSE );
636 }
637
638
639 /***********************************************************************
640  *           FindAtomW   (KERNEL32.@)
641  */
642 ATOM WINAPI FindAtomW( LPCWSTR str )
643 {
644     return ATOM_FindAtomW( str, TRUE );
645 }
646
647
648 static UINT ATOM_GetAtomNameA( ATOM atom, LPSTR buffer, INT count, BOOL local )
649 {
650     INT len;
651
652     if (count <= 0)
653     {
654         SetLastError( ERROR_MORE_DATA );
655         return 0;
656     }
657     if (atom < MAXINTATOM)
658     {
659         char name[8];
660         if (!atom)
661         {
662             SetLastError( ERROR_INVALID_PARAMETER );
663             return 0;
664         }
665         len = sprintf( name, "#%d", atom );
666         lstrcpynA( buffer, name, count );
667     }
668     else
669     {
670         WCHAR full_name[MAX_ATOM_LEN];
671
672         len = 0;
673         SERVER_START_REQ( get_atom_name )
674         {
675             req->atom = atom;
676             req->local = local;
677             wine_server_set_reply( req, full_name, sizeof(full_name) );
678             if (!wine_server_call_err( req ))
679             {
680                 len = WideCharToMultiByte( CP_ACP, 0, full_name,
681                                            wine_server_reply_size(reply) / sizeof(WCHAR),
682                                            buffer, count - 1, NULL, NULL );
683                 if (!len) len = count; /* overflow */
684                 else buffer[len] = 0;
685             }
686         }
687         SERVER_END_REQ;
688     }
689
690     if (len && count <= len)
691     {
692         SetLastError( ERROR_MORE_DATA );
693         buffer[count-1] = 0;
694         return 0;
695     }
696     TRACE( "(%s) %x -> %s\n", local ? "local" : "global", atom, debugstr_a(buffer) );
697     return len;
698 }
699
700
701 /***********************************************************************
702  *           GlobalGetAtomNameA   (KERNEL32.@)
703  *
704  * Retrieves a copy of the string associated with an atom.
705  *
706  * RETURNS
707  *      Length of string in characters: Success
708  *      0: Failure
709  */
710 UINT WINAPI GlobalGetAtomNameA(
711               ATOM atom,    /* [in]  Atom identifier */
712               LPSTR buffer, /* [out] Pointer to buffer for atom string */
713               INT count )   /* [in]  Size of buffer */
714 {
715     return ATOM_GetAtomNameA( atom, buffer, count, FALSE );
716 }
717
718
719 /***********************************************************************
720  *           GetAtomNameA   (KERNEL32.@)
721  * Retrieves a copy of the string associated with the atom.
722  *
723  * RETURNS
724  *      Length of string: Success
725  *      0: Failure
726  */
727 UINT WINAPI GetAtomNameA(
728               ATOM atom,    /* [in]  Atom */
729               LPSTR buffer, /* [out] Pointer to string for atom string */
730               INT count)    /* [in]  Size of buffer */
731 {
732     return ATOM_GetAtomNameA( atom, buffer, count, TRUE );
733 }
734
735
736 static UINT ATOM_GetAtomNameW( ATOM atom, LPWSTR buffer, INT count, BOOL local )
737 {
738     INT len;
739
740     if (count <= 0)
741     {
742         SetLastError( ERROR_MORE_DATA );
743         return 0;
744     }
745     if (atom < MAXINTATOM)
746     {
747         char name[8];
748         if (!atom)
749         {
750             SetLastError( ERROR_INVALID_PARAMETER );
751             return 0;
752         }
753         sprintf( name, "#%d", atom );
754         len = MultiByteToWideChar( CP_ACP, 0, name, -1, buffer, count );
755         if (!len) buffer[count-1] = 0;  /* overflow */
756     }
757     else
758     {
759         WCHAR full_name[MAX_ATOM_LEN];
760
761         len = 0;
762         SERVER_START_REQ( get_atom_name )
763         {
764             req->atom = atom;
765             req->local = local;
766             wine_server_set_reply( req, full_name, sizeof(full_name) );
767             if (!wine_server_call_err( req ))
768             {
769                 len = wine_server_reply_size(reply) / sizeof(WCHAR);
770                 if (count > len) count = len + 1;
771                 memcpy( buffer, full_name, (count-1) * sizeof(WCHAR) );
772                 buffer[count-1] = 0;
773             }
774         }
775         SERVER_END_REQ;
776         if (!len) return 0;
777     }
778     if (count <= len)
779     {
780         SetLastError( ERROR_MORE_DATA );
781         return 0;
782     }
783     TRACE( "(%s) %x -> %s\n", local ? "local" : "global", atom, debugstr_w(buffer) );
784     return len;
785 }
786
787
788 /***********************************************************************
789  *           GlobalGetAtomNameW   (KERNEL32.@)
790  */
791 UINT WINAPI GlobalGetAtomNameW( ATOM atom, LPWSTR buffer, INT count )
792 {
793     return ATOM_GetAtomNameW( atom, buffer, count, FALSE);
794 }
795
796
797 /***********************************************************************
798  *           GetAtomNameW   (KERNEL32.@)
799  */
800 UINT WINAPI GetAtomNameW( ATOM atom, LPWSTR buffer, INT count )
801 {
802     return ATOM_GetAtomNameW( atom, buffer, count, TRUE );
803 }