user: Fix LB_GETTEXT unmapping for A<->W.
[wine/multimedia.git] / dlls / msi / string.c
bloba5ec138b987db062d40d9166902a933727022c2d
1 /*
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
21 #include <stdarg.h>
22 #include <assert.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winerror.h"
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
29 #include "msi.h"
30 #include "msiquery.h"
31 #include "objbase.h"
32 #include "objidl.h"
33 #include "msipriv.h"
34 #include "winnls.h"
36 #include "query.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
40 typedef struct _msistring
42 UINT hash;
43 UINT refcount;
44 LPWSTR str;
45 } msistring;
47 struct string_table
49 UINT maxcount; /* the number of strings */
50 UINT freeslot;
51 UINT codepage;
52 msistring *strings; /* an array of strings (in the tree) */
55 static UINT msistring_makehash( const WCHAR *str )
57 UINT hash = 0;
59 if (str==NULL)
60 return hash;
62 while( *str )
64 hash ^= *str++;
65 hash *= 53;
66 hash = (hash<<5) | (hash>>27);
68 return hash;
71 string_table *msi_init_stringtable( int entries, UINT codepage )
73 string_table *st;
75 st = msi_alloc( sizeof (string_table) );
76 if( !st )
77 return NULL;
78 if( entries < 1 )
79 entries = 1;
80 st->strings = msi_alloc_zero( sizeof (msistring) * entries );
81 if( !st )
83 msi_free( st );
84 return NULL;
86 st->maxcount = entries;
87 st->freeslot = 1;
88 st->codepage = codepage;
90 return st;
93 VOID msi_destroy_stringtable( string_table *st )
95 UINT i;
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 );
103 msi_free( st );
106 static int st_find_free_entry( string_table *st )
108 UINT i, sz;
109 msistring *p;
111 TRACE("%p\n", st);
113 if( st->freeslot )
115 for( i = st->freeslot; i < st->maxcount; i++ )
116 if( !st->strings[i].refcount )
117 return i;
119 for( i = 1; i < st->maxcount; i++ )
120 if( !st->strings[i].refcount )
121 return i;
123 /* dynamically resize */
124 sz = st->maxcount + 1 + st->maxcount/2;
125 p = msi_realloc_zero( st->strings, sz*sizeof(msistring) );
126 if( !p )
127 return -1;
128 st->strings = p;
129 st->freeslot = st->maxcount;
130 st->maxcount = sz;
131 if( st->strings[st->freeslot].refcount )
132 ERR("oops. expected freeslot to be free...\n");
133 return st->freeslot;
136 static void st_mark_entry_used( string_table *st, UINT n )
138 if( n >= st->maxcount )
139 return;
140 st->freeslot = n + 1;
143 int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, UINT refcount )
145 int sz;
147 if( !data )
148 return 0;
149 if( !data[0] )
150 return 0;
151 if( n > 0 )
153 if( st->strings[n].refcount )
154 return -1;
156 else
158 if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
160 st->strings[n].refcount++;
161 return n;
163 n = st_find_free_entry( st );
164 if( n < 0 )
165 return -1;
168 if( n < 1 )
170 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
171 return -1;
174 /* allocate a new string */
175 if( len < 0 )
176 len = strlen(data);
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 )
180 return -1;
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 );
188 return 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) ); */
195 if( !data )
196 return 0;
197 if( !data[0] )
198 return 0;
199 if( n > 0 )
201 if( st->strings[n].refcount )
202 return -1;
204 else
206 if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
208 st->strings[n].refcount++;
209 return n;
211 n = st_find_free_entry( st );
212 if( n < 0 )
213 return -1;
216 if( n < 1 )
218 ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
219 return -1;
222 /* allocate a new string */
223 if(len<0)
224 len = strlenW(data);
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 )
229 return -1;
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 );
238 return 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 };
245 if( id == 0 )
246 return zero;
248 if( id >= st->maxcount )
249 return NULL;
251 if( id && !st->strings[id].refcount )
252 return NULL;
254 return st->strings[id].str;
258 * msi_id2stringW
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 )
271 UINT len;
272 const WCHAR *str;
274 TRACE("Finding string %d of %d\n", id, st->maxcount);
276 str = msi_string_lookup_id( st, id );
277 if( !str )
278 return ERROR_FUNCTION_FAILED;
280 len = strlenW( str ) + 1;
282 if( !buffer )
284 *sz = len;
285 return ERROR_SUCCESS;
288 if( *sz < len )
289 *sz = len;
290 memcpy( buffer, str, (*sz)*sizeof(WCHAR) );
291 *sz = len;
293 return ERROR_SUCCESS;
297 * msi_id2stringA
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 )
310 UINT len;
311 const WCHAR *str;
312 int n;
314 TRACE("Finding string %d of %d\n", id, st->maxcount);
316 str = msi_string_lookup_id( st, id );
317 if( !str )
318 return ERROR_FUNCTION_FAILED;
320 len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
322 if( !buffer )
324 *sz = len;
325 return ERROR_SUCCESS;
328 if( len > *sz )
330 n = strlenW( str ) + 1;
331 while( n && (len > *sz) )
332 len = WideCharToMultiByte( st->codepage, 0,
333 str, --n, NULL, 0, NULL, NULL );
335 else
336 n = -1;
338 *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
340 return ERROR_SUCCESS;
344 * msi_string2idW
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 )
352 UINT hash;
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 ) ))
362 r = ERROR_SUCCESS;
363 *id = i;
364 break;
368 return r;
371 UINT msi_string2idA( string_table *st, LPCSTR buffer, UINT *id )
373 DWORD sz;
374 UINT r = ERROR_INVALID_PARAMETER;
375 LPWSTR str;
377 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
379 if( buffer[0] == 0 )
381 *id = 0;
382 return ERROR_SUCCESS;
385 sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
386 if( sz <= 0 )
387 return r;
388 str = msi_alloc( sz*sizeof(WCHAR) );
389 if( !str )
390 return ERROR_NOT_ENOUGH_MEMORY;
391 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
393 r = msi_string2idW( st, str, id );
394 msi_free( str );
396 return r;
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 );
404 if( !l_str )
405 return ERROR_INVALID_PARAMETER;
407 r_str = msi_string_lookup_id( st, rval );
408 if( !r_str )
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 )
419 return st->maxcount;
422 UINT msi_id_refcount( string_table *st, UINT i )
424 if( i >= st->maxcount )
425 return 0;
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");
435 *total = 0;
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);
443 if( len )
444 len--;
445 size += len;
446 *total = (i+1);
449 TRACE("%u/%u strings %u bytes codepage %x\n", *total, st->maxcount, size, st->codepage );
450 return size;
453 UINT msi_string_get_codepage( string_table *st )
455 return st->codepage;