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 );
101 msiobj_release( &rec->hdr );
105 unsigned int MSI_RecordGetFieldCount( MSIRECORD *rec )
110 unsigned int WINAPI MsiRecordGetFieldCount( MSIHANDLE handle )
115 TRACE("%ld\n", handle );
117 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
121 msiobj_lock( &rec->hdr );
122 ret = MSI_RecordGetFieldCount( rec );
123 msiobj_unlock( &rec->hdr );
124 msiobj_release( &rec->hdr );
129 static BOOL string2intW( LPCWSTR str, int *out )
134 if( *p == '-' ) /* skip the minus sign */
138 if( (*p < '0') || (*p > '9') )
145 if( str[0] == '-' ) /* check if it's negative */
152 int MSI_RecordGetInteger( MSIRECORD *rec, unsigned int iField)
156 TRACE("%p %d\n", rec, iField );
158 if( iField > rec->count )
159 return MSI_NULL_INTEGER;
161 switch( rec->fields[iField].type )
164 return rec->fields[iField].u.iVal;
166 if( string2intW( rec->fields[iField].u.szwVal, &ret ) )
168 return MSI_NULL_INTEGER;
173 return MSI_NULL_INTEGER;
176 int WINAPI MsiRecordGetInteger( MSIHANDLE handle, unsigned int iField)
181 TRACE("%ld %d\n", handle, iField );
183 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
185 return MSI_NULL_INTEGER;
187 msiobj_lock( &rec->hdr );
188 ret = MSI_RecordGetInteger( rec, iField );
189 msiobj_unlock( &rec->hdr );
190 msiobj_release( &rec->hdr );
195 UINT WINAPI MsiRecordClearData( MSIHANDLE handle )
200 TRACE("%ld\n", handle );
202 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
204 return ERROR_INVALID_HANDLE;
206 msiobj_lock( &rec->hdr );
207 for( i=0; i<=rec->count; i++)
209 MSI_FreeField( &rec->fields[i] );
210 rec->fields[i].type = MSIFIELD_NULL;
211 rec->fields[i].u.iVal = 0;
213 msiobj_unlock( &rec->hdr );
214 msiobj_release( &rec->hdr );
216 return ERROR_SUCCESS;
219 UINT MSI_RecordSetInteger( MSIRECORD *rec, unsigned int iField, int iVal )
221 TRACE("%p %u %d\n", rec, iField, iVal);
223 if( iField > rec->count )
224 return ERROR_INVALID_PARAMETER;
226 MSI_FreeField( &rec->fields[iField] );
227 rec->fields[iField].type = MSIFIELD_INT;
228 rec->fields[iField].u.iVal = iVal;
230 return ERROR_SUCCESS;
233 UINT WINAPI MsiRecordSetInteger( MSIHANDLE handle, unsigned int iField, int iVal )
238 TRACE("%ld %u %d\n", handle, iField, iVal);
240 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
242 return ERROR_INVALID_HANDLE;
244 msiobj_lock( &rec->hdr );
245 ret = MSI_RecordSetInteger( rec, iField, iVal );
246 msiobj_unlock( &rec->hdr );
247 msiobj_release( &rec->hdr );
251 BOOL MSI_RecordIsNull( MSIRECORD *rec, unsigned int iField )
255 TRACE("%p %d\n", rec, iField );
257 r = ( iField > rec->count ) ||
258 ( rec->fields[iField].type == MSIFIELD_NULL );
263 BOOL WINAPI MsiRecordIsNull( MSIHANDLE handle, unsigned int iField )
268 TRACE("%ld %d\n", handle, iField );
270 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
273 msiobj_lock( &rec->hdr );
274 ret = MSI_RecordIsNull( rec, iField );
275 msiobj_unlock( &rec->hdr );
276 msiobj_release( &rec->hdr );
281 UINT MSI_RecordGetStringA(MSIRECORD *rec, unsigned int iField,
282 LPSTR szValue, DWORD *pcchValue)
287 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
289 if( iField > rec->count )
290 return ERROR_INVALID_PARAMETER;
293 switch( rec->fields[iField].type )
296 wsprintfA(buffer, "%d", rec->fields[iField].u.iVal);
297 len = lstrlenA( buffer );
298 lstrcpynA(szValue, buffer, *pcchValue);
301 len = WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
302 NULL, 0 , NULL, NULL);
303 WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
304 szValue, *pcchValue, NULL, NULL);
305 if( *pcchValue && len>*pcchValue )
306 szValue[*pcchValue-1] = 0;
315 ret = ERROR_INVALID_PARAMETER;
319 if( *pcchValue < len )
320 ret = ERROR_MORE_DATA;
326 UINT WINAPI MsiRecordGetStringA(MSIHANDLE handle, unsigned int iField,
327 LPSTR szValue, DWORD *pcchValue)
332 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
334 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
336 return ERROR_INVALID_HANDLE;
337 msiobj_lock( &rec->hdr );
338 ret = MSI_RecordGetStringA( rec, iField, szValue, pcchValue);
339 msiobj_unlock( &rec->hdr );
340 msiobj_release( &rec->hdr );
344 const WCHAR *MSI_RecordGetString( MSIRECORD *rec, unsigned int iField )
346 if( iField > rec->count )
349 if( rec->fields[iField].type != MSIFIELD_WSTR )
352 return rec->fields[iField].u.szwVal;
355 UINT MSI_RecordGetStringW(MSIRECORD *rec, unsigned int iField,
356 LPWSTR szValue, DWORD *pcchValue)
360 static const WCHAR szFormat[] = { '%','d',0 };
362 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
364 if( iField > rec->count )
365 return ERROR_INVALID_PARAMETER;
368 switch( rec->fields[iField].type )
371 wsprintfW(buffer, szFormat, rec->fields[iField].u.iVal);
372 len = lstrlenW( buffer );
373 lstrcpynW(szValue, buffer, *pcchValue);
376 len = lstrlenW( rec->fields[iField].u.szwVal );
377 lstrcpynW(szValue, rec->fields[iField].u.szwVal, *pcchValue);
387 if( *pcchValue < len )
388 ret = ERROR_MORE_DATA;
394 UINT WINAPI MsiRecordGetStringW(MSIHANDLE handle, unsigned int iField,
395 LPWSTR szValue, DWORD *pcchValue)
400 TRACE("%ld %d %p %p\n", handle, iField, szValue, pcchValue);
402 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
404 return ERROR_INVALID_HANDLE;
406 msiobj_lock( &rec->hdr );
407 ret = MSI_RecordGetStringW( rec, iField, szValue, pcchValue );
408 msiobj_unlock( &rec->hdr );
409 msiobj_release( &rec->hdr );
413 UINT MSI_RecordDataSize(MSIRECORD *rec, unsigned int iField)
415 TRACE("%p %d\n", rec, iField);
417 if( iField > rec->count )
420 switch( rec->fields[iField].type )
425 return lstrlenW( rec->fields[iField].u.szwVal );
432 UINT WINAPI MsiRecordDataSize(MSIHANDLE handle, unsigned int iField)
437 TRACE("%ld %d\n", handle, iField);
439 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
442 msiobj_lock( &rec->hdr );
443 ret = MSI_RecordDataSize( rec, iField);
444 msiobj_unlock( &rec->hdr );
445 msiobj_release( &rec->hdr );
449 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 str = strdupAtoW( szValue );
462 rec->fields[iField].type = MSIFIELD_WSTR;
463 rec->fields[iField].u.szwVal = str;
467 rec->fields[iField].type = MSIFIELD_NULL;
468 rec->fields[iField].u.szwVal = NULL;
474 UINT WINAPI MsiRecordSetStringA( MSIHANDLE handle, unsigned int iField, LPCSTR szValue )
479 TRACE("%ld %d %s\n", handle, iField, debugstr_a(szValue));
481 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
483 return ERROR_INVALID_HANDLE;
484 msiobj_lock( &rec->hdr );
485 ret = MSI_RecordSetStringA( rec, iField, szValue );
486 msiobj_unlock( &rec->hdr );
487 msiobj_release( &rec->hdr );
491 UINT MSI_RecordSetStringW( MSIRECORD *rec, unsigned int iField, LPCWSTR szValue )
495 TRACE("%p %d %s\n", rec, iField, debugstr_w(szValue));
497 if( iField > rec->count )
498 return ERROR_INVALID_FIELD;
500 MSI_FreeField( &rec->fields[iField] );
502 if( szValue && szValue[0] )
504 str = strdupW( szValue );
505 rec->fields[iField].type = MSIFIELD_WSTR;
506 rec->fields[iField].u.szwVal = str;
510 rec->fields[iField].type = MSIFIELD_NULL;
511 rec->fields[iField].u.szwVal = NULL;
517 UINT WINAPI MsiRecordSetStringW( MSIHANDLE handle, unsigned int iField, LPCWSTR szValue )
522 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szValue));
524 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
526 return ERROR_INVALID_HANDLE;
528 msiobj_lock( &rec->hdr );
529 ret = MSI_RecordSetStringW( rec, iField, szValue );
530 msiobj_unlock( &rec->hdr );
531 msiobj_release( &rec->hdr );
535 /* read the data in a file into an IStream */
536 UINT RECORD_StreamFromFile(LPCWSTR szFile, IStream **pstm)
538 DWORD sz, szHighWord = 0, read;
542 ULARGE_INTEGER ulSize;
544 TRACE("reading %s\n", debugstr_w(szFile));
546 /* read the file into memory */
547 handle = CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
548 if( handle == INVALID_HANDLE_VALUE )
549 return GetLastError();
550 sz = GetFileSize(handle, &szHighWord);
551 if( sz != INVALID_FILE_SIZE && szHighWord == 0 )
553 hGlob = GlobalAlloc(GMEM_FIXED, sz);
556 BOOL r = ReadFile(handle, hGlob, sz, &read, NULL);
566 return ERROR_FUNCTION_FAILED;
568 /* make a stream out of it, and set the correct file size */
569 hr = CreateStreamOnHGlobal(hGlob, TRUE, pstm);
573 return ERROR_FUNCTION_FAILED;
576 /* set the correct size - CreateStreamOnHGlobal screws it up */
577 ulSize.QuadPart = sz;
578 IStream_SetSize(*pstm, ulSize);
580 TRACE("read %s, %ld bytes into IStream %p\n", debugstr_w(szFile), sz, *pstm);
582 return ERROR_SUCCESS;
585 UINT MSI_RecordSetStreamW(MSIRECORD *rec, unsigned int iField, LPCWSTR szFilename)
590 if( (iField == 0) || (iField > rec->count) )
591 return ERROR_INVALID_PARAMETER;
593 /* no filename means we should seek back to the start of the stream */
599 if( rec->fields[iField].type != MSIFIELD_STREAM )
600 return ERROR_INVALID_FIELD;
602 stm = rec->fields[iField].u.stream;
604 return ERROR_INVALID_FIELD;
607 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
609 return ERROR_FUNCTION_FAILED;
613 /* read the file into a stream and save the stream in the record */
614 r = RECORD_StreamFromFile(szFilename, &stm);
615 if( r != ERROR_SUCCESS )
618 /* if all's good, store it in the record */
619 MSI_FreeField( &rec->fields[iField] );
620 rec->fields[iField].type = MSIFIELD_STREAM;
621 rec->fields[iField].u.stream = stm;
624 return ERROR_SUCCESS;
627 UINT WINAPI MsiRecordSetStreamA(MSIHANDLE hRecord, unsigned int iField, LPCSTR szFilename)
632 TRACE("%ld %d %s\n", hRecord, iField, debugstr_a(szFilename));
636 wstr = strdupAtoW( szFilename );
638 return ERROR_OUTOFMEMORY;
640 ret = MsiRecordSetStreamW(hRecord, iField, wstr);
641 HeapFree(GetProcessHeap(),0,wstr);
646 UINT WINAPI MsiRecordSetStreamW(MSIHANDLE handle, unsigned int iField, LPCWSTR szFilename)
651 TRACE("%ld %d %s\n", handle, iField, debugstr_w(szFilename));
653 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
655 return ERROR_INVALID_HANDLE;
657 msiobj_lock( &rec->hdr );
658 ret = MSI_RecordSetStreamW( rec, iField, szFilename );
659 msiobj_unlock( &rec->hdr );
660 msiobj_release( &rec->hdr );
664 UINT MSI_RecordReadStream(MSIRECORD *rec, unsigned int iField, char *buf, DWORD *sz)
670 TRACE("%p %d %p %p\n", rec, iField, buf, sz);
673 return ERROR_INVALID_PARAMETER;
675 if( iField > rec->count)
676 return ERROR_INVALID_PARAMETER;
678 if( rec->fields[iField].type != MSIFIELD_STREAM )
679 return ERROR_INVALID_DATATYPE;
681 stm = rec->fields[iField].u.stream;
683 return ERROR_INVALID_PARAMETER;
685 /* if there's no buffer pointer, calculate the length to the end */
689 ULARGE_INTEGER end, cur;
691 ofs.QuadPart = cur.QuadPart = 0;
693 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
694 IStream_Seek( stm, ofs, STREAM_SEEK_END, &end );
695 ofs.QuadPart = cur.QuadPart;
696 IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
697 *sz = end.QuadPart - cur.QuadPart;
699 return ERROR_SUCCESS;
704 r = IStream_Read( stm, buf, *sz, &count );
708 return ERROR_FUNCTION_FAILED;
713 return ERROR_SUCCESS;
716 UINT WINAPI MsiRecordReadStream(MSIHANDLE handle, unsigned int iField, char *buf, DWORD *sz)
721 TRACE("%ld %d %p %p\n", handle, iField, buf, sz);
723 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
725 return ERROR_INVALID_HANDLE;
726 msiobj_lock( &rec->hdr );
727 ret = MSI_RecordReadStream( rec, iField, buf, sz );
728 msiobj_unlock( &rec->hdr );
729 msiobj_release( &rec->hdr );
733 UINT MSI_RecordSetIStream( MSIRECORD *rec, unsigned int iField, IStream *stm )
735 TRACE("%p %d %p\n", rec, iField, stm);
737 if( iField > rec->count )
738 return ERROR_INVALID_FIELD;
740 MSI_FreeField( &rec->fields[iField] );
742 rec->fields[iField].type = MSIFIELD_STREAM;
743 rec->fields[iField].u.stream = stm;
744 IStream_AddRef( stm );
746 return ERROR_SUCCESS;
749 UINT MSI_RecordGetIStream( MSIRECORD *rec, unsigned int iField, IStream **pstm)
751 TRACE("%p %d %p\n", rec, iField, pstm);
753 if( iField > rec->count )
754 return ERROR_INVALID_FIELD;
756 if( rec->fields[iField].type != MSIFIELD_STREAM )
757 return ERROR_INVALID_FIELD;
759 *pstm = rec->fields[iField].u.stream;
760 IStream_AddRef( *pstm );
762 return ERROR_SUCCESS;