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 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 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");
417 r = IStream_Write(stm, data, sz, &count );
418 if( FAILED( r ) || ( count != sz ) )
420 WARN("Failed to Write\n");
428 IStream_Release( stm );
433 static void free_table( MSITABLE *table )
436 for( i=0; i<table->row_count; i++ )
437 msi_free( table->data[i] );
438 msi_free( table->data );
442 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
444 const MSICOLUMNINFO *last_col = &cols[count-1];
447 return last_col->offset + bytes_per_column( last_col );
450 /* add this table to the list of cached tables in the database */
451 static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
452 const MSICOLUMNINFO *cols, UINT num_cols )
455 USHORT *rawdata = NULL;
456 UINT rawsize = 0, i, j, row_size = 0;
458 TRACE("%s\n",debugstr_w(name));
460 /* nonexistent tables should be interpreted as empty tables */
461 t = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
465 row_size = msi_table_get_row_size( cols, num_cols );
469 lstrcpyW( t->name, name );
471 /* if we can't read the table, just assume that it's empty */
472 read_stream_data( stg, name, &rawdata, &rawsize );
476 TRACE("Read %d bytes\n", rawsize );
478 if( rawsize % row_size )
480 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
484 t->row_count = rawsize / row_size;
485 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
489 /* transpose all the data */
490 TRACE("Transposing data from %d rows\n", t->row_count );
491 for( i=0; i<t->row_count; i++ )
493 t->data[i] = msi_alloc( row_size );
497 for( j=0; j<num_cols; j++ )
499 UINT ofs = cols[j].offset/2;
500 UINT n = bytes_per_column( &cols[j] );
505 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
508 t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
509 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
512 ERR("oops - unknown column width %d\n", n);
526 void free_cached_tables( MSIDATABASE *db )
528 while( !list_empty( &db->tables ) )
530 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
532 list_remove( &t->entry );
537 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
541 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
542 if( !lstrcmpW( name, t->name ) )
548 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
550 UINT r, column_count = 0;
551 MSICOLUMNINFO *columns;
553 /* get the number of columns in this table */
555 r = get_tablecolumns( db, name, NULL, &column_count );
556 if( r != ERROR_SUCCESS )
559 /* if there's no columns, there's no table */
560 if( column_count == 0 )
561 return ERROR_INVALID_PARAMETER;
563 TRACE("Table %s found\n", debugstr_w(name) );
565 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
567 return ERROR_FUNCTION_FAILED;
569 r = get_tablecolumns( db, name, columns, &column_count );
570 if( r != ERROR_SUCCESS )
573 return ERROR_FUNCTION_FAILED;
577 *pcount = column_count;
582 static MSITABLE *get_table( MSIDATABASE *db, LPCWSTR name,
583 const MSICOLUMNINFO *cols, UINT num_cols )
587 /* first, see if the table is cached */
588 table = find_cached_table( db, name );
592 table = read_table_from_storage( db->storage, name, cols, num_cols );
594 list_add_head( &db->tables, &table->entry );
599 static UINT save_table( MSIDATABASE *db, MSITABLE *t )
601 USHORT *rawdata = NULL, *p;
602 UINT rawsize, r, i, j, row_size, num_cols = 0;
603 MSICOLUMNINFO *cols = NULL;
605 TRACE("Saving %s\n", debugstr_w( t->name ) );
607 r = table_get_column_info( db, t->name, &cols, &num_cols );
608 if( r != ERROR_SUCCESS )
611 row_size = msi_table_get_row_size( cols, num_cols );
613 rawsize = t->row_count * row_size;
614 rawdata = msi_alloc_zero( rawsize );
617 r = ERROR_NOT_ENOUGH_MEMORY;
622 for( i=0; i<num_cols; i++ )
624 for( j=0; j<t->row_count; j++ )
626 UINT offset = cols[i].offset;
628 *p++ = t->data[j][offset/2];
629 if( 4 == bytes_per_column( &cols[i] ) )
630 *p++ = t->data[j][offset/2+1];
634 TRACE("writing %d bytes\n", rawsize);
635 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
638 msi_free_colinfo( cols, num_cols );
645 /* information for default tables */
646 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
647 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
648 static const WCHAR szName[] = { 'N','a','m','e',0 };
649 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
650 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
651 static const WCHAR szType[] = { 'T','y','p','e',0 };
653 static const MSICOLUMNINFO _Columns_cols[4] = {
654 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
655 { szColumns, 2, szNumber, MSITYPE_VALID | 2, 2 },
656 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
657 { szColumns, 4, szType, MSITYPE_VALID | 2, 6 },
659 static const MSICOLUMNINFO _Tables_cols[1] = {
660 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
663 static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count )
667 for( i=0; colinfo && (i<count); i++ )
669 assert( (i+1) == colinfo[ i ].number );
671 colinfo[i].offset = colinfo[ i - 1 ].offset
672 + bytes_per_column( &colinfo[ i - 1 ] );
674 colinfo[i].offset = 0;
675 TRACE("column %d is [%s] with type %08x ofs %d\n",
676 colinfo[i].number, debugstr_w(colinfo[i].colname),
677 colinfo[i].type, colinfo[i].offset);
681 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
683 const MSICOLUMNINFO *p;
686 TRACE("%s\n", debugstr_w(name));
688 if (!lstrcmpW( name, szTables ))
693 else if (!lstrcmpW( name, szColumns ))
699 return ERROR_FUNCTION_FAILED;
701 /* duplicate the string data so we can free it in msi_free_colinfo */
704 if (colinfo && (i < *sz) )
706 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
707 colinfo[i].tablename = strdupW( p[i].tablename );
708 colinfo[i].colname = strdupW( p[i].colname );
710 if( colinfo && (i >= *sz) )
713 table_calc_column_offsets( colinfo, n );
715 return ERROR_SUCCESS;
718 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
722 for( i=0; i<count; i++ )
724 msi_free( (LPWSTR) colinfo[i].tablename );
725 msi_free( (LPWSTR) colinfo[i].colname );
726 msi_free( colinfo[i].hash_table );
730 static LPWSTR msi_makestring( MSIDATABASE *db, UINT stringid)
732 return strdupW(msi_string_lookup_id( db->strings, stringid ));
735 static UINT get_tablecolumns( MSIDATABASE *db,
736 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
738 UINT r, i, n=0, table_id, count, maxcount = *sz;
739 MSITABLE *table = NULL;
741 TRACE("%s\n", debugstr_w(szTableName));
743 /* first check if there is a default table with that name */
744 r = get_defaulttablecolumns( szTableName, colinfo, sz );
745 if( ( r == ERROR_SUCCESS ) && *sz )
748 table = get_table( db, szColumns, _Columns_cols, 4 );
751 ERR("couldn't load _Columns table\n");
752 return ERROR_FUNCTION_FAILED;
755 /* convert table and column names to IDs from the string table */
756 r = msi_string2idW( db->strings, szTableName, &table_id );
757 if( r != ERROR_SUCCESS )
759 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
763 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
765 /* if maxcount is non-zero, assume it's exactly right for this table */
766 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
767 count = table->row_count;
768 for( i=0; i<count; i++ )
770 if( table->data[ i ][ 0 ] != table_id )
774 UINT id = table->data[ i ] [ 2 ];
775 UINT col = table->data[ i ][ 1 ] - (1<<15);
777 /* check the column number is in range */
778 if (col<1 || col>maxcount)
780 ERR("column %d out of range\n", col);
784 /* check if this column was already set */
785 if (colinfo[ col - 1 ].number)
787 ERR("duplicate column %d\n", col);
791 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
792 colinfo[ col - 1 ].number = col;
793 colinfo[ col - 1 ].colname = msi_makestring( db, id );
794 colinfo[ col - 1 ].type = table->data[ i ] [ 3 ] - (1<<15);
795 colinfo[ col - 1 ].offset = 0;
796 colinfo[ col - 1 ].hash_table = NULL;
801 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
803 if (colinfo && n != maxcount)
805 ERR("missing column in table %s\n", debugstr_w(szTableName));
806 msi_free_colinfo(colinfo, maxcount );
807 return ERROR_FUNCTION_FAILED;
810 table_calc_column_offsets( colinfo, n );
813 return ERROR_SUCCESS;
816 /* try to find the table name in the _Tables table */
817 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
819 UINT r, table_id = 0, i, count;
820 MSITABLE *table = NULL;
822 if( !lstrcmpW( name, szTables ) )
824 if( !lstrcmpW( name, szColumns ) )
827 r = msi_string2idW( db->strings, name, &table_id );
828 if( r != ERROR_SUCCESS )
830 TRACE("Couldn't find id for %s\n", debugstr_w(name));
834 table = get_table( db, szTables, _Tables_cols, 1 );
837 TRACE("table %s not available\n", debugstr_w(szTables));
841 /* count = table->size/2; */
842 count = table->row_count;
843 for( i=0; i<count; i++ )
844 if( table->data[ i ][ 0 ] == table_id )
850 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
855 /* below is the query interface to a table */
857 typedef struct tagMSITABLEVIEW
862 MSICOLUMNINFO *columns;
868 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
870 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
871 UINT offset, num_rows, n;
874 return ERROR_INVALID_PARAMETER;
876 if( (col==0) || (col>tv->num_cols) )
877 return ERROR_INVALID_PARAMETER;
879 /* how many rows are there ? */
880 num_rows = tv->table->row_count;
881 if( row >= num_rows )
882 return ERROR_NO_MORE_ITEMS;
884 if( tv->columns[col-1].offset >= tv->row_size )
886 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
887 ERR("%p %p\n", tv, tv->columns );
888 return ERROR_FUNCTION_FAILED;
891 offset = row + (tv->columns[col-1].offset/2) * num_rows;
892 n = bytes_per_column( &tv->columns[col-1] );
896 offset = tv->columns[col-1].offset/2;
897 *val = tv->table->data[row][offset] +
898 (tv->table->data[row][offset + 1] << 16);
901 offset = tv->columns[col-1].offset/2;
902 *val = tv->table->data[row][offset];
905 ERR("oops! what is %d bytes per column?\n", n );
906 return ERROR_FUNCTION_FAILED;
909 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
911 return ERROR_SUCCESS;
915 * We need a special case for streams, as we need to reference column with
916 * the name of the stream in the same table, and the table name
917 * which may not be available at higher levels of the query
919 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
921 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
922 UINT ival = 0, refcol = 0, r;
926 static const WCHAR szDot[] = { '.', 0 };
929 if( !view->ops->fetch_int )
930 return ERROR_INVALID_PARAMETER;
933 * The column marked with the type stream data seems to have a single number
934 * which references the column containing the name of the stream data
936 * Fetch the column to reference first.
938 r = view->ops->fetch_int( view, row, col, &ival );
939 if( r != ERROR_SUCCESS )
942 /* check the column value is in range */
943 if (ival < 0 || ival > tv->num_cols || ival == col)
945 ERR("bad column ref (%u) for stream\n", ival);
946 return ERROR_FUNCTION_FAILED;
949 if ( tv->columns[ival - 1].type & MSITYPE_STRING )
951 /* now get the column with the name of the stream */
952 r = view->ops->fetch_int( view, row, ival, &refcol );
953 if ( r != ERROR_SUCCESS )
956 /* lookup the string value from the string table */
957 sval = msi_string_lookup_id( tv->db->strings, refcol );
959 return ERROR_INVALID_PARAMETER;
963 static const WCHAR fmt[] = { '%','d',0 };
964 sprintfW( number, fmt, ival );
968 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
969 full_name = msi_alloc( len*sizeof(WCHAR) );
970 lstrcpyW( full_name, tv->name );
971 lstrcatW( full_name, szDot );
972 lstrcatW( full_name, sval );
974 r = db_get_raw_stream( tv->db, full_name, stm );
976 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
977 msi_free( full_name );
982 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
987 return ERROR_INVALID_PARAMETER;
989 if( (col==0) || (col>tv->num_cols) )
990 return ERROR_INVALID_PARAMETER;
992 if( tv->columns[col-1].offset >= tv->row_size )
994 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
995 ERR("%p %p\n", tv, tv->columns );
996 return ERROR_FUNCTION_FAILED;
999 msi_free( tv->columns[col-1].hash_table );
1000 tv->columns[col-1].hash_table = NULL;
1002 n = bytes_per_column( &tv->columns[col-1] );
1006 offset = tv->columns[col-1].offset/2;
1007 tv->table->data[row][offset] = val & 0xffff;
1008 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1011 offset = tv->columns[col-1].offset/2;
1012 tv->table->data[row][offset] = val;
1015 ERR("oops! what is %d bytes per column?\n", n );
1016 return ERROR_FUNCTION_FAILED;
1018 return ERROR_SUCCESS;
1021 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1023 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1024 UINT i, val, r = ERROR_SUCCESS;
1027 return ERROR_INVALID_PARAMETER;
1029 /* test if any of the mask bits are invalid */
1030 if ( mask >= (1<<tv->num_cols) )
1031 return ERROR_INVALID_PARAMETER;
1033 for ( i = 0; i < tv->num_cols; i++ )
1035 /* only update the fields specified in the mask */
1036 if ( !(mask&(1<<i)) )
1039 /* FIXME: should we allow updating keys? */
1042 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1044 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1046 val = 1; /* refers to the first key column */
1048 else if ( tv->columns[i].type & MSITYPE_STRING )
1050 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1051 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1, StringPersistent );
1053 else if ( 2 == bytes_per_column( &tv->columns[ i ] ) )
1055 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1056 if ( val & 0xffff0000 )
1058 ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1059 return ERROR_FUNCTION_FAILED;
1064 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1065 val = ival ^ 0x80000000;
1069 r = TABLE_set_int( tv, row, i+1, val );
1070 if ( r != ERROR_SUCCESS )
1076 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
1078 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1082 TRACE("%p\n", view);
1085 return ERROR_INVALID_PARAMETER;
1087 row = msi_alloc_zero( tv->row_size );
1089 return ERROR_NOT_ENOUGH_MEMORY;
1091 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1092 if( tv->table->data )
1093 p = msi_realloc( tv->table->data, sz );
1095 p = msi_alloc( sz );
1099 return ERROR_NOT_ENOUGH_MEMORY;
1102 tv->table->data = p;
1103 tv->table->data[tv->table->row_count] = row;
1104 *num = tv->table->row_count;
1105 tv->table->row_count++;
1107 return ERROR_SUCCESS;
1110 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1112 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1114 TRACE("%p %p\n", tv, record);
1116 TRACE("There are %d columns\n", tv->num_cols );
1117 tv->table = get_table( tv->db, tv->name, tv->columns, tv->num_cols );
1119 return ERROR_FUNCTION_FAILED;
1121 return ERROR_SUCCESS;
1124 static UINT TABLE_close( struct tagMSIVIEW *view )
1126 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1128 TRACE("%p\n", view );
1131 return ERROR_FUNCTION_FAILED;
1135 return ERROR_SUCCESS;
1138 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1140 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1142 TRACE("%p %p %p\n", view, rows, cols );
1145 *cols = tv->num_cols;
1149 return ERROR_INVALID_PARAMETER;
1150 *rows = tv->table->row_count;
1153 return ERROR_SUCCESS;
1156 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1157 UINT n, LPWSTR *name, UINT *type )
1159 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1161 TRACE("%p %d %p %p\n", tv, n, name, type );
1163 if( ( n == 0 ) || ( n > tv->num_cols ) )
1164 return ERROR_INVALID_PARAMETER;
1168 *name = strdupW( tv->columns[n-1].colname );
1170 return ERROR_FUNCTION_FAILED;
1173 *type = tv->columns[n-1].type;
1175 return ERROR_SUCCESS;
1178 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1180 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1184 /* check there's no null values where they're not allowed */
1185 for( i = 0; i < tv->num_cols; i++ )
1187 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1190 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1191 TRACE("skipping binary column\n");
1192 else if ( tv->columns[i].type & MSITYPE_STRING )
1196 str = MSI_RecordGetString( rec, i+1 );
1197 if (str == NULL || str[0] == 0)
1198 return ERROR_INVALID_DATA;
1204 n = MSI_RecordGetInteger( rec, i+1 );
1205 if (n == MSI_NULL_INTEGER)
1206 return ERROR_INVALID_DATA;
1210 /* check there's no duplicate keys */
1211 r = msi_table_find_row( tv, rec, &row );
1212 if (r == ERROR_SUCCESS)
1213 return ERROR_INVALID_DATA;
1215 return ERROR_SUCCESS;
1218 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1220 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1223 TRACE("%p %p\n", tv, rec );
1225 /* check that the key is unique - can we find a matching row? */
1226 r = table_validate_new( tv, rec );
1227 if( r != ERROR_SUCCESS )
1228 return ERROR_FUNCTION_FAILED;
1230 r = table_create_new_row( view, &row );
1231 TRACE("insert_row returned %08x\n", r);
1232 if( r != ERROR_SUCCESS )
1235 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1238 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1241 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1244 TRACE("%p %d %p\n", view, eModifyMode, rec );
1248 r = TABLE_execute( view, NULL );
1253 switch (eModifyMode)
1255 case MSIMODIFY_VALIDATE_NEW:
1256 r = table_validate_new( tv, rec );
1259 case MSIMODIFY_INSERT_TEMPORARY:
1260 r = table_validate_new( tv, rec );
1261 if (r != ERROR_SUCCESS)
1263 r = TABLE_insert_row( view, rec );
1266 case MSIMODIFY_REFRESH:
1267 case MSIMODIFY_INSERT:
1268 case MSIMODIFY_UPDATE:
1269 case MSIMODIFY_ASSIGN:
1270 case MSIMODIFY_REPLACE:
1271 case MSIMODIFY_MERGE:
1272 case MSIMODIFY_DELETE:
1273 case MSIMODIFY_VALIDATE:
1274 case MSIMODIFY_VALIDATE_FIELD:
1275 case MSIMODIFY_VALIDATE_DELETE:
1276 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1277 r = ERROR_CALL_NOT_IMPLEMENTED;
1281 r = ERROR_INVALID_DATA;
1287 static UINT TABLE_delete( struct tagMSIVIEW *view )
1289 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1291 TRACE("%p\n", view );
1297 msi_free_colinfo( tv->columns, tv->num_cols );
1298 msi_free( tv->columns );
1304 return ERROR_SUCCESS;
1307 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1308 UINT val, UINT *row, MSIITERHANDLE *handle )
1310 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1311 const MSICOLUMNHASHENTRY *entry;
1313 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1316 return ERROR_INVALID_PARAMETER;
1318 if( (col==0) || (col > tv->num_cols) )
1319 return ERROR_INVALID_PARAMETER;
1321 if( !tv->columns[col-1].hash_table )
1324 UINT num_rows = tv->table->row_count;
1325 MSICOLUMNHASHENTRY **hash_table;
1326 MSICOLUMNHASHENTRY *new_entry;
1328 if( tv->columns[col-1].offset >= tv->row_size )
1330 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1331 ERR("%p %p\n", tv, tv->columns );
1332 return ERROR_FUNCTION_FAILED;
1335 /* allocate contiguous memory for the table and its entries so we
1336 * don't have to do an expensive cleanup */
1337 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1338 num_rows * sizeof(MSICOLUMNHASHENTRY));
1340 return ERROR_OUTOFMEMORY;
1342 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1343 tv->columns[col-1].hash_table = hash_table;
1345 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1347 for (i = 0; i < num_rows; i++, new_entry++)
1350 UINT offset = i + (tv->columns[col-1].offset/2) * num_rows;
1351 n = bytes_per_column( &tv->columns[col-1] );
1355 offset = tv->columns[col-1].offset/2;
1356 row_value = tv->table->data[i][offset] +
1357 (tv->table->data[i][offset + 1] << 16);
1360 offset = tv->columns[col-1].offset/2;
1361 row_value = tv->table->data[i][offset];
1364 ERR("oops! what is %d bytes per column?\n", n );
1368 new_entry->next = NULL;
1369 new_entry->value = row_value;
1371 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1373 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1374 while (prev_entry->next)
1375 prev_entry = prev_entry->next;
1376 prev_entry->next = new_entry;
1379 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1384 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1386 entry = (*handle)->next;
1388 while (entry && entry->value != val)
1389 entry = entry->next;
1393 return ERROR_NO_MORE_ITEMS;
1396 return ERROR_SUCCESS;
1400 static const MSIVIEWOPS table_ops =
1408 TABLE_get_dimensions,
1409 TABLE_get_column_info,
1412 TABLE_find_matching_rows
1415 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1418 UINT r, sz, column_count;
1419 MSICOLUMNINFO *columns;
1421 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1423 /* get the number of columns in this table */
1425 r = get_tablecolumns( db, name, NULL, &column_count );
1426 if( r != ERROR_SUCCESS )
1429 /* if there's no columns, there's no table */
1430 if( column_count == 0 )
1431 return ERROR_INVALID_PARAMETER;
1433 TRACE("Table found\n");
1435 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1436 tv = msi_alloc_zero( sz );
1438 return ERROR_FUNCTION_FAILED;
1440 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO));
1444 return ERROR_FUNCTION_FAILED;
1447 r = get_tablecolumns( db, name, columns, &column_count );
1448 if( r != ERROR_SUCCESS )
1450 msi_free( columns );
1452 return ERROR_FUNCTION_FAILED;
1455 TRACE("Table has %d columns\n", column_count);
1457 /* fill the structure */
1458 tv->view.ops = &table_ops;
1460 tv->columns = columns;
1461 tv->num_cols = column_count;
1463 tv->row_size = msi_table_get_row_size( columns, column_count );
1465 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
1467 *view = (MSIVIEW*) tv;
1468 lstrcpyW( tv->name, name );
1470 return ERROR_SUCCESS;
1473 UINT MSI_CommitTables( MSIDATABASE *db )
1476 MSITABLE *table = NULL;
1480 r = msi_save_string_table( db->strings, db->storage );
1481 if( r != ERROR_SUCCESS )
1483 WARN("failed to save string table r=%08x\n",r);
1487 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1489 r = save_table( db, table );
1490 if( r != ERROR_SUCCESS )
1492 WARN("failed to save table %s (r=%08x)\n",
1493 debugstr_w(table->name), r);
1498 /* force everything to reload next time */
1499 free_cached_tables( db );
1501 return ERROR_SUCCESS;
1504 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
1507 return MSICONDITION_ERROR;
1509 if (!TABLE_Exists( db, table ))
1510 return MSICONDITION_NONE;
1512 return MSICONDITION_FALSE;
1515 static MSIRECORD *msi_get_transform_record( MSITABLEVIEW *tv, string_table *st, USHORT *rawdata )
1517 UINT i, val, ofs = 0;
1518 USHORT mask = *rawdata++;
1519 MSICOLUMNINFO *columns = tv->columns;
1522 rec = MSI_CreateRecord( tv->num_cols );
1527 for( i=0; i<tv->num_cols; i++ )
1529 UINT n = bytes_per_column( &columns[i] );
1531 if ( (mask&1) && (i>=(mask>>8)) )
1533 /* all keys must be present */
1534 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
1541 if( (columns[i].type & MSITYPE_STRING) &&
1542 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1544 LPCWSTR sval = msi_string_lookup_id( st, val );
1545 MSI_RecordSetStringW( rec, i+1, sval );
1546 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
1551 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
1552 TRACE(" field %d [0x%04x]\n", i+1, val );
1556 val = (rawdata[ofs] + (rawdata[ofs + 1]<<16));
1558 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
1559 TRACE(" field %d [0x%08x]\n", i+1, val );
1562 ERR("oops - unknown column width %d\n", n);
1570 static void dump_record( MSIRECORD *rec )
1574 n = MSI_RecordGetFieldCount( rec );
1575 for( i=1; i<=n; i++ )
1577 LPCWSTR sval = MSI_RecordGetString( rec, i );
1579 if( MSI_RecordIsNull( rec, i ) )
1580 TRACE("row -> []\n");
1581 else if( (sval = MSI_RecordGetString( rec, i )) )
1582 TRACE("row -> [%s]\n", debugstr_w(sval));
1584 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
1588 static void dump_table( string_table *st, USHORT *rawdata, UINT rawsize )
1593 for( i=0; i<(rawsize/2); i++ )
1595 sval = msi_string_lookup_id( st, rawdata[i] );
1596 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
1600 static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
1605 data = msi_alloc( tv->num_cols *sizeof (UINT) );
1606 for( i=0; i<tv->num_cols; i++ )
1610 if ( ~tv->columns[i].type & MSITYPE_KEY )
1613 /* turn the transform column value into a row value */
1614 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
1615 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1617 str = MSI_RecordGetString( rec, i+1 );
1618 r = msi_string2idW( tv->db->strings, str, &data[i] );
1620 /* if there's no matching string in the string table,
1621 these keys can't match any record, so fail now. */
1622 if( ERROR_SUCCESS != r )
1630 data[i] = MSI_RecordGetInteger( rec, i+1 );
1631 if ((tv->columns[i].type&0xff) == 2)
1634 data[i] += 0x80000000;
1640 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, UINT *data )
1642 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
1644 for( i=0; i<tv->num_cols; i++ )
1646 if ( ~tv->columns[i].type & MSITYPE_KEY )
1649 /* turn the transform column value into a row value */
1650 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
1651 if ( r != ERROR_SUCCESS )
1653 ERR("TABLE_fetch_int shouldn't fail here\n");
1657 /* if this key matches, move to the next column */
1660 ret = ERROR_FUNCTION_FAILED;
1664 ret = ERROR_SUCCESS;
1670 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
1672 UINT i, r = ERROR_FUNCTION_FAILED, *data;
1674 data = msi_record_to_row( tv, rec );
1677 for( i=0; i<tv->table->row_count; i++ )
1679 r = msi_row_matches( tv, i, data );
1680 if( r == ERROR_SUCCESS )
1690 static UINT msi_delete_row( MSITABLEVIEW *tv, UINT row )
1694 for( i=1; i<=tv->num_cols; i++ )
1695 TABLE_set_int( tv, row, i, 0 );
1696 return ERROR_SUCCESS;
1699 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
1700 string_table *st, LPCWSTR name )
1703 USHORT *rawdata = NULL;
1704 MSITABLEVIEW *tv = NULL;
1705 UINT r, n, sz, i, mask;
1706 MSIRECORD *rec = NULL;
1711 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
1713 /* read the transform data */
1714 read_stream_data( stg, name, &rawdata, &rawsize );
1717 TRACE("table %s empty\n", debugstr_w(name) );
1718 return ERROR_INVALID_TABLE;
1721 /* create a table view */
1722 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
1723 if( r != ERROR_SUCCESS )
1726 r = tv->view.ops->execute( &tv->view, NULL );
1727 if( r != ERROR_SUCCESS )
1730 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1731 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
1733 /* interpret the data */
1735 for( n=0; n < (rawsize/2); )
1742 * if the low bit is set, columns are continuous and
1743 * the number of columns is specified in the high byte
1745 sz = 2 + tv->row_size;
1750 * If the low bit is not set, rowdata[n] is a bitmask.
1751 * Excepting for key fields, which are always present,
1752 * each bit indicates that a field is present in the transform record.
1754 * rawdata[n] == 0 is a special case ... only the keys will be present
1755 * and it means that this row should be deleted.
1758 for( i=0; i<tv->num_cols; i++ )
1760 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
1761 sz += bytes_per_column( &tv->columns[i] );
1765 /* check we didn't run of the end of the table */
1766 if ( (n+sz) > rawsize )
1769 dump_table( st, rawdata, rawsize );
1773 rec = msi_get_transform_record( tv, st, &rawdata[n] );
1778 TRACE("inserting record\n");
1781 * Native msi seems writes nul into the
1782 * Number (2nd) column of the _Columns table.
1783 * Not sure that it's deliberate...
1785 if (!lstrcmpW(name, szColumns))
1790 MSI_RecordGetStringW( rec, 1, table, &sz );
1792 /* reset the column number on a new table */
1793 if ( lstrcmpW(coltable, table) )
1796 lstrcpyW( coltable, table );
1799 /* fix nul column numbers */
1800 MSI_RecordSetInteger( rec, 2, ++colcol );
1803 r = TABLE_insert_row( &tv->view, rec );
1804 if (r != ERROR_SUCCESS)
1805 ERR("insert row failed\n");
1811 r = msi_table_find_row( tv, rec, &row );
1812 if (r != ERROR_SUCCESS)
1813 ERR("no matching row to transform\n");
1816 TRACE("modifying row [%d]:\n", row);
1817 TABLE_set_row( &tv->view, row, rec, mask );
1821 TRACE("deleting row [%d]:\n", row);
1822 msi_delete_row( tv, row );
1825 if( TRACE_ON(msidb) ) dump_record( rec );
1826 msiobj_release( &rec->hdr );
1833 /* no need to free the table, it's associated with the database */
1834 msi_free( rawdata );
1836 tv->view.ops->delete( &tv->view );
1838 return ERROR_SUCCESS;
1842 * msi_table_apply_transform
1844 * Enumerate the table transforms in a transform storage and apply each one.
1846 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
1848 IEnumSTATSTG *stgenum = NULL;
1853 string_table *strings;
1854 UINT ret = ERROR_FUNCTION_FAILED;
1856 TRACE("%p %p\n", db, stg );
1858 strings = msi_load_string_table( stg );
1862 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
1867 * Apply _Tables and _Columns transforms first so that
1868 * the table metadata is correct, and empty tables exist.
1870 ret = msi_table_load_transform( db, stg, strings, szTables );
1871 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
1874 ret = msi_table_load_transform( db, stg, strings, szColumns );
1875 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
1878 ret = ERROR_SUCCESS;
1880 while( r == ERROR_SUCCESS )
1883 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
1884 if( FAILED( r ) || !count )
1887 decode_streamname( stat.pwcsName, name );
1888 if ( name[0] != 0x4840 )
1891 TRACE("transform contains stream %s\n", debugstr_w(name));
1893 if ( !lstrcmpW( name+1, szStringPool ) ||
1894 !lstrcmpW( name+1, szStringData ) ||
1895 !lstrcmpW( name+1, szColumns ) ||
1896 !lstrcmpW( name+1, szTables ) )
1899 ret = msi_table_load_transform( db, stg, strings, name+1 );
1902 if ( ret == ERROR_SUCCESS )
1903 append_storage_to_db( db, stg );
1907 IEnumSTATSTG_Release( stgenum );
1909 msi_destroy_stringtable( strings );
1914 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
1918 t = msi_alloc( sizeof *t );
1920 IStorage_AddRef( stg );
1921 list_add_tail( &db->transforms, &t->entry );
1924 void msi_free_transforms( MSIDATABASE *db )
1926 while( !list_empty( &db->transforms ) )
1928 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
1929 MSITRANSFORM, entry );
1930 list_remove( &t->entry );
1931 IStorage_Release( t->stg );