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 st
= msi_alloc( sizeof (string_table
) );
89 st
->strings
= msi_alloc_zero( sizeof (msistring
) * entries
);
95 st
->maxcount
= entries
;
97 st
->codepage
= codepage
;
99 for( i
=0; i
<HASH_SIZE
; i
++ )
105 VOID
msi_destroy_stringtable( string_table
*st
)
109 for( i
=0; i
<st
->maxcount
; i
++ )
111 if( st
->strings
[i
].persistent_refcount
||
112 st
->strings
[i
].nonpersistent_refcount
)
113 msi_free( st
->strings
[i
].str
);
115 msi_free( st
->strings
);
119 static int st_find_free_entry( string_table
*st
)
128 for( i
= st
->freeslot
; i
< st
->maxcount
; i
++ )
129 if( !st
->strings
[i
].persistent_refcount
&&
130 !st
->strings
[i
].nonpersistent_refcount
)
133 for( i
= 1; i
< st
->maxcount
; i
++ )
134 if( !st
->strings
[i
].persistent_refcount
&&
135 !st
->strings
[i
].nonpersistent_refcount
)
138 /* dynamically resize */
139 sz
= st
->maxcount
+ 1 + st
->maxcount
/2;
140 p
= msi_realloc_zero( st
->strings
, sz
*sizeof(msistring
) );
144 st
->freeslot
= st
->maxcount
;
146 if( st
->strings
[st
->freeslot
].persistent_refcount
||
147 st
->strings
[st
->freeslot
].nonpersistent_refcount
)
148 ERR("oops. expected freeslot to be free...\n");
152 static void set_st_entry( string_table
*st
, UINT n
, LPWSTR str
, UINT refcount
, enum StringPersistence persistence
)
154 UINT hash
= msistring_makehash( str
);
156 if (persistence
== StringPersistent
)
158 st
->strings
[n
].persistent_refcount
= refcount
;
159 st
->strings
[n
].nonpersistent_refcount
= 0;
163 st
->strings
[n
].persistent_refcount
= 0;
164 st
->strings
[n
].nonpersistent_refcount
= refcount
;
167 st
->strings
[n
].str
= str
;
169 st
->strings
[n
].hash_next
= st
->hash
[hash
];
172 if( n
< st
->maxcount
)
173 st
->freeslot
= n
+ 1;
176 static int msi_addstring( string_table
*st
, UINT n
, const CHAR
*data
, int len
, UINT refcount
, enum StringPersistence persistence
)
187 if( st
->strings
[n
].persistent_refcount
||
188 st
->strings
[n
].nonpersistent_refcount
)
193 if( ERROR_SUCCESS
== msi_string2idA( st
, data
, &n
) )
195 if (persistence
== StringPersistent
)
196 st
->strings
[n
].persistent_refcount
+= refcount
;
198 st
->strings
[n
].nonpersistent_refcount
+= refcount
;
201 n
= st_find_free_entry( st
);
208 ERR("invalid index adding %s (%d)\n", debugstr_a( data
), n
);
212 /* allocate a new string */
215 sz
= MultiByteToWideChar( st
->codepage
, 0, data
, len
, NULL
, 0 );
216 str
= msi_alloc( (sz
+1)*sizeof(WCHAR
) );
219 MultiByteToWideChar( st
->codepage
, 0, data
, len
, str
, sz
);
222 set_st_entry( st
, n
, str
, refcount
, persistence
);
227 int msi_addstringW( string_table
*st
, UINT n
, const WCHAR
*data
, int len
, UINT refcount
, enum StringPersistence persistence
)
231 /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
239 if( st
->strings
[n
].persistent_refcount
||
240 st
->strings
[n
].nonpersistent_refcount
)
245 if( ERROR_SUCCESS
== msi_string2idW( st
, data
, &n
) )
247 if (persistence
== StringPersistent
)
248 st
->strings
[n
].persistent_refcount
+= refcount
;
250 st
->strings
[n
].nonpersistent_refcount
+= refcount
;
253 n
= st_find_free_entry( st
);
260 ERR("invalid index adding %s (%d)\n", debugstr_w( data
), n
);
264 /* allocate a new string */
267 TRACE("%s, n = %d len = %d\n", debugstr_w(data
), n
, len
);
269 str
= msi_alloc( (len
+1)*sizeof(WCHAR
) );
272 TRACE("%d\n",__LINE__
);
273 memcpy( str
, data
, len
*sizeof(WCHAR
) );
276 set_st_entry( st
, n
, str
, refcount
, persistence
);
281 /* find the string identified by an id - return null if there's none */
282 const WCHAR
*msi_string_lookup_id( const string_table
*st
, UINT id
)
284 static const WCHAR zero
[] = { 0 };
288 if( id
>= st
->maxcount
)
291 if( id
&& !st
->strings
[id
].persistent_refcount
&& !st
->strings
[id
].nonpersistent_refcount
)
294 return st
->strings
[id
].str
;
300 * [in] st - pointer to the string table
301 * [in] id - id of the string to retrieve
302 * [out] buffer - destination of the string
303 * [in/out] sz - number of bytes available in the buffer on input
304 * number of bytes used on output
306 * The size includes the terminating nul character. Short buffers
307 * will be filled, but not nul terminated.
309 UINT
msi_id2stringW( const string_table
*st
, UINT id
, LPWSTR buffer
, UINT
*sz
)
314 TRACE("Finding string %d of %d\n", id
, st
->maxcount
);
316 str
= msi_string_lookup_id( st
, id
);
318 return ERROR_FUNCTION_FAILED
;
320 len
= strlenW( str
) + 1;
325 return ERROR_SUCCESS
;
330 memcpy( buffer
, str
, (*sz
)*sizeof(WCHAR
) );
333 return ERROR_SUCCESS
;
339 * [in] st - pointer to the string table
340 * [in] id - id of the string to retrieve
341 * [out] buffer - destination of the UTF8 string
342 * [in/out] sz - number of bytes available in the buffer on input
343 * number of bytes used on output
345 * The size includes the terminating nul character. Short buffers
346 * will be filled, but not nul terminated.
348 UINT
msi_id2stringA( const string_table
*st
, UINT id
, LPSTR buffer
, UINT
*sz
)
354 TRACE("Finding string %d of %d\n", id
, st
->maxcount
);
356 str
= msi_string_lookup_id( st
, id
);
358 return ERROR_FUNCTION_FAILED
;
360 len
= WideCharToMultiByte( st
->codepage
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
365 return ERROR_SUCCESS
;
370 n
= strlenW( str
) + 1;
371 while( n
&& (len
> *sz
) )
372 len
= WideCharToMultiByte( st
->codepage
, 0,
373 str
, --n
, NULL
, 0, NULL
, NULL
);
378 *sz
= WideCharToMultiByte( st
->codepage
, 0, str
, n
, buffer
, len
, NULL
, NULL
);
380 return ERROR_SUCCESS
;
386 * [in] st - pointer to the string table
387 * [in] str - string to find in the string table
388 * [out] id - id of the string, if found
390 UINT
msi_string2idW( const string_table
*st
, LPCWSTR str
, UINT
*id
)
392 UINT n
, hash
= msistring_makehash( str
);
393 msistring
*se
= st
->strings
;
395 for (n
= st
->hash
[hash
]; n
!= -1; n
= st
->strings
[n
].hash_next
)
397 if ((str
== se
[n
].str
) || !lstrcmpW(str
, se
[n
].str
))
400 return ERROR_SUCCESS
;
404 return ERROR_INVALID_PARAMETER
;
407 UINT
msi_string2idA( const string_table
*st
, LPCSTR buffer
, UINT
*id
)
410 UINT r
= ERROR_INVALID_PARAMETER
;
413 TRACE("Finding string %s in string table\n", debugstr_a(buffer
) );
418 return ERROR_SUCCESS
;
421 sz
= MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, NULL
, 0 );
424 str
= msi_alloc( sz
*sizeof(WCHAR
) );
426 return ERROR_NOT_ENOUGH_MEMORY
;
427 MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, str
, sz
);
429 r
= msi_string2idW( st
, str
, id
);
435 UINT
msi_strcmp( const string_table
*st
, UINT lval
, UINT rval
, UINT
*res
)
437 const WCHAR
*l_str
, *r_str
;
439 l_str
= msi_string_lookup_id( st
, lval
);
441 return ERROR_INVALID_PARAMETER
;
443 r_str
= msi_string_lookup_id( st
, rval
);
445 return ERROR_INVALID_PARAMETER
;
447 /* does this do the right thing for all UTF-8 strings? */
448 *res
= strcmpW( l_str
, r_str
);
450 return ERROR_SUCCESS
;
453 static void string_totalsize( const string_table
*st
, UINT
*datasize
, UINT
*poolsize
)
455 UINT i
, len
, max
, holesize
;
457 if( st
->strings
[0].str
|| st
->strings
[0].persistent_refcount
|| st
->strings
[0].nonpersistent_refcount
)
458 ERR("oops. element 0 has a string\n");
464 for( i
=1; i
<st
->maxcount
; i
++ )
466 if( !st
->strings
[i
].persistent_refcount
)
468 if( st
->strings
[i
].str
)
470 TRACE("[%u] = %s\n", i
, debugstr_w(st
->strings
[i
].str
));
471 len
= WideCharToMultiByte( st
->codepage
, 0,
472 st
->strings
[i
].str
, -1, NULL
, 0, NULL
, NULL
);
479 (*poolsize
) += holesize
+ 4;
485 TRACE("data %u pool %u codepage %x\n", *datasize
, *poolsize
, st
->codepage
);
488 static const WCHAR szStringData
[] = {
489 '_','S','t','r','i','n','g','D','a','t','a',0 };
490 static const WCHAR szStringPool
[] = {
491 '_','S','t','r','i','n','g','P','o','o','l',0 };
493 HRESULT
msi_init_string_table( IStorage
*stg
)
495 USHORT zero
[2] = { 0, 0 };
498 /* create the StringPool stream... add the zero string to it*/
499 ret
= write_stream_data(stg
, szStringPool
, zero
, sizeof zero
, TRUE
);
500 if (ret
!= ERROR_SUCCESS
)
503 /* create the StringData stream... make it zero length */
504 ret
= write_stream_data(stg
, szStringData
, NULL
, 0, TRUE
);
505 if (ret
!= ERROR_SUCCESS
)
511 string_table
*msi_load_string_table( IStorage
*stg
, UINT
*bytes_per_strref
)
513 string_table
*st
= NULL
;
516 UINT r
, datasize
= 0, poolsize
= 0, codepage
;
517 DWORD i
, count
, offset
, len
, n
, refs
;
519 static const USHORT large_str_sig
[] = { 0x0000, 0x8000 };
521 r
= read_stream_data( stg
, szStringPool
, &pool
, &poolsize
);
522 if( r
!= ERROR_SUCCESS
)
524 r
= read_stream_data( stg
, szStringData
, (USHORT
**)&data
, &datasize
);
525 if( r
!= ERROR_SUCCESS
)
528 if ( !memcmp(pool
, large_str_sig
, sizeof(large_str_sig
)) )
529 *bytes_per_strref
= LONG_STR_BYTES
;
531 *bytes_per_strref
= sizeof(USHORT
);
533 /* FIXME: don't know where the codepage is in large str tables */
535 if( poolsize
> 4 && *bytes_per_strref
!= LONG_STR_BYTES
)
536 codepage
= pool
[0] | ( pool
[1] << 16 );
539 st
= init_stringtable( count
, codepage
);
546 /* the string reference count is always the second word */
549 /* empty entries have two zeros, still have a string id */
550 if (pool
[i
*2] == 0 && refs
== 0)
558 * If a string is over 64k, the previous string entry is made null
559 * and its the high word of the length is inserted in the null string's
560 * reference count field.
564 len
= (pool
[i
*2+3] << 16) + pool
[i
*2+2];
573 if ( (offset
+ len
) > datasize
)
575 ERR("string table corrupt?\n");
579 r
= msi_addstring( st
, n
, data
+offset
, len
, refs
, StringPersistent
);
581 ERR("Failed to add string %d\n", n
);
586 if ( datasize
!= offset
)
587 ERR("string table load failed! (%08x != %08x), please report\n", datasize
, offset
);
589 TRACE("Loaded %d strings\n", count
);
598 UINT
msi_save_string_table( const string_table
*st
, IStorage
*storage
)
600 UINT i
, datasize
= 0, poolsize
= 0, sz
, used
, r
, codepage
, n
;
601 UINT ret
= ERROR_FUNCTION_FAILED
;
607 /* construct the new table in memory first */
608 string_totalsize( st
, &datasize
, &poolsize
);
610 TRACE("%u %u %u\n", st
->maxcount
, datasize
, poolsize
);
612 pool
= msi_alloc( poolsize
);
615 WARN("Failed to alloc pool %d bytes\n", poolsize
);
618 data
= msi_alloc( datasize
);
621 WARN("Failed to alloc data %d bytes\n", poolsize
);
626 codepage
= st
->codepage
;
627 pool
[0]=codepage
&0xffff;
628 pool
[1]=(codepage
>>16);
630 for( i
=1; i
<st
->maxcount
; i
++ )
632 if( !st
->strings
[i
].persistent_refcount
)
634 sz
= datasize
- used
;
635 r
= msi_id2stringA( st
, i
, data
+used
, &sz
);
636 if( r
!= ERROR_SUCCESS
)
638 ERR("failed to fetch string\n");
641 if( sz
&& (sz
< (datasize
- used
) ) )
645 pool
[ n
*2 + 1 ] = st
->strings
[i
].persistent_refcount
;
656 pool
[ n
*2 + 2 ] = sz
&0xffff;
657 pool
[ n
*2 + 3 ] = (sz
>>16);
661 if( used
> datasize
)
663 ERR("oops overran %d >= %d\n", used
, datasize
);
668 if( used
!= datasize
)
670 ERR("oops used %d != datasize %d\n", used
, datasize
);
674 /* write the streams */
675 r
= write_stream_data( storage
, szStringData
, data
, datasize
, TRUE
);
676 TRACE("Wrote StringData r=%08x\n", r
);
679 r
= write_stream_data( storage
, szStringPool
, pool
, poolsize
, TRUE
);
680 TRACE("Wrote StringPool r=%08x\n", r
);