1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 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 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672
|
/*
* Atom table functions
*
* Copyright 1993, 1994, 1995 Alexandre Julliard
*/
/*
* Warning: The code assumes that LocalAlloc() returns a block aligned
* on a 4-bytes boundary (because of the shifting done in
* HANDLETOATOM). If this is not the case, the allocation code will
* have to be changed.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "wine/winbase16.h"
#include "wine/winuser16.h"
#include "winuser.h"
#include "global.h"
#include "instance.h"
#include "ldt.h"
#include "stackframe.h"
#include "user.h"
#include "debugtools.h"
DEFAULT_DEBUG_CHANNEL(atom)
#define DEFAULT_ATOMTABLE_SIZE 37
#define MIN_STR_ATOM 0xc000
#define MAX_ATOM_LEN 255
#define ATOMTOHANDLE(atom) ((HANDLE16)(atom) << 2)
#define HANDLETOATOM(handle) ((ATOM)(0xc000 | ((handle) >> 2)))
#define HAS_ATOM_TABLE(sel) \
((INSTANCEDATA*)PTR_SEG_OFF_TO_LIN(sel,0))->atomtable != 0)
#define GET_ATOM_TABLE(sel) ((ATOMTABLE*)PTR_SEG_OFF_TO_LIN(sel, \
((INSTANCEDATA*)PTR_SEG_OFF_TO_LIN(sel,0))->atomtable))
typedef struct
{
HANDLE16 next;
WORD refCount;
BYTE length;
BYTE str[1];
} ATOMENTRY;
typedef struct
{
WORD size;
HANDLE16 entries[1];
} ATOMTABLE;
static WORD ATOM_GlobalTable = 0;
/***********************************************************************
* ATOM_InitTable
*
* NOTES
* Should this validate the value of entries to be 0 < x < 0x3fff?
*
* RETURNS
* Handle: Success
* 0: Failure
*/
static HANDLE16 ATOM_InitTable(
WORD selector, /* [in] Segment */
WORD entries /* [in] Size of atom table */
) {
int i;
HANDLE16 handle;
ATOMTABLE *table;
/* We consider the first table to be initialized as the global table.
* This works, as USER (both built-in and native) is the first one to
* register ...
*/
if (!ATOM_GlobalTable) ATOM_GlobalTable = selector;
/* Allocate the table */
handle = LOCAL_Alloc( selector, LMEM_FIXED,
sizeof(ATOMTABLE) + (entries-1) * sizeof(HANDLE16) );
if (!handle) return 0;
table = (ATOMTABLE *)PTR_SEG_OFF_TO_LIN( selector, handle );
table->size = entries;
for (i = 0; i < entries; i++) table->entries[i] = 0;
/* Store a pointer to the table in the instance data */
((INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( selector, 0 ))->atomtable = handle;
return handle;
}
/***********************************************************************
* ATOM_Init
*
* Global table initialisation.
*/
BOOL ATOM_Init( WORD globalTableSel )
{
return ATOM_InitTable( globalTableSel, DEFAULT_ATOMTABLE_SIZE ) != 0;
}
/***********************************************************************
* ATOM_GetTable
*
* Return a pointer to the atom table of a given segment, creating
* it if necessary.
*
* RETURNS
* Pointer to table: Success
* NULL: Failure
*/
static ATOMTABLE *ATOM_GetTable(
WORD selector, /* [in] Segment */
BOOL create /* [in] Create */ )
{
INSTANCEDATA *ptr = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( selector, 0 );
if (ptr->atomtable)
{
ATOMTABLE *table = (ATOMTABLE *)((char *)ptr + ptr->atomtable);
if (table->size) return table;
}
if (!create) return NULL;
if (!ATOM_InitTable( selector, DEFAULT_ATOMTABLE_SIZE )) return NULL;
/* Reload ptr in case it moved in linear memory */
ptr = (INSTANCEDATA *)PTR_SEG_OFF_TO_LIN( selector, 0 );
return (ATOMTABLE *)((char *)ptr + ptr->atomtable);
}
/***********************************************************************
* ATOM_MakePtr
*
* Make an ATOMENTRY pointer from a handle (obtained from GetAtomHandle()).
*/
static ATOMENTRY *ATOM_MakePtr(
WORD selector, /* [in] Segment */
HANDLE16 handle /* [in] Handle */
) {
return (ATOMENTRY *)PTR_SEG_OFF_TO_LIN( selector, handle );
}
/***********************************************************************
* ATOM_Hash
* RETURNS
* The hash value for the input string
*/
static WORD ATOM_Hash(
WORD entries, /* [in] Total number of entries */
LPCSTR str, /* [in] Pointer to string to hash */
WORD len /* [in] Length of string */
) {
WORD i, hash = 0;
TRACE("%x, %s, %x\n", entries, str, len);
for (i = 0; i < len; i++) hash ^= toupper(str[i]) + i;
return hash % entries;
}
static BOOL ATOM_IsIntAtom(LPCSTR atomstr,WORD *atomid) {
LPSTR xend;
if (!HIWORD(atomstr)) {
*atomid = LOWORD(atomstr);
return TRUE;
}
if (atomstr[0]!='#')
return FALSE;
*atomid=strtol(atomstr+1,&xend,10);
if (*xend) {
/* atom has a name like '## some name ##' */
return FALSE;
}
return TRUE;
}
/***********************************************************************
* ATOM_AddAtom
*
* Windows DWORD aligns the atom entry size.
* The remaining unused string space created by the alignment
* gets padded with '\0's in a certain way to ensure
* that at least one trailing '\0' remains.
*
* RETURNS
* Atom: Success
* 0: Failure
*/
static ATOM ATOM_AddAtom(
WORD selector, /* [in] Segment */
LPCSTR str /* [in] Pointer to the string to add */
) {
WORD hash;
HANDLE16 entry;
ATOMENTRY * entryPtr;
ATOMTABLE * table;
int len, ae_len;
WORD iatom;
TRACE("0x%x, %s\n", selector, str);
if (ATOM_IsIntAtom(str,&iatom))
return iatom;
if ((len = strlen( str )) > MAX_ATOM_LEN) len = MAX_ATOM_LEN;
if (!(table = ATOM_GetTable( selector, TRUE ))) return 0;
hash = ATOM_Hash( table->size, str, len );
entry = table->entries[hash];
while (entry)
{
entryPtr = ATOM_MakePtr( selector, entry );
if ((entryPtr->length == len) &&
(!lstrncmpiA( entryPtr->str, str, len )))
{
entryPtr->refCount++;
TRACE("-- existing 0x%x\n", entry);
return HANDLETOATOM( entry );
}
entry = entryPtr->next;
}
ae_len = (sizeof(ATOMENTRY)+len+3) & ~3;
entry = LOCAL_Alloc( selector, LMEM_FIXED, ae_len);
if (!entry) return 0;
/* Reload the table ptr in case it moved in linear memory */
table = ATOM_GetTable( selector, FALSE );
entryPtr = ATOM_MakePtr( selector, entry );
entryPtr->next = table->entries[hash];
entryPtr->refCount = 1;
entryPtr->length = len;
/* Some applications _need_ the '\0' padding provided by this strncpy */
strncpy( entryPtr->str, str, ae_len - sizeof(ATOMENTRY) + 1 );
entryPtr->str[ae_len - sizeof(ATOMENTRY)] = '\0';
table->entries[hash] = entry;
TRACE("-- new 0x%x\n", entry);
return HANDLETOATOM( entry );
}
/***********************************************************************
* ATOM_DeleteAtom
* RETURNS
* 0: Success
* Atom: Failure
*/
static ATOM ATOM_DeleteAtom(
WORD selector, /* [in] Segment */
ATOM atom /* [in] Atom to delete */
) {
ATOMENTRY * entryPtr;
ATOMTABLE * table;
HANDLE16 entry, *prevEntry;
WORD hash;
TRACE("0x%x, 0x%x\n", selector, atom);
if (atom < MIN_STR_ATOM) return 0; /* Integer atom */
if (!(table = ATOM_GetTable( selector, FALSE ))) return 0;
entry = ATOMTOHANDLE( atom );
entryPtr = ATOM_MakePtr( selector, entry );
/* Find previous atom */
hash = ATOM_Hash( table->size, entryPtr->str, entryPtr->length );
prevEntry = &table->entries[hash];
while (*prevEntry && *prevEntry != entry)
{
ATOMENTRY * prevEntryPtr = ATOM_MakePtr( selector, *prevEntry );
prevEntry = &prevEntryPtr->next;
}
if (!*prevEntry) return atom;
/* Delete atom */
if (--entryPtr->refCount == 0)
{
*prevEntry = entryPtr->next;
LOCAL_Free( selector, entry );
}
return 0;
}
/***********************************************************************
* ATOM_FindAtom
* RETURNS
* Atom: Success
* 0: Failure
*/
static ATOM ATOM_FindAtom(
WORD selector, /* [in] Segment */
LPCSTR str /* [in] Pointer to string to find */
) {
ATOMTABLE * table;
WORD hash,iatom;
HANDLE16 entry;
int len;
TRACE("%x, %s\n", selector, str);
if (ATOM_IsIntAtom(str,&iatom))
return iatom;
if ((len = strlen( str )) > 255) len = 255;
if (!(table = ATOM_GetTable( selector, FALSE ))) return 0;
hash = ATOM_Hash( table->size, str, len );
entry = table->entries[hash];
while (entry)
{
ATOMENTRY * entryPtr = ATOM_MakePtr( selector, entry );
if ((entryPtr->length == len) &&
(!lstrncmpiA( entryPtr->str, str, len )))
{ TRACE("-- found %x\n", entry);
return HANDLETOATOM( entry );
}
entry = entryPtr->next;
}
TRACE("-- not found\n");
return 0;
}
/***********************************************************************
* ATOM_GetAtomName
* RETURNS
* Length of string copied to buffer: Success
* 0: Failure
*/
static UINT ATOM_GetAtomName(
WORD selector, /* [in] Segment */
ATOM atom, /* [in] Atom identifier */
LPSTR buffer, /* [out] Pointer to buffer for atom string */
INT count /* [in] Size of buffer */
) {
ATOMTABLE * table;
ATOMENTRY * entryPtr;
HANDLE16 entry;
char * strPtr;
UINT len;
char text[8];
TRACE("%x, %x\n", selector, atom);
if (!count) return 0;
if (atom < MIN_STR_ATOM)
{
sprintf( text, "#%d", atom );
len = strlen(text);
strPtr = text;
}
else
{
if (!(table = ATOM_GetTable( selector, FALSE ))) return 0;
entry = ATOMTOHANDLE( atom );
entryPtr = ATOM_MakePtr( selector, entry );
len = entryPtr->length;
strPtr = entryPtr->str;
}
if (len >= count) len = count-1;
memcpy( buffer, strPtr, len );
buffer[len] = '\0';
return len;
}
/***********************************************************************
* InitAtomTable16 (KERNEL.68)
*/
WORD WINAPI InitAtomTable16( WORD entries )
{
if (!entries) entries = DEFAULT_ATOMTABLE_SIZE; /* sanity check */
return ATOM_InitTable( CURRENT_DS, entries );
}
/***********************************************************************
* GetAtomHandle (KERNEL.73)
*/
HANDLE16 WINAPI GetAtomHandle16( ATOM atom )
{
if (atom < MIN_STR_ATOM) return 0;
return ATOMTOHANDLE( atom );
}
/***********************************************************************
* AddAtom16 (KERNEL.70)
*/
ATOM WINAPI AddAtom16( SEGPTR str )
{
ATOM atom;
HANDLE16 ds = CURRENT_DS;
if (!HIWORD(str)) return (ATOM)LOWORD(str); /* Integer atom */
if (SELECTOR_TO_ENTRY(LOWORD(str)) == SELECTOR_TO_ENTRY(ds))
{
/* If the string is in the same data segment as the atom table, make */
/* a copy of the string to be sure it doesn't move in linear memory. */
char buffer[MAX_ATOM_LEN+1];
lstrcpynA( buffer, (char *)PTR_SEG_TO_LIN(str), sizeof(buffer) );
atom = ATOM_AddAtom( ds, buffer );
}
else atom = ATOM_AddAtom( ds, (LPCSTR)PTR_SEG_TO_LIN(str) );
return atom;
}
/***********************************************************************
* AddAtom32A (KERNEL32.0)
* Adds a string to the atom table and returns the atom identifying the
* string.
*
* RETURNS
* Atom: Success
* 0: Failure
*/
ATOM WINAPI AddAtomA(
LPCSTR str /* [in] Pointer to string to add */
) {
return GlobalAddAtomA( str ); /* FIXME */
}
/***********************************************************************
* AddAtom32W (KERNEL32.1)
* See AddAtom32A
*/
ATOM WINAPI AddAtomW( LPCWSTR str )
{
return GlobalAddAtomW( str ); /* FIXME */
}
/***********************************************************************
* DeleteAtom16 (KERNEL.71)
*/
ATOM WINAPI DeleteAtom16( ATOM atom )
{
return ATOM_DeleteAtom( CURRENT_DS, atom );
}
/***********************************************************************
* DeleteAtom32 (KERNEL32.69)
* Decrements the reference count of a string atom. If count becomes
* zero, the string associated with the atom is removed from the table.
*
* RETURNS
* 0: Success
* Atom: Failure
*/
ATOM WINAPI DeleteAtom(
ATOM atom /* [in] Atom to delete */
) {
return GlobalDeleteAtom( atom ); /* FIXME */
}
/***********************************************************************
* FindAtom16 (KERNEL.69)
*/
ATOM WINAPI FindAtom16( SEGPTR str )
{
if (!HIWORD(str)) return (ATOM)LOWORD(str); /* Integer atom */
return ATOM_FindAtom( CURRENT_DS, (LPCSTR)PTR_SEG_TO_LIN(str) );
}
/***********************************************************************
* FindAtom32A (KERNEL32.117)
* Searches the local atom table for the string and returns the atom
* associated with that string.
*
* RETURNS
* Atom: Success
* 0: Failure
*/
ATOM WINAPI FindAtomA(
LPCSTR str /* [in] Pointer to string to find */
) {
return GlobalFindAtomA( str ); /* FIXME */
}
/***********************************************************************
* FindAtom32W (KERNEL32.118)
* See FindAtom32A
*/
ATOM WINAPI FindAtomW( LPCWSTR str )
{
return GlobalFindAtomW( str ); /* FIXME */
}
/***********************************************************************
* GetAtomName16 (KERNEL.72)
*/
UINT16 WINAPI GetAtomName16( ATOM atom, LPSTR buffer, INT16 count )
{
return (UINT16)ATOM_GetAtomName( CURRENT_DS, atom, buffer, count );
}
/***********************************************************************
* GetAtomName32A (KERNEL32.149)
* Retrieves a copy of the string associated with the atom.
*
* RETURNS
* Length of string: Success
* 0: Failure
*/
UINT WINAPI GetAtomNameA(
ATOM atom, /* [in] Atom */
LPSTR buffer, /* [out] Pointer to string for atom string */
INT count /* [in] Size of buffer */
) {
return GlobalGetAtomNameA( atom, buffer, count ); /* FIXME */
}
/***********************************************************************
* GetAtomName32W (KERNEL32.150)
* See GetAtomName32A
*/
UINT WINAPI GetAtomNameW( ATOM atom, LPWSTR buffer, INT count )
{
return GlobalGetAtomNameW( atom, buffer, count ); /* FIXME */
}
/***********************************************************************
* GlobalAddAtom16 (USER.268)
*/
ATOM WINAPI GlobalAddAtom16( SEGPTR str )
{
if (!HIWORD(str)) return (ATOM)LOWORD(str); /* Integer atom */
return ATOM_AddAtom( ATOM_GlobalTable, (LPCSTR)PTR_SEG_TO_LIN(str) );
}
/***********************************************************************
* GlobalAddAtom32A (KERNEL32.313)
* Adds a character string to the global atom table and returns a unique
* value identifying the string.
*
* RETURNS
* Atom: Success
* 0: Failure
*/
ATOM WINAPI GlobalAddAtomA(
LPCSTR str /* [in] Pointer to string to add */
) {
if (!HIWORD(str)) return (ATOM)LOWORD(str); /* Integer atom */
return ATOM_AddAtom( ATOM_GlobalTable, str );
}
/***********************************************************************
* GlobalAddAtom32W (KERNEL32.314)
* See GlobalAddAtom32A
*/
ATOM WINAPI GlobalAddAtomW( LPCWSTR str )
{
char buffer[MAX_ATOM_LEN+1];
if (!HIWORD(str)) return (ATOM)LOWORD(str); /* Integer atom */
lstrcpynWtoA( buffer, str, sizeof(buffer) );
return ATOM_AddAtom( ATOM_GlobalTable, buffer );
}
/***********************************************************************
* GlobalDeleteAtom (USER.269) (KERNEL32.317)
* Decrements the reference count of a string atom. If the count is
* zero, the string associated with the atom is removed from the table.
*
* RETURNS
* 0: Success
* Atom: Failure
*/
ATOM WINAPI GlobalDeleteAtom(
ATOM atom /* [in] Atom to delete */
) {
return ATOM_DeleteAtom( ATOM_GlobalTable, atom );
}
/***********************************************************************
* GlobalFindAtom16 (USER.270)
*/
ATOM WINAPI GlobalFindAtom16( SEGPTR str )
{
if (!HIWORD(str)) return (ATOM)LOWORD(str); /* Integer atom */
return ATOM_FindAtom( ATOM_GlobalTable, (LPCSTR)PTR_SEG_TO_LIN(str) );
}
/***********************************************************************
* GlobalFindAtom32A (KERNEL32.318)
* Searches the atom table for the string and returns the atom
* associated with it.
*
* RETURNS
* Atom: Success
* 0: Failure
*/
ATOM WINAPI GlobalFindAtomA(
LPCSTR str /* [in] Pointer to string to search for */
) {
if (!HIWORD(str)) return (ATOM)LOWORD(str); /* Integer atom */
return ATOM_FindAtom( ATOM_GlobalTable, str );
}
/***********************************************************************
* GlobalFindAtom32W (KERNEL32.319)
* See GlobalFindAtom32A
*/
ATOM WINAPI GlobalFindAtomW( LPCWSTR str )
{
char buffer[MAX_ATOM_LEN+1];
if (!HIWORD(str)) return (ATOM)LOWORD(str); /* Integer atom */
lstrcpynWtoA( buffer, str, sizeof(buffer) );
return ATOM_FindAtom( ATOM_GlobalTable, buffer );
}
/***********************************************************************
* GlobalGetAtomName16 (USER.271)
*/
UINT16 WINAPI GlobalGetAtomName16( ATOM atom, LPSTR buffer, INT16 count )
{
return (UINT16)ATOM_GetAtomName( ATOM_GlobalTable, atom, buffer, count );
}
/***********************************************************************
* GlobalGetAtomName32A (KERNEL32.323)
* Retrieves a copy of the string associated with an atom.
*
* RETURNS
* Length of string in characters: Success
* 0: Failure
*/
UINT WINAPI GlobalGetAtomNameA(
ATOM atom, /* [in] Atom identifier */
LPSTR buffer, /* [out] Pointer to buffer for atom string */
INT count /* [in] Size of buffer */
) {
return ATOM_GetAtomName( ATOM_GlobalTable, atom, buffer, count );
}
/***********************************************************************
* GlobalGetAtomName32W (KERNEL32.324)
* See GlobalGetAtomName32A
*/
UINT WINAPI GlobalGetAtomNameW( ATOM atom, LPWSTR buffer, INT count )
{
char tmp[MAX_ATOM_LEN+1];
ATOM_GetAtomName( ATOM_GlobalTable, atom, tmp, sizeof(tmp) );
lstrcpynAtoW( buffer, tmp, count );
return lstrlenW( buffer );
}
|