rpcrt4: Try a lot harder to resuse existing connections by comparing inside the RpcQu...
[wine] / dlls / msi / table.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002-2005 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 #include <assert.h>
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "msi.h"
32 #include "msiquery.h"
33 #include "objbase.h"
34 #include "objidl.h"
35 #include "winnls.h"
36 #include "msipriv.h"
37 #include "query.h"
38
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
43
44 #define MSITABLE_HASH_TABLE_SIZE 37
45 #define LONG_STR_BYTES 3
46
47 typedef struct tagMSICOLUMNHASHENTRY
48 {
49     struct tagMSICOLUMNHASHENTRY *next;
50     UINT value;
51     UINT row;
52 } MSICOLUMNHASHENTRY;
53
54 typedef struct tagMSICOLUMNINFO
55 {
56     LPCWSTR tablename;
57     UINT   number;
58     LPCWSTR colname;
59     UINT   type;
60     UINT   offset;
61     MSICOLUMNHASHENTRY **hash_table;
62 } MSICOLUMNINFO;
63
64 struct tagMSITABLE
65 {
66     BYTE **data;
67     UINT row_count;
68     BYTE **nonpersistent_data;
69     UINT nonpersistent_row_count;
70     struct list entry;
71     MSICOLUMNINFO *colinfo;
72     UINT col_count;
73     BOOL persistent;
74     WCHAR name[1];
75 };
76
77 typedef struct tagMSITRANSFORM {
78     struct list entry;
79     IStorage *stg;
80 } MSITRANSFORM;
81
82 static const WCHAR szStringData[] = {
83     '_','S','t','r','i','n','g','D','a','t','a',0 };
84 static const WCHAR szStringPool[] = {
85     '_','S','t','r','i','n','g','P','o','o','l',0 };
86
87 /* information for default tables */
88 static const WCHAR szTables[]  = { '_','T','a','b','l','e','s',0 };
89 static const WCHAR szTable[]  = { 'T','a','b','l','e',0 };
90 static const WCHAR szName[]    = { 'N','a','m','e',0 };
91 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
92 static const WCHAR szNumber[]  = { 'N','u','m','b','e','r',0 };
93 static const WCHAR szType[]    = { 'T','y','p','e',0 };
94
95 /* These tables are written into (the .hash_table part).
96  * Do not mark them const.
97  */
98 static MSICOLUMNINFO _Columns_cols[4] = {
99     { szColumns, 1, szTable,  MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
100     { szColumns, 2, szNumber, MSITYPE_VALID | 2,                   2 },
101     { szColumns, 3, szName,   MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
102     { szColumns, 4, szType,   MSITYPE_VALID | 2,                   6 },
103 };
104 static MSICOLUMNINFO _Tables_cols[1] = {
105     { szTables,  1, szName,   MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
106 };
107
108 #define MAX_STREAM_NAME 0x1f
109
110 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
111        MSICOLUMNINFO **pcols, UINT *pcount );
112 static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count );
113 static UINT get_tablecolumns( MSIDATABASE *db,
114        LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
115 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
116
117
118 void msi_table_set_strref(UINT bytes_per_strref)
119 {
120     _Columns_cols[0].offset = 0;
121     _Columns_cols[1].offset = bytes_per_strref;
122     _Columns_cols[2].offset = _Columns_cols[1].offset + sizeof(USHORT);
123     _Columns_cols[3].offset = _Columns_cols[2].offset + bytes_per_strref;
124 }
125
126 static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
127 {
128     if( col->type & MSITYPE_STRING )
129         return _Columns_cols[1].offset;
130     if( (col->type & 0xff) > 4 )
131         ERR("Invalid column size!\n");
132     return col->type & 0xff;
133 }
134
135 static int utf2mime(int x)
136 {
137     if( (x>='0') && (x<='9') )
138         return x-'0';
139     if( (x>='A') && (x<='Z') )
140         return x-'A'+10;
141     if( (x>='a') && (x<='z') )
142         return x-'a'+10+26;
143     if( x=='.' )
144         return 10+26+26;
145     if( x=='_' )
146         return 10+26+26+1;
147     return -1;
148 }
149
150 LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
151 {
152     DWORD count = MAX_STREAM_NAME;
153     DWORD ch, next;
154     LPWSTR out, p;
155
156     if( !bTable )
157         count = lstrlenW( in )+2;
158     out = msi_alloc( count*sizeof(WCHAR) );
159     p = out;
160
161     if( bTable )
162     {
163          *p++ = 0x4840;
164          count --;
165     }
166     while( count -- ) 
167     {
168         ch = *in++;
169         if( !ch )
170         {
171             *p = ch;
172             return out;
173         }
174         if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
175         {
176             ch = utf2mime(ch) + 0x4800;
177             next = *in;
178             if( next && (next<0x80) )
179             {
180                 next = utf2mime(next);
181                 if( next >= 0  )
182                 {
183                      next += 0x3ffffc0;
184                      ch += (next<<6);
185                      in++;
186                 }
187             }
188         }
189         *p++ = ch;
190     }
191     ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
192     msi_free( out );
193     return NULL;
194 }
195
196 static int mime2utf(int x)
197 {
198     if( x<10 )
199         return x + '0';
200     if( x<(10+26))
201         return x - 10 + 'A';
202     if( x<(10+26+26))
203         return x - 10 - 26 + 'a';
204     if( x == (10+26+26) )
205         return '.';
206     return '_';
207 }
208
209 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
210 {
211     WCHAR ch;
212     DWORD count = 0;
213
214     while ( (ch = *in++) )
215     {
216         if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
217         {
218             if( ch >= 0x4800 )
219                 ch = mime2utf(ch-0x4800);
220             else
221             {
222                 ch -= 0x3800;
223                 *out++ = mime2utf(ch&0x3f);
224                 count++;
225                 ch = mime2utf((ch>>6)&0x3f);
226             }
227         }
228         *out++ = ch;
229         count++;
230     }
231     *out = 0;
232     return count;
233 }
234
235 void enum_stream_names( IStorage *stg )
236 {
237     IEnumSTATSTG *stgenum = NULL;
238     HRESULT r;
239     STATSTG stat;
240     ULONG n, count;
241     WCHAR name[0x40];
242
243     r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
244     if( FAILED( r ) )
245         return;
246
247     n = 0;
248     while( 1 )
249     {
250         count = 0;
251         r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
252         if( FAILED( r ) || !count )
253             break;
254         decode_streamname( stat.pwcsName, name );
255         TRACE("stream %2d -> %s %s\n", n,
256               debugstr_w(stat.pwcsName), debugstr_w(name) );
257         n++;
258     }
259
260     IEnumSTATSTG_Release( stgenum );
261 }
262
263 UINT read_stream_data( IStorage *stg, LPCWSTR stname,
264                        USHORT **pdata, UINT *psz )
265 {
266     HRESULT r;
267     UINT ret = ERROR_FUNCTION_FAILED;
268     VOID *data;
269     ULONG sz, count;
270     IStream *stm = NULL;
271     STATSTG stat;
272     LPWSTR encname;
273
274     encname = encode_streamname(TRUE, stname);
275
276     TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
277
278     r = IStorage_OpenStream(stg, encname, NULL, 
279             STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
280     msi_free( encname );
281     if( FAILED( r ) )
282     {
283         WARN("open stream failed r = %08x - empty table?\n", r);
284         return ret;
285     }
286
287     r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
288     if( FAILED( r ) )
289     {
290         WARN("open stream failed r = %08x!\n", r);
291         goto end;
292     }
293
294     if( stat.cbSize.QuadPart >> 32 )
295     {
296         WARN("Too big!\n");
297         goto end;
298     }
299         
300     sz = stat.cbSize.QuadPart;
301     data = msi_alloc( sz );
302     if( !data )
303     {
304         WARN("couldn't allocate memory r=%08x!\n", r);
305         ret = ERROR_NOT_ENOUGH_MEMORY;
306         goto end;
307     }
308         
309     r = IStream_Read(stm, data, sz, &count );
310     if( FAILED( r ) || ( count != sz ) )
311     {
312         msi_free( data );
313         WARN("read stream failed r = %08x!\n", r);
314         goto end;
315     }
316
317     *pdata = data;
318     *psz = sz;
319     ret = ERROR_SUCCESS;
320
321 end:
322     IStream_Release( stm );
323
324     return ret;
325 }
326
327 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
328 {
329     LPWSTR encname;
330     HRESULT r;
331
332     encname = encode_streamname(FALSE, stname);
333
334     TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
335
336     r = IStorage_OpenStream(db->storage, encname, NULL, 
337             STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
338     if( FAILED( r ) )
339     {
340         MSITRANSFORM *transform;
341
342         LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
343         {
344             TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
345             r = IStorage_OpenStream( transform->stg, encname, NULL, 
346                     STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
347             if (SUCCEEDED(r))
348                 break;
349         }
350     }
351
352     msi_free( encname );
353
354     return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
355 }
356
357 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
358                               USHORT **pdata, UINT *psz )
359 {
360     HRESULT r;
361     UINT ret = ERROR_FUNCTION_FAILED;
362     VOID *data;
363     ULONG sz, count;
364     IStream *stm = NULL;
365     STATSTG stat;
366
367     r = db_get_raw_stream( db, stname, &stm );
368     if( r != ERROR_SUCCESS)
369         return ret;
370     r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
371     if( FAILED( r ) )
372     {
373         WARN("open stream failed r = %08x!\n", r);
374         goto end;
375     }
376
377     if( stat.cbSize.QuadPart >> 32 )
378     {
379         WARN("Too big!\n");
380         goto end;
381     }
382         
383     sz = stat.cbSize.QuadPart;
384     data = msi_alloc( sz );
385     if( !data )
386     {
387         WARN("couldn't allocate memory r=%08x!\n", r);
388         ret = ERROR_NOT_ENOUGH_MEMORY;
389         goto end;
390     }
391         
392     r = IStream_Read(stm, data, sz, &count );
393     if( FAILED( r ) || ( count != sz ) )
394     {
395         msi_free( data );
396         WARN("read stream failed r = %08x!\n", r);
397         goto end;
398     }
399
400     *pdata = data;
401     *psz = sz;
402     ret = ERROR_SUCCESS;
403
404 end:
405     IStream_Release( stm );
406
407     return ret;
408 }
409
410 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
411                         LPCVOID data, UINT sz, BOOL bTable )
412 {
413     HRESULT r;
414     UINT ret = ERROR_FUNCTION_FAILED;
415     ULONG count;
416     IStream *stm = NULL;
417     ULARGE_INTEGER size;
418     LARGE_INTEGER pos;
419     LPWSTR encname;
420
421     encname = encode_streamname(bTable, stname );
422     r = IStorage_OpenStream( stg, encname, NULL, 
423             STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
424     if( FAILED(r) )
425     {
426         r = IStorage_CreateStream( stg, encname,
427                 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
428     }
429     msi_free( encname );
430     if( FAILED( r ) )
431     {
432         WARN("open stream failed r = %08x\n", r);
433         return ret;
434     }
435
436     size.QuadPart = sz;
437     r = IStream_SetSize( stm, size );
438     if( FAILED( r ) )
439     {
440         WARN("Failed to SetSize\n");
441         goto end;
442     }
443
444     pos.QuadPart = 0;
445     r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
446     if( FAILED( r ) )
447     {
448         WARN("Failed to Seek\n");
449         goto end;
450     }
451
452     if (sz)
453     {
454         r = IStream_Write(stm, data, sz, &count );
455         if( FAILED( r ) || ( count != sz ) )
456         {
457             WARN("Failed to Write\n");
458             goto end;
459         }
460     }
461
462     ret = ERROR_SUCCESS;
463
464 end:
465     IStream_Release( stm );
466
467     return ret;
468 }
469
470 static void free_table( MSITABLE *table )
471 {
472     int i;
473     for( i=0; i<table->row_count; i++ )
474         msi_free( table->data[i] );
475     msi_free( table->data );
476     for( i=0; i<table->nonpersistent_row_count; i++ )
477         msi_free( table->nonpersistent_data[i] );
478     msi_free( table->nonpersistent_data );
479     if( (table->colinfo != _Tables_cols) &&
480         (table->colinfo != _Columns_cols) )
481     {
482         msi_free_colinfo( table->colinfo, table->col_count );
483         msi_free( table->colinfo );
484     }
485     msi_free( table );
486 }
487
488 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
489 {
490     const MSICOLUMNINFO *last_col = &cols[count-1];
491     if (!count)
492         return 0;
493     return last_col->offset + bytes_per_column( last_col );
494 }
495
496 /* add this table to the list of cached tables in the database */
497 static UINT read_table_from_storage( MSITABLE *t, IStorage *stg )
498 {
499     BYTE *rawdata = NULL;
500     UINT rawsize = 0, i, j, row_size = 0;
501
502     TRACE("%s\n",debugstr_w(t->name));
503
504     row_size = msi_table_get_row_size( t->colinfo, t->col_count );
505
506     /* if we can't read the table, just assume that it's empty */
507     read_stream_data( stg, t->name, (USHORT **)&rawdata, &rawsize );
508     if( !rawdata )
509         return ERROR_SUCCESS;
510
511     TRACE("Read %d bytes\n", rawsize );
512
513     if( rawsize % row_size )
514     {
515         WARN("Table size is invalid %d/%d\n", rawsize, row_size );
516         goto err;
517     }
518
519     t->row_count = rawsize / row_size;
520     t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
521     if( !t->data )
522         goto err;
523
524     /* transpose all the data */
525     TRACE("Transposing data from %d rows\n", t->row_count );
526     for( i=0; i<t->row_count; i++ )
527     {
528         t->data[i] = msi_alloc( row_size );
529         if( !t->data[i] )
530             goto err;
531
532         for( j=0; j<t->col_count; j++ )
533         {
534             UINT ofs = t->colinfo[j].offset;
535             UINT n = bytes_per_column( &t->colinfo[j] );
536             UINT k;
537
538             if ( n != 2 && n != 3 && n != 4 )
539             {
540                 ERR("oops - unknown column width %d\n", n);
541                 goto err;
542             }
543
544             for ( k = 0; k < n; k++ )
545                 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
546         }
547     }
548
549     msi_free( rawdata );
550     return ERROR_SUCCESS;
551 err:
552     msi_free( rawdata );
553     return ERROR_FUNCTION_FAILED;
554 }
555
556 void free_cached_tables( MSIDATABASE *db )
557 {
558     while( !list_empty( &db->tables ) )
559     {
560         MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
561
562         list_remove( &t->entry );
563         free_table( t );
564     }
565 }
566
567 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
568 {
569     MSITABLE *t;
570
571     LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
572         if( !lstrcmpW( name, t->name ) )
573             return t;
574
575     return NULL;
576 }
577
578 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
579 {
580     UINT r, column_count = 0;
581     MSICOLUMNINFO *columns;
582
583     /* get the number of columns in this table */
584     column_count = 0;
585     r = get_tablecolumns( db, name, NULL, &column_count );
586     if( r != ERROR_SUCCESS )
587         return r;
588
589     /* if there's no columns, there's no table */
590     if( column_count == 0 )
591         return ERROR_INVALID_PARAMETER;
592
593     TRACE("Table %s found\n", debugstr_w(name) );
594
595     columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
596     if( !columns )
597         return ERROR_FUNCTION_FAILED;
598
599     r = get_tablecolumns( db, name, columns, &column_count );
600     if( r != ERROR_SUCCESS )
601     {
602         msi_free( columns );
603         return ERROR_FUNCTION_FAILED;
604     }
605
606     *pcols = columns;
607     *pcount = column_count;
608
609     return r;
610 }
611
612 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
613                        BOOL persistent, MSITABLE **table_ret)
614 {
615     UINT r, nField;
616     MSIVIEW *tv = NULL;
617     MSIRECORD *rec = NULL;
618     column_info *col;
619     MSITABLE *table;
620     UINT i;
621
622     /* only add tables that don't exist already */
623     if( TABLE_Exists(db, name ) )
624     {
625         WARN("table %s exists\n", debugstr_w(name));
626         return ERROR_BAD_QUERY_SYNTAX;
627     }
628
629     table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
630     if( !table )
631         return ERROR_FUNCTION_FAILED;
632
633     table->row_count = 0;
634     table->data = NULL;
635     table->nonpersistent_row_count = 0;
636     table->nonpersistent_data = NULL;
637     table->colinfo = NULL;
638     table->col_count = 0;
639     table->persistent = persistent;
640     lstrcpyW( table->name, name );
641
642     for( col = col_info; col; col = col->next )
643         table->col_count++;
644
645     table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
646     if (!table->colinfo)
647     {
648         free_table( table );
649         return ERROR_FUNCTION_FAILED;
650     }
651
652     for( i = 0, col = col_info; col; i++, col = col->next )
653     {
654         table->colinfo[ i ].tablename = strdupW( col->table );
655         table->colinfo[ i ].number = i + 1;
656         table->colinfo[ i ].colname = strdupW( col->column );
657         table->colinfo[ i ].type = col->type;
658         table->colinfo[ i ].offset = 0;
659         table->colinfo[ i ].hash_table = NULL;
660     }
661     table_calc_column_offsets( table->colinfo, table->col_count);
662
663     r = TABLE_CreateView( db, szTables, &tv );
664     TRACE("CreateView returned %x\n", r);
665     if( r )
666     {
667         free_table( table );
668         return r;
669     }
670
671     r = tv->ops->execute( tv, 0 );
672     TRACE("tv execute returned %x\n", r);
673     if( r )
674         goto err;
675
676     rec = MSI_CreateRecord( 1 );
677     if( !rec )
678         goto err;
679
680     r = MSI_RecordSetStringW( rec, 1, name );
681     if( r )
682         goto err;
683
684     r = tv->ops->insert_row( tv, rec, !persistent );
685     TRACE("insert_row returned %x\n", r);
686     if( r )
687         goto err;
688
689     tv->ops->delete( tv );
690     tv = NULL;
691
692     msiobj_release( &rec->hdr );
693     rec = NULL;
694
695     if( persistent )
696     {
697         /* add each column to the _Columns table */
698         r = TABLE_CreateView( db, szColumns, &tv );
699         if( r )
700             return r;
701
702         r = tv->ops->execute( tv, 0 );
703         TRACE("tv execute returned %x\n", r);
704         if( r )
705             goto err;
706
707         rec = MSI_CreateRecord( 4 );
708         if( !rec )
709             goto err;
710
711         r = MSI_RecordSetStringW( rec, 1, name );
712         if( r )
713             goto err;
714
715         /*
716          * need to set the table, column number, col name and type
717          * for each column we enter in the table
718          */
719         nField = 1;
720         for( col = col_info; col; col = col->next )
721         {
722             r = MSI_RecordSetInteger( rec, 2, nField );
723             if( r )
724                 goto err;
725
726             r = MSI_RecordSetStringW( rec, 3, col->column );
727             if( r )
728                 goto err;
729
730             r = MSI_RecordSetInteger( rec, 4, col->type );
731             if( r )
732                 goto err;
733
734             r = tv->ops->insert_row( tv, rec, FALSE );
735             if( r )
736                 goto err;
737
738             nField++;
739         }
740         if( !col )
741             r = ERROR_SUCCESS;
742     }
743
744 err:
745     if (rec)
746         msiobj_release( &rec->hdr );
747     /* FIXME: remove values from the string table on error */
748     if( tv )
749         tv->ops->delete( tv );
750
751     if (r == ERROR_SUCCESS)
752     {
753         list_add_head( &db->tables, &table->entry );
754         *table_ret = table;
755     }
756     else
757         free_table( table );
758
759     return r;
760 }
761
762 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
763 {
764     MSITABLE *table;
765     UINT r;
766
767     /* first, see if the table is cached */
768     table = find_cached_table( db, name );
769     if( table )
770     {
771         *table_ret = table;
772         return ERROR_SUCCESS;
773     }
774
775     /* nonexistent tables should be interpreted as empty tables */
776     table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
777     if( !table )
778         return ERROR_FUNCTION_FAILED;
779
780     table->row_count = 0;
781     table->data = NULL;
782     table->nonpersistent_row_count = 0;
783     table->nonpersistent_data = NULL;
784     table->colinfo = NULL;
785     table->col_count = 0;
786     table->persistent = TRUE;
787     lstrcpyW( table->name, name );
788
789     /* these two tables are special - we know the column types already */
790     if( !lstrcmpW( name, szColumns ) )
791     {
792         table->colinfo = _Columns_cols;
793         table->col_count = sizeof(_Columns_cols)/sizeof(_Columns_cols[0]);
794     }
795     else if( !lstrcmpW( name, szTables ) )
796     {
797         table->colinfo = _Tables_cols;
798         table->col_count = sizeof(_Tables_cols)/sizeof(_Tables_cols[0]);
799     }
800     else
801     {
802         r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
803         if (r != ERROR_SUCCESS)
804         {
805             free_table ( table );
806             return r;
807         }
808     }
809
810     r = read_table_from_storage( table, db->storage );
811     if( r != ERROR_SUCCESS )
812     {
813         free_table( table );
814         return r;
815     }
816
817     list_add_head( &db->tables, &table->entry );
818     *table_ret = table;
819     return ERROR_SUCCESS;
820 }
821
822 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
823 {
824     BYTE *rawdata = NULL, *p;
825     UINT rawsize, r, i, j, row_size;
826
827     /* Nothing to do for non-persistent tables */
828     if( !t->persistent )
829         return ERROR_SUCCESS;
830
831     TRACE("Saving %s\n", debugstr_w( t->name ) );
832
833     row_size = msi_table_get_row_size( t->colinfo, t->col_count );
834
835     rawsize = t->row_count * row_size;
836     rawdata = msi_alloc_zero( rawsize );
837     if( !rawdata )
838     {
839         r = ERROR_NOT_ENOUGH_MEMORY;
840         goto err;
841     }
842
843     p = rawdata;
844     for( i=0; i<t->col_count; i++ )
845     {
846         for( j=0; j<t->row_count; j++ )
847         {
848             UINT offset = t->colinfo[i].offset;
849
850             *p++ = t->data[j][offset];
851             *p++ = t->data[j][offset + 1];
852             if( 4 == bytes_per_column( &t->colinfo[i] ) )
853             {
854                 *p++ = t->data[j][offset + 2];
855                 *p++ = t->data[j][offset + 3];
856             }
857         }
858     }
859
860     TRACE("writing %d bytes\n", rawsize);
861     r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
862
863 err:
864     msi_free( rawdata );
865
866     return r;
867 }
868
869 static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count )
870 {
871     DWORD i;
872
873     for( i=0; colinfo && (i<count); i++ )
874     {
875          assert( (i+1) == colinfo[ i ].number );
876          if (i)
877              colinfo[i].offset = colinfo[ i - 1 ].offset
878                                + bytes_per_column( &colinfo[ i - 1 ] );
879          else
880              colinfo[i].offset = 0;
881          TRACE("column %d is [%s] with type %08x ofs %d\n",
882                colinfo[i].number, debugstr_w(colinfo[i].colname),
883                colinfo[i].type, colinfo[i].offset);
884     }
885 }
886
887 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
888 {
889     const MSICOLUMNINFO *p;
890     DWORD i, n;
891
892     TRACE("%s\n", debugstr_w(name));
893
894     if (!lstrcmpW( name, szTables ))
895     {
896         p = _Tables_cols;
897         n = 1;
898     }
899     else if (!lstrcmpW( name, szColumns ))
900     {
901         p = _Columns_cols;
902         n = 4;
903     }
904     else
905         return ERROR_FUNCTION_FAILED;
906
907     /* duplicate the string data so we can free it in msi_free_colinfo */
908     for (i=0; i<n; i++)
909     {
910         if (colinfo && (i < *sz) )
911         {
912             memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
913             colinfo[i].tablename = strdupW( p[i].tablename );
914             colinfo[i].colname = strdupW( p[i].colname );
915         }
916         if( colinfo && (i >= *sz) )
917             break;
918     }
919     table_calc_column_offsets( colinfo, n );
920     *sz = n;
921     return ERROR_SUCCESS;
922 }
923
924 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
925 {
926     UINT i;
927
928     for( i=0; i<count; i++ )
929     {
930         msi_free( (LPWSTR) colinfo[i].tablename );
931         msi_free( (LPWSTR) colinfo[i].colname );
932         msi_free( colinfo[i].hash_table );
933     }
934 }
935
936 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
937 {
938     return strdupW(msi_string_lookup_id( db->strings, stringid ));
939 }
940
941 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
942 {
943     UINT ret = 0, i;
944
945     for (i = 0; i < bytes; i++)
946         ret += (data[row][col + i] << i * 8);
947
948     return ret;
949 }
950
951 static UINT get_tablecolumns( MSIDATABASE *db,
952        LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
953 {
954     UINT r, i, n=0, table_id, count, maxcount = *sz;
955     MSITABLE *table = NULL;
956
957     TRACE("%s\n", debugstr_w(szTableName));
958
959     /* first check if there is a default table with that name */
960     r = get_defaulttablecolumns( szTableName, colinfo, sz );
961     if( ( r == ERROR_SUCCESS ) && *sz )
962         return r;
963
964     r = get_table( db, szColumns, &table );
965     if( r != ERROR_SUCCESS )
966     {
967         ERR("couldn't load _Columns table\n");
968         return ERROR_FUNCTION_FAILED;
969     }
970
971     /* convert table and column names to IDs from the string table */
972     r = msi_string2idW( db->strings, szTableName, &table_id );
973     if( r != ERROR_SUCCESS )
974     {
975         WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
976         return r;
977     }
978
979     TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
980
981     /* Note: _Columns table doesn't have non-persistent data */
982
983     /* if maxcount is non-zero, assume it's exactly right for this table */
984     memset( colinfo, 0, maxcount*sizeof(*colinfo) );
985     count = table->row_count;
986     for( i=0; i<count; i++ )
987     {
988         if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
989             continue;
990         if( colinfo )
991         {
992             UINT id = read_table_int(table->data, i, _Columns_cols[2].offset, db->bytes_per_strref);
993             UINT col = read_table_int(table->data, i, _Columns_cols[1].offset, sizeof(USHORT)) - (1<<15);
994
995             /* check the column number is in range */
996             if (col<1 || col>maxcount)
997             {
998                 ERR("column %d out of range\n", col);
999                 continue;
1000             }
1001
1002             /* check if this column was already set */
1003             if (colinfo[ col - 1 ].number)
1004             {
1005                 ERR("duplicate column %d\n", col);
1006                 continue;
1007             }
1008
1009             colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
1010             colinfo[ col - 1 ].number = col;
1011             colinfo[ col - 1 ].colname = msi_makestring( db, id );
1012             colinfo[ col - 1 ].type = read_table_int(table->data, i, _Columns_cols[3].offset, sizeof(USHORT)) - (1<<15);
1013             colinfo[ col - 1 ].offset = 0;
1014             colinfo[ col - 1 ].hash_table = NULL;
1015         }
1016         n++;
1017     }
1018
1019     TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
1020
1021     if (colinfo && n != maxcount)
1022     {
1023         ERR("missing column in table %s\n", debugstr_w(szTableName));
1024         msi_free_colinfo(colinfo, maxcount );
1025         return ERROR_FUNCTION_FAILED;
1026     }
1027
1028     table_calc_column_offsets( colinfo, n );
1029     *sz = n;
1030
1031     return ERROR_SUCCESS;
1032 }
1033
1034 /* try to find the table name in the _Tables table */
1035 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1036 {
1037     UINT r, table_id = 0, i, count;
1038     MSITABLE *table = NULL;
1039
1040     if( !lstrcmpW( name, szTables ) )
1041         return TRUE;
1042     if( !lstrcmpW( name, szColumns ) )
1043         return TRUE;
1044
1045     r = msi_string2idW( db->strings, name, &table_id );
1046     if( r != ERROR_SUCCESS )
1047     {
1048         TRACE("Couldn't find id for %s\n", debugstr_w(name));
1049         return FALSE;
1050     }
1051
1052     r = get_table( db, szTables, &table );
1053     if( r != ERROR_SUCCESS )
1054     {
1055         ERR("table %s not available\n", debugstr_w(szTables));
1056         return FALSE;
1057     }
1058
1059     count = table->row_count;
1060     for( i=0; i<count; i++ )
1061         if( table->data[ i ][ 0 ] == table_id )
1062             break;
1063
1064     if (i!=count)
1065         return TRUE;
1066
1067     count = table->nonpersistent_row_count;
1068     for( i=0; i<count; i++ )
1069         if( table->nonpersistent_data[ i ][ 0 ] == table_id )
1070             break;
1071
1072     if (i!=count)
1073         return TRUE;
1074
1075     TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1076
1077     return FALSE;
1078 }
1079
1080 /* below is the query interface to a table */
1081
1082 typedef struct tagMSITABLEVIEW
1083 {
1084     MSIVIEW        view;
1085     MSIDATABASE   *db;
1086     MSITABLE      *table;
1087     MSICOLUMNINFO *columns;
1088     UINT           num_cols;
1089     UINT           row_size;
1090     WCHAR          name[1];
1091 } MSITABLEVIEW;
1092
1093 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1094 {
1095     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1096     UINT offset, n;
1097     BYTE **data;
1098
1099     if( !tv->table )
1100         return ERROR_INVALID_PARAMETER;
1101
1102     if( (col==0) || (col>tv->num_cols) )
1103         return ERROR_INVALID_PARAMETER;
1104
1105     /* how many rows are there ? */
1106     if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1107         return ERROR_NO_MORE_ITEMS;
1108
1109     if( tv->columns[col-1].offset >= tv->row_size )
1110     {
1111         ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1112         ERR("%p %p\n", tv, tv->columns );
1113         return ERROR_FUNCTION_FAILED;
1114     }
1115
1116     if (row >= tv->table->row_count)
1117     {
1118         row -= tv->table->row_count;
1119         data = tv->table->nonpersistent_data;
1120     }
1121     else
1122         data = tv->table->data;
1123
1124     n = bytes_per_column( &tv->columns[col-1] );
1125     if (n != 2 && n != 3 && n != 4)
1126     {
1127         ERR("oops! what is %d bytes per column?\n", n );
1128         return ERROR_FUNCTION_FAILED;
1129     }
1130
1131     offset = tv->columns[col-1].offset;
1132     *val = read_table_int(data, row, offset, n);
1133
1134     /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1135
1136     return ERROR_SUCCESS;
1137 }
1138
1139 /*
1140  * We need a special case for streams, as we need to reference column with
1141  * the name of the stream in the same table, and the table name
1142  * which may not be available at higher levels of the query
1143  */
1144 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1145 {
1146     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1147     UINT ival = 0, refcol = 0, r;
1148     LPCWSTR sval;
1149     LPWSTR full_name;
1150     DWORD len;
1151     static const WCHAR szDot[] = { '.', 0 };
1152     WCHAR number[0x20];
1153
1154     if( !view->ops->fetch_int )
1155         return ERROR_INVALID_PARAMETER;
1156
1157     /*
1158      * The column marked with the type stream data seems to have a single number
1159      * which references the column containing the name of the stream data
1160      *
1161      * Fetch the column to reference first.
1162      */
1163     r = view->ops->fetch_int( view, row, col, &ival );
1164     if( r != ERROR_SUCCESS )
1165         return r;
1166
1167     /* check the column value is in range */
1168     if (ival < 0 || ival > tv->num_cols || ival == col)
1169     {
1170         ERR("bad column ref (%u) for stream\n", ival);
1171         return ERROR_FUNCTION_FAILED;
1172     }
1173
1174     if ( tv->columns[ival - 1].type & MSITYPE_STRING )
1175     {
1176         /* now get the column with the name of the stream */
1177         r = view->ops->fetch_int( view, row, ival, &refcol );
1178         if ( r != ERROR_SUCCESS )
1179             return r;
1180
1181         /* lookup the string value from the string table */
1182         sval = msi_string_lookup_id( tv->db->strings, refcol );
1183         if ( !sval )
1184             return ERROR_INVALID_PARAMETER;
1185     }
1186     else
1187     {
1188         static const WCHAR fmt[] = { '%','d',0 };
1189         sprintfW( number, fmt, ival );
1190         sval = number;
1191     }
1192
1193     len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1194     full_name = msi_alloc( len*sizeof(WCHAR) );
1195     lstrcpyW( full_name, tv->name );
1196     lstrcatW( full_name, szDot );
1197     lstrcatW( full_name, sval );
1198
1199     r = db_get_raw_stream( tv->db, full_name, stm );
1200     if( r )
1201         ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1202     msi_free( full_name );
1203
1204     return r;
1205 }
1206
1207 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1208 {
1209     UINT offset, n, i;
1210     BYTE **data;
1211
1212     if( !tv->table )
1213         return ERROR_INVALID_PARAMETER;
1214
1215     if( (col==0) || (col>tv->num_cols) )
1216         return ERROR_INVALID_PARAMETER;
1217
1218     if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1219         return ERROR_INVALID_PARAMETER;
1220
1221     if( tv->columns[col-1].offset >= tv->row_size )
1222     {
1223         ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1224         ERR("%p %p\n", tv, tv->columns );
1225         return ERROR_FUNCTION_FAILED;
1226     }
1227
1228     msi_free( tv->columns[col-1].hash_table );
1229     tv->columns[col-1].hash_table = NULL;
1230
1231     if (row >= tv->table->row_count)
1232     {
1233         row -= tv->table->row_count;
1234         data = tv->table->nonpersistent_data;
1235     }
1236     else
1237         data = tv->table->data;
1238
1239     n = bytes_per_column( &tv->columns[col-1] );
1240     if ( n != 2 && n != 3 && n != 4 )
1241     {
1242         ERR("oops! what is %d bytes per column?\n", n );
1243         return ERROR_FUNCTION_FAILED;
1244     }
1245
1246     offset = tv->columns[col-1].offset;
1247     for ( i = 0; i < n; i++ )
1248         data[row][offset + i] = (val >> i * 8) & 0xff;
1249
1250     return ERROR_SUCCESS;
1251 }
1252
1253 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1254 {
1255     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1256     UINT i, val, r = ERROR_SUCCESS;
1257
1258     if ( !tv->table )
1259         return ERROR_INVALID_PARAMETER;
1260
1261     /* test if any of the mask bits are invalid */
1262     if ( mask >= (1<<tv->num_cols) )
1263         return ERROR_INVALID_PARAMETER;
1264
1265     for ( i = 0; i < tv->num_cols; i++ )
1266     {
1267         BOOL persistent;
1268
1269         /* only update the fields specified in the mask */
1270         if ( !(mask&(1<<i)) )
1271             continue;
1272
1273         /* if row >= tv->table->row_count then it is a non-persistent row */
1274         persistent = tv->table->persistent && (row < tv->table->row_count);
1275         /* FIXME: should we allow updating keys? */
1276
1277         val = 0;
1278         if ( !MSI_RecordIsNull( rec, i + 1 ) )
1279         {
1280             if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1281             {
1282                 val = 1; /* refers to the first key column */
1283             }
1284             else if ( tv->columns[i].type & MSITYPE_STRING )
1285             {
1286                 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1287                 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
1288                                       persistent ? StringPersistent : StringNonPersistent );
1289             }
1290             else if ( 2 == bytes_per_column( &tv->columns[ i ] ) )
1291             {
1292                 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1293                 if ( val & 0xffff0000 )
1294                 {
1295                     ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1296                     return ERROR_FUNCTION_FAILED;
1297                 }
1298             }
1299             else
1300             {
1301                 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1302                 val = ival ^ 0x80000000;
1303             }
1304         }
1305
1306         r = TABLE_set_int( tv, row, i+1, val );
1307         if ( r != ERROR_SUCCESS )
1308             break;
1309     }
1310     return r;
1311 }
1312
1313 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1314 {
1315     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1316     BYTE **p, *row;
1317     UINT sz;
1318     BYTE ***data_ptr;
1319     UINT *row_count;
1320
1321     TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1322
1323     if( !tv->table )
1324         return ERROR_INVALID_PARAMETER;
1325
1326     row = msi_alloc_zero( tv->row_size );
1327     if( !row )
1328         return ERROR_NOT_ENOUGH_MEMORY;
1329
1330     if( temporary )
1331     {
1332         row_count = &tv->table->nonpersistent_row_count;
1333         data_ptr = &tv->table->nonpersistent_data;
1334         *num = tv->table->row_count + tv->table->nonpersistent_row_count;
1335     }
1336     else
1337     {
1338         row_count = &tv->table->row_count;
1339         data_ptr = &tv->table->data;
1340         *num = tv->table->row_count;
1341     }
1342
1343     sz = (*row_count + 1) * sizeof (BYTE*);
1344     if( *data_ptr )
1345         p = msi_realloc( *data_ptr, sz );
1346     else
1347         p = msi_alloc( sz );
1348     if( !p )
1349     {
1350         msi_free( row );
1351         return ERROR_NOT_ENOUGH_MEMORY;
1352     }
1353
1354     *data_ptr = p;
1355     (*data_ptr)[*row_count] = row;
1356     (*row_count)++;
1357
1358     return ERROR_SUCCESS;
1359 }
1360
1361 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1362 {
1363     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1364
1365     TRACE("%p %p\n", tv, record);
1366
1367     TRACE("There are %d columns\n", tv->num_cols );
1368
1369     return ERROR_SUCCESS;
1370 }
1371
1372 static UINT TABLE_close( struct tagMSIVIEW *view )
1373 {
1374     TRACE("%p\n", view );
1375     
1376     return ERROR_SUCCESS;
1377 }
1378
1379 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1380 {
1381     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1382
1383     TRACE("%p %p %p\n", view, rows, cols );
1384
1385     if( cols )
1386         *cols = tv->num_cols;
1387     if( rows )
1388     {
1389         if( !tv->table )
1390             return ERROR_INVALID_PARAMETER;
1391         *rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1392     }
1393
1394     return ERROR_SUCCESS;
1395 }
1396
1397 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1398                 UINT n, LPWSTR *name, UINT *type )
1399 {
1400     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1401
1402     TRACE("%p %d %p %p\n", tv, n, name, type );
1403
1404     if( ( n == 0 ) || ( n > tv->num_cols ) )
1405         return ERROR_INVALID_PARAMETER;
1406
1407     if( name )
1408     {
1409         *name = strdupW( tv->columns[n-1].colname );
1410         if( !*name )
1411             return ERROR_FUNCTION_FAILED;
1412     }
1413     if( type )
1414         *type = tv->columns[n-1].type;
1415
1416     return ERROR_SUCCESS;
1417 }
1418
1419 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1420
1421 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1422 {
1423     UINT r, row, i;
1424
1425     /* check there's no null values where they're not allowed */
1426     for( i = 0; i < tv->num_cols; i++ )
1427     {
1428         if ( tv->columns[i].type & MSITYPE_NULLABLE )
1429             continue;
1430
1431         if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1432             TRACE("skipping binary column\n");
1433         else if ( tv->columns[i].type & MSITYPE_STRING )
1434         {
1435             LPCWSTR str;
1436
1437             str = MSI_RecordGetString( rec, i+1 );
1438             if (str == NULL || str[0] == 0)
1439                 return ERROR_INVALID_DATA;
1440         }
1441         else
1442         {
1443             UINT n;
1444
1445             n = MSI_RecordGetInteger( rec, i+1 );
1446             if (n == MSI_NULL_INTEGER)
1447                 return ERROR_INVALID_DATA;
1448         }
1449     }
1450
1451     /* check there's no duplicate keys */
1452     r = msi_table_find_row( tv, rec, &row );
1453     if (r == ERROR_SUCCESS)
1454         return ERROR_INVALID_DATA;
1455
1456     return ERROR_SUCCESS;
1457 }
1458
1459 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary )
1460 {
1461     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1462     UINT r, row = -1;
1463
1464     TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1465
1466     /* check that the key is unique - can we find a matching row? */
1467     r = table_validate_new( tv, rec );
1468     if( r != ERROR_SUCCESS )
1469         return ERROR_FUNCTION_FAILED;
1470
1471     r = table_create_new_row( view, &row, temporary );
1472     TRACE("insert_row returned %08x\n", r);
1473     if( r != ERROR_SUCCESS )
1474         return r;
1475
1476     return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1477 }
1478
1479 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1480 {
1481     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1482     UINT r, num_rows, num_cols;
1483     BYTE *src, *dest;
1484
1485     TRACE("%p %d\n", tv, row);
1486
1487     if ( !tv->table )
1488         return ERROR_INVALID_PARAMETER;
1489
1490     r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1491     if ( r != ERROR_SUCCESS )
1492         return r;
1493
1494     if ( row >= num_rows )
1495         return ERROR_FUNCTION_FAILED;
1496
1497     tv->table->row_count--;
1498
1499     if ( row == num_rows - 1 )
1500         return ERROR_SUCCESS;
1501
1502     dest = tv->table->data[row];
1503     src = tv->table->data[row + 1];
1504     memmove(dest, src, (num_rows - row - 1) * tv->row_size);
1505     return ERROR_SUCCESS;
1506 }
1507
1508 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1509                 MSIRECORD *rec)
1510 {
1511     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1512     UINT r;
1513
1514     TRACE("%p %d %p\n", view, eModifyMode, rec );
1515
1516     switch (eModifyMode)
1517     {
1518     case MSIMODIFY_VALIDATE_NEW:
1519         r = table_validate_new( tv, rec );
1520         break;
1521
1522     case MSIMODIFY_INSERT_TEMPORARY:
1523         r = table_validate_new( tv, rec );
1524         if (r != ERROR_SUCCESS)
1525             break;
1526         r = TABLE_insert_row( view, rec, TRUE );
1527         break;
1528
1529     case MSIMODIFY_REFRESH:
1530     case MSIMODIFY_INSERT:
1531     case MSIMODIFY_UPDATE:
1532     case MSIMODIFY_ASSIGN:
1533     case MSIMODIFY_REPLACE:
1534     case MSIMODIFY_MERGE:
1535     case MSIMODIFY_DELETE:
1536     case MSIMODIFY_VALIDATE:
1537     case MSIMODIFY_VALIDATE_FIELD:
1538     case MSIMODIFY_VALIDATE_DELETE:
1539         FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1540         r = ERROR_CALL_NOT_IMPLEMENTED;
1541         break;
1542
1543     default:
1544         r = ERROR_INVALID_DATA;
1545     }
1546
1547     return r;
1548 }
1549
1550 static UINT TABLE_delete( struct tagMSIVIEW *view )
1551 {
1552     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1553
1554     TRACE("%p\n", view );
1555
1556     tv->table = NULL;
1557
1558     tv->columns = NULL;
1559
1560     msi_free( tv );
1561
1562     return ERROR_SUCCESS;
1563 }
1564
1565 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1566     UINT val, UINT *row, MSIITERHANDLE *handle )
1567 {
1568     MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1569     const MSICOLUMNHASHENTRY *entry;
1570
1571     TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1572
1573     if( !tv->table )
1574         return ERROR_INVALID_PARAMETER;
1575
1576     if( (col==0) || (col > tv->num_cols) )
1577         return ERROR_INVALID_PARAMETER;
1578
1579     if( !tv->columns[col-1].hash_table )
1580     {
1581         UINT i;
1582         UINT num_rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1583         MSICOLUMNHASHENTRY **hash_table;
1584         MSICOLUMNHASHENTRY *new_entry;
1585
1586         if( tv->columns[col-1].offset >= tv->row_size )
1587         {
1588             ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1589             ERR("%p %p\n", tv, tv->columns );
1590             return ERROR_FUNCTION_FAILED;
1591         }
1592
1593         /* allocate contiguous memory for the table and its entries so we
1594          * don't have to do an expensive cleanup */
1595         hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1596             num_rows * sizeof(MSICOLUMNHASHENTRY));
1597         if (!hash_table)
1598             return ERROR_OUTOFMEMORY;
1599
1600         memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1601         tv->columns[col-1].hash_table = hash_table;
1602
1603         new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1604
1605         for (i = 0; i < num_rows; i++, new_entry++)
1606         {
1607             UINT row_value;
1608
1609             if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1610                 continue;
1611
1612             new_entry->next = NULL;
1613             new_entry->value = row_value;
1614             new_entry->row = i;
1615             if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1616             {
1617                 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1618                 while (prev_entry->next)
1619                     prev_entry = prev_entry->next;
1620                 prev_entry->next = new_entry;
1621             }
1622             else
1623                 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1624         }
1625     }
1626
1627     if( !*handle )
1628         entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1629     else
1630         entry = (*handle)->next;
1631
1632     while (entry && entry->value != val)
1633         entry = entry->next;
1634
1635     *handle = entry;
1636     if (!entry)
1637         return ERROR_NO_MORE_ITEMS;
1638
1639     *row = entry->row;
1640     return ERROR_SUCCESS;
1641 }
1642
1643
1644 static const MSIVIEWOPS table_ops =
1645 {
1646     TABLE_fetch_int,
1647     TABLE_fetch_stream,
1648     TABLE_set_row,
1649     TABLE_insert_row,
1650     TABLE_delete_row,
1651     TABLE_execute,
1652     TABLE_close,
1653     TABLE_get_dimensions,
1654     TABLE_get_column_info,
1655     TABLE_modify,
1656     TABLE_delete,
1657     TABLE_find_matching_rows
1658 };
1659
1660 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1661 {
1662     MSITABLEVIEW *tv ;
1663     UINT r, sz;
1664
1665     static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
1666
1667     TRACE("%p %s %p\n", db, debugstr_w(name), view );
1668
1669     if ( !lstrcmpW( name, Streams ) )
1670         return STREAMS_CreateView( db, view );
1671
1672     sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1673     tv = msi_alloc_zero( sz );
1674     if( !tv )
1675         return ERROR_FUNCTION_FAILED;
1676
1677     r = get_table( db, name, &tv->table );
1678     if( r != ERROR_SUCCESS )
1679     {
1680         msi_free( tv );
1681         WARN("table not found\n");
1682         return r;
1683     }
1684
1685     TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
1686
1687     /* fill the structure */
1688     tv->view.ops = &table_ops;
1689     tv->db = db;
1690     tv->columns = tv->table->colinfo;
1691     tv->num_cols = tv->table->col_count;
1692     tv->row_size = msi_table_get_row_size( tv->table->colinfo, tv->table->col_count );
1693
1694     TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
1695
1696     *view = (MSIVIEW*) tv;
1697     lstrcpyW( tv->name, name );
1698
1699     return ERROR_SUCCESS;
1700 }
1701
1702 UINT MSI_CommitTables( MSIDATABASE *db )
1703 {
1704     UINT r;
1705     MSITABLE *table = NULL;
1706
1707     TRACE("%p\n",db);
1708
1709     r = msi_save_string_table( db->strings, db->storage );
1710     if( r != ERROR_SUCCESS )
1711     {
1712         WARN("failed to save string table r=%08x\n",r);
1713         return r;
1714     }
1715
1716     LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1717     {
1718         r = save_table( db, table );
1719         if( r != ERROR_SUCCESS )
1720         {
1721             WARN("failed to save table %s (r=%08x)\n",
1722                   debugstr_w(table->name), r);
1723             return r;
1724         }
1725     }
1726
1727     /* force everything to reload next time */
1728     free_cached_tables( db );
1729
1730     return ERROR_SUCCESS;
1731 }
1732
1733 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
1734 {
1735     MSITABLE *t;
1736     UINT r;
1737
1738     TRACE("%p %s\n", db, debugstr_w(table));
1739
1740     if (!table)
1741         return MSICONDITION_ERROR;
1742
1743     r = get_table( db, table, &t );
1744     if (r != ERROR_SUCCESS)
1745         return MSICONDITION_NONE;
1746
1747     if (t->persistent)
1748         return MSICONDITION_TRUE;
1749     else
1750         return MSICONDITION_FALSE;
1751 }
1752
1753 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
1754                                             const USHORT *rawdata )
1755 {
1756     UINT i, val, ofs = 0;
1757     USHORT mask = *rawdata++;
1758     MSICOLUMNINFO *columns = tv->columns;
1759     MSIRECORD *rec;
1760
1761     rec = MSI_CreateRecord( tv->num_cols );
1762     if( !rec )
1763         return rec;
1764
1765     TRACE("row ->\n");
1766     for( i=0; i<tv->num_cols; i++ )
1767     {
1768         UINT n = bytes_per_column( &columns[i] );
1769
1770         if ( (mask&1) && (i>=(mask>>8)) )
1771             break;
1772         /* all keys must be present */
1773         if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
1774             continue;
1775
1776         switch( n )
1777         {
1778         case 2:
1779             val = rawdata[ofs];
1780             if( (columns[i].type & MSITYPE_STRING) &&
1781                 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1782             {
1783                 LPCWSTR sval = msi_string_lookup_id( st, val );
1784                 MSI_RecordSetStringW( rec, i+1, sval );
1785                 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
1786             }
1787             else
1788             {
1789                 if (val)
1790                     MSI_RecordSetInteger( rec, i+1, val^0x8000 );
1791                 TRACE(" field %d [0x%04x]\n", i+1, val );
1792             }
1793             break;
1794         case 4:
1795             val = (rawdata[ofs] + (rawdata[ofs + 1]<<16));
1796             if (val)
1797                 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
1798             TRACE(" field %d [0x%08x]\n", i+1, val );
1799             break;
1800         default:
1801             ERR("oops - unknown column width %d\n", n);
1802             break;
1803         }
1804         ofs += n/2;
1805     }
1806     return rec;
1807 }
1808
1809 static void dump_record( MSIRECORD *rec )
1810 {
1811     UINT i, n;
1812
1813     n = MSI_RecordGetFieldCount( rec );
1814     for( i=1; i<=n; i++ )
1815     {
1816         LPCWSTR sval = MSI_RecordGetString( rec, i );
1817
1818         if( MSI_RecordIsNull( rec, i ) )
1819             TRACE("row -> []\n");
1820         else if( (sval = MSI_RecordGetString( rec, i )) )
1821             TRACE("row -> [%s]\n", debugstr_w(sval));
1822         else
1823             TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
1824     }
1825 }
1826
1827 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
1828 {
1829     LPCWSTR sval;
1830     UINT i;
1831
1832     for( i=0; i<(rawsize/2); i++ )
1833     {
1834         sval = msi_string_lookup_id( st, rawdata[i] );
1835         MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
1836     }
1837 }
1838
1839 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
1840 {
1841     LPCWSTR str;
1842     UINT i, r, *data;
1843
1844     data = msi_alloc( tv->num_cols *sizeof (UINT) );
1845     for( i=0; i<tv->num_cols; i++ )
1846     {
1847         data[i] = 0;
1848
1849         if ( ~tv->columns[i].type & MSITYPE_KEY )
1850             continue;
1851
1852         /* turn the transform column value into a row value */
1853         if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
1854              ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1855         {
1856             str = MSI_RecordGetString( rec, i+1 );
1857             r = msi_string2idW( tv->db->strings, str, &data[i] );
1858
1859             /* if there's no matching string in the string table,
1860                these keys can't match any record, so fail now. */
1861             if( ERROR_SUCCESS != r )
1862             {
1863                 msi_free( data );
1864                 return NULL;
1865             }
1866         }
1867         else
1868         {
1869             data[i] = MSI_RecordGetInteger( rec, i+1 );
1870             if ((tv->columns[i].type&0xff) == 2)
1871                 data[i] += 0x8000;
1872             else
1873                 data[i] += 0x80000000;
1874         }
1875     }
1876     return data;
1877 }
1878
1879 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
1880 {
1881     UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
1882
1883     for( i=0; i<tv->num_cols; i++ )
1884     {
1885         if ( ~tv->columns[i].type & MSITYPE_KEY )
1886             continue;
1887
1888         /* turn the transform column value into a row value */
1889         r = TABLE_fetch_int( &tv->view, row, i+1, &x );
1890         if ( r != ERROR_SUCCESS )
1891         {
1892             ERR("TABLE_fetch_int shouldn't fail here\n");
1893             break;
1894         }
1895
1896         /* if this key matches, move to the next column */
1897         if ( x != data[i] )
1898         {
1899             ret = ERROR_FUNCTION_FAILED;
1900             break;
1901         }
1902
1903         ret = ERROR_SUCCESS;
1904     }
1905
1906     return ret;
1907 }
1908
1909 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
1910 {
1911     UINT i, r = ERROR_FUNCTION_FAILED, *data;
1912
1913     data = msi_record_to_row( tv, rec );
1914     if( !data )
1915         return r;
1916     for( i = 0; i < tv->table->row_count + tv->table->nonpersistent_row_count; i++ )
1917     {
1918         r = msi_row_matches( tv, i, data );
1919         if( r == ERROR_SUCCESS )
1920         {
1921             *row = i;
1922             break;
1923         }
1924     }
1925     msi_free( data );
1926     return r;
1927 }
1928
1929 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
1930                                       string_table *st, LPCWSTR name )
1931 {
1932     UINT rawsize = 0;
1933     USHORT *rawdata = NULL;
1934     MSITABLEVIEW *tv = NULL;
1935     UINT r, n, sz, i, mask;
1936     MSIRECORD *rec = NULL;
1937     UINT colcol = 0;
1938     WCHAR coltable[32];
1939
1940     coltable[0] = 0;
1941     TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
1942
1943     /* read the transform data */
1944     read_stream_data( stg, name, &rawdata, &rawsize );
1945     if ( !rawdata )
1946     {
1947         TRACE("table %s empty\n", debugstr_w(name) );
1948         return ERROR_INVALID_TABLE;
1949     }
1950
1951     /* create a table view */
1952     r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
1953     if( r != ERROR_SUCCESS )
1954         goto err;
1955
1956     r = tv->view.ops->execute( &tv->view, NULL );
1957     if( r != ERROR_SUCCESS )
1958         goto err;
1959
1960     TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1961           debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
1962
1963     /* interpret the data */
1964     r = ERROR_SUCCESS;
1965     for( n=0; n < (rawsize/2);  )
1966     {
1967         mask = rawdata[n];
1968
1969         if (mask&1)
1970         {
1971             /*
1972              * if the low bit is set, columns are continuous and
1973              * the number of columns is specified in the high byte
1974              */
1975             sz = 2 + tv->row_size;
1976         }
1977         else
1978         {
1979             /*
1980              * If the low bit is not set, rowdata[n] is a bitmask.
1981              * Excepting for key fields, which are always present,
1982              *  each bit indicates that a field is present in the transform record.
1983              *
1984              * rawdata[n] == 0 is a special case ... only the keys will be present
1985              * and it means that this row should be deleted.
1986              */
1987             sz = 2;
1988             for( i=0; i<tv->num_cols; i++ )
1989             {
1990                 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
1991                     sz += bytes_per_column( &tv->columns[i] );
1992             }
1993         }
1994
1995         /* check we didn't run of the end of the table */
1996         if ( (n+sz) > rawsize )
1997         {
1998             ERR("borked.\n");
1999             dump_table( st, rawdata, rawsize );
2000             break;
2001         }
2002
2003         rec = msi_get_transform_record( tv, st, &rawdata[n] );
2004         if (rec)
2005         {
2006             if ( mask & 1 )
2007             {
2008                 TRACE("inserting record\n");
2009
2010                 /*
2011                  * Native msi seems writes nul into the
2012                  * Number (2nd) column of the _Columns table.
2013                  * Not sure that it's deliberate...
2014                  */
2015                 if (!lstrcmpW(name, szColumns))
2016                 {
2017                     WCHAR table[32];
2018                     DWORD sz = 32;
2019
2020                     MSI_RecordGetStringW( rec, 1, table, &sz );
2021
2022                     /* reset the column number on a new table */
2023                     if ( lstrcmpW(coltable, table) )
2024                     {
2025                         colcol = 0;
2026                         lstrcpyW( coltable, table );
2027                     }
2028
2029                     /* fix nul column numbers */
2030                     MSI_RecordSetInteger( rec, 2, ++colcol );
2031                 }
2032
2033                 r = TABLE_insert_row( &tv->view, rec, FALSE );
2034                 if (r != ERROR_SUCCESS)
2035                     ERR("insert row failed\n");
2036             }
2037             else
2038             {
2039                 UINT row = 0;
2040
2041                 r = msi_table_find_row( tv, rec, &row );
2042                 if (r != ERROR_SUCCESS)
2043                     ERR("no matching row to transform\n");
2044                 else if ( mask )
2045                 {
2046                     TRACE("modifying row [%d]:\n", row);
2047                     TABLE_set_row( &tv->view, row, rec, mask );
2048                 }
2049                 else
2050                 {
2051                     TRACE("deleting row [%d]:\n", row);
2052                     TABLE_delete_row( &tv->view, row );
2053                 }
2054             }
2055             if( TRACE_ON(msidb) ) dump_record( rec );
2056             msiobj_release( &rec->hdr );
2057         }
2058
2059         n += sz/2;
2060     }
2061
2062 err:
2063     /* no need to free the table, it's associated with the database */
2064     msi_free( rawdata );
2065     if( tv )
2066         tv->view.ops->delete( &tv->view );
2067
2068     return ERROR_SUCCESS;
2069 }
2070
2071 /*
2072  * msi_table_apply_transform
2073  *
2074  * Enumerate the table transforms in a transform storage and apply each one.
2075  */
2076 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2077 {
2078     IEnumSTATSTG *stgenum = NULL;
2079     HRESULT r;
2080     STATSTG stat;
2081     ULONG count;
2082     WCHAR name[0x40];
2083     string_table *strings;
2084     UINT ret = ERROR_FUNCTION_FAILED;
2085
2086     TRACE("%p %p\n", db, stg );
2087
2088     strings = msi_load_string_table( stg, &db->bytes_per_strref );
2089     if( !strings )
2090         goto end;
2091
2092     r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2093     if( FAILED( r ) )
2094         goto end;
2095
2096     /*
2097      * Apply _Tables and _Columns transforms first so that
2098      * the table metadata is correct, and empty tables exist.
2099      */
2100     ret = msi_table_load_transform( db, stg, strings, szTables );
2101     if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2102         goto end;
2103
2104     ret = msi_table_load_transform( db, stg, strings, szColumns );
2105     if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2106         goto end;
2107
2108     ret = ERROR_SUCCESS;
2109
2110     while( r == ERROR_SUCCESS )
2111     {
2112         count = 0;
2113         r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2114         if( FAILED( r ) || !count )
2115             break;
2116
2117         decode_streamname( stat.pwcsName, name );
2118         if ( name[0] != 0x4840 )
2119             continue;
2120
2121         TRACE("transform contains stream %s\n", debugstr_w(name));
2122
2123         if ( !lstrcmpW( name+1, szStringPool ) ||
2124              !lstrcmpW( name+1, szStringData ) ||
2125              !lstrcmpW( name+1, szColumns ) ||
2126              !lstrcmpW( name+1, szTables ) )
2127             continue;
2128
2129         ret = msi_table_load_transform( db, stg, strings, name+1 );
2130     }
2131
2132     if ( ret == ERROR_SUCCESS )
2133         append_storage_to_db( db, stg );
2134
2135 end:
2136     if ( stgenum )
2137         IEnumSTATSTG_Release( stgenum );
2138     if ( strings )
2139         msi_destroy_stringtable( strings );
2140
2141     return ret;
2142 }
2143
2144 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
2145 {
2146     MSITRANSFORM *t;
2147
2148     t = msi_alloc( sizeof *t );
2149     t->stg = stg;
2150     IStorage_AddRef( stg );
2151     list_add_tail( &db->transforms, &t->entry );
2152 }
2153
2154 void msi_free_transforms( MSIDATABASE *db )
2155 {
2156     while( !list_empty( &db->transforms ) )
2157     {
2158         MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2159                                       MSITRANSFORM, entry );
2160         list_remove( &t->entry );
2161         IStorage_Release( t->stg );
2162         msi_free( t );
2163     }
2164 }