2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002 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"
37 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
39 typedef struct _msistring
48 UINT count
; /* the number of strings */
50 msistring
*strings
; /* an array of strings (in the tree) */
53 static int msistring_makehash( char *str
)
61 hash
= (hash
<<5) || (hash
>>27);
66 string_table
*msi_init_stringtable( int entries
)
70 st
= HeapAlloc( GetProcessHeap(), 0, sizeof (string_table
) );
73 st
->strings
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
74 sizeof (msistring
) * entries
);
77 HeapFree( GetProcessHeap(), 0, st
);
86 VOID
msi_destroy_stringtable( string_table
*st
)
90 for( i
=0; i
<st
->count
; i
++ )
92 if( st
->strings
[i
].refcount
)
93 HeapFree( GetProcessHeap(), 0, st
->strings
[i
].str
);
95 HeapFree( GetProcessHeap(), 0, st
->strings
);
96 HeapFree( GetProcessHeap(), 0, st
);
99 static int st_find_free_entry( string_table
*st
)
103 for( i
= st
->freeslot
; i
< st
->count
; i
++ )
104 if( !st
->strings
[i
].refcount
)
106 for( i
= 0; i
< st
->freeslot
; i
++ )
107 if( !st
->strings
[i
].refcount
)
110 FIXME("dynamically resize\n");
115 static void st_mark_entry_used( string_table
*st
, int n
)
119 st
->freeslot
= n
+ 1;
122 int msi_addstring( string_table
*st
, UINT string_no
, CHAR
*data
, UINT len
, UINT refcount
)
126 /* TRACE("[%2d] = %s\n", string_no, debugstr_an(data,len) ); */
128 n
= st_find_free_entry( st
);
132 /* allocate a new string */
134 len
= strlen( data
);
135 st
->strings
[n
].str
= HeapAlloc( GetProcessHeap(), 0, len
+ 1 );
136 if( !st
->strings
[n
].str
)
138 memcpy( st
->strings
[n
].str
, data
, len
);
139 st
->strings
[n
].str
[len
] = 0;
140 st
->strings
[n
].refcount
= 1;
141 st
->strings
[n
].hash
= msistring_makehash( st
->strings
[n
].str
);
143 st_mark_entry_used( st
, n
);
148 UINT
msi_id2stringW( string_table
*st
, UINT string_no
, LPWSTR buffer
, UINT
*sz
)
153 TRACE("Finding string %d of %d\n", string_no
, st
->count
);
154 if( string_no
>= st
->count
)
155 return ERROR_FUNCTION_FAILED
;
157 if( !st
->strings
[string_no
].refcount
)
158 return ERROR_FUNCTION_FAILED
;
160 str
= st
->strings
[string_no
].str
;
165 *sz
= MultiByteToWideChar(CP_ACP
,0,str
,len
,NULL
,0);
166 return ERROR_SUCCESS
;
169 len
= MultiByteToWideChar(CP_ACP
,0,str
,len
+1,buffer
,*sz
);
170 if (!len
) buffer
[*sz
-1] = 0;
173 return ERROR_SUCCESS
;
176 UINT
msi_id2stringA( string_table
*st
, UINT string_no
, LPSTR buffer
, UINT
*sz
)
181 TRACE("Finding string %d of %d\n", string_no
, st
->count
);
182 if( string_no
>= st
->count
)
183 return ERROR_FUNCTION_FAILED
;
185 if( !st
->strings
[string_no
].refcount
)
186 return ERROR_FUNCTION_FAILED
;
188 str
= st
->strings
[string_no
].str
;
194 return ERROR_SUCCESS
;
197 if (len
>= *sz
) len
= *sz
- 1;
198 memcpy( buffer
, str
, len
);
202 return ERROR_SUCCESS
;
205 UINT
msi_string2id( string_table
*st
, LPCWSTR buffer
, UINT
*id
)
208 UINT i
, r
= ERROR_INVALID_PARAMETER
;
212 TRACE("Finding string %s in string table\n", debugstr_w(buffer
) );
214 sz
= WideCharToMultiByte( CP_ACP
, 0, buffer
, -1, NULL
, 0, NULL
, NULL
);
217 str
= HeapAlloc( GetProcessHeap(), 0, sz
);
219 return ERROR_NOT_ENOUGH_MEMORY
;
220 WideCharToMultiByte( CP_ACP
, 0, buffer
, -1, str
, sz
, NULL
, NULL
);
222 hash
= msistring_makehash( str
);
223 for( i
=0; i
<st
->count
; i
++)
225 if( ( st
->strings
[i
].hash
== hash
) &&
226 !strcmp( st
->strings
[i
].str
, str
) )
235 HeapFree( GetProcessHeap(), 0, str
);
240 UINT
msi_string_count( string_table
*st
)
245 UINT
msi_id_refcount( string_table
*st
, UINT i
)
249 return st
->strings
[i
].refcount
;
252 UINT
msi_string_totalsize( string_table
*st
)
256 for( i
=0; i
<st
->count
; i
++)
258 if( st
->strings
[i
].str
)
259 size
+= strlen( st
->strings
[i
].str
);