2 * Setupapi string table functions
4 * Copyright 2005 Eric Kohl
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
33 #include "wine/debug.h"
36 #define TABLE_DEFAULT_SIZE 256
38 WINE_DEFAULT_DEBUG_CHANNEL(setupapi);
40 DECLARE_HANDLE(HSTRING_TABLE);
42 typedef struct _TABLE_SLOT
47 } TABLE_SLOT, *PTABLE_SLOT;
49 typedef struct _STRING_TABLE
55 } STRING_TABLE, *PSTRING_TABLE;
58 /**************************************************************************
59 * StringTableInitialize [SETUPAPI.@]
61 * Creates a new string table and initializes it.
67 * Success: Handle to the string table
71 StringTableInitialize(VOID)
73 PSTRING_TABLE pStringTable;
77 pStringTable = MyMalloc(sizeof(STRING_TABLE));
78 if (pStringTable == NULL)
80 ERR("Invalid hStringTable!\n");
84 memset(pStringTable, 0, sizeof(STRING_TABLE));
86 pStringTable->pSlots = MyMalloc(sizeof(TABLE_SLOT) * TABLE_DEFAULT_SIZE);
87 if (pStringTable->pSlots == NULL)
93 memset(pStringTable->pSlots, 0, sizeof(TABLE_SLOT) * TABLE_DEFAULT_SIZE);
95 pStringTable->dwUsedSlots = 0;
96 pStringTable->dwMaxSlots = TABLE_DEFAULT_SIZE;
97 pStringTable->dwMaxDataSize = 0;
101 return (HSTRING_TABLE)pStringTable;
105 /**************************************************************************
106 * StringTableInitializeEx [SETUPAPI.@]
108 * Creates a new string table and initializes it.
111 * dwMaxExtraDataSize [I] Maximum extra data size
112 * dwReserved [I] Unused
115 * Success: Handle to the string table
119 StringTableInitializeEx(DWORD dwMaxExtraDataSize,
122 PSTRING_TABLE pStringTable;
126 pStringTable = MyMalloc(sizeof(STRING_TABLE));
127 if (pStringTable == NULL) return NULL;
129 memset(pStringTable, 0, sizeof(STRING_TABLE));
131 pStringTable->pSlots = MyMalloc(sizeof(TABLE_SLOT) * TABLE_DEFAULT_SIZE);
132 if (pStringTable->pSlots == NULL)
134 MyFree(pStringTable);
138 memset(pStringTable->pSlots, 0, sizeof(TABLE_SLOT) * TABLE_DEFAULT_SIZE);
140 pStringTable->dwUsedSlots = 0;
141 pStringTable->dwMaxSlots = TABLE_DEFAULT_SIZE;
142 pStringTable->dwMaxDataSize = dwMaxExtraDataSize;
146 return (HSTRING_TABLE)pStringTable;
150 /**************************************************************************
151 * StringTableDestroy [SETUPAPI.@]
153 * Destroys a string table.
156 * hStringTable [I] Handle to the string table to be destroyed
162 StringTableDestroy(HSTRING_TABLE hStringTable)
164 PSTRING_TABLE pStringTable;
167 TRACE("%p\n", hStringTable);
169 pStringTable = (PSTRING_TABLE)hStringTable;
170 if (pStringTable == NULL)
173 if (pStringTable->pSlots != NULL)
175 for (i = 0; i < pStringTable->dwMaxSlots; i++)
177 MyFree(pStringTable->pSlots[i].pString);
178 pStringTable->pSlots[i].pString = NULL;
180 MyFree(pStringTable->pSlots[i].pData);
181 pStringTable->pSlots[i].pData = NULL;
182 pStringTable->pSlots[i].dwSize = 0;
185 MyFree(pStringTable->pSlots);
188 MyFree(pStringTable);
192 /**************************************************************************
193 * StringTableAddStringEx [SETUPAPI.@]
195 * Adds a new string plus extra data to the string table.
198 * hStringTable [I] Handle to the string table
199 * lpString [I] String to be added to the string table
201 * 1: case sensitive compare
202 * lpExtraData [I] Pointer to the extra data
203 * dwExtraDataSize [I] Size of the extra data
210 * If the given string already exists in the string table it will not
211 * be added again. The ID of the existing string will be returned in
215 StringTableAddStringEx(HSTRING_TABLE hStringTable, LPWSTR lpString,
216 DWORD dwFlags, LPVOID lpExtraData, DWORD dwExtraDataSize)
218 PSTRING_TABLE pStringTable;
221 TRACE("%p %s %x %p, %u\n", hStringTable, debugstr_w(lpString), dwFlags,
222 lpExtraData, dwExtraDataSize);
224 pStringTable = (PSTRING_TABLE)hStringTable;
227 ERR("Invalid hStringTable!\n");
231 /* Search for existing string in the string table */
232 for (i = 0; i < pStringTable->dwMaxSlots; i++)
234 if (pStringTable->pSlots[i].pString)
238 if (!lstrcmpW(pStringTable->pSlots[i].pString, lpString))
243 if (!lstrcmpiW(pStringTable->pSlots[i].pString, lpString))
249 /* Check for filled slot table */
250 if (pStringTable->dwUsedSlots == pStringTable->dwMaxSlots)
252 FIXME("Resize the string table!\n");
256 /* Search for an empty slot */
257 for (i = 0; i < pStringTable->dwMaxSlots; i++)
259 if (!pStringTable->pSlots[i].pString)
261 pStringTable->pSlots[i].pString = MyMalloc((lstrlenW(lpString) + 1) * sizeof(WCHAR));
262 if (!pStringTable->pSlots[i].pString)
264 WARN("Couldn't allocate memory for a new string!\n");
267 lstrcpyW(pStringTable->pSlots[i].pString, lpString);
269 pStringTable->pSlots[i].pData = MyMalloc(dwExtraDataSize);
270 if (!pStringTable->pSlots[i].pData)
272 TRACE("Couldn't allocate memory for data!\n");
275 memcpy(pStringTable->pSlots[i].pData, lpExtraData, dwExtraDataSize);
276 pStringTable->dwUsedSlots++;
280 TRACE("Couldn't find an empty slot!\n");
284 /**************************************************************************
285 * StringTableAddString [SETUPAPI.@]
287 * Adds a new string to the string table.
290 * hStringTable [I] Handle to the string table
291 * lpString [I] String to be added to the string table
293 * 1: case sensitive compare
300 * If the given string already exists in the string table it will not
301 * be added again. The ID of the existing string will be returned in
305 StringTableAddString(HSTRING_TABLE hStringTable, LPWSTR lpString, DWORD dwFlags)
307 return StringTableAddStringEx(hStringTable, lpString, dwFlags, NULL, 0);
311 /**************************************************************************
312 * StringTableDuplicate [SETUPAPI.@]
314 * Duplicates a given string table.
317 * hStringTable [I] Handle to the string table
320 * Success: Handle to the duplicated string table
325 StringTableDuplicate(HSTRING_TABLE hStringTable)
327 PSTRING_TABLE pSourceTable;
328 PSTRING_TABLE pDestinationTable;
332 TRACE("%p\n", hStringTable);
334 pSourceTable = (PSTRING_TABLE)hStringTable;
335 if (pSourceTable == NULL)
337 ERR("Invalid hStringTable!\n");
341 pDestinationTable = MyMalloc(sizeof(STRING_TABLE));
342 if (pDestinationTable == NULL)
344 ERR("Could not allocate a new string table!\n");
348 memset(pDestinationTable, 0, sizeof(STRING_TABLE));
350 pDestinationTable->pSlots = MyMalloc(sizeof(TABLE_SLOT) * pSourceTable->dwMaxSlots);
351 if (pDestinationTable->pSlots == NULL)
353 MyFree(pDestinationTable);
357 memset(pDestinationTable->pSlots, 0, sizeof(TABLE_SLOT) * pSourceTable->dwMaxSlots);
359 pDestinationTable->dwUsedSlots = 0;
360 pDestinationTable->dwMaxSlots = pSourceTable->dwMaxSlots;
362 for (i = 0; i < pSourceTable->dwMaxSlots; i++)
364 if (pSourceTable->pSlots[i].pString != NULL)
366 length = (lstrlenW(pSourceTable->pSlots[i].pString) + 1) * sizeof(WCHAR);
367 pDestinationTable->pSlots[i].pString = MyMalloc(length);
368 if (pDestinationTable->pSlots[i].pString != NULL)
370 memcpy(pDestinationTable->pSlots[i].pString,
371 pSourceTable->pSlots[i].pString,
373 pDestinationTable->dwUsedSlots++;
376 if (pSourceTable->pSlots[i].pData != NULL)
378 length = pSourceTable->pSlots[i].dwSize;
379 pDestinationTable->pSlots[i].pData = MyMalloc(length);
380 if (pDestinationTable->pSlots[i].pData)
382 memcpy(pDestinationTable->pSlots[i].pData,
383 pSourceTable->pSlots[i].pData,
385 pDestinationTable->pSlots[i].dwSize = length;
391 return (HSTRING_TABLE)pDestinationTable;
395 /**************************************************************************
396 * StringTableGetExtraData [SETUPAPI.@]
398 * Retrieves extra data from a given string table entry.
401 * hStringTable [I] Handle to the string table
403 * lpExtraData [I] Pointer a buffer that receives the extra data
404 * dwExtraDataSize [I] Size of the buffer
411 StringTableGetExtraData(HSTRING_TABLE hStringTable,
414 DWORD dwExtraDataSize)
416 PSTRING_TABLE pStringTable;
418 TRACE("%p %x %p %u\n",
419 hStringTable, dwId, lpExtraData, dwExtraDataSize);
421 pStringTable = (PSTRING_TABLE)hStringTable;
422 if (pStringTable == NULL)
424 ERR("Invalid hStringTable!\n");
428 if (dwId == 0 || dwId > pStringTable->dwMaxSlots)
430 ERR("Invalid Slot id!\n");
434 if (pStringTable->pSlots[dwId - 1].dwSize > dwExtraDataSize)
436 ERR("Data size is too large!\n");
441 pStringTable->pSlots[dwId - 1].pData,
448 /**************************************************************************
449 * StringTableLookUpStringEx [SETUPAPI.@]
451 * Searches a string table and extra data for a given string.
454 * hStringTable [I] Handle to the string table
455 * lpString [I] String to be searched for
457 * 1: case sensitive compare
458 * lpExtraData [O] Pointer to the buffer that receives the extra data
459 * lpReserved [I/O] Unused
466 StringTableLookUpStringEx(HSTRING_TABLE hStringTable,
472 PSTRING_TABLE pStringTable;
475 TRACE("%p %s %x %p, %p\n", hStringTable, debugstr_w(lpString), dwFlags,
476 lpExtraData, lpReserved);
478 pStringTable = (PSTRING_TABLE)hStringTable;
479 if (pStringTable == NULL)
481 ERR("Invalid hStringTable!\n");
485 /* Search for existing string in the string table */
486 for (i = 0; i < pStringTable->dwMaxSlots; i++)
488 if (pStringTable->pSlots[i].pString != NULL)
492 if (!lstrcmpW(pStringTable->pSlots[i].pString, lpString))
495 memcpy(lpExtraData, pStringTable->pSlots[i].pData, pStringTable->pSlots[i].dwSize);
501 if (!lstrcmpiW(pStringTable->pSlots[i].pString, lpString))
504 memcpy(lpExtraData, pStringTable->pSlots[i].pData, pStringTable->pSlots[i].dwSize);
514 /**************************************************************************
515 * StringTableLookUpString [SETUPAPI.@]
517 * Searches a string table for a given string.
520 * hStringTable [I] Handle to the string table
521 * lpString [I] String to be searched for
523 * 1: case sensitive compare
530 StringTableLookUpString(HSTRING_TABLE hStringTable,
534 return StringTableLookUpStringEx(hStringTable, lpString, dwFlags, NULL, NULL);
538 /**************************************************************************
539 * StringTableSetExtraData [SETUPAPI.@]
541 * Sets extra data for a given string table entry.
544 * hStringTable [I] Handle to the string table
546 * lpExtraData [I] Pointer to the extra data
547 * dwExtraDataSize [I] Size of the extra data
554 StringTableSetExtraData(HSTRING_TABLE hStringTable,
557 DWORD dwExtraDataSize)
559 PSTRING_TABLE pStringTable;
561 TRACE("%p %x %p %u\n",
562 hStringTable, dwId, lpExtraData, dwExtraDataSize);
564 pStringTable = (PSTRING_TABLE)hStringTable;
565 if (pStringTable == NULL)
567 ERR("Invalid hStringTable!\n");
571 if (dwId == 0 || dwId > pStringTable->dwMaxSlots)
573 ERR("Invalid Slot id!\n");
577 if (pStringTable->dwMaxDataSize < dwExtraDataSize)
579 ERR("Data size is too large!\n");
583 pStringTable->pSlots[dwId - 1].pData = MyMalloc(dwExtraDataSize);
584 if (pStringTable->pSlots[dwId - 1].pData == NULL)
590 memcpy(pStringTable->pSlots[dwId - 1].pData,
593 pStringTable->pSlots[dwId - 1].dwSize = dwExtraDataSize;
599 /**************************************************************************
600 * StringTableStringFromId [SETUPAPI.@]
602 * Returns a pointer to a string for the given string ID.
605 * hStringTable [I] Handle to the string table.
609 * Success: Pointer to the string
613 StringTableStringFromId(HSTRING_TABLE hStringTable,
616 PSTRING_TABLE pStringTable;
617 static WCHAR empty[] = {0};
619 TRACE("%p %x\n", hStringTable, dwId);
621 pStringTable = (PSTRING_TABLE)hStringTable;
622 if (pStringTable == NULL)
624 ERR("Invalid hStringTable!\n");
628 if (dwId == 0 || dwId > pStringTable->dwMaxSlots)
631 return pStringTable->pSlots[dwId - 1].pString;
635 /**************************************************************************
636 * StringTableStringFromIdEx [SETUPAPI.@]
638 * Returns a string for the given string ID.
641 * hStringTable [I] Handle to the string table
643 * lpBuffer [I] Pointer to string buffer
644 * lpBufferSize [I/O] Pointer to the size of the string buffer
651 StringTableStringFromIdEx(HSTRING_TABLE hStringTable,
654 LPDWORD lpBufferLength)
656 PSTRING_TABLE pStringTable;
658 BOOL bResult = FALSE;
660 TRACE("%p %x %p %p\n", hStringTable, dwId, lpBuffer, lpBufferLength);
662 pStringTable = (PSTRING_TABLE)hStringTable;
663 if (pStringTable == NULL)
665 ERR("Invalid hStringTable!\n");
670 if (dwId == 0 || dwId > pStringTable->dwMaxSlots ||
671 pStringTable->pSlots[dwId - 1].pString == NULL)
673 WARN("Invalid string ID!\n");
678 dwLength = (lstrlenW(pStringTable->pSlots[dwId - 1].pString) + 1) * sizeof(WCHAR);
679 if (dwLength <= *lpBufferLength)
681 lstrcpyW(lpBuffer, pStringTable->pSlots[dwId - 1].pString);
685 *lpBufferLength = dwLength;
691 /**************************************************************************
692 * StringTableTrim [SETUPAPI.@]
697 * hStringTable [I] Handle to the string table
703 StringTableTrim(HSTRING_TABLE hStringTable)
705 FIXME("%p\n", hStringTable);