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
29 #include "wine/debug.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
44 #define MSIFIELD_NULL 0
45 #define MSIFIELD_INT 1
46 #define MSIFIELD_WSTR 3
47 #define MSIFIELD_STREAM 4
49 static void MSI_FreeField( MSIFIELD *field )
57 msi_free( field->u.szwVal);
60 IStream_Release( field->u.stream );
63 ERR("Invalid field type %d\n", field->type);
67 static void MSI_CloseRecord( MSIOBJECTHDR *arg )
69 MSIRECORD *rec = (MSIRECORD *) arg;
72 for( i=0; i<=rec->count; i++ )
73 MSI_FreeField( &rec->fields[i] );
76 MSIRECORD *MSI_CreateRecord( unsigned int cParams )
81 TRACE("%d\n", cParams);
86 len = sizeof (MSIRECORD) + sizeof (MSIFIELD)*cParams;
87 rec = alloc_msiobject( MSIHANDLETYPE_RECORD, len, MSI_CloseRecord );
93 MSIHANDLE WINAPI MsiCreateRecord( unsigned int cParams )
98 TRACE("%d\n", cParams);
100 rec = MSI_CreateRecord( cParams );
103 ret = alloc_msihandle( &rec->hdr );
104 msiobj_release( &rec->hdr );
109 unsigned int MSI_RecordGetFieldCount( MSIRECORD *rec )
114 unsigned int WINAPI MsiRecordGetFieldCount( MSIHANDLE handle )
119 TRACE("%ld\n", handle );
121 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
125 msiobj_lock( &rec->hdr );
126 ret = MSI_RecordGetFieldCount( rec );
127 msiobj_unlock( &rec->hdr );
128 msiobj_release( &rec->hdr );
133 static BOOL string2intW( LPCWSTR str, int *out )
138 if( *p == '-' ) /* skip the minus sign */
142 if( (*p < '0') || (*p > '9') )
149 if( str[0] == '-' ) /* check if it's negative */
156 UINT MSI_RecordCopyField( MSIRECORD *in_rec, unsigned int in_n,
157 MSIRECORD *out_rec, unsigned int out_n )
159 UINT r = ERROR_SUCCESS;
161 msiobj_lock( &in_rec->hdr );
163 if ( in_n > in_rec->count || out_n > out_rec->count )
164 r = ERROR_FUNCTION_FAILED;
165 else if ( in_rec != out_rec || in_n != out_n )
170 in = &in_rec->fields[in_n];
171 out = &out_rec->fields[out_n];
178 out->u.iVal = in->u.iVal;
181 str = strdupW( in->u.szwVal );
183 r = ERROR_OUTOFMEMORY;
187 case MSIFIELD_STREAM:
188 IStream_AddRef( in->u.stream );
189 out->u.stream = in->u.stream;
192 ERR("invalid field type %d\n", in->type);
194 if (r == ERROR_SUCCESS)
195 out->type = in->type;
198 msiobj_unlock( &in_rec->hdr );
203 int MSI_RecordGetInteger( MSIRECORD *rec, unsigned int iField)
207 TRACE("%p %d\n", rec, iField );
209 if( iField > rec->count )
210 return MSI_NULL_INTEGER;
212 switch( rec->fields[iField].type )
215 return rec->fields[iField].u.iVal;
217 if( string2intW( rec->fields[iField].u.szwVal, &ret ) )
219 return MSI_NULL_INTEGER;
224 return MSI_NULL_INTEGER;
227 int WINAPI MsiRecordGetInteger( MSIHANDLE handle, unsigned int iField)
232 TRACE("%ld %d\n", handle, iField );
234 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
236 return MSI_NULL_INTEGER;
238 msiobj_lock( &rec->hdr );
239 ret = MSI_RecordGetInteger( rec, iField );
240 msiobj_unlock( &rec->hdr );
241 msiobj_release( &rec->hdr );
246 UINT WINAPI MsiRecordClearData( MSIHANDLE handle )
251 TRACE("%ld\n", handle );
253 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
255 return ERROR_INVALID_HANDLE;
257 msiobj_lock( &rec->hdr );
258 for( i=0; i<=rec->count; i++)
260 MSI_FreeField( &rec->fields[i] );
261 rec->fields[i].type = MSIFIELD_NULL;
262 rec->fields[i].u.iVal = 0;
264 msiobj_unlock( &rec->hdr );
265 msiobj_release( &rec->hdr );
267 return ERROR_SUCCESS;
270 UINT MSI_RecordSetInteger( MSIRECORD *rec, unsigned int iField, int iVal )
272 TRACE("%p %u %d\n", rec, iField, iVal);
274 if( iField > rec->count )
275 return ERROR_INVALID_PARAMETER;
277 MSI_FreeField( &rec->fields[iField] );
278 rec->fields[iField].type = MSIFIELD_INT;
279 rec->fields[iField].u.iVal = iVal;
281 return ERROR_SUCCESS;
284 UINT WINAPI MsiRecordSetInteger( MSIHANDLE handle, unsigned int iField, int iVal )
289 TRACE("%ld %u %d\n", handle, iField, iVal);
291 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
293 return ERROR_INVALID_HANDLE;
295 msiobj_lock( &rec->hdr );
296 ret = MSI_RecordSetInteger( rec, iField, iVal );
297 msiobj_unlock( &rec->hdr );
298 msiobj_release( &rec->hdr );
302 BOOL MSI_RecordIsNull( MSIRECORD *rec, unsigned int iField )
306 TRACE("%p %d\n", rec, iField );
308 r = ( iField > rec->count ) ||
309 ( rec->fields[iField].type == MSIFIELD_NULL );
314 BOOL WINAPI MsiRecordIsNull( MSIHANDLE handle, unsigned int iField )
319 TRACE("%ld %d\n", handle, iField );
321 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
324 msiobj_lock( &rec->hdr );
325 ret = MSI_RecordIsNull( rec, iField );
326 msiobj_unlock( &rec->hdr );
327 msiobj_release( &rec->hdr );
332 UINT MSI_RecordGetStringA(MSIRECORD *rec, unsigned int iField,
333 LPSTR szValue, DWORD *pcchValue)
338 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
340 if( iField > rec->count )
341 return ERROR_INVALID_PARAMETER;
344 switch( rec->fields[iField].type )
347 wsprintfA(buffer, "%d", rec->fields[iField].u.iVal);
348 len = lstrlenA( buffer );
349 lstrcpynA(szValue, buffer, *pcchValue);
352 len = WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
353 NULL, 0 , NULL, NULL);
354 WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
355 szValue, *pcchValue, NULL, NULL);
356 if( szValue && *pcchValue && len>*pcchValue )
357 szValue[*pcchValue-1] = 0;
366 ret = ERROR_INVALID_PARAMETER;
370 if( szValue && *pcchValue <= len )
371 ret = ERROR_MORE_DATA;
377 UINT WINAPI MsiRecordGetStringA(MSIHANDLE handle, unsigned int iField,
378 LPSTR szValue, DWORD *pcchValue)
383 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
385 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
387 return ERROR_INVALID_HANDLE;
388 msiobj_lock( &rec->hdr );
389 ret = MSI_RecordGetStringA( rec, iField, szValue, pcchValue);
390 msiobj_unlock( &rec->hdr );
391 msiobj_release( &rec->hdr );
395 const WCHAR *MSI_RecordGetString( MSIRECORD *rec, unsigned int iField )
397 if( iField > rec->count )
400 if( rec->fields[iField].type != MSIFIELD_WSTR )
403 return rec->fields[iField].u.szwVal;
406 UINT MSI_RecordGetStringW(MSIRECORD *rec, unsigned int iField,
407 LPWSTR szValue, DWORD *pcchValue)
411 static const WCHAR szFormat[] = { '%','d',0 };
413 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
415 if( iField > rec->count )
416 return ERROR_INVALID_PARAMETER;
419 switch( rec->fields[iField].type )
422 wsprintfW(buffer, szFormat, rec->fields[iField].u.iVal);
423 len = lstrlenW( buffer );
424 lstrcpynW(szValue, buffer, *pcchValue);
427 len = lstrlenW( rec->fields[iField].u.szwVal );
428 lstrcpynW(szValue, rec->fields[iField].u.szwVal, *pcchValue);
432 if( szValue && *pcchValue > 0 )
438 if( szValue && *pcchValue <= len )
439 ret = ERROR_MORE_DATA;
445 UINT WINAPI MsiRecordGetStringW(MSIHANDLE handle, unsigned int iField,
446 LPWSTR szValue, DWORD *pcchValue)
451 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
453 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
455 return ERROR_INVALID_HANDLE;
457 msiobj_lock( &rec->hdr );
458 ret = MSI_RecordGetStringW( rec, iField, szValue, pcchValue );
459 msiobj_unlock( &rec->hdr );
460 msiobj_release( &rec->hdr );
464 static UINT msi_get_stream_size( IStream *stm )
469 r = IStream_Stat( stm, &stat, STATFLAG_NONAME );
472 return stat.cbSize.QuadPart;
475 UINT MSI_RecordDataSize(MSIRECORD *rec, unsigned int iField)
477 TRACE("%p %d\n", rec, iField);
479 if( iField > rec->count )
482 switch( rec->fields[iField].type )
487 return lstrlenW( rec->fields[iField].u.szwVal );
490 case MSIFIELD_STREAM:
491 return msi_get_stream_size( rec->fields[iField].u.stream );
496 UINT WINAPI MsiRecordDataSize(MSIHANDLE handle, unsigned int iField)
501 TRACE("%ld %d\n", handle, iField);
503 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
506 msiobj_lock( &rec->hdr );
507 ret = MSI_RecordDataSize( rec, iField);
508 msiobj_unlock( &rec->hdr );
509 msiobj_release( &rec->hdr );
513 UINT MSI_RecordSetStringA( MSIRECORD *rec, unsigned int iField, LPCSTR szValue )
517 TRACE("%p %d %s\n", rec, iField, debugstr_a(szValue));
519 if( iField > rec->count )
520 return ERROR_INVALID_FIELD;
522 MSI_FreeField( &rec->fields[iField] );
523 if( szValue && szValue[0] )
525 str = strdupAtoW( szValue );
526 rec->fields[iField].type = MSIFIELD_WSTR;
527 rec->fields[iField].u.szwVal = str;
531 rec->fields[iField].type = MSIFIELD_NULL;
532 rec->fields[iField].u.szwVal = NULL;
538 UINT WINAPI MsiRecordSetStringA( MSIHANDLE handle, unsigned int iField, LPCSTR szValue )
543 TRACE("%ld %d %s\n", handle, iField, debugstr_a(szValue));
545 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
547 return ERROR_INVALID_HANDLE;
548 msiobj_lock( &rec->hdr );
549 ret = MSI_RecordSetStringA( rec, iField, szValue );
550 msiobj_unlock( &rec->hdr );
551 msiobj_release( &rec->hdr );
555 UINT MSI_RecordSetStringW( MSIRECORD *rec, unsigned int iField, LPCWSTR szValue )
559 TRACE("%p %d %s\n", rec, iField, debugstr_w(szValue));
561 if( iField > rec->count )
562 return ERROR_INVALID_FIELD;
564 MSI_FreeField( &rec->fields[iField] );
566 if( szValue && szValue[0] )
568 str = strdupW( szValue );
569 rec->fields[iField].type = MSIFIELD_WSTR;
570 rec->fields[iField].u.szwVal = str;
574 rec->fields[iField].type = MSIFIELD_NULL;
575 rec->fields[iField].u.szwVal = NULL;
581 UINT WINAPI MsiRecordSetStringW( MSIHANDLE handle, unsigned int iField, LPCWSTR szValue )
586 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szValue));
588 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
590 return ERROR_INVALID_HANDLE;
592 msiobj_lock( &rec->hdr );
593 ret = MSI_RecordSetStringW( rec, iField, szValue );
594 msiobj_unlock( &rec->hdr );
595 msiobj_release( &rec->hdr );
599 /* read the data in a file into an IStream */
600 static UINT RECORD_StreamFromFile(LPCWSTR szFile, IStream **pstm)
602 DWORD sz, szHighWord = 0, read;
606 ULARGE_INTEGER ulSize;
608 TRACE("reading %s\n", debugstr_w(szFile));
610 /* read the file into memory */
611 handle = CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
612 if( handle == INVALID_HANDLE_VALUE )
613 return GetLastError();
614 sz = GetFileSize(handle, &szHighWord);
615 if( sz != INVALID_FILE_SIZE && szHighWord == 0 )
617 hGlob = GlobalAlloc(GMEM_FIXED, sz);
620 BOOL r = ReadFile(handle, hGlob, sz, &read, NULL);
630 return ERROR_FUNCTION_FAILED;
632 /* make a stream out of it, and set the correct file size */
633 hr = CreateStreamOnHGlobal(hGlob, TRUE, pstm);
637 return ERROR_FUNCTION_FAILED;
640 /* set the correct size - CreateStreamOnHGlobal screws it up */
641 ulSize.QuadPart = sz;
642 IStream_SetSize(*pstm, ulSize);
644 TRACE("read %s, %d bytes into IStream %p\n", debugstr_w(szFile), sz, *pstm);
646 return ERROR_SUCCESS;
649 UINT MSI_RecordSetStreamW(MSIRECORD *rec, unsigned int iField, LPCWSTR szFilename)
654 if( (iField == 0) || (iField > rec->count) )
655 return ERROR_INVALID_PARAMETER;
657 /* no filename means we should seek back to the start of the stream */
663 if( rec->fields[iField].type != MSIFIELD_STREAM )
664 return ERROR_INVALID_FIELD;
666 stm = rec->fields[iField].u.stream;
668 return ERROR_INVALID_FIELD;
671 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
673 return ERROR_FUNCTION_FAILED;
677 /* read the file into a stream and save the stream in the record */
678 r = RECORD_StreamFromFile(szFilename, &stm);
679 if( r != ERROR_SUCCESS )
682 /* if all's good, store it in the record */
683 MSI_FreeField( &rec->fields[iField] );
684 rec->fields[iField].type = MSIFIELD_STREAM;
685 rec->fields[iField].u.stream = stm;
688 return ERROR_SUCCESS;
691 UINT WINAPI MsiRecordSetStreamA(MSIHANDLE hRecord, unsigned int iField, LPCSTR szFilename)
696 TRACE("%ld %d %s\n", hRecord, iField, debugstr_a(szFilename));
700 wstr = strdupAtoW( szFilename );
702 return ERROR_OUTOFMEMORY;
704 ret = MsiRecordSetStreamW(hRecord, iField, wstr);
710 UINT WINAPI MsiRecordSetStreamW(MSIHANDLE handle, unsigned int iField, LPCWSTR szFilename)
715 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szFilename));
717 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
719 return ERROR_INVALID_HANDLE;
721 msiobj_lock( &rec->hdr );
722 ret = MSI_RecordSetStreamW( rec, iField, szFilename );
723 msiobj_unlock( &rec->hdr );
724 msiobj_release( &rec->hdr );
728 UINT MSI_RecordReadStream(MSIRECORD *rec, unsigned int iField, char *buf, DWORD *sz)
734 TRACE("%p %d %p %p\n", rec, iField, buf, sz);
737 return ERROR_INVALID_PARAMETER;
739 if( iField > rec->count)
740 return ERROR_INVALID_PARAMETER;
742 if( rec->fields[iField].type != MSIFIELD_STREAM )
743 return ERROR_INVALID_DATATYPE;
745 stm = rec->fields[iField].u.stream;
747 return ERROR_INVALID_PARAMETER;
749 /* if there's no buffer pointer, calculate the length to the end */
753 ULARGE_INTEGER end, cur;
755 ofs.QuadPart = cur.QuadPart = 0;
757 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
758 IStream_Seek( stm, ofs, STREAM_SEEK_END, &end );
759 ofs.QuadPart = cur.QuadPart;
760 IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
761 *sz = end.QuadPart - cur.QuadPart;
763 return ERROR_SUCCESS;
768 r = IStream_Read( stm, buf, *sz, &count );
772 return ERROR_FUNCTION_FAILED;
777 return ERROR_SUCCESS;
780 UINT WINAPI MsiRecordReadStream(MSIHANDLE handle, unsigned int iField, char *buf, DWORD *sz)
785 TRACE("%ld %d %p %p\n", handle, iField, buf, sz);
787 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
789 return ERROR_INVALID_HANDLE;
790 msiobj_lock( &rec->hdr );
791 ret = MSI_RecordReadStream( rec, iField, buf, sz );
792 msiobj_unlock( &rec->hdr );
793 msiobj_release( &rec->hdr );
797 UINT MSI_RecordSetIStream( MSIRECORD *rec, unsigned int iField, IStream *stm )
799 TRACE("%p %d %p\n", rec, iField, stm);
801 if( iField > rec->count )
802 return ERROR_INVALID_FIELD;
804 MSI_FreeField( &rec->fields[iField] );
806 rec->fields[iField].type = MSIFIELD_STREAM;
807 rec->fields[iField].u.stream = stm;
808 IStream_AddRef( stm );
810 return ERROR_SUCCESS;
813 UINT MSI_RecordGetIStream( MSIRECORD *rec, unsigned int iField, IStream **pstm)
815 TRACE("%p %d %p\n", rec, iField, pstm);
817 if( iField > rec->count )
818 return ERROR_INVALID_FIELD;
820 if( rec->fields[iField].type != MSIFIELD_STREAM )
821 return ERROR_INVALID_FIELD;
823 *pstm = rec->fields[iField].u.stream;
824 IStream_AddRef( *pstm );
826 return ERROR_SUCCESS;
829 static UINT msi_dump_stream_to_file( IStream *stm, LPCWSTR name )
837 stgm = STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_FAILIFTHERE;
838 r = SHCreateStreamOnFileW( name, stgm, &out );
840 return ERROR_FUNCTION_FAILED;
843 r = IStream_Seek( stm, pos, STREAM_SEEK_END, &size );
848 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
852 r = IStream_CopyTo( stm, out, size, NULL, NULL );
855 IStream_Release( out );
857 return ERROR_FUNCTION_FAILED;
858 return ERROR_SUCCESS;
861 UINT MSI_RecordStreamToFile( MSIRECORD *rec, unsigned int iField, LPCWSTR name )
866 TRACE("%p %u %s\n", rec, iField, debugstr_w(name));
868 msiobj_lock( &rec->hdr );
870 r = MSI_RecordGetIStream( rec, iField, &stm );
871 if( r == ERROR_SUCCESS )
873 r = msi_dump_stream_to_file( stm, name );
874 IStream_Release( stm );
877 msiobj_unlock( &rec->hdr );