[APPHELP] Implement automatic stringtable generation when writing an Sdb database...
[reactos.git] / reactos / dll / appcompat / apphelp / sdbstringtable.c
1 /*
2 * Copyright 2016 Mark Jansen
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #if !defined(SDBWRITE_HOSTTOOL)
20 #define WIN32_NO_STATUS
21 #include "windows.h"
22 #include "sdbtypes.h"
23 #include "sdbpapi.h"
24 #else /* !defined(SDBWRITE_HOSTTOOL) */
25 #include <typedefs.h>
26 #include "sdbtypes.h"
27 #include "sdbpapi.h"
28 #endif /* !defined(SDBWRITE_HOSTTOOL) */
29
30 #include "sdbstringtable.h"
31
32 #define DEFAULT_TABLE_SIZE 0x100
33
34 typedef struct SdbHashEntry
35 {
36 struct SdbHashEntry* Next;
37 TAGID Tagid;
38 WCHAR Name[1];
39 } SdbHashEntry;
40
41 struct SdbStringHashTable
42 {
43 DWORD Size;
44 struct SdbHashEntry** Entries;
45 };
46
47
48 static struct SdbStringHashTable* HashCreate(void)
49 {
50 struct SdbStringHashTable* tab = SdbAlloc(sizeof(*tab));
51 if (!tab)
52 {
53 SHIM_ERR("Failed to allocate 8 bytes.\r\n");
54 return tab;
55 }
56 tab->Size = DEFAULT_TABLE_SIZE;
57 tab->Entries = SdbAlloc(tab->Size * sizeof(*tab->Entries));
58 return tab;
59 }
60
61
62 void SdbpTableDestroy(struct SdbStringHashTable** pTable)
63 {
64 struct SdbStringHashTable* table = *pTable;
65 struct SdbHashEntry* entry, *next;
66 DWORD n, depth = 0, once = 1;
67
68 *pTable = NULL;
69 for (n = 0; n < table->Size; ++n)
70 {
71 depth = 0;
72 entry = next = table->Entries[n];
73 while (entry)
74 {
75 next = entry->Next;
76 SdbFree(entry);
77 entry = next;
78 depth++;
79 }
80 if (once && depth > 3)
81 {
82 // warn
83 once = 0;
84 }
85 }
86 SdbFree(table->Entries);
87 SdbFree(table);
88 }
89
90 /* Based on RtlHashUnicodeString */
91 static DWORD StringHash(const WCHAR* str)
92 {
93 DWORD hash = 0;
94 for (; *str; str++)
95 {
96 hash = ((65599 * hash) + (ULONG)(*str));
97 }
98 return hash;
99 }
100
101 static struct SdbHashEntry** TableFindPtr(struct SdbStringHashTable* table, const WCHAR* str)
102 {
103 DWORD hash = StringHash(str);
104 struct SdbHashEntry** entry = &table->Entries[hash % table->Size];
105 while (*entry)
106 {
107 if (!wcscmp((*entry)->Name, str))
108 return entry;
109 entry = &(*entry)->Next;
110 }
111 return entry;
112 }
113
114 static BOOL HashAddString(struct SdbStringHashTable* table, struct SdbHashEntry** position, const WCHAR* str, TAGID tagid)
115 {
116 struct SdbHashEntry* entry;
117 SIZE_T size;
118
119 if (!position)
120 position = TableFindPtr(table, str);
121
122 size = offsetof(struct SdbHashEntry, Name[SdbpStrlen(str) + 2]);
123 entry = (*position) = SdbAlloc(size);
124 if (!entry)
125 {
126 SHIM_ERR("Failed to allocate %u bytes.", size);
127 return FALSE;
128 }
129 entry->Tagid = tagid;
130 wcscpy(entry->Name, str);
131 return TRUE;
132 }
133
134
135 BOOL SdbpAddStringToTable(struct SdbStringHashTable** table, const WCHAR* str, TAGID* tagid)
136 {
137 struct SdbHashEntry** entry;
138
139 if (!*table)
140 {
141 *table = HashCreate();
142 if (!*table)
143 {
144 SHIM_ERR("Error creating hash table\n");
145 return FALSE;
146 }
147 }
148
149 entry = TableFindPtr(*table, str);
150 if (*entry)
151 {
152 *tagid = (*entry)->Tagid;
153 return FALSE;
154 }
155 return HashAddString(*table, entry, str, *tagid);
156 }
157