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
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(setupapi
);
38 DECLARE_HANDLE(HSTRING_TABLE
);
54 #define BUCKET_COUNT 509
55 #define DEFAULT_ALLOC_SIZE 4096
60 Returned string table 'handle' is a pointer to 'struct stringtable' structure.
61 Data itself is allocated separately, pointer is stored in 'data' field.
63 Data starts with array of 509 DWORDs - lookup table. Initially all offsets in that
64 array are set to -1. Right after lookup table goes data itself, stored in linked lists.
65 Lookup table offset points to first record of 'struct stringentry' type. When more
66 than one record is present in a bucket, first record links to next one with 'nextoffset'
67 field. Last record has nextoffset == -1, same when there's only one record. String data
68 is placed right after offset, and is followed by extra data. Each record has reserved
69 'max_extra_size' bytes to store extra data, it's not compacted in any way.
71 A simple hash function is used to determine which bucket a given string belongs to (see below).
73 All offsets including returned string ids are relative to 'data' pointer. When table
74 needs to grow 'allocated' size is doubled, but offsets are always valid and preserved.
78 static inline DWORD
get_string_hash(const WCHAR
*str
, BOOL case_sensitive
)
83 WCHAR ch
= case_sensitive
? *str
: tolowerW(*str
);
90 return hash
% BUCKET_COUNT
;
93 static inline DWORD
*get_bucket_ptr(struct stringtable
*table
, const WCHAR
*string
, BOOL case_sensitive
)
95 DWORD hash
= get_string_hash(string
, case_sensitive
);
96 return (DWORD
*)(table
->data
+ hash
*sizeof(DWORD
));
99 static inline WCHAR
*get_string_ptr(struct stringtable
*table
, DWORD id
)
101 return (WCHAR
*)(table
->data
+ id
+ sizeof(DWORD
));
104 static inline char *get_extradata_ptr(struct stringtable
*table
, DWORD id
)
106 WCHAR
*ptrW
= get_string_ptr(table
, id
);
107 /* skip string itself */
108 return (char*)(ptrW
+ strlenW(ptrW
) + 1);
111 static inline BOOL
is_valid_string_id(struct stringtable
*table
, DWORD id
)
113 return (id
>= BUCKET_COUNT
*sizeof(DWORD
)) && (id
< table
->allocated
);
116 static inline int get_aligned16_size(int size
)
118 return (size
+ 15) & ~15;
121 /**************************************************************************
122 * StringTableInitializeEx [SETUPAPI.@]
124 * Creates a new string table and initializes it.
127 * max_extra_size [I] Maximum extra data size
128 * reserved [I] Unused
131 * Success: Handle to the string table
134 HSTRING_TABLE WINAPI
StringTableInitializeEx(ULONG max_extra_size
, DWORD reserved
)
136 struct stringtable
*table
;
138 TRACE("(%d %x)\n", max_extra_size
, reserved
);
140 table
= MyMalloc(sizeof(*table
));
141 if (!table
) return NULL
;
143 table
->allocated
= get_aligned16_size(BUCKET_COUNT
*sizeof(DWORD
) + DEFAULT_ALLOC_SIZE
);
144 table
->data
= MyMalloc(table
->allocated
);
150 table
->nextoffset
= BUCKET_COUNT
*sizeof(DWORD
);
151 /* FIXME: actually these two are not zero */
152 table
->unk
[0] = table
->unk
[1] = 0;
153 table
->max_extra_size
= max_extra_size
;
154 table
->lcid
= GetThreadLocale();
156 /* bucket area is filled with 0xff, actual string data area is zeroed */
157 memset(table
->data
, 0xff, table
->nextoffset
);
158 memset(table
->data
+ table
->nextoffset
, 0, table
->allocated
- table
->nextoffset
);
160 return (HSTRING_TABLE
)table
;
163 /**************************************************************************
164 * StringTableInitialize [SETUPAPI.@]
166 * Creates a new string table and initializes it.
172 * Success: Handle to the string table
175 HSTRING_TABLE WINAPI
StringTableInitialize(void)
177 return StringTableInitializeEx(0, 0);
180 /**************************************************************************
181 * StringTableDestroy [SETUPAPI.@]
183 * Destroys a string table.
186 * hTable [I] Handle to the string table to be destroyed
191 void WINAPI
StringTableDestroy(HSTRING_TABLE hTable
)
193 struct stringtable
*table
= (struct stringtable
*)hTable
;
195 TRACE("%p\n", table
);
204 /**************************************************************************
205 * StringTableDuplicate [SETUPAPI.@]
207 * Duplicates a given string table.
210 * hTable [I] Handle to the string table
213 * Success: Handle to the duplicated string table
217 HSTRING_TABLE WINAPI
StringTableDuplicate(HSTRING_TABLE hTable
)
219 struct stringtable
*src
= (struct stringtable
*)hTable
, *dest
;
226 dest
= MyMalloc(sizeof(*dest
));
231 dest
->data
= MyMalloc(src
->allocated
);
237 memcpy(dest
->data
, src
->data
, src
->allocated
);
238 return (HSTRING_TABLE
)dest
;
241 /**************************************************************************
242 * StringTableGetExtraData [SETUPAPI.@]
244 * Retrieves extra data from a given string table entry.
247 * hTable [I] Handle to the string table
249 * extra [I] Pointer a buffer that receives the extra data
250 * extra_size [I] Size of the buffer
256 BOOL WINAPI
StringTableGetExtraData(HSTRING_TABLE hTable
, ULONG id
, void *extra
, ULONG extra_size
)
258 struct stringtable
*table
= (struct stringtable
*)hTable
;
261 TRACE("%p %u %p %u\n", table
, id
, extra
, extra_size
);
266 if (!is_valid_string_id(table
, id
))
269 if (table
->max_extra_size
> extra_size
)
271 ERR("data size is too large\n");
275 extraptr
= get_extradata_ptr(table
, id
);
276 memcpy(extra
, extraptr
, extra_size
);
280 /**************************************************************************
281 * StringTableLookUpStringEx [SETUPAPI.@]
283 * Searches a string table and extra data for a given string.
286 * hTable [I] Handle to the string table
287 * string [I] String to be searched for
289 * 1: case sensitive compare
290 * extra [O] Pointer to the buffer that receives the extra data
291 * extra_size [I/O] Unused
297 DWORD WINAPI
StringTableLookUpStringEx(HSTRING_TABLE hTable
, LPWSTR string
, DWORD flags
,
298 void *extra
, ULONG extra_size
)
300 struct stringtable
*table
= (struct stringtable
*)hTable
;
301 BOOL case_sensitive
= flags
& 1;
302 struct stringentry
*entry
;
306 TRACE("%p->%p %s %x %p, %x\n", table
, table
->data
, debugstr_w(string
), flags
, extra
, extra_size
);
311 /* get corresponding offset */
312 offset
= *get_bucket_ptr(table
, string
, case_sensitive
);
316 /* now we're at correct bucket, do linear search for string */
318 entry
= (struct stringentry
*)(table
->data
+ offset
);
320 cmp
= lstrcmpW(entry
->data
, string
);
322 cmp
= lstrcmpiW(entry
->data
, string
);
325 memcpy(extra
, get_extradata_ptr(table
, offset
), extra_size
);
330 if (entry
->nextoffset
== -1)
333 offset
= entry
->nextoffset
;
334 if (offset
> table
->allocated
)
339 /**************************************************************************
340 * StringTableLookUpString [SETUPAPI.@]
342 * Searches a string table for a given string.
345 * hTable [I] Handle to the string table
346 * string [I] String to be searched for
348 * 1: case sensitive compare
354 DWORD WINAPI
StringTableLookUpString(HSTRING_TABLE hTable
, LPWSTR string
, DWORD flags
)
356 return StringTableLookUpStringEx(hTable
, string
, flags
, NULL
, 0);
359 /**************************************************************************
360 * StringTableAddStringEx [SETUPAPI.@]
362 * Adds a new string plus extra data to the string table.
365 * hTable [I] Handle to the string table
366 * string [I] String to be added to the string table
368 * 1: case sensitive compare
369 * extra [I] Pointer to the extra data
370 * extra_size [I] Size of the extra data
377 * If the given string already exists in the string table it will not
378 * be added again. The ID of the existing string will be returned in
381 DWORD WINAPI
StringTableAddStringEx(HSTRING_TABLE hTable
, LPWSTR string
,
382 DWORD flags
, void *extra
, DWORD extra_size
)
384 struct stringtable
*table
= (struct stringtable
*)hTable
;
385 BOOL case_sensitive
= flags
& 1;
386 struct stringentry
*entry
;
391 TRACE("%p %s %x %p, %u\n", hTable
, debugstr_w(string
), flags
, extra
, extra_size
);
396 id
= StringTableLookUpStringEx(hTable
, string
, flags
, NULL
, 0);
400 /* needed space for new record */
401 len
= sizeof(DWORD
) + (strlenW(string
)+1)*sizeof(WCHAR
) + table
->max_extra_size
;
402 if (table
->nextoffset
+ len
>= table
->allocated
) {
403 table
->allocated
<<= 1;
404 table
->data
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, table
->data
, table
->allocated
);
408 offset
= get_bucket_ptr(table
, string
, case_sensitive
);
410 /* bucket used for a very first time */
411 *offset
= table
->nextoffset
;
413 entry
= (struct stringentry
*)(table
->data
+ *offset
);
414 /* link existing last entry to newly added */
415 while (entry
->nextoffset
!= -1)
416 entry
= (struct stringentry
*)(table
->data
+ entry
->nextoffset
);
417 entry
->nextoffset
= table
->nextoffset
;
419 entry
= (struct stringentry
*)(table
->data
+ table
->nextoffset
);
420 entry
->nextoffset
= -1;
421 id
= table
->nextoffset
;
424 ptrW
= get_string_ptr(table
, id
);
425 strcpyW(ptrW
, string
);
429 /* copy extra data */
431 memcpy(get_extradata_ptr(table
, id
), extra
, extra_size
);
433 table
->nextoffset
+= len
;
437 /**************************************************************************
438 * StringTableAddString [SETUPAPI.@]
440 * Adds a new string to the string table.
443 * hTable [I] Handle to the string table
444 * string [I] String to be added to the string table
446 * 1: case sensitive compare
453 * If the given string already exists in the string table it will not
454 * be added again. The ID of the existing string will be returned in
457 DWORD WINAPI
StringTableAddString(HSTRING_TABLE hTable
, LPWSTR string
, DWORD flags
)
459 return StringTableAddStringEx(hTable
, string
, flags
, NULL
, 0);
462 /**************************************************************************
463 * StringTableSetExtraData [SETUPAPI.@]
465 * Sets extra data for a given string table entry.
468 * hTable [I] Handle to the string table
470 * extra [I] Pointer to the extra data
471 * extra_size [I] Size of the extra data
477 BOOL WINAPI
StringTableSetExtraData(HSTRING_TABLE hTable
, DWORD id
, void *extra
, ULONG extra_size
)
479 struct stringtable
*table
= (struct stringtable
*)hTable
;
482 TRACE("%p %d %p %u\n", hTable
, id
, extra
, extra_size
);
487 if (!is_valid_string_id(table
, id
))
490 if (table
->max_extra_size
< extra_size
)
492 ERR("data size is too large\n");
496 extraptr
= get_extradata_ptr(table
, id
);
497 memset(extraptr
, 0, table
->max_extra_size
);
498 memcpy(extraptr
, extra
, extra_size
);
503 /**************************************************************************
504 * StringTableStringFromId [SETUPAPI.@]
506 * Returns a pointer to a string for the given string ID.
509 * hTable [I] Handle to the string table.
513 * Success: Pointer to the string
516 LPWSTR WINAPI
StringTableStringFromId(HSTRING_TABLE hTable
, ULONG id
)
518 struct stringtable
*table
= (struct stringtable
*)hTable
;
519 static WCHAR empty
[] = {0};
521 TRACE("%p %d\n", table
, id
);
526 if (!is_valid_string_id(table
, id
))
529 return get_string_ptr(table
, id
);
532 /**************************************************************************
533 * StringTableStringFromIdEx [SETUPAPI.@]
535 * Returns a string for the given string ID.
538 * hTable [I] Handle to the string table
540 * buff [I] Pointer to string buffer
541 * buflen [I/O] Pointer to the size of the string buffer
547 BOOL WINAPI
StringTableStringFromIdEx(HSTRING_TABLE hTable
, ULONG id
, LPWSTR buff
, DWORD
*buflen
)
549 struct stringtable
*table
= (struct stringtable
*)hTable
;
554 TRACE("%p %x %p %p\n", table
, id
, buff
, buflen
);
561 if (!is_valid_string_id(table
, id
)) {
562 WARN("invalid string id\n");
567 ptrW
= get_string_ptr(table
, id
);
568 len
= (strlenW(ptrW
) + 1)*sizeof(WCHAR
);
578 /**************************************************************************
579 * StringTableTrim [SETUPAPI.@]
584 * hTable [I] Handle to the string table
589 void WINAPI
StringTableTrim(HSTRING_TABLE hTable
)
591 FIXME("%p\n", hTable
);