[CMAKE]
[reactos.git] / dll / win32 / msi / string.c
1 /*
2 * String Table Functions
3 *
4 * Copyright 2002-2004, Mike McCormack for CodeWeavers
5 * Copyright 2007 Robert Shearman for CodeWeavers
6 * Copyright 2010 Hans Leidekker for CodeWeavers
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #define COBJMACROS
24
25 #include <stdarg.h>
26 #include <assert.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33 #include "msi.h"
34 #include "msiquery.h"
35 #include "objbase.h"
36 #include "objidl.h"
37 #include "msipriv.h"
38 #include "winnls.h"
39
40 #include "query.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
43
44 typedef struct _msistring
45 {
46 USHORT persistent_refcount;
47 USHORT nonpersistent_refcount;
48 LPWSTR str;
49 } msistring;
50
51 struct string_table
52 {
53 UINT maxcount; /* the number of strings */
54 UINT freeslot;
55 UINT codepage;
56 UINT sortcount;
57 msistring *strings; /* an array of strings */
58 UINT *sorted; /* index */
59 };
60
61 static string_table *init_stringtable( int entries, UINT codepage )
62 {
63 string_table *st;
64
65 if (codepage != CP_ACP && !IsValidCodePage(codepage))
66 {
67 ERR("invalid codepage %d\n", codepage);
68 return NULL;
69 }
70
71 st = msi_alloc( sizeof (string_table) );
72 if( !st )
73 return NULL;
74 if( entries < 1 )
75 entries = 1;
76
77 st->strings = msi_alloc_zero( sizeof (msistring) * entries );
78 if( !st->strings )
79 {
80 msi_free( st );
81 return NULL;
82 }
83
84 st->sorted = msi_alloc( sizeof (UINT) * entries );
85 if( !st->sorted )
86 {
87 msi_free( st->strings );
88 msi_free( st );
89 return NULL;
90 }
91
92 st->maxcount = entries;
93 st->freeslot = 1;
94 st->codepage = codepage;
95 st->sortcount = 0;
96
97 return st;
98 }
99
100 VOID msi_destroy_stringtable( string_table *st )
101 {
102 UINT i;
103
104 for( i=0; i<st->maxcount; i++ )
105 {
106 if( st->strings[i].persistent_refcount ||
107 st->strings[i].nonpersistent_refcount )
108 msi_free( st->strings[i].str );
109 }
110 msi_free( st->strings );
111 msi_free( st->sorted );
112 msi_free( st );
113 }
114
115 static int st_find_free_entry( string_table *st )
116 {
117 UINT i, sz, *s;
118 msistring *p;
119
120 TRACE("%p\n", st);
121
122 if( st->freeslot )
123 {
124 for( i = st->freeslot; i < st->maxcount; i++ )
125 if( !st->strings[i].persistent_refcount &&
126 !st->strings[i].nonpersistent_refcount )
127 return i;
128 }
129 for( i = 1; i < st->maxcount; i++ )
130 if( !st->strings[i].persistent_refcount &&
131 !st->strings[i].nonpersistent_refcount )
132 return i;
133
134 /* dynamically resize */
135 sz = st->maxcount + 1 + st->maxcount/2;
136 p = msi_realloc_zero( st->strings, sz*sizeof(msistring) );
137 if( !p )
138 return -1;
139
140 s = msi_realloc( st->sorted, sz*sizeof(UINT) );
141 if( !s )
142 {
143 msi_free( p );
144 return -1;
145 }
146
147 st->strings = p;
148 st->sorted = s;
149
150 st->freeslot = st->maxcount;
151 st->maxcount = sz;
152 if( st->strings[st->freeslot].persistent_refcount ||
153 st->strings[st->freeslot].nonpersistent_refcount )
154 ERR("oops. expected freeslot to be free...\n");
155 return st->freeslot;
156 }
157
158 static int find_insert_index( const string_table *st, UINT string_id )
159 {
160 int i, c, low = 0, high = st->sortcount - 1;
161
162 while (low <= high)
163 {
164 i = (low + high) / 2;
165 c = lstrcmpW( st->strings[string_id].str, st->strings[st->sorted[i]].str );
166
167 if (c < 0)
168 high = i - 1;
169 else if (c > 0)
170 low = i + 1;
171 else
172 return -1; /* already exists */
173 }
174 return high + 1;
175 }
176
177 static void insert_string_sorted( string_table *st, UINT string_id )
178 {
179 int i;
180
181 i = find_insert_index( st, string_id );
182 if (i == -1)
183 return;
184
185 memmove( &st->sorted[i] + 1, &st->sorted[i], (st->sortcount - i) * sizeof(UINT) );
186 st->sorted[i] = string_id;
187 st->sortcount++;
188 }
189
190 static void set_st_entry( string_table *st, UINT n, LPWSTR str, USHORT refcount, enum StringPersistence persistence )
191 {
192 if (persistence == StringPersistent)
193 {
194 st->strings[n].persistent_refcount = refcount;
195 st->strings[n].nonpersistent_refcount = 0;
196 }
197 else
198 {
199 st->strings[n].persistent_refcount = 0;
200 st->strings[n].nonpersistent_refcount = refcount;
201 }
202
203 st->strings[n].str = str;
204
205 insert_string_sorted( st, n );
206
207 if( n < st->maxcount )
208 st->freeslot = n + 1;
209 }
210
211 static UINT msi_string2idA( const string_table *st, LPCSTR buffer, UINT *id )
212 {
213 DWORD sz;
214 UINT r = ERROR_INVALID_PARAMETER;
215 LPWSTR str;
216
217 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
218
219 if( buffer[0] == 0 )
220 {
221 *id = 0;
222 return ERROR_SUCCESS;
223 }
224
225 sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
226 if( sz <= 0 )
227 return r;
228 str = msi_alloc( sz*sizeof(WCHAR) );
229 if( !str )
230 return ERROR_NOT_ENOUGH_MEMORY;
231 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
232
233 r = msi_string2idW( st, str, id );
234 msi_free( str );
235
236 return r;
237 }
238
239 static int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, USHORT refcount, enum StringPersistence persistence )
240 {
241 LPWSTR str;
242 int sz;
243
244 if( !data )
245 return 0;
246 if( !data[0] )
247 return 0;
248 if( n > 0 )
249 {
250 if( st->strings[n].persistent_refcount ||
251 st->strings[n].nonpersistent_refcount )
252 return -1;
253 }
254 else
255 {
256 if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
257 {
258 if (persistence == StringPersistent)
259 st->strings[n].persistent_refcount += refcount;
260 else
261 st->strings[n].nonpersistent_refcount += refcount;
262 return n;
263 }
264 n = st_find_free_entry( st );
265 if( n == -1 )
266 return -1;
267 }
268
269 if( n < 1 )
270 {
271 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
272 return -1;
273 }
274
275 /* allocate a new string */
276 if( len < 0 )
277 len = strlen(data);
278 sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
279 str = msi_alloc( (sz+1)*sizeof(WCHAR) );
280 if( !str )
281 return -1;
282 MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
283 str[sz] = 0;
284
285 set_st_entry( st, n, str, refcount, persistence );
286
287 return n;
288 }
289
290 int msi_addstringW( string_table *st, const WCHAR *data, int len, USHORT refcount, enum StringPersistence persistence )
291 {
292 UINT n;
293 LPWSTR str;
294
295 if( !data )
296 return 0;
297 if( !data[0] )
298 return 0;
299
300 if( msi_string2idW( st, data, &n ) == ERROR_SUCCESS )
301 {
302 if (persistence == StringPersistent)
303 st->strings[n].persistent_refcount += refcount;
304 else
305 st->strings[n].nonpersistent_refcount += refcount;
306 return n;
307 }
308
309 n = st_find_free_entry( st );
310 if( n == -1 )
311 return -1;
312
313 /* allocate a new string */
314 if(len<0)
315 len = strlenW(data);
316 TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
317
318 str = msi_alloc( (len+1)*sizeof(WCHAR) );
319 if( !str )
320 return -1;
321 memcpy( str, data, len*sizeof(WCHAR) );
322 str[len] = 0;
323
324 set_st_entry( st, n, str, refcount, persistence );
325
326 return n;
327 }
328
329 /* find the string identified by an id - return null if there's none */
330 const WCHAR *msi_string_lookup_id( const string_table *st, UINT id )
331 {
332 if( id == 0 )
333 return szEmpty;
334
335 if( id >= st->maxcount )
336 return NULL;
337
338 if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
339 return NULL;
340
341 return st->strings[id].str;
342 }
343
344 /*
345 * msi_id2stringA
346 *
347 * [in] st - pointer to the string table
348 * [in] id - id of the string to retrieve
349 * [out] buffer - destination of the UTF8 string
350 * [in/out] sz - number of bytes available in the buffer on input
351 * number of bytes used on output
352 *
353 * The size includes the terminating nul character. Short buffers
354 * will be filled, but not nul terminated.
355 */
356 static UINT msi_id2stringA( const string_table *st, UINT id, LPSTR buffer, UINT *sz )
357 {
358 UINT len;
359 const WCHAR *str;
360 int n;
361
362 TRACE("Finding string %d of %d\n", id, st->maxcount);
363
364 str = msi_string_lookup_id( st, id );
365 if( !str )
366 return ERROR_FUNCTION_FAILED;
367
368 len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
369
370 if( !buffer )
371 {
372 *sz = len;
373 return ERROR_SUCCESS;
374 }
375
376 if( len > *sz )
377 {
378 n = strlenW( str ) + 1;
379 while( n && (len > *sz) )
380 len = WideCharToMultiByte( st->codepage, 0,
381 str, --n, NULL, 0, NULL, NULL );
382 }
383 else
384 n = -1;
385
386 *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
387
388 return ERROR_SUCCESS;
389 }
390
391 /*
392 * msi_string2idW
393 *
394 * [in] st - pointer to the string table
395 * [in] str - string to find in the string table
396 * [out] id - id of the string, if found
397 */
398 UINT msi_string2idW( const string_table *st, LPCWSTR str, UINT *id )
399 {
400 int i, c, low = 0, high = st->sortcount - 1;
401
402 while (low <= high)
403 {
404 i = (low + high) / 2;
405 c = lstrcmpW( str, st->strings[st->sorted[i]].str );
406
407 if (c < 0)
408 high = i - 1;
409 else if (c > 0)
410 low = i + 1;
411 else
412 {
413 *id = st->sorted[i];
414 return ERROR_SUCCESS;
415 }
416 }
417
418 return ERROR_INVALID_PARAMETER;
419 }
420
421 static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
422 {
423 UINT i, len, holesize;
424
425 if( st->strings[0].str || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
426 ERR("oops. element 0 has a string\n");
427
428 *poolsize = 4;
429 *datasize = 0;
430 holesize = 0;
431 for( i=1; i<st->maxcount; i++ )
432 {
433 if( !st->strings[i].persistent_refcount )
434 {
435 TRACE("[%u] nonpersistent = %s\n", i, debugstr_w(st->strings[i].str));
436 (*poolsize) += 4;
437 }
438 else if( st->strings[i].str )
439 {
440 TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
441 len = WideCharToMultiByte( st->codepage, 0,
442 st->strings[i].str, -1, NULL, 0, NULL, NULL);
443 if( len )
444 len--;
445 (*datasize) += len;
446 if (len>0xffff)
447 (*poolsize) += 4;
448 (*poolsize) += holesize + 4;
449 holesize = 0;
450 }
451 else
452 holesize += 4;
453 }
454 TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
455 }
456
457 static const WCHAR szStringData[] = {
458 '_','S','t','r','i','n','g','D','a','t','a',0 };
459 static const WCHAR szStringPool[] = {
460 '_','S','t','r','i','n','g','P','o','o','l',0 };
461
462 HRESULT msi_init_string_table( IStorage *stg )
463 {
464 USHORT zero[2] = { 0, 0 };
465 UINT ret;
466
467 /* create the StringPool stream... add the zero string to it*/
468 ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
469 if (ret != ERROR_SUCCESS)
470 return E_FAIL;
471
472 /* create the StringData stream... make it zero length */
473 ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
474 if (ret != ERROR_SUCCESS)
475 return E_FAIL;
476
477 return S_OK;
478 }
479
480 string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
481 {
482 string_table *st = NULL;
483 CHAR *data = NULL;
484 USHORT *pool = NULL;
485 UINT r, datasize = 0, poolsize = 0, codepage;
486 DWORD i, count, offset, len, n, refs;
487
488 r = read_stream_data( stg, szStringPool, TRUE, (BYTE **)&pool, &poolsize );
489 if( r != ERROR_SUCCESS)
490 goto end;
491 r = read_stream_data( stg, szStringData, TRUE, (BYTE **)&data, &datasize );
492 if( r != ERROR_SUCCESS)
493 goto end;
494
495 if ( (poolsize > 4) && (pool[1] & 0x8000) )
496 *bytes_per_strref = LONG_STR_BYTES;
497 else
498 *bytes_per_strref = sizeof(USHORT);
499
500 count = poolsize/4;
501 if( poolsize > 4 )
502 codepage = pool[0] | ( (pool[1] & ~0x8000) << 16 );
503 else
504 codepage = CP_ACP;
505 st = init_stringtable( count, codepage );
506 if (!st)
507 goto end;
508
509 offset = 0;
510 n = 1;
511 i = 1;
512 while( i<count )
513 {
514 /* the string reference count is always the second word */
515 refs = pool[i*2+1];
516
517 /* empty entries have two zeros, still have a string id */
518 if (pool[i*2] == 0 && refs == 0)
519 {
520 i++;
521 n++;
522 continue;
523 }
524
525 /*
526 * If a string is over 64k, the previous string entry is made null
527 * and its the high word of the length is inserted in the null string's
528 * reference count field.
529 */
530 if( pool[i*2] == 0)
531 {
532 len = (pool[i*2+3] << 16) + pool[i*2+2];
533 i += 2;
534 }
535 else
536 {
537 len = pool[i*2];
538 i += 1;
539 }
540
541 if ( (offset + len) > datasize )
542 {
543 ERR("string table corrupt?\n");
544 break;
545 }
546
547 r = msi_addstring( st, n, data+offset, len, refs, StringPersistent );
548 if( r != n )
549 ERR("Failed to add string %d\n", n );
550 n++;
551 offset += len;
552 }
553
554 if ( datasize != offset )
555 ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
556
557 TRACE("Loaded %d strings\n", count);
558
559 end:
560 msi_free( pool );
561 msi_free( data );
562
563 return st;
564 }
565
566 UINT msi_save_string_table( const string_table *st, IStorage *storage, UINT *bytes_per_strref )
567 {
568 UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
569 UINT ret = ERROR_FUNCTION_FAILED;
570 CHAR *data = NULL;
571 USHORT *pool = NULL;
572
573 TRACE("\n");
574
575 /* construct the new table in memory first */
576 string_totalsize( st, &datasize, &poolsize );
577
578 TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
579
580 pool = msi_alloc( poolsize );
581 if( ! pool )
582 {
583 WARN("Failed to alloc pool %d bytes\n", poolsize );
584 goto err;
585 }
586 data = msi_alloc( datasize );
587 if( ! data )
588 {
589 WARN("Failed to alloc data %d bytes\n", poolsize );
590 goto err;
591 }
592
593 used = 0;
594 codepage = st->codepage;
595 pool[0] = codepage & 0xffff;
596 pool[1] = codepage >> 16;
597 if (st->maxcount > 0xffff)
598 {
599 pool[1] |= 0x8000;
600 *bytes_per_strref = LONG_STR_BYTES;
601 }
602 else
603 *bytes_per_strref = sizeof(USHORT);
604
605 n = 1;
606 for( i=1; i<st->maxcount; i++ )
607 {
608 if( !st->strings[i].persistent_refcount )
609 {
610 pool[ n*2 ] = 0;
611 pool[ n*2 + 1] = 0;
612 n++;
613 continue;
614 }
615
616 sz = datasize - used;
617 r = msi_id2stringA( st, i, data+used, &sz );
618 if( r != ERROR_SUCCESS )
619 {
620 ERR("failed to fetch string\n");
621 sz = 0;
622 }
623 if( sz && (sz < (datasize - used ) ) )
624 sz--;
625
626 if (sz)
627 pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
628 else
629 pool[ n*2 + 1 ] = 0;
630 if (sz < 0x10000)
631 {
632 pool[ n*2 ] = sz;
633 n++;
634 }
635 else
636 {
637 pool[ n*2 ] = 0;
638 pool[ n*2 + 2 ] = sz&0xffff;
639 pool[ n*2 + 3 ] = (sz>>16);
640 n += 2;
641 }
642 used += sz;
643 if( used > datasize )
644 {
645 ERR("oops overran %d >= %d\n", used, datasize);
646 goto err;
647 }
648 }
649
650 if( used != datasize )
651 {
652 ERR("oops used %d != datasize %d\n", used, datasize);
653 goto err;
654 }
655
656 /* write the streams */
657 r = write_stream_data( storage, szStringData, data, datasize, TRUE );
658 TRACE("Wrote StringData r=%08x\n", r);
659 if( r )
660 goto err;
661 r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
662 TRACE("Wrote StringPool r=%08x\n", r);
663 if( r )
664 goto err;
665
666 ret = ERROR_SUCCESS;
667
668 err:
669 msi_free( data );
670 msi_free( pool );
671
672 return ret;
673 }