quartz: Free two assert calls from having side effects.
[wine/testsucceed.git] / dlls / msi / record.c
blob1dace7879068fd9d866e1f4cdc034850c30fc7a1
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 break;
498 default:
499 break;
502 if( szValue && *pcchValue <= len )
503 ret = ERROR_MORE_DATA;
504 *pcchValue = len;
506 return ret;
509 UINT WINAPI MsiRecordGetStringW(MSIHANDLE handle, UINT iField,
510 LPWSTR szValue, LPDWORD pcchValue)
512 MSIRECORD *rec;
513 UINT ret;
515 TRACE("%d %d %p %p\n", handle, iField, szValue, pcchValue);
517 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
518 if( !rec )
519 return ERROR_INVALID_HANDLE;
521 msiobj_lock( &rec->hdr );
522 ret = MSI_RecordGetStringW( rec, iField, szValue, pcchValue );
523 msiobj_unlock( &rec->hdr );
524 msiobj_release( &rec->hdr );
525 return ret;
528 static UINT msi_get_stream_size( IStream *stm )
530 STATSTG stat;
531 HRESULT r;
533 r = IStream_Stat( stm, &stat, STATFLAG_NONAME );
534 if( FAILED(r) )
535 return 0;
536 return stat.cbSize.QuadPart;
539 static UINT MSI_RecordDataSize(MSIRECORD *rec, UINT iField)
541 TRACE("%p %d\n", rec, iField);
543 if( iField > rec->count )
544 return 0;
546 switch( rec->fields[iField].type )
548 case MSIFIELD_INT:
549 return sizeof (INT);
550 case MSIFIELD_WSTR:
551 return lstrlenW( rec->fields[iField].u.szwVal );
552 case MSIFIELD_NULL:
553 break;
554 case MSIFIELD_STREAM:
555 return msi_get_stream_size( rec->fields[iField].u.stream );
557 return 0;
560 UINT WINAPI MsiRecordDataSize(MSIHANDLE handle, UINT iField)
562 MSIRECORD *rec;
563 UINT ret;
565 TRACE("%d %d\n", handle, iField);
567 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
568 if( !rec )
569 return 0;
570 msiobj_lock( &rec->hdr );
571 ret = MSI_RecordDataSize( rec, iField);
572 msiobj_unlock( &rec->hdr );
573 msiobj_release( &rec->hdr );
574 return ret;
577 static UINT MSI_RecordSetStringA( MSIRECORD *rec, UINT iField, LPCSTR szValue )
579 LPWSTR str;
581 TRACE("%p %d %s\n", rec, iField, debugstr_a(szValue));
583 if( iField > rec->count )
584 return ERROR_INVALID_FIELD;
586 MSI_FreeField( &rec->fields[iField] );
587 if( szValue && szValue[0] )
589 str = strdupAtoW( szValue );
590 rec->fields[iField].type = MSIFIELD_WSTR;
591 rec->fields[iField].u.szwVal = str;
593 else
595 rec->fields[iField].type = MSIFIELD_NULL;
596 rec->fields[iField].u.szwVal = NULL;
599 return 0;
602 UINT WINAPI MsiRecordSetStringA( MSIHANDLE handle, UINT iField, LPCSTR szValue )
604 MSIRECORD *rec;
605 UINT ret;
607 TRACE("%d %d %s\n", handle, iField, debugstr_a(szValue));
609 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
610 if( !rec )
611 return ERROR_INVALID_HANDLE;
612 msiobj_lock( &rec->hdr );
613 ret = MSI_RecordSetStringA( rec, iField, szValue );
614 msiobj_unlock( &rec->hdr );
615 msiobj_release( &rec->hdr );
616 return ret;
619 UINT MSI_RecordSetStringW( MSIRECORD *rec, UINT iField, LPCWSTR szValue )
621 LPWSTR str;
623 TRACE("%p %d %s\n", rec, iField, debugstr_w(szValue));
625 if( iField > rec->count )
626 return ERROR_INVALID_FIELD;
628 MSI_FreeField( &rec->fields[iField] );
630 if( szValue && szValue[0] )
632 str = strdupW( szValue );
633 rec->fields[iField].type = MSIFIELD_WSTR;
634 rec->fields[iField].u.szwVal = str;
636 else
638 rec->fields[iField].type = MSIFIELD_NULL;
639 rec->fields[iField].u.szwVal = NULL;
642 return 0;
645 UINT WINAPI MsiRecordSetStringW( MSIHANDLE handle, UINT iField, LPCWSTR szValue )
647 MSIRECORD *rec;
648 UINT ret;
650 TRACE("%d %d %s\n", handle, iField, debugstr_w(szValue));
652 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
653 if( !rec )
654 return ERROR_INVALID_HANDLE;
656 msiobj_lock( &rec->hdr );
657 ret = MSI_RecordSetStringW( rec, iField, szValue );
658 msiobj_unlock( &rec->hdr );
659 msiobj_release( &rec->hdr );
660 return ret;
663 /* read the data in a file into an IStream */
664 static UINT RECORD_StreamFromFile(LPCWSTR szFile, IStream **pstm)
666 DWORD sz, szHighWord = 0, read;
667 HANDLE handle;
668 HGLOBAL hGlob = 0;
669 HRESULT hr;
670 ULARGE_INTEGER ulSize;
672 TRACE("reading %s\n", debugstr_w(szFile));
674 /* read the file into memory */
675 handle = CreateFileW(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
676 if( handle == INVALID_HANDLE_VALUE )
677 return GetLastError();
678 sz = GetFileSize(handle, &szHighWord);
679 if( sz != INVALID_FILE_SIZE && szHighWord == 0 )
681 hGlob = GlobalAlloc(GMEM_FIXED, sz);
682 if( hGlob )
684 BOOL r = ReadFile(handle, hGlob, sz, &read, NULL);
685 if( !r )
687 GlobalFree(hGlob);
688 hGlob = 0;
692 CloseHandle(handle);
693 if( !hGlob )
694 return ERROR_FUNCTION_FAILED;
696 /* make a stream out of it, and set the correct file size */
697 hr = CreateStreamOnHGlobal(hGlob, TRUE, pstm);
698 if( FAILED( hr ) )
700 GlobalFree(hGlob);
701 return ERROR_FUNCTION_FAILED;
704 /* set the correct size - CreateStreamOnHGlobal screws it up */
705 ulSize.QuadPart = sz;
706 IStream_SetSize(*pstm, ulSize);
708 TRACE("read %s, %d bytes into IStream %p\n", debugstr_w(szFile), sz, *pstm);
710 return ERROR_SUCCESS;
713 UINT MSI_RecordSetStream(MSIRECORD *rec, UINT iField, IStream *stream)
715 if ( (iField == 0) || (iField > rec->count) )
716 return ERROR_INVALID_PARAMETER;
718 MSI_FreeField( &rec->fields[iField] );
719 rec->fields[iField].type = MSIFIELD_STREAM;
720 rec->fields[iField].u.stream = stream;
722 return ERROR_SUCCESS;
725 UINT MSI_RecordSetStreamFromFileW(MSIRECORD *rec, UINT iField, LPCWSTR szFilename)
727 IStream *stm = NULL;
728 HRESULT r;
730 if( (iField == 0) || (iField > rec->count) )
731 return ERROR_INVALID_PARAMETER;
733 /* no filename means we should seek back to the start of the stream */
734 if( !szFilename )
736 LARGE_INTEGER ofs;
737 ULARGE_INTEGER cur;
739 if( rec->fields[iField].type != MSIFIELD_STREAM )
740 return ERROR_INVALID_FIELD;
742 stm = rec->fields[iField].u.stream;
743 if( !stm )
744 return ERROR_INVALID_FIELD;
746 ofs.QuadPart = 0;
747 r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
748 if( FAILED( r ) )
749 return ERROR_FUNCTION_FAILED;
751 else
753 /* read the file into a stream and save the stream in the record */
754 r = RECORD_StreamFromFile(szFilename, &stm);
755 if( r != ERROR_SUCCESS )
756 return r;
758 /* if all's good, store it in the record */
759 MSI_RecordSetStream(rec, iField, stm);
762 return ERROR_SUCCESS;
765 UINT WINAPI MsiRecordSetStreamA(MSIHANDLE hRecord, UINT iField, LPCSTR szFilename)
767 LPWSTR wstr = NULL;
768 UINT ret;
770 TRACE("%d %d %s\n", hRecord, iField, debugstr_a(szFilename));
772 if( szFilename )
774 wstr = strdupAtoW( szFilename );
775 if( !wstr )
776 return ERROR_OUTOFMEMORY;
778 ret = MsiRecordSetStreamW(hRecord, iField, wstr);
779 msi_free(wstr);
781 return ret;
784 UINT WINAPI MsiRecordSetStreamW(MSIHANDLE handle, UINT iField, LPCWSTR szFilename)
786 MSIRECORD *rec;
787 UINT ret;
789 TRACE("%d %d %s\n", handle, iField, debugstr_w(szFilename));
791 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
792 if( !rec )
793 return ERROR_INVALID_HANDLE;
795 msiobj_lock( &rec->hdr );
796 ret = MSI_RecordSetStreamFromFileW( rec, iField, szFilename );
797 msiobj_unlock( &rec->hdr );
798 msiobj_release( &rec->hdr );
799 return ret;
802 UINT MSI_RecordReadStream(MSIRECORD *rec, UINT iField, char *buf, LPDWORD sz)
804 ULONG count;
805 HRESULT r;
806 IStream *stm;
808 TRACE("%p %d %p %p\n", rec, iField, buf, sz);
810 if( !sz )
811 return ERROR_INVALID_PARAMETER;
813 if( iField > rec->count)
814 return ERROR_INVALID_PARAMETER;
816 if ( rec->fields[iField].type == MSIFIELD_NULL )
818 *sz = 0;
819 return ERROR_INVALID_DATA;
822 if( rec->fields[iField].type != MSIFIELD_STREAM )
823 return ERROR_INVALID_DATATYPE;
825 stm = rec->fields[iField].u.stream;
826 if( !stm )
827 return ERROR_INVALID_PARAMETER;
829 /* if there's no buffer pointer, calculate the length to the end */
830 if( !buf )
832 LARGE_INTEGER ofs;
833 ULARGE_INTEGER end, cur;
835 ofs.QuadPart = cur.QuadPart = 0;
836 end.QuadPart = 0;
837 IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
838 IStream_Seek( stm, ofs, STREAM_SEEK_END, &end );
839 ofs.QuadPart = cur.QuadPart;
840 IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
841 *sz = end.QuadPart - cur.QuadPart;
843 return ERROR_SUCCESS;
846 /* read the data */
847 count = 0;
848 r = IStream_Read( stm, buf, *sz, &count );
849 if( FAILED( r ) )
851 *sz = 0;
852 return ERROR_FUNCTION_FAILED;
855 *sz = count;
857 return ERROR_SUCCESS;
860 UINT WINAPI MsiRecordReadStream(MSIHANDLE handle, UINT iField, char *buf, LPDWORD sz)
862 MSIRECORD *rec;
863 UINT ret;
865 TRACE("%d %d %p %p\n", handle, iField, buf, sz);
867 rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
868 if( !rec )
869 return ERROR_INVALID_HANDLE;
870 msiobj_lock( &rec->hdr );
871 ret = MSI_RecordReadStream( rec, iField, buf, sz );
872 msiobj_unlock( &rec->hdr );
873 msiobj_release( &rec->hdr );
874 return ret;
877 UINT MSI_RecordSetIStream( MSIRECORD *rec, UINT iField, IStream *stm )
879 TRACE("%p %d %p\n", rec, iField, stm);
881 if( iField > rec->count )
882 return ERROR_INVALID_FIELD;
884 MSI_FreeField( &rec->fields[iField] );
886 rec->fields[iField].type = MSIFIELD_STREAM;
887 rec->fields[iField].u.stream = stm;
888 IStream_AddRef( stm );
890 return ERROR_SUCCESS;
893 UINT MSI_RecordGetIStream( MSIRECORD *rec, UINT iField, IStream **pstm)
895 TRACE("%p %d %p\n", rec, iField, pstm);
897 if( iField > rec->count )
898 return ERROR_INVALID_FIELD;
900 if( rec->fields[iField].type != MSIFIELD_STREAM )
901 return ERROR_INVALID_FIELD;
903 *pstm = rec->fields[iField].u.stream;
904 IStream_AddRef( *pstm );
906 return ERROR_SUCCESS;
909 static UINT msi_dump_stream_to_file( IStream *stm, LPCWSTR name )
911 ULARGE_INTEGER size;
912 LARGE_INTEGER pos;
913 IStream *out;
914 DWORD stgm;
915 HRESULT r;
917 stgm = STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_FAILIFTHERE;
918 r = SHCreateStreamOnFileW( name, stgm, &out );
919 if( FAILED( r ) )
920 return ERROR_FUNCTION_FAILED;
922 pos.QuadPart = 0;
923 r = IStream_Seek( stm, pos, STREAM_SEEK_END, &size );
924 if( FAILED( r ) )
925 goto end;
927 pos.QuadPart = 0;
928 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
929 if( FAILED( r ) )
930 goto end;
932 r = IStream_CopyTo( stm, out, size, NULL, NULL );
934 end:
935 IStream_Release( out );
936 if( FAILED( r ) )
937 return ERROR_FUNCTION_FAILED;
938 return ERROR_SUCCESS;
941 UINT MSI_RecordStreamToFile( MSIRECORD *rec, UINT iField, LPCWSTR name )
943 IStream *stm = NULL;
944 UINT r;
946 TRACE("%p %u %s\n", rec, iField, debugstr_w(name));
948 msiobj_lock( &rec->hdr );
950 r = MSI_RecordGetIStream( rec, iField, &stm );
951 if( r == ERROR_SUCCESS )
953 r = msi_dump_stream_to_file( stm, name );
954 IStream_Release( stm );
957 msiobj_unlock( &rec->hdr );
959 return r;
962 MSIRECORD *MSI_CloneRecord(MSIRECORD *rec)
964 MSIRECORD *clone;
965 UINT r, i, count;
967 count = MSI_RecordGetFieldCount(rec);
968 clone = MSI_CreateRecord(count);
969 if (!clone)
970 return NULL;
972 for (i = 0; i <= count; i++)
974 if (rec->fields[i].type == MSIFIELD_STREAM)
976 if (FAILED(IStream_Clone(rec->fields[i].u.stream,
977 &clone->fields[i].u.stream)))
979 msiobj_release(&clone->hdr);
980 return NULL;
982 clone->fields[i].type = MSIFIELD_STREAM;
984 else
986 r = MSI_RecordCopyField(rec, i, clone, i);
987 if (r != ERROR_SUCCESS)
989 msiobj_release(&clone->hdr);
990 return NULL;
995 return clone;
998 BOOL MSI_RecordsAreFieldsEqual(MSIRECORD *a, MSIRECORD *b, UINT field)
1000 if (a->fields[field].type != b->fields[field].type)
1001 return FALSE;
1003 switch (a->fields[field].type)
1005 case MSIFIELD_NULL:
1006 break;
1008 case MSIFIELD_INT:
1009 if (a->fields[field].u.iVal != b->fields[field].u.iVal)
1010 return FALSE;
1011 break;
1013 case MSIFIELD_WSTR:
1014 if (strcmpW(a->fields[field].u.szwVal, b->fields[field].u.szwVal))
1015 return FALSE;
1016 break;
1018 case MSIFIELD_STREAM:
1019 default:
1020 return FALSE;
1022 return TRUE;
1026 BOOL MSI_RecordsAreEqual(MSIRECORD *a, MSIRECORD *b)
1028 UINT i;
1030 if (a->count != b->count)
1031 return FALSE;
1033 for (i = 0; i <= a->count; i++)
1035 if (!MSI_RecordsAreFieldsEqual( a, b, i ))
1036 return FALSE;
1039 return TRUE;
1042 WCHAR *msi_dup_record_field( MSIRECORD *rec, INT field )
1044 DWORD sz = 0;
1045 WCHAR *str;
1046 UINT r;
1048 if (MSI_RecordIsNull( rec, field )) return NULL;
1050 r = MSI_RecordGetStringW( rec, field, NULL, &sz );
1051 if (r != ERROR_SUCCESS)
1052 return NULL;
1054 sz++;
1055 str = msi_alloc( sz * sizeof(WCHAR) );
1056 if (!str) return NULL;
1057 str[0] = 0;
1058 r = MSI_RecordGetStringW( rec, field, str, &sz );
1059 if (r != ERROR_SUCCESS)
1061 ERR("failed to get string!\n");
1062 msi_free( str );
1063 return NULL;
1065 return str;