wined3d: Small rhw vertex fix.
[wine/wine-kai.git] / dlls / msi / string.c
blob3155c123ad157a10311ef5739ffe11b1f9aaad82
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 #define HASH_SIZE 67
42 typedef struct _msistring
44 int hash_next;
45 UINT refcount;
46 LPWSTR str;
47 } msistring;
49 struct string_table
51 UINT maxcount; /* the number of strings */
52 UINT freeslot;
53 UINT codepage;
54 int hash[HASH_SIZE];
55 msistring *strings; /* an array of strings (in the tree) */
58 static UINT msistring_makehash( const WCHAR *str )
60 UINT hash = 0;
62 if (str==NULL)
63 return hash;
65 while( *str )
67 hash ^= *str++;
68 hash *= 53;
69 hash = (hash<<5) | (hash>>27);
71 return hash % HASH_SIZE;
74 string_table *msi_init_stringtable( int entries, UINT codepage )
76 string_table *st;
77 int i;
79 st = msi_alloc( sizeof (string_table) );
80 if( !st )
81 return NULL;
82 if( entries < 1 )
83 entries = 1;
84 st->strings = msi_alloc_zero( sizeof (msistring) * entries );
85 if( !st->strings )
87 msi_free( st );
88 return NULL;
90 st->maxcount = entries;
91 st->freeslot = 1;
92 st->codepage = codepage;
94 for( i=0; i<HASH_SIZE; i++ )
95 st->hash[i] = -1;
97 return st;
100 VOID msi_destroy_stringtable( string_table *st )
102 UINT i;
104 for( i=0; i<st->maxcount; i++ )
106 if( st->strings[i].refcount )
107 msi_free( st->strings[i].str );
109 msi_free( st->strings );
110 msi_free( st );
113 static int st_find_free_entry( string_table *st )
115 UINT i, sz;
116 msistring *p;
118 TRACE("%p\n", st);
120 if( st->freeslot )
122 for( i = st->freeslot; i < st->maxcount; i++ )
123 if( !st->strings[i].refcount )
124 return i;
126 for( i = 1; i < st->maxcount; i++ )
127 if( !st->strings[i].refcount )
128 return i;
130 /* dynamically resize */
131 sz = st->maxcount + 1 + st->maxcount/2;
132 p = msi_realloc_zero( st->strings, sz*sizeof(msistring) );
133 if( !p )
134 return -1;
135 st->strings = p;
136 st->freeslot = st->maxcount;
137 st->maxcount = sz;
138 if( st->strings[st->freeslot].refcount )
139 ERR("oops. expected freeslot to be free...\n");
140 return st->freeslot;
143 static void set_st_entry( string_table *st, UINT n, LPWSTR str )
145 UINT hash = msistring_makehash( str );
147 st->strings[n].refcount = 1;
148 st->strings[n].str = str;
150 st->strings[n].hash_next = st->hash[hash];
151 st->hash[hash] = n;
153 if( n < st->maxcount )
154 st->freeslot = n + 1;
157 int msi_addstring( string_table *st, UINT n, const CHAR *data, int len, UINT refcount )
159 LPWSTR str;
160 int sz;
162 if( !data )
163 return 0;
164 if( !data[0] )
165 return 0;
166 if( n > 0 )
168 if( st->strings[n].refcount )
169 return -1;
171 else
173 if( ERROR_SUCCESS == msi_string2idA( st, data, &n ) )
175 st->strings[n].refcount++;
176 return n;
178 n = st_find_free_entry( st );
179 if( n < 0 )
180 return -1;
183 if( n < 1 )
185 ERR("invalid index adding %s (%d)\n", debugstr_a( data ), n );
186 return -1;
189 /* allocate a new string */
190 if( len < 0 )
191 len = strlen(data);
192 sz = MultiByteToWideChar( st->codepage, 0, data, len, NULL, 0 );
193 str = msi_alloc( (sz+1)*sizeof(WCHAR) );
194 if( !str )
195 return -1;
196 MultiByteToWideChar( st->codepage, 0, data, len, str, sz );
197 str[sz] = 0;
199 set_st_entry( st, n, str );
201 return n;
204 int msi_addstringW( string_table *st, UINT n, const WCHAR *data, int len, UINT refcount )
206 LPWSTR str;
208 /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
210 if( !data )
211 return 0;
212 if( !data[0] )
213 return 0;
214 if( n > 0 )
216 if( st->strings[n].refcount )
217 return -1;
219 else
221 if( ERROR_SUCCESS == msi_string2idW( st, data, &n ) )
223 st->strings[n].refcount++;
224 return n;
226 n = st_find_free_entry( st );
227 if( n < 0 )
228 return -1;
231 if( n < 1 )
233 ERR("invalid index adding %s (%d)\n", debugstr_w( data ), n );
234 return -1;
237 /* allocate a new string */
238 if(len<0)
239 len = strlenW(data);
240 TRACE("%s, n = %d len = %d\n", debugstr_w(data), n, len );
242 str = msi_alloc( (len+1)*sizeof(WCHAR) );
243 if( !str )
244 return -1;
245 TRACE("%d\n",__LINE__);
246 memcpy( str, data, len*sizeof(WCHAR) );
247 str[len] = 0;
249 set_st_entry( st, n, str );
251 return n;
254 /* find the string identified by an id - return null if there's none */
255 const WCHAR *msi_string_lookup_id( string_table *st, UINT id )
257 static const WCHAR zero[] = { 0 };
258 if( id == 0 )
259 return zero;
261 if( id >= st->maxcount )
262 return NULL;
264 if( id && !st->strings[id].refcount )
265 return NULL;
267 return st->strings[id].str;
271 * msi_id2stringW
273 * [in] st - pointer to the string table
274 * [in] id - id of the string to retrieve
275 * [out] buffer - destination of the string
276 * [in/out] sz - number of bytes available in the buffer on input
277 * number of bytes used on output
279 * The size includes the terminating nul character. Short buffers
280 * will be filled, but not nul terminated.
282 UINT msi_id2stringW( string_table *st, UINT id, LPWSTR buffer, UINT *sz )
284 UINT len;
285 const WCHAR *str;
287 TRACE("Finding string %d of %d\n", id, st->maxcount);
289 str = msi_string_lookup_id( st, id );
290 if( !str )
291 return ERROR_FUNCTION_FAILED;
293 len = strlenW( str ) + 1;
295 if( !buffer )
297 *sz = len;
298 return ERROR_SUCCESS;
301 if( *sz < len )
302 *sz = len;
303 memcpy( buffer, str, (*sz)*sizeof(WCHAR) );
304 *sz = len;
306 return ERROR_SUCCESS;
310 * msi_id2stringA
312 * [in] st - pointer to the string table
313 * [in] id - id of the string to retrieve
314 * [out] buffer - destination of the UTF8 string
315 * [in/out] sz - number of bytes available in the buffer on input
316 * number of bytes used on output
318 * The size includes the terminating nul character. Short buffers
319 * will be filled, but not nul terminated.
321 UINT msi_id2stringA( string_table *st, UINT id, LPSTR buffer, UINT *sz )
323 UINT len;
324 const WCHAR *str;
325 int n;
327 TRACE("Finding string %d of %d\n", id, st->maxcount);
329 str = msi_string_lookup_id( st, id );
330 if( !str )
331 return ERROR_FUNCTION_FAILED;
333 len = WideCharToMultiByte( st->codepage, 0, str, -1, NULL, 0, NULL, NULL );
335 if( !buffer )
337 *sz = len;
338 return ERROR_SUCCESS;
341 if( len > *sz )
343 n = strlenW( str ) + 1;
344 while( n && (len > *sz) )
345 len = WideCharToMultiByte( st->codepage, 0,
346 str, --n, NULL, 0, NULL, NULL );
348 else
349 n = -1;
351 *sz = WideCharToMultiByte( st->codepage, 0, str, n, buffer, len, NULL, NULL );
353 return ERROR_SUCCESS;
357 * msi_string2idW
359 * [in] st - pointer to the string table
360 * [in] str - string to find in the string table
361 * [out] id - id of the string, if found
363 UINT msi_string2idW( string_table *st, LPCWSTR str, UINT *id )
365 UINT n, hash = msistring_makehash( str );
366 msistring *se = st->strings;
368 for (n = st->hash[hash]; n != -1; n = st->strings[n].hash_next )
370 if ((str == se[n].str) || !lstrcmpW(str, se[n].str))
372 *id = n;
373 return ERROR_SUCCESS;
377 return ERROR_INVALID_PARAMETER;
380 UINT msi_string2idA( string_table *st, LPCSTR buffer, UINT *id )
382 DWORD sz;
383 UINT r = ERROR_INVALID_PARAMETER;
384 LPWSTR str;
386 TRACE("Finding string %s in string table\n", debugstr_a(buffer) );
388 if( buffer[0] == 0 )
390 *id = 0;
391 return ERROR_SUCCESS;
394 sz = MultiByteToWideChar( st->codepage, 0, buffer, -1, NULL, 0 );
395 if( sz <= 0 )
396 return r;
397 str = msi_alloc( sz*sizeof(WCHAR) );
398 if( !str )
399 return ERROR_NOT_ENOUGH_MEMORY;
400 MultiByteToWideChar( st->codepage, 0, buffer, -1, str, sz );
402 r = msi_string2idW( st, str, id );
403 msi_free( str );
405 return r;
408 UINT msi_strcmp( string_table *st, UINT lval, UINT rval, UINT *res )
410 const WCHAR *l_str, *r_str;
412 l_str = msi_string_lookup_id( st, lval );
413 if( !l_str )
414 return ERROR_INVALID_PARAMETER;
416 r_str = msi_string_lookup_id( st, rval );
417 if( !r_str )
418 return ERROR_INVALID_PARAMETER;
420 /* does this do the right thing for all UTF-8 strings? */
421 *res = strcmpW( l_str, r_str );
423 return ERROR_SUCCESS;
426 UINT msi_string_count( string_table *st )
428 return st->maxcount;
431 UINT msi_id_refcount( string_table *st, UINT i )
433 if( i >= st->maxcount )
434 return 0;
435 return st->strings[i].refcount;
438 UINT msi_string_totalsize( string_table *st, UINT *total )
440 UINT size = 0, i, len;
442 if( st->strings[0].str || st->strings[0].refcount )
443 ERR("oops. element 0 has a string\n");
444 *total = 0;
445 for( i=1; i<st->maxcount; i++ )
447 if( st->strings[i].str )
449 TRACE("[%u] = %s\n", i, debugstr_w(st->strings[i].str));
450 len = WideCharToMultiByte( st->codepage, 0,
451 st->strings[i].str, -1, NULL, 0, NULL, NULL);
452 if( len )
453 len--;
454 size += len;
455 *total = (i+1);
458 TRACE("%u/%u strings %u bytes codepage %x\n", *total, st->maxcount, size, st->codepage );
459 return size;
462 UINT msi_string_get_codepage( string_table *st )
464 return st->codepage;