blob: 21b1db2a405a9f8fda310a23c9baba1f99eaacde [file] [log] [blame]
Mike McCormackf5e1c132004-03-17 20:49:59 +00001/*
Rob Shearmane261d602007-04-23 08:18:27 +01002 * String Table Functions
Mike McCormackf5e1c132004-03-17 20:49:59 +00003 *
Mike McCormack068b4ec2004-03-19 01:16:36 +00004 * Copyright 2002-2004, Mike McCormack for CodeWeavers
Rob Shearmane261d602007-04-23 08:18:27 +01005 * Copyright 2007 Robert Shearman for CodeWeavers
Mike McCormackf5e1c132004-03-17 20:49:59 +00006 *
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.
11 *
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.
16 *
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
Jonathan Ernst360a3f92006-05-18 14:49:52 +020019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Mike McCormackf5e1c132004-03-17 20:49:59 +000020 */
21
Rob Shearman37b11ba2007-04-23 08:17:40 +010022#define COBJMACROS
23
Mike McCormackf5e1c132004-03-17 20:49:59 +000024#include <stdarg.h>
25#include <assert.h>
26
27#include "windef.h"
28#include "winbase.h"
29#include "winerror.h"
30#include "wine/debug.h"
Mike McCormackab519f22004-06-30 18:18:27 +000031#include "wine/unicode.h"
Mike McCormackf5e1c132004-03-17 20:49:59 +000032#include "msi.h"
33#include "msiquery.h"
34#include "objbase.h"
35#include "objidl.h"
36#include "msipriv.h"
37#include "winnls.h"
38
39#include "query.h"
40
Mike McCormack50684c12005-11-02 14:24:21 +000041WINE_DEFAULT_DEBUG_CHANNEL(msidb);
Mike McCormackf5e1c132004-03-17 20:49:59 +000042
James Hawkinsa05613a2007-06-07 16:41:51 -070043#define LONG_STR_BYTES 3
Mike McCormackf537eb22006-04-03 18:48:52 +090044
Mike McCormackf5e1c132004-03-17 20:49:59 +000045typedef struct _msistring
46{
Hans Leidekker109ffd02010-04-13 11:15:08 +020047 USHORT persistent_refcount;
48 USHORT nonpersistent_refcount;
Mike McCormackab519f22004-06-30 18:18:27 +000049 LPWSTR str;
Mike McCormackf5e1c132004-03-17 20:49:59 +000050} msistring;
51
52struct string_table
53{
Mike McCormackabd259f2004-06-30 18:24:33 +000054 UINT maxcount; /* the number of strings */
Mike McCormackf5e1c132004-03-17 20:49:59 +000055 UINT freeslot;
Mike McCormackc0523aa2004-06-26 00:18:36 +000056 UINT codepage;
Hans Leidekker2c526b72009-12-16 11:11:45 +010057 UINT sortcount;
Hans Leidekker2c526b72009-12-16 11:11:45 +010058 msistring *strings; /* an array of strings */
59 UINT *sorted; /* index */
Mike McCormackf5e1c132004-03-17 20:49:59 +000060};
61
Rob Shearmane261d602007-04-23 08:18:27 +010062static string_table *init_stringtable( int entries, UINT codepage )
Mike McCormackf5e1c132004-03-17 20:49:59 +000063{
64 string_table *st;
65
Rob Shearman0876f762007-06-26 22:24:05 +010066 if (codepage != CP_ACP && !IsValidCodePage(codepage))
67 {
68 ERR("invalid codepage %d\n", codepage);
69 return NULL;
70 }
71
Mike McCormack8dc28d52005-09-20 11:57:19 +000072 st = msi_alloc( sizeof (string_table) );
Mike McCormackf5e1c132004-03-17 20:49:59 +000073 if( !st )
74 return NULL;
Mike McCormackde71dbb2005-09-26 09:54:56 +000075 if( entries < 1 )
76 entries = 1;
Hans Leidekker2c526b72009-12-16 11:11:45 +010077
Mike McCormack8dc28d52005-09-20 11:57:19 +000078 st->strings = msi_alloc_zero( sizeof (msistring) * entries );
Mike McCormacked9745b2006-04-07 13:30:40 +090079 if( !st->strings )
Mike McCormackf5e1c132004-03-17 20:49:59 +000080 {
Mike McCormack8dc28d52005-09-20 11:57:19 +000081 msi_free( st );
Mike McCormackf5e1c132004-03-17 20:49:59 +000082 return NULL;
83 }
Hans Leidekker2c526b72009-12-16 11:11:45 +010084
85 st->sorted = msi_alloc( sizeof (UINT) * entries );
86 if( !st->sorted )
87 {
88 msi_free( st->strings );
89 msi_free( st );
90 return NULL;
91 }
92
Mike McCormackabd259f2004-06-30 18:24:33 +000093 st->maxcount = entries;
Mike McCormack068b4ec2004-03-19 01:16:36 +000094 st->freeslot = 1;
Mike McCormackc0523aa2004-06-26 00:18:36 +000095 st->codepage = codepage;
Hans Leidekker2c526b72009-12-16 11:11:45 +010096 st->sortcount = 0;
Mike McCormackf537eb22006-04-03 18:48:52 +090097
Mike McCormackf5e1c132004-03-17 20:49:59 +000098 return st;
99}
100
101VOID msi_destroy_stringtable( string_table *st )
102{
103 UINT i;
104
Mike McCormackabd259f2004-06-30 18:24:33 +0000105 for( i=0; i<st->maxcount; i++ )
Mike McCormackf5e1c132004-03-17 20:49:59 +0000106 {
Rob Shearman2e3289c2007-04-23 08:17:00 +0100107 if( st->strings[i].persistent_refcount ||
108 st->strings[i].nonpersistent_refcount )
Mike McCormack8dc28d52005-09-20 11:57:19 +0000109 msi_free( st->strings[i].str );
Mike McCormackf5e1c132004-03-17 20:49:59 +0000110 }
Mike McCormack8dc28d52005-09-20 11:57:19 +0000111 msi_free( st->strings );
Hans Leidekker2c526b72009-12-16 11:11:45 +0100112 msi_free( st->sorted );
Mike McCormack8dc28d52005-09-20 11:57:19 +0000113 msi_free( st );
Mike McCormackf5e1c132004-03-17 20:49:59 +0000114}
115
116static int st_find_free_entry( string_table *st )
117{
Hans Leidekker2c526b72009-12-16 11:11:45 +0100118 UINT i, sz, *s;
Mike McCormack068b4ec2004-03-19 01:16:36 +0000119 msistring *p;
Mike McCormackf5e1c132004-03-17 20:49:59 +0000120
Mike McCormackab519f22004-06-30 18:18:27 +0000121 TRACE("%p\n", st);
122
Mike McCormackabd259f2004-06-30 18:24:33 +0000123 if( st->freeslot )
124 {
125 for( i = st->freeslot; i < st->maxcount; i++ )
Rob Shearman2e3289c2007-04-23 08:17:00 +0100126 if( !st->strings[i].persistent_refcount &&
127 !st->strings[i].nonpersistent_refcount )
Mike McCormackabd259f2004-06-30 18:24:33 +0000128 return i;
129 }
130 for( i = 1; i < st->maxcount; i++ )
Rob Shearman2e3289c2007-04-23 08:17:00 +0100131 if( !st->strings[i].persistent_refcount &&
132 !st->strings[i].nonpersistent_refcount )
Mike McCormackf5e1c132004-03-17 20:49:59 +0000133 return i;
134
Mike McCormack068b4ec2004-03-19 01:16:36 +0000135 /* dynamically resize */
Mike McCormackabd259f2004-06-30 18:24:33 +0000136 sz = st->maxcount + 1 + st->maxcount/2;
Mike McCormack8dc28d52005-09-20 11:57:19 +0000137 p = msi_realloc_zero( st->strings, sz*sizeof(msistring) );
Mike McCormack068b4ec2004-03-19 01:16:36 +0000138 if( !p )
139 return -1;
Hans Leidekker2c526b72009-12-16 11:11:45 +0100140
141 s = msi_realloc( st->sorted, sz*sizeof(UINT) );
142 if( !s )
143 {
144 msi_free( p );
145 return -1;
146 }
147
Mike McCormack068b4ec2004-03-19 01:16:36 +0000148 st->strings = p;
Hans Leidekker2c526b72009-12-16 11:11:45 +0100149 st->sorted = s;
150
Mike McCormackabd259f2004-06-30 18:24:33 +0000151 st->freeslot = st->maxcount;
152 st->maxcount = sz;
Rob Shearman2e3289c2007-04-23 08:17:00 +0100153 if( st->strings[st->freeslot].persistent_refcount ||
154 st->strings[st->freeslot].nonpersistent_refcount )
Mike McCormack068b4ec2004-03-19 01:16:36 +0000155 ERR("oops. expected freeslot to be free...\n");
156 return st->freeslot;
Mike McCormackf5e1c132004-03-17 20:49:59 +0000157}
158
Hans Leidekker2c526b72009-12-16 11:11:45 +0100159static int find_insert_index( const string_table *st, UINT string_id )
160{
161 int i, c, low = 0, high = st->sortcount - 1;
162
163 while (low <= high)
164 {
165 i = (low + high) / 2;
166 c = lstrcmpW( st->strings[string_id].str, st->strings[st->sorted[i]].str );
167
168 if (c < 0)
169 high = i - 1;
170 else if (c > 0)
171 low = i + 1;
172 else
173 return -1; /* already exists */
174 }
175 return high + 1;
176}
177
178static void insert_string_sorted( string_table *st, UINT string_id )
179{
180 int i;
181
182 i = find_insert_index( st, string_id );
183 if (i == -1)
184 return;
185
186 memmove( &st->sorted[i] + 1, &st->sorted[i], (st->sortcount - i) * sizeof(UINT) );
187 st->sorted[i] = string_id;
188 st->sortcount++;
189}
190
Hans Leidekker109ffd02010-04-13 11:15:08 +0200191static void set_st_entry( string_table *st, UINT n, LPWSTR str, USHORT refcount, enum StringPersistence persistence )
Mike McCormackf5e1c132004-03-17 20:49:59 +0000192{
Rob Shearman2e3289c2007-04-23 08:17:00 +0100193 if (persistence == StringPersistent)
194 {
195 st->strings[n].persistent_refcount = refcount;
196 st->strings[n].nonpersistent_refcount = 0;
197 }
198 else
199 {
200 st->strings[n].persistent_refcount = 0;
201 st->strings[n].nonpersistent_refcount = refcount;
202 }
203
Mike McCormackf537eb22006-04-03 18:48:52 +0900204 st->strings[n].str = str;
205
Hans Leidekker2c526b72009-12-16 11:11:45 +0100206 insert_string_sorted( st, n );
Mike McCormackf537eb22006-04-03 18:48:52 +0900207
208 if( n < st->maxcount )
209 st->freeslot = n + 1;
Mike McCormackf5e1c132004-03-17 20:49:59 +0000210}
211
Andrew Talbot668197a2009-01-10 14:20:03 +0000212static UINT msi_string2idA( const string_table *st, LPCSTR buffer, UINT *id )
213{
214 DWORD sz;
215 UINT r = ERROR_INVALID_PARAMETER;
216 LPWSTR str;
217
218 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
219
220 if( buffer[0] == 0 )
221 {
222 *id = 0;
223 return ERROR_SUCCESS;
224 }
225
226 sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
227 if( sz <= 0 )
228 return r;
229 str = msi_alloc( sz*sizeof(WCHAR) );
230 if( !str )
231 return ERROR_NOT_ENOUGH_MEMORY;
232 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
233
234 r = msi_string2idW( st, str, id );
235 msi_free( str );
236
237 return r;
238}
239
Hans Leidekker109ffd02010-04-13 11:15:08 +0200240static int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, USHORT refcount, enum StringPersistence persistence )
Mike McCormackf5e1c132004-03-17 20:49:59 +0000241{
Mike McCormackf537eb22006-04-03 18:48:52 +0900242 LPWSTR str;
Mike McCormackab519f22004-06-30 18:18:27 +0000243 int sz;
244
Mike McCormackde8674e2004-06-30 18:42:02 +0000245 if( !data )
246 return 0;
Mike McCormack068b4ec2004-03-19 01:16:36 +0000247 if( !data[0] )
248 return 0;
249 if( n > 0 )
250 {
Rob Shearman2e3289c2007-04-23 08:17:00 +0100251 if( st->strings[n].persistent_refcount ||
252 st->strings[n].nonpersistent_refcount )
Mike McCormack068b4ec2004-03-19 01:16:36 +0000253 return -1;
254 }
255 else
256 {
257 if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
258 {
Rob Shearman2e3289c2007-04-23 08:17:00 +0100259 if (persistence == StringPersistent)
260 st->strings[n].persistent_refcount += refcount;
261 else
262 st->strings[n].nonpersistent_refcount += refcount;
Mike McCormack068b4ec2004-03-19 01:16:36 +0000263 return n;
264 }
265 n = st_find_free_entry( st );
James Hawkins1c1cf262007-11-05 04:49:07 -0500266 if( n == -1 )
Mike McCormack068b4ec2004-03-19 01:16:36 +0000267 return -1;
268 }
Mike McCormackf5e1c132004-03-17 20:49:59 +0000269
Mike McCormackabd259f2004-06-30 18:24:33 +0000270 if( n < 1 )
271 {
272 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
273 return -1;
274 }
275
Mike McCormackf5e1c132004-03-17 20:49:59 +0000276 /* allocate a new string */
277 if( len < 0 )
Mike McCormackab519f22004-06-30 18:18:27 +0000278 len = strlen(data);
279 sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
Mike McCormackf537eb22006-04-03 18:48:52 +0900280 str = msi_alloc( (sz+1)*sizeof(WCHAR) );
281 if( !str )
Mike McCormackf5e1c132004-03-17 20:49:59 +0000282 return -1;
Mike McCormackf537eb22006-04-03 18:48:52 +0900283 MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
284 str[sz] = 0;
Mike McCormackf5e1c132004-03-17 20:49:59 +0000285
Rob Shearman2e3289c2007-04-23 08:17:00 +0100286 set_st_entry( st, n, str, refcount, persistence );
Mike McCormackf5e1c132004-03-17 20:49:59 +0000287
288 return n;
289}
290
Hans Leidekker8f534052010-04-19 12:37:38 +0200291int msi_addstringW( string_table *st, const WCHAR *data, int len, USHORT refcount, enum StringPersistence persistence )
Mike McCormack068b4ec2004-03-19 01:16:36 +0000292{
Hans Leidekker8f534052010-04-19 12:37:38 +0200293 UINT n;
Mike McCormackf537eb22006-04-03 18:48:52 +0900294 LPWSTR str;
295
Mike McCormackde8674e2004-06-30 18:42:02 +0000296 if( !data )
297 return 0;
Mike McCormack068b4ec2004-03-19 01:16:36 +0000298 if( !data[0] )
299 return 0;
Hans Leidekker8f534052010-04-19 12:37:38 +0200300
301 if( msi_string2idW( st, data, &n ) == ERROR_SUCCESS )
Mike McCormack068b4ec2004-03-19 01:16:36 +0000302 {
Hans Leidekker8f534052010-04-19 12:37:38 +0200303 if (persistence == StringPersistent)
304 st->strings[n].persistent_refcount += refcount;
305 else
306 st->strings[n].nonpersistent_refcount += refcount;
307 return n;
Mike McCormack068b4ec2004-03-19 01:16:36 +0000308 }
309
Hans Leidekker8f534052010-04-19 12:37:38 +0200310 n = st_find_free_entry( st );
311 if( n == -1 )
Mike McCormackabd259f2004-06-30 18:24:33 +0000312 return -1;
Mike McCormackabd259f2004-06-30 18:24:33 +0000313
Mike McCormack068b4ec2004-03-19 01:16:36 +0000314 /* allocate a new string */
Mike McCormackab519f22004-06-30 18:18:27 +0000315 if(len<0)
316 len = strlenW(data);
317 TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
318
Mike McCormackf537eb22006-04-03 18:48:52 +0900319 str = msi_alloc( (len+1)*sizeof(WCHAR) );
320 if( !str )
Mike McCormack068b4ec2004-03-19 01:16:36 +0000321 return -1;
Mike McCormackf537eb22006-04-03 18:48:52 +0900322 memcpy( str, data, len*sizeof(WCHAR) );
323 str[len] = 0;
Mike McCormack068b4ec2004-03-19 01:16:36 +0000324
Rob Shearman2e3289c2007-04-23 08:17:00 +0100325 set_st_entry( st, n, str, refcount, persistence );
Mike McCormack068b4ec2004-03-19 01:16:36 +0000326
327 return n;
328}
329
Mike McCormack943a71e2004-03-19 19:14:12 +0000330/* find the string identified by an id - return null if there's none */
Andrew Talbot8b362f72007-06-12 21:51:36 +0100331const WCHAR *msi_string_lookup_id( const string_table *st, UINT id )
Mike McCormackb040e4b2004-03-18 04:04:08 +0000332{
Mike McCormack943a71e2004-03-19 19:14:12 +0000333 if( id == 0 )
Hans Leidekker843382f2009-10-15 12:46:27 +0200334 return szEmpty;
Mike McCormackb040e4b2004-03-18 04:04:08 +0000335
Mike McCormackabd259f2004-06-30 18:24:33 +0000336 if( id >= st->maxcount )
Mike McCormack943a71e2004-03-19 19:14:12 +0000337 return NULL;
Mike McCormackb040e4b2004-03-18 04:04:08 +0000338
Rob Shearman2e3289c2007-04-23 08:17:00 +0100339 if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
Mike McCormack943a71e2004-03-19 19:14:12 +0000340 return NULL;
Mike McCormackb040e4b2004-03-18 04:04:08 +0000341
Mike McCormack943a71e2004-03-19 19:14:12 +0000342 return st->strings[id].str;
Mike McCormackb040e4b2004-03-18 04:04:08 +0000343}
344
Mike McCormack943a71e2004-03-19 19:14:12 +0000345/*
Mike McCormack943a71e2004-03-19 19:14:12 +0000346 * msi_id2stringA
347 *
348 * [in] st - pointer to the string table
Francois Gouget497709b2004-06-15 20:26:45 +0000349 * [in] id - id of the string to retrieve
Mike McCormack943a71e2004-03-19 19:14:12 +0000350 * [out] buffer - destination of the UTF8 string
351 * [in/out] sz - number of bytes available in the buffer on input
352 * number of bytes used on output
353 *
354 * The size includes the terminating nul character. Short buffers
355 * will be filled, but not nul terminated.
356 */
Andrew Talbot668197a2009-01-10 14:20:03 +0000357static UINT msi_id2stringA( const string_table *st, UINT id, LPSTR buffer, UINT *sz )
Mike McCormack943a71e2004-03-19 19:14:12 +0000358{
359 UINT len;
Mike McCormackab519f22004-06-30 18:18:27 +0000360 const WCHAR *str;
Mike McCormackabd259f2004-06-30 18:24:33 +0000361 int n;
Mike McCormack943a71e2004-03-19 19:14:12 +0000362
Mike McCormackabd259f2004-06-30 18:24:33 +0000363 TRACE("Finding string %d of %d\n", id, st->maxcount);
Mike McCormack943a71e2004-03-19 19:14:12 +0000364
Mike McCormack9d66d942004-06-26 00:11:08 +0000365 str = msi_string_lookup_id( st, id );
Mike McCormack943a71e2004-03-19 19:14:12 +0000366 if( !str )
367 return ERROR_FUNCTION_FAILED;
368
Mike McCormackab519f22004-06-30 18:18:27 +0000369 len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
Mike McCormack943a71e2004-03-19 19:14:12 +0000370
371 if( !buffer )
372 {
373 *sz = len;
374 return ERROR_SUCCESS;
375 }
376
Mike McCormackabd259f2004-06-30 18:24:33 +0000377 if( len > *sz )
378 {
379 n = strlenW( str ) + 1;
380 while( n && (len > *sz) )
381 len = WideCharToMultiByte( st->codepage, 0,
382 str, --n, NULL, 0, NULL, NULL );
383 }
384 else
385 n = -1;
386
387 *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
Mike McCormack943a71e2004-03-19 19:14:12 +0000388
389 return ERROR_SUCCESS;
390}
391
392/*
Mike McCormackab519f22004-06-30 18:18:27 +0000393 * msi_string2idW
Mike McCormack943a71e2004-03-19 19:14:12 +0000394 *
395 * [in] st - pointer to the string table
Mike McCormackab519f22004-06-30 18:18:27 +0000396 * [in] str - string to find in the string table
Mike McCormack943a71e2004-03-19 19:14:12 +0000397 * [out] id - id of the string, if found
398 */
Andrew Talbot8b362f72007-06-12 21:51:36 +0100399UINT msi_string2idW( const string_table *st, LPCWSTR str, UINT *id )
Mike McCormackf5e1c132004-03-17 20:49:59 +0000400{
Hans Leidekker2c526b72009-12-16 11:11:45 +0100401 int i, c, low = 0, high = st->sortcount - 1;
Mike McCormackf5e1c132004-03-17 20:49:59 +0000402
Hans Leidekker2c526b72009-12-16 11:11:45 +0100403 while (low <= high)
Mike McCormackf5e1c132004-03-17 20:49:59 +0000404 {
Hans Leidekker2c526b72009-12-16 11:11:45 +0100405 i = (low + high) / 2;
406 c = lstrcmpW( str, st->strings[st->sorted[i]].str );
407
408 if (c < 0)
409 high = i - 1;
410 else if (c > 0)
411 low = i + 1;
412 else
Mike McCormackf5e1c132004-03-17 20:49:59 +0000413 {
Hans Leidekker2c526b72009-12-16 11:11:45 +0100414 *id = st->sorted[i];
Mike McCormackf537eb22006-04-03 18:48:52 +0900415 return ERROR_SUCCESS;
Mike McCormackf5e1c132004-03-17 20:49:59 +0000416 }
417 }
418
Mike McCormackf537eb22006-04-03 18:48:52 +0900419 return ERROR_INVALID_PARAMETER;
Mike McCormack068b4ec2004-03-19 01:16:36 +0000420}
421
Andrew Talbot8b362f72007-06-12 21:51:36 +0100422static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
Mike McCormackb040e4b2004-03-18 04:04:08 +0000423{
James Hawkins22743882007-10-17 22:57:18 -0500424 UINT i, len, holesize;
Mike McCormackb040e4b2004-03-18 04:04:08 +0000425
Rob Shearman2e3289c2007-04-23 08:17:00 +0100426 if( st->strings[0].str || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
Mike McCormackabd259f2004-06-30 18:24:33 +0000427 ERR("oops. element 0 has a string\n");
Mike McCormack5f832b22006-08-28 16:51:41 +0900428
429 *poolsize = 4;
430 *datasize = 0;
Mike McCormack5f832b22006-08-28 16:51:41 +0900431 holesize = 0;
Mike McCormackabd259f2004-06-30 18:24:33 +0000432 for( i=1; i<st->maxcount; i++ )
Mike McCormackb040e4b2004-03-18 04:04:08 +0000433 {
Rob Shearman2e3289c2007-04-23 08:17:00 +0100434 if( !st->strings[i].persistent_refcount )
James Hawkins22743882007-10-17 22:57:18 -0500435 {
436 TRACE("[%u] nonpersistent = %s\n", i, debugstr_w(st->strings[i].str));
437 (*poolsize) += 4;
438 }
439 else if( st->strings[i].str )
Mike McCormackab519f22004-06-30 18:18:27 +0000440 {
Mike McCormackabd259f2004-06-30 18:24:33 +0000441 TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
Mike McCormackab519f22004-06-30 18:18:27 +0000442 len = WideCharToMultiByte( st->codepage, 0,
443 st->strings[i].str, -1, NULL, 0, NULL, NULL);
444 if( len )
445 len--;
Mike McCormack5f832b22006-08-28 16:51:41 +0900446 (*datasize) += len;
447 if (len>0xffff)
448 (*poolsize) += 4;
Mike McCormack5f832b22006-08-28 16:51:41 +0900449 (*poolsize) += holesize + 4;
450 holesize = 0;
Mike McCormackab519f22004-06-30 18:18:27 +0000451 }
Mike McCormack5f832b22006-08-28 16:51:41 +0900452 else
453 holesize += 4;
Mike McCormackb040e4b2004-03-18 04:04:08 +0000454 }
Mike McCormack5f832b22006-08-28 16:51:41 +0900455 TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
Mike McCormackabd259f2004-06-30 18:24:33 +0000456}
Rob Shearman37b11ba2007-04-23 08:17:40 +0100457
458static const WCHAR szStringData[] = {
459 '_','S','t','r','i','n','g','D','a','t','a',0 };
460static const WCHAR szStringPool[] = {
461 '_','S','t','r','i','n','g','P','o','o','l',0 };
462
463HRESULT msi_init_string_table( IStorage *stg )
464{
465 USHORT zero[2] = { 0, 0 };
466 UINT ret;
467
468 /* create the StringPool stream... add the zero string to it*/
James Hawkinsda552852007-04-25 05:22:01 -0500469 ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
Rob Shearman37b11ba2007-04-23 08:17:40 +0100470 if (ret != ERROR_SUCCESS)
471 return E_FAIL;
472
473 /* create the StringData stream... make it zero length */
James Hawkinsda552852007-04-25 05:22:01 -0500474 ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
Rob Shearman37b11ba2007-04-23 08:17:40 +0100475 if (ret != ERROR_SUCCESS)
476 return E_FAIL;
477
478 return S_OK;
479}
480
James Hawkinsa05613a2007-06-07 16:41:51 -0700481string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
Rob Shearman37b11ba2007-04-23 08:17:40 +0100482{
483 string_table *st = NULL;
484 CHAR *data = NULL;
485 USHORT *pool = NULL;
486 UINT r, datasize = 0, poolsize = 0, codepage;
487 DWORD i, count, offset, len, n, refs;
488
James Hawkinsdc3060c2007-07-16 14:45:29 -0700489 r = read_stream_data( stg, szStringPool, TRUE, (BYTE **)&pool, &poolsize );
Rob Shearman37b11ba2007-04-23 08:17:40 +0100490 if( r != ERROR_SUCCESS)
491 goto end;
James Hawkinsdc3060c2007-07-16 14:45:29 -0700492 r = read_stream_data( stg, szStringData, TRUE, (BYTE **)&data, &datasize );
Rob Shearman37b11ba2007-04-23 08:17:40 +0100493 if( r != ERROR_SUCCESS)
494 goto end;
495
Rob Shearman0876f762007-06-26 22:24:05 +0100496 if ( (poolsize > 4) && (pool[1] & 0x8000) )
James Hawkinsa05613a2007-06-07 16:41:51 -0700497 *bytes_per_strref = LONG_STR_BYTES;
498 else
499 *bytes_per_strref = sizeof(USHORT);
500
Rob Shearman37b11ba2007-04-23 08:17:40 +0100501 count = poolsize/4;
Rob Shearman0876f762007-06-26 22:24:05 +0100502 if( poolsize > 4 )
503 codepage = pool[0] | ( (pool[1] & ~0x8000) << 16 );
Rob Shearman37b11ba2007-04-23 08:17:40 +0100504 else
505 codepage = CP_ACP;
Rob Shearmane261d602007-04-23 08:18:27 +0100506 st = init_stringtable( count, codepage );
Rob Shearman0876f762007-06-26 22:24:05 +0100507 if (!st)
508 goto end;
Rob Shearman37b11ba2007-04-23 08:17:40 +0100509
510 offset = 0;
511 n = 1;
512 i = 1;
513 while( i<count )
514 {
515 /* the string reference count is always the second word */
516 refs = pool[i*2+1];
517
518 /* empty entries have two zeros, still have a string id */
519 if (pool[i*2] == 0 && refs == 0)
520 {
521 i++;
522 n++;
523 continue;
524 }
525
526 /*
527 * If a string is over 64k, the previous string entry is made null
528 * and its the high word of the length is inserted in the null string's
529 * reference count field.
530 */
531 if( pool[i*2] == 0)
532 {
533 len = (pool[i*2+3] << 16) + pool[i*2+2];
534 i += 2;
535 }
536 else
537 {
538 len = pool[i*2];
539 i += 1;
540 }
541
542 if ( (offset + len) > datasize )
543 {
544 ERR("string table corrupt?\n");
545 break;
546 }
547
548 r = msi_addstring( st, n, data+offset, len, refs, StringPersistent );
549 if( r != n )
550 ERR("Failed to add string %d\n", n );
551 n++;
552 offset += len;
553 }
554
555 if ( datasize != offset )
556 ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
557
558 TRACE("Loaded %d strings\n", count);
559
560end:
561 msi_free( pool );
562 msi_free( data );
563
564 return st;
565}
566
Andrew Talbot8b362f72007-06-12 21:51:36 +0100567UINT msi_save_string_table( const string_table *st, IStorage *storage )
Rob Shearman37b11ba2007-04-23 08:17:40 +0100568{
Rob Shearmane261d602007-04-23 08:18:27 +0100569 UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
Rob Shearman37b11ba2007-04-23 08:17:40 +0100570 UINT ret = ERROR_FUNCTION_FAILED;
571 CHAR *data = NULL;
572 USHORT *pool = NULL;
573
574 TRACE("\n");
575
576 /* construct the new table in memory first */
Rob Shearmane261d602007-04-23 08:18:27 +0100577 string_totalsize( st, &datasize, &poolsize );
Rob Shearman37b11ba2007-04-23 08:17:40 +0100578
Rob Shearmane261d602007-04-23 08:18:27 +0100579 TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
Rob Shearman37b11ba2007-04-23 08:17:40 +0100580
581 pool = msi_alloc( poolsize );
582 if( ! pool )
583 {
584 WARN("Failed to alloc pool %d bytes\n", poolsize );
585 goto err;
586 }
587 data = msi_alloc( datasize );
588 if( ! data )
589 {
590 WARN("Failed to alloc data %d bytes\n", poolsize );
591 goto err;
592 }
593
594 used = 0;
Rob Shearmane261d602007-04-23 08:18:27 +0100595 codepage = st->codepage;
Rob Shearman37b11ba2007-04-23 08:17:40 +0100596 pool[0]=codepage&0xffff;
597 pool[1]=(codepage>>16);
598 n = 1;
Rob Shearmane261d602007-04-23 08:18:27 +0100599 for( i=1; i<st->maxcount; i++ )
Rob Shearman37b11ba2007-04-23 08:17:40 +0100600 {
Rob Shearmane261d602007-04-23 08:18:27 +0100601 if( !st->strings[i].persistent_refcount )
James Hawkins22743882007-10-17 22:57:18 -0500602 {
603 pool[ n*2 ] = 0;
604 pool[ n*2 + 1] = 0;
605 n++;
Rob Shearman37b11ba2007-04-23 08:17:40 +0100606 continue;
James Hawkins22743882007-10-17 22:57:18 -0500607 }
608
Rob Shearman37b11ba2007-04-23 08:17:40 +0100609 sz = datasize - used;
610 r = msi_id2stringA( st, i, data+used, &sz );
611 if( r != ERROR_SUCCESS )
612 {
613 ERR("failed to fetch string\n");
614 sz = 0;
615 }
616 if( sz && (sz < (datasize - used ) ) )
617 sz--;
618
619 if (sz)
Rob Shearmane261d602007-04-23 08:18:27 +0100620 pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
Rob Shearman37b11ba2007-04-23 08:17:40 +0100621 else
622 pool[ n*2 + 1 ] = 0;
623 if (sz < 0x10000)
624 {
625 pool[ n*2 ] = sz;
626 n++;
627 }
628 else
629 {
630 pool[ n*2 ] = 0;
631 pool[ n*2 + 2 ] = sz&0xffff;
632 pool[ n*2 + 3 ] = (sz>>16);
633 n += 2;
634 }
635 used += sz;
636 if( used > datasize )
637 {
638 ERR("oops overran %d >= %d\n", used, datasize);
639 goto err;
640 }
641 }
642
643 if( used != datasize )
644 {
645 ERR("oops used %d != datasize %d\n", used, datasize);
646 goto err;
647 }
648
649 /* write the streams */
James Hawkinsda552852007-04-25 05:22:01 -0500650 r = write_stream_data( storage, szStringData, data, datasize, TRUE );
Rob Shearman37b11ba2007-04-23 08:17:40 +0100651 TRACE("Wrote StringData r=%08x\n", r);
652 if( r )
653 goto err;
James Hawkinsda552852007-04-25 05:22:01 -0500654 r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
Rob Shearman37b11ba2007-04-23 08:17:40 +0100655 TRACE("Wrote StringPool r=%08x\n", r);
656 if( r )
657 goto err;
658
659 ret = ERROR_SUCCESS;
660
661err:
662 msi_free( data );
663 msi_free( pool );
664
665 return ret;
666}