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
);
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( 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_RecordSetStreamW(MSIRECORD
*rec
, unsigned int iField
, LPCWSTR szFilename
)
657 if( (iField
== 0) || (iField
> rec
->count
) )
658 return ERROR_INVALID_PARAMETER
;
660 /* no filename means we should seek back to the start of the stream */
666 if( rec
->fields
[iField
].type
!= MSIFIELD_STREAM
)
667 return ERROR_INVALID_FIELD
;
669 stm
= rec
->fields
[iField
].u
.stream
;
671 return ERROR_INVALID_FIELD
;
674 r
= IStream_Seek( stm
, ofs
, STREAM_SEEK_SET
, &cur
);
676 return ERROR_FUNCTION_FAILED
;
680 /* read the file into a stream and save the stream in the record */
681 r
= RECORD_StreamFromFile(szFilename
, &stm
);
682 if( r
!= ERROR_SUCCESS
)
685 /* if all's good, store it in the record */
686 MSI_FreeField( &rec
->fields
[iField
] );
687 rec
->fields
[iField
].type
= MSIFIELD_STREAM
;
688 rec
->fields
[iField
].u
.stream
= stm
;
691 return ERROR_SUCCESS
;
694 UINT WINAPI
MsiRecordSetStreamA(MSIHANDLE hRecord
, unsigned int iField
, LPCSTR szFilename
)
699 TRACE("%ld %d %s\n", hRecord
, iField
, debugstr_a(szFilename
));
703 wstr
= strdupAtoW( szFilename
);
705 return ERROR_OUTOFMEMORY
;
707 ret
= MsiRecordSetStreamW(hRecord
, iField
, wstr
);
713 UINT WINAPI
MsiRecordSetStreamW(MSIHANDLE handle
, unsigned int iField
, LPCWSTR szFilename
)
718 TRACE("%ld %d %s\n", handle
, iField
, debugstr_w(szFilename
));
720 rec
= msihandle2msiinfo( handle
, MSIHANDLETYPE_RECORD
);
722 return ERROR_INVALID_HANDLE
;
724 msiobj_lock( &rec
->hdr
);
725 ret
= MSI_RecordSetStreamW( rec
, iField
, szFilename
);
726 msiobj_unlock( &rec
->hdr
);
727 msiobj_release( &rec
->hdr
);
731 UINT
MSI_RecordReadStream(MSIRECORD
*rec
, unsigned int iField
, char *buf
, DWORD
*sz
)
737 TRACE("%p %d %p %p\n", rec
, iField
, buf
, sz
);
740 return ERROR_INVALID_PARAMETER
;
742 if( iField
> rec
->count
)
743 return ERROR_INVALID_PARAMETER
;
745 if( rec
->fields
[iField
].type
!= MSIFIELD_STREAM
)
746 return ERROR_INVALID_DATATYPE
;
748 stm
= rec
->fields
[iField
].u
.stream
;
750 return ERROR_INVALID_PARAMETER
;
752 /* if there's no buffer pointer, calculate the length to the end */
756 ULARGE_INTEGER end
, cur
;
758 ofs
.QuadPart
= cur
.QuadPart
= 0;
760 r
= IStream_Seek( stm
, ofs
, STREAM_SEEK_SET
, &cur
);
761 IStream_Seek( stm
, ofs
, STREAM_SEEK_END
, &end
);
762 ofs
.QuadPart
= cur
.QuadPart
;
763 IStream_Seek( stm
, ofs
, STREAM_SEEK_SET
, &cur
);
764 *sz
= end
.QuadPart
- cur
.QuadPart
;
766 return ERROR_SUCCESS
;
771 r
= IStream_Read( stm
, buf
, *sz
, &count
);
775 return ERROR_FUNCTION_FAILED
;
780 return ERROR_SUCCESS
;
783 UINT WINAPI
MsiRecordReadStream(MSIHANDLE handle
, unsigned int iField
, char *buf
, DWORD
*sz
)
788 TRACE("%ld %d %p %p\n", handle
, iField
, buf
, sz
);
790 rec
= msihandle2msiinfo( handle
, MSIHANDLETYPE_RECORD
);
792 return ERROR_INVALID_HANDLE
;
793 msiobj_lock( &rec
->hdr
);
794 ret
= MSI_RecordReadStream( rec
, iField
, buf
, sz
);
795 msiobj_unlock( &rec
->hdr
);
796 msiobj_release( &rec
->hdr
);
800 UINT
MSI_RecordSetIStream( MSIRECORD
*rec
, unsigned int iField
, IStream
*stm
)
802 TRACE("%p %d %p\n", rec
, iField
, stm
);
804 if( iField
> rec
->count
)
805 return ERROR_INVALID_FIELD
;
807 MSI_FreeField( &rec
->fields
[iField
] );
809 rec
->fields
[iField
].type
= MSIFIELD_STREAM
;
810 rec
->fields
[iField
].u
.stream
= stm
;
811 IStream_AddRef( stm
);
813 return ERROR_SUCCESS
;
816 UINT
MSI_RecordGetIStream( MSIRECORD
*rec
, unsigned int iField
, IStream
**pstm
)
818 TRACE("%p %d %p\n", rec
, iField
, pstm
);
820 if( iField
> rec
->count
)
821 return ERROR_INVALID_FIELD
;
823 if( rec
->fields
[iField
].type
!= MSIFIELD_STREAM
)
824 return ERROR_INVALID_FIELD
;
826 *pstm
= rec
->fields
[iField
].u
.stream
;
827 IStream_AddRef( *pstm
);
829 return ERROR_SUCCESS
;
832 static UINT
msi_dump_stream_to_file( IStream
*stm
, LPCWSTR name
)
840 stgm
= STGM_READWRITE
| STGM_SHARE_EXCLUSIVE
| STGM_FAILIFTHERE
;
841 r
= SHCreateStreamOnFileW( name
, stgm
, &out
);
843 return ERROR_FUNCTION_FAILED
;
846 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_END
, &size
);
851 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
855 r
= IStream_CopyTo( stm
, out
, size
, NULL
, NULL
);
858 IStream_Release( out
);
860 return ERROR_FUNCTION_FAILED
;
861 return ERROR_SUCCESS
;
864 UINT
MSI_RecordStreamToFile( MSIRECORD
*rec
, unsigned int iField
, LPCWSTR name
)
869 TRACE("%p %u %s\n", rec
, iField
, debugstr_w(name
));
871 msiobj_lock( &rec
->hdr
);
873 r
= MSI_RecordGetIStream( rec
, iField
, &stm
);
874 if( r
== ERROR_SUCCESS
)
876 r
= msi_dump_stream_to_file( stm
, name
);
877 IStream_Release( stm
);
880 msiobj_unlock( &rec
->hdr
);