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 )
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] );
458 if( szValue && szValue[0] )
460 str = strdupAtoW( szValue );
461 rec->fields[iField].type = MSIFIELD_WSTR;
462 rec->fields[iField].u.szwVal = str;
466 rec->fields[iField].type = MSIFIELD_NULL;
467 rec->fields[iField].u.szwVal = NULL;
473 UINT WINAPI MsiRecordSetStringA( MSIHANDLE handle, unsigned int iField, LPCSTR szValue )
478 TRACE("%ld %d %s\n", handle, iField, debugstr_a(szValue));
480 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
482 return ERROR_INVALID_HANDLE;
483 msiobj_lock( &rec->hdr );
484 ret = MSI_RecordSetStringA( rec, iField, szValue );
485 msiobj_unlock( &rec->hdr );
486 msiobj_release( &rec->hdr );
490 UINT MSI_RecordSetStringW( MSIRECORD *rec, unsigned int iField, LPCWSTR szValue )
494 TRACE("%p %d %s\n", rec, iField, debugstr_w(szValue));
496 if( iField > rec->count )
497 return ERROR_INVALID_FIELD;
499 MSI_FreeField( &rec->fields[iField] );
501 if( szValue && szValue[0] )
503 str = strdupW( szValue );
504 rec->fields[iField].type = MSIFIELD_WSTR;
505 rec->fields[iField].u.szwVal = str;
509 rec->fields[iField].type = MSIFIELD_NULL;
510 rec->fields[iField].u.szwVal = NULL;
516 UINT WINAPI MsiRecordSetStringW( MSIHANDLE handle, unsigned int iField, LPCWSTR szValue )
521 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szValue));
523 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
525 return ERROR_INVALID_HANDLE;
527 msiobj_lock( &rec->hdr );
528 ret = MSI_RecordSetStringW( rec, iField, szValue );
529 msiobj_unlock( &rec->hdr );
530 msiobj_release( &rec->hdr );
534 /* read the data in a file into an IStream */
535 UINT RECORD_StreamFromFile(LPCWSTR szFile, IStream **pstm)
537 DWORD sz, szHighWord = 0, read;
541 ULARGE_INTEGER ulSize;
543 TRACE("reading %s\n", debugstr_w(szFile));
545 /* read the file into memory */
546 handle = CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
547 if( handle == INVALID_HANDLE_VALUE )
548 return GetLastError();
549 sz = GetFileSize(handle, &szHighWord);
550 if( sz != INVALID_FILE_SIZE && szHighWord == 0 )
552 hGlob = GlobalAlloc(GMEM_FIXED, sz);
555 BOOL r = ReadFile(handle, hGlob, sz, &read, NULL);
565 return ERROR_FUNCTION_FAILED;
567 /* make a stream out of it, and set the correct file size */
568 hr = CreateStreamOnHGlobal(hGlob, TRUE, pstm);
572 return ERROR_FUNCTION_FAILED;
575 /* set the correct size - CreateStreamOnHGlobal screws it up */
576 ulSize.QuadPart = sz;
577 IStream_SetSize(*pstm, ulSize);
579 TRACE("read %s, %ld bytes into IStream %p\n", debugstr_w(szFile), sz, *pstm);
581 return ERROR_SUCCESS;
584 UINT MSI_RecordSetStreamW(MSIRECORD *rec, unsigned int iField, LPCWSTR szFilename)
589 if( (iField == 0) || (iField > rec->count) )
590 return ERROR_INVALID_PARAMETER;
592 /* no filename means we should seek back to the start of the stream */
598 if( rec->fields[iField].type != MSIFIELD_STREAM )
599 return ERROR_INVALID_FIELD;
601 stm = rec->fields[iField].u.stream;
603 return ERROR_INVALID_FIELD;
606 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
608 return ERROR_FUNCTION_FAILED;
612 /* read the file into a stream and save the stream in the record */
613 r = RECORD_StreamFromFile(szFilename, &stm);
614 if( r != ERROR_SUCCESS )
617 /* if all's good, store it in the record */
618 MSI_FreeField( &rec->fields[iField] );
619 rec->fields[iField].type = MSIFIELD_STREAM;
620 rec->fields[iField].u.stream = stm;
623 return ERROR_SUCCESS;
626 UINT WINAPI MsiRecordSetStreamA(MSIHANDLE hRecord, unsigned int iField, LPCSTR szFilename)
631 TRACE("%ld %d %s\n", hRecord, iField, debugstr_a(szFilename));
635 wstr = strdupAtoW( szFilename );
637 return ERROR_OUTOFMEMORY;
639 ret = MsiRecordSetStreamW(hRecord, iField, wstr);
640 HeapFree(GetProcessHeap(),0,wstr);
645 UINT WINAPI MsiRecordSetStreamW(MSIHANDLE handle, unsigned int iField, LPCWSTR szFilename)
650 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szFilename));
652 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
654 return ERROR_INVALID_HANDLE;
656 msiobj_lock( &rec->hdr );
657 ret = MSI_RecordSetStreamW( rec, iField, szFilename );
658 msiobj_unlock( &rec->hdr );
659 msiobj_release( &rec->hdr );
663 UINT MSI_RecordReadStream(MSIRECORD *rec, unsigned int iField, char *buf, DWORD *sz)
669 TRACE("%p %d %p %p\n", rec, iField, buf, sz);
672 return ERROR_INVALID_PARAMETER;
674 if( iField > rec->count)
675 return ERROR_INVALID_PARAMETER;
677 if( rec->fields[iField].type != MSIFIELD_STREAM )
678 return ERROR_INVALID_DATATYPE;
680 stm = rec->fields[iField].u.stream;
682 return ERROR_INVALID_PARAMETER;
684 /* if there's no buffer pointer, calculate the length to the end */
688 ULARGE_INTEGER end, cur;
690 ofs.QuadPart = cur.QuadPart = 0;
692 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
693 IStream_Seek( stm, ofs, STREAM_SEEK_END, &end );
694 ofs.QuadPart = cur.QuadPart;
695 IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
696 *sz = end.QuadPart - cur.QuadPart;
698 return ERROR_SUCCESS;
703 r = IStream_Read( stm, buf, *sz, &count );
707 return ERROR_FUNCTION_FAILED;
712 return ERROR_SUCCESS;
715 UINT WINAPI MsiRecordReadStream(MSIHANDLE handle, unsigned int iField, char *buf, DWORD *sz)
720 TRACE("%ld %d %p %p\n", handle, iField, buf, sz);
722 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
724 return ERROR_INVALID_HANDLE;
725 msiobj_lock( &rec->hdr );
726 ret = MSI_RecordReadStream( rec, iField, buf, sz );
727 msiobj_unlock( &rec->hdr );
728 msiobj_release( &rec->hdr );
732 UINT MSI_RecordSetIStream( MSIRECORD *rec, unsigned int iField, IStream *stm )
734 TRACE("%p %d %p\n", rec, iField, stm);
736 if( iField > rec->count )
737 return ERROR_INVALID_FIELD;
739 MSI_FreeField( &rec->fields[iField] );
741 rec->fields[iField].type = MSIFIELD_STREAM;
742 rec->fields[iField].u.stream = stm;
743 IStream_AddRef( stm );
745 return ERROR_SUCCESS;
748 UINT MSI_RecordGetIStream( MSIRECORD *rec, unsigned int iField, IStream **pstm)
750 TRACE("%p %d %p\n", rec, iField, pstm);
752 if( iField > rec->count )
753 return ERROR_INVALID_FIELD;
755 if( rec->fields[iField].type != MSIFIELD_STREAM )
756 return ERROR_INVALID_FIELD;
758 *pstm = rec->fields[iField].u.stream;
759 IStream_AddRef( *pstm );
761 return ERROR_SUCCESS;