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 int msistring_makehash( const WCHAR
*str
)
63 hash
= (hash
<<5) || (hash
>>27);
68 string_table
*msi_init_stringtable( int entries
, UINT codepage
)
72 st
= HeapAlloc( GetProcessHeap(), 0, sizeof (string_table
) );
75 st
->strings
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
76 sizeof (msistring
) * entries
);
79 HeapFree( GetProcessHeap(), 0, st
);
84 st
->maxcount
= entries
;
86 st
->codepage
= codepage
;
91 VOID
msi_destroy_stringtable( string_table
*st
)
95 for( i
=0; i
<st
->maxcount
; i
++ )
97 if( st
->strings
[i
].refcount
)
98 HeapFree( GetProcessHeap(), 0, st
->strings
[i
].str
);
100 HeapFree( GetProcessHeap(), 0, st
->strings
);
101 HeapFree( GetProcessHeap(), 0, st
);
104 static int st_find_free_entry( string_table
*st
)
113 for( i
= st
->freeslot
; i
< st
->maxcount
; i
++ )
114 if( !st
->strings
[i
].refcount
)
117 for( i
= 1; i
< st
->maxcount
; i
++ )
118 if( !st
->strings
[i
].refcount
)
121 /* dynamically resize */
122 sz
= st
->maxcount
+ 1 + st
->maxcount
/2;
123 p
= HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
124 st
->strings
, sz
*sizeof(msistring
) );
128 st
->freeslot
= st
->maxcount
;
130 if( st
->strings
[st
->freeslot
].refcount
)
131 ERR("oops. expected freeslot to be free...\n");
135 static void st_mark_entry_used( string_table
*st
, int n
)
137 if( n
>= st
->maxcount
)
139 st
->freeslot
= n
+ 1;
142 int msi_addstring( string_table
*st
, int n
, const CHAR
*data
, int len
, UINT refcount
)
152 if( st
->strings
[n
].refcount
)
157 if( ERROR_SUCCESS
== msi_string2idA( st
, data
, &n
) )
159 st
->strings
[n
].refcount
++;
162 n
= st_find_free_entry( st
);
169 ERR("invalid index adding %s (%d)\n", debugstr_a( data
), n
);
173 /* allocate a new string */
176 sz
= MultiByteToWideChar( st
->codepage
, 0, data
, len
, NULL
, 0 );
177 st
->strings
[n
].str
= HeapAlloc( GetProcessHeap(), 0, (sz
+1)*sizeof(WCHAR
) );
178 if( !st
->strings
[n
].str
)
180 MultiByteToWideChar( st
->codepage
, 0, data
, len
, st
->strings
[n
].str
, sz
);
181 st
->strings
[n
].str
[sz
] = 0;
182 st
->strings
[n
].refcount
= 1;
183 st
->strings
[n
].hash
= msistring_makehash( st
->strings
[n
].str
);
185 st_mark_entry_used( st
, n
);
190 int msi_addstringW( string_table
*st
, int n
, const WCHAR
*data
, int len
, UINT refcount
)
192 /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
200 if( st
->strings
[n
].refcount
)
205 if( ERROR_SUCCESS
== msi_string2idW( st
, data
, &n
) )
207 st
->strings
[n
].refcount
++;
210 n
= st_find_free_entry( st
);
217 ERR("invalid index adding %s (%d)\n", debugstr_w( data
), n
);
221 /* allocate a new string */
224 TRACE("%s, n = %d len = %d\n", debugstr_w(data
), n
, len
);
226 st
->strings
[n
].str
= HeapAlloc( GetProcessHeap(), 0, (len
+1)*sizeof(WCHAR
) );
227 if( !st
->strings
[n
].str
)
229 TRACE("%d\n",__LINE__
);
230 memcpy( st
->strings
[n
].str
, data
, len
*sizeof(WCHAR
) );
231 st
->strings
[n
].str
[len
] = 0;
232 st
->strings
[n
].refcount
= 1;
233 st
->strings
[n
].hash
= msistring_makehash( st
->strings
[n
].str
);
235 st_mark_entry_used( st
, n
);
240 /* find the string identified by an id - return null if there's none */
241 const WCHAR
*msi_string_lookup_id( string_table
*st
, UINT id
)
243 static const WCHAR zero
[] = { 0 };
247 if( id
>= st
->maxcount
)
250 if( id
&& !st
->strings
[id
].refcount
)
253 return st
->strings
[id
].str
;
259 * [in] st - pointer to the string table
260 * [in] id - id of the string to retrieve
261 * [out] buffer - destination of the string
262 * [in/out] sz - number of bytes available in the buffer on input
263 * number of bytes used on output
265 * The size includes the terminating nul character. Short buffers
266 * will be filled, but not nul terminated.
268 UINT
msi_id2stringW( string_table
*st
, UINT id
, LPWSTR buffer
, UINT
*sz
)
273 TRACE("Finding string %d of %d\n", id
, st
->maxcount
);
275 str
= msi_string_lookup_id( st
, id
);
277 return ERROR_FUNCTION_FAILED
;
279 len
= strlenW( str
) + 1;
284 return ERROR_SUCCESS
;
289 memcpy( buffer
, str
, (*sz
)*sizeof(WCHAR
) );
292 return ERROR_SUCCESS
;
298 * [in] st - pointer to the string table
299 * [in] id - id of the string to retrieve
300 * [out] buffer - destination of the UTF8 string
301 * [in/out] sz - number of bytes available in the buffer on input
302 * number of bytes used on output
304 * The size includes the terminating nul character. Short buffers
305 * will be filled, but not nul terminated.
307 UINT
msi_id2stringA( string_table
*st
, UINT id
, LPSTR buffer
, UINT
*sz
)
313 TRACE("Finding string %d of %d\n", id
, st
->maxcount
);
315 str
= msi_string_lookup_id( st
, id
);
317 return ERROR_FUNCTION_FAILED
;
319 len
= WideCharToMultiByte( st
->codepage
, 0, str
, -1, NULL
, 0, NULL
, NULL
);
324 return ERROR_SUCCESS
;
329 n
= strlenW( str
) + 1;
330 while( n
&& (len
> *sz
) )
331 len
= WideCharToMultiByte( st
->codepage
, 0,
332 str
, --n
, NULL
, 0, NULL
, NULL
);
337 *sz
= WideCharToMultiByte( st
->codepage
, 0, str
, n
, buffer
, len
, NULL
, NULL
);
339 return ERROR_SUCCESS
;
345 * [in] st - pointer to the string table
346 * [in] str - string to find in the string table
347 * [out] id - id of the string, if found
349 UINT
msi_string2idW( string_table
*st
, LPCWSTR str
, UINT
*id
)
352 UINT i
, r
= ERROR_INVALID_PARAMETER
;
354 hash
= msistring_makehash( str
);
355 for( i
=0; i
<st
->maxcount
; i
++ )
357 if( ( st
->strings
[i
].hash
== hash
) &&
358 !strcmpW( st
->strings
[i
].str
, str
) )
369 UINT
msi_string2idA( string_table
*st
, LPCSTR buffer
, UINT
*id
)
372 UINT r
= ERROR_INVALID_PARAMETER
;
375 TRACE("Finding string %s in string table\n", debugstr_a(buffer
) );
380 return ERROR_SUCCESS
;
383 sz
= MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, NULL
, 0 );
386 str
= HeapAlloc( GetProcessHeap(), 0, sz
*sizeof(WCHAR
) );
388 return ERROR_NOT_ENOUGH_MEMORY
;
389 MultiByteToWideChar( st
->codepage
, 0, buffer
, -1, str
, sz
);
391 r
= msi_string2idW( st
, str
, id
);
393 HeapFree( GetProcessHeap(), 0, str
);
398 UINT
msi_strcmp( string_table
*st
, UINT lval
, UINT rval
, UINT
*res
)
400 const WCHAR
*l_str
, *r_str
;
402 l_str
= msi_string_lookup_id( st
, lval
);
404 return ERROR_INVALID_PARAMETER
;
406 r_str
= msi_string_lookup_id( st
, rval
);
408 return ERROR_INVALID_PARAMETER
;
410 /* does this do the right thing for all UTF-8 strings? */
411 *res
= strcmpW( l_str
, r_str
);
413 return ERROR_SUCCESS
;
416 UINT
msi_string_count( string_table
*st
)
421 UINT
msi_id_refcount( string_table
*st
, UINT i
)
423 if( i
>= st
->maxcount
)
425 return st
->strings
[i
].refcount
;
428 UINT
msi_string_totalsize( string_table
*st
, UINT
*total
)
430 UINT size
= 0, i
, len
;
432 if( st
->strings
[0].str
|| st
->strings
[0].refcount
)
433 ERR("oops. element 0 has a string\n");
435 for( i
=1; i
<st
->maxcount
; i
++ )
437 if( st
->strings
[i
].str
)
439 TRACE("[%u] = %s\n", i
, debugstr_w(st
->strings
[i
].str
));
440 len
= WideCharToMultiByte( st
->codepage
, 0,
441 st
->strings
[i
].str
, -1, NULL
, 0, NULL
, NULL
);
448 TRACE("%u/%u strings %u bytes codepage %x\n", *total
, st
->maxcount
, size
, st
->codepage
);
452 UINT
msi_string_get_codepage( string_table
*st
)