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
29 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41 #define MSIFIELD_NULL 0
42 #define MSIFIELD_INT 1
43 #define MSIFIELD_STR 2
44 #define MSIFIELD_WSTR 3
45 #define MSIFIELD_STREAM 4
47 void MSI_FreeField( MSIFIELD *field )
55 HeapFree( GetProcessHeap(), 0, field->u.szwVal);
58 IStream_Release( field->u.stream );
61 ERR("Invalid field type %d\n", field->type);
65 void MSI_CloseRecord( MSIOBJECTHDR *arg )
67 MSIRECORD *rec = (MSIRECORD *) arg;
70 for( i=0; i<=rec->count; i++ )
71 MSI_FreeField( &rec->fields[i] );
74 MSIRECORD *MSI_CreateRecord( unsigned int cParams )
79 TRACE("%d\n", cParams);
84 len = sizeof (MSIRECORD) + sizeof (MSIFIELD)*cParams;
85 rec = alloc_msiobject( MSIHANDLETYPE_RECORD, len, MSI_CloseRecord );
91 MSIHANDLE WINAPI MsiCreateRecord( unsigned int cParams )
96 TRACE("%d\n", cParams);
98 rec = MSI_CreateRecord( cParams );
100 ret = alloc_msihandle( &rec->hdr );
104 unsigned int MSI_RecordGetFieldCount( MSIRECORD *rec )
109 unsigned int WINAPI MsiRecordGetFieldCount( MSIHANDLE handle )
114 TRACE("%ld\n", handle );
116 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
120 msiobj_lock( &rec->hdr );
121 ret = MSI_RecordGetFieldCount( rec );
122 msiobj_unlock( &rec->hdr );
123 msiobj_release( &rec->hdr );
128 static BOOL string2intW( LPCWSTR str, int *out )
133 if( *p == '-' ) /* skip the minus sign */
137 if( (*p < '0') || (*p > '9') )
144 if( str[0] == '-' ) /* check if it's negative */
151 int MSI_RecordGetInteger( MSIRECORD *rec, unsigned int iField)
155 TRACE("%p %d\n", rec, iField );
157 if( iField > rec->count )
158 return MSI_NULL_INTEGER;
160 switch( rec->fields[iField].type )
163 return rec->fields[iField].u.iVal;
165 if( string2intW( rec->fields[iField].u.szwVal, &ret ) )
167 return MSI_NULL_INTEGER;
172 return MSI_NULL_INTEGER;
175 int WINAPI MsiRecordGetInteger( MSIHANDLE handle, unsigned int iField)
180 TRACE("%ld %d\n", handle, iField );
182 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
184 return MSI_NULL_INTEGER;
186 msiobj_lock( &rec->hdr );
187 ret = MSI_RecordGetInteger( rec, iField );
188 msiobj_unlock( &rec->hdr );
189 msiobj_release( &rec->hdr );
194 UINT WINAPI MsiRecordClearData( MSIHANDLE handle )
199 TRACE("%ld\n", handle );
201 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
203 return ERROR_INVALID_HANDLE;
205 msiobj_lock( &rec->hdr );
206 for( i=0; i<=rec->count; i++)
208 MSI_FreeField( &rec->fields[i] );
209 rec->fields[i].type = MSIFIELD_NULL;
210 rec->fields[i].u.iVal = 0;
212 msiobj_unlock( &rec->hdr );
214 return ERROR_SUCCESS;
217 UINT MSI_RecordSetInteger( MSIRECORD *rec, unsigned int iField, int iVal )
219 TRACE("%p %u %d\n", rec, iField, iVal);
221 if( iField > rec->count )
222 return ERROR_INVALID_PARAMETER;
224 MSI_FreeField( &rec->fields[iField] );
225 rec->fields[iField].type = MSIFIELD_INT;
226 rec->fields[iField].u.iVal = iVal;
228 return ERROR_SUCCESS;
231 UINT WINAPI MsiRecordSetInteger( MSIHANDLE handle, unsigned int iField, int iVal )
236 TRACE("%ld %u %d\n", handle, iField, iVal);
238 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
240 return ERROR_INVALID_HANDLE;
242 msiobj_lock( &rec->hdr );
243 ret = MSI_RecordSetInteger( rec, iField, iVal );
244 msiobj_unlock( &rec->hdr );
245 msiobj_release( &rec->hdr );
249 BOOL MSI_RecordIsNull( MSIRECORD *rec, unsigned int iField )
253 TRACE("%p %d\n", rec, iField );
255 r = ( iField > rec->count ) ||
256 ( rec->fields[iField].type == MSIFIELD_NULL );
261 BOOL WINAPI MsiRecordIsNull( MSIHANDLE handle, unsigned int iField )
266 TRACE("%ld %d\n", handle, iField );
268 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
271 msiobj_lock( &rec->hdr );
272 ret = MSI_RecordIsNull( rec, iField );
273 msiobj_unlock( &rec->hdr );
274 msiobj_release( &rec->hdr );
279 UINT MSI_RecordGetStringA(MSIRECORD *rec, unsigned int iField,
280 LPSTR szValue, DWORD *pcchValue)
285 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
287 if( iField > rec->count )
288 return ERROR_INVALID_PARAMETER;
291 switch( rec->fields[iField].type )
294 wsprintfA(buffer, "%d", rec->fields[iField].u.iVal);
295 len = lstrlenA( buffer );
296 lstrcpynA(szValue, buffer, *pcchValue);
299 len = WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
300 NULL, 0 , NULL, NULL);
301 WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
302 szValue, *pcchValue, NULL, NULL);
303 if( *pcchValue && len>*pcchValue )
304 szValue[*pcchValue-1] = 0;
313 ret = ERROR_INVALID_PARAMETER;
317 if( *pcchValue < len )
318 ret = ERROR_MORE_DATA;
324 UINT WINAPI MsiRecordGetStringA(MSIHANDLE handle, unsigned int iField,
325 LPSTR szValue, DWORD *pcchValue)
330 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
332 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
334 return ERROR_INVALID_HANDLE;
335 msiobj_lock( &rec->hdr );
336 ret = MSI_RecordGetStringA( rec, iField, szValue, pcchValue);
337 msiobj_unlock( &rec->hdr );
338 msiobj_release( &rec->hdr );
342 const WCHAR *MSI_RecordGetString( MSIRECORD *rec, unsigned int iField )
344 if( iField > rec->count )
347 if( rec->fields[iField].type != MSIFIELD_WSTR )
350 return rec->fields[iField].u.szwVal;
353 UINT MSI_RecordGetStringW(MSIRECORD *rec, unsigned int iField,
354 LPWSTR szValue, DWORD *pcchValue)
358 static const WCHAR szFormat[] = { '%','d',0 };
360 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
362 if( iField > rec->count )
363 return ERROR_INVALID_PARAMETER;
366 switch( rec->fields[iField].type )
369 wsprintfW(buffer, szFormat, rec->fields[iField].u.iVal);
370 len = lstrlenW( buffer );
371 lstrcpynW(szValue, buffer, *pcchValue);
374 len = lstrlenW( rec->fields[iField].u.szwVal );
375 lstrcpynW(szValue, rec->fields[iField].u.szwVal, *pcchValue);
385 if( *pcchValue < len )
386 ret = ERROR_MORE_DATA;
392 UINT WINAPI MsiRecordGetStringW(MSIHANDLE handle, unsigned int iField,
393 LPWSTR szValue, DWORD *pcchValue)
398 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
400 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
402 return ERROR_INVALID_HANDLE;
404 msiobj_lock( &rec->hdr );
405 ret = MSI_RecordGetStringW( rec, iField, szValue, pcchValue );
406 msiobj_unlock( &rec->hdr );
407 msiobj_release( &rec->hdr );
411 UINT MSI_RecordDataSize(MSIRECORD *rec, unsigned int iField)
413 TRACE("%p %d\n", rec, iField);
415 if( iField > rec->count )
418 switch( rec->fields[iField].type )
423 return lstrlenW( rec->fields[iField].u.szwVal );
430 UINT WINAPI MsiRecordDataSize(MSIHANDLE handle, unsigned int iField)
435 TRACE("%ld %d\n", handle, iField);
437 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
440 msiobj_lock( &rec->hdr );
441 ret = MSI_RecordDataSize( rec, iField);
442 msiobj_unlock( &rec->hdr );
443 msiobj_release( &rec->hdr );
447 UINT MSI_RecordSetStringA( MSIRECORD *rec, unsigned int iField, LPCSTR szValue )
452 TRACE("%p %d %s\n", rec, iField, debugstr_a(szValue));
454 if( iField > rec->count )
455 return ERROR_INVALID_FIELD;
457 MSI_FreeField( &rec->fields[iField] );
460 len = MultiByteToWideChar( CP_ACP, 0, szValue, -1, NULL, 0 );
461 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
462 MultiByteToWideChar( CP_ACP, 0, szValue, -1, str, len );
463 rec->fields[iField].type = MSIFIELD_WSTR;
464 rec->fields[iField].u.szwVal = str;
468 rec->fields[iField].type = MSIFIELD_NULL;
469 rec->fields[iField].u.szwVal = NULL;
475 UINT WINAPI MsiRecordSetStringA( MSIHANDLE handle, unsigned int iField, LPCSTR szValue )
480 TRACE("%ld %d %s\n", handle, iField, debugstr_a(szValue));
482 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
484 return ERROR_INVALID_HANDLE;
485 msiobj_lock( &rec->hdr );
486 ret = MSI_RecordSetStringA( rec, iField, szValue );
487 msiobj_unlock( &rec->hdr );
488 msiobj_release( &rec->hdr );
492 UINT MSI_RecordSetStringW( MSIRECORD *rec, unsigned int iField, LPCWSTR szValue )
497 TRACE("%p %d %s\n", rec, iField, debugstr_w(szValue));
499 if( iField > rec->count )
500 return ERROR_INVALID_FIELD;
502 MSI_FreeField( &rec->fields[iField] );
506 len = lstrlenW(szValue) + 1;
507 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR));
508 lstrcpyW( str, szValue );
510 rec->fields[iField].type = MSIFIELD_WSTR;
511 rec->fields[iField].u.szwVal = str;
515 rec->fields[iField].type = MSIFIELD_NULL;
516 rec->fields[iField].u.szwVal = NULL;
522 UINT WINAPI MsiRecordSetStringW( MSIHANDLE handle, unsigned int iField, LPCWSTR szValue )
527 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szValue));
529 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
531 return ERROR_INVALID_HANDLE;
533 msiobj_lock( &rec->hdr );
534 ret = MSI_RecordSetStringW( rec, iField, szValue );
535 msiobj_unlock( &rec->hdr );
536 msiobj_release( &rec->hdr );
540 /* read the data in a file into an IStream */
541 UINT RECORD_StreamFromFile(LPCWSTR szFile, IStream **pstm)
543 DWORD sz, szHighWord = 0, read;
547 ULARGE_INTEGER ulSize;
549 TRACE("reading %s\n", debugstr_w(szFile));
551 /* read the file into memory */
552 handle = CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
553 if( handle == INVALID_HANDLE_VALUE )
554 return GetLastError();
555 sz = GetFileSize(handle, &szHighWord);
556 if( sz != INVALID_FILE_SIZE && szHighWord == 0 )
558 hGlob = GlobalAlloc(GMEM_FIXED, sz);
561 BOOL r = ReadFile(handle, hGlob, sz, &read, NULL);
571 return ERROR_FUNCTION_FAILED;
573 /* make a stream out of it, and set the correct file size */
574 hr = CreateStreamOnHGlobal(hGlob, TRUE, pstm);
578 return ERROR_FUNCTION_FAILED;
581 /* set the correct size - CreateStreamOnHGlobal screws it up */
582 ulSize.QuadPart = sz;
583 IStream_SetSize(*pstm, ulSize);
585 TRACE("read %s, %ld bytes into IStream %p\n", debugstr_w(szFile), sz, *pstm);
587 return ERROR_SUCCESS;
590 UINT MSI_RecordSetStreamW(MSIRECORD *rec, unsigned int iField, LPCWSTR szFilename)
595 if( (iField == 0) || (iField > rec->count) )
596 return ERROR_INVALID_PARAMETER;
598 /* no filename means we should seek back to the start of the stream */
604 if( rec->fields[iField].type != MSIFIELD_STREAM )
605 return ERROR_INVALID_FIELD;
607 stm = rec->fields[iField].u.stream;
609 return ERROR_INVALID_FIELD;
612 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
614 return ERROR_FUNCTION_FAILED;
618 /* read the file into a stream and save the stream in the record */
619 r = RECORD_StreamFromFile(szFilename, &stm);
620 if( r != ERROR_SUCCESS )
623 /* if all's good, store it in the record */
624 MSI_FreeField( &rec->fields[iField] );
625 rec->fields[iField].type = MSIFIELD_STREAM;
626 rec->fields[iField].u.stream = stm;
629 return ERROR_SUCCESS;
632 UINT WINAPI MsiRecordSetStreamA(MSIHANDLE hRecord, unsigned int iField, LPCSTR szFilename)
637 TRACE("%ld %d %s\n", hRecord, iField, debugstr_a(szFilename));
641 len = MultiByteToWideChar(CP_ACP,0,szFilename,-1,NULL,0);
642 wstr = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
643 MultiByteToWideChar(CP_ACP,0,szFilename,-1,wstr,len);
645 ret = MsiRecordSetStreamW(hRecord, iField, wstr);
646 HeapFree(GetProcessHeap(),0,wstr);
651 UINT WINAPI MsiRecordSetStreamW(MSIHANDLE handle, unsigned int iField, LPCWSTR szFilename)
656 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szFilename));
658 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
660 return ERROR_INVALID_HANDLE;
662 msiobj_lock( &rec->hdr );
663 ret = MSI_RecordSetStreamW( rec, iField, szFilename );
664 msiobj_unlock( &rec->hdr );
665 msiobj_release( &rec->hdr );
669 UINT MSI_RecordReadStream(MSIRECORD *rec, unsigned int iField, char *buf, DWORD *sz)
675 TRACE("%p %d %p %p\n", rec, iField, buf, sz);
678 return ERROR_INVALID_PARAMETER;
680 if( iField > rec->count)
681 return ERROR_INVALID_PARAMETER;
683 if( rec->fields[iField].type != MSIFIELD_STREAM )
684 return ERROR_INVALID_DATATYPE;
686 stm = rec->fields[iField].u.stream;
688 return ERROR_INVALID_PARAMETER;
690 /* if there's no buffer pointer, calculate the length to the end */
694 ULARGE_INTEGER end, cur;
696 ofs.QuadPart = cur.QuadPart = 0;
698 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
699 IStream_Seek( stm, ofs, STREAM_SEEK_END, &end );
700 ofs.QuadPart = cur.QuadPart;
701 IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
702 *sz = end.QuadPart - cur.QuadPart;
704 return ERROR_SUCCESS;
709 r = IStream_Read( stm, buf, *sz, &count );
713 return ERROR_FUNCTION_FAILED;
718 return ERROR_SUCCESS;
721 UINT WINAPI MsiRecordReadStream(MSIHANDLE handle, unsigned int iField, char *buf, DWORD *sz)
726 TRACE("%ld %d %p %p\n", handle, iField, buf, sz);
728 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
730 return ERROR_INVALID_HANDLE;
731 msiobj_lock( &rec->hdr );
732 ret = MSI_RecordReadStream( rec, iField, buf, sz );
733 msiobj_unlock( &rec->hdr );
734 msiobj_release( &rec->hdr );
738 UINT MSI_RecordSetIStream( MSIRECORD *rec, unsigned int iField, IStream *stm )
740 TRACE("%p %d %p\n", rec, iField, stm);
742 if( iField > rec->count )
743 return ERROR_INVALID_FIELD;
745 MSI_FreeField( &rec->fields[iField] );
747 rec->fields[iField].type = MSIFIELD_STREAM;
748 rec->fields[iField].u.stream = stm;
749 IStream_AddRef( stm );
751 return ERROR_SUCCESS;