2 * String Table Functions
4 * Copyright 2002-2004, Mike McCormack for CodeWeavers
5 * Copyright 2007 Robert Shearman for CodeWeavers
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.
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.
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
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
30 #include "wine/debug.h"
31 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
43 #define HASH_SIZE 0x101
45 typedef struct _msistring
48 UINT persistent_refcount;
49 UINT nonpersistent_refcount;
55 UINT maxcount; /* the number of strings */
59 msistring *strings; /* an array of strings (in the tree) */
62 static UINT msistring_makehash( const WCHAR *str )
73 hash = (hash<<5) | (hash>>27);
75 return hash % HASH_SIZE;
78 static string_table *init_stringtable( int entries, UINT codepage )
83 st = msi_alloc( sizeof (string_table) );
88 st->strings = msi_alloc_zero( sizeof (msistring) * entries );
94 st->maxcount = entries;
96 st->codepage = codepage;
98 for( i=0; i<HASH_SIZE; i++ )
104 VOID msi_destroy_stringtable( string_table *st )
108 for( i=0; i<st->maxcount; i++ )
110 if( st->strings[i].persistent_refcount ||
111 st->strings[i].nonpersistent_refcount )
112 msi_free( st->strings[i].str );
114 msi_free( st->strings );
118 static int st_find_free_entry( string_table *st )
127 for( i = st->freeslot; i < st->maxcount; i++ )
128 if( !st->strings[i].persistent_refcount &&
129 !st->strings[i].nonpersistent_refcount )
132 for( i = 1; i < st->maxcount; i++ )
133 if( !st->strings[i].persistent_refcount &&
134 !st->strings[i].nonpersistent_refcount )
137 /* dynamically resize */
138 sz = st->maxcount + 1 + st->maxcount/2;
139 p = msi_realloc_zero( st->strings, sz*sizeof(msistring) );
143 st->freeslot = st->maxcount;
145 if( st->strings[st->freeslot].persistent_refcount ||
146 st->strings[st->freeslot].nonpersistent_refcount )
147 ERR("oops. expected freeslot to be free...\n");
151 static void set_st_entry( string_table *st, UINT n, LPWSTR str, UINT refcount, enum StringPersistence persistence )
153 UINT hash = msistring_makehash( str );
155 if (persistence == StringPersistent)
157 st->strings[n].persistent_refcount = refcount;
158 st->strings[n].nonpersistent_refcount = 0;
162 st->strings[n].persistent_refcount = 0;
163 st->strings[n].nonpersistent_refcount = refcount;
166 st->strings[n].str = str;
168 st->strings[n].hash_next = st->hash[hash];
171 if( n < st->maxcount )
172 st->freeslot = n + 1;
175 static int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, UINT refcount, enum StringPersistence persistence )
186 if( st->strings[n].persistent_refcount ||
187 st->strings[n].nonpersistent_refcount )
192 if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
194 if (persistence == StringPersistent)
195 st->strings[n].persistent_refcount += refcount;
197 st->strings[n].nonpersistent_refcount += refcount;
200 n = st_find_free_entry( st );
207 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
211 /* allocate a new string */
214 sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
215 str = msi_alloc( (sz+1)*sizeof(WCHAR) );
218 MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
221 set_st_entry( st, n, str, refcount, persistence );
226 int msi_addstringW( string_table *st, UINT n, const WCHAR *data, int len, UINT refcount, enum StringPersistence persistence )
230 /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
238 if( st->strings[n].persistent_refcount ||
239 st->strings[n].nonpersistent_refcount )
244 if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
246 if (persistence == StringPersistent)
247 st->strings[n].persistent_refcount += refcount;
249 st->strings[n].nonpersistent_refcount += refcount;
252 n = st_find_free_entry( st );
259 ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
263 /* allocate a new string */
266 TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
268 str = msi_alloc( (len+1)*sizeof(WCHAR) );
271 TRACE("%d\n",__LINE__);
272 memcpy( str, data, len*sizeof(WCHAR) );
275 set_st_entry( st, n, str, refcount, persistence );
280 /* find the string identified by an id - return null if there's none */
281 const WCHAR *msi_string_lookup_id( string_table *st, UINT id )
283 static const WCHAR zero[] = { 0 };
287 if( id >= st->maxcount )
290 if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
293 return st->strings[id].str;
299 * [in] st - pointer to the string table
300 * [in] id - id of the string to retrieve
301 * [out] buffer - destination of the string
302 * [in/out] sz - number of bytes available in the buffer on input
303 * number of bytes used on output
305 * The size includes the terminating nul character. Short buffers
306 * will be filled, but not nul terminated.
308 UINT msi_id2stringW( string_table *st, UINT id, LPWSTR buffer, UINT *sz )
313 TRACE("Finding string %d of %d\n", id, st->maxcount);
315 str = msi_string_lookup_id( st, id );
317 return ERROR_FUNCTION_FAILED;
319 len = strlenW( str ) + 1;
324 return ERROR_SUCCESS;
329 memcpy( buffer, str, (*sz)*sizeof(WCHAR) );
332 return ERROR_SUCCESS;
338 * [in] st - pointer to the string table
339 * [in] id - id of the string to retrieve
340 * [out] buffer - destination of the UTF8 string
341 * [in/out] sz - number of bytes available in the buffer on input
342 * number of bytes used on output
344 * The size includes the terminating nul character. Short buffers
345 * will be filled, but not nul terminated.
347 UINT msi_id2stringA( string_table *st, UINT id, LPSTR buffer, UINT *sz )
353 TRACE("Finding string %d of %d\n", id, st->maxcount);
355 str = msi_string_lookup_id( st, id );
357 return ERROR_FUNCTION_FAILED;
359 len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
364 return ERROR_SUCCESS;
369 n = strlenW( str ) + 1;
370 while( n && (len > *sz) )
371 len = WideCharToMultiByte( st->codepage, 0,
372 str, --n, NULL, 0, NULL, NULL );
377 *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
379 return ERROR_SUCCESS;
385 * [in] st - pointer to the string table
386 * [in] str - string to find in the string table
387 * [out] id - id of the string, if found
389 UINT msi_string2idW( string_table *st, LPCWSTR str, UINT *id )
391 UINT n, hash = msistring_makehash( str );
392 msistring *se = st->strings;
394 for (n = st->hash[hash]; n != -1; n = st->strings[n].hash_next )
396 if ((str == se[n].str) || !lstrcmpW(str, se[n].str))
399 return ERROR_SUCCESS;
403 return ERROR_INVALID_PARAMETER;
406 UINT msi_string2idA( string_table *st, LPCSTR buffer, UINT *id )
409 UINT r = ERROR_INVALID_PARAMETER;
412 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
417 return ERROR_SUCCESS;
420 sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
423 str = msi_alloc( sz*sizeof(WCHAR) );
425 return ERROR_NOT_ENOUGH_MEMORY;
426 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
428 r = msi_string2idW( st, str, id );
434 UINT msi_strcmp( string_table *st, UINT lval, UINT rval, UINT *res )
436 const WCHAR *l_str, *r_str;
438 l_str = msi_string_lookup_id( st, lval );
440 return ERROR_INVALID_PARAMETER;
442 r_str = msi_string_lookup_id( st, rval );
444 return ERROR_INVALID_PARAMETER;
446 /* does this do the right thing for all UTF-8 strings? */
447 *res = strcmpW( l_str, r_str );
449 return ERROR_SUCCESS;
452 static void string_totalsize( string_table *st, UINT *datasize, UINT *poolsize )
454 UINT i, len, max, holesize;
456 if( st->strings[0].str || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
457 ERR("oops. element 0 has a string\n");
463 for( i=1; i<st->maxcount; i++ )
465 if( !st->strings[i].persistent_refcount )
467 if( st->strings[i].str )
469 TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
470 len = WideCharToMultiByte( st->codepage, 0,
471 st->strings[i].str, -1, NULL, 0, NULL, NULL);
478 (*poolsize) += holesize + 4;
484 TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
487 static const WCHAR szStringData[] = {
488 '_','S','t','r','i','n','g','D','a','t','a',0 };
489 static const WCHAR szStringPool[] = {
490 '_','S','t','r','i','n','g','P','o','o','l',0 };
492 HRESULT msi_init_string_table( IStorage *stg )
494 USHORT zero[2] = { 0, 0 };
497 /* create the StringPool stream... add the zero string to it*/
498 ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
499 if (ret != ERROR_SUCCESS)
502 /* create the StringData stream... make it zero length */
503 ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
504 if (ret != ERROR_SUCCESS)
510 string_table *msi_load_string_table( IStorage *stg )
512 string_table *st = NULL;
515 UINT r, datasize = 0, poolsize = 0, codepage;
516 DWORD i, count, offset, len, n, refs;
518 r = read_stream_data( stg, szStringPool, &pool, &poolsize );
519 if( r != ERROR_SUCCESS)
521 r = read_stream_data( stg, szStringData, (USHORT**)&data, &datasize );
522 if( r != ERROR_SUCCESS)
527 codepage = pool[0] | ( pool[1] << 16 );
530 st = init_stringtable( count, codepage );
537 /* the string reference count is always the second word */
540 /* empty entries have two zeros, still have a string id */
541 if (pool[i*2] == 0 && refs == 0)
549 * If a string is over 64k, the previous string entry is made null
550 * and its the high word of the length is inserted in the null string's
551 * reference count field.
555 len = (pool[i*2+3] << 16) + pool[i*2+2];
564 if ( (offset + len) > datasize )
566 ERR("string table corrupt?\n");
570 r = msi_addstring( st, n, data+offset, len, refs, StringPersistent );
572 ERR("Failed to add string %d\n", n );
577 if ( datasize != offset )
578 ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
580 TRACE("Loaded %d strings\n", count);
589 UINT msi_save_string_table( string_table *st, IStorage *storage )
591 UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
592 UINT ret = ERROR_FUNCTION_FAILED;
598 /* construct the new table in memory first */
599 string_totalsize( st, &datasize, &poolsize );
601 TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
603 pool = msi_alloc( poolsize );
606 WARN("Failed to alloc pool %d bytes\n", poolsize );
609 data = msi_alloc( datasize );
612 WARN("Failed to alloc data %d bytes\n", poolsize );
617 codepage = st->codepage;
618 pool[0]=codepage&0xffff;
619 pool[1]=(codepage>>16);
621 for( i=1; i<st->maxcount; i++ )
623 if( !st->strings[i].persistent_refcount )
625 sz = datasize - used;
626 r = msi_id2stringA( st, i, data+used, &sz );
627 if( r != ERROR_SUCCESS )
629 ERR("failed to fetch string\n");
632 if( sz && (sz < (datasize - used ) ) )
636 pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
647 pool[ n*2 + 2 ] = sz&0xffff;
648 pool[ n*2 + 3 ] = (sz>>16);
652 if( used > datasize )
654 ERR("oops overran %d >= %d\n", used, datasize);
659 if( used != datasize )
661 ERR("oops used %d != datasize %d\n", used, datasize);
665 /* write the streams */
666 r = write_stream_data( storage, szStringData, data, datasize, TRUE );
667 TRACE("Wrote StringData r=%08x\n", r);
670 r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
671 TRACE("Wrote StringPool r=%08x\n", r);