2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2004, Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msi);
40 typedef struct _msistring
49 UINT maxcount; /* the number of strings */
52 msistring *strings; /* an array of strings (in the tree) */
55 static UINT msistring_makehash( const WCHAR *str )
66 hash = (hash<<5) | (hash>>27);
71 string_table *msi_init_stringtable( int entries, UINT codepage )
75 st = HeapAlloc( GetProcessHeap(), 0, sizeof (string_table) );
78 st->strings = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
79 sizeof (msistring) * entries );
82 HeapFree( GetProcessHeap(), 0, st );
87 st->maxcount = entries;
89 st->codepage = codepage;
94 VOID msi_destroy_stringtable( string_table *st )
98 for( i=0; i<st->maxcount; i++ )
100 if( st->strings[i].refcount )
101 HeapFree( GetProcessHeap(), 0, st->strings[i].str );
103 HeapFree( GetProcessHeap(), 0, st->strings );
104 HeapFree( GetProcessHeap(), 0, st );
107 static int st_find_free_entry( string_table *st )
116 for( i = st->freeslot; i < st->maxcount; i++ )
117 if( !st->strings[i].refcount )
120 for( i = 1; i < st->maxcount; i++ )
121 if( !st->strings[i].refcount )
124 /* dynamically resize */
125 sz = st->maxcount + 1 + st->maxcount/2;
126 p = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
127 st->strings, sz*sizeof(msistring) );
131 st->freeslot = st->maxcount;
133 if( st->strings[st->freeslot].refcount )
134 ERR("oops. expected freeslot to be free...\n");
138 static void st_mark_entry_used( string_table *st, UINT n )
140 if( n >= st->maxcount )
142 st->freeslot = n + 1;
145 int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, UINT refcount )
155 if( st->strings[n].refcount )
160 if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
162 st->strings[n].refcount++;
165 n = st_find_free_entry( st );
172 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
176 /* allocate a new string */
179 sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
180 st->strings[n].str = HeapAlloc( GetProcessHeap(), 0, (sz+1)*sizeof(WCHAR) );
181 if( !st->strings[n].str )
183 MultiByteToWideChar( st->codepage, 0, data, len, st->strings[n].str, sz );
184 st->strings[n].str[sz] = 0;
185 st->strings[n].refcount = 1;
186 st->strings[n].hash = msistring_makehash( st->strings[n].str );
188 st_mark_entry_used( st, n );
193 int msi_addstringW( string_table *st, UINT n, const WCHAR *data, int len, UINT refcount )
195 /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
203 if( st->strings[n].refcount )
208 if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
210 st->strings[n].refcount++;
213 n = st_find_free_entry( st );
220 ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
224 /* allocate a new string */
227 TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
229 st->strings[n].str = HeapAlloc( GetProcessHeap(), 0, (len+1)*sizeof(WCHAR) );
230 if( !st->strings[n].str )
232 TRACE("%d\n",__LINE__);
233 memcpy( st->strings[n].str, data, len*sizeof(WCHAR) );
234 st->strings[n].str[len] = 0;
235 st->strings[n].refcount = 1;
236 st->strings[n].hash = msistring_makehash( st->strings[n].str );
238 st_mark_entry_used( st, n );
243 /* find the string identified by an id - return null if there's none */
244 const WCHAR *msi_string_lookup_id( string_table *st, UINT id )
246 static const WCHAR zero[] = { 0 };
250 if( id >= st->maxcount )
253 if( id && !st->strings[id].refcount )
256 return st->strings[id].str;
262 * [in] st - pointer to the string table
263 * [in] id - id of the string to retrieve
264 * [out] buffer - destination of the string
265 * [in/out] sz - number of bytes available in the buffer on input
266 * number of bytes used on output
268 * The size includes the terminating nul character. Short buffers
269 * will be filled, but not nul terminated.
271 UINT msi_id2stringW( string_table *st, UINT id, LPWSTR buffer, UINT *sz )
276 TRACE("Finding string %d of %d\n", id, st->maxcount);
278 str = msi_string_lookup_id( st, id );
280 return ERROR_FUNCTION_FAILED;
282 len = strlenW( str ) + 1;
287 return ERROR_SUCCESS;
292 memcpy( buffer, str, (*sz)*sizeof(WCHAR) );
295 return ERROR_SUCCESS;
301 * [in] st - pointer to the string table
302 * [in] id - id of the string to retrieve
303 * [out] buffer - destination of the UTF8 string
304 * [in/out] sz - number of bytes available in the buffer on input
305 * number of bytes used on output
307 * The size includes the terminating nul character. Short buffers
308 * will be filled, but not nul terminated.
310 UINT msi_id2stringA( string_table *st, UINT id, LPSTR buffer, UINT *sz )
316 TRACE("Finding string %d of %d\n", id, st->maxcount);
318 str = msi_string_lookup_id( st, id );
320 return ERROR_FUNCTION_FAILED;
322 len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
327 return ERROR_SUCCESS;
332 n = strlenW( str ) + 1;
333 while( n && (len > *sz) )
334 len = WideCharToMultiByte( st->codepage, 0,
335 str, --n, NULL, 0, NULL, NULL );
340 *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
342 return ERROR_SUCCESS;
348 * [in] st - pointer to the string table
349 * [in] str - string to find in the string table
350 * [out] id - id of the string, if found
352 UINT msi_string2idW( string_table *st, LPCWSTR str, UINT *id )
355 UINT i, r = ERROR_INVALID_PARAMETER;
357 hash = msistring_makehash( str );
358 for( i=0; i<st->maxcount; i++ )
360 if ( (str == NULL && st->strings[i].str == NULL) ||
361 ( ( st->strings[i].hash == hash ) &&
362 !strcmpW( st->strings[i].str, str ) ))
373 UINT msi_string2idA( string_table *st, LPCSTR buffer, UINT *id )
376 UINT r = ERROR_INVALID_PARAMETER;
379 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
384 return ERROR_SUCCESS;
387 sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
390 str = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
392 return ERROR_NOT_ENOUGH_MEMORY;
393 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
395 r = msi_string2idW( st, str, id );
396 HeapFree( GetProcessHeap(), 0, str );
401 UINT msi_strcmp( string_table *st, UINT lval, UINT rval, UINT *res )
403 const WCHAR *l_str, *r_str;
405 l_str = msi_string_lookup_id( st, lval );
407 return ERROR_INVALID_PARAMETER;
409 r_str = msi_string_lookup_id( st, rval );
411 return ERROR_INVALID_PARAMETER;
413 /* does this do the right thing for all UTF-8 strings? */
414 *res = strcmpW( l_str, r_str );
416 return ERROR_SUCCESS;
419 UINT msi_string_count( string_table *st )
424 UINT msi_id_refcount( string_table *st, UINT i )
426 if( i >= st->maxcount )
428 return st->strings[i].refcount;
431 UINT msi_string_totalsize( string_table *st, UINT *total )
433 UINT size = 0, i, len;
435 if( st->strings[0].str || st->strings[0].refcount )
436 ERR("oops. element 0 has a string\n");
438 for( i=1; i<st->maxcount; i++ )
440 if( st->strings[i].str )
442 TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
443 len = WideCharToMultiByte( st->codepage, 0,
444 st->strings[i].str, -1, NULL, 0, NULL, NULL);
451 TRACE("%u/%u strings %u bytes codepage %x\n", *total, st->maxcount, size, st->codepage );
455 UINT msi_string_get_codepage( string_table *st )