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(msidb);
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 = msi_alloc( sizeof (string_table) );
80 st->strings = msi_alloc_zero( sizeof (msistring) * entries );
86 st->maxcount = entries;
88 st->codepage = codepage;
93 VOID msi_destroy_stringtable( string_table *st )
97 for( i=0; i<st->maxcount; i++ )
99 if( st->strings[i].refcount )
100 msi_free( st->strings[i].str );
102 msi_free( st->strings );
106 static int st_find_free_entry( string_table *st )
115 for( i = st->freeslot; i < st->maxcount; i++ )
116 if( !st->strings[i].refcount )
119 for( i = 1; i < st->maxcount; i++ )
120 if( !st->strings[i].refcount )
123 /* dynamically resize */
124 sz = st->maxcount + 1 + st->maxcount/2;
125 p = msi_realloc_zero( st->strings, sz*sizeof(msistring) );
129 st->freeslot = st->maxcount;
131 if( st->strings[st->freeslot].refcount )
132 ERR("oops. expected freeslot to be free...\n");
136 static void st_mark_entry_used( string_table *st, UINT n )
138 if( n >= st->maxcount )
140 st->freeslot = n + 1;
143 int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, UINT refcount )
153 if( st->strings[n].refcount )
158 if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
160 st->strings[n].refcount++;
163 n = st_find_free_entry( st );
170 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
174 /* allocate a new string */
177 sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
178 st->strings[n].str = msi_alloc( (sz+1)*sizeof(WCHAR) );
179 if( !st->strings[n].str )
181 MultiByteToWideChar( st->codepage, 0, data, len, st->strings[n].str, sz );
182 st->strings[n].str[sz] = 0;
183 st->strings[n].refcount = 1;
184 st->strings[n].hash = msistring_makehash( st->strings[n].str );
186 st_mark_entry_used( st, n );
191 int msi_addstringW( string_table *st, UINT n, const WCHAR *data, int len, UINT refcount )
193 /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
201 if( st->strings[n].refcount )
206 if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
208 st->strings[n].refcount++;
211 n = st_find_free_entry( st );
218 ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
222 /* allocate a new string */
225 TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
227 st->strings[n].str = msi_alloc( (len+1)*sizeof(WCHAR) );
228 if( !st->strings[n].str )
230 TRACE("%d\n",__LINE__);
231 memcpy( st->strings[n].str, data, len*sizeof(WCHAR) );
232 st->strings[n].str[len] = 0;
233 st->strings[n].refcount = 1;
234 st->strings[n].hash = msistring_makehash( st->strings[n].str );
236 st_mark_entry_used( st, n );
241 /* find the string identified by an id - return null if there's none */
242 const WCHAR *msi_string_lookup_id( string_table *st, UINT id )
244 static const WCHAR zero[] = { 0 };
248 if( id >= st->maxcount )
251 if( id && !st->strings[id].refcount )
254 return st->strings[id].str;
260 * [in] st - pointer to the string table
261 * [in] id - id of the string to retrieve
262 * [out] buffer - destination of the string
263 * [in/out] sz - number of bytes available in the buffer on input
264 * number of bytes used on output
266 * The size includes the terminating nul character. Short buffers
267 * will be filled, but not nul terminated.
269 UINT msi_id2stringW( string_table *st, UINT id, LPWSTR buffer, UINT *sz )
274 TRACE("Finding string %d of %d\n", id, st->maxcount);
276 str = msi_string_lookup_id( st, id );
278 return ERROR_FUNCTION_FAILED;
280 len = strlenW( str ) + 1;
285 return ERROR_SUCCESS;
290 memcpy( buffer, str, (*sz)*sizeof(WCHAR) );
293 return ERROR_SUCCESS;
299 * [in] st - pointer to the string table
300 * [in] id - id of the string to retrieve
301 * [out] buffer - destination of the UTF8 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_id2stringA( string_table *st, UINT id, LPSTR 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 = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
325 return ERROR_SUCCESS;
330 n = strlenW( str ) + 1;
331 while( n && (len > *sz) )
332 len = WideCharToMultiByte( st->codepage, 0,
333 str, --n, NULL, 0, NULL, NULL );
338 *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
340 return ERROR_SUCCESS;
346 * [in] st - pointer to the string table
347 * [in] str - string to find in the string table
348 * [out] id - id of the string, if found
350 UINT msi_string2idW( string_table *st, LPCWSTR str, UINT *id )
353 UINT i, r = ERROR_INVALID_PARAMETER;
355 hash = msistring_makehash( str );
356 for( i=0; i<st->maxcount; i++ )
358 if ( (str == NULL && st->strings[i].str == NULL) ||
359 ( ( st->strings[i].hash == hash ) &&
360 !strcmpW( st->strings[i].str, str ) ))
371 UINT msi_string2idA( string_table *st, LPCSTR buffer, UINT *id )
374 UINT r = ERROR_INVALID_PARAMETER;
377 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
382 return ERROR_SUCCESS;
385 sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
388 str = msi_alloc( sz*sizeof(WCHAR) );
390 return ERROR_NOT_ENOUGH_MEMORY;
391 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
393 r = msi_string2idW( st, str, id );
399 UINT msi_strcmp( string_table *st, UINT lval, UINT rval, UINT *res )
401 const WCHAR *l_str, *r_str;
403 l_str = msi_string_lookup_id( st, lval );
405 return ERROR_INVALID_PARAMETER;
407 r_str = msi_string_lookup_id( st, rval );
409 return ERROR_INVALID_PARAMETER;
411 /* does this do the right thing for all UTF-8 strings? */
412 *res = strcmpW( l_str, r_str );
414 return ERROR_SUCCESS;
417 UINT msi_string_count( string_table *st )
422 UINT msi_id_refcount( string_table *st, UINT i )
424 if( i >= st->maxcount )
426 return st->strings[i].refcount;
429 UINT msi_string_totalsize( string_table *st, UINT *total )
431 UINT size = 0, i, len;
433 if( st->strings[0].str || st->strings[0].refcount )
434 ERR("oops. element 0 has a string\n");
436 for( i=1; i<st->maxcount; i++ )
438 if( st->strings[i].str )
440 TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
441 len = WideCharToMultiByte( st->codepage, 0,
442 st->strings[i].str, -1, NULL, 0, NULL, NULL);
449 TRACE("%u/%u strings %u bytes codepage %x\n", *total, st->maxcount, size, st->codepage );
453 UINT msi_string_get_codepage( string_table *st )