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 static const WCHAR szStringData[] = {
75 '_','S','t','r','i','n','g','D','a','t','a',0 };
76 static const WCHAR szStringPool[] = {
77 '_','S','t','r','i','n','g','P','o','o','l',0 };
79 #define MAX_STREAM_NAME 0x1f
81 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
82 MSICOLUMNINFO **pcols, UINT *pcount );
83 static UINT get_tablecolumns( MSIDATABASE *db,
84 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
85 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
87 static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
89 if( col->type & MSITYPE_STRING )
91 if( (col->type & 0xff) > 4 )
92 ERR("Invalid column size!\n");
93 return col->type & 0xff;
96 static int utf2mime(int x)
98 if( (x>='0') && (x<='9') )
100 if( (x>='A') && (x<='Z') )
102 if( (x>='a') && (x<='z') )
111 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
113 DWORD count = MAX_STREAM_NAME;
118 count = lstrlenW( in )+2;
119 out = msi_alloc( count*sizeof(WCHAR) );
135 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
137 ch = utf2mime(ch) + 0x4800;
139 if( next && (next<0x80) )
141 next = utf2mime(next);
152 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
157 static int mime2utf(int x)
164 return x - 10 - 26 + 'a';
165 if( x == (10+26+26) )
170 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
175 while ( (ch = *in++) )
177 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
180 ch = mime2utf(ch-0x4800);
184 *out++ = mime2utf(ch&0x3f);
186 ch = mime2utf((ch>>6)&0x3f);
196 void enum_stream_names( IStorage *stg )
198 IEnumSTATSTG *stgenum = NULL;
204 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
212 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
213 if( FAILED( r ) || !count )
215 decode_streamname( stat.pwcsName, name );
216 TRACE("stream %2d -> %s %s\n", n,
217 debugstr_w(stat.pwcsName), debugstr_w(name) );
221 IEnumSTATSTG_Release( stgenum );
224 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
225 USHORT **pdata, UINT *psz )
228 UINT ret = ERROR_FUNCTION_FAILED;
235 encname = encode_streamname(TRUE, stname);
237 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
239 r = IStorage_OpenStream(stg, encname, NULL,
240 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
244 WARN("open stream failed r = %08x - empty table?\n", r);
248 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
251 WARN("open stream failed r = %08x!\n", r);
255 if( stat.cbSize.QuadPart >> 32 )
261 sz = stat.cbSize.QuadPart;
262 data = msi_alloc( sz );
265 WARN("couldn't allocate memory r=%08x!\n", r);
266 ret = ERROR_NOT_ENOUGH_MEMORY;
270 r = IStream_Read(stm, data, sz, &count );
271 if( FAILED( r ) || ( count != sz ) )
274 WARN("read stream failed r = %08x!\n", r);
283 IStream_Release( stm );
288 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
293 encname = encode_streamname(FALSE, stname);
295 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
297 r = IStorage_OpenStream(db->storage, encname, NULL,
298 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
301 MSITRANSFORM *transform;
303 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
305 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
306 r = IStorage_OpenStream( transform->stg, encname, NULL,
307 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
315 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
318 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
319 USHORT **pdata, UINT *psz )
322 UINT ret = ERROR_FUNCTION_FAILED;
328 r = db_get_raw_stream( db, stname, &stm );
329 if( r != ERROR_SUCCESS)
331 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
334 WARN("open stream failed r = %08x!\n", r);
338 if( stat.cbSize.QuadPart >> 32 )
344 sz = stat.cbSize.QuadPart;
345 data = msi_alloc( sz );
348 WARN("couldn't allocate memory r=%08x!\n", r);
349 ret = ERROR_NOT_ENOUGH_MEMORY;
353 r = IStream_Read(stm, data, sz, &count );
354 if( FAILED( r ) || ( count != sz ) )
357 WARN("read stream failed r = %08x!\n", r);
366 IStream_Release( stm );
371 static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
372 LPVOID data, UINT sz )
375 UINT ret = ERROR_FUNCTION_FAILED;
382 encname = encode_streamname(TRUE, stname );
383 r = IStorage_OpenStream( stg, encname, NULL,
384 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
387 r = IStorage_CreateStream( stg, encname,
388 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
393 WARN("open stream failed r = %08x\n", r);
398 r = IStream_SetSize( stm, size );
401 WARN("Failed to SetSize\n");
406 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
409 WARN("Failed to Seek\n");
413 r = IStream_Write(stm, data, sz, &count );
414 if( FAILED( r ) || ( count != sz ) )
416 WARN("Failed to Write\n");
423 IStream_Release( stm );
428 static void free_table( MSITABLE *table )
431 for( i=0; i<table->row_count; i++ )
432 msi_free( table->data[i] );
433 msi_free( table->data );
437 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
439 const MSICOLUMNINFO *last_col = &cols[count-1];
442 return last_col->offset + bytes_per_column( last_col );
445 /* add this table to the list of cached tables in the database */
446 static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
447 const MSICOLUMNINFO *cols, UINT num_cols )
450 USHORT *rawdata = NULL;
451 UINT rawsize = 0, i, j, row_size = 0;
453 TRACE("%s\n",debugstr_w(name));
455 /* nonexistent tables should be interpreted as empty tables */
456 t = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
460 row_size = msi_table_get_row_size( cols, num_cols );
464 lstrcpyW( t->name, name );
466 /* if we can't read the table, just assume that it's empty */
467 read_stream_data( stg, name, &rawdata, &rawsize );
471 TRACE("Read %d bytes\n", rawsize );
473 if( rawsize % row_size )
475 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
479 t->row_count = rawsize / row_size;
480 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
484 /* transpose all the data */
485 TRACE("Transposing data from %d rows\n", t->row_count );
486 for( i=0; i<t->row_count; i++ )
488 t->data[i] = msi_alloc( row_size );
492 for( j=0; j<num_cols; j++ )
494 UINT ofs = cols[j].offset/2;
495 UINT n = bytes_per_column( &cols[j] );
500 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
503 t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
504 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
507 ERR("oops - unknown column width %d\n", n);
521 void free_cached_tables( MSIDATABASE *db )
523 while( !list_empty( &db->tables ) )
525 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
527 list_remove( &t->entry );
532 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
536 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
537 if( !lstrcmpW( name, t->name ) )
543 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
545 UINT r, column_count = 0;
546 MSICOLUMNINFO *columns;
548 /* get the number of columns in this table */
550 r = get_tablecolumns( db, name, NULL, &column_count );
551 if( r != ERROR_SUCCESS )
554 /* if there's no columns, there's no table */
555 if( column_count == 0 )
556 return ERROR_INVALID_PARAMETER;
558 TRACE("Table %s found\n", debugstr_w(name) );
560 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
562 return ERROR_FUNCTION_FAILED;
564 r = get_tablecolumns( db, name, columns, &column_count );
565 if( r != ERROR_SUCCESS )
568 return ERROR_FUNCTION_FAILED;
572 *pcount = column_count;
577 static MSITABLE *get_table( MSIDATABASE *db, LPCWSTR name,
578 const MSICOLUMNINFO *cols, UINT num_cols )
582 /* first, see if the table is cached */
583 table = find_cached_table( db, name );
587 table = read_table_from_storage( db->storage, name, cols, num_cols );
589 list_add_head( &db->tables, &table->entry );
594 static UINT save_table( MSIDATABASE *db, MSITABLE *t )
596 USHORT *rawdata = NULL, *p;
597 UINT rawsize, r, i, j, row_size, num_cols = 0;
598 MSICOLUMNINFO *cols = NULL;
600 TRACE("Saving %s\n", debugstr_w( t->name ) );
602 r = table_get_column_info( db, t->name, &cols, &num_cols );
603 if( r != ERROR_SUCCESS )
606 row_size = msi_table_get_row_size( cols, num_cols );
608 rawsize = t->row_count * row_size;
609 rawdata = msi_alloc_zero( rawsize );
612 r = ERROR_NOT_ENOUGH_MEMORY;
617 for( i=0; i<num_cols; i++ )
619 for( j=0; j<t->row_count; j++ )
621 UINT offset = cols[i].offset;
623 *p++ = t->data[j][offset/2];
624 if( 4 == bytes_per_column( &cols[i] ) )
625 *p++ = t->data[j][offset/2+1];
629 TRACE("writing %d bytes\n", rawsize);
630 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
633 msi_free_colinfo( cols, num_cols );
640 HRESULT init_string_table( IStorage *stg )
643 USHORT zero[2] = { 0, 0 };
648 encname = encode_streamname(TRUE, szStringPool );
650 /* create the StringPool stream... add the zero string to it*/
651 r = IStorage_CreateStream( stg, encname,
652 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
660 r = IStream_Write(stm, zero, sizeof zero, &count );
661 IStream_Release( stm );
663 if( FAILED( r ) || ( count != sizeof zero ) )
669 /* create the StringData stream... make it zero length */
670 encname = encode_streamname(TRUE, szStringData );
671 r = IStorage_CreateStream( stg, encname,
672 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
679 IStream_Release( stm );
684 string_table *load_string_table( IStorage *stg )
686 string_table *st = NULL;
689 UINT r, datasize = 0, poolsize = 0, codepage;
690 DWORD i, count, offset, len, n, refs;
692 r = read_stream_data( stg, szStringPool, &pool, &poolsize );
693 if( r != ERROR_SUCCESS)
695 r = read_stream_data( stg, szStringData, (USHORT**)&data, &datasize );
696 if( r != ERROR_SUCCESS)
701 codepage = pool[0] | ( pool[1] << 16 );
704 st = msi_init_stringtable( count, codepage );
711 /* the string reference count is always the second word */
714 /* empty entries have two zeros, still have a string id */
715 if (pool[i*2] == 0 && refs == 0)
723 * If a string is over 64k, the previous string entry is made null
724 * and its the high word of the length is inserted in the null string's
725 * reference count field.
729 len = (pool[i*2+3] << 16) + pool[i*2+2];
738 if ( (offset + len) > datasize )
740 ERR("string table corrupt?\n");
744 r = msi_addstring( st, n, data+offset, len, refs );
746 ERR("Failed to add string %d\n", n );
751 if ( datasize != offset )
752 ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
754 TRACE("Loaded %d strings\n", count);
763 static UINT save_string_table( MSIDATABASE *db )
765 UINT i, count, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
766 UINT ret = ERROR_FUNCTION_FAILED;
772 /* construct the new table in memory first */
773 count = msi_string_totalsize( db->strings, &datasize, &poolsize );
775 TRACE("%u %u %u\n", count, datasize, poolsize );
777 pool = msi_alloc( poolsize );
780 WARN("Failed to alloc pool %d bytes\n", poolsize );
783 data = msi_alloc( datasize );
786 WARN("Failed to alloc data %d bytes\n", poolsize );
791 codepage = msi_string_get_codepage( db->strings );
792 pool[0]=codepage&0xffff;
793 pool[1]=(codepage>>16);
795 for( i=1; i<count; i++ )
797 sz = datasize - used;
798 r = msi_id2stringA( db->strings, i, data+used, &sz );
799 if( r != ERROR_SUCCESS )
801 ERR("failed to fetch string\n");
804 if( sz && (sz < (datasize - used ) ) )
807 pool[ n*2 + 1 ] = msi_id_refcount( db->strings, i );
816 pool[ n*2 + 2 ] = sz&0xffff;
817 pool[ n*2 + 3 ] = (sz>>16);
821 if( used > datasize )
823 ERR("oops overran %d >= %d\n", used, datasize);
828 if( used != datasize )
830 ERR("oops used %d != datasize %d\n", used, datasize);
834 /* write the streams */
835 r = write_stream_data( db->storage, szStringData, data, datasize );
836 TRACE("Wrote StringData r=%08x\n", r);
839 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
840 TRACE("Wrote StringPool r=%08x\n", r);
853 /* information for default tables */
854 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
855 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
856 static const WCHAR szName[] = { 'N','a','m','e',0 };
857 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
858 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
859 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
860 static const WCHAR szType[] = { 'T','y','p','e',0 };
862 static const MSICOLUMNINFO _Columns_cols[4] = {
863 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
864 { szColumns, 2, szNumber, MSITYPE_VALID | 2, 2 },
865 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
866 { szColumns, 4, szType, MSITYPE_VALID | 2, 6 },
868 static const MSICOLUMNINFO _Tables_cols[1] = {
869 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
872 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
874 const MSICOLUMNINFO *p;
877 TRACE("%s\n", debugstr_w(name));
879 if (!lstrcmpW( name, szTables ))
884 else if (!lstrcmpW( name, szColumns ))
890 return ERROR_FUNCTION_FAILED;
892 /* duplicate the string data so we can free it in msi_free_colinfo */
895 if (colinfo && (i < *sz) )
897 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
898 colinfo[i].tablename = strdupW( p[i].tablename );
899 colinfo[i].colname = strdupW( p[i].colname );
901 if( colinfo && (i >= *sz) )
905 return ERROR_SUCCESS;
908 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
912 for( i=0; i<count; i++ )
914 msi_free( (LPWSTR) colinfo[i].tablename );
915 msi_free( (LPWSTR) colinfo[i].colname );
916 msi_free( colinfo[i].hash_table );
920 static LPWSTR msi_makestring( MSIDATABASE *db, UINT stringid)
922 return strdupW(msi_string_lookup_id( db->strings, stringid ));
925 static UINT get_tablecolumns( MSIDATABASE *db,
926 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
928 UINT r, i, n=0, table_id, count, maxcount = *sz;
929 MSITABLE *table = NULL;
931 /* first check if there is a default table with that name */
932 r = get_defaulttablecolumns( szTableName, colinfo, sz );
933 if( ( r == ERROR_SUCCESS ) && *sz )
936 table = get_table( db, szColumns, _Columns_cols, 4 );
939 ERR("couldn't load _Columns table\n");
940 return ERROR_FUNCTION_FAILED;
943 /* convert table and column names to IDs from the string table */
944 r = msi_string2idW( db->strings, szTableName, &table_id );
945 if( r != ERROR_SUCCESS )
947 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
951 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
953 count = table->row_count;
954 for( i=0; i<count; i++ )
956 if( table->data[ i ][ 0 ] != table_id )
960 UINT id = table->data[ i ] [ 2 ];
961 colinfo[n].tablename = msi_makestring( db, table_id );
962 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
963 colinfo[n].colname = msi_makestring( db, id );
964 colinfo[n].type = table->data[ i ] [ 3 ] ^ 0x8000;
965 colinfo[n].hash_table = NULL;
966 /* this assumes that columns are in order in the table */
968 colinfo[n].offset = colinfo[n-1].offset
969 + bytes_per_column( &colinfo[n-1] );
971 colinfo[n].offset = 0;
972 TRACE("table %s column %d is [%s] (%d) with type %08x "
973 "offset %d at row %d\n", debugstr_w(szTableName),
974 colinfo[n].number, debugstr_w(colinfo[n].colname),
975 id, colinfo[n].type, colinfo[n].offset, i);
976 if( n != (colinfo[n].number-1) )
978 ERR("oops. data in the _Columns table isn't in the right "
979 "order for table %s\n", debugstr_w(szTableName));
980 msi_free_colinfo(colinfo, n+1 );
981 return ERROR_FUNCTION_FAILED;
985 if( colinfo && ( n >= maxcount ) )
990 return ERROR_SUCCESS;
993 /* try to find the table name in the _Tables table */
994 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
996 UINT r, table_id = 0, i, count;
997 MSITABLE *table = NULL;
999 if( !lstrcmpW( name, szTables ) )
1001 if( !lstrcmpW( name, szColumns ) )
1004 r = msi_string2idW( db->strings, name, &table_id );
1005 if( r != ERROR_SUCCESS )
1007 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1011 table = get_table( db, szTables, _Tables_cols, 1 );
1014 TRACE("table %s not available\n", debugstr_w(szTables));
1018 /* count = table->size/2; */
1019 count = table->row_count;
1020 for( i=0; i<count; i++ )
1021 if( table->data[ i ][ 0 ] == table_id )
1027 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1032 /* below is the query interface to a table */
1034 typedef struct tagMSITABLEVIEW
1039 MSICOLUMNINFO *columns;
1045 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1047 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1048 UINT offset, num_rows, n;
1051 return ERROR_INVALID_PARAMETER;
1053 if( (col==0) || (col>tv->num_cols) )
1054 return ERROR_INVALID_PARAMETER;
1056 /* how many rows are there ? */
1057 num_rows = tv->table->row_count;
1058 if( row >= num_rows )
1059 return ERROR_NO_MORE_ITEMS;
1061 if( tv->columns[col-1].offset >= tv->row_size )
1063 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1064 ERR("%p %p\n", tv, tv->columns );
1065 return ERROR_FUNCTION_FAILED;
1068 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1069 n = bytes_per_column( &tv->columns[col-1] );
1073 offset = tv->columns[col-1].offset/2;
1074 *val = tv->table->data[row][offset] +
1075 (tv->table->data[row][offset + 1] << 16);
1078 offset = tv->columns[col-1].offset/2;
1079 *val = tv->table->data[row][offset];
1082 ERR("oops! what is %d bytes per column?\n", n );
1083 return ERROR_FUNCTION_FAILED;
1086 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1088 return ERROR_SUCCESS;
1092 * We need a special case for streams, as we need to reference column with
1093 * the name of the stream in the same table, and the table name
1094 * which may not be available at higher levels of the query
1096 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1098 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1099 UINT ival = 0, refcol = 0, r;
1103 static const WCHAR szDot[] = { '.', 0 };
1105 if( !view->ops->fetch_int )
1106 return ERROR_INVALID_PARAMETER;
1109 * The column marked with the type stream data seems to have a single number
1110 * which references the column containing the name of the stream data
1112 * Fetch the column to reference first.
1114 r = view->ops->fetch_int( view, row, col, &ival );
1115 if( r != ERROR_SUCCESS )
1118 /* now get the column with the name of the stream */
1119 r = view->ops->fetch_int( view, row, ival, &refcol );
1120 if( r != ERROR_SUCCESS )
1123 /* lookup the string value from the string table */
1124 sval = msi_string_lookup_id( tv->db->strings, refcol );
1126 return ERROR_INVALID_PARAMETER;
1128 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1129 full_name = msi_alloc( len*sizeof(WCHAR) );
1130 lstrcpyW( full_name, tv->name );
1131 lstrcatW( full_name, szDot );
1132 lstrcatW( full_name, sval );
1134 r = db_get_raw_stream( tv->db, full_name, stm );
1136 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1137 msi_free( full_name );
1142 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1144 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1148 return ERROR_INVALID_PARAMETER;
1150 if( (col==0) || (col>tv->num_cols) )
1151 return ERROR_INVALID_PARAMETER;
1153 if( tv->columns[col-1].offset >= tv->row_size )
1155 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1156 ERR("%p %p\n", tv, tv->columns );
1157 return ERROR_FUNCTION_FAILED;
1160 n = bytes_per_column( &tv->columns[col-1] );
1164 offset = tv->columns[col-1].offset/2;
1165 tv->table->data[row][offset] = val & 0xffff;
1166 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1169 offset = tv->columns[col-1].offset/2;
1170 tv->table->data[row][offset] = val;
1173 ERR("oops! what is %d bytes per column?\n", n );
1174 return ERROR_FUNCTION_FAILED;
1176 return ERROR_SUCCESS;
1179 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
1181 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1185 TRACE("%p\n", view);
1188 return ERROR_INVALID_PARAMETER;
1190 row = msi_alloc_zero( tv->row_size );
1192 return ERROR_NOT_ENOUGH_MEMORY;
1194 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1195 if( tv->table->data )
1196 p = msi_realloc( tv->table->data, sz );
1198 p = msi_alloc( sz );
1202 return ERROR_NOT_ENOUGH_MEMORY;
1205 tv->table->data = p;
1206 tv->table->data[tv->table->row_count] = row;
1207 *num = tv->table->row_count;
1208 tv->table->row_count++;
1210 return ERROR_SUCCESS;
1213 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1215 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1217 TRACE("%p %p\n", tv, record);
1219 TRACE("There are %d columns\n", tv->num_cols );
1220 tv->table = get_table( tv->db, tv->name, tv->columns, tv->num_cols );
1222 return ERROR_FUNCTION_FAILED;
1224 return ERROR_SUCCESS;
1227 static UINT TABLE_close( struct tagMSIVIEW *view )
1229 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1231 TRACE("%p\n", view );
1234 return ERROR_FUNCTION_FAILED;
1238 return ERROR_SUCCESS;
1241 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1243 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1245 TRACE("%p %p %p\n", view, rows, cols );
1248 *cols = tv->num_cols;
1252 return ERROR_INVALID_PARAMETER;
1253 *rows = tv->table->row_count;
1256 return ERROR_SUCCESS;
1259 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1260 UINT n, LPWSTR *name, UINT *type )
1262 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1264 TRACE("%p %d %p %p\n", tv, n, name, type );
1266 if( ( n == 0 ) || ( n > tv->num_cols ) )
1267 return ERROR_INVALID_PARAMETER;
1271 *name = strdupW( tv->columns[n-1].colname );
1273 return ERROR_FUNCTION_FAILED;
1276 *type = tv->columns[n-1].type;
1278 return ERROR_SUCCESS;
1281 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1283 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1287 /* check there's no null values where they're not allowed */
1288 for( i = 0; i < tv->num_cols; i++ )
1290 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1293 if ( tv->columns[i].type & MSITYPE_STRING )
1297 str = MSI_RecordGetString( rec, i+1 );
1298 if (str == NULL || str[0] == 0)
1299 return ERROR_INVALID_DATA;
1305 n = MSI_RecordGetInteger( rec, i+1 );
1306 if (n == MSI_NULL_INTEGER)
1307 return ERROR_INVALID_DATA;
1311 /* check there's no duplicate keys */
1312 r = msi_table_find_row( tv, rec, &row );
1313 if (r == ERROR_SUCCESS)
1314 return ERROR_INVALID_DATA;
1316 return ERROR_SUCCESS;
1319 static UINT msi_table_modify_row( MSITABLEVIEW *tv, MSIRECORD *rec,
1320 UINT row, UINT mask )
1322 UINT i, val, r = ERROR_SUCCESS;
1324 TRACE("%p %p %u %08x\n", tv, rec, row, mask );
1326 for( i = 0; i < tv->num_cols; i++ )
1328 /* set keys or values specified in the mask */
1329 if( (~tv->columns[i].type & MSITYPE_KEY) && (~mask & (1<<i)) )
1332 if( (tv->columns[i].type & MSITYPE_STRING) &&
1333 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1335 const WCHAR *str = MSI_RecordGetString( rec, i+1 );
1336 val = msi_addstringW( tv->db->strings, 0, str, -1, 1 );
1340 val = MSI_RecordGetInteger( rec, i+1 );
1341 if ( 2 == bytes_per_column( &tv->columns[i] ) )
1346 r = TABLE_set_int( &tv->view, row, i+1, val );
1354 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1356 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1359 TRACE("%p %p\n", tv, rec );
1361 /* check that the key is unique - can we find a matching row? */
1362 r = table_validate_new( tv, rec );
1363 if( r != ERROR_SUCCESS )
1364 return ERROR_FUNCTION_FAILED;
1366 r = table_create_new_row( view, &row );
1367 TRACE("insert_row returned %08x\n", r);
1368 if( r != ERROR_SUCCESS )
1371 return msi_table_modify_row( tv, rec, row, ~0 );
1374 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1377 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1380 TRACE("%p %d %p\n", view, eModifyMode, rec );
1384 r = TABLE_execute( view, NULL );
1389 switch (eModifyMode)
1391 case MSIMODIFY_VALIDATE_NEW:
1392 r = table_validate_new( tv, rec );
1395 case MSIMODIFY_INSERT_TEMPORARY:
1396 r = table_validate_new( tv, rec );
1397 if (r != ERROR_SUCCESS)
1399 r = TABLE_insert_row( view, rec );
1402 case MSIMODIFY_REFRESH:
1403 case MSIMODIFY_INSERT:
1404 case MSIMODIFY_UPDATE:
1405 case MSIMODIFY_ASSIGN:
1406 case MSIMODIFY_REPLACE:
1407 case MSIMODIFY_MERGE:
1408 case MSIMODIFY_DELETE:
1409 case MSIMODIFY_VALIDATE:
1410 case MSIMODIFY_VALIDATE_FIELD:
1411 case MSIMODIFY_VALIDATE_DELETE:
1412 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1413 r = ERROR_CALL_NOT_IMPLEMENTED;
1417 r = ERROR_INVALID_DATA;
1423 static UINT TABLE_delete( struct tagMSIVIEW *view )
1425 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1427 TRACE("%p\n", view );
1433 msi_free_colinfo( tv->columns, tv->num_cols );
1434 msi_free( tv->columns );
1440 return ERROR_SUCCESS;
1443 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1444 UINT val, UINT *row, MSIITERHANDLE *handle )
1446 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1447 const MSICOLUMNHASHENTRY *entry;
1449 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1452 return ERROR_INVALID_PARAMETER;
1454 if( (col==0) || (col > tv->num_cols) )
1455 return ERROR_INVALID_PARAMETER;
1457 if( !tv->columns[col-1].hash_table )
1460 UINT num_rows = tv->table->row_count;
1461 MSICOLUMNHASHENTRY **hash_table;
1462 MSICOLUMNHASHENTRY *new_entry;
1464 if( tv->columns[col-1].offset >= tv->row_size )
1466 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1467 ERR("%p %p\n", tv, tv->columns );
1468 return ERROR_FUNCTION_FAILED;
1471 /* allocate contiguous memory for the table and its entries so we
1472 * don't have to do an expensive cleanup */
1473 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1474 num_rows * sizeof(MSICOLUMNHASHENTRY));
1476 return ERROR_OUTOFMEMORY;
1478 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1479 tv->columns[col-1].hash_table = hash_table;
1481 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1483 for (i = 0; i < num_rows; i++, new_entry++)
1486 UINT offset = i + (tv->columns[col-1].offset/2) * num_rows;
1487 n = bytes_per_column( &tv->columns[col-1] );
1491 offset = tv->columns[col-1].offset/2;
1492 row_value = tv->table->data[i][offset] +
1493 (tv->table->data[i][offset + 1] << 16);
1496 offset = tv->columns[col-1].offset/2;
1497 row_value = tv->table->data[i][offset];
1500 ERR("oops! what is %d bytes per column?\n", n );
1504 new_entry->next = NULL;
1505 new_entry->value = row_value;
1507 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1509 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1510 while (prev_entry->next)
1511 prev_entry = prev_entry->next;
1512 prev_entry->next = new_entry;
1515 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1520 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1522 entry = ((const MSICOLUMNHASHENTRY *)*handle)->next;
1524 while (entry && entry->value != val)
1525 entry = entry->next;
1527 *handle = (MSIITERHANDLE)entry;
1529 return ERROR_NO_MORE_ITEMS;
1532 return ERROR_SUCCESS;
1536 static const MSIVIEWOPS table_ops =
1544 TABLE_get_dimensions,
1545 TABLE_get_column_info,
1548 TABLE_find_matching_rows
1551 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1554 UINT r, sz, column_count;
1555 MSICOLUMNINFO *columns;
1557 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1559 /* get the number of columns in this table */
1561 r = get_tablecolumns( db, name, NULL, &column_count );
1562 if( r != ERROR_SUCCESS )
1565 /* if there's no columns, there's no table */
1566 if( column_count == 0 )
1567 return ERROR_INVALID_PARAMETER;
1569 TRACE("Table found\n");
1571 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1572 tv = msi_alloc_zero( sz );
1574 return ERROR_FUNCTION_FAILED;
1576 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO));
1580 return ERROR_FUNCTION_FAILED;
1583 r = get_tablecolumns( db, name, columns, &column_count );
1584 if( r != ERROR_SUCCESS )
1586 msi_free( columns );
1588 return ERROR_FUNCTION_FAILED;
1591 TRACE("Table has %d columns\n", column_count);
1593 /* fill the structure */
1594 tv->view.ops = &table_ops;
1596 tv->columns = columns;
1597 tv->num_cols = column_count;
1599 tv->row_size = msi_table_get_row_size( columns, column_count );
1601 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
1603 *view = (MSIVIEW*) tv;
1604 lstrcpyW( tv->name, name );
1606 return ERROR_SUCCESS;
1609 UINT MSI_CommitTables( MSIDATABASE *db )
1612 MSITABLE *table = NULL;
1616 r = save_string_table( db );
1617 if( r != ERROR_SUCCESS )
1619 WARN("failed to save string table r=%08x\n",r);
1623 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1625 r = save_table( db, table );
1626 if( r != ERROR_SUCCESS )
1628 WARN("failed to save table %s (r=%08x)\n",
1629 debugstr_w(table->name), r);
1634 /* force everything to reload next time */
1635 free_cached_tables( db );
1637 return ERROR_SUCCESS;
1640 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
1643 return MSICONDITION_ERROR;
1645 return MSICONDITION_FALSE;
1648 static MSIRECORD *msi_get_transform_record( MSITABLEVIEW *tv, string_table *st, USHORT *rawdata )
1650 UINT i, val, ofs = 0;
1651 USHORT mask = *rawdata++;
1652 MSICOLUMNINFO *columns = tv->columns;
1655 rec = MSI_CreateRecord( tv->num_cols );
1660 for( i=0; i<tv->num_cols; i++ )
1662 UINT n = bytes_per_column( &columns[i] );
1664 if ( (mask&1) && (i>=(mask>>8)) )
1666 /* all keys must be present */
1667 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
1674 if( (columns[i].type & MSITYPE_STRING) &&
1675 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1677 LPCWSTR sval = msi_string_lookup_id( st, val );
1678 MSI_RecordSetStringW( rec, i+1, sval );
1679 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
1684 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
1685 TRACE(" field %d [0x%04x]\n", i+1, val );
1689 val = (rawdata[ofs] + (rawdata[ofs + 1]<<16));
1691 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
1692 TRACE(" field %d [0x%08x]\n", i+1, val );
1695 ERR("oops - unknown column width %d\n", n);
1703 static void dump_record( MSIRECORD *rec )
1707 n = MSI_RecordGetFieldCount( rec );
1708 for( i=1; i<=n; i++ )
1710 LPCWSTR sval = MSI_RecordGetString( rec, i );
1712 if( MSI_RecordIsNull( rec, i ) )
1713 TRACE("row -> []\n");
1714 else if( (sval = MSI_RecordGetString( rec, i )) )
1715 TRACE("row -> [%s]\n", debugstr_w(sval));
1717 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
1721 static void dump_table( string_table *st, USHORT *rawdata, UINT rawsize )
1726 for( i=0; i<(rawsize/2); i++ )
1728 sval = msi_string_lookup_id( st, rawdata[i] );
1729 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
1733 static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
1738 data = msi_alloc( tv->num_cols *sizeof (UINT) );
1739 for( i=0; i<tv->num_cols; i++ )
1743 if ( ~tv->columns[i].type & MSITYPE_KEY )
1746 /* turn the transform column value into a row value */
1747 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
1748 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1750 str = MSI_RecordGetString( rec, i+1 );
1751 r = msi_string2idW( tv->db->strings, str, &data[i] );
1753 /* if there's no matching string in the string table,
1754 these keys can't match any record, so fail now. */
1755 if( ERROR_SUCCESS != r )
1762 data[i] = MSI_RecordGetInteger( rec, i+1 );
1767 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, UINT *data )
1769 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
1771 for( i=0; i<tv->num_cols; i++ )
1773 if ( ~tv->columns[i].type & MSITYPE_KEY )
1776 /* turn the transform column value into a row value */
1777 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
1778 if ( r != ERROR_SUCCESS )
1780 ERR("TABLE_fetch_int shouldn't fail here\n");
1784 /* if this key matches, move to the next column */
1787 ret = ERROR_FUNCTION_FAILED;
1791 ret = ERROR_SUCCESS;
1797 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
1799 UINT i, r = ERROR_FUNCTION_FAILED, *data;
1801 data = msi_record_to_row( tv, rec );
1804 for( i=0; i<tv->table->row_count; i++ )
1806 r = msi_row_matches( tv, i, data );
1807 if( r == ERROR_SUCCESS )
1817 static UINT msi_delete_row( MSITABLEVIEW *tv, UINT row )
1820 for( i=1; i<=tv->num_cols; i++ )
1821 tv->view.ops->set_int( &tv->view, row, i, 0 );
1822 return ERROR_SUCCESS;
1825 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
1826 string_table *st, LPCWSTR name )
1829 USHORT *rawdata = NULL;
1830 MSITABLEVIEW *tv = NULL;
1831 UINT r, n, sz, i, mask;
1832 MSIRECORD *rec = NULL;
1835 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
1837 /* read the transform data */
1838 read_stream_data( stg, name, &rawdata, &rawsize );
1841 TRACE("table %s empty\n", debugstr_w(name) );
1842 return ERROR_INVALID_TABLE;
1845 /* create a table view */
1846 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
1847 if( r != ERROR_SUCCESS )
1850 r = tv->view.ops->execute( &tv->view, NULL );
1851 if( r != ERROR_SUCCESS )
1854 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1855 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
1857 /* interpret the data */
1859 for( n=0; n < (rawsize/2); )
1866 * if the low bit is set, columns are continuous and
1867 * the number of columns is specified in the high byte
1869 sz = 2 + tv->row_size;
1874 * If the low bit is not set, rowdata[n] is a bitmask.
1875 * Excepting for key fields, which are always present,
1876 * each bit indicates that a field is present in the transform record.
1878 * rawdata[n] == 0 is a special case ... only the keys will be present
1879 * and it means that this row should be deleted.
1882 for( i=0; i<tv->num_cols; i++ )
1884 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
1885 sz += bytes_per_column( &tv->columns[i] );
1889 /* check we didn't run of the end of the table */
1890 if ( (n+sz) > rawsize )
1893 dump_table( st, rawdata, rawsize );
1897 rec = msi_get_transform_record( tv, st, &rawdata[n] );
1902 r = msi_table_find_row( tv, rec, &row );
1906 TRACE("insert [%d]: \n", row);
1909 * Native msi seems writes nul into the
1910 * Number (2nd) column of the _Columns table.
1911 * Not sure that it's deliberate...
1913 if (!lstrcmpW(name, szColumns))
1915 if ( MSI_RecordIsNull( rec, 2 ) )
1916 MSI_RecordSetInteger( rec, 2, ++colcol );
1918 ERR("_Columns has non-null data...\n");
1921 TABLE_insert_row( &tv->view, rec );
1923 else if( mask & 0xff )
1925 TRACE("modify [%d]: \n", row);
1926 msi_table_modify_row( tv, rec, row, mask );
1930 TRACE("delete [%d]: \n", row);
1931 msi_delete_row( tv, row );
1933 if( TRACE_ON(msidb) ) dump_record( rec );
1934 msiobj_release( &rec->hdr );
1942 /* no need to free the table, it's associated with the database */
1943 msi_free( rawdata );
1945 tv->view.ops->delete( &tv->view );
1947 return ERROR_SUCCESS;
1951 * msi_table_apply_transform
1953 * Enumerate the table transforms in a transform storage and apply each one.
1955 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
1957 IEnumSTATSTG *stgenum = NULL;
1962 string_table *strings;
1963 UINT ret = ERROR_FUNCTION_FAILED;
1965 TRACE("%p %p\n", db, stg );
1967 strings = load_string_table( stg );
1971 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
1976 * Apply _Tables and _Coluimns transforms first so that
1977 * the table metadata is correct, and empty tables exist.
1979 ret = msi_table_load_transform( db, stg, strings, szTables );
1980 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
1983 ret = msi_table_load_transform( db, stg, strings, szColumns );
1984 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
1987 ret = ERROR_SUCCESS;
1989 while( r == ERROR_SUCCESS )
1992 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
1993 if( FAILED( r ) || !count )
1996 decode_streamname( stat.pwcsName, name );
1997 if ( name[0] != 0x4840 )
2000 TRACE("transform contains stream %s\n", debugstr_w(name));
2002 if ( !lstrcmpW( name+1, szStringPool ) ||
2003 !lstrcmpW( name+1, szStringData ) ||
2004 !lstrcmpW( name+1, szColumns ) ||
2005 !lstrcmpW( name+1, szTables ) )
2008 ret = msi_table_load_transform( db, stg, strings, name+1 );
2011 if ( ret == ERROR_SUCCESS )
2015 t = msi_alloc( sizeof *t );
2017 IStorage_AddRef( stg );
2018 list_add_tail( &db->transforms, &t->entry );
2023 IEnumSTATSTG_Release( stgenum );
2025 msi_destroy_stringtable( strings );
2030 void msi_free_transforms( MSIDATABASE *db )
2032 while( !list_empty( &db->transforms ) )
2034 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2035 MSITRANSFORM, entry );
2036 list_remove( &t->entry );
2037 IStorage_Release( t->stg );