winhttp/tests: Initialize a variant with a known value.
[wine/multimedia.git] / dlls / msi / record.c
blob7acbfc77b9a957920616920ec17ca34e5cfb584a
1 /*
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
21 #include <stdarg.h>
23 #define COBJMACROS
25 #include "windef.h"
26 #include "winbase.h"
27 #include "winuser.h"
28 #include "winerror.h"
29 #include "wine/debug.h"
30 #include "wine/unicode.h"
31 #include "msi.h"
32 #include "msiquery.h"
33 #include "msipriv.h"
34 #include "objidl.h"
35 #include "winnls.h"
36 #include "ole2.h"
38 #include "winreg.h"
39 #include "shlwapi.h"
41 #include "query.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
45 #define MSIFIELD_NULL 0
46 #define MSIFIELD_INT 1
47 #define MSIFIELD_WSTR 3
48 #define MSIFIELD_STREAM 4
49 #define MSIFIELD_INTPTR 5
51 static void MSI_FreeField( MSIFIELD *field )
53 switch( field->type )
55 case MSIFIELD_NULL:
56 case MSIFIELD_INT:
57 case MSIFIELD_INTPTR:
58 break;
59 case MSIFIELD_WSTR:
60 msi_free( field->u.szwVal);
61 break;
62 case MSIFIELD_STREAM:
63 IStream_Release( field->u.stream );
64 break;
65 default:
66 ERR("Invalid field type %d\n", field->type);
70 void MSI_CloseRecord( MSIOBJECTHDR *arg )
72 MSIRECORD *rec = (MSIRECORD *) arg;
73 UINT i;
75 for( i=0; i<=rec->count; i++ )
76 MSI_FreeField( &rec->fields[i] );
79 MSIRECORD *MSI_CreateRecord( UINT cParams )
81 MSIRECORD *rec;
82 UINT len;
84 TRACE("%d\n", cParams);
86 if( cParams>65535 )
87 return NULL;
89 len = sizeof (MSIRECORD) + sizeof (MSIFIELD)*cParams;
90 rec = alloc_msiobject( MSIHANDLETYPE_RECORD, len, MSI_CloseRecord );
91 if( rec )
92 rec->count = cParams;
93 return rec;
96 MSIHANDLE WINAPI MsiCreateRecord( UINT cParams )
98 MSIRECORD *rec;
99 MSIHANDLE ret = 0;
101 TRACE("%d\n", cParams);
103 rec = MSI_CreateRecord( cParams );
104 if( rec )
106 ret = alloc_msihandle( &rec->hdr );
107 msiobj_release( &rec->hdr );
109 return ret;
112 UINT MSI_RecordGetFieldCount( const MSIRECORD *rec )
114 return rec->count;
117 UINT WINAPI MsiRecordGetFieldCount( MSIHANDLE handle )
119 MSIRECORD *rec;
120 UINT ret;
122 TRACE("%d\n", handle );
124 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
125 if( !rec )
126 return -1;
128 msiobj_lock( &rec->hdr );
129 ret = MSI_RecordGetFieldCount( rec );
130 msiobj_unlock( &rec->hdr );
131 msiobj_release( &rec->hdr );
133 return ret;
136 static BOOL string2intW( LPCWSTR str, int *out )
138 int x = 0;
139 LPCWSTR p = str;
141 if( *p == '-' ) /* skip the minus sign */
142 p++;
143 while ( *p )
145 if( (*p < '0') || (*p > '9') )
146 return FALSE;
147 x *= 10;
148 x += (*p - '0');
149 p++;
152 if( str[0] == '-' ) /* check if it's negative */
153 x = -x;
154 *out = x;
156 return TRUE;
159 UINT MSI_RecordCopyField( MSIRECORD *in_rec, UINT in_n,
160 MSIRECORD *out_rec, UINT out_n )
162 UINT r = ERROR_SUCCESS;
164 msiobj_lock( &in_rec->hdr );
166 if ( in_n > in_rec->count || out_n > out_rec->count )
167 r = ERROR_FUNCTION_FAILED;
168 else if ( in_rec != out_rec || in_n != out_n )
170 LPWSTR str;
171 MSIFIELD *in, *out;
173 in = &in_rec->fields[in_n];
174 out = &out_rec->fields[out_n];
176 switch ( in->type )
178 case MSIFIELD_NULL:
179 break;
180 case MSIFIELD_INT:
181 out->u.iVal = in->u.iVal;
182 break;
183 case MSIFIELD_INTPTR:
184 out->u.pVal = in->u.pVal;
185 break;
186 case MSIFIELD_WSTR:
187 str = strdupW( in->u.szwVal );
188 if ( !str )
189 r = ERROR_OUTOFMEMORY;
190 else
191 out->u.szwVal = str;
192 break;
193 case MSIFIELD_STREAM:
194 IStream_AddRef( in->u.stream );
195 out->u.stream = in->u.stream;
196 break;
197 default:
198 ERR("invalid field type %d\n", in->type);
200 if (r == ERROR_SUCCESS)
201 out->type = in->type;
204 msiobj_unlock( &in_rec->hdr );
206 return r;
209 INT_PTR MSI_RecordGetIntPtr( MSIRECORD *rec, UINT iField )
211 int ret;
213 TRACE( "%p %d\n", rec, iField );
215 if( iField > rec->count )
216 return MININT_PTR;
218 switch( rec->fields[iField].type )
220 case MSIFIELD_INT:
221 return rec->fields[iField].u.iVal;
222 case MSIFIELD_INTPTR:
223 return rec->fields[iField].u.pVal;
224 case MSIFIELD_WSTR:
225 if( string2intW( rec->fields[iField].u.szwVal, &ret ) )
226 return ret;
227 return MININT_PTR;
228 default:
229 break;
232 return MININT_PTR;
235 int MSI_RecordGetInteger( MSIRECORD *rec, UINT iField)
237 int ret = 0;
239 TRACE("%p %d\n", rec, iField );
241 if( iField > rec->count )
242 return MSI_NULL_INTEGER;
244 switch( rec->fields[iField].type )
246 case MSIFIELD_INT:
247 return rec->fields[iField].u.iVal;
248 case MSIFIELD_INTPTR:
249 return rec->fields[iField].u.pVal;
250 case MSIFIELD_WSTR:
251 if( string2intW( rec->fields[iField].u.szwVal, &ret ) )
252 return ret;
253 return MSI_NULL_INTEGER;
254 default:
255 break;
258 return MSI_NULL_INTEGER;
261 int WINAPI MsiRecordGetInteger( MSIHANDLE handle, UINT iField)
263 MSIRECORD *rec;
264 UINT ret;
266 TRACE("%d %d\n", handle, iField );
268 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
269 if( !rec )
270 return MSI_NULL_INTEGER;
272 msiobj_lock( &rec->hdr );
273 ret = MSI_RecordGetInteger( rec, iField );
274 msiobj_unlock( &rec->hdr );
275 msiobj_release( &rec->hdr );
277 return ret;
280 UINT WINAPI MsiRecordClearData( MSIHANDLE handle )
282 MSIRECORD *rec;
283 UINT i;
285 TRACE("%d\n", handle );
287 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
288 if( !rec )
289 return ERROR_INVALID_HANDLE;
291 msiobj_lock( &rec->hdr );
292 for( i=0; i<=rec->count; i++)
294 MSI_FreeField( &rec->fields[i] );
295 rec->fields[i].type = MSIFIELD_NULL;
296 rec->fields[i].u.iVal = 0;
298 msiobj_unlock( &rec->hdr );
299 msiobj_release( &rec->hdr );
301 return ERROR_SUCCESS;
304 UINT MSI_RecordSetIntPtr( MSIRECORD *rec, UINT iField, INT_PTR pVal )
306 TRACE("%p %u %ld\n", rec, iField, pVal);
308 if( iField > rec->count )
309 return ERROR_INVALID_PARAMETER;
311 MSI_FreeField( &rec->fields[iField] );
312 rec->fields[iField].type = MSIFIELD_INTPTR;
313 rec->fields[iField].u.pVal = pVal;
315 return ERROR_SUCCESS;
318 UINT MSI_RecordSetInteger( MSIRECORD *rec, UINT iField, int iVal )
320 TRACE("%p %u %d\n", rec, iField, iVal);
322 if( iField > rec->count )
323 return ERROR_INVALID_PARAMETER;
325 MSI_FreeField( &rec->fields[iField] );
326 rec->fields[iField].type = MSIFIELD_INT;
327 rec->fields[iField].u.iVal = iVal;
329 return ERROR_SUCCESS;
332 UINT WINAPI MsiRecordSetInteger( MSIHANDLE handle, UINT iField, int iVal )
334 MSIRECORD *rec;
335 UINT ret;
337 TRACE("%d %u %d\n", handle, iField, iVal);
339 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
340 if( !rec )
341 return ERROR_INVALID_HANDLE;
343 msiobj_lock( &rec->hdr );
344 ret = MSI_RecordSetInteger( rec, iField, iVal );
345 msiobj_unlock( &rec->hdr );
346 msiobj_release( &rec->hdr );
347 return ret;
350 BOOL MSI_RecordIsNull( MSIRECORD *rec, UINT iField )
352 BOOL r = TRUE;
354 TRACE("%p %d\n", rec, iField );
356 r = ( iField > rec->count ) ||
357 ( rec->fields[iField].type == MSIFIELD_NULL );
359 return r;
362 BOOL WINAPI MsiRecordIsNull( MSIHANDLE handle, UINT iField )
364 MSIRECORD *rec;
365 UINT ret;
367 TRACE("%d %d\n", handle, iField );
369 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
370 if( !rec )
371 return 0;
372 msiobj_lock( &rec->hdr );
373 ret = MSI_RecordIsNull( rec, iField );
374 msiobj_unlock( &rec->hdr );
375 msiobj_release( &rec->hdr );
376 return ret;
380 UINT MSI_RecordGetStringA(MSIRECORD *rec, UINT iField,
381 LPSTR szValue, LPDWORD pcchValue)
383 UINT len=0, ret;
384 CHAR buffer[16];
386 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
388 if( iField > rec->count )
390 if ( szValue && *pcchValue > 0 )
391 szValue[0] = 0;
393 *pcchValue = 0;
394 return ERROR_SUCCESS;
397 ret = ERROR_SUCCESS;
398 switch( rec->fields[iField].type )
400 case MSIFIELD_INT:
401 wsprintfA(buffer, "%d", rec->fields[iField].u.iVal);
402 len = lstrlenA( buffer );
403 if (szValue)
404 lstrcpynA(szValue, buffer, *pcchValue);
405 break;
406 case MSIFIELD_WSTR:
407 len = WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
408 NULL, 0 , NULL, NULL);
409 if (szValue)
410 WideCharToMultiByte( CP_ACP, 0, rec->fields[iField].u.szwVal, -1,
411 szValue, *pcchValue, NULL, NULL);
412 if( szValue && *pcchValue && len>*pcchValue )
413 szValue[*pcchValue-1] = 0;
414 if( len )
415 len--;
416 break;
417 case MSIFIELD_NULL:
418 if( szValue && *pcchValue > 0 )
419 szValue[0] = 0;
420 break;
421 default:
422 ret = ERROR_INVALID_PARAMETER;
423 break;
426 if( szValue && *pcchValue <= len )
427 ret = ERROR_MORE_DATA;
428 *pcchValue = len;
430 return ret;
433 UINT WINAPI MsiRecordGetStringA(MSIHANDLE handle, UINT iField,
434 LPSTR szValue, LPDWORD pcchValue)
436 MSIRECORD *rec;
437 UINT ret;
439 TRACE("%d %d %p %p\n", handle, iField, szValue, pcchValue);
441 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
442 if( !rec )
443 return ERROR_INVALID_HANDLE;
444 msiobj_lock( &rec->hdr );
445 ret = MSI_RecordGetStringA( rec, iField, szValue, pcchValue);
446 msiobj_unlock( &rec->hdr );
447 msiobj_release( &rec->hdr );
448 return ret;
451 const WCHAR *MSI_RecordGetString( const MSIRECORD *rec, UINT iField )
453 if( iField > rec->count )
454 return NULL;
456 if( rec->fields[iField].type != MSIFIELD_WSTR )
457 return NULL;
459 return rec->fields[iField].u.szwVal;
462 UINT MSI_RecordGetStringW(MSIRECORD *rec, UINT iField,
463 LPWSTR szValue, LPDWORD pcchValue)
465 UINT len=0, ret;
466 WCHAR buffer[16];
467 static const WCHAR szFormat[] = { '%','d',0 };
469 TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
471 if( iField > rec->count )
473 if ( szValue && *pcchValue > 0 )
474 szValue[0] = 0;
476 *pcchValue = 0;
477 return ERROR_SUCCESS;
480 ret = ERROR_SUCCESS;
481 switch( rec->fields[iField].type )
483 case MSIFIELD_INT:
484 wsprintfW(buffer, szFormat, rec->fields[iField].u.iVal);
485 len = lstrlenW( buffer );
486 if (szValue)
487 lstrcpynW(szValue, buffer, *pcchValue);
488 break;
489 case MSIFIELD_WSTR:
490 len = lstrlenW( rec->fields[iField].u.szwVal );
491 if (szValue)
492 lstrcpynW(szValue, rec->fields[iField].u.szwVal, *pcchValue);
493 break;
494 case MSIFIELD_NULL:
495 if( szValue && *pcchValue > 0 )
496 szValue[0] = 0;
497 default:
498 break;
501 if( szValue && *pcchValue <= len )
502 ret = ERROR_MORE_DATA;
503 *pcchValue = len;
505 return ret;
508 UINT WINAPI MsiRecordGetStringW(MSIHANDLE handle, UINT iField,
509 LPWSTR szValue, LPDWORD pcchValue)
511 MSIRECORD *rec;
512 UINT ret;
514 TRACE("%d %d %p %p\n", handle, iField, szValue, pcchValue);
516 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
517 if( !rec )
518 return ERROR_INVALID_HANDLE;
520 msiobj_lock( &rec->hdr );
521 ret = MSI_RecordGetStringW( rec, iField, szValue, pcchValue );
522 msiobj_unlock( &rec->hdr );
523 msiobj_release( &rec->hdr );
524 return ret;
527 static UINT msi_get_stream_size( IStream *stm )
529 STATSTG stat;
530 HRESULT r;
532 r = IStream_Stat( stm, &stat, STATFLAG_NONAME );
533 if( FAILED(r) )
534 return 0;
535 return stat.cbSize.QuadPart;
538 static UINT MSI_RecordDataSize(MSIRECORD *rec, UINT iField)
540 TRACE("%p %d\n", rec, iField);
542 if( iField > rec->count )
543 return 0;
545 switch( rec->fields[iField].type )
547 case MSIFIELD_INT:
548 return sizeof (INT);
549 case MSIFIELD_WSTR:
550 return lstrlenW( rec->fields[iField].u.szwVal );
551 case MSIFIELD_NULL:
552 break;
553 case MSIFIELD_STREAM:
554 return msi_get_stream_size( rec->fields[iField].u.stream );
556 return 0;
559 UINT WINAPI MsiRecordDataSize(MSIHANDLE handle, UINT iField)
561 MSIRECORD *rec;
562 UINT ret;
564 TRACE("%d %d\n", handle, iField);
566 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
567 if( !rec )
568 return 0;
569 msiobj_lock( &rec->hdr );
570 ret = MSI_RecordDataSize( rec, iField);
571 msiobj_unlock( &rec->hdr );
572 msiobj_release( &rec->hdr );
573 return ret;
576 static UINT MSI_RecordSetStringA( MSIRECORD *rec, UINT iField, LPCSTR szValue )
578 LPWSTR str;
580 TRACE("%p %d %s\n", rec, iField, debugstr_a(szValue));
582 if( iField > rec->count )
583 return ERROR_INVALID_FIELD;
585 MSI_FreeField( &rec->fields[iField] );
586 if( szValue && szValue[0] )
588 str = strdupAtoW( szValue );
589 rec->fields[iField].type = MSIFIELD_WSTR;
590 rec->fields[iField].u.szwVal = str;
592 else
594 rec->fields[iField].type = MSIFIELD_NULL;
595 rec->fields[iField].u.szwVal = NULL;
598 return 0;
601 UINT WINAPI MsiRecordSetStringA( MSIHANDLE handle, UINT iField, LPCSTR szValue )
603 MSIRECORD *rec;
604 UINT ret;
606 TRACE("%d %d %s\n", handle, iField, debugstr_a(szValue));
608 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
609 if( !rec )
610 return ERROR_INVALID_HANDLE;
611 msiobj_lock( &rec->hdr );
612 ret = MSI_RecordSetStringA( rec, iField, szValue );
613 msiobj_unlock( &rec->hdr );
614 msiobj_release( &rec->hdr );
615 return ret;
618 UINT MSI_RecordSetStringW( MSIRECORD *rec, UINT iField, LPCWSTR szValue )
620 LPWSTR str;
622 TRACE("%p %d %s\n", rec, iField, debugstr_w(szValue));
624 if( iField > rec->count )
625 return ERROR_INVALID_FIELD;
627 MSI_FreeField( &rec->fields[iField] );
629 if( szValue && szValue[0] )
631 str = strdupW( szValue );
632 rec->fields[iField].type = MSIFIELD_WSTR;
633 rec->fields[iField].u.szwVal = str;
635 else
637 rec->fields[iField].type = MSIFIELD_NULL;
638 rec->fields[iField].u.szwVal = NULL;
641 return 0;
644 UINT WINAPI MsiRecordSetStringW( MSIHANDLE handle, UINT iField, LPCWSTR szValue )
646 MSIRECORD *rec;
647 UINT ret;
649 TRACE("%d %d %s\n", handle, iField, debugstr_w(szValue));
651 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
652 if( !rec )
653 return ERROR_INVALID_HANDLE;
655 msiobj_lock( &rec->hdr );
656 ret = MSI_RecordSetStringW( rec, iField, szValue );
657 msiobj_unlock( &rec->hdr );
658 msiobj_release( &rec->hdr );
659 return ret;
662 /* read the data in a file into an IStream */
663 static UINT RECORD_StreamFromFile(LPCWSTR szFile, IStream **pstm)
665 DWORD sz, szHighWord = 0, read;
666 HANDLE handle;
667 HGLOBAL hGlob = 0;
668 HRESULT hr;
669 ULARGE_INTEGER ulSize;
671 TRACE("reading %s\n", debugstr_w(szFile));
673 /* read the file into memory */
674 handle = CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
675 if( handle == INVALID_HANDLE_VALUE )
676 return GetLastError();
677 sz = GetFileSize(handle, &szHighWord);
678 if( sz != INVALID_FILE_SIZE && szHighWord == 0 )
680 hGlob = GlobalAlloc(GMEM_FIXED, sz);
681 if( hGlob )
683 BOOL r = ReadFile(handle, hGlob, sz, &read, NULL);
684 if( !r )
686 GlobalFree(hGlob);
687 hGlob = 0;
691 CloseHandle(handle);
692 if( !hGlob )
693 return ERROR_FUNCTION_FAILED;
695 /* make a stream out of it, and set the correct file size */
696 hr = CreateStreamOnHGlobal(hGlob, TRUE, pstm);
697 if( FAILED( hr ) )
699 GlobalFree(hGlob);
700 return ERROR_FUNCTION_FAILED;
703 /* set the correct size - CreateStreamOnHGlobal screws it up */
704 ulSize.QuadPart = sz;
705 IStream_SetSize(*pstm, ulSize);
707 TRACE("read %s, %d bytes into IStream %p\n", debugstr_w(szFile), sz, *pstm);
709 return ERROR_SUCCESS;
712 UINT MSI_RecordSetStream(MSIRECORD *rec, UINT iField, IStream *stream)
714 if ( (iField == 0) || (iField > rec->count) )
715 return ERROR_INVALID_PARAMETER;
717 MSI_FreeField( &rec->fields[iField] );
718 rec->fields[iField].type = MSIFIELD_STREAM;
719 rec->fields[iField].u.stream = stream;
721 return ERROR_SUCCESS;
724 UINT MSI_RecordSetStreamFromFileW(MSIRECORD *rec, UINT iField, LPCWSTR szFilename)
726 IStream *stm = NULL;
727 HRESULT r;
729 if( (iField == 0) || (iField > rec->count) )
730 return ERROR_INVALID_PARAMETER;
732 /* no filename means we should seek back to the start of the stream */
733 if( !szFilename )
735 LARGE_INTEGER ofs;
736 ULARGE_INTEGER cur;
738 if( rec->fields[iField].type != MSIFIELD_STREAM )
739 return ERROR_INVALID_FIELD;
741 stm = rec->fields[iField].u.stream;
742 if( !stm )
743 return ERROR_INVALID_FIELD;
745 ofs.QuadPart = 0;
746 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
747 if( FAILED( r ) )
748 return ERROR_FUNCTION_FAILED;
750 else
752 /* read the file into a stream and save the stream in the record */
753 r = RECORD_StreamFromFile(szFilename, &stm);
754 if( r != ERROR_SUCCESS )
755 return r;
757 /* if all's good, store it in the record */
758 MSI_RecordSetStream(rec, iField, stm);
761 return ERROR_SUCCESS;
764 UINT WINAPI MsiRecordSetStreamA(MSIHANDLE hRecord, UINT iField, LPCSTR szFilename)
766 LPWSTR wstr = NULL;
767 UINT ret;
769 TRACE("%d %d %s\n", hRecord, iField, debugstr_a(szFilename));
771 if( szFilename )
773 wstr = strdupAtoW( szFilename );
774 if( !wstr )
775 return ERROR_OUTOFMEMORY;
777 ret = MsiRecordSetStreamW(hRecord, iField, wstr);
778 msi_free(wstr);
780 return ret;
783 UINT WINAPI MsiRecordSetStreamW(MSIHANDLE handle, UINT iField, LPCWSTR szFilename)
785 MSIRECORD *rec;
786 UINT ret;
788 TRACE("%d %d %s\n", handle, iField, debugstr_w(szFilename));
790 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
791 if( !rec )
792 return ERROR_INVALID_HANDLE;
794 msiobj_lock( &rec->hdr );
795 ret = MSI_RecordSetStreamFromFileW( rec, iField, szFilename );
796 msiobj_unlock( &rec->hdr );
797 msiobj_release( &rec->hdr );
798 return ret;
801 UINT MSI_RecordReadStream(MSIRECORD *rec, UINT iField, char *buf, LPDWORD sz)
803 ULONG count;
804 HRESULT r;
805 IStream *stm;
807 TRACE("%p %d %p %p\n", rec, iField, buf, sz);
809 if( !sz )
810 return ERROR_INVALID_PARAMETER;
812 if( iField > rec->count)
813 return ERROR_INVALID_PARAMETER;
815 if ( rec->fields[iField].type == MSIFIELD_NULL )
817 *sz = 0;
818 return ERROR_INVALID_DATA;
821 if( rec->fields[iField].type != MSIFIELD_STREAM )
822 return ERROR_INVALID_DATATYPE;
824 stm = rec->fields[iField].u.stream;
825 if( !stm )
826 return ERROR_INVALID_PARAMETER;
828 /* if there's no buffer pointer, calculate the length to the end */
829 if( !buf )
831 LARGE_INTEGER ofs;
832 ULARGE_INTEGER end, cur;
834 ofs.QuadPart = cur.QuadPart = 0;
835 end.QuadPart = 0;
836 IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
837 IStream_Seek( stm, ofs, STREAM_SEEK_END, &end );
838 ofs.QuadPart = cur.QuadPart;
839 IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
840 *sz = end.QuadPart - cur.QuadPart;
842 return ERROR_SUCCESS;
845 /* read the data */
846 count = 0;
847 r = IStream_Read( stm, buf, *sz, &count );
848 if( FAILED( r ) )
850 *sz = 0;
851 return ERROR_FUNCTION_FAILED;
854 *sz = count;
856 return ERROR_SUCCESS;
859 UINT WINAPI MsiRecordReadStream(MSIHANDLE handle, UINT iField, char *buf, LPDWORD sz)
861 MSIRECORD *rec;
862 UINT ret;
864 TRACE("%d %d %p %p\n", handle, iField, buf, sz);
866 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
867 if( !rec )
868 return ERROR_INVALID_HANDLE;
869 msiobj_lock( &rec->hdr );
870 ret = MSI_RecordReadStream( rec, iField, buf, sz );
871 msiobj_unlock( &rec->hdr );
872 msiobj_release( &rec->hdr );
873 return ret;
876 UINT MSI_RecordSetIStream( MSIRECORD *rec, UINT iField, IStream *stm )
878 TRACE("%p %d %p\n", rec, iField, stm);
880 if( iField > rec->count )
881 return ERROR_INVALID_FIELD;
883 MSI_FreeField( &rec->fields[iField] );
885 rec->fields[iField].type = MSIFIELD_STREAM;
886 rec->fields[iField].u.stream = stm;
887 IStream_AddRef( stm );
889 return ERROR_SUCCESS;
892 UINT MSI_RecordGetIStream( MSIRECORD *rec, UINT iField, IStream **pstm)
894 TRACE("%p %d %p\n", rec, iField, pstm);
896 if( iField > rec->count )
897 return ERROR_INVALID_FIELD;
899 if( rec->fields[iField].type != MSIFIELD_STREAM )
900 return ERROR_INVALID_FIELD;
902 *pstm = rec->fields[iField].u.stream;
903 IStream_AddRef( *pstm );
905 return ERROR_SUCCESS;
908 static UINT msi_dump_stream_to_file( IStream *stm, LPCWSTR name )
910 ULARGE_INTEGER size;
911 LARGE_INTEGER pos;
912 IStream *out;
913 DWORD stgm;
914 HRESULT r;
916 stgm = STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_FAILIFTHERE;
917 r = SHCreateStreamOnFileW( name, stgm, &out );
918 if( FAILED( r ) )
919 return ERROR_FUNCTION_FAILED;
921 pos.QuadPart = 0;
922 r = IStream_Seek( stm, pos, STREAM_SEEK_END, &size );
923 if( FAILED( r ) )
924 goto end;
926 pos.QuadPart = 0;
927 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
928 if( FAILED( r ) )
929 goto end;
931 r = IStream_CopyTo( stm, out, size, NULL, NULL );
933 end:
934 IStream_Release( out );
935 if( FAILED( r ) )
936 return ERROR_FUNCTION_FAILED;
937 return ERROR_SUCCESS;
940 UINT MSI_RecordStreamToFile( MSIRECORD *rec, UINT iField, LPCWSTR name )
942 IStream *stm = NULL;
943 UINT r;
945 TRACE("%p %u %s\n", rec, iField, debugstr_w(name));
947 msiobj_lock( &rec->hdr );
949 r = MSI_RecordGetIStream( rec, iField, &stm );
950 if( r == ERROR_SUCCESS )
952 r = msi_dump_stream_to_file( stm, name );
953 IStream_Release( stm );
956 msiobj_unlock( &rec->hdr );
958 return r;
961 MSIRECORD *MSI_CloneRecord(MSIRECORD *rec)
963 MSIRECORD *clone;
964 UINT r, i, count;
966 count = MSI_RecordGetFieldCount(rec);
967 clone = MSI_CreateRecord(count);
968 if (!clone)
969 return NULL;
971 for (i = 0; i <= count; i++)
973 if (rec->fields[i].type == MSIFIELD_STREAM)
975 if (FAILED(IStream_Clone(rec->fields[i].u.stream,
976 &clone->fields[i].u.stream)))
978 msiobj_release(&clone->hdr);
979 return NULL;
981 clone->fields[i].type = MSIFIELD_STREAM;
983 else
985 r = MSI_RecordCopyField(rec, i, clone, i);
986 if (r != ERROR_SUCCESS)
988 msiobj_release(&clone->hdr);
989 return NULL;
994 return clone;
997 BOOL MSI_RecordsAreFieldsEqual(MSIRECORD *a, MSIRECORD *b, UINT field)
999 if (a->fields[field].type != b->fields[field].type)
1000 return FALSE;
1002 switch (a->fields[field].type)
1004 case MSIFIELD_NULL:
1005 break;
1007 case MSIFIELD_INT:
1008 if (a->fields[field].u.iVal != b->fields[field].u.iVal)
1009 return FALSE;
1010 break;
1012 case MSIFIELD_WSTR:
1013 if (strcmpW(a->fields[field].u.szwVal, b->fields[field].u.szwVal))
1014 return FALSE;
1015 break;
1017 case MSIFIELD_STREAM:
1018 default:
1019 return FALSE;
1021 return TRUE;
1025 BOOL MSI_RecordsAreEqual(MSIRECORD *a, MSIRECORD *b)
1027 UINT i;
1029 if (a->count != b->count)
1030 return FALSE;
1032 for (i = 0; i <= a->count; i++)
1034 if (!MSI_RecordsAreFieldsEqual( a, b, i ))
1035 return FALSE;
1038 return TRUE;
1041 WCHAR *msi_dup_record_field( MSIRECORD *rec, INT field )
1043 DWORD sz = 0;
1044 WCHAR *str;
1045 UINT r;
1047 if (MSI_RecordIsNull( rec, field )) return NULL;
1049 r = MSI_RecordGetStringW( rec, field, NULL, &sz );
1050 if (r != ERROR_SUCCESS)
1051 return NULL;
1053 sz++;
1054 str = msi_alloc( sz * sizeof(WCHAR) );
1055 if (!str) return NULL;
1056 str[0] = 0;
1057 r = MSI_RecordGetStringW( rec, field, str, &sz );
1058 if (r != ERROR_SUCCESS)
1060 ERR("failed to get string!\n");
1061 msi_free( str );
1062 return NULL;
1064 return str;