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
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
30 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
42 #define MSITABLE_HASH_TABLE_SIZE 37
44 typedef struct tagMSICOLUMNHASHENTRY
46 struct tagMSICOLUMNHASHENTRY *next;
51 typedef struct tagMSICOLUMNINFO
58 MSICOLUMNHASHENTRY **hash_table;
69 typedef struct tagMSITRANSFORM {
74 #define MAX_STREAM_NAME 0x1f
76 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
77 MSICOLUMNINFO **pcols, UINT *pcount );
78 static UINT get_tablecolumns( MSIDATABASE *db,
79 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
80 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
82 static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
84 if( col->type & MSITYPE_STRING )
86 if( (col->type & 0xff) > 4 )
87 ERR("Invalid column size!\n");
88 return col->type & 0xff;
91 static int utf2mime(int x)
93 if( (x>='0') && (x<='9') )
95 if( (x>='A') && (x<='Z') )
97 if( (x>='a') && (x<='z') )
106 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
108 DWORD count = MAX_STREAM_NAME;
113 count = lstrlenW( in )+2;
114 out = msi_alloc( count*sizeof(WCHAR) );
130 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
132 ch = utf2mime(ch) + 0x4800;
134 if( next && (next<0x80) )
136 next = utf2mime(next);
147 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
152 static int mime2utf(int x)
159 return x - 10 - 26 + 'a';
160 if( x == (10+26+26) )
165 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
170 while ( (ch = *in++) )
172 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
175 ch = mime2utf(ch-0x4800);
179 *out++ = mime2utf(ch&0x3f);
181 ch = mime2utf((ch>>6)&0x3f);
191 void enum_stream_names( IStorage *stg )
193 IEnumSTATSTG *stgenum = NULL;
199 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
207 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
208 if( FAILED( r ) || !count )
210 decode_streamname( stat.pwcsName, name );
211 TRACE("stream %2ld -> %s %s\n", n,
212 debugstr_w(stat.pwcsName), debugstr_w(name) );
216 IEnumSTATSTG_Release( stgenum );
219 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
220 USHORT **pdata, UINT *psz )
223 UINT ret = ERROR_FUNCTION_FAILED;
230 encname = encode_streamname(TRUE, stname);
232 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
234 r = IStorage_OpenStream(stg, encname, NULL,
235 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
239 WARN("open stream failed r = %08lx - empty table?\n",r);
243 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
246 WARN("open stream failed r = %08lx!\n",r);
250 if( stat.cbSize.QuadPart >> 32 )
256 sz = stat.cbSize.QuadPart;
257 data = msi_alloc( sz );
260 WARN("couldn't allocate memory r=%08lx!\n",r);
261 ret = ERROR_NOT_ENOUGH_MEMORY;
265 r = IStream_Read(stm, data, sz, &count );
266 if( FAILED( r ) || ( count != sz ) )
269 WARN("read stream failed r = %08lx!\n",r);
278 IStream_Release( stm );
283 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
288 encname = encode_streamname(FALSE, stname);
290 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
292 r = IStorage_OpenStream(db->storage, encname, NULL,
293 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
296 MSITRANSFORM *transform;
298 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
300 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
301 r = IStorage_OpenStream( transform->stg, encname, NULL,
302 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
310 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
313 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
314 USHORT **pdata, UINT *psz )
317 UINT ret = ERROR_FUNCTION_FAILED;
323 r = db_get_raw_stream( db, stname, &stm );
324 if( r != ERROR_SUCCESS)
326 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
329 WARN("open stream failed r = %08lx!\n",r);
333 if( stat.cbSize.QuadPart >> 32 )
339 sz = stat.cbSize.QuadPart;
340 data = msi_alloc( sz );
343 WARN("couldn't allocate memory r=%08lx!\n",r);
344 ret = ERROR_NOT_ENOUGH_MEMORY;
348 r = IStream_Read(stm, data, sz, &count );
349 if( FAILED( r ) || ( count != sz ) )
352 WARN("read stream failed r = %08lx!\n",r);
361 IStream_Release( stm );
366 static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
367 LPVOID data, UINT sz )
370 UINT ret = ERROR_FUNCTION_FAILED;
377 encname = encode_streamname(TRUE, stname );
378 r = IStorage_OpenStream( stg, encname, NULL,
379 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
382 r = IStorage_CreateStream( stg, encname,
383 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
388 WARN("open stream failed r = %08lx\n",r);
393 r = IStream_SetSize( stm, size );
396 WARN("Failed to SetSize\n");
401 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
404 WARN("Failed to Seek\n");
408 r = IStream_Write(stm, data, sz, &count );
409 if( FAILED( r ) || ( count != sz ) )
411 WARN("Failed to Write\n");
418 IStream_Release( stm );
423 static void free_table( MSITABLE *table )
426 for( i=0; i<table->row_count; i++ )
427 msi_free( table->data[i] );
428 msi_free( table->data );
432 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
434 const MSICOLUMNINFO *last_col = &cols[count-1];
437 return last_col->offset + bytes_per_column( last_col );
440 /* add this table to the list of cached tables in the database */
441 static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
442 const MSICOLUMNINFO *cols, UINT num_cols )
445 USHORT *rawdata = NULL;
446 UINT rawsize = 0, i, j, row_size = 0;
448 TRACE("%s\n",debugstr_w(name));
450 /* nonexistent tables should be interpreted as empty tables */
451 t = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
455 row_size = msi_table_get_row_size( cols, num_cols );
459 lstrcpyW( t->name, name );
461 /* if we can't read the table, just assume that it's empty */
462 read_stream_data( stg, name, &rawdata, &rawsize );
466 TRACE("Read %d bytes\n", rawsize );
468 if( rawsize % row_size )
470 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
474 t->row_count = rawsize / row_size;
475 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
479 /* transpose all the data */
480 TRACE("Transposing data from %d rows\n", t->row_count );
481 for( i=0; i<t->row_count; i++ )
483 t->data[i] = msi_alloc( row_size );
487 for( j=0; j<num_cols; j++ )
489 UINT ofs = cols[j].offset/2;
490 UINT n = bytes_per_column( &cols[j] );
495 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
498 t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
499 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
502 ERR("oops - unknown column width %d\n", n);
516 void free_cached_tables( MSIDATABASE *db )
518 while( !list_empty( &db->tables ) )
520 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
522 list_remove( &t->entry );
527 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
531 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
532 if( !lstrcmpW( name, t->name ) )
538 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
540 UINT r, column_count = 0;
541 MSICOLUMNINFO *columns;
543 /* get the number of columns in this table */
545 r = get_tablecolumns( db, name, NULL, &column_count );
546 if( r != ERROR_SUCCESS )
549 /* if there's no columns, there's no table */
550 if( column_count == 0 )
551 return ERROR_INVALID_PARAMETER;
553 TRACE("Table %s found\n", debugstr_w(name) );
555 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
557 return ERROR_FUNCTION_FAILED;
559 r = get_tablecolumns( db, name, columns, &column_count );
560 if( r != ERROR_SUCCESS )
563 return ERROR_FUNCTION_FAILED;
567 *pcount = column_count;
572 static MSITABLE *get_table( MSIDATABASE *db, LPCWSTR name,
573 const MSICOLUMNINFO *cols, UINT num_cols )
577 /* first, see if the table is cached */
578 table = find_cached_table( db, name );
582 table = read_table_from_storage( db->storage, name, cols, num_cols );
584 list_add_head( &db->tables, &table->entry );
589 static UINT save_table( MSIDATABASE *db, MSITABLE *t )
591 USHORT *rawdata = NULL, *p;
592 UINT rawsize, r, i, j, row_size, num_cols = 0;
593 MSICOLUMNINFO *cols = NULL;
595 TRACE("Saving %s\n", debugstr_w( t->name ) );
597 r = table_get_column_info( db, t->name, &cols, &num_cols );
598 if( r != ERROR_SUCCESS )
601 row_size = msi_table_get_row_size( cols, num_cols );
603 rawsize = t->row_count * row_size;
604 rawdata = msi_alloc_zero( rawsize );
607 r = ERROR_NOT_ENOUGH_MEMORY;
612 for( i=0; i<num_cols; i++ )
614 for( j=0; j<t->row_count; j++ )
616 UINT offset = cols[i].offset;
618 *p++ = t->data[j][offset/2];
619 if( 4 == bytes_per_column( &cols[i] ) )
620 *p++ = t->data[j][offset/2+1];
624 TRACE("writing %d bytes\n", rawsize);
625 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
628 msi_free_colinfo( cols, num_cols );
635 HRESULT init_string_table( IStorage *stg )
638 static const WCHAR szStringData[] = {
639 '_','S','t','r','i','n','g','D','a','t','a',0 };
640 static const WCHAR szStringPool[] = {
641 '_','S','t','r','i','n','g','P','o','o','l',0 };
642 USHORT zero[2] = { 0, 0 };
647 encname = encode_streamname(TRUE, szStringPool );
649 /* create the StringPool stream... add the zero string to it*/
650 r = IStorage_CreateStream( stg, encname,
651 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
659 r = IStream_Write(stm, zero, sizeof zero, &count );
660 IStream_Release( stm );
662 if( FAILED( r ) || ( count != sizeof zero ) )
668 /* create the StringData stream... make it zero length */
669 encname = encode_streamname(TRUE, szStringData );
670 r = IStorage_CreateStream( stg, encname,
671 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
678 IStream_Release( stm );
683 string_table *load_string_table( IStorage *stg )
685 string_table *st = NULL;
688 UINT r, datasize = 0, poolsize = 0, codepage;
689 DWORD i, count, offset, len, n, refs;
690 static const WCHAR szStringData[] = {
691 '_','S','t','r','i','n','g','D','a','t','a',0 };
692 static const WCHAR szStringPool[] = {
693 '_','S','t','r','i','n','g','P','o','o','l',0 };
695 r = read_stream_data( stg, szStringPool, &pool, &poolsize );
696 if( r != ERROR_SUCCESS)
698 r = read_stream_data( stg, szStringData, (USHORT**)&data, &datasize );
699 if( r != ERROR_SUCCESS)
704 codepage = pool[0] | ( pool[1] << 16 );
707 st = msi_init_stringtable( count, codepage );
714 /* the string reference count is always the second word */
717 /* empty entries have two zeros, still have a string id */
718 if (pool[i*2] == 0 && refs == 0)
726 * If a string is over 64k, the previous string entry is made null
727 * and its the high word of the length is inserted in the null string's
728 * reference count field.
732 len = (pool[i*2+3] << 16) + pool[i*2+2];
741 if ( (offset + len) > datasize )
743 ERR("string table corrupt?\n");
747 r = msi_addstring( st, n, data+offset, len, refs );
749 ERR("Failed to add string %ld\n", n );
754 if ( datasize != offset )
755 ERR("string table load failed! (%08x != %08lx), please report\n", datasize, offset );
757 TRACE("Loaded %ld strings\n", count);
766 static UINT save_string_table( MSIDATABASE *db )
768 UINT i, count, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
769 UINT ret = ERROR_FUNCTION_FAILED;
770 static const WCHAR szStringData[] = {
771 '_','S','t','r','i','n','g','D','a','t','a',0 };
772 static const WCHAR szStringPool[] = {
773 '_','S','t','r','i','n','g','P','o','o','l',0 };
779 /* construct the new table in memory first */
780 count = msi_string_totalsize( db->strings, &datasize, &poolsize );
782 TRACE("%u %u %u\n", count, datasize, poolsize );
784 pool = msi_alloc( poolsize );
787 WARN("Failed to alloc pool %d bytes\n", poolsize );
790 data = msi_alloc( datasize );
793 WARN("Failed to alloc data %d bytes\n", poolsize );
798 codepage = msi_string_get_codepage( db->strings );
799 pool[0]=codepage&0xffff;
800 pool[1]=(codepage>>16);
802 for( i=1; i<count; i++ )
804 sz = datasize - used;
805 r = msi_id2stringA( db->strings, i, data+used, &sz );
806 if( r != ERROR_SUCCESS )
808 ERR("failed to fetch string\n");
811 if( sz && (sz < (datasize - used ) ) )
814 pool[ n*2 + 1 ] = msi_id_refcount( db->strings, i );
823 pool[ n*2 + 2 ] = sz&0xffff;
824 pool[ n*2 + 3 ] = (sz>>16);
828 if( used > datasize )
830 ERR("oops overran %d >= %d\n", used, datasize);
835 if( used != datasize )
837 ERR("oops used %d != datasize %d\n", used, datasize);
841 /* write the streams */
842 r = write_stream_data( db->storage, szStringData, data, datasize );
843 TRACE("Wrote StringData r=%08x\n", r);
846 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
847 TRACE("Wrote StringPool r=%08x\n", r);
860 /* information for default tables */
861 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
862 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
863 static const WCHAR szName[] = { 'N','a','m','e',0 };
864 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
865 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
866 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
867 static const WCHAR szType[] = { 'T','y','p','e',0 };
869 static const MSICOLUMNINFO _Columns_cols[4] = {
870 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
871 { szColumns, 2, szNumber, MSITYPE_VALID | 2, 2 },
872 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
873 { szColumns, 4, szType, MSITYPE_VALID | 2, 6 },
875 static const MSICOLUMNINFO _Tables_cols[1] = {
876 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
879 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
881 const MSICOLUMNINFO *p;
884 TRACE("%s\n", debugstr_w(name));
886 if (!lstrcmpW( name, szTables ))
891 else if (!lstrcmpW( name, szColumns ))
897 return ERROR_FUNCTION_FAILED;
899 /* duplicate the string data so we can free it in msi_free_colinfo */
902 if (colinfo && (i < *sz) )
904 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
905 colinfo[i].tablename = strdupW( p[i].tablename );
906 colinfo[i].colname = strdupW( p[i].colname );
908 if( colinfo && (i >= *sz) )
912 return ERROR_SUCCESS;
915 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
919 for( i=0; i<count; i++ )
921 msi_free( (LPWSTR) colinfo[i].tablename );
922 msi_free( (LPWSTR) colinfo[i].colname );
923 msi_free( colinfo[i].hash_table );
927 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
932 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
933 if( r != ERROR_SUCCESS )
935 str = msi_alloc( sz*sizeof (WCHAR) );
938 r = msi_id2stringW( db->strings, stringid, str, &sz );
939 if( r == ERROR_SUCCESS )
945 static UINT get_tablecolumns( MSIDATABASE *db,
946 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
948 UINT r, i, n=0, table_id, count, maxcount = *sz;
949 MSITABLE *table = NULL;
951 /* first check if there is a default table with that name */
952 r = get_defaulttablecolumns( szTableName, colinfo, sz );
953 if( ( r == ERROR_SUCCESS ) && *sz )
956 table = get_table( db, szColumns, _Columns_cols, 4 );
959 ERR("couldn't load _Columns table\n");
960 return ERROR_FUNCTION_FAILED;
963 /* convert table and column names to IDs from the string table */
964 r = msi_string2idW( db->strings, szTableName, &table_id );
965 if( r != ERROR_SUCCESS )
967 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
971 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
973 count = table->row_count;
974 for( i=0; i<count; i++ )
976 if( table->data[ i ][ 0 ] != table_id )
980 UINT id = table->data[ i ] [ 2 ];
981 colinfo[n].tablename = MSI_makestring( db, table_id );
982 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
983 colinfo[n].colname = MSI_makestring( db, id );
984 colinfo[n].type = table->data[ i ] [ 3 ] ^ 0x8000;
985 colinfo[n].hash_table = NULL;
986 /* this assumes that columns are in order in the table */
988 colinfo[n].offset = colinfo[n-1].offset
989 + bytes_per_column( &colinfo[n-1] );
991 colinfo[n].offset = 0;
992 TRACE("table %s column %d is [%s] (%d) with type %08x "
993 "offset %d at row %d\n", debugstr_w(szTableName),
994 colinfo[n].number, debugstr_w(colinfo[n].colname),
995 id, colinfo[n].type, colinfo[n].offset, i);
996 if( n != (colinfo[n].number-1) )
998 ERR("oops. data in the _Columns table isn't in the right "
999 "order for table %s\n", debugstr_w(szTableName));
1000 return ERROR_FUNCTION_FAILED;
1004 if( colinfo && ( n >= maxcount ) )
1009 return ERROR_SUCCESS;
1012 /* try to find the table name in the _Tables table */
1013 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
1015 UINT r, table_id = 0, i, count;
1016 MSITABLE *table = NULL;
1018 if( !lstrcmpW( name, szTables ) )
1020 if( !lstrcmpW( name, szColumns ) )
1023 r = msi_string2idW( db->strings, name, &table_id );
1024 if( r != ERROR_SUCCESS )
1026 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1030 table = get_table( db, szTables, _Tables_cols, 1 );
1033 TRACE("table %s not available\n", debugstr_w(szTables));
1037 /* count = table->size/2; */
1038 count = table->row_count;
1039 for( i=0; i<count; i++ )
1040 if( table->data[ i ][ 0 ] == table_id )
1046 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1051 /* below is the query interface to a table */
1053 typedef struct tagMSITABLEVIEW
1058 MSICOLUMNINFO *columns;
1064 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1066 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1067 UINT offset, num_rows, n;
1070 return ERROR_INVALID_PARAMETER;
1072 if( (col==0) || (col>tv->num_cols) )
1073 return ERROR_INVALID_PARAMETER;
1075 /* how many rows are there ? */
1076 num_rows = tv->table->row_count;
1077 if( row >= num_rows )
1078 return ERROR_NO_MORE_ITEMS;
1080 if( tv->columns[col-1].offset >= tv->row_size )
1082 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1083 ERR("%p %p\n", tv, tv->columns );
1084 return ERROR_FUNCTION_FAILED;
1087 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1088 n = bytes_per_column( &tv->columns[col-1] );
1092 offset = tv->columns[col-1].offset/2;
1093 *val = tv->table->data[row][offset] +
1094 (tv->table->data[row][offset + 1] << 16);
1097 offset = tv->columns[col-1].offset/2;
1098 *val = tv->table->data[row][offset];
1101 ERR("oops! what is %d bytes per column?\n", n );
1102 return ERROR_FUNCTION_FAILED;
1105 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1107 return ERROR_SUCCESS;
1111 * We need a special case for streams, as we need to reference column with
1112 * the name of the stream in the same table, and the table name
1113 * which may not be available at higher levels of the query
1115 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1117 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1118 UINT ival = 0, refcol = 0, r;
1122 static const WCHAR szDot[] = { '.', 0 };
1124 if( !view->ops->fetch_int )
1125 return ERROR_INVALID_PARAMETER;
1128 * The column marked with the type stream data seems to have a single number
1129 * which references the column containing the name of the stream data
1131 * Fetch the column to reference first.
1133 r = view->ops->fetch_int( view, row, col, &ival );
1134 if( r != ERROR_SUCCESS )
1137 /* now get the column with the name of the stream */
1138 r = view->ops->fetch_int( view, row, ival, &refcol );
1139 if( r != ERROR_SUCCESS )
1142 /* lookup the string value from the string table */
1143 sval = MSI_makestring( tv->db, refcol );
1145 return ERROR_INVALID_PARAMETER;
1147 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1148 full_name = msi_alloc( len*sizeof(WCHAR) );
1149 lstrcpyW( full_name, tv->name );
1150 lstrcatW( full_name, szDot );
1151 lstrcatW( full_name, sval );
1153 r = db_get_raw_stream( tv->db, full_name, stm );
1155 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1156 msi_free( full_name );
1162 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1164 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1168 return ERROR_INVALID_PARAMETER;
1170 if( (col==0) || (col>tv->num_cols) )
1171 return ERROR_INVALID_PARAMETER;
1173 if( tv->columns[col-1].offset >= tv->row_size )
1175 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1176 ERR("%p %p\n", tv, tv->columns );
1177 return ERROR_FUNCTION_FAILED;
1180 n = bytes_per_column( &tv->columns[col-1] );
1184 offset = tv->columns[col-1].offset/2;
1185 tv->table->data[row][offset] = val & 0xffff;
1186 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1189 offset = tv->columns[col-1].offset/2;
1190 tv->table->data[row][offset] = val;
1193 ERR("oops! what is %d bytes per column?\n", n );
1194 return ERROR_FUNCTION_FAILED;
1196 return ERROR_SUCCESS;
1199 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
1201 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1205 TRACE("%p\n", view);
1208 return ERROR_INVALID_PARAMETER;
1210 row = msi_alloc_zero( tv->row_size );
1212 return ERROR_NOT_ENOUGH_MEMORY;
1214 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1215 if( tv->table->data )
1216 p = msi_realloc( tv->table->data, sz );
1218 p = msi_alloc( sz );
1222 return ERROR_NOT_ENOUGH_MEMORY;
1225 tv->table->data = p;
1226 tv->table->data[tv->table->row_count] = row;
1227 *num = tv->table->row_count;
1228 tv->table->row_count++;
1230 return ERROR_SUCCESS;
1233 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1235 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1237 TRACE("%p %p\n", tv, record);
1239 TRACE("There are %d columns\n", tv->num_cols );
1240 tv->table = get_table( tv->db, tv->name, tv->columns, tv->num_cols );
1242 return ERROR_FUNCTION_FAILED;
1244 return ERROR_SUCCESS;
1247 static UINT TABLE_close( struct tagMSIVIEW *view )
1249 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1251 TRACE("%p\n", view );
1254 return ERROR_FUNCTION_FAILED;
1258 return ERROR_SUCCESS;
1261 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1263 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1265 TRACE("%p %p %p\n", view, rows, cols );
1268 *cols = tv->num_cols;
1272 return ERROR_INVALID_PARAMETER;
1273 *rows = tv->table->row_count;
1276 return ERROR_SUCCESS;
1279 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1280 UINT n, LPWSTR *name, UINT *type )
1282 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1284 TRACE("%p %d %p %p\n", tv, n, name, type );
1286 if( ( n == 0 ) || ( n > tv->num_cols ) )
1287 return ERROR_INVALID_PARAMETER;
1291 *name = strdupW( tv->columns[n-1].colname );
1293 return ERROR_FUNCTION_FAILED;
1296 *type = tv->columns[n-1].type;
1298 return ERROR_SUCCESS;
1301 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1303 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1307 r = msi_table_find_row( tv, rec, &row );
1308 if (r != ERROR_SUCCESS)
1309 return ERROR_SUCCESS;
1310 return ERROR_INVALID_DATA;
1313 static UINT msi_table_modify_row( MSITABLEVIEW *tv, MSIRECORD *rec,
1314 UINT row, UINT mask )
1316 UINT i, val, r = ERROR_SUCCESS;
1318 TRACE("%p %p %u %08x\n", tv, rec, row, mask );
1320 for( i = 0; i < tv->num_cols; i++ )
1322 /* set keys or values specified in the mask */
1323 if( (~tv->columns[i].type & MSITYPE_KEY) && (~mask & (1<<i)) )
1326 if( (tv->columns[i].type & MSITYPE_STRING) &&
1327 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1329 const WCHAR *str = MSI_RecordGetString( rec, i+1 );
1330 val = msi_addstringW( tv->db->strings, 0, str, -1, 1 );
1334 val = MSI_RecordGetInteger( rec, i+1 );
1335 if ( 2 == bytes_per_column( &tv->columns[i] ) )
1340 r = TABLE_set_int( &tv->view, row, i+1, val );
1348 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1350 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1353 TRACE("%p %p\n", tv, rec );
1355 r = table_create_new_row( view, &row );
1356 TRACE("insert_row returned %08x\n", r);
1357 if( r != ERROR_SUCCESS )
1360 return msi_table_modify_row( tv, rec, row, ~0 );
1363 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1366 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1369 TRACE("%p %d %p\n", view, eModifyMode, rec );
1373 r = TABLE_execute( view, NULL );
1378 switch (eModifyMode)
1380 case MSIMODIFY_VALIDATE_NEW:
1381 r = table_validate_new( tv, rec );
1384 case MSIMODIFY_INSERT_TEMPORARY:
1385 r = table_validate_new( tv, rec );
1386 if (r != ERROR_SUCCESS)
1388 r = TABLE_insert_row( view, rec );
1391 case MSIMODIFY_REFRESH:
1392 case MSIMODIFY_INSERT:
1393 case MSIMODIFY_UPDATE:
1394 case MSIMODIFY_ASSIGN:
1395 case MSIMODIFY_REPLACE:
1396 case MSIMODIFY_MERGE:
1397 case MSIMODIFY_DELETE:
1398 case MSIMODIFY_VALIDATE:
1399 case MSIMODIFY_VALIDATE_FIELD:
1400 case MSIMODIFY_VALIDATE_DELETE:
1401 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1402 r = ERROR_CALL_NOT_IMPLEMENTED;
1406 r = ERROR_INVALID_DATA;
1412 static UINT TABLE_delete( struct tagMSIVIEW *view )
1414 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1416 TRACE("%p\n", view );
1422 msi_free_colinfo( tv->columns, tv->num_cols );
1423 msi_free( tv->columns );
1429 return ERROR_SUCCESS;
1432 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1433 UINT val, UINT *row, MSIITERHANDLE *handle )
1435 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1436 const MSICOLUMNHASHENTRY *entry;
1438 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1441 return ERROR_INVALID_PARAMETER;
1443 if( (col==0) || (col > tv->num_cols) )
1444 return ERROR_INVALID_PARAMETER;
1446 if( !tv->columns[col-1].hash_table )
1449 UINT num_rows = tv->table->row_count;
1450 MSICOLUMNHASHENTRY **hash_table;
1451 MSICOLUMNHASHENTRY *new_entry;
1453 if( tv->columns[col-1].offset >= tv->row_size )
1455 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1456 ERR("%p %p\n", tv, tv->columns );
1457 return ERROR_FUNCTION_FAILED;
1460 /* allocate contiguous memory for the table and its entries so we
1461 * don't have to do an expensive cleanup */
1462 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1463 num_rows * sizeof(MSICOLUMNHASHENTRY));
1465 return ERROR_OUTOFMEMORY;
1467 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1468 tv->columns[col-1].hash_table = hash_table;
1470 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1472 for (i = 0; i < num_rows; i++, new_entry++)
1475 UINT offset = i + (tv->columns[col-1].offset/2) * num_rows;
1476 n = bytes_per_column( &tv->columns[col-1] );
1480 offset = tv->columns[col-1].offset/2;
1481 row_value = tv->table->data[i][offset] +
1482 (tv->table->data[i][offset + 1] << 16);
1485 offset = tv->columns[col-1].offset/2;
1486 row_value = tv->table->data[i][offset];
1489 ERR("oops! what is %d bytes per column?\n", n );
1493 new_entry->next = NULL;
1494 new_entry->value = row_value;
1496 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1498 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1499 while (prev_entry->next)
1500 prev_entry = prev_entry->next;
1501 prev_entry->next = new_entry;
1504 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1509 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1511 entry = ((const MSICOLUMNHASHENTRY *)*handle)->next;
1513 while (entry && entry->value != val)
1514 entry = entry->next;
1516 *handle = (MSIITERHANDLE)entry;
1518 return ERROR_NO_MORE_ITEMS;
1521 return ERROR_SUCCESS;
1525 static const MSIVIEWOPS table_ops =
1533 TABLE_get_dimensions,
1534 TABLE_get_column_info,
1537 TABLE_find_matching_rows
1540 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1543 UINT r, sz, column_count;
1544 MSICOLUMNINFO *columns;
1546 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1548 /* get the number of columns in this table */
1550 r = get_tablecolumns( db, name, NULL, &column_count );
1551 if( r != ERROR_SUCCESS )
1554 /* if there's no columns, there's no table */
1555 if( column_count == 0 )
1556 return ERROR_INVALID_PARAMETER;
1558 TRACE("Table found\n");
1560 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1561 tv = msi_alloc_zero( sz );
1563 return ERROR_FUNCTION_FAILED;
1565 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO));
1569 return ERROR_FUNCTION_FAILED;
1572 r = get_tablecolumns( db, name, columns, &column_count );
1573 if( r != ERROR_SUCCESS )
1575 msi_free( columns );
1577 return ERROR_FUNCTION_FAILED;
1580 TRACE("Table has %d columns\n", column_count);
1582 /* fill the structure */
1583 tv->view.ops = &table_ops;
1585 tv->columns = columns;
1586 tv->num_cols = column_count;
1588 tv->row_size = msi_table_get_row_size( columns, column_count );
1590 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
1592 *view = (MSIVIEW*) tv;
1593 lstrcpyW( tv->name, name );
1595 return ERROR_SUCCESS;
1598 UINT MSI_CommitTables( MSIDATABASE *db )
1601 MSITABLE *table = NULL;
1605 r = save_string_table( db );
1606 if( r != ERROR_SUCCESS )
1608 WARN("failed to save string table r=%08x\n",r);
1612 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1614 r = save_table( db, table );
1615 if( r != ERROR_SUCCESS )
1617 WARN("failed to save table %s (r=%08x)\n",
1618 debugstr_w(table->name), r);
1623 /* force everything to reload next time */
1624 free_cached_tables( db );
1626 return ERROR_SUCCESS;
1629 static MSIRECORD *msi_get_transform_record( MSITABLEVIEW *tv, string_table *st, USHORT *rawdata )
1631 UINT i, val, ofs = 0;
1632 USHORT mask = *rawdata++;
1633 MSICOLUMNINFO *columns = tv->columns;
1636 rec = MSI_CreateRecord( tv->num_cols );
1641 for( i=0; i<tv->num_cols; i++ )
1643 UINT n = bytes_per_column( &columns[i] );
1645 if ( (mask&1) && (i>=(mask>>8)) )
1647 /* all keys must be present */
1648 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
1655 if( (columns[i].type & MSITYPE_STRING) &&
1656 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1658 LPCWSTR sval = msi_string_lookup_id( st, val );
1659 MSI_RecordSetStringW( rec, i+1, sval );
1660 TRACE("[%s]", debugstr_w(sval));
1665 MSI_RecordSetInteger( rec, i+1, val );
1666 TRACE("[0x%04x]", val );
1670 val = rawdata[ofs] + (rawdata[ofs + 1]<<16);
1671 /* val ^= 0x80000000; */
1672 MSI_RecordSetInteger( rec, i+1, val );
1673 TRACE("[0x%08x]", val );
1676 ERR("oops - unknown column width %d\n", n);
1685 static void dump_record( MSIRECORD *rec )
1689 n = MSI_RecordGetFieldCount( rec );
1690 for( i=1; i<=n; i++ )
1692 LPCWSTR sval = MSI_RecordGetString( rec, i );
1694 if( MSI_RecordIsNull( rec, i ) )
1695 TRACE("row -> []\n");
1696 else if( (sval = MSI_RecordGetString( rec, i )) )
1697 TRACE("row -> [%s]\n", debugstr_w(sval));
1699 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
1703 static void dump_table( string_table *st, USHORT *rawdata, UINT rawsize )
1708 for( i=0; i<(rawsize/2); i++ )
1710 sval = msi_string_lookup_id( st, rawdata[i] );
1711 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
1715 static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
1720 data = msi_alloc( tv->num_cols *sizeof (UINT) );
1721 for( i=0; i<tv->num_cols; i++ )
1725 if ( ~tv->columns[i].type & MSITYPE_KEY )
1728 /* turn the transform column value into a row value */
1729 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
1730 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1732 str = MSI_RecordGetString( rec, i+1 );
1733 r = msi_string2idW( tv->db->strings, str, &data[i] );
1735 /* if there's no matching string in the string table,
1736 these keys can't match any record, so fail now. */
1737 if( ERROR_SUCCESS != r )
1744 data[i] = MSI_RecordGetInteger( rec, i+1 );
1749 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, UINT *data )
1751 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
1753 for( i=0; i<tv->num_cols; i++ )
1755 if ( ~tv->columns[i].type & MSITYPE_KEY )
1758 /* turn the transform column value into a row value */
1759 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
1760 if ( r != ERROR_SUCCESS )
1762 ERR("TABLE_fetch_int shouldn't fail here\n");
1766 /* if this key matches, move to the next column */
1769 ret = ERROR_FUNCTION_FAILED;
1773 ret = ERROR_SUCCESS;
1779 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
1781 UINT i, r = ERROR_FUNCTION_FAILED, *data;
1783 data = msi_record_to_row( tv, rec );
1786 for( i=0; i<tv->table->row_count; i++ )
1788 r = msi_row_matches( tv, i, data );
1789 if( r == ERROR_SUCCESS )
1799 static UINT msi_delete_row( MSITABLEVIEW *tv, UINT row )
1802 for( i=1; i<=tv->num_cols; i++ )
1803 tv->view.ops->set_int( &tv->view, row, i, 0 );
1804 return ERROR_SUCCESS;
1807 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
1808 string_table *st, LPCWSTR name )
1811 USHORT *rawdata = NULL;
1812 MSITABLEVIEW *tv = NULL;
1813 UINT r, n, sz, i, mask;
1814 MSIRECORD *rec = NULL;
1816 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
1818 /* create a table view */
1819 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
1820 if( r != ERROR_SUCCESS )
1823 r = tv->view.ops->execute( &tv->view, NULL );
1824 if( r != ERROR_SUCCESS )
1827 /* read the transform data */
1828 r = ERROR_FUNCTION_FAILED;
1829 read_stream_data( stg, name, &rawdata, &rawsize );
1830 if( !rawdata || (rawsize < 2) )
1832 ERR("odd sized transform for table %s\n", debugstr_w(name));
1836 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1837 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
1839 /* interpret the data */
1841 for( n=0; n < (rawsize/2); )
1848 * if the low bit is set, columns are continuous and
1849 * the number of columns is specified in the high byte
1851 sz = 2 + tv->row_size;
1856 * If the low bit is not set, rowdata[n] is a bitmask.
1857 * Excepting for key fields, which are always present,
1858 * each bit indicates that a field is present in the transform record.
1860 * rawdata[n] == 0 is a special case ... only the keys will be present
1861 * and it means that this row should be deleted.
1864 for( i=0; i<tv->num_cols; i++ )
1866 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
1867 sz += bytes_per_column( &tv->columns[i] );
1871 /* check we didn't run of the end of the table */
1872 if ( (n+sz) > rawsize )
1875 dump_table( st, rawdata, rawsize );
1879 rec = msi_get_transform_record( tv, st, &rawdata[n] );
1884 r = msi_table_find_row( tv, rec, &row );
1888 TRACE("insert [%d]: ", row);
1889 TABLE_insert_row( &tv->view, rec );
1891 else if( mask & 0xff )
1893 TRACE("modify [%d]: ", row);
1894 msi_table_modify_row( tv, rec, row, mask );
1898 TRACE("delete [%d]: ", row);
1899 msi_delete_row( tv, row );
1901 if( TRACE_ON(msidb) ) dump_record( rec );
1902 msiobj_release( &rec->hdr );
1910 /* no need to free the table, it's associated with the database */
1911 msi_free( rawdata );
1913 tv->view.ops->delete( &tv->view );
1915 return ERROR_SUCCESS;
1919 * msi_table_apply_transform
1921 * Enumerate the table transforms in a transform storage and apply each one.
1923 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
1925 IEnumSTATSTG *stgenum = NULL;
1930 string_table *strings;
1931 UINT ret = ERROR_FUNCTION_FAILED;
1933 TRACE("%p %p\n", db, stg );
1935 strings = load_string_table( stg );
1939 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
1944 ret = ERROR_SUCCESS;
1946 while( r == ERROR_SUCCESS )
1949 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
1950 if( FAILED( r ) || !count )
1952 decode_streamname( stat.pwcsName, name );
1953 if( ( name[0] == 0x4840 ) && ( name[1] != '_' ) )
1954 ret = msi_table_load_transform( db, stg, strings, name+1 );
1956 TRACE("transform contains stream %s\n", debugstr_w(name));
1960 if ( ret == ERROR_SUCCESS )
1964 t = msi_alloc( sizeof *t );
1966 IStorage_AddRef( stg );
1967 list_add_tail( &db->transforms, &t->entry );
1972 IEnumSTATSTG_Release( stgenum );
1974 msi_destroy_stringtable( strings );
1979 void msi_free_transforms( MSIDATABASE *db )
1981 while( !list_empty( &db->transforms ) )
1983 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
1984 MSITRANSFORM, entry );
1985 list_remove( &t->entry );
1986 IStorage_Release( t->stg );