ntoskrnl.exe: Reimplement KeSetTimerEx() on top of thread pool.
[wine.git] / dlls / msi / string.c
blob5c6432ba1ed8bc6a29f53a4049aa82a550fc1108
1 /*
2 * String Table Functions
4 * Copyright 2002-2004, Mike McCormack for CodeWeavers
5 * Copyright 2007 Robert Shearman for CodeWeavers
6 * Copyright 2010 Hans Leidekker for CodeWeavers
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.
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.
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
23 #define COBJMACROS
25 #include <stdarg.h>
26 #include <assert.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "wine/debug.h"
32 #include "msi.h"
33 #include "msiquery.h"
34 #include "objbase.h"
35 #include "objidl.h"
36 #include "msipriv.h"
37 #include "winnls.h"
39 #include "query.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
43 struct msistring
45 USHORT persistent_refcount;
46 USHORT nonpersistent_refcount;
47 WCHAR *data;
48 int len;
51 struct string_table
53 UINT maxcount; /* the number of strings */
54 UINT freeslot;
55 UINT codepage;
56 UINT sortcount;
57 struct msistring *strings; /* an array of strings */
58 UINT *sorted; /* index */
61 static BOOL validate_codepage( UINT codepage )
63 if (codepage != CP_ACP && !IsValidCodePage( codepage ))
65 WARN("invalid codepage %u\n", codepage);
66 return FALSE;
68 return TRUE;
71 static string_table *init_stringtable( int entries, UINT codepage )
73 string_table *st;
75 if (!validate_codepage( codepage ))
76 return NULL;
78 st = msi_alloc( sizeof (string_table) );
79 if( !st )
80 return NULL;
81 if( entries < 1 )
82 entries = 1;
84 st->strings = msi_alloc_zero( sizeof(struct msistring) * entries );
85 if( !st->strings )
87 msi_free( st );
88 return NULL;
91 st->sorted = msi_alloc( sizeof (UINT) * entries );
92 if( !st->sorted )
94 msi_free( st->strings );
95 msi_free( st );
96 return NULL;
99 st->maxcount = entries;
100 st->freeslot = 1;
101 st->codepage = codepage;
102 st->sortcount = 0;
104 return st;
107 VOID msi_destroy_stringtable( string_table *st )
109 UINT i;
111 for( i=0; i<st->maxcount; i++ )
113 if( st->strings[i].persistent_refcount ||
114 st->strings[i].nonpersistent_refcount )
115 msi_free( st->strings[i].data );
117 msi_free( st->strings );
118 msi_free( st->sorted );
119 msi_free( st );
122 static int st_find_free_entry( string_table *st )
124 UINT i, sz, *s;
125 struct msistring *p;
127 TRACE("%p\n", st);
129 if( st->freeslot )
131 for( i = st->freeslot; i < st->maxcount; i++ )
132 if( !st->strings[i].persistent_refcount &&
133 !st->strings[i].nonpersistent_refcount )
134 return i;
136 for( i = 1; i < st->maxcount; i++ )
137 if( !st->strings[i].persistent_refcount &&
138 !st->strings[i].nonpersistent_refcount )
139 return i;
141 /* dynamically resize */
142 sz = st->maxcount + 1 + st->maxcount/2;
143 p = msi_realloc_zero( st->strings, sz * sizeof(struct msistring) );
144 if( !p )
145 return -1;
147 s = msi_realloc( st->sorted, sz*sizeof(UINT) );
148 if( !s )
150 msi_free( p );
151 return -1;
154 st->strings = p;
155 st->sorted = s;
157 st->freeslot = st->maxcount;
158 st->maxcount = sz;
159 if( st->strings[st->freeslot].persistent_refcount ||
160 st->strings[st->freeslot].nonpersistent_refcount )
161 ERR("oops. expected freeslot to be free...\n");
162 return st->freeslot;
165 static inline int cmp_string( const WCHAR *str1, int len1, const WCHAR *str2, int len2 )
167 if (len1 < len2) return -1;
168 else if (len1 > len2) return 1;
169 while (len1)
171 if (*str1 == *str2) { str1++; str2++; }
172 else return *str1 - *str2;
173 len1--;
175 return 0;
178 static int find_insert_index( const string_table *st, UINT string_id )
180 int i, c, low = 0, high = st->sortcount - 1;
182 while (low <= high)
184 i = (low + high) / 2;
185 c = cmp_string( st->strings[string_id].data, st->strings[string_id].len,
186 st->strings[st->sorted[i]].data, st->strings[st->sorted[i]].len );
187 if (c < 0)
188 high = i - 1;
189 else if (c > 0)
190 low = i + 1;
191 else
192 return -1; /* already exists */
194 return high + 1;
197 static void insert_string_sorted( string_table *st, UINT string_id )
199 int i;
201 i = find_insert_index( st, string_id );
202 if (i == -1)
203 return;
205 memmove( &st->sorted[i] + 1, &st->sorted[i], (st->sortcount - i) * sizeof(UINT) );
206 st->sorted[i] = string_id;
207 st->sortcount++;
210 static void set_st_entry( string_table *st, UINT n, WCHAR *str, int len, USHORT refcount,
211 BOOL persistent )
213 if (persistent)
215 st->strings[n].persistent_refcount = refcount;
216 st->strings[n].nonpersistent_refcount = 0;
218 else
220 st->strings[n].persistent_refcount = 0;
221 st->strings[n].nonpersistent_refcount = refcount;
224 st->strings[n].data = str;
225 st->strings[n].len = len;
227 insert_string_sorted( st, n );
229 if( n < st->maxcount )
230 st->freeslot = n + 1;
233 static UINT string2id( const string_table *st, const char *buffer, UINT *id )
235 int sz;
236 UINT r = ERROR_INVALID_PARAMETER;
237 LPWSTR str;
239 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
241 if( buffer[0] == 0 )
243 *id = 0;
244 return ERROR_SUCCESS;
247 if (!(sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 )))
248 return r;
249 str = msi_alloc( sz*sizeof(WCHAR) );
250 if( !str )
251 return ERROR_NOT_ENOUGH_MEMORY;
252 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
254 r = msi_string2id( st, str, sz - 1, id );
255 msi_free( str );
256 return r;
259 static int add_string( string_table *st, UINT n, const char *data, UINT len, USHORT refcount, BOOL persistent )
261 LPWSTR str;
262 int sz;
264 if( !data || !len )
265 return 0;
266 if( n > 0 )
268 if( st->strings[n].persistent_refcount ||
269 st->strings[n].nonpersistent_refcount )
270 return -1;
272 else
274 if (string2id( st, data, &n ) == ERROR_SUCCESS)
276 if (persistent)
277 st->strings[n].persistent_refcount += refcount;
278 else
279 st->strings[n].nonpersistent_refcount += refcount;
280 return n;
282 n = st_find_free_entry( st );
283 if( n == -1 )
284 return -1;
287 if( n < 1 )
289 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
290 return -1;
293 /* allocate a new string */
294 sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
295 str = msi_alloc( (sz+1)*sizeof(WCHAR) );
296 if( !str )
297 return -1;
298 MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
299 str[sz] = 0;
301 set_st_entry( st, n, str, sz, refcount, persistent );
302 return n;
305 int msi_add_string( string_table *st, const WCHAR *data, int len, BOOL persistent )
307 UINT n;
308 LPWSTR str;
310 if( !data )
311 return 0;
313 if (len < 0) len = lstrlenW( data );
315 if( !data[0] && !len )
316 return 0;
318 if (msi_string2id( st, data, len, &n) == ERROR_SUCCESS )
320 if (persistent)
321 st->strings[n].persistent_refcount++;
322 else
323 st->strings[n].nonpersistent_refcount++;
324 return n;
327 n = st_find_free_entry( st );
328 if( n == -1 )
329 return -1;
331 /* allocate a new string */
332 TRACE( "%s, n = %d len = %d\n", debugstr_wn(data, len), n, len );
334 str = msi_alloc( (len+1)*sizeof(WCHAR) );
335 if( !str )
336 return -1;
337 memcpy( str, data, len*sizeof(WCHAR) );
338 str[len] = 0;
340 set_st_entry( st, n, str, len, 1, persistent );
341 return n;
344 /* find the string identified by an id - return null if there's none */
345 const WCHAR *msi_string_lookup( const string_table *st, UINT id, int *len )
347 if( id == 0 )
349 if (len) *len = 0;
350 return szEmpty;
352 if( id >= st->maxcount )
353 return NULL;
355 if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
356 return NULL;
358 if (len) *len = st->strings[id].len;
360 return st->strings[id].data;
364 * id2string
366 * [in] st - pointer to the string table
367 * [in] id - id of the string to retrieve
368 * [out] buffer - destination of the UTF8 string
369 * [in/out] sz - number of bytes available in the buffer on input
370 * number of bytes used on output
372 * Returned string is not nul terminated.
374 static UINT id2string( const string_table *st, UINT id, char *buffer, UINT *sz )
376 int len, lenW;
377 const WCHAR *str;
379 TRACE("Finding string %d of %d\n", id, st->maxcount);
381 str = msi_string_lookup( st, id, &lenW );
382 if( !str )
383 return ERROR_FUNCTION_FAILED;
385 len = WideCharToMultiByte( st->codepage, 0, str, lenW, NULL, 0, NULL, NULL );
386 if( *sz < len )
388 *sz = len;
389 return ERROR_MORE_DATA;
391 *sz = WideCharToMultiByte( st->codepage, 0, str, lenW, buffer, *sz, NULL, NULL );
392 return ERROR_SUCCESS;
396 * msi_string2id
398 * [in] st - pointer to the string table
399 * [in] str - string to find in the string table
400 * [out] id - id of the string, if found
402 UINT msi_string2id( const string_table *st, const WCHAR *str, int len, UINT *id )
404 int i, c, low = 0, high = st->sortcount - 1;
406 if (len < 0) len = lstrlenW( str );
408 while (low <= high)
410 i = (low + high) / 2;
411 c = cmp_string( str, len, st->strings[st->sorted[i]].data, st->strings[st->sorted[i]].len );
413 if (c < 0)
414 high = i - 1;
415 else if (c > 0)
416 low = i + 1;
417 else
419 *id = st->sorted[i];
420 return ERROR_SUCCESS;
423 return ERROR_INVALID_PARAMETER;
426 static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
428 UINT i, len, holesize;
430 if( st->strings[0].data || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
431 ERR("oops. element 0 has a string\n");
433 *poolsize = 4;
434 *datasize = 0;
435 holesize = 0;
436 for( i=1; i<st->maxcount; i++ )
438 if( !st->strings[i].persistent_refcount )
440 TRACE("[%u] nonpersistent = %s\n", i, debugstr_wn(st->strings[i].data, st->strings[i].len));
441 (*poolsize) += 4;
443 else if( st->strings[i].data )
445 TRACE("[%u] = %s\n", i, debugstr_wn(st->strings[i].data, st->strings[i].len));
446 len = WideCharToMultiByte( st->codepage, 0, st->strings[i].data, st->strings[i].len + 1,
447 NULL, 0, NULL, NULL);
448 if( len )
449 len--;
450 (*datasize) += len;
451 if (len>0xffff)
452 (*poolsize) += 4;
453 (*poolsize) += holesize + 4;
454 holesize = 0;
456 else
457 holesize += 4;
459 TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
462 HRESULT msi_init_string_table( IStorage *stg )
464 USHORT zero[2] = { 0, 0 };
465 UINT ret;
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;
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;
477 return S_OK;
480 string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
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;
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;
495 if ( (poolsize > 4) && (pool[1] & 0x8000) )
496 *bytes_per_strref = LONG_STR_BYTES;
497 else
498 *bytes_per_strref = sizeof(USHORT);
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;
509 offset = 0;
510 n = 1;
511 i = 1;
512 while( i<count )
514 /* the string reference count is always the second word */
515 refs = pool[i*2+1];
517 /* empty entries have two zeros, still have a string id */
518 if (pool[i*2] == 0 && refs == 0)
520 i++;
521 n++;
522 continue;
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.
530 if( pool[i*2] == 0)
532 len = (pool[i*2+3] << 16) + pool[i*2+2];
533 i += 2;
535 else
537 len = pool[i*2];
538 i += 1;
541 if ( (offset + len) > datasize )
543 ERR("string table corrupt?\n");
544 break;
547 r = add_string( st, n, data+offset, len, refs, TRUE );
548 if( r != n )
549 ERR("Failed to add string %d\n", n );
550 n++;
551 offset += len;
554 if ( datasize != offset )
555 ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
557 TRACE("Loaded %d strings\n", count);
559 end:
560 msi_free( pool );
561 msi_free( data );
563 return st;
566 UINT msi_save_string_table( const string_table *st, IStorage *storage, UINT *bytes_per_strref )
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;
573 TRACE("\n");
575 /* construct the new table in memory first */
576 string_totalsize( st, &datasize, &poolsize );
578 TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
580 pool = msi_alloc( poolsize );
581 if( ! pool )
583 WARN("Failed to alloc pool %d bytes\n", poolsize );
584 goto err;
586 data = msi_alloc( datasize );
587 if( ! data )
589 WARN("Failed to alloc data %d bytes\n", datasize );
590 goto err;
593 used = 0;
594 codepage = st->codepage;
595 pool[0] = codepage & 0xffff;
596 pool[1] = codepage >> 16;
597 if (st->maxcount > 0xffff)
599 pool[1] |= 0x8000;
600 *bytes_per_strref = LONG_STR_BYTES;
602 else
603 *bytes_per_strref = sizeof(USHORT);
605 n = 1;
606 for( i=1; i<st->maxcount; i++ )
608 if( !st->strings[i].persistent_refcount )
610 pool[ n*2 ] = 0;
611 pool[ n*2 + 1] = 0;
612 n++;
613 continue;
616 sz = datasize - used;
617 r = id2string( st, i, data+used, &sz );
618 if( r != ERROR_SUCCESS )
620 ERR("failed to fetch string\n");
621 sz = 0;
624 if (sz)
625 pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
626 else
627 pool[ n*2 + 1 ] = 0;
628 if (sz < 0x10000)
630 pool[ n*2 ] = sz;
631 n++;
633 else
635 pool[ n*2 ] = 0;
636 pool[ n*2 + 2 ] = sz&0xffff;
637 pool[ n*2 + 3 ] = (sz>>16);
638 n += 2;
640 used += sz;
641 if( used > datasize )
643 ERR("oops overran %d >= %d\n", used, datasize);
644 goto err;
648 if( used != datasize )
650 ERR("oops used %d != datasize %d\n", used, datasize);
651 goto err;
654 /* write the streams */
655 r = write_stream_data( storage, szStringData, data, datasize, TRUE );
656 TRACE("Wrote StringData r=%08x\n", r);
657 if( r )
658 goto err;
659 r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
660 TRACE("Wrote StringPool r=%08x\n", r);
661 if( r )
662 goto err;
664 ret = ERROR_SUCCESS;
666 err:
667 msi_free( data );
668 msi_free( pool );
670 return ret;
673 UINT msi_get_string_table_codepage( const string_table *st )
675 return st->codepage;
678 UINT msi_set_string_table_codepage( string_table *st, UINT codepage )
680 if (validate_codepage( codepage ))
682 st->codepage = codepage;
683 return ERROR_SUCCESS;
685 return ERROR_FUNCTION_FAILED;