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
27 #include "wine/debug.h"
28 #include "wine/unicode.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
40 #define HASH_SIZE 0x101
42 typedef struct _msistring
51 UINT maxcount
; /* the number of strings */
55 msistring
*strings
; /* an array of strings (in the tree) */
58 static UINT
msistring_makehash( const WCHAR
*str
)
69 hash
= (hash
<<5) | (hash
>>27);
71 return hash
% HASH_SIZE
;
74 string_table
*msi_init_stringtable( int entries
, UINT codepage
)
79 st
= msi_alloc( sizeof (string_table
) );
84 st
->strings
= msi_alloc_zero( sizeof (msistring
) * entries
);
90 st
->maxcount
= entries
;
92 st
->codepage
= codepage
;
94 for( i
=0; i
<HASH_SIZE
; i
++ )
100 VOID
msi_destroy_stringtable( string_table
*st
)
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
);
113 static int st_find_free_entry( string_table
*st
)
122 for( i
= st
->freeslot
; i
< st
->maxcount
; i
++ )
123 if( !st
->strings
[i
].refcount
)
126 for( i
= 1; i
< st
->maxcount
; i
++ )
127 if( !st
->strings
[i
].refcount
)
130 /* dynamically resize */
131 sz
= st
->maxcount
+ 1 + st
->maxcount
/2;
132 p
= msi_realloc_zero( st
->strings
, sz
*sizeof(msistring
) );
136 st
->freeslot
= st
->maxcount
;
138 if( st
->strings
[st
->freeslot
].refcount
)
139 ERR("oops. expected freeslot to be free...\n");
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
];
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
)
168 if( st
->strings
[n
].refcount
)
173 if( ERROR_SUCCESS
== msi_string2idA( st
, data
, &n
) )
175 st
->strings
[n
].refcount
++;
178 n
= st_find_free_entry( st
);
185 ERR("invalid index adding %s (%d)\n", debugstr_a( data
), n
);
189 /* allocate a new string */
192 sz
= MultiByteToWideChar( st
->codepage
, 0, data
, len
, NULL
, 0 );
193 str
= msi_alloc( (sz
+1)*sizeof(WCHAR
) );
196 MultiByteToWideChar( st
->codepage
, 0, data
, len
, str
, sz
);
199 set_st_entry( st
, n
, str
);
204 int msi_addstringW( string_table
*st
, UINT n
, const WCHAR
*data
, int len
, UINT refcount
)
208 /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
216 if( st
->strings
[n
].refcount
)
221 if( ERROR_SUCCESS
== msi_string2idW( st
, data
, &n
) )
223 st
->strings
[n
].refcount
++;
226 n
= st_find_free_entry( st
);
233 ERR("invalid index adding %s (%d)\n", debugstr_w( data
), n
);
237 /* allocate a new string */
240 TRACE("%s, n = %d len = %d\n", debugstr_w(data
), n
, len
);
242 str
= msi_alloc( (len
+1)*sizeof(WCHAR
) );
245 TRACE("%d\n",__LINE__
);
246 memcpy( str
, data
, len
*sizeof(WCHAR
) );
249 set_st_entry( st
, n
, str
);
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 };
261 if( id
>= st
->maxcount
)
264 if( id
&& !st
->strings
[id
].refcount
)
267 return st
->strings
[id
].str
;
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
)
287 TRACE("Finding string %d of %d\n", id
, st
->maxcount
);
289 str
= msi_string_lookup_id( st
, id
);
291 return ERROR_FUNCTION_FAILED
;
293 len
= strlenW( str
) + 1;
298 return ERROR_SUCCESS
;
303 memcpy( buffer
, str
, (*sz
)*sizeof(WCHAR
) );
306 return ERROR_SUCCESS
;
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
)
327 TRACE("Finding string %d of %d\n", id
, st
->maxcount
);
329 str
= msi_string_lookup_id( st
, id
);
331 return ERROR_FUNCTION_FAILED
;
333 len
= WideCharToMultiByte( st
->codepage
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
338 return ERROR_SUCCESS
;
343 n
= strlenW( str
) + 1;
344 while( n
&& (len
> *sz
) )
345 len
= WideCharToMultiByte( st
->codepage
, 0,
346 str
, --n
, NULL
, 0, NULL
, NULL
);
351 *sz
= WideCharToMultiByte( st
->codepage
, 0, str
, n
, buffer
, len
, NULL
, NULL
);
353 return ERROR_SUCCESS
;
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
))
373 return ERROR_SUCCESS
;
377 return ERROR_INVALID_PARAMETER
;
380 UINT
msi_string2idA( string_table
*st
, LPCSTR buffer
, UINT
*id
)
383 UINT r
= ERROR_INVALID_PARAMETER
;
386 TRACE("Finding string %s in string table\n", debugstr_a(buffer
) );
391 return ERROR_SUCCESS
;
394 sz
= MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, NULL
, 0 );
397 str
= msi_alloc( sz
*sizeof(WCHAR
) );
399 return ERROR_NOT_ENOUGH_MEMORY
;
400 MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, str
, sz
);
402 r
= msi_string2idW( st
, str
, id
);
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
);
414 return ERROR_INVALID_PARAMETER
;
416 r_str
= msi_string_lookup_id( st
, rval
);
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
)
431 UINT
msi_id_refcount( string_table
*st
, UINT i
)
433 if( i
>= st
->maxcount
)
435 return st
->strings
[i
].refcount
;
438 UINT
msi_string_totalsize( string_table
*st
, UINT
*datasize
, UINT
*poolsize
)
440 UINT i
, len
, max
, holesize
;
442 if( st
->strings
[0].str
|| st
->strings
[0].refcount
)
443 ERR("oops. element 0 has a string\n");
449 for( i
=1; i
<st
->maxcount
; i
++ )
451 if( st
->strings
[i
].str
)
453 TRACE("[%u] = %s\n", i
, debugstr_w(st
->strings
[i
].str
));
454 len
= WideCharToMultiByte( st
->codepage
, 0,
455 st
->strings
[i
].str
, -1, NULL
, 0, NULL
, NULL
);
462 (*poolsize
) += holesize
+ 4;
468 TRACE("data %u pool %u codepage %x\n", *datasize
, *poolsize
, st
->codepage
);
472 UINT
msi_string_get_codepage( string_table
*st
)