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( const 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
);
350 lstrcpynA(szValue
, buffer
, *pcchValue
);
353 len
= WideCharToMultiByte( CP_ACP
, 0, rec
->fields
[iField
].u
.szwVal
, -1,
354 NULL
, 0 , NULL
, NULL
);
355 WideCharToMultiByte( CP_ACP
, 0, rec
->fields
[iField
].u
.szwVal
, -1,
356 szValue
, *pcchValue
, NULL
, NULL
);
357 if( szValue
&& *pcchValue
&& len
>*pcchValue
)
358 szValue
[*pcchValue
-1] = 0;
367 ret
= ERROR_INVALID_PARAMETER
;
371 if( szValue
&& *pcchValue
<= len
)
372 ret
= ERROR_MORE_DATA
;
378 UINT WINAPI
MsiRecordGetStringA(MSIHANDLE handle
, unsigned int iField
,
379 LPSTR szValue
, DWORD
*pcchValue
)
384 TRACE("%ld %d %p %p\n", handle
, iField
, szValue
, pcchValue
);
386 rec
= msihandle2msiinfo( handle
, MSIHANDLETYPE_RECORD
);
388 return ERROR_INVALID_HANDLE
;
389 msiobj_lock( &rec
->hdr
);
390 ret
= MSI_RecordGetStringA( rec
, iField
, szValue
, pcchValue
);
391 msiobj_unlock( &rec
->hdr
);
392 msiobj_release( &rec
->hdr
);
396 const WCHAR
*MSI_RecordGetString( const MSIRECORD
*rec
, unsigned int iField
)
398 if( iField
> rec
->count
)
401 if( rec
->fields
[iField
].type
!= MSIFIELD_WSTR
)
404 return rec
->fields
[iField
].u
.szwVal
;
407 UINT
MSI_RecordGetStringW(MSIRECORD
*rec
, unsigned int iField
,
408 LPWSTR szValue
, DWORD
*pcchValue
)
412 static const WCHAR szFormat
[] = { '%','d',0 };
414 TRACE("%p %d %p %p\n", rec
, iField
, szValue
, pcchValue
);
416 if( iField
> rec
->count
)
417 return ERROR_INVALID_PARAMETER
;
420 switch( rec
->fields
[iField
].type
)
423 wsprintfW(buffer
, szFormat
, rec
->fields
[iField
].u
.iVal
);
424 len
= lstrlenW( buffer
);
426 lstrcpynW(szValue
, buffer
, *pcchValue
);
429 len
= lstrlenW( rec
->fields
[iField
].u
.szwVal
);
431 lstrcpynW(szValue
, rec
->fields
[iField
].u
.szwVal
, *pcchValue
);
435 if( szValue
&& *pcchValue
> 0 )
441 if( szValue
&& *pcchValue
<= len
)
442 ret
= ERROR_MORE_DATA
;
448 UINT WINAPI
MsiRecordGetStringW(MSIHANDLE handle
, unsigned int iField
,
449 LPWSTR szValue
, DWORD
*pcchValue
)
454 TRACE("%ld %d %p %p\n", handle
, iField
, szValue
, pcchValue
);
456 rec
= msihandle2msiinfo( handle
, MSIHANDLETYPE_RECORD
);
458 return ERROR_INVALID_HANDLE
;
460 msiobj_lock( &rec
->hdr
);
461 ret
= MSI_RecordGetStringW( rec
, iField
, szValue
, pcchValue
);
462 msiobj_unlock( &rec
->hdr
);
463 msiobj_release( &rec
->hdr
);
467 static UINT
msi_get_stream_size( IStream
*stm
)
472 r
= IStream_Stat( stm
, &stat
, STATFLAG_NONAME
);
475 return stat
.cbSize
.QuadPart
;
478 UINT
MSI_RecordDataSize(MSIRECORD
*rec
, unsigned int iField
)
480 TRACE("%p %d\n", rec
, iField
);
482 if( iField
> rec
->count
)
485 switch( rec
->fields
[iField
].type
)
490 return lstrlenW( rec
->fields
[iField
].u
.szwVal
);
493 case MSIFIELD_STREAM
:
494 return msi_get_stream_size( rec
->fields
[iField
].u
.stream
);
499 UINT WINAPI
MsiRecordDataSize(MSIHANDLE handle
, unsigned int iField
)
504 TRACE("%ld %d\n", handle
, iField
);
506 rec
= msihandle2msiinfo( handle
, MSIHANDLETYPE_RECORD
);
509 msiobj_lock( &rec
->hdr
);
510 ret
= MSI_RecordDataSize( rec
, iField
);
511 msiobj_unlock( &rec
->hdr
);
512 msiobj_release( &rec
->hdr
);
516 UINT
MSI_RecordSetStringA( MSIRECORD
*rec
, unsigned int iField
, LPCSTR szValue
)
520 TRACE("%p %d %s\n", rec
, iField
, debugstr_a(szValue
));
522 if( iField
> rec
->count
)
523 return ERROR_INVALID_FIELD
;
525 MSI_FreeField( &rec
->fields
[iField
] );
526 if( szValue
&& szValue
[0] )
528 str
= strdupAtoW( szValue
);
529 rec
->fields
[iField
].type
= MSIFIELD_WSTR
;
530 rec
->fields
[iField
].u
.szwVal
= str
;
534 rec
->fields
[iField
].type
= MSIFIELD_NULL
;
535 rec
->fields
[iField
].u
.szwVal
= NULL
;
541 UINT WINAPI
MsiRecordSetStringA( MSIHANDLE handle
, unsigned int iField
, LPCSTR szValue
)
546 TRACE("%ld %d %s\n", handle
, iField
, debugstr_a(szValue
));
548 rec
= msihandle2msiinfo( handle
, MSIHANDLETYPE_RECORD
);
550 return ERROR_INVALID_HANDLE
;
551 msiobj_lock( &rec
->hdr
);
552 ret
= MSI_RecordSetStringA( rec
, iField
, szValue
);
553 msiobj_unlock( &rec
->hdr
);
554 msiobj_release( &rec
->hdr
);
558 UINT
MSI_RecordSetStringW( MSIRECORD
*rec
, unsigned int iField
, LPCWSTR szValue
)
562 TRACE("%p %d %s\n", rec
, iField
, debugstr_w(szValue
));
564 if( iField
> rec
->count
)
565 return ERROR_INVALID_FIELD
;
567 MSI_FreeField( &rec
->fields
[iField
] );
569 if( szValue
&& szValue
[0] )
571 str
= strdupW( szValue
);
572 rec
->fields
[iField
].type
= MSIFIELD_WSTR
;
573 rec
->fields
[iField
].u
.szwVal
= str
;
577 rec
->fields
[iField
].type
= MSIFIELD_NULL
;
578 rec
->fields
[iField
].u
.szwVal
= NULL
;
584 UINT WINAPI
MsiRecordSetStringW( MSIHANDLE handle
, unsigned int iField
, LPCWSTR szValue
)
589 TRACE("%ld %d %s\n", handle
, iField
, debugstr_w(szValue
));
591 rec
= msihandle2msiinfo( handle
, MSIHANDLETYPE_RECORD
);
593 return ERROR_INVALID_HANDLE
;
595 msiobj_lock( &rec
->hdr
);
596 ret
= MSI_RecordSetStringW( rec
, iField
, szValue
);
597 msiobj_unlock( &rec
->hdr
);
598 msiobj_release( &rec
->hdr
);
602 /* read the data in a file into an IStream */
603 static UINT
RECORD_StreamFromFile(LPCWSTR szFile
, IStream
**pstm
)
605 DWORD sz
, szHighWord
= 0, read
;
609 ULARGE_INTEGER ulSize
;
611 TRACE("reading %s\n", debugstr_w(szFile
));
613 /* read the file into memory */
614 handle
= CreateFileW(szFile
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, 0, NULL
);
615 if( handle
== INVALID_HANDLE_VALUE
)
616 return GetLastError();
617 sz
= GetFileSize(handle
, &szHighWord
);
618 if( sz
!= INVALID_FILE_SIZE
&& szHighWord
== 0 )
620 hGlob
= GlobalAlloc(GMEM_FIXED
, sz
);
623 BOOL r
= ReadFile(handle
, hGlob
, sz
, &read
, NULL
);
633 return ERROR_FUNCTION_FAILED
;
635 /* make a stream out of it, and set the correct file size */
636 hr
= CreateStreamOnHGlobal(hGlob
, TRUE
, pstm
);
640 return ERROR_FUNCTION_FAILED
;
643 /* set the correct size - CreateStreamOnHGlobal screws it up */
644 ulSize
.QuadPart
= sz
;
645 IStream_SetSize(*pstm
, ulSize
);
647 TRACE("read %s, %d bytes into IStream %p\n", debugstr_w(szFile
), sz
, *pstm
);
649 return ERROR_SUCCESS
;
652 UINT
MSI_RecordSetStream(MSIRECORD
*rec
, unsigned int iField
, IStream
*stream
)
654 if ( (iField
== 0) || (iField
> rec
->count
) )
655 return ERROR_INVALID_PARAMETER
;
657 MSI_FreeField( &rec
->fields
[iField
] );
658 rec
->fields
[iField
].type
= MSIFIELD_STREAM
;
659 rec
->fields
[iField
].u
.stream
= stream
;
661 return ERROR_SUCCESS
;
664 UINT
MSI_RecordSetStreamFromFileW(MSIRECORD
*rec
, unsigned int iField
, LPCWSTR szFilename
)
669 if( (iField
== 0) || (iField
> rec
->count
) )
670 return ERROR_INVALID_PARAMETER
;
672 /* no filename means we should seek back to the start of the stream */
678 if( rec
->fields
[iField
].type
!= MSIFIELD_STREAM
)
679 return ERROR_INVALID_FIELD
;
681 stm
= rec
->fields
[iField
].u
.stream
;
683 return ERROR_INVALID_FIELD
;
686 r
= IStream_Seek( stm
, ofs
, STREAM_SEEK_SET
, &cur
);
688 return ERROR_FUNCTION_FAILED
;
692 /* read the file into a stream and save the stream in the record */
693 r
= RECORD_StreamFromFile(szFilename
, &stm
);
694 if( r
!= ERROR_SUCCESS
)
697 /* if all's good, store it in the record */
698 MSI_RecordSetStream(rec
, iField
, stm
);
701 return ERROR_SUCCESS
;
704 UINT WINAPI
MsiRecordSetStreamA(MSIHANDLE hRecord
, unsigned int iField
, LPCSTR szFilename
)
709 TRACE("%ld %d %s\n", hRecord
, iField
, debugstr_a(szFilename
));
713 wstr
= strdupAtoW( szFilename
);
715 return ERROR_OUTOFMEMORY
;
717 ret
= MsiRecordSetStreamW(hRecord
, iField
, wstr
);
723 UINT WINAPI
MsiRecordSetStreamW(MSIHANDLE handle
, unsigned int iField
, LPCWSTR szFilename
)
728 TRACE("%ld %d %s\n", handle
, iField
, debugstr_w(szFilename
));
730 rec
= msihandle2msiinfo( handle
, MSIHANDLETYPE_RECORD
);
732 return ERROR_INVALID_HANDLE
;
734 msiobj_lock( &rec
->hdr
);
735 ret
= MSI_RecordSetStreamFromFileW( rec
, iField
, szFilename
);
736 msiobj_unlock( &rec
->hdr
);
737 msiobj_release( &rec
->hdr
);
741 UINT
MSI_RecordReadStream(MSIRECORD
*rec
, unsigned int iField
, char *buf
, DWORD
*sz
)
747 TRACE("%p %d %p %p\n", rec
, iField
, buf
, sz
);
750 return ERROR_INVALID_PARAMETER
;
752 if( iField
> rec
->count
)
753 return ERROR_INVALID_PARAMETER
;
755 if( rec
->fields
[iField
].type
!= MSIFIELD_STREAM
)
756 return ERROR_INVALID_DATATYPE
;
758 stm
= rec
->fields
[iField
].u
.stream
;
760 return ERROR_INVALID_PARAMETER
;
762 /* if there's no buffer pointer, calculate the length to the end */
766 ULARGE_INTEGER end
, cur
;
768 ofs
.QuadPart
= cur
.QuadPart
= 0;
770 r
= IStream_Seek( stm
, ofs
, STREAM_SEEK_SET
, &cur
);
771 IStream_Seek( stm
, ofs
, STREAM_SEEK_END
, &end
);
772 ofs
.QuadPart
= cur
.QuadPart
;
773 IStream_Seek( stm
, ofs
, STREAM_SEEK_SET
, &cur
);
774 *sz
= end
.QuadPart
- cur
.QuadPart
;
776 return ERROR_SUCCESS
;
781 r
= IStream_Read( stm
, buf
, *sz
, &count
);
785 return ERROR_FUNCTION_FAILED
;
790 return ERROR_SUCCESS
;
793 UINT WINAPI
MsiRecordReadStream(MSIHANDLE handle
, unsigned int iField
, char *buf
, DWORD
*sz
)
798 TRACE("%ld %d %p %p\n", handle
, iField
, buf
, sz
);
800 rec
= msihandle2msiinfo( handle
, MSIHANDLETYPE_RECORD
);
802 return ERROR_INVALID_HANDLE
;
803 msiobj_lock( &rec
->hdr
);
804 ret
= MSI_RecordReadStream( rec
, iField
, buf
, sz
);
805 msiobj_unlock( &rec
->hdr
);
806 msiobj_release( &rec
->hdr
);
810 UINT
MSI_RecordSetIStream( MSIRECORD
*rec
, unsigned int iField
, IStream
*stm
)
812 TRACE("%p %d %p\n", rec
, iField
, stm
);
814 if( iField
> rec
->count
)
815 return ERROR_INVALID_FIELD
;
817 MSI_FreeField( &rec
->fields
[iField
] );
819 rec
->fields
[iField
].type
= MSIFIELD_STREAM
;
820 rec
->fields
[iField
].u
.stream
= stm
;
821 IStream_AddRef( stm
);
823 return ERROR_SUCCESS
;
826 UINT
MSI_RecordGetIStream( MSIRECORD
*rec
, unsigned int iField
, IStream
**pstm
)
828 TRACE("%p %d %p\n", rec
, iField
, pstm
);
830 if( iField
> rec
->count
)
831 return ERROR_INVALID_FIELD
;
833 if( rec
->fields
[iField
].type
!= MSIFIELD_STREAM
)
834 return ERROR_INVALID_FIELD
;
836 *pstm
= rec
->fields
[iField
].u
.stream
;
837 IStream_AddRef( *pstm
);
839 return ERROR_SUCCESS
;
842 static UINT
msi_dump_stream_to_file( IStream
*stm
, LPCWSTR name
)
850 stgm
= STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
| STGM_FAILIFTHERE
;
851 r
= SHCreateStreamOnFileW( name
, stgm
, &out
);
853 return ERROR_FUNCTION_FAILED
;
856 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_END
, &size
);
861 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
865 r
= IStream_CopyTo( stm
, out
, size
, NULL
, NULL
);
868 IStream_Release( out
);
870 return ERROR_FUNCTION_FAILED
;
871 return ERROR_SUCCESS
;
874 UINT
MSI_RecordStreamToFile( MSIRECORD
*rec
, unsigned int iField
, LPCWSTR name
)
879 TRACE("%p %u %s\n", rec
, iField
, debugstr_w(name
));
881 msiobj_lock( &rec
->hdr
);
883 r
= MSI_RecordGetIStream( rec
, iField
, &stm
);
884 if( r
== ERROR_SUCCESS
)
886 r
= msi_dump_stream_to_file( stm
, name
);
887 IStream_Release( stm
);
890 msiobj_unlock( &rec
->hdr
);