msi: Use an array instead of a hashtable for WHEREVIEW.
[wine] / dlls / msi / record.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002-2004 Mike McCormack for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22
23 #define COBJMACROS
24
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"
37
38 #include "winreg.h"
39 #include "shlwapi.h"
40
41 #include "query.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
44
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
50
51 static void MSI_FreeField( MSIFIELD *field )
52 {
53     switch( field->type )
54     {
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);
67     }
68 }
69
70 void MSI_CloseRecord( MSIOBJECTHDR *arg )
71 {
72     MSIRECORD *rec = (MSIRECORD *) arg;
73     UINT i;
74
75     for( i=0; i<=rec->count; i++ )
76         MSI_FreeField( &rec->fields[i] );
77 }
78
79 MSIRECORD *MSI_CreateRecord( UINT cParams )
80 {
81     MSIRECORD *rec;
82     UINT len;
83
84     TRACE("%d\n", cParams);
85
86     if( cParams>65535 )
87         return NULL;
88
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;
94 }
95
96 MSIHANDLE WINAPI MsiCreateRecord( UINT cParams )
97 {
98     MSIRECORD *rec;
99     MSIHANDLE ret = 0;
100
101     TRACE("%d\n", cParams);
102
103     rec = MSI_CreateRecord( cParams );
104     if( rec )
105     {
106         ret = alloc_msihandle( &rec->hdr );
107         msiobj_release( &rec->hdr );
108     }
109     return ret;
110 }
111
112 UINT MSI_RecordGetFieldCount( const MSIRECORD *rec )
113 {
114     return rec->count;
115 }
116
117 UINT WINAPI MsiRecordGetFieldCount( MSIHANDLE handle )
118 {
119     MSIRECORD *rec;
120     UINT ret;
121
122     TRACE("%d\n", handle );
123
124     rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
125     if( !rec )
126         return -1;
127
128     msiobj_lock( &rec->hdr );
129     ret = MSI_RecordGetFieldCount( rec );
130     msiobj_unlock( &rec->hdr );
131     msiobj_release( &rec->hdr );
132
133     return ret;
134 }
135
136 static BOOL string2intW( LPCWSTR str, int *out )
137 {
138     int x = 0;
139     LPCWSTR p = str;
140
141     if( *p == '-' ) /* skip the minus sign */
142         p++;
143     while ( *p )
144     {
145         if( (*p < '0') || (*p > '9') )
146             return FALSE;
147         x *= 10;
148         x += (*p - '0');
149         p++;
150     }
151
152     if( str[0] == '-' ) /* check if it's negative */
153         x = -x;
154     *out = x; 
155
156     return TRUE;
157 }
158
159 UINT MSI_RecordCopyField( MSIRECORD *in_rec, UINT in_n,
160                           MSIRECORD *out_rec, UINT out_n )
161 {
162     UINT r = ERROR_SUCCESS;
163
164     msiobj_lock( &in_rec->hdr );
165
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 )
169     {
170         LPWSTR str;
171         MSIFIELD *in, *out;
172
173         in = &in_rec->fields[in_n];
174         out = &out_rec->fields[out_n];
175
176         switch ( in->type )
177         {
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);
199         }
200         if (r == ERROR_SUCCESS)
201             out->type = in->type;
202     }
203
204     msiobj_unlock( &in_rec->hdr );
205
206     return r;
207 }
208
209 INT_PTR MSI_RecordGetIntPtr( MSIRECORD *rec, UINT iField )
210 {
211     int ret;
212
213     TRACE( "%p %d\n", rec, iField );
214
215     if( iField > rec->count )
216         return MININT_PTR;
217
218     switch( rec->fields[iField].type )
219     {
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;
230     }
231
232     return MININT_PTR;
233 }
234
235 int MSI_RecordGetInteger( MSIRECORD *rec, UINT iField)
236 {
237     int ret = 0;
238
239     TRACE("%p %d\n", rec, iField );
240
241     if( iField > rec->count )
242         return MSI_NULL_INTEGER;
243
244     switch( rec->fields[iField].type )
245     {
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;
256     }
257
258     return MSI_NULL_INTEGER;
259 }
260
261 int WINAPI MsiRecordGetInteger( MSIHANDLE handle, UINT iField)
262 {
263     MSIRECORD *rec;
264     UINT ret;
265
266     TRACE("%d %d\n", handle, iField );
267
268     rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
269     if( !rec )
270         return MSI_NULL_INTEGER;
271
272     msiobj_lock( &rec->hdr );
273     ret = MSI_RecordGetInteger( rec, iField );
274     msiobj_unlock( &rec->hdr );
275     msiobj_release( &rec->hdr );
276
277     return ret;
278 }
279
280 UINT WINAPI MsiRecordClearData( MSIHANDLE handle )
281 {
282     MSIRECORD *rec;
283     UINT i;
284
285     TRACE("%d\n", handle );
286
287     rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
288     if( !rec )
289         return ERROR_INVALID_HANDLE;
290
291     msiobj_lock( &rec->hdr );
292     for( i=0; i<=rec->count; i++)
293     {
294         MSI_FreeField( &rec->fields[i] );
295         rec->fields[i].type = MSIFIELD_NULL;
296         rec->fields[i].u.iVal = 0;
297     }
298     msiobj_unlock( &rec->hdr );
299     msiobj_release( &rec->hdr );
300
301     return ERROR_SUCCESS;
302 }
303
304 UINT MSI_RecordSetIntPtr( MSIRECORD *rec, UINT iField, INT_PTR pVal )
305 {
306     TRACE("%p %u %ld\n", rec, iField, pVal);
307
308     if( iField > rec->count )
309         return ERROR_INVALID_PARAMETER;
310
311     MSI_FreeField( &rec->fields[iField] );
312     rec->fields[iField].type = MSIFIELD_INTPTR;
313     rec->fields[iField].u.pVal = pVal;
314
315     return ERROR_SUCCESS;
316 }
317
318 UINT MSI_RecordSetInteger( MSIRECORD *rec, UINT iField, int iVal )
319 {
320     TRACE("%p %u %d\n", rec, iField, iVal);
321
322     if( iField > rec->count )
323         return ERROR_INVALID_PARAMETER;
324
325     MSI_FreeField( &rec->fields[iField] );
326     rec->fields[iField].type = MSIFIELD_INT;
327     rec->fields[iField].u.iVal = iVal;
328
329     return ERROR_SUCCESS;
330 }
331
332 UINT WINAPI MsiRecordSetInteger( MSIHANDLE handle, UINT iField, int iVal )
333 {
334     MSIRECORD *rec;
335     UINT ret;
336
337     TRACE("%d %u %d\n", handle, iField, iVal);
338
339     rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
340     if( !rec )
341         return ERROR_INVALID_HANDLE;
342
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;
348 }
349
350 BOOL MSI_RecordIsNull( MSIRECORD *rec, UINT iField )
351 {
352     BOOL r = TRUE;
353
354     TRACE("%p %d\n", rec, iField );
355
356     r = ( iField > rec->count ) ||
357         ( rec->fields[iField].type == MSIFIELD_NULL );
358
359     return r;
360 }
361
362 BOOL WINAPI MsiRecordIsNull( MSIHANDLE handle, UINT iField )
363 {
364     MSIRECORD *rec;
365     UINT ret;
366
367     TRACE("%d %d\n", handle, iField );
368
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;
377
378 }
379
380 UINT MSI_RecordGetStringA(MSIRECORD *rec, UINT iField,
381                LPSTR szValue, LPDWORD pcchValue)
382 {
383     UINT len=0, ret;
384     CHAR buffer[16];
385
386     TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
387
388     if( iField > rec->count )
389     {
390         if ( szValue && *pcchValue > 0 )
391             szValue[0] = 0;
392
393         *pcchValue = 0;
394         return ERROR_SUCCESS;
395     }
396
397     ret = ERROR_SUCCESS;
398     switch( rec->fields[iField].type )
399     {
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;
424     }
425
426     if( szValue && *pcchValue <= len )
427         ret = ERROR_MORE_DATA;
428     *pcchValue = len;
429
430     return ret;
431 }
432
433 UINT WINAPI MsiRecordGetStringA(MSIHANDLE handle, UINT iField,
434                LPSTR szValue, LPDWORD pcchValue)
435 {
436     MSIRECORD *rec;
437     UINT ret;
438
439     TRACE("%d %d %p %p\n", handle, iField, szValue, pcchValue);
440
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;
449 }
450
451 const WCHAR *MSI_RecordGetString( const MSIRECORD *rec, UINT iField )
452 {
453     if( iField > rec->count )
454         return NULL;
455
456     if( rec->fields[iField].type != MSIFIELD_WSTR )
457         return NULL;
458
459     return rec->fields[iField].u.szwVal;
460 }
461
462 UINT MSI_RecordGetStringW(MSIRECORD *rec, UINT iField,
463                LPWSTR szValue, LPDWORD pcchValue)
464 {
465     UINT len=0, ret;
466     WCHAR buffer[16];
467     static const WCHAR szFormat[] = { '%','d',0 };
468
469     TRACE("%p %d %p %p\n", rec, iField, szValue, pcchValue);
470
471     if( iField > rec->count )
472     {
473         if ( szValue && *pcchValue > 0 )
474             szValue[0] = 0;
475
476         *pcchValue = 0;
477         return ERROR_SUCCESS;
478     }
479
480     ret = ERROR_SUCCESS;
481     switch( rec->fields[iField].type )
482     {
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;
499     }
500
501     if( szValue && *pcchValue <= len )
502         ret = ERROR_MORE_DATA;
503     *pcchValue = len;
504
505     return ret;
506 }
507
508 UINT WINAPI MsiRecordGetStringW(MSIHANDLE handle, UINT iField,
509                LPWSTR szValue, LPDWORD pcchValue)
510 {
511     MSIRECORD *rec;
512     UINT ret;
513
514     TRACE("%d %d %p %p\n", handle, iField, szValue, pcchValue);
515
516     rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
517     if( !rec )
518         return ERROR_INVALID_HANDLE;
519
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;
525 }
526
527 static UINT msi_get_stream_size( IStream *stm )
528 {
529     STATSTG stat;
530     HRESULT r;
531
532     r = IStream_Stat( stm, &stat, STATFLAG_NONAME );
533     if( FAILED(r) )
534         return 0;
535     return stat.cbSize.QuadPart;
536 }
537
538 static UINT MSI_RecordDataSize(MSIRECORD *rec, UINT iField)
539 {
540     TRACE("%p %d\n", rec, iField);
541
542     if( iField > rec->count )
543         return 0;
544
545     switch( rec->fields[iField].type )
546     {
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 );
555     }
556     return 0;
557 }
558
559 UINT WINAPI MsiRecordDataSize(MSIHANDLE handle, UINT iField)
560 {
561     MSIRECORD *rec;
562     UINT ret;
563
564     TRACE("%d %d\n", handle, iField);
565
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;
574 }
575
576 static UINT MSI_RecordSetStringA( MSIRECORD *rec, UINT iField, LPCSTR szValue )
577 {
578     LPWSTR str;
579
580     TRACE("%p %d %s\n", rec, iField, debugstr_a(szValue));
581
582     if( iField > rec->count )
583         return ERROR_INVALID_FIELD;
584
585     MSI_FreeField( &rec->fields[iField] );
586     if( szValue && szValue[0] )
587     {
588         str = strdupAtoW( szValue );
589         rec->fields[iField].type = MSIFIELD_WSTR;
590         rec->fields[iField].u.szwVal = str;
591     }
592     else
593     {
594         rec->fields[iField].type = MSIFIELD_NULL;
595         rec->fields[iField].u.szwVal = NULL;
596     }
597
598     return 0;
599 }
600
601 UINT WINAPI MsiRecordSetStringA( MSIHANDLE handle, UINT iField, LPCSTR szValue )
602 {
603     MSIRECORD *rec;
604     UINT ret;
605
606     TRACE("%d %d %s\n", handle, iField, debugstr_a(szValue));
607
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;
616 }
617
618 UINT MSI_RecordSetStringW( MSIRECORD *rec, UINT iField, LPCWSTR szValue )
619 {
620     LPWSTR str;
621
622     TRACE("%p %d %s\n", rec, iField, debugstr_w(szValue));
623
624     if( iField > rec->count )
625         return ERROR_INVALID_FIELD;
626
627     MSI_FreeField( &rec->fields[iField] );
628
629     if( szValue && szValue[0] )
630     {
631         str = strdupW( szValue );
632         rec->fields[iField].type = MSIFIELD_WSTR;
633         rec->fields[iField].u.szwVal = str;
634     }
635     else
636     {
637         rec->fields[iField].type = MSIFIELD_NULL;
638         rec->fields[iField].u.szwVal = NULL;
639     }
640
641     return 0;
642 }
643
644 UINT WINAPI MsiRecordSetStringW( MSIHANDLE handle, UINT iField, LPCWSTR szValue )
645 {
646     MSIRECORD *rec;
647     UINT ret;
648
649     TRACE("%d %d %s\n", handle, iField, debugstr_w(szValue));
650
651     rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
652     if( !rec )
653         return ERROR_INVALID_HANDLE;
654
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;
660 }
661
662 /* read the data in a file into an IStream */
663 static UINT RECORD_StreamFromFile(LPCWSTR szFile, IStream **pstm)
664 {
665     DWORD sz, szHighWord = 0, read;
666     HANDLE handle;
667     HGLOBAL hGlob = 0;
668     HRESULT hr;
669     ULARGE_INTEGER ulSize;
670
671     TRACE("reading %s\n", debugstr_w(szFile));
672
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 )
679     {
680         hGlob = GlobalAlloc(GMEM_FIXED, sz);
681         if( hGlob )
682         {
683             BOOL r = ReadFile(handle, hGlob, sz, &read, NULL);
684             if( !r )
685             {
686                 GlobalFree(hGlob);
687                 hGlob = 0;
688             }
689         }
690     }
691     CloseHandle(handle);
692     if( !hGlob )
693         return ERROR_FUNCTION_FAILED;
694
695     /* make a stream out of it, and set the correct file size */
696     hr = CreateStreamOnHGlobal(hGlob, TRUE, pstm);
697     if( FAILED( hr ) )
698     {
699         GlobalFree(hGlob);
700         return ERROR_FUNCTION_FAILED;
701     }
702
703     /* set the correct size - CreateStreamOnHGlobal screws it up */
704     ulSize.QuadPart = sz;
705     IStream_SetSize(*pstm, ulSize);
706
707     TRACE("read %s, %d bytes into IStream %p\n", debugstr_w(szFile), sz, *pstm);
708
709     return ERROR_SUCCESS;
710 }
711
712 UINT MSI_RecordSetStream(MSIRECORD *rec, UINT iField, IStream *stream)
713 {
714     if ( (iField == 0) || (iField > rec->count) )
715         return ERROR_INVALID_PARAMETER;
716
717     MSI_FreeField( &rec->fields[iField] );
718     rec->fields[iField].type = MSIFIELD_STREAM;
719     rec->fields[iField].u.stream = stream;
720
721     return ERROR_SUCCESS;
722 }
723
724 UINT MSI_RecordSetStreamFromFileW(MSIRECORD *rec, UINT iField, LPCWSTR szFilename)
725 {
726     IStream *stm = NULL;
727     HRESULT r;
728
729     if( (iField == 0) || (iField > rec->count) )
730         return ERROR_INVALID_PARAMETER;
731
732     /* no filename means we should seek back to the start of the stream */
733     if( !szFilename )
734     {
735         LARGE_INTEGER ofs;
736         ULARGE_INTEGER cur;
737
738         if( rec->fields[iField].type != MSIFIELD_STREAM )
739             return ERROR_INVALID_FIELD;
740
741         stm = rec->fields[iField].u.stream;
742         if( !stm )
743             return ERROR_INVALID_FIELD;
744
745         ofs.QuadPart = 0;
746         r = IStream_Seek( stm, ofs, STREAM_SEEK_SET, &cur );
747         if( FAILED( r ) )
748             return ERROR_FUNCTION_FAILED;
749     }
750     else
751     {
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;
756
757         /* if all's good, store it in the record */
758         MSI_RecordSetStream(rec, iField, stm);
759     }
760
761     return ERROR_SUCCESS;
762 }
763
764 UINT WINAPI MsiRecordSetStreamA(MSIHANDLE hRecord, UINT iField, LPCSTR szFilename)
765 {
766     LPWSTR wstr = NULL;
767     UINT ret;
768
769     TRACE("%d %d %s\n", hRecord, iField, debugstr_a(szFilename));
770
771     if( szFilename )
772     {
773         wstr = strdupAtoW( szFilename );
774         if( !wstr )
775              return ERROR_OUTOFMEMORY;
776     }
777     ret = MsiRecordSetStreamW(hRecord, iField, wstr);
778     msi_free(wstr);
779
780     return ret;
781 }
782
783 UINT WINAPI MsiRecordSetStreamW(MSIHANDLE handle, UINT iField, LPCWSTR szFilename)
784 {
785     MSIRECORD *rec;
786     UINT ret;
787
788     TRACE("%d %d %s\n", handle, iField, debugstr_w(szFilename));
789
790     rec = msihandle2msiinfo( handle, MSIHANDLETYPE_RECORD );
791     if( !rec )
792         return ERROR_INVALID_HANDLE;
793
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;
799 }
800
801 UINT MSI_RecordReadStream(MSIRECORD *rec, UINT iField, char *buf, LPDWORD sz)
802 {
803     ULONG count;
804     HRESULT r;
805     IStream *stm;
806
807     TRACE("%p %d %p %p\n", rec, iField, buf, sz);
808
809     if( !sz )
810         return ERROR_INVALID_PARAMETER;
811
812     if( iField > rec->count)
813         return ERROR_INVALID_PARAMETER;
814
815     if ( rec->fields[iField].type == MSIFIELD_NULL )
816     {
817         *sz = 0;
818         return ERROR_INVALID_DATA;
819     }
820
821     if( rec->fields[iField].type != MSIFIELD_STREAM )
822         return ERROR_INVALID_DATATYPE;
823
824     stm = rec->fields[iField].u.stream;
825     if( !stm )
826         return ERROR_INVALID_PARAMETER;
827
828     /* if there's no buffer pointer, calculate the length to the end */
829     if( !buf )
830     {
831         LARGE_INTEGER ofs;
832         ULARGE_INTEGER end, cur;
833
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;
841
842         return ERROR_SUCCESS;
843     }
844
845     /* read the data */
846     count = 0;
847     r = IStream_Read( stm, buf, *sz, &count );
848     if( FAILED( r ) )
849     {
850         *sz = 0;
851         return ERROR_FUNCTION_FAILED;
852     }
853
854     *sz = count;
855
856     return ERROR_SUCCESS;
857 }
858
859 UINT WINAPI MsiRecordReadStream(MSIHANDLE handle, UINT iField, char *buf, LPDWORD sz)
860 {
861     MSIRECORD *rec;
862     UINT ret;
863
864     TRACE("%d %d %p %p\n", handle, iField, buf, sz);
865
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;
874 }
875
876 UINT MSI_RecordSetIStream( MSIRECORD *rec, UINT iField, IStream *stm )
877 {
878     TRACE("%p %d %p\n", rec, iField, stm);
879
880     if( iField > rec->count )
881         return ERROR_INVALID_FIELD;
882
883     MSI_FreeField( &rec->fields[iField] );
884
885     rec->fields[iField].type = MSIFIELD_STREAM;
886     rec->fields[iField].u.stream = stm;
887     IStream_AddRef( stm );
888
889     return ERROR_SUCCESS;
890 }
891
892 UINT MSI_RecordGetIStream( MSIRECORD *rec, UINT iField, IStream **pstm)
893 {
894     TRACE("%p %d %p\n", rec, iField, pstm);
895
896     if( iField > rec->count )
897         return ERROR_INVALID_FIELD;
898
899     if( rec->fields[iField].type != MSIFIELD_STREAM )
900         return ERROR_INVALID_FIELD;
901
902     *pstm = rec->fields[iField].u.stream;
903     IStream_AddRef( *pstm );
904
905     return ERROR_SUCCESS;
906 }
907
908 static UINT msi_dump_stream_to_file( IStream *stm, LPCWSTR name )
909 {
910     ULARGE_INTEGER size;
911     LARGE_INTEGER pos;
912     IStream *out;
913     DWORD stgm;
914     HRESULT r;
915
916     stgm = STGM_READWRITE | STGM_SHARE_EXCLUSIVE | STGM_FAILIFTHERE;
917     r = SHCreateStreamOnFileW( name, stgm, &out );
918     if( FAILED( r ) )
919         return ERROR_FUNCTION_FAILED;
920
921     pos.QuadPart = 0;
922     r = IStream_Seek( stm, pos, STREAM_SEEK_END, &size );
923     if( FAILED( r ) )
924         goto end;
925
926     pos.QuadPart = 0;
927     r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
928     if( FAILED( r ) )
929         goto end;
930
931     r = IStream_CopyTo( stm, out, size, NULL, NULL );
932
933 end:
934     IStream_Release( out );
935     if( FAILED( r ) )
936         return ERROR_FUNCTION_FAILED;
937     return ERROR_SUCCESS;
938 }
939
940 UINT MSI_RecordStreamToFile( MSIRECORD *rec, UINT iField, LPCWSTR name )
941 {
942     IStream *stm = NULL;
943     UINT r;
944
945     TRACE("%p %u %s\n", rec, iField, debugstr_w(name));
946
947     msiobj_lock( &rec->hdr );
948
949     r = MSI_RecordGetIStream( rec, iField, &stm );
950     if( r == ERROR_SUCCESS )
951     {
952         r = msi_dump_stream_to_file( stm, name );
953         IStream_Release( stm );
954     }
955
956     msiobj_unlock( &rec->hdr );
957
958     return r;
959 }
960
961 MSIRECORD *MSI_CloneRecord(MSIRECORD *rec)
962 {
963     MSIRECORD *clone;
964     UINT r, i, count;
965
966     count = MSI_RecordGetFieldCount(rec);
967     clone = MSI_CreateRecord(count);
968     if (!clone)
969         return NULL;
970
971     for (i = 0; i <= count; i++)
972     {
973         if (rec->fields[i].type == MSIFIELD_STREAM)
974         {
975             if (FAILED(IStream_Clone(rec->fields[i].u.stream,
976                                      &clone->fields[i].u.stream)))
977             {
978                 msiobj_release(&clone->hdr);
979                 return NULL;
980             }
981             clone->fields[i].type = MSIFIELD_STREAM;
982         }
983         else
984         {
985             r = MSI_RecordCopyField(rec, i, clone, i);
986             if (r != ERROR_SUCCESS)
987             {
988                 msiobj_release(&clone->hdr);
989                 return NULL;
990             }
991         }
992     }
993
994     return clone;
995 }
996
997 BOOL MSI_RecordsAreEqual(MSIRECORD *a, MSIRECORD *b)
998 {
999     UINT i;
1000
1001     if (a->count != b->count)
1002         return FALSE;
1003
1004     for (i = 0; i <= a->count; i++)
1005     {
1006         if (a->fields[i].type != b->fields[i].type)
1007             return FALSE;
1008
1009         switch (a->fields[i].type)
1010         {
1011             case MSIFIELD_NULL:
1012                 break;
1013
1014             case MSIFIELD_INT:
1015                 if (a->fields[i].u.iVal != b->fields[i].u.iVal)
1016                     return FALSE;
1017                 break;
1018
1019             case MSIFIELD_WSTR:
1020                 if (strcmpW(a->fields[i].u.szwVal, b->fields[i].u.szwVal))
1021                     return FALSE;
1022                 break;
1023
1024             case MSIFIELD_STREAM:
1025             default:
1026                 return FALSE;
1027         }
1028     }
1029
1030     return TRUE;
1031 }
1032
1033 WCHAR *msi_dup_record_field( MSIRECORD *rec, INT field )
1034 {
1035     DWORD sz = 0;
1036     WCHAR *str;
1037     UINT r;
1038
1039     if (MSI_RecordIsNull( rec, field )) return NULL;
1040
1041     r = MSI_RecordGetStringW( rec, field, NULL, &sz );
1042     if (r != ERROR_SUCCESS)
1043         return NULL;
1044
1045     sz++;
1046     str = msi_alloc( sz * sizeof(WCHAR) );
1047     if (!str) return NULL;
1048     str[0] = 0;
1049     r = MSI_RecordGetStringW( rec, field, str, &sz );
1050     if (r != ERROR_SUCCESS)
1051     {
1052         ERR("failed to get string!\n");
1053         msi_free( str );
1054         return NULL;
1055     }
1056     return str;
1057 }