2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2005 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
44 #define MSITABLE_HASH_TABLE_SIZE 37
46 typedef struct tagMSICOLUMNHASHENTRY
48 struct tagMSICOLUMNHASHENTRY *next;
53 typedef struct tagMSICOLUMNINFO
60 MSICOLUMNHASHENTRY **hash_table;
71 typedef struct tagMSITRANSFORM {
76 static const WCHAR szStringData[] = {
77 '_','S','t','r','i','n','g','D','a','t','a',0 };
78 static const WCHAR szStringPool[] = {
79 '_','S','t','r','i','n','g','P','o','o','l',0 };
81 #define MAX_STREAM_NAME 0x1f
83 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
84 MSICOLUMNINFO **pcols, UINT *pcount );
85 static UINT get_tablecolumns( MSIDATABASE *db,
86 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
87 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
89 static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
91 if( col->type & MSITYPE_STRING )
93 if( (col->type & 0xff) > 4 )
94 ERR("Invalid column size!\n");
95 return col->type & 0xff;
98 static int utf2mime(int x)
100 if( (x>='0') && (x<='9') )
102 if( (x>='A') && (x<='Z') )
104 if( (x>='a') && (x<='z') )
113 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
115 DWORD count = MAX_STREAM_NAME;
120 count = lstrlenW( in )+2;
121 out = msi_alloc( count*sizeof(WCHAR) );
137 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
139 ch = utf2mime(ch) + 0x4800;
141 if( next && (next<0x80) )
143 next = utf2mime(next);
154 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
159 static int mime2utf(int x)
166 return x - 10 - 26 + 'a';
167 if( x == (10+26+26) )
172 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
177 while ( (ch = *in++) )
179 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
182 ch = mime2utf(ch-0x4800);
186 *out++ = mime2utf(ch&0x3f);
188 ch = mime2utf((ch>>6)&0x3f);
198 void enum_stream_names( IStorage *stg )
200 IEnumSTATSTG *stgenum = NULL;
206 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
214 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
215 if( FAILED( r ) || !count )
217 decode_streamname( stat.pwcsName, name );
218 TRACE("stream %2d -> %s %s\n", n,
219 debugstr_w(stat.pwcsName), debugstr_w(name) );
223 IEnumSTATSTG_Release( stgenum );
226 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
227 USHORT **pdata, UINT *psz )
230 UINT ret = ERROR_FUNCTION_FAILED;
237 encname = encode_streamname(TRUE, stname);
239 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
241 r = IStorage_OpenStream(stg, encname, NULL,
242 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
246 WARN("open stream failed r = %08x - empty table?\n", r);
250 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
253 WARN("open stream failed r = %08x!\n", r);
257 if( stat.cbSize.QuadPart >> 32 )
263 sz = stat.cbSize.QuadPart;
264 data = msi_alloc( sz );
267 WARN("couldn't allocate memory r=%08x!\n", r);
268 ret = ERROR_NOT_ENOUGH_MEMORY;
272 r = IStream_Read(stm, data, sz, &count );
273 if( FAILED( r ) || ( count != sz ) )
276 WARN("read stream failed r = %08x!\n", r);
285 IStream_Release( stm );
290 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
295 encname = encode_streamname(FALSE, stname);
297 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
299 r = IStorage_OpenStream(db->storage, encname, NULL,
300 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
303 MSITRANSFORM *transform;
305 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
307 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
308 r = IStorage_OpenStream( transform->stg, encname, NULL,
309 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
317 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
320 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
321 USHORT **pdata, UINT *psz )
324 UINT ret = ERROR_FUNCTION_FAILED;
330 r = db_get_raw_stream( db, stname, &stm );
331 if( r != ERROR_SUCCESS)
333 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
336 WARN("open stream failed r = %08x!\n", r);
340 if( stat.cbSize.QuadPart >> 32 )
346 sz = stat.cbSize.QuadPart;
347 data = msi_alloc( sz );
350 WARN("couldn't allocate memory r=%08x!\n", r);
351 ret = ERROR_NOT_ENOUGH_MEMORY;
355 r = IStream_Read(stm, data, sz, &count );
356 if( FAILED( r ) || ( count != sz ) )
359 WARN("read stream failed r = %08x!\n", r);
368 IStream_Release( stm );
373 static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
374 LPVOID data, UINT sz )
377 UINT ret = ERROR_FUNCTION_FAILED;
384 encname = encode_streamname(TRUE, stname );
385 r = IStorage_OpenStream( stg, encname, NULL,
386 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
389 r = IStorage_CreateStream( stg, encname,
390 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
395 WARN("open stream failed r = %08x\n", r);
400 r = IStream_SetSize( stm, size );
403 WARN("Failed to SetSize\n");
408 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
411 WARN("Failed to Seek\n");
415 r = IStream_Write(stm, data, sz, &count );
416 if( FAILED( r ) || ( count != sz ) )
418 WARN("Failed to Write\n");
425 IStream_Release( stm );
430 static void free_table( MSITABLE *table )
433 for( i=0; i<table->row_count; i++ )
434 msi_free( table->data[i] );
435 msi_free( table->data );
439 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
441 const MSICOLUMNINFO *last_col = &cols[count-1];
444 return last_col->offset + bytes_per_column( last_col );
447 /* add this table to the list of cached tables in the database */
448 static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
449 const MSICOLUMNINFO *cols, UINT num_cols )
452 USHORT *rawdata = NULL;
453 UINT rawsize = 0, i, j, row_size = 0;
455 TRACE("%s\n",debugstr_w(name));
457 /* nonexistent tables should be interpreted as empty tables */
458 t = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
462 row_size = msi_table_get_row_size( cols, num_cols );
466 lstrcpyW( t->name, name );
468 /* if we can't read the table, just assume that it's empty */
469 read_stream_data( stg, name, &rawdata, &rawsize );
473 TRACE("Read %d bytes\n", rawsize );
475 if( rawsize % row_size )
477 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
481 t->row_count = rawsize / row_size;
482 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
486 /* transpose all the data */
487 TRACE("Transposing data from %d rows\n", t->row_count );
488 for( i=0; i<t->row_count; i++ )
490 t->data[i] = msi_alloc( row_size );
494 for( j=0; j<num_cols; j++ )
496 UINT ofs = cols[j].offset/2;
497 UINT n = bytes_per_column( &cols[j] );
502 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
505 t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
506 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
509 ERR("oops - unknown column width %d\n", n);
523 void free_cached_tables( MSIDATABASE *db )
525 while( !list_empty( &db->tables ) )
527 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
529 list_remove( &t->entry );
534 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
538 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
539 if( !lstrcmpW( name, t->name ) )
545 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
547 UINT r, column_count = 0;
548 MSICOLUMNINFO *columns;
550 /* get the number of columns in this table */
552 r = get_tablecolumns( db, name, NULL, &column_count );
553 if( r != ERROR_SUCCESS )
556 /* if there's no columns, there's no table */
557 if( column_count == 0 )
558 return ERROR_INVALID_PARAMETER;
560 TRACE("Table %s found\n", debugstr_w(name) );
562 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
564 return ERROR_FUNCTION_FAILED;
566 r = get_tablecolumns( db, name, columns, &column_count );
567 if( r != ERROR_SUCCESS )
570 return ERROR_FUNCTION_FAILED;
574 *pcount = column_count;
579 static MSITABLE *get_table( MSIDATABASE *db, LPCWSTR name,
580 const MSICOLUMNINFO *cols, UINT num_cols )
584 /* first, see if the table is cached */
585 table = find_cached_table( db, name );
589 table = read_table_from_storage( db->storage, name, cols, num_cols );
591 list_add_head( &db->tables, &table->entry );
596 static UINT save_table( MSIDATABASE *db, MSITABLE *t )
598 USHORT *rawdata = NULL, *p;
599 UINT rawsize, r, i, j, row_size, num_cols = 0;
600 MSICOLUMNINFO *cols = NULL;
602 TRACE("Saving %s\n", debugstr_w( t->name ) );
604 r = table_get_column_info( db, t->name, &cols, &num_cols );
605 if( r != ERROR_SUCCESS )
608 row_size = msi_table_get_row_size( cols, num_cols );
610 rawsize = t->row_count * row_size;
611 rawdata = msi_alloc_zero( rawsize );
614 r = ERROR_NOT_ENOUGH_MEMORY;
619 for( i=0; i<num_cols; i++ )
621 for( j=0; j<t->row_count; j++ )
623 UINT offset = cols[i].offset;
625 *p++ = t->data[j][offset/2];
626 if( 4 == bytes_per_column( &cols[i] ) )
627 *p++ = t->data[j][offset/2+1];
631 TRACE("writing %d bytes\n", rawsize);
632 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
635 msi_free_colinfo( cols, num_cols );
642 HRESULT init_string_table( IStorage *stg )
645 USHORT zero[2] = { 0, 0 };
650 encname = encode_streamname(TRUE, szStringPool );
652 /* create the StringPool stream... add the zero string to it*/
653 r = IStorage_CreateStream( stg, encname,
654 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
662 r = IStream_Write(stm, zero, sizeof zero, &count );
663 IStream_Release( stm );
665 if( FAILED( r ) || ( count != sizeof zero ) )
671 /* create the StringData stream... make it zero length */
672 encname = encode_streamname(TRUE, szStringData );
673 r = IStorage_CreateStream( stg, encname,
674 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
681 IStream_Release( stm );
686 string_table *load_string_table( IStorage *stg )
688 string_table *st = NULL;
691 UINT r, datasize = 0, poolsize = 0, codepage;
692 DWORD i, count, offset, len, n, refs;
694 r = read_stream_data( stg, szStringPool, &pool, &poolsize );
695 if( r != ERROR_SUCCESS)
697 r = read_stream_data( stg, szStringData, (USHORT**)&data, &datasize );
698 if( r != ERROR_SUCCESS)
703 codepage = pool[0] | ( pool[1] << 16 );
706 st = msi_init_stringtable( count, codepage );
713 /* the string reference count is always the second word */
716 /* empty entries have two zeros, still have a string id */
717 if (pool[i*2] == 0 && refs == 0)
725 * If a string is over 64k, the previous string entry is made null
726 * and its the high word of the length is inserted in the null string's
727 * reference count field.
731 len = (pool[i*2+3] << 16) + pool[i*2+2];
740 if ( (offset + len) > datasize )
742 ERR("string table corrupt?\n");
746 r = msi_addstring( st, n, data+offset, len, refs );
748 ERR("Failed to add string %d\n", n );
753 if ( datasize != offset )
754 ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
756 TRACE("Loaded %d strings\n", count);
765 static UINT save_string_table( MSIDATABASE *db )
767 UINT i, count, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
768 UINT ret = ERROR_FUNCTION_FAILED;
774 /* construct the new table in memory first */
775 count = msi_string_totalsize( db->strings, &datasize, &poolsize );
777 TRACE("%u %u %u\n", count, datasize, poolsize );
779 pool = msi_alloc( poolsize );
782 WARN("Failed to alloc pool %d bytes\n", poolsize );
785 data = msi_alloc( datasize );
788 WARN("Failed to alloc data %d bytes\n", poolsize );
793 codepage = msi_string_get_codepage( db->strings );
794 pool[0]=codepage&0xffff;
795 pool[1]=(codepage>>16);
797 for( i=1; i<count; i++ )
799 sz = datasize - used;
800 r = msi_id2stringA( db->strings, i, data+used, &sz );
801 if( r != ERROR_SUCCESS )
803 ERR("failed to fetch string\n");
806 if( sz && (sz < (datasize - used ) ) )
810 pool[ n*2 + 1 ] = msi_id_refcount( db->strings, i );
821 pool[ n*2 + 2 ] = sz&0xffff;
822 pool[ n*2 + 3 ] = (sz>>16);
826 if( used > datasize )
828 ERR("oops overran %d >= %d\n", used, datasize);
833 if( used != datasize )
835 ERR("oops used %d != datasize %d\n", used, datasize);
839 /* write the streams */
840 r = write_stream_data( db->storage, szStringData, data, datasize );
841 TRACE("Wrote StringData r=%08x\n", r);
844 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
845 TRACE("Wrote StringPool r=%08x\n", r);
858 /* information for default tables */
859 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
860 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
861 static const WCHAR szName[] = { 'N','a','m','e',0 };
862 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
863 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
864 static const WCHAR szType[] = { 'T','y','p','e',0 };
866 static const MSICOLUMNINFO _Columns_cols[4] = {
867 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
868 { szColumns, 2, szNumber, MSITYPE_VALID | 2, 2 },
869 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
870 { szColumns, 4, szType, MSITYPE_VALID | 2, 6 },
872 static const MSICOLUMNINFO _Tables_cols[1] = {
873 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
876 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
878 const MSICOLUMNINFO *p;
881 TRACE("%s\n", debugstr_w(name));
883 if (!lstrcmpW( name, szTables ))
888 else if (!lstrcmpW( name, szColumns ))
894 return ERROR_FUNCTION_FAILED;
896 /* duplicate the string data so we can free it in msi_free_colinfo */
899 if (colinfo && (i < *sz) )
901 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
902 colinfo[i].tablename = strdupW( p[i].tablename );
903 colinfo[i].colname = strdupW( p[i].colname );
905 if( colinfo && (i >= *sz) )
909 return ERROR_SUCCESS;
912 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
916 for( i=0; i<count; i++ )
918 msi_free( (LPWSTR) colinfo[i].tablename );
919 msi_free( (LPWSTR) colinfo[i].colname );
920 msi_free( colinfo[i].hash_table );
924 static LPWSTR msi_makestring( MSIDATABASE *db, UINT stringid)
926 return strdupW(msi_string_lookup_id( db->strings, stringid ));
929 static UINT get_tablecolumns( MSIDATABASE *db,
930 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
932 UINT r, i, n=0, table_id, count, maxcount = *sz;
933 MSITABLE *table = NULL;
935 TRACE("%s\n", debugstr_w(szTableName));
937 /* first check if there is a default table with that name */
938 r = get_defaulttablecolumns( szTableName, colinfo, sz );
939 if( ( r == ERROR_SUCCESS ) && *sz )
942 table = get_table( db, szColumns, _Columns_cols, 4 );
945 ERR("couldn't load _Columns table\n");
946 return ERROR_FUNCTION_FAILED;
949 /* convert table and column names to IDs from the string table */
950 r = msi_string2idW( db->strings, szTableName, &table_id );
951 if( r != ERROR_SUCCESS )
953 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
957 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
959 /* if maxcount is non-zero, assume it's exactly right for this table */
960 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
961 count = table->row_count;
962 for( i=0; i<count; i++ )
964 if( table->data[ i ][ 0 ] != table_id )
968 UINT id = table->data[ i ] [ 2 ];
969 UINT col = table->data[ i ][ 1 ] - (1<<15);
971 /* check the column number is in range */
972 if (col<1 || col>maxcount)
974 ERR("column %d out of range\n", col);
978 /* check if this column was already set */
979 if (colinfo[ col - 1 ].number)
981 ERR("duplicate column %d\n", col);
985 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
986 colinfo[ col - 1 ].number = col;
987 colinfo[ col - 1 ].colname = msi_makestring( db, id );
988 colinfo[ col - 1 ].type = table->data[ i ] [ 3 ] - (1<<15);
989 colinfo[ col - 1 ].offset = 0;
990 colinfo[ col - 1 ].hash_table = NULL;
995 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
997 if (maxcount && n != maxcount)
999 ERR("missing column in table %s\n", debugstr_w(szTableName));
1000 msi_free_colinfo(colinfo, maxcount );
1001 return ERROR_FUNCTION_FAILED;
1004 /* calculate the offsets */
1005 for( i=0; maxcount && (i<maxcount); i++ )
1007 assert( (i+1) == colinfo[ i ].number );
1009 colinfo[i].offset = colinfo[ i - 1 ].offset
1010 + bytes_per_column( &colinfo[ i - 1 ] );
1012 colinfo[i].offset = 0;
1013 TRACE("column %d is [%s] with type %08x ofs %d\n",
1014 colinfo[i].number, debugstr_w(colinfo[i].colname),
1015 colinfo[i].type, colinfo[i].offset);
1019 return ERROR_SUCCESS;
1022 /* try to find the table name in the _Tables table */
1023 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
1025 UINT r, table_id = 0, i, count;
1026 MSITABLE *table = NULL;
1028 if( !lstrcmpW( name, szTables ) )
1030 if( !lstrcmpW( name, szColumns ) )
1033 r = msi_string2idW( db->strings, name, &table_id );
1034 if( r != ERROR_SUCCESS )
1036 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1040 table = get_table( db, szTables, _Tables_cols, 1 );
1043 TRACE("table %s not available\n", debugstr_w(szTables));
1047 /* count = table->size/2; */
1048 count = table->row_count;
1049 for( i=0; i<count; i++ )
1050 if( table->data[ i ][ 0 ] == table_id )
1056 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1061 /* below is the query interface to a table */
1063 typedef struct tagMSITABLEVIEW
1068 MSICOLUMNINFO *columns;
1074 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1076 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1077 UINT offset, num_rows, n;
1080 return ERROR_INVALID_PARAMETER;
1082 if( (col==0) || (col>tv->num_cols) )
1083 return ERROR_INVALID_PARAMETER;
1085 /* how many rows are there ? */
1086 num_rows = tv->table->row_count;
1087 if( row >= num_rows )
1088 return ERROR_NO_MORE_ITEMS;
1090 if( tv->columns[col-1].offset >= tv->row_size )
1092 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1093 ERR("%p %p\n", tv, tv->columns );
1094 return ERROR_FUNCTION_FAILED;
1097 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1098 n = bytes_per_column( &tv->columns[col-1] );
1102 offset = tv->columns[col-1].offset/2;
1103 *val = tv->table->data[row][offset] +
1104 (tv->table->data[row][offset + 1] << 16);
1107 offset = tv->columns[col-1].offset/2;
1108 *val = tv->table->data[row][offset];
1111 ERR("oops! what is %d bytes per column?\n", n );
1112 return ERROR_FUNCTION_FAILED;
1115 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1117 return ERROR_SUCCESS;
1121 * We need a special case for streams, as we need to reference column with
1122 * the name of the stream in the same table, and the table name
1123 * which may not be available at higher levels of the query
1125 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1127 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1128 UINT ival = 0, refcol = 0, r;
1132 static const WCHAR szDot[] = { '.', 0 };
1135 if( !view->ops->fetch_int )
1136 return ERROR_INVALID_PARAMETER;
1139 * The column marked with the type stream data seems to have a single number
1140 * which references the column containing the name of the stream data
1142 * Fetch the column to reference first.
1144 r = view->ops->fetch_int( view, row, col, &ival );
1145 if( r != ERROR_SUCCESS )
1148 /* check the column value is in range */
1149 if (ival < 0 || ival > tv->num_cols || ival == col)
1151 ERR("bad column ref (%u) for stream\n", ival);
1152 return ERROR_FUNCTION_FAILED;
1155 if ( tv->columns[ival - 1].type & MSITYPE_STRING )
1157 /* now get the column with the name of the stream */
1158 r = view->ops->fetch_int( view, row, ival, &refcol );
1159 if ( r != ERROR_SUCCESS )
1162 /* lookup the string value from the string table */
1163 sval = msi_string_lookup_id( tv->db->strings, refcol );
1165 return ERROR_INVALID_PARAMETER;
1169 static const WCHAR fmt[] = { '%','d',0 };
1170 sprintfW( number, fmt, ival );
1174 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1175 full_name = msi_alloc( len*sizeof(WCHAR) );
1176 lstrcpyW( full_name, tv->name );
1177 lstrcatW( full_name, szDot );
1178 lstrcatW( full_name, sval );
1180 r = db_get_raw_stream( tv->db, full_name, stm );
1182 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1183 msi_free( full_name );
1188 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1193 return ERROR_INVALID_PARAMETER;
1195 if( (col==0) || (col>tv->num_cols) )
1196 return ERROR_INVALID_PARAMETER;
1198 if( tv->columns[col-1].offset >= tv->row_size )
1200 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1201 ERR("%p %p\n", tv, tv->columns );
1202 return ERROR_FUNCTION_FAILED;
1205 n = bytes_per_column( &tv->columns[col-1] );
1209 offset = tv->columns[col-1].offset/2;
1210 tv->table->data[row][offset] = val & 0xffff;
1211 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1214 offset = tv->columns[col-1].offset/2;
1215 tv->table->data[row][offset] = val;
1218 ERR("oops! what is %d bytes per column?\n", n );
1219 return ERROR_FUNCTION_FAILED;
1221 return ERROR_SUCCESS;
1224 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1226 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1227 UINT i, val, r = ERROR_SUCCESS;
1230 return ERROR_INVALID_PARAMETER;
1232 /* test if any of the mask bits are invalid */
1233 if ( mask >= (1<<tv->num_cols) )
1234 return ERROR_INVALID_PARAMETER;
1236 for ( i = 0; i < tv->num_cols; i++ )
1238 /* only update the fields specified in the mask */
1239 if ( !(mask&(1<<i)) )
1242 /* FIXME: should we allow updating keys? */
1245 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1247 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1249 val = 1; /* refers to the first key column */
1251 else if ( tv->columns[i].type & MSITYPE_STRING )
1253 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1254 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1 );
1256 else if ( 2 == bytes_per_column( &tv->columns[ i ] ) )
1258 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1259 if ( val & 0xffff0000 )
1261 ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1262 return ERROR_FUNCTION_FAILED;
1267 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1268 val = ival ^ 0x80000000;
1272 r = TABLE_set_int( tv, row, i+1, val );
1273 if ( r != ERROR_SUCCESS )
1279 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
1281 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1285 TRACE("%p\n", view);
1288 return ERROR_INVALID_PARAMETER;
1290 row = msi_alloc_zero( tv->row_size );
1292 return ERROR_NOT_ENOUGH_MEMORY;
1294 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1295 if( tv->table->data )
1296 p = msi_realloc( tv->table->data, sz );
1298 p = msi_alloc( sz );
1302 return ERROR_NOT_ENOUGH_MEMORY;
1305 tv->table->data = p;
1306 tv->table->data[tv->table->row_count] = row;
1307 *num = tv->table->row_count;
1308 tv->table->row_count++;
1310 return ERROR_SUCCESS;
1313 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1315 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1317 TRACE("%p %p\n", tv, record);
1319 TRACE("There are %d columns\n", tv->num_cols );
1320 tv->table = get_table( tv->db, tv->name, tv->columns, tv->num_cols );
1322 return ERROR_FUNCTION_FAILED;
1324 return ERROR_SUCCESS;
1327 static UINT TABLE_close( struct tagMSIVIEW *view )
1329 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1331 TRACE("%p\n", view );
1334 return ERROR_FUNCTION_FAILED;
1338 return ERROR_SUCCESS;
1341 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1343 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1345 TRACE("%p %p %p\n", view, rows, cols );
1348 *cols = tv->num_cols;
1352 return ERROR_INVALID_PARAMETER;
1353 *rows = tv->table->row_count;
1356 return ERROR_SUCCESS;
1359 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1360 UINT n, LPWSTR *name, UINT *type )
1362 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1364 TRACE("%p %d %p %p\n", tv, n, name, type );
1366 if( ( n == 0 ) || ( n > tv->num_cols ) )
1367 return ERROR_INVALID_PARAMETER;
1371 *name = strdupW( tv->columns[n-1].colname );
1373 return ERROR_FUNCTION_FAILED;
1376 *type = tv->columns[n-1].type;
1378 return ERROR_SUCCESS;
1381 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1383 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1387 /* check there's no null values where they're not allowed */
1388 for( i = 0; i < tv->num_cols; i++ )
1390 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1393 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1394 TRACE("skipping binary column\n");
1395 else if ( tv->columns[i].type & MSITYPE_STRING )
1399 str = MSI_RecordGetString( rec, i+1 );
1400 if (str == NULL || str[0] == 0)
1401 return ERROR_INVALID_DATA;
1407 n = MSI_RecordGetInteger( rec, i+1 );
1408 if (n == MSI_NULL_INTEGER)
1409 return ERROR_INVALID_DATA;
1413 /* check there's no duplicate keys */
1414 r = msi_table_find_row( tv, rec, &row );
1415 if (r == ERROR_SUCCESS)
1416 return ERROR_INVALID_DATA;
1418 return ERROR_SUCCESS;
1421 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1423 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1426 TRACE("%p %p\n", tv, rec );
1428 /* check that the key is unique - can we find a matching row? */
1429 r = table_validate_new( tv, rec );
1430 if( r != ERROR_SUCCESS )
1431 return ERROR_FUNCTION_FAILED;
1433 r = table_create_new_row( view, &row );
1434 TRACE("insert_row returned %08x\n", r);
1435 if( r != ERROR_SUCCESS )
1438 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1441 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1444 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1447 TRACE("%p %d %p\n", view, eModifyMode, rec );
1451 r = TABLE_execute( view, NULL );
1456 switch (eModifyMode)
1458 case MSIMODIFY_VALIDATE_NEW:
1459 r = table_validate_new( tv, rec );
1462 case MSIMODIFY_INSERT_TEMPORARY:
1463 r = table_validate_new( tv, rec );
1464 if (r != ERROR_SUCCESS)
1466 r = TABLE_insert_row( view, rec );
1469 case MSIMODIFY_REFRESH:
1470 case MSIMODIFY_INSERT:
1471 case MSIMODIFY_UPDATE:
1472 case MSIMODIFY_ASSIGN:
1473 case MSIMODIFY_REPLACE:
1474 case MSIMODIFY_MERGE:
1475 case MSIMODIFY_DELETE:
1476 case MSIMODIFY_VALIDATE:
1477 case MSIMODIFY_VALIDATE_FIELD:
1478 case MSIMODIFY_VALIDATE_DELETE:
1479 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1480 r = ERROR_CALL_NOT_IMPLEMENTED;
1484 r = ERROR_INVALID_DATA;
1490 static UINT TABLE_delete( struct tagMSIVIEW *view )
1492 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1494 TRACE("%p\n", view );
1500 msi_free_colinfo( tv->columns, tv->num_cols );
1501 msi_free( tv->columns );
1507 return ERROR_SUCCESS;
1510 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1511 UINT val, UINT *row, MSIITERHANDLE *handle )
1513 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1514 const MSICOLUMNHASHENTRY *entry;
1516 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1519 return ERROR_INVALID_PARAMETER;
1521 if( (col==0) || (col > tv->num_cols) )
1522 return ERROR_INVALID_PARAMETER;
1524 if( !tv->columns[col-1].hash_table )
1527 UINT num_rows = tv->table->row_count;
1528 MSICOLUMNHASHENTRY **hash_table;
1529 MSICOLUMNHASHENTRY *new_entry;
1531 if( tv->columns[col-1].offset >= tv->row_size )
1533 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1534 ERR("%p %p\n", tv, tv->columns );
1535 return ERROR_FUNCTION_FAILED;
1538 /* allocate contiguous memory for the table and its entries so we
1539 * don't have to do an expensive cleanup */
1540 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1541 num_rows * sizeof(MSICOLUMNHASHENTRY));
1543 return ERROR_OUTOFMEMORY;
1545 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1546 tv->columns[col-1].hash_table = hash_table;
1548 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1550 for (i = 0; i < num_rows; i++, new_entry++)
1553 UINT offset = i + (tv->columns[col-1].offset/2) * num_rows;
1554 n = bytes_per_column( &tv->columns[col-1] );
1558 offset = tv->columns[col-1].offset/2;
1559 row_value = tv->table->data[i][offset] +
1560 (tv->table->data[i][offset + 1] << 16);
1563 offset = tv->columns[col-1].offset/2;
1564 row_value = tv->table->data[i][offset];
1567 ERR("oops! what is %d bytes per column?\n", n );
1571 new_entry->next = NULL;
1572 new_entry->value = row_value;
1574 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1576 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1577 while (prev_entry->next)
1578 prev_entry = prev_entry->next;
1579 prev_entry->next = new_entry;
1582 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1587 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1589 entry = (*handle)->next;
1591 while (entry && entry->value != val)
1592 entry = entry->next;
1596 return ERROR_NO_MORE_ITEMS;
1599 return ERROR_SUCCESS;
1603 static const MSIVIEWOPS table_ops =
1611 TABLE_get_dimensions,
1612 TABLE_get_column_info,
1615 TABLE_find_matching_rows
1618 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1621 UINT r, sz, column_count;
1622 MSICOLUMNINFO *columns;
1624 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1626 /* get the number of columns in this table */
1628 r = get_tablecolumns( db, name, NULL, &column_count );
1629 if( r != ERROR_SUCCESS )
1632 /* if there's no columns, there's no table */
1633 if( column_count == 0 )
1634 return ERROR_INVALID_PARAMETER;
1636 TRACE("Table found\n");
1638 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1639 tv = msi_alloc_zero( sz );
1641 return ERROR_FUNCTION_FAILED;
1643 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO));
1647 return ERROR_FUNCTION_FAILED;
1650 r = get_tablecolumns( db, name, columns, &column_count );
1651 if( r != ERROR_SUCCESS )
1653 msi_free( columns );
1655 return ERROR_FUNCTION_FAILED;
1658 TRACE("Table has %d columns\n", column_count);
1660 /* fill the structure */
1661 tv->view.ops = &table_ops;
1663 tv->columns = columns;
1664 tv->num_cols = column_count;
1666 tv->row_size = msi_table_get_row_size( columns, column_count );
1668 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
1670 *view = (MSIVIEW*) tv;
1671 lstrcpyW( tv->name, name );
1673 return ERROR_SUCCESS;
1676 UINT MSI_CommitTables( MSIDATABASE *db )
1679 MSITABLE *table = NULL;
1683 r = save_string_table( db );
1684 if( r != ERROR_SUCCESS )
1686 WARN("failed to save string table r=%08x\n",r);
1690 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1692 r = save_table( db, table );
1693 if( r != ERROR_SUCCESS )
1695 WARN("failed to save table %s (r=%08x)\n",
1696 debugstr_w(table->name), r);
1701 /* force everything to reload next time */
1702 free_cached_tables( db );
1704 return ERROR_SUCCESS;
1707 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
1710 return MSICONDITION_ERROR;
1712 return MSICONDITION_FALSE;
1715 static MSIRECORD *msi_get_transform_record( MSITABLEVIEW *tv, string_table *st, USHORT *rawdata )
1717 UINT i, val, ofs = 0;
1718 USHORT mask = *rawdata++;
1719 MSICOLUMNINFO *columns = tv->columns;
1722 rec = MSI_CreateRecord( tv->num_cols );
1727 for( i=0; i<tv->num_cols; i++ )
1729 UINT n = bytes_per_column( &columns[i] );
1731 if ( (mask&1) && (i>=(mask>>8)) )
1733 /* all keys must be present */
1734 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
1741 if( (columns[i].type & MSITYPE_STRING) &&
1742 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1744 LPCWSTR sval = msi_string_lookup_id( st, val );
1745 MSI_RecordSetStringW( rec, i+1, sval );
1746 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
1751 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
1752 TRACE(" field %d [0x%04x]\n", i+1, val );
1756 val = (rawdata[ofs] + (rawdata[ofs + 1]<<16));
1758 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
1759 TRACE(" field %d [0x%08x]\n", i+1, val );
1762 ERR("oops - unknown column width %d\n", n);
1770 static void dump_record( MSIRECORD *rec )
1774 n = MSI_RecordGetFieldCount( rec );
1775 for( i=1; i<=n; i++ )
1777 LPCWSTR sval = MSI_RecordGetString( rec, i );
1779 if( MSI_RecordIsNull( rec, i ) )
1780 TRACE("row -> []\n");
1781 else if( (sval = MSI_RecordGetString( rec, i )) )
1782 TRACE("row -> [%s]\n", debugstr_w(sval));
1784 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
1788 static void dump_table( string_table *st, USHORT *rawdata, UINT rawsize )
1793 for( i=0; i<(rawsize/2); i++ )
1795 sval = msi_string_lookup_id( st, rawdata[i] );
1796 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
1800 static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
1805 data = msi_alloc( tv->num_cols *sizeof (UINT) );
1806 for( i=0; i<tv->num_cols; i++ )
1810 if ( ~tv->columns[i].type & MSITYPE_KEY )
1813 /* turn the transform column value into a row value */
1814 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
1815 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1817 str = MSI_RecordGetString( rec, i+1 );
1818 r = msi_string2idW( tv->db->strings, str, &data[i] );
1820 /* if there's no matching string in the string table,
1821 these keys can't match any record, so fail now. */
1822 if( ERROR_SUCCESS != r )
1830 data[i] = MSI_RecordGetInteger( rec, i+1 );
1831 if ((tv->columns[i].type&0xff) == 2)
1834 data[i] += 0x80000000;
1840 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, UINT *data )
1842 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
1844 for( i=0; i<tv->num_cols; i++ )
1846 if ( ~tv->columns[i].type & MSITYPE_KEY )
1849 /* turn the transform column value into a row value */
1850 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
1851 if ( r != ERROR_SUCCESS )
1853 ERR("TABLE_fetch_int shouldn't fail here\n");
1857 /* if this key matches, move to the next column */
1860 ret = ERROR_FUNCTION_FAILED;
1864 ret = ERROR_SUCCESS;
1870 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
1872 UINT i, r = ERROR_FUNCTION_FAILED, *data;
1874 data = msi_record_to_row( tv, rec );
1877 for( i=0; i<tv->table->row_count; i++ )
1879 r = msi_row_matches( tv, i, data );
1880 if( r == ERROR_SUCCESS )
1890 static UINT msi_delete_row( MSITABLEVIEW *tv, UINT row )
1894 for( i=1; i<=tv->num_cols; i++ )
1895 TABLE_set_int( tv, row, i, 0 );
1896 return ERROR_SUCCESS;
1899 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
1900 string_table *st, LPCWSTR name )
1903 USHORT *rawdata = NULL;
1904 MSITABLEVIEW *tv = NULL;
1905 UINT r, n, sz, i, mask;
1906 MSIRECORD *rec = NULL;
1911 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
1913 /* read the transform data */
1914 read_stream_data( stg, name, &rawdata, &rawsize );
1917 TRACE("table %s empty\n", debugstr_w(name) );
1918 return ERROR_INVALID_TABLE;
1921 /* create a table view */
1922 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
1923 if( r != ERROR_SUCCESS )
1926 r = tv->view.ops->execute( &tv->view, NULL );
1927 if( r != ERROR_SUCCESS )
1930 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1931 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
1933 /* interpret the data */
1935 for( n=0; n < (rawsize/2); )
1942 * if the low bit is set, columns are continuous and
1943 * the number of columns is specified in the high byte
1945 sz = 2 + tv->row_size;
1950 * If the low bit is not set, rowdata[n] is a bitmask.
1951 * Excepting for key fields, which are always present,
1952 * each bit indicates that a field is present in the transform record.
1954 * rawdata[n] == 0 is a special case ... only the keys will be present
1955 * and it means that this row should be deleted.
1958 for( i=0; i<tv->num_cols; i++ )
1960 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
1961 sz += bytes_per_column( &tv->columns[i] );
1965 /* check we didn't run of the end of the table */
1966 if ( (n+sz) > rawsize )
1969 dump_table( st, rawdata, rawsize );
1973 rec = msi_get_transform_record( tv, st, &rawdata[n] );
1978 TRACE("inserting record\n");
1981 * Native msi seems writes nul into the
1982 * Number (2nd) column of the _Columns table.
1983 * Not sure that it's deliberate...
1985 if (!lstrcmpW(name, szColumns))
1990 MSI_RecordGetStringW( rec, 1, table, &sz );
1992 /* reset the column number on a new table */
1993 if ( lstrcmpW(coltable, table) )
1996 lstrcpyW( coltable, table );
1999 /* fix nul column numbers */
2000 MSI_RecordSetInteger( rec, 2, ++colcol );
2003 r = TABLE_insert_row( &tv->view, rec );
2004 if (r != ERROR_SUCCESS)
2005 ERR("insert row failed\n");
2011 r = msi_table_find_row( tv, rec, &row );
2012 if (r != ERROR_SUCCESS)
2013 ERR("no matching row to transform\n");
2016 TRACE("modifying row [%d]:\n", row);
2017 TABLE_set_row( &tv->view, row, rec, mask );
2021 TRACE("deleting row [%d]:\n", row);
2022 msi_delete_row( tv, row );
2025 if( TRACE_ON(msidb) ) dump_record( rec );
2026 msiobj_release( &rec->hdr );
2033 /* no need to free the table, it's associated with the database */
2034 msi_free( rawdata );
2036 tv->view.ops->delete( &tv->view );
2038 return ERROR_SUCCESS;
2042 * msi_table_apply_transform
2044 * Enumerate the table transforms in a transform storage and apply each one.
2046 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2048 IEnumSTATSTG *stgenum = NULL;
2053 string_table *strings;
2054 UINT ret = ERROR_FUNCTION_FAILED;
2056 TRACE("%p %p\n", db, stg );
2058 strings = load_string_table( stg );
2062 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2067 * Apply _Tables and _Columns transforms first so that
2068 * the table metadata is correct, and empty tables exist.
2070 ret = msi_table_load_transform( db, stg, strings, szTables );
2071 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2074 ret = msi_table_load_transform( db, stg, strings, szColumns );
2075 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2078 ret = ERROR_SUCCESS;
2080 while( r == ERROR_SUCCESS )
2083 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2084 if( FAILED( r ) || !count )
2087 decode_streamname( stat.pwcsName, name );
2088 if ( name[0] != 0x4840 )
2091 TRACE("transform contains stream %s\n", debugstr_w(name));
2093 if ( !lstrcmpW( name+1, szStringPool ) ||
2094 !lstrcmpW( name+1, szStringData ) ||
2095 !lstrcmpW( name+1, szColumns ) ||
2096 !lstrcmpW( name+1, szTables ) )
2099 ret = msi_table_load_transform( db, stg, strings, name+1 );
2102 if ( ret == ERROR_SUCCESS )
2103 append_storage_to_db( db, stg );
2107 IEnumSTATSTG_Release( stgenum );
2109 msi_destroy_stringtable( strings );
2114 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
2118 t = msi_alloc( sizeof *t );
2120 IStorage_AddRef( stg );
2121 list_add_tail( &db->transforms, &t->entry );
2124 void msi_free_transforms( MSIDATABASE *db )
2126 while( !list_empty( &db->transforms ) )
2128 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2129 MSITRANSFORM, entry );
2130 list_remove( &t->entry );
2131 IStorage_Release( t->stg );