2 * Setupapi string table functions
4 * Copyright 2005 Eric Kohl
5 * Copyright 2014 Nikolay Sivov for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
35 DECLARE_HANDLE(HSTRING_TABLE
);
51 #define BUCKET_COUNT 509
52 #define DEFAULT_ALLOC_SIZE 4096
57 Returned string table 'handle' is a pointer to 'struct stringtable' structure.
58 Data itself is allocated separately, pointer is stored in 'data' field.
60 Data starts with array of 509 DWORDs - lookup table. Initially all offsets in that
61 array are set to -1. Right after lookup table goes data itself, stored in linked lists.
62 Lookup table offset points to first record of 'struct stringentry' type. When more
63 than one record is present in a bucket, first record links to next one with 'nextoffset'
64 field. Last record has nextoffset == -1, same when there's only one record. String data
65 is placed right after offset, and is followed by extra data. Each record has reserved
66 'max_extra_size' bytes to store extra data, it's not compacted in any way.
68 A simple hash function is used to determine which bucket a given string belongs to (see below).
70 All offsets including returned string ids are relative to 'data' pointer. When table
71 needs to grow 'allocated' size is doubled, but offsets are always valid and preserved.
75 static inline DWORD
get_string_hash(const WCHAR
*str
, BOOL case_sensitive
)
80 WCHAR ch
= case_sensitive
? *str
: towlower(*str
);
87 return hash
% BUCKET_COUNT
;
90 static inline DWORD
*get_bucket_ptr(struct stringtable
*table
, const WCHAR
*string
, BOOL case_sensitive
)
92 DWORD hash
= get_string_hash(string
, case_sensitive
);
93 return (DWORD
*)(table
->data
+ hash
*sizeof(DWORD
));
96 static inline WCHAR
*get_string_ptr(struct stringtable
*table
, DWORD id
)
98 return (WCHAR
*)(table
->data
+ id
+ sizeof(DWORD
));
101 static inline char *get_extradata_ptr(struct stringtable
*table
, DWORD id
)
103 WCHAR
*ptrW
= get_string_ptr(table
, id
);
104 /* skip string itself */
105 return (char*)(ptrW
+ lstrlenW(ptrW
) + 1);
108 static inline BOOL
is_valid_string_id(struct stringtable
*table
, DWORD id
)
110 return (id
>= BUCKET_COUNT
*sizeof(DWORD
)) && (id
< table
->allocated
);
113 static inline int get_aligned16_size(int size
)
115 return (size
+ 15) & ~15;
118 /**************************************************************************
119 * StringTableInitializeEx [SETUPAPI.@]
121 * Creates a new string table and initializes it.
124 * max_extra_size [I] Maximum extra data size
125 * reserved [I] Unused
128 * Success: Handle to the string table
131 HSTRING_TABLE WINAPI
StringTableInitializeEx(ULONG max_extra_size
, DWORD reserved
)
133 struct stringtable
*table
;
135 TRACE("(%d %x)\n", max_extra_size
, reserved
);
137 table
= MyMalloc(sizeof(*table
));
138 if (!table
) return NULL
;
140 table
->allocated
= get_aligned16_size(BUCKET_COUNT
*sizeof(DWORD
) + DEFAULT_ALLOC_SIZE
);
141 table
->data
= MyMalloc(table
->allocated
);
147 table
->nextoffset
= BUCKET_COUNT
*sizeof(DWORD
);
148 /* FIXME: actually these two are not zero */
149 table
->unk
[0] = table
->unk
[1] = 0;
150 table
->max_extra_size
= max_extra_size
;
151 table
->lcid
= GetThreadLocale();
153 /* bucket area is filled with 0xff, actual string data area is zeroed */
154 memset(table
->data
, 0xff, table
->nextoffset
);
155 memset(table
->data
+ table
->nextoffset
, 0, table
->allocated
- table
->nextoffset
);
157 return (HSTRING_TABLE
)table
;
160 /**************************************************************************
161 * StringTableInitialize [SETUPAPI.@]
163 * Creates a new string table and initializes it.
169 * Success: Handle to the string table
172 HSTRING_TABLE WINAPI
StringTableInitialize(void)
174 return StringTableInitializeEx(0, 0);
177 /**************************************************************************
178 * StringTableDestroy [SETUPAPI.@]
180 * Destroys a string table.
183 * hTable [I] Handle to the string table to be destroyed
188 void WINAPI
StringTableDestroy(HSTRING_TABLE hTable
)
190 struct stringtable
*table
= (struct stringtable
*)hTable
;
192 TRACE("%p\n", table
);
201 /**************************************************************************
202 * StringTableDuplicate [SETUPAPI.@]
204 * Duplicates a given string table.
207 * hTable [I] Handle to the string table
210 * Success: Handle to the duplicated string table
214 HSTRING_TABLE WINAPI
StringTableDuplicate(HSTRING_TABLE hTable
)
216 struct stringtable
*src
= (struct stringtable
*)hTable
, *dest
;
223 dest
= MyMalloc(sizeof(*dest
));
228 dest
->data
= MyMalloc(src
->allocated
);
234 memcpy(dest
->data
, src
->data
, src
->allocated
);
235 return (HSTRING_TABLE
)dest
;
238 /**************************************************************************
239 * StringTableGetExtraData [SETUPAPI.@]
241 * Retrieves extra data from a given string table entry.
244 * hTable [I] Handle to the string table
246 * extra [I] Pointer a buffer that receives the extra data
247 * extra_size [I] Size of the buffer
253 BOOL WINAPI
StringTableGetExtraData(HSTRING_TABLE hTable
, ULONG id
, void *extra
, ULONG extra_size
)
255 struct stringtable
*table
= (struct stringtable
*)hTable
;
258 TRACE("%p %u %p %u\n", table
, id
, extra
, extra_size
);
263 if (!is_valid_string_id(table
, id
))
266 if (table
->max_extra_size
> extra_size
)
268 ERR("data size is too large\n");
272 extraptr
= get_extradata_ptr(table
, id
);
273 memcpy(extra
, extraptr
, extra_size
);
277 /**************************************************************************
278 * StringTableLookUpStringEx [SETUPAPI.@]
280 * Searches a string table and extra data for a given string.
283 * hTable [I] Handle to the string table
284 * string [I] String to be searched for
286 * 1: case sensitive compare
287 * extra [O] Pointer to the buffer that receives the extra data
288 * extra_size [I/O] Unused
294 DWORD WINAPI
StringTableLookUpStringEx(HSTRING_TABLE hTable
, LPWSTR string
, DWORD flags
,
295 void *extra
, ULONG extra_size
)
297 struct stringtable
*table
= (struct stringtable
*)hTable
;
298 BOOL case_sensitive
= flags
& 1;
299 struct stringentry
*entry
;
303 TRACE("%p->%p %s %x %p, %x\n", table
, table
->data
, debugstr_w(string
), flags
, extra
, extra_size
);
308 /* get corresponding offset */
309 offset
= *get_bucket_ptr(table
, string
, case_sensitive
);
313 /* now we're at correct bucket, do linear search for string */
315 entry
= (struct stringentry
*)(table
->data
+ offset
);
317 cmp
= wcscmp(entry
->data
, string
);
319 cmp
= lstrcmpiW(entry
->data
, string
);
322 memcpy(extra
, get_extradata_ptr(table
, offset
), extra_size
);
327 if (entry
->nextoffset
== -1)
330 offset
= entry
->nextoffset
;
331 if (offset
> table
->allocated
)
336 /**************************************************************************
337 * StringTableLookUpString [SETUPAPI.@]
339 * Searches a string table for a given string.
342 * hTable [I] Handle to the string table
343 * string [I] String to be searched for
345 * 1: case sensitive compare
351 DWORD WINAPI
StringTableLookUpString(HSTRING_TABLE hTable
, LPWSTR string
, DWORD flags
)
353 return StringTableLookUpStringEx(hTable
, string
, flags
, NULL
, 0);
356 /**************************************************************************
357 * StringTableAddStringEx [SETUPAPI.@]
359 * Adds a new string plus extra data to the string table.
362 * hTable [I] Handle to the string table
363 * string [I] String to be added to the string table
365 * 1: case sensitive compare
366 * extra [I] Pointer to the extra data
367 * extra_size [I] Size of the extra data
374 * If the given string already exists in the string table it will not
375 * be added again. The ID of the existing string will be returned in
378 DWORD WINAPI
StringTableAddStringEx(HSTRING_TABLE hTable
, LPWSTR string
,
379 DWORD flags
, void *extra
, DWORD extra_size
)
381 struct stringtable
*table
= (struct stringtable
*)hTable
;
382 BOOL case_sensitive
= flags
& 1;
383 struct stringentry
*entry
;
388 TRACE("%p %s %x %p, %u\n", hTable
, debugstr_w(string
), flags
, extra
, extra_size
);
393 id
= StringTableLookUpStringEx(hTable
, string
, flags
, NULL
, 0);
397 /* needed space for new record */
398 len
= sizeof(DWORD
) + (lstrlenW(string
)+1)*sizeof(WCHAR
) + table
->max_extra_size
;
399 if (table
->nextoffset
+ len
>= table
->allocated
) {
400 table
->allocated
<<= 1;
401 table
->data
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, table
->data
, table
->allocated
);
405 offset
= get_bucket_ptr(table
, string
, case_sensitive
);
407 /* bucket used for a very first time */
408 *offset
= table
->nextoffset
;
410 entry
= (struct stringentry
*)(table
->data
+ *offset
);
411 /* link existing last entry to newly added */
412 while (entry
->nextoffset
!= -1)
413 entry
= (struct stringentry
*)(table
->data
+ entry
->nextoffset
);
414 entry
->nextoffset
= table
->nextoffset
;
416 entry
= (struct stringentry
*)(table
->data
+ table
->nextoffset
);
417 entry
->nextoffset
= -1;
418 id
= table
->nextoffset
;
421 ptrW
= get_string_ptr(table
, id
);
422 lstrcpyW(ptrW
, string
);
426 /* copy extra data */
428 memcpy(get_extradata_ptr(table
, id
), extra
, extra_size
);
430 table
->nextoffset
+= len
;
434 /**************************************************************************
435 * StringTableAddString [SETUPAPI.@]
437 * Adds a new string to the string table.
440 * hTable [I] Handle to the string table
441 * string [I] String to be added to the string table
443 * 1: case sensitive compare
450 * If the given string already exists in the string table it will not
451 * be added again. The ID of the existing string will be returned in
454 DWORD WINAPI
StringTableAddString(HSTRING_TABLE hTable
, LPWSTR string
, DWORD flags
)
456 return StringTableAddStringEx(hTable
, string
, flags
, NULL
, 0);
459 /**************************************************************************
460 * StringTableSetExtraData [SETUPAPI.@]
462 * Sets extra data for a given string table entry.
465 * hTable [I] Handle to the string table
467 * extra [I] Pointer to the extra data
468 * extra_size [I] Size of the extra data
474 BOOL WINAPI
StringTableSetExtraData(HSTRING_TABLE hTable
, DWORD id
, void *extra
, ULONG extra_size
)
476 struct stringtable
*table
= (struct stringtable
*)hTable
;
479 TRACE("%p %d %p %u\n", hTable
, id
, extra
, extra_size
);
484 if (!is_valid_string_id(table
, id
))
487 if (table
->max_extra_size
< extra_size
)
489 ERR("data size is too large\n");
493 extraptr
= get_extradata_ptr(table
, id
);
494 memset(extraptr
, 0, table
->max_extra_size
);
495 memcpy(extraptr
, extra
, extra_size
);
500 /**************************************************************************
501 * StringTableStringFromId [SETUPAPI.@]
503 * Returns a pointer to a string for the given string ID.
506 * hTable [I] Handle to the string table.
510 * Success: Pointer to the string
513 LPWSTR WINAPI
StringTableStringFromId(HSTRING_TABLE hTable
, ULONG id
)
515 struct stringtable
*table
= (struct stringtable
*)hTable
;
516 static WCHAR empty
[] = {0};
518 TRACE("%p %d\n", table
, id
);
523 if (!is_valid_string_id(table
, id
))
526 return get_string_ptr(table
, id
);
529 /**************************************************************************
530 * StringTableStringFromIdEx [SETUPAPI.@]
532 * Returns a string for the given string ID.
535 * hTable [I] Handle to the string table
537 * buff [I] Pointer to string buffer
538 * buflen [I/O] Pointer to the size of the string buffer
544 BOOL WINAPI
StringTableStringFromIdEx(HSTRING_TABLE hTable
, ULONG id
, LPWSTR buff
, DWORD
*buflen
)
546 struct stringtable
*table
= (struct stringtable
*)hTable
;
551 TRACE("%p %x %p %p\n", table
, id
, buff
, buflen
);
558 if (!is_valid_string_id(table
, id
)) {
559 WARN("invalid string id\n");
564 ptrW
= get_string_ptr(table
, id
);
565 len
= (lstrlenW(ptrW
) + 1)*sizeof(WCHAR
);
567 lstrcpyW(buff
, ptrW
);
575 /**************************************************************************
576 * StringTableTrim [SETUPAPI.@]
581 * hTable [I] Handle to the string table
586 void WINAPI
StringTableTrim(HSTRING_TABLE hTable
)
588 FIXME("%p\n", hTable
);