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 );
213 msiobj_release( &rec->hdr );
215 return ERROR_SUCCESS;
218 UINT MSI_RecordSetInteger( MSIRECORD *rec, unsigned int iField, int iVal )
220 TRACE("%p %u %d\n", rec, iField, iVal);
222 if( iField > rec->count )
223 return ERROR_INVALID_PARAMETER;
225 MSI_FreeField( &rec->fields[iField] );
226 rec->fields[iField].type = MSIFIELD_INT;
227 rec->fields[iField].u.iVal = iVal;
229 return ERROR_SUCCESS;
232 UINT WINAPI MsiRecordSetInteger( MSIHANDLE handle, unsigned int iField, int iVal )
237 TRACE("%ld %u %d\n", handle, iField, iVal);
239 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
241 return ERROR_INVALID_HANDLE;
243 msiobj_lock( &rec->hdr );
244 ret = MSI_RecordSetInteger( rec, iField, iVal );
245 msiobj_unlock( &rec->hdr );
246 msiobj_release( &rec->hdr );
250 BOOL MSI_RecordIsNull( MSIRECORD *rec, unsigned int iField )
254 TRACE("%p %d\n", rec, iField );
256 r = ( iField > rec->count ) ||
257 ( rec->fields[iField].type == MSIFIELD_NULL );
262 BOOL WINAPI MsiRecordIsNull( MSIHANDLE handle, unsigned int iField )
267 TRACE("%ld %d\n", handle, iField );
269 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
272 msiobj_lock( &rec->hdr );
273 ret = MSI_RecordIsNull( rec, iField );
274 msiobj_unlock( &rec->hdr );
275 msiobj_release( &rec->hdr );
280 UINT MSI_RecordGetStringA(MSIRECORD *rec, unsigned int iField,
281 LPSTR szValue, DWORD *pcchValue)
286 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
288 if( iField > rec->count )
289 return ERROR_INVALID_PARAMETER;
292 switch( rec->fields[iField].type )
295 wsprintfA(buffer, "%d", rec->fields[iField].u.iVal);
296 len = lstrlenA( buffer );
297 lstrcpynA(szValue, buffer, *pcchValue);
300 len = WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
301 NULL, 0 , NULL, NULL);
302 WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
303 szValue, *pcchValue, NULL, NULL);
304 if( *pcchValue && len>*pcchValue )
305 szValue[*pcchValue-1] = 0;
314 ret = ERROR_INVALID_PARAMETER;
318 if( *pcchValue < len )
319 ret = ERROR_MORE_DATA;
325 UINT WINAPI MsiRecordGetStringA(MSIHANDLE handle, unsigned int iField,
326 LPSTR szValue, DWORD *pcchValue)
331 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
333 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
335 return ERROR_INVALID_HANDLE;
336 msiobj_lock( &rec->hdr );
337 ret = MSI_RecordGetStringA( rec, iField, szValue, pcchValue);
338 msiobj_unlock( &rec->hdr );
339 msiobj_release( &rec->hdr );
343 const WCHAR *MSI_RecordGetString( MSIRECORD *rec, unsigned int iField )
345 if( iField > rec->count )
348 if( rec->fields[iField].type != MSIFIELD_WSTR )
351 return rec->fields[iField].u.szwVal;
354 UINT MSI_RecordGetStringW(MSIRECORD *rec, unsigned int iField,
355 LPWSTR szValue, DWORD *pcchValue)
359 static const WCHAR szFormat[] = { '%','d',0 };
361 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
363 if( iField > rec->count )
364 return ERROR_INVALID_PARAMETER;
367 switch( rec->fields[iField].type )
370 wsprintfW(buffer, szFormat, rec->fields[iField].u.iVal);
371 len = lstrlenW( buffer );
372 lstrcpynW(szValue, buffer, *pcchValue);
375 len = lstrlenW( rec->fields[iField].u.szwVal );
376 lstrcpynW(szValue, rec->fields[iField].u.szwVal, *pcchValue);
386 if( *pcchValue < len )
387 ret = ERROR_MORE_DATA;
393 UINT WINAPI MsiRecordGetStringW(MSIHANDLE handle, unsigned int iField,
394 LPWSTR szValue, DWORD *pcchValue)
399 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
401 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
403 return ERROR_INVALID_HANDLE;
405 msiobj_lock( &rec->hdr );
406 ret = MSI_RecordGetStringW( rec, iField, szValue, pcchValue );
407 msiobj_unlock( &rec->hdr );
408 msiobj_release( &rec->hdr );
412 UINT MSI_RecordDataSize(MSIRECORD *rec, unsigned int iField)
414 TRACE("%p %d\n", rec, iField);
416 if( iField > rec->count )
419 switch( rec->fields[iField].type )
424 return lstrlenW( rec->fields[iField].u.szwVal );
431 UINT WINAPI MsiRecordDataSize(MSIHANDLE handle, unsigned int iField)
436 TRACE("%ld %d\n", handle, iField);
438 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
441 msiobj_lock( &rec->hdr );
442 ret = MSI_RecordDataSize( rec, iField);
443 msiobj_unlock( &rec->hdr );
444 msiobj_release( &rec->hdr );
448 UINT MSI_RecordSetStringA( MSIRECORD *rec, unsigned int iField, LPCSTR szValue )
453 TRACE("%p %d %s\n", rec, iField, debugstr_a(szValue));
455 if( iField > rec->count )
456 return ERROR_INVALID_FIELD;
458 MSI_FreeField( &rec->fields[iField] );
459 if( szValue && szValue[0] )
461 len = MultiByteToWideChar( CP_ACP, 0, szValue, -1, NULL, 0 );
462 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
463 MultiByteToWideChar( CP_ACP, 0, szValue, -1, str, len );
464 rec->fields[iField].type = MSIFIELD_WSTR;
465 rec->fields[iField].u.szwVal = str;
469 rec->fields[iField].type = MSIFIELD_NULL;
470 rec->fields[iField].u.szwVal = NULL;
476 UINT WINAPI MsiRecordSetStringA( MSIHANDLE handle, unsigned int iField, LPCSTR szValue )
481 TRACE("%ld %d %s\n", handle, iField, debugstr_a(szValue));
483 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
485 return ERROR_INVALID_HANDLE;
486 msiobj_lock( &rec->hdr );
487 ret = MSI_RecordSetStringA( rec, iField, szValue );
488 msiobj_unlock( &rec->hdr );
489 msiobj_release( &rec->hdr );
493 UINT MSI_RecordSetStringW( MSIRECORD *rec, unsigned int iField, LPCWSTR szValue )
498 TRACE("%p %d %s\n", rec, iField, debugstr_w(szValue));
500 if( iField > rec->count )
501 return ERROR_INVALID_FIELD;
503 MSI_FreeField( &rec->fields[iField] );
505 if( szValue && szValue[0] )
507 len = lstrlenW(szValue) + 1;
508 str = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR));
509 lstrcpyW( str, szValue );
511 rec->fields[iField].type = MSIFIELD_WSTR;
512 rec->fields[iField].u.szwVal = str;
516 rec->fields[iField].type = MSIFIELD_NULL;
517 rec->fields[iField].u.szwVal = NULL;
523 UINT WINAPI MsiRecordSetStringW( MSIHANDLE handle, unsigned int iField, LPCWSTR szValue )
528 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szValue));
530 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
532 return ERROR_INVALID_HANDLE;
534 msiobj_lock( &rec->hdr );
535 ret = MSI_RecordSetStringW( rec, iField, szValue );
536 msiobj_unlock( &rec->hdr );
537 msiobj_release( &rec->hdr );
541 /* read the data in a file into an IStream */
542 UINT RECORD_StreamFromFile(LPCWSTR szFile, IStream **pstm)
544 DWORD sz, szHighWord = 0, read;
548 ULARGE_INTEGER ulSize;
550 TRACE("reading %s\n", debugstr_w(szFile));
552 /* read the file into memory */
553 handle = CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
554 if( handle == INVALID_HANDLE_VALUE )
555 return GetLastError();
556 sz = GetFileSize(handle, &szHighWord);
557 if( sz != INVALID_FILE_SIZE && szHighWord == 0 )
559 hGlob = GlobalAlloc(GMEM_FIXED, sz);
562 BOOL r = ReadFile(handle, hGlob, sz, &read, NULL);
572 return ERROR_FUNCTION_FAILED;
574 /* make a stream out of it, and set the correct file size */
575 hr = CreateStreamOnHGlobal(hGlob, TRUE, pstm);
579 return ERROR_FUNCTION_FAILED;
582 /* set the correct size - CreateStreamOnHGlobal screws it up */
583 ulSize.QuadPart = sz;
584 IStream_SetSize(*pstm, ulSize);
586 TRACE("read %s, %ld bytes into IStream %p\n", debugstr_w(szFile), sz, *pstm);
588 return ERROR_SUCCESS;
591 UINT MSI_RecordSetStreamW(MSIRECORD *rec, unsigned int iField, LPCWSTR szFilename)
596 if( (iField == 0) || (iField > rec->count) )
597 return ERROR_INVALID_PARAMETER;
599 /* no filename means we should seek back to the start of the stream */
605 if( rec->fields[iField].type != MSIFIELD_STREAM )
606 return ERROR_INVALID_FIELD;
608 stm = rec->fields[iField].u.stream;
610 return ERROR_INVALID_FIELD;
613 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
615 return ERROR_FUNCTION_FAILED;
619 /* read the file into a stream and save the stream in the record */
620 r = RECORD_StreamFromFile(szFilename, &stm);
621 if( r != ERROR_SUCCESS )
624 /* if all's good, store it in the record */
625 MSI_FreeField( &rec->fields[iField] );
626 rec->fields[iField].type = MSIFIELD_STREAM;
627 rec->fields[iField].u.stream = stm;
630 return ERROR_SUCCESS;
633 UINT WINAPI MsiRecordSetStreamA(MSIHANDLE hRecord, unsigned int iField, LPCSTR szFilename)
638 TRACE("%ld %d %s\n", hRecord, iField, debugstr_a(szFilename));
642 len = MultiByteToWideChar(CP_ACP,0,szFilename,-1,NULL,0);
643 wstr = HeapAlloc(GetProcessHeap(),0,len*sizeof(WCHAR));
644 MultiByteToWideChar(CP_ACP,0,szFilename,-1,wstr,len);
646 ret = MsiRecordSetStreamW(hRecord, iField, wstr);
647 HeapFree(GetProcessHeap(),0,wstr);
652 UINT WINAPI MsiRecordSetStreamW(MSIHANDLE handle, unsigned int iField, LPCWSTR szFilename)
657 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szFilename));
659 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
661 return ERROR_INVALID_HANDLE;
663 msiobj_lock( &rec->hdr );
664 ret = MSI_RecordSetStreamW( rec, iField, szFilename );
665 msiobj_unlock( &rec->hdr );
666 msiobj_release( &rec->hdr );
670 UINT MSI_RecordReadStream(MSIRECORD *rec, unsigned int iField, char *buf, DWORD *sz)
676 TRACE("%p %d %p %p\n", rec, iField, buf, sz);
679 return ERROR_INVALID_PARAMETER;
681 if( iField > rec->count)
682 return ERROR_INVALID_PARAMETER;
684 if( rec->fields[iField].type != MSIFIELD_STREAM )
685 return ERROR_INVALID_DATATYPE;
687 stm = rec->fields[iField].u.stream;
689 return ERROR_INVALID_PARAMETER;
691 /* if there's no buffer pointer, calculate the length to the end */
695 ULARGE_INTEGER end, cur;
697 ofs.QuadPart = cur.QuadPart = 0;
699 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
700 IStream_Seek( stm, ofs, STREAM_SEEK_END, &end );
701 ofs.QuadPart = cur.QuadPart;
702 IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
703 *sz = end.QuadPart - cur.QuadPart;
705 return ERROR_SUCCESS;
710 r = IStream_Read( stm, buf, *sz, &count );
714 return ERROR_FUNCTION_FAILED;
719 return ERROR_SUCCESS;
722 UINT WINAPI MsiRecordReadStream(MSIHANDLE handle, unsigned int iField, char *buf, DWORD *sz)
727 TRACE("%ld %d %p %p\n", handle, iField, buf, sz);
729 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
731 return ERROR_INVALID_HANDLE;
732 msiobj_lock( &rec->hdr );
733 ret = MSI_RecordReadStream( rec, iField, buf, sz );
734 msiobj_unlock( &rec->hdr );
735 msiobj_release( &rec->hdr );
739 UINT MSI_RecordSetIStream( MSIRECORD *rec, unsigned int iField, IStream *stm )
741 TRACE("%p %d %p\n", rec, iField, stm);
743 if( iField > rec->count )
744 return ERROR_INVALID_FIELD;
746 MSI_FreeField( &rec->fields[iField] );
748 rec->fields[iField].type = MSIFIELD_STREAM;
749 rec->fields[iField].u.stream = stm;
750 IStream_AddRef( stm );
752 return ERROR_SUCCESS;