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
44 #define LONG_STR_BYTES 3
46 typedef struct _msistring
49 UINT persistent_refcount;
50 UINT nonpersistent_refcount;
56 UINT maxcount; /* the number of strings */
60 msistring *strings; /* an array of strings (in the tree) */
63 static UINT msistring_makehash( const WCHAR *str )
74 hash = (hash<<5) | (hash>>27);
76 return hash % HASH_SIZE;
79 static string_table *init_stringtable( int entries, UINT codepage )
84 if (codepage != CP_ACP && !IsValidCodePage(codepage))
86 ERR("invalid codepage %d\n", codepage);
90 st = msi_alloc( sizeof (string_table) );
95 st->strings = msi_alloc_zero( sizeof (msistring) * entries );
101 st->maxcount = entries;
103 st->codepage = codepage;
105 for( i=0; i<HASH_SIZE; i++ )
111 VOID msi_destroy_stringtable( string_table *st )
115 for( i=0; i<st->maxcount; i++ )
117 if( st->strings[i].persistent_refcount ||
118 st->strings[i].nonpersistent_refcount )
119 msi_free( st->strings[i].str );
121 msi_free( st->strings );
125 static int st_find_free_entry( string_table *st )
134 for( i = st->freeslot; i < st->maxcount; i++ )
135 if( !st->strings[i].persistent_refcount &&
136 !st->strings[i].nonpersistent_refcount )
139 for( i = 1; i < st->maxcount; i++ )
140 if( !st->strings[i].persistent_refcount &&
141 !st->strings[i].nonpersistent_refcount )
144 /* dynamically resize */
145 sz = st->maxcount + 1 + st->maxcount/2;
146 p = msi_realloc_zero( st->strings, sz*sizeof(msistring) );
150 st->freeslot = st->maxcount;
152 if( st->strings[st->freeslot].persistent_refcount ||
153 st->strings[st->freeslot].nonpersistent_refcount )
154 ERR("oops. expected freeslot to be free...\n");
158 static void set_st_entry( string_table *st, UINT n, LPWSTR str, UINT refcount, enum StringPersistence persistence )
160 UINT hash = msistring_makehash( str );
162 if (persistence == StringPersistent)
164 st->strings[n].persistent_refcount = refcount;
165 st->strings[n].nonpersistent_refcount = 0;
169 st->strings[n].persistent_refcount = 0;
170 st->strings[n].nonpersistent_refcount = refcount;
173 st->strings[n].str = str;
175 st->strings[n].hash_next = st->hash[hash];
178 if( n < st->maxcount )
179 st->freeslot = n + 1;
182 static int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, UINT refcount, enum StringPersistence persistence )
193 if( st->strings[n].persistent_refcount ||
194 st->strings[n].nonpersistent_refcount )
199 if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
201 if (persistence == StringPersistent)
202 st->strings[n].persistent_refcount += refcount;
204 st->strings[n].nonpersistent_refcount += refcount;
207 n = st_find_free_entry( st );
214 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
218 /* allocate a new string */
221 sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
222 str = msi_alloc( (sz+1)*sizeof(WCHAR) );
225 MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
228 set_st_entry( st, n, str, refcount, persistence );
233 int msi_addstringW( string_table *st, UINT n, const WCHAR *data, int len, UINT refcount, enum StringPersistence persistence )
237 /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
245 if( st->strings[n].persistent_refcount ||
246 st->strings[n].nonpersistent_refcount )
251 if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
253 if (persistence == StringPersistent)
254 st->strings[n].persistent_refcount += refcount;
256 st->strings[n].nonpersistent_refcount += refcount;
259 n = st_find_free_entry( st );
266 ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
270 /* allocate a new string */
273 TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
275 str = msi_alloc( (len+1)*sizeof(WCHAR) );
278 memcpy( str, data, len*sizeof(WCHAR) );
281 set_st_entry( st, n, str, refcount, persistence );
286 /* find the string identified by an id - return null if there's none */
287 const WCHAR *msi_string_lookup_id( const string_table *st, UINT id )
289 static const WCHAR zero[] = { 0 };
293 if( id >= st->maxcount )
296 if( id && !st->strings[id].persistent_refcount && !st->strings[id].nonpersistent_refcount)
299 return st->strings[id].str;
305 * [in] st - pointer to the string table
306 * [in] id - id of the string to retrieve
307 * [out] buffer - destination of the string
308 * [in/out] sz - number of bytes available in the buffer on input
309 * number of bytes used on output
311 * The size includes the terminating nul character. Short buffers
312 * will be filled, but not nul terminated.
314 UINT msi_id2stringW( const string_table *st, UINT id, LPWSTR buffer, UINT *sz )
319 TRACE("Finding string %d of %d\n", id, st->maxcount);
321 str = msi_string_lookup_id( st, id );
323 return ERROR_FUNCTION_FAILED;
325 len = strlenW( str ) + 1;
330 return ERROR_SUCCESS;
335 memcpy( buffer, str, (*sz)*sizeof(WCHAR) );
338 return ERROR_SUCCESS;
344 * [in] st - pointer to the string table
345 * [in] id - id of the string to retrieve
346 * [out] buffer - destination of the UTF8 string
347 * [in/out] sz - number of bytes available in the buffer on input
348 * number of bytes used on output
350 * The size includes the terminating nul character. Short buffers
351 * will be filled, but not nul terminated.
353 UINT msi_id2stringA( const string_table *st, UINT id, LPSTR buffer, UINT *sz )
359 TRACE("Finding string %d of %d\n", id, st->maxcount);
361 str = msi_string_lookup_id( st, id );
363 return ERROR_FUNCTION_FAILED;
365 len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
370 return ERROR_SUCCESS;
375 n = strlenW( str ) + 1;
376 while( n && (len > *sz) )
377 len = WideCharToMultiByte( st->codepage, 0,
378 str, --n, NULL, 0, NULL, NULL );
383 *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
385 return ERROR_SUCCESS;
391 * [in] st - pointer to the string table
392 * [in] str - string to find in the string table
393 * [out] id - id of the string, if found
395 UINT msi_string2idW( const string_table *st, LPCWSTR str, UINT *id )
397 UINT n, hash = msistring_makehash( str );
398 msistring *se = st->strings;
400 for (n = st->hash[hash]; n != -1; n = st->strings[n].hash_next )
402 if ((str == se[n].str) || !lstrcmpW(str, se[n].str))
405 return ERROR_SUCCESS;
409 return ERROR_INVALID_PARAMETER;
412 UINT msi_string2idA( const string_table *st, LPCSTR buffer, UINT *id )
415 UINT r = ERROR_INVALID_PARAMETER;
418 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
423 return ERROR_SUCCESS;
426 sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
429 str = msi_alloc( sz*sizeof(WCHAR) );
431 return ERROR_NOT_ENOUGH_MEMORY;
432 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
434 r = msi_string2idW( st, str, id );
440 UINT msi_strcmp( const string_table *st, UINT lval, UINT rval, UINT *res )
442 const WCHAR *l_str, *r_str;
444 l_str = msi_string_lookup_id( st, lval );
446 return ERROR_INVALID_PARAMETER;
448 r_str = msi_string_lookup_id( st, rval );
450 return ERROR_INVALID_PARAMETER;
452 /* does this do the right thing for all UTF-8 strings? */
453 *res = strcmpW( l_str, r_str );
455 return ERROR_SUCCESS;
458 static void string_totalsize( const string_table *st, UINT *datasize, UINT *poolsize )
460 UINT i, len, holesize;
462 if( st->strings[0].str || st->strings[0].persistent_refcount || st->strings[0].nonpersistent_refcount)
463 ERR("oops. element 0 has a string\n");
468 for( i=1; i<st->maxcount; i++ )
470 if( !st->strings[i].persistent_refcount )
472 TRACE("[%u] nonpersistent = %s\n", i, debugstr_w(st->strings[i].str));
475 else if( st->strings[i].str )
477 TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
478 len = WideCharToMultiByte( st->codepage, 0,
479 st->strings[i].str, -1, NULL, 0, NULL, NULL);
485 (*poolsize) += holesize + 4;
491 TRACE("data %u pool %u codepage %x\n", *datasize, *poolsize, st->codepage );
494 static const WCHAR szStringData[] = {
495 '_','S','t','r','i','n','g','D','a','t','a',0 };
496 static const WCHAR szStringPool[] = {
497 '_','S','t','r','i','n','g','P','o','o','l',0 };
499 HRESULT msi_init_string_table( IStorage *stg )
501 USHORT zero[2] = { 0, 0 };
504 /* create the StringPool stream... add the zero string to it*/
505 ret = write_stream_data(stg, szStringPool, zero, sizeof zero, TRUE);
506 if (ret != ERROR_SUCCESS)
509 /* create the StringData stream... make it zero length */
510 ret = write_stream_data(stg, szStringData, NULL, 0, TRUE);
511 if (ret != ERROR_SUCCESS)
517 string_table *msi_load_string_table( IStorage *stg, UINT *bytes_per_strref )
519 string_table *st = NULL;
522 UINT r, datasize = 0, poolsize = 0, codepage;
523 DWORD i, count, offset, len, n, refs;
525 r = read_stream_data( stg, szStringPool, TRUE, (BYTE **)&pool, &poolsize );
526 if( r != ERROR_SUCCESS)
528 r = read_stream_data( stg, szStringData, TRUE, (BYTE **)&data, &datasize );
529 if( r != ERROR_SUCCESS)
532 if ( (poolsize > 4) && (pool[1] & 0x8000) )
533 *bytes_per_strref = LONG_STR_BYTES;
535 *bytes_per_strref = sizeof(USHORT);
539 codepage = pool[0] | ( (pool[1] & ~0x8000) << 16 );
542 st = init_stringtable( count, codepage );
551 /* the string reference count is always the second word */
554 /* empty entries have two zeros, still have a string id */
555 if (pool[i*2] == 0 && refs == 0)
563 * If a string is over 64k, the previous string entry is made null
564 * and its the high word of the length is inserted in the null string's
565 * reference count field.
569 len = (pool[i*2+3] << 16) + pool[i*2+2];
578 if ( (offset + len) > datasize )
580 ERR("string table corrupt?\n");
584 r = msi_addstring( st, n, data+offset, len, refs, StringPersistent );
586 ERR("Failed to add string %d\n", n );
591 if ( datasize != offset )
592 ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
594 TRACE("Loaded %d strings\n", count);
603 UINT msi_save_string_table( const string_table *st, IStorage *storage )
605 UINT i, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
606 UINT ret = ERROR_FUNCTION_FAILED;
612 /* construct the new table in memory first */
613 string_totalsize( st, &datasize, &poolsize );
615 TRACE("%u %u %u\n", st->maxcount, datasize, poolsize );
617 pool = msi_alloc( poolsize );
620 WARN("Failed to alloc pool %d bytes\n", poolsize );
623 data = msi_alloc( datasize );
626 WARN("Failed to alloc data %d bytes\n", poolsize );
631 codepage = st->codepage;
632 pool[0]=codepage&0xffff;
633 pool[1]=(codepage>>16);
635 for( i=1; i<st->maxcount; i++ )
637 if( !st->strings[i].persistent_refcount )
645 sz = datasize - used;
646 r = msi_id2stringA( st, i, data+used, &sz );
647 if( r != ERROR_SUCCESS )
649 ERR("failed to fetch string\n");
652 if( sz && (sz < (datasize - used ) ) )
656 pool[ n*2 + 1 ] = st->strings[i].persistent_refcount;
667 pool[ n*2 + 2 ] = sz&0xffff;
668 pool[ n*2 + 3 ] = (sz>>16);
672 if( used > datasize )
674 ERR("oops overran %d >= %d\n", used, datasize);
679 if( used != datasize )
681 ERR("oops used %d != datasize %d\n", used, datasize);
685 /* write the streams */
686 r = write_stream_data( storage, szStringData, data, datasize, TRUE );
687 TRACE("Wrote StringData r=%08x\n", r);
690 r = write_stream_data( storage, szStringPool, pool, poolsize, TRUE );
691 TRACE("Wrote StringPool r=%08x\n", r);