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
45 #define LONG_STR_BYTES 3
47 typedef struct tagMSICOLUMNHASHENTRY
49 struct tagMSICOLUMNHASHENTRY *next;
54 typedef struct tagMSICOLUMNINFO
63 MSICOLUMNHASHENTRY **hash_table;
66 typedef struct tagMSIORDERINFO
77 BYTE **nonpersistent_data;
78 UINT nonpersistent_row_count;
80 MSICOLUMNINFO *colinfo;
82 MSICONDITION persistent;
87 typedef struct tagMSITRANSFORM {
92 static const WCHAR szStringData[] = {
93 '_','S','t','r','i','n','g','D','a','t','a',0 };
94 static const WCHAR szStringPool[] = {
95 '_','S','t','r','i','n','g','P','o','o','l',0 };
97 /* information for default tables */
98 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
99 static WCHAR szTable[] = { 'T','a','b','l','e',0 };
100 static WCHAR szName[] = { 'N','a','m','e',0 };
101 static WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
102 static WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
103 static WCHAR szType[] = { 'T','y','p','e',0 };
105 static const MSICOLUMNINFO _Columns_cols[4] = {
106 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
107 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2, 0, 0, NULL },
108 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
109 { szColumns, 4, szType, MSITYPE_VALID | 2, 6, 0, 0, NULL },
112 static const MSICOLUMNINFO _Tables_cols[1] = {
113 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
116 #define MAX_STREAM_NAME 0x1f
118 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
119 MSICOLUMNINFO **pcols, UINT *pcount );
120 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
122 static UINT get_tablecolumns( MSIDATABASE *db,
123 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
124 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
126 static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col )
128 if( MSITYPE_IS_BINARY(col->type) )
130 if( col->type & MSITYPE_STRING )
131 return db->bytes_per_strref;
132 if( (col->type & 0xff) > 4 )
133 ERR("Invalid column size!\n");
134 return col->type & 0xff;
137 static int utf2mime(int x)
139 if( (x>='0') && (x<='9') )
141 if( (x>='A') && (x<='Z') )
143 if( (x>='a') && (x<='z') )
152 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
154 DWORD count = MAX_STREAM_NAME;
159 count = lstrlenW( in )+2;
160 out = msi_alloc( count*sizeof(WCHAR) );
176 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
178 ch = utf2mime(ch) + 0x4800;
180 if( next && (next<0x80) )
182 next = utf2mime(next);
193 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
198 static int mime2utf(int x)
205 return x - 10 - 26 + 'a';
206 if( x == (10+26+26) )
211 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
216 while ( (ch = *in++) )
218 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
221 ch = mime2utf(ch-0x4800);
225 *out++ = mime2utf(ch&0x3f);
227 ch = mime2utf((ch>>6)&0x3f);
237 void enum_stream_names( IStorage *stg )
239 IEnumSTATSTG *stgenum = NULL;
245 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
253 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
254 if( FAILED( r ) || !count )
256 decode_streamname( stat.pwcsName, name );
257 TRACE("stream %2d -> %s %s\n", n,
258 debugstr_w(stat.pwcsName), debugstr_w(name) );
259 CoTaskMemFree( stat.pwcsName );
263 IEnumSTATSTG_Release( stgenum );
266 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
267 BYTE **pdata, UINT *psz )
270 UINT ret = ERROR_FUNCTION_FAILED;
277 encname = encode_streamname(table, stname);
279 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
281 r = IStorage_OpenStream(stg, encname, NULL,
282 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
286 WARN("open stream failed r = %08x - empty table?\n", r);
290 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
293 WARN("open stream failed r = %08x!\n", r);
297 if( stat.cbSize.QuadPart >> 32 )
303 sz = stat.cbSize.QuadPart;
304 data = msi_alloc( sz );
307 WARN("couldn't allocate memory r=%08x!\n", r);
308 ret = ERROR_NOT_ENOUGH_MEMORY;
312 r = IStream_Read(stm, data, sz, &count );
313 if( FAILED( r ) || ( count != sz ) )
316 WARN("read stream failed r = %08x!\n", r);
325 IStream_Release( stm );
330 static UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
335 encname = encode_streamname(FALSE, stname);
337 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
339 r = IStorage_OpenStream(db->storage, encname, NULL,
340 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
343 MSITRANSFORM *transform;
345 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
347 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
348 r = IStorage_OpenStream( transform->stg, encname, NULL,
349 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
357 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
360 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
361 USHORT **pdata, UINT *psz )
364 UINT ret = ERROR_FUNCTION_FAILED;
370 r = db_get_raw_stream( db, stname, &stm );
371 if( r != ERROR_SUCCESS)
373 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
376 WARN("open stream failed r = %08x!\n", r);
380 if( stat.cbSize.QuadPart >> 32 )
386 sz = stat.cbSize.QuadPart;
387 data = msi_alloc( sz );
390 WARN("couldn't allocate memory r=%08x!\n", r);
391 ret = ERROR_NOT_ENOUGH_MEMORY;
395 r = IStream_Read(stm, data, sz, &count );
396 if( FAILED( r ) || ( count != sz ) )
399 WARN("read stream failed r = %08x!\n", r);
408 IStream_Release( stm );
413 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
414 LPCVOID data, UINT sz, BOOL bTable )
417 UINT ret = ERROR_FUNCTION_FAILED;
424 encname = encode_streamname(bTable, stname );
425 r = IStorage_OpenStream( stg, encname, NULL,
426 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
429 r = IStorage_CreateStream( stg, encname,
430 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
435 WARN("open stream failed r = %08x\n", r);
440 r = IStream_SetSize( stm, size );
443 WARN("Failed to SetSize\n");
448 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
451 WARN("Failed to Seek\n");
457 r = IStream_Write(stm, data, sz, &count );
458 if( FAILED( r ) || ( count != sz ) )
460 WARN("Failed to Write\n");
468 IStream_Release( stm );
473 static void free_table( MSITABLE *table )
476 for( i=0; i<table->row_count; i++ )
477 msi_free( table->data[i] );
478 msi_free( table->data );
479 for( i=0; i<table->nonpersistent_row_count; i++ )
480 msi_free( table->nonpersistent_data[i] );
481 msi_free( table->nonpersistent_data );
482 msi_free_colinfo( table->colinfo, table->col_count );
483 msi_free( table->colinfo );
487 static UINT msi_table_get_row_size( MSIDATABASE *db,const MSICOLUMNINFO *cols,
490 const MSICOLUMNINFO *last_col = &cols[count-1];
493 return last_col->offset + bytes_per_column( db, last_col );
496 /* add this table to the list of cached tables in the database */
497 static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
499 BYTE *rawdata = NULL;
500 UINT rawsize = 0, i, j, row_size = 0;
502 TRACE("%s\n",debugstr_w(t->name));
504 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
506 /* if we can't read the table, just assume that it's empty */
507 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
509 return ERROR_SUCCESS;
511 TRACE("Read %d bytes\n", rawsize );
513 if( rawsize % row_size )
515 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
519 t->row_count = rawsize / row_size;
520 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
524 /* transpose all the data */
525 TRACE("Transposing data from %d rows\n", t->row_count );
526 for( i=0; i<t->row_count; i++ )
528 t->data[i] = msi_alloc( row_size );
532 for( j=0; j<t->col_count; j++ )
534 UINT ofs = t->colinfo[j].offset;
535 UINT n = bytes_per_column( db, &t->colinfo[j] );
538 if ( n != 2 && n != 3 && n != 4 )
540 ERR("oops - unknown column width %d\n", n);
544 for ( k = 0; k < n; k++ )
545 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
550 return ERROR_SUCCESS;
553 return ERROR_FUNCTION_FAILED;
556 void free_cached_tables( MSIDATABASE *db )
558 while( !list_empty( &db->tables ) )
560 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
562 list_remove( &t->entry );
567 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
571 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
572 if( !lstrcmpW( name, t->name ) )
578 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
580 UINT r, column_count = 0;
581 MSICOLUMNINFO *columns;
583 /* get the number of columns in this table */
585 r = get_tablecolumns( db, name, NULL, &column_count );
586 if( r != ERROR_SUCCESS )
589 *pcount = column_count;
591 /* if there's no columns, there's no table */
592 if( column_count == 0 )
593 return ERROR_INVALID_PARAMETER;
595 TRACE("Table %s found\n", debugstr_w(name) );
597 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
599 return ERROR_FUNCTION_FAILED;
601 r = get_tablecolumns( db, name, columns, &column_count );
602 if( r != ERROR_SUCCESS )
605 return ERROR_FUNCTION_FAILED;
613 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
614 MSICONDITION persistent, MSITABLE **table_ret)
618 MSIRECORD *rec = NULL;
623 /* only add tables that don't exist already */
624 if( TABLE_Exists(db, name ) )
626 WARN("table %s exists\n", debugstr_w(name));
627 return ERROR_BAD_QUERY_SYNTAX;
630 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
632 return ERROR_FUNCTION_FAILED;
634 table->ref_count = 1;
635 table->row_count = 0;
637 table->nonpersistent_row_count = 0;
638 table->nonpersistent_data = NULL;
639 table->colinfo = NULL;
640 table->col_count = 0;
641 table->persistent = persistent;
642 lstrcpyW( table->name, name );
644 for( col = col_info; col; col = col->next )
647 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
651 return ERROR_FUNCTION_FAILED;
654 for( i = 0, col = col_info; col; i++, col = col->next )
656 table->colinfo[ i ].tablename = strdupW( col->table );
657 table->colinfo[ i ].number = i + 1;
658 table->colinfo[ i ].colname = strdupW( col->column );
659 table->colinfo[ i ].type = col->type;
660 table->colinfo[ i ].offset = 0;
661 table->colinfo[ i ].ref_count = 0;
662 table->colinfo[ i ].hash_table = NULL;
663 table->colinfo[ i ].temporary = col->temporary;
665 table_calc_column_offsets( db, table->colinfo, table->col_count);
667 r = TABLE_CreateView( db, szTables, &tv );
668 TRACE("CreateView returned %x\n", r);
675 r = tv->ops->execute( tv, 0 );
676 TRACE("tv execute returned %x\n", r);
680 rec = MSI_CreateRecord( 1 );
684 r = MSI_RecordSetStringW( rec, 1, name );
688 r = tv->ops->insert_row( tv, rec, -1, persistent == MSICONDITION_FALSE );
689 TRACE("insert_row returned %x\n", r);
693 tv->ops->delete( tv );
696 msiobj_release( &rec->hdr );
699 if( persistent != MSICONDITION_FALSE )
701 /* add each column to the _Columns table */
702 r = TABLE_CreateView( db, szColumns, &tv );
706 r = tv->ops->execute( tv, 0 );
707 TRACE("tv execute returned %x\n", r);
711 rec = MSI_CreateRecord( 4 );
715 r = MSI_RecordSetStringW( rec, 1, name );
720 * need to set the table, column number, col name and type
721 * for each column we enter in the table
724 for( col = col_info; col; col = col->next )
726 r = MSI_RecordSetInteger( rec, 2, nField );
730 r = MSI_RecordSetStringW( rec, 3, col->column );
734 r = MSI_RecordSetInteger( rec, 4, col->type );
738 r = tv->ops->insert_row( tv, rec, -1, FALSE );
750 msiobj_release( &rec->hdr );
751 /* FIXME: remove values from the string table on error */
753 tv->ops->delete( tv );
755 if (r == ERROR_SUCCESS)
757 list_add_head( &db->tables, &table->entry );
766 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
771 /* first, see if the table is cached */
772 table = find_cached_table( db, name );
776 return ERROR_SUCCESS;
779 /* nonexistent tables should be interpreted as empty tables */
780 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
782 return ERROR_FUNCTION_FAILED;
784 table->row_count = 0;
786 table->nonpersistent_row_count = 0;
787 table->nonpersistent_data = NULL;
788 table->colinfo = NULL;
789 table->col_count = 0;
790 table->persistent = MSICONDITION_TRUE;
791 lstrcpyW( table->name, name );
793 if ( !lstrcmpW(name, szTables) || !lstrcmpW(name, szColumns) )
794 table->persistent = MSICONDITION_NONE;
796 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
797 if (r != ERROR_SUCCESS)
799 free_table ( table );
803 r = read_table_from_storage( db, table, db->storage );
804 if( r != ERROR_SUCCESS )
810 list_add_head( &db->tables, &table->entry );
812 return ERROR_SUCCESS;
815 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
817 BYTE *rawdata = NULL, *p;
818 UINT rawsize, r, i, j, row_size;
820 /* Nothing to do for non-persistent tables */
821 if( t->persistent == MSICONDITION_FALSE )
822 return ERROR_SUCCESS;
824 TRACE("Saving %s\n", debugstr_w( t->name ) );
826 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
828 rawsize = t->row_count * row_size;
829 rawdata = msi_alloc_zero( rawsize );
832 r = ERROR_NOT_ENOUGH_MEMORY;
837 for( i=0; i<t->col_count; i++ )
839 for( j=0; j<t->row_count; j++ )
841 UINT offset = t->colinfo[i].offset;
843 *p++ = t->data[j][offset];
844 *p++ = t->data[j][offset + 1];
845 if( 4 == bytes_per_column( db, &t->colinfo[i] ) )
847 *p++ = t->data[j][offset + 2];
848 *p++ = t->data[j][offset + 3];
853 TRACE("writing %d bytes\n", rawsize);
854 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
862 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
867 for( i=0; colinfo && (i<count); i++ )
869 assert( (i+1) == colinfo[ i ].number );
871 colinfo[i].offset = colinfo[ i - 1 ].offset
872 + bytes_per_column( db, &colinfo[ i - 1 ] );
874 colinfo[i].offset = 0;
875 TRACE("column %d is [%s] with type %08x ofs %d\n",
876 colinfo[i].number, debugstr_w(colinfo[i].colname),
877 colinfo[i].type, colinfo[i].offset);
881 static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name,
882 MSICOLUMNINFO *colinfo, UINT *sz)
884 const MSICOLUMNINFO *p;
887 TRACE("%s\n", debugstr_w(name));
889 if (!lstrcmpW( name, szTables ))
894 else if (!lstrcmpW( name, szColumns ))
900 return ERROR_FUNCTION_FAILED;
902 /* duplicate the string data so we can free it in msi_free_colinfo */
905 if (colinfo && (i < *sz) )
908 colinfo[i].tablename = strdupW( p[i].tablename );
909 colinfo[i].colname = strdupW( p[i].colname );
911 if( colinfo && (i >= *sz) )
914 table_calc_column_offsets( db, colinfo, n );
916 return ERROR_SUCCESS;
919 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
923 for( i=0; i<count; i++ )
925 msi_free( colinfo[i].tablename );
926 msi_free( colinfo[i].colname );
927 msi_free( colinfo[i].hash_table );
931 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
933 return strdupW(msi_string_lookup_id( db->strings, stringid ));
936 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
940 for (i = 0; i < bytes; i++)
941 ret += (data[row][col + i] << i * 8);
946 static UINT get_tablecolumns( MSIDATABASE *db,
947 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
949 UINT r, i, n=0, table_id, count, maxcount = *sz;
950 MSITABLE *table = NULL;
952 TRACE("%s\n", debugstr_w(szTableName));
954 /* first check if there is a default table with that name */
955 r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
956 if( ( r == ERROR_SUCCESS ) && *sz )
959 r = get_table( db, szColumns, &table );
960 if( r != ERROR_SUCCESS )
962 ERR("couldn't load _Columns table\n");
963 return ERROR_FUNCTION_FAILED;
966 /* convert table and column names to IDs from the string table */
967 r = msi_string2idW( db->strings, szTableName, &table_id );
968 if( r != ERROR_SUCCESS )
970 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
974 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
976 /* Note: _Columns table doesn't have non-persistent data */
978 /* if maxcount is non-zero, assume it's exactly right for this table */
979 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
980 count = table->row_count;
981 for( i=0; i<count; i++ )
983 if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
987 UINT id = read_table_int(table->data, i, table->colinfo[2].offset, db->bytes_per_strref);
988 UINT col = read_table_int(table->data, i, table->colinfo[1].offset, sizeof(USHORT)) - (1<<15);
990 /* check the column number is in range */
991 if (col<1 || col>maxcount)
993 ERR("column %d out of range\n", col);
997 /* check if this column was already set */
998 if (colinfo[ col - 1 ].number)
1000 ERR("duplicate column %d\n", col);
1004 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
1005 colinfo[ col - 1 ].number = col;
1006 colinfo[ col - 1 ].colname = msi_makestring( db, id );
1007 colinfo[ col - 1 ].type = read_table_int(table->data, i,
1008 table->colinfo[3].offset,
1009 sizeof(USHORT)) - (1<<15);
1010 colinfo[ col - 1 ].offset = 0;
1011 colinfo[ col - 1 ].ref_count = 0;
1012 colinfo[ col - 1 ].hash_table = NULL;
1017 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
1019 if (colinfo && n != maxcount)
1021 ERR("missing column in table %s\n", debugstr_w(szTableName));
1022 msi_free_colinfo(colinfo, maxcount );
1023 return ERROR_FUNCTION_FAILED;
1026 table_calc_column_offsets( db, colinfo, n );
1029 return ERROR_SUCCESS;
1032 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
1035 UINT size, offset, old_count;
1038 table = find_cached_table( db, name );
1039 old_count = table->col_count;
1040 msi_free( table->colinfo );
1041 table->colinfo = NULL;
1043 table_get_column_info( db, name, &table->colinfo, &table->col_count );
1044 if (!table->col_count)
1047 size = msi_table_get_row_size( db, table->colinfo, table->col_count );
1048 offset = table->colinfo[table->col_count - 1].offset;
1050 for ( n = 0; n < table->row_count; n++ )
1052 table->data[n] = msi_realloc( table->data[n], size );
1053 if (old_count < table->col_count)
1054 memset( &table->data[n][offset], 0, size - offset );
1058 /* try to find the table name in the _Tables table */
1059 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1061 UINT r, table_id = 0, i, count;
1062 MSITABLE *table = NULL;
1064 static const WCHAR szStreams[] = {'_','S','t','r','e','a','m','s',0};
1065 static const WCHAR szStorages[] = {'_','S','t','o','r','a','g','e','s',0};
1067 if( !lstrcmpW( name, szTables ) || !lstrcmpW( name, szColumns ) ||
1068 !lstrcmpW( name, szStreams ) || !lstrcmpW( name, szStorages ) )
1071 r = msi_string2idW( db->strings, name, &table_id );
1072 if( r != ERROR_SUCCESS )
1074 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1078 r = get_table( db, szTables, &table );
1079 if( r != ERROR_SUCCESS )
1081 ERR("table %s not available\n", debugstr_w(szTables));
1085 count = table->row_count;
1086 for( i=0; i<count; i++ )
1087 if( table->data[ i ][ 0 ] == table_id )
1090 count = table->nonpersistent_row_count;
1091 for( i=0; i<count; i++ )
1092 if( table->nonpersistent_data[ i ][ 0 ] == table_id )
1098 /* below is the query interface to a table */
1100 typedef struct tagMSITABLEVIEW
1105 MSICOLUMNINFO *columns;
1106 MSIORDERINFO *order;
1112 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1114 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1119 return ERROR_INVALID_PARAMETER;
1121 if( (col==0) || (col>tv->num_cols) )
1122 return ERROR_INVALID_PARAMETER;
1124 /* how many rows are there ? */
1125 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1126 return ERROR_NO_MORE_ITEMS;
1128 if( tv->columns[col-1].offset >= tv->row_size )
1130 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1131 ERR("%p %p\n", tv, tv->columns );
1132 return ERROR_FUNCTION_FAILED;
1136 row = tv->order->reorder[row];
1138 if (row >= tv->table->row_count)
1140 row -= tv->table->row_count;
1141 data = tv->table->nonpersistent_data;
1144 data = tv->table->data;
1146 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1147 if (n != 2 && n != 3 && n != 4)
1149 ERR("oops! what is %d bytes per column?\n", n );
1150 return ERROR_FUNCTION_FAILED;
1153 offset = tv->columns[col-1].offset;
1154 *val = read_table_int(data, row, offset, n);
1156 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1158 return ERROR_SUCCESS;
1162 * We need a special case for streams, as we need to reference column with
1163 * the name of the stream in the same table, and the table name
1164 * which may not be available at higher levels of the query
1166 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1168 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1169 UINT ival = 0, refcol = 0, r;
1173 static const WCHAR szDot[] = { '.', 0 };
1176 if( !view->ops->fetch_int )
1177 return ERROR_INVALID_PARAMETER;
1180 * The column marked with the type stream data seems to have a single number
1181 * which references the column containing the name of the stream data
1183 * Fetch the column to reference first.
1185 r = view->ops->fetch_int( view, row, col, &ival );
1186 if( r != ERROR_SUCCESS )
1189 /* check the column value is in range */
1190 if (ival > tv->num_cols || ival == col)
1192 ERR("bad column ref (%u) for stream\n", ival);
1193 return ERROR_FUNCTION_FAILED;
1196 if ( tv->columns[ival - 1].type & MSITYPE_STRING )
1198 /* now get the column with the name of the stream */
1199 r = view->ops->fetch_int( view, row, ival, &refcol );
1200 if ( r != ERROR_SUCCESS )
1203 /* lookup the string value from the string table */
1204 sval = msi_string_lookup_id( tv->db->strings, refcol );
1206 return ERROR_INVALID_PARAMETER;
1210 static const WCHAR fmt[] = { '%','d',0 };
1211 sprintfW( number, fmt, ival );
1215 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1216 full_name = msi_alloc( len*sizeof(WCHAR) );
1217 lstrcpyW( full_name, tv->name );
1218 lstrcatW( full_name, szDot );
1219 lstrcatW( full_name, sval );
1221 r = db_get_raw_stream( tv->db, full_name, stm );
1223 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1224 msi_free( full_name );
1229 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1235 return ERROR_INVALID_PARAMETER;
1237 if( (col==0) || (col>tv->num_cols) )
1238 return ERROR_INVALID_PARAMETER;
1240 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1241 return ERROR_INVALID_PARAMETER;
1243 if( tv->columns[col-1].offset >= tv->row_size )
1245 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1246 ERR("%p %p\n", tv, tv->columns );
1247 return ERROR_FUNCTION_FAILED;
1250 msi_free( tv->columns[col-1].hash_table );
1251 tv->columns[col-1].hash_table = NULL;
1253 if (row >= tv->table->row_count)
1255 row -= tv->table->row_count;
1256 data = tv->table->nonpersistent_data;
1259 data = tv->table->data;
1261 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1262 if ( n != 2 && n != 3 && n != 4 )
1264 ERR("oops! what is %d bytes per column?\n", n );
1265 return ERROR_FUNCTION_FAILED;
1268 offset = tv->columns[col-1].offset;
1269 for ( i = 0; i < n; i++ )
1270 data[row][offset + i] = (val >> i * 8) & 0xff;
1272 return ERROR_SUCCESS;
1275 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1277 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1280 return ERROR_INVALID_PARAMETER;
1283 row = tv->order->reorder[row];
1285 return msi_view_get_row(tv->db, view, row, rec);
1288 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1290 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1291 UINT i, val, r = ERROR_SUCCESS;
1294 return ERROR_INVALID_PARAMETER;
1296 /* test if any of the mask bits are invalid */
1297 if ( mask >= (1<<tv->num_cols) )
1298 return ERROR_INVALID_PARAMETER;
1300 for ( i = 0; i < tv->num_cols; i++ )
1304 /* only update the fields specified in the mask */
1305 if ( !(mask&(1<<i)) )
1308 /* if row >= tv->table->row_count then it is a non-persistent row */
1309 persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1310 (row < tv->table->row_count);
1311 /* FIXME: should we allow updating keys? */
1314 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1316 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1318 val = 1; /* refers to the first key column */
1320 else if ( tv->columns[i].type & MSITYPE_STRING )
1322 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1325 r = msi_string2idW(tv->db->strings, sval, &ival);
1326 if (r == ERROR_SUCCESS)
1328 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1333 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
1334 persistent ? StringPersistent : StringNonPersistent );
1336 else if ( 2 == bytes_per_column( tv->db, &tv->columns[ i ] ) )
1338 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1339 if ( val & 0xffff0000 )
1341 ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1342 return ERROR_FUNCTION_FAILED;
1347 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1348 val = ival ^ 0x80000000;
1352 r = TABLE_set_int( tv, row, i+1, val );
1353 if ( r != ERROR_SUCCESS )
1359 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1361 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1367 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1370 return ERROR_INVALID_PARAMETER;
1372 row = msi_alloc_zero( tv->row_size );
1374 return ERROR_NOT_ENOUGH_MEMORY;
1378 row_count = &tv->table->nonpersistent_row_count;
1379 data_ptr = &tv->table->nonpersistent_data;
1381 *num = tv->table->row_count + tv->table->nonpersistent_row_count;
1385 row_count = &tv->table->row_count;
1386 data_ptr = &tv->table->data;
1388 *num = tv->table->row_count;
1391 sz = (*row_count + 1) * sizeof (BYTE*);
1393 p = msi_realloc( *data_ptr, sz );
1395 p = msi_alloc( sz );
1399 return ERROR_NOT_ENOUGH_MEMORY;
1403 (*data_ptr)[*row_count] = row;
1406 return ERROR_SUCCESS;
1409 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1411 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1413 TRACE("%p %p\n", tv, record);
1415 TRACE("There are %d columns\n", tv->num_cols );
1417 return ERROR_SUCCESS;
1420 static UINT TABLE_close( struct tagMSIVIEW *view )
1422 TRACE("%p\n", view );
1424 return ERROR_SUCCESS;
1427 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1429 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1431 TRACE("%p %p %p\n", view, rows, cols );
1434 *cols = tv->num_cols;
1438 return ERROR_INVALID_PARAMETER;
1439 *rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1442 return ERROR_SUCCESS;
1445 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1446 UINT n, LPWSTR *name, UINT *type, BOOL *temporary )
1448 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1450 TRACE("%p %d %p %p\n", tv, n, name, type );
1452 if( ( n == 0 ) || ( n > tv->num_cols ) )
1453 return ERROR_INVALID_PARAMETER;
1457 *name = strdupW( tv->columns[n-1].colname );
1459 return ERROR_FUNCTION_FAILED;
1463 *type = tv->columns[n-1].type;
1466 *temporary = tv->columns[n-1].temporary;
1468 return ERROR_SUCCESS;
1471 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1473 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1477 /* check there's no null values where they're not allowed */
1478 for( i = 0; i < tv->num_cols; i++ )
1480 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1483 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1484 TRACE("skipping binary column\n");
1485 else if ( tv->columns[i].type & MSITYPE_STRING )
1489 str = MSI_RecordGetString( rec, i+1 );
1490 if (str == NULL || str[0] == 0)
1491 return ERROR_INVALID_DATA;
1497 n = MSI_RecordGetInteger( rec, i+1 );
1498 if (n == MSI_NULL_INTEGER)
1499 return ERROR_INVALID_DATA;
1503 /* check there's no duplicate keys */
1504 r = msi_table_find_row( tv, rec, &row );
1505 if (r == ERROR_SUCCESS)
1506 return ERROR_FUNCTION_FAILED;
1508 return ERROR_SUCCESS;
1511 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1513 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1514 UINT i, r, idx, size;
1517 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1519 /* check that the key is unique - can we find a matching row? */
1520 r = table_validate_new( tv, rec );
1521 if( r != ERROR_SUCCESS )
1522 return ERROR_FUNCTION_FAILED;
1524 r = table_create_new_row( view, &row, temporary );
1525 TRACE("insert_row returned %08x\n", r);
1526 if( r != ERROR_SUCCESS )
1532 data = tv->table->nonpersistent_data;
1533 size = tv->table->nonpersistent_row_count;
1534 idx -= tv->table->row_count;
1538 data = tv->table->data;
1539 size = tv->table->row_count;
1542 /* shift the rows to make room for the new row */
1543 if( idx != size - 1 )
1545 for (i = 1; i < size - idx; i++)
1546 memmove(&(data[size - i][0]),
1547 &(data[size - i - 1][0]), tv->row_size);
1550 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1553 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1555 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1556 UINT r, num_rows, num_cols, i;
1559 TRACE("%p %d\n", tv, row);
1562 return ERROR_INVALID_PARAMETER;
1564 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1565 if ( r != ERROR_SUCCESS )
1568 if ( row >= num_rows )
1569 return ERROR_FUNCTION_FAILED;
1571 if ( row < tv->table->row_count )
1573 num_rows = tv->table->row_count;
1574 tv->table->row_count--;
1575 data = tv->table->data;
1579 num_rows = tv->table->nonpersistent_row_count;
1580 row -= tv->table->row_count;
1581 tv->table->nonpersistent_row_count--;
1582 data = tv->table->nonpersistent_data;
1585 /* reset the hash tables */
1586 for (i = 0; i < tv->num_cols; i++)
1588 msi_free( tv->columns[i].hash_table );
1589 tv->columns[i].hash_table = NULL;
1592 if ( row == num_rows - 1 )
1593 return ERROR_SUCCESS;
1595 for (i = row + 1; i < num_rows; i++)
1596 memcpy(data[i - 1], data[i], tv->row_size);
1598 return ERROR_SUCCESS;
1601 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1603 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1606 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1607 * sets the fetched record apart from other records
1611 return ERROR_INVALID_PARAMETER;
1613 r = msi_table_find_row(tv, rec, &new_row);
1614 if (r != ERROR_SUCCESS)
1616 ERR("can't find row to modify\n");
1617 return ERROR_FUNCTION_FAILED;
1620 /* the row cannot be changed */
1621 if (row != new_row + 1)
1622 return ERROR_FUNCTION_FAILED;
1624 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1627 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1629 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1632 r = msi_table_find_row(tv, rec, &row);
1633 if (r != ERROR_SUCCESS)
1636 return TABLE_delete_row(view, row);
1639 static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1644 r = TABLE_get_row(view, row - 1, &curr);
1645 if (r != ERROR_SUCCESS)
1648 count = MSI_RecordGetFieldCount(rec);
1649 for (i = 0; i < count; i++)
1650 MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1652 msiobj_release(&curr->hdr);
1653 return ERROR_SUCCESS;
1656 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1657 MSIRECORD *rec, UINT row)
1659 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1662 TRACE("%p %d %p\n", view, eModifyMode, rec );
1664 switch (eModifyMode)
1666 case MSIMODIFY_DELETE:
1667 r = modify_delete_row( view, rec );
1669 case MSIMODIFY_VALIDATE_NEW:
1670 r = table_validate_new( tv, rec );
1673 case MSIMODIFY_INSERT:
1674 r = table_validate_new( tv, rec );
1675 if (r != ERROR_SUCCESS)
1677 r = TABLE_insert_row( view, rec, -1, FALSE );
1680 case MSIMODIFY_INSERT_TEMPORARY:
1681 r = table_validate_new( tv, rec );
1682 if (r != ERROR_SUCCESS)
1684 r = TABLE_insert_row( view, rec, -1, TRUE );
1687 case MSIMODIFY_REFRESH:
1688 r = msi_refresh_record( view, rec, row );
1691 case MSIMODIFY_UPDATE:
1692 r = msi_table_update( view, rec, row );
1695 case MSIMODIFY_ASSIGN:
1696 case MSIMODIFY_REPLACE:
1697 case MSIMODIFY_MERGE:
1698 case MSIMODIFY_VALIDATE:
1699 case MSIMODIFY_VALIDATE_FIELD:
1700 case MSIMODIFY_VALIDATE_DELETE:
1701 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1702 r = ERROR_CALL_NOT_IMPLEMENTED;
1706 r = ERROR_INVALID_DATA;
1712 static UINT TABLE_delete( struct tagMSIVIEW *view )
1714 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1716 TRACE("%p\n", view );
1723 msi_free( tv->order->reorder );
1724 msi_free( tv->order );
1730 return ERROR_SUCCESS;
1733 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1734 UINT val, UINT *row, MSIITERHANDLE *handle )
1736 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1737 const MSICOLUMNHASHENTRY *entry;
1739 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1742 return ERROR_INVALID_PARAMETER;
1744 if( (col==0) || (col > tv->num_cols) )
1745 return ERROR_INVALID_PARAMETER;
1747 if( !tv->columns[col-1].hash_table )
1750 UINT num_rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1751 MSICOLUMNHASHENTRY **hash_table;
1752 MSICOLUMNHASHENTRY *new_entry;
1754 if( tv->columns[col-1].offset >= tv->row_size )
1756 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1757 ERR("%p %p\n", tv, tv->columns );
1758 return ERROR_FUNCTION_FAILED;
1761 /* allocate contiguous memory for the table and its entries so we
1762 * don't have to do an expensive cleanup */
1763 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1764 num_rows * sizeof(MSICOLUMNHASHENTRY));
1766 return ERROR_OUTOFMEMORY;
1768 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1769 tv->columns[col-1].hash_table = hash_table;
1771 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1773 for (i = 0; i < num_rows; i++, new_entry++)
1777 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1780 new_entry->next = NULL;
1781 new_entry->value = row_value;
1783 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1785 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1786 while (prev_entry->next)
1787 prev_entry = prev_entry->next;
1788 prev_entry->next = new_entry;
1791 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1796 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1798 entry = (*handle)->next;
1800 while (entry && entry->value != val)
1801 entry = entry->next;
1805 return ERROR_NO_MORE_ITEMS;
1809 return ERROR_SUCCESS;
1812 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1814 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1817 TRACE("%p %d\n", view, tv->table->ref_count);
1819 for (i = 0; i < tv->table->col_count; i++)
1821 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1822 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
1825 return InterlockedIncrement(&tv->table->ref_count);
1828 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
1830 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1831 MSIRECORD *rec = NULL;
1832 MSIVIEW *columns = NULL;
1835 rec = MSI_CreateRecord(2);
1837 return ERROR_OUTOFMEMORY;
1839 MSI_RecordSetStringW(rec, 1, table);
1840 MSI_RecordSetInteger(rec, 2, number);
1842 r = TABLE_CreateView(tv->db, szColumns, &columns);
1843 if (r != ERROR_SUCCESS)
1846 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row);
1847 if (r != ERROR_SUCCESS)
1850 r = TABLE_delete_row(columns, row);
1851 if (r != ERROR_SUCCESS)
1854 msi_update_table_columns(tv->db, table);
1857 msiobj_release(&rec->hdr);
1858 columns->ops->delete(columns);
1862 static UINT TABLE_release(struct tagMSIVIEW *view)
1864 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1865 INT ref = tv->table->ref_count;
1868 TRACE("%p %d\n", view, ref);
1870 for (i = 0; i < tv->table->col_count; i++)
1872 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1874 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
1877 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
1878 tv->table->colinfo[i].number);
1879 if (r != ERROR_SUCCESS)
1885 ref = InterlockedDecrement(&tv->table->ref_count);
1888 if (!tv->table->row_count)
1890 list_remove(&tv->table->entry);
1891 free_table(tv->table);
1899 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
1900 LPCWSTR column, UINT type, BOOL hold)
1902 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1907 rec = MSI_CreateRecord(4);
1909 return ERROR_OUTOFMEMORY;
1911 MSI_RecordSetStringW(rec, 1, table);
1912 MSI_RecordSetInteger(rec, 2, number);
1913 MSI_RecordSetStringW(rec, 3, column);
1914 MSI_RecordSetInteger(rec, 4, type);
1916 r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
1917 if (r != ERROR_SUCCESS)
1920 msi_update_table_columns(tv->db, table);
1925 msitable = find_cached_table(tv->db, table);
1926 for (i = 0; i < msitable->col_count; i++)
1928 if (!lstrcmpW(msitable->colinfo[i].colname, column))
1930 InterlockedIncrement(&msitable->colinfo[i].ref_count);
1936 msiobj_release(&rec->hdr);
1940 static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
1944 r = TABLE_get_dimensions(view, NULL, &count);
1945 if (r != ERROR_SUCCESS)
1948 if (order->num_cols >= count)
1949 return ERROR_FUNCTION_FAILED;
1951 r = VIEW_find_column(view, name, &n);
1952 if (r != ERROR_SUCCESS)
1955 order->cols[order->num_cols] = n;
1956 TRACE("Ordering by column %s (%d)\n", debugstr_w(name), n);
1960 return ERROR_SUCCESS;
1963 static UINT order_compare(struct tagMSIVIEW *view, MSIORDERINFO *order,
1964 UINT a, UINT b, UINT *swap)
1966 UINT r, i, a_val = 0, b_val = 0;
1969 for (i = 0; i < order->num_cols; i++)
1971 r = TABLE_fetch_int(view, a, order->cols[i], &a_val);
1972 if (r != ERROR_SUCCESS)
1975 r = TABLE_fetch_int(view, b, order->cols[i], &b_val);
1976 if (r != ERROR_SUCCESS)
1987 return ERROR_SUCCESS;
1990 static UINT order_mergesort(struct tagMSIVIEW *view, MSIORDERINFO *order,
1991 UINT left, UINT right)
1994 UINT swap = 0, center = (left + right) / 2;
1995 UINT *array = order->reorder;
1998 return ERROR_SUCCESS;
2000 /* sort the left half */
2001 r = order_mergesort(view, order, left, center);
2002 if (r != ERROR_SUCCESS)
2005 /* sort the right half */
2006 r = order_mergesort(view, order, center + 1, right);
2007 if (r != ERROR_SUCCESS)
2010 for (i = left, j = center + 1; (i <= center) && (j <= right); i++)
2012 r = order_compare(view, order, array[i], array[j], &swap);
2013 if (r != ERROR_SUCCESS)
2019 memmove(&array[i + 1], &array[i], (j - i) * sizeof(UINT));
2026 return ERROR_SUCCESS;
2029 static UINT order_verify(struct tagMSIVIEW *view, MSIORDERINFO *order, UINT num_rows)
2033 for (i = 1; i < num_rows; i++)
2035 r = order_compare(view, order, order->reorder[i - 1],
2036 order->reorder[i], &swap);
2037 if (r != ERROR_SUCCESS)
2043 ERR("Bad order! %d\n", i);
2044 return ERROR_FUNCTION_FAILED;
2047 return ERROR_SUCCESS;
2050 static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
2052 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2053 MSIORDERINFO *order;
2058 TRACE("sorting table %s\n", debugstr_w(tv->name));
2060 r = TABLE_get_dimensions(view, &rows, &cols);
2061 if (r != ERROR_SUCCESS)
2065 return ERROR_SUCCESS;
2067 order = msi_alloc_zero(sizeof(MSIORDERINFO) + sizeof(UINT) * cols);
2069 return ERROR_OUTOFMEMORY;
2071 for (ptr = columns; ptr; ptr = ptr->next)
2072 order_add_column(view, order, ptr->column);
2074 order->reorder = msi_alloc(rows * sizeof(UINT));
2075 if (!order->reorder)
2076 return ERROR_OUTOFMEMORY;
2078 for (i = 0; i < rows; i++)
2079 order->reorder[i] = i;
2081 r = order_mergesort(view, order, 0, rows - 1);
2082 if (r != ERROR_SUCCESS)
2085 r = order_verify(view, order, rows);
2086 if (r != ERROR_SUCCESS)
2091 return ERROR_SUCCESS;
2094 static UINT TABLE_drop(struct tagMSIVIEW *view)
2096 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2097 MSIVIEW *tables = NULL;
2098 MSIRECORD *rec = NULL;
2102 TRACE("dropping table %s\n", debugstr_w(tv->name));
2104 for (i = tv->table->col_count - 1; i >= 0; i--)
2106 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2107 tv->table->colinfo[i].number);
2108 if (r != ERROR_SUCCESS)
2112 rec = MSI_CreateRecord(1);
2114 return ERROR_OUTOFMEMORY;
2116 MSI_RecordSetStringW(rec, 1, tv->name);
2118 r = TABLE_CreateView(tv->db, szTables, &tables);
2119 if (r != ERROR_SUCCESS)
2122 r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row);
2123 if (r != ERROR_SUCCESS)
2126 r = TABLE_delete_row(tables, row);
2127 if (r != ERROR_SUCCESS)
2130 list_remove(&tv->table->entry);
2131 free_table(tv->table);
2135 msiobj_release(&rec->hdr);
2136 tables->ops->delete(tables);
2141 static const MSIVIEWOPS table_ops =
2151 TABLE_get_dimensions,
2152 TABLE_get_column_info,
2155 TABLE_find_matching_rows,
2159 TABLE_remove_column,
2164 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2169 static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
2170 static const WCHAR Storages[] = {'_','S','t','o','r','a','g','e','s',0};
2172 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2174 if ( !lstrcmpW( name, Streams ) )
2175 return STREAMS_CreateView( db, view );
2176 else if ( !lstrcmpW( name, Storages ) )
2177 return STORAGES_CreateView( db, view );
2179 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
2180 tv = msi_alloc_zero( sz );
2182 return ERROR_FUNCTION_FAILED;
2184 r = get_table( db, name, &tv->table );
2185 if( r != ERROR_SUCCESS )
2188 WARN("table not found\n");
2192 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2194 /* fill the structure */
2195 tv->view.ops = &table_ops;
2197 tv->columns = tv->table->colinfo;
2198 tv->num_cols = tv->table->col_count;
2199 tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count );
2201 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2203 *view = (MSIVIEW*) tv;
2204 lstrcpyW( tv->name, name );
2206 return ERROR_SUCCESS;
2209 UINT MSI_CommitTables( MSIDATABASE *db )
2212 MSITABLE *table = NULL;
2216 r = msi_save_string_table( db->strings, db->storage );
2217 if( r != ERROR_SUCCESS )
2219 WARN("failed to save string table r=%08x\n",r);
2223 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2225 r = save_table( db, table );
2226 if( r != ERROR_SUCCESS )
2228 WARN("failed to save table %s (r=%08x)\n",
2229 debugstr_w(table->name), r);
2234 /* force everything to reload next time */
2235 free_cached_tables( db );
2237 return ERROR_SUCCESS;
2240 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2245 TRACE("%p %s\n", db, debugstr_w(table));
2248 return MSICONDITION_ERROR;
2250 r = get_table( db, table, &t );
2251 if (r != ERROR_SUCCESS)
2252 return MSICONDITION_NONE;
2254 return t->persistent;
2257 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2261 for (i = 0; i < bytes; i++)
2262 ret += (data[col + i] << i * 8);
2267 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2268 const BYTE *rawdata, UINT bytes_per_strref )
2270 UINT i, val, ofs = 0;
2272 MSICOLUMNINFO *columns = tv->columns;
2275 mask = rawdata[0] | (rawdata[1] << 8);
2278 rec = MSI_CreateRecord( tv->num_cols );
2283 for( i=0; i<tv->num_cols; i++ )
2285 if ( (mask&1) && (i>=(mask>>8)) )
2287 /* all keys must be present */
2288 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2291 if( (columns[i].type & MSITYPE_STRING) &&
2292 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2296 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2297 sval = msi_string_lookup_id( st, val );
2298 MSI_RecordSetStringW( rec, i+1, sval );
2299 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
2300 ofs += bytes_per_strref;
2304 UINT n = bytes_per_column( tv->db, &columns[i] );
2308 val = read_raw_int(rawdata, ofs, n);
2310 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
2311 TRACE(" field %d [0x%04x]\n", i+1, val );
2314 val = read_raw_int(rawdata, ofs, n);
2316 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2317 TRACE(" field %d [0x%08x]\n", i+1, val );
2320 ERR("oops - unknown column width %d\n", n);
2329 static void dump_record( MSIRECORD *rec )
2333 n = MSI_RecordGetFieldCount( rec );
2334 for( i=1; i<=n; i++ )
2336 LPCWSTR sval = MSI_RecordGetString( rec, i );
2338 if( MSI_RecordIsNull( rec, i ) )
2339 TRACE("row -> []\n");
2340 else if( (sval = MSI_RecordGetString( rec, i )) )
2341 TRACE("row -> [%s]\n", debugstr_w(sval));
2343 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2347 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2352 for( i=0; i<(rawsize/2); i++ )
2354 sval = msi_string_lookup_id( st, rawdata[i] );
2355 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
2359 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2364 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2365 for( i=0; i<tv->num_cols; i++ )
2369 if ( ~tv->columns[i].type & MSITYPE_KEY )
2372 /* turn the transform column value into a row value */
2373 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2374 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2376 str = MSI_RecordGetString( rec, i+1 );
2377 r = msi_string2idW( tv->db->strings, str, &data[i] );
2379 /* if there's no matching string in the string table,
2380 these keys can't match any record, so fail now. */
2381 if( ERROR_SUCCESS != r )
2389 data[i] = MSI_RecordGetInteger( rec, i+1 );
2391 if (data[i] == MSI_NULL_INTEGER)
2393 else if ((tv->columns[i].type&0xff) == 2)
2396 data[i] += 0x80000000;
2402 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
2404 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2406 for( i=0; i<tv->num_cols; i++ )
2408 if ( ~tv->columns[i].type & MSITYPE_KEY )
2411 /* turn the transform column value into a row value */
2412 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2413 if ( r != ERROR_SUCCESS )
2415 ERR("TABLE_fetch_int shouldn't fail here\n");
2419 /* if this key matches, move to the next column */
2422 ret = ERROR_FUNCTION_FAILED;
2426 ret = ERROR_SUCCESS;
2432 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
2434 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2436 data = msi_record_to_row( tv, rec );
2439 for( i = 0; i < tv->table->row_count + tv->table->nonpersistent_row_count; i++ )
2441 r = msi_row_matches( tv, i, data );
2442 if( r == ERROR_SUCCESS )
2458 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2459 string_table *st, TRANSFORMDATA *transform,
2460 UINT bytes_per_strref )
2463 BYTE *rawdata = NULL;
2464 MSITABLEVIEW *tv = NULL;
2465 UINT r, n, sz, i, mask;
2466 MSIRECORD *rec = NULL;
2472 return ERROR_SUCCESS;
2474 name = transform->name;
2477 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2479 /* read the transform data */
2480 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2483 TRACE("table %s empty\n", debugstr_w(name) );
2484 return ERROR_INVALID_TABLE;
2487 /* create a table view */
2488 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2489 if( r != ERROR_SUCCESS )
2492 r = tv->view.ops->execute( &tv->view, NULL );
2493 if( r != ERROR_SUCCESS )
2496 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2497 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2499 /* interpret the data */
2501 for( n=0; n < rawsize; )
2503 mask = rawdata[n] | (rawdata[n+1] << 8);
2508 * if the low bit is set, columns are continuous and
2509 * the number of columns is specified in the high byte
2512 for( i=0; i<tv->num_cols; i++ )
2514 if( (tv->columns[i].type & MSITYPE_STRING) &&
2515 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2516 sz += bytes_per_strref;
2518 sz += bytes_per_column( tv->db, &tv->columns[i] );
2524 * If the low bit is not set, mask is a bitmask.
2525 * Excepting for key fields, which are always present,
2526 * each bit indicates that a field is present in the transform record.
2528 * mask == 0 is a special case ... only the keys will be present
2529 * and it means that this row should be deleted.
2532 for( i=0; i<tv->num_cols; i++ )
2534 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2536 if( (tv->columns[i].type & MSITYPE_STRING) &&
2537 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2538 sz += bytes_per_strref;
2540 sz += bytes_per_column( tv->db, &tv->columns[i] );
2545 /* check we didn't run of the end of the table */
2546 if ( (n+sz) > rawsize )
2549 dump_table( st, (USHORT *)rawdata, rawsize );
2553 rec = msi_get_transform_record( tv, st, &rawdata[n], bytes_per_strref );
2560 UINT number = MSI_NULL_INTEGER;
2562 TRACE("inserting record\n");
2564 if (!lstrcmpW(name, szColumns))
2566 MSI_RecordGetStringW( rec, 1, table, &sz );
2567 number = MSI_RecordGetInteger( rec, 2 );
2570 * Native msi seems writes nul into the Number (2nd) column of
2571 * the _Columns table, only when the columns are from a new table
2573 if ( number == MSI_NULL_INTEGER )
2575 /* reset the column number on a new table */
2576 if ( lstrcmpW(coltable, table) )
2579 lstrcpyW( coltable, table );
2582 /* fix nul column numbers */
2583 MSI_RecordSetInteger( rec, 2, ++colcol );
2587 r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
2588 if (r != ERROR_SUCCESS)
2589 ERR("insert row failed\n");
2591 if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
2592 msi_update_table_columns( db, table );
2598 r = msi_table_find_row( tv, rec, &row );
2599 if (r != ERROR_SUCCESS)
2600 ERR("no matching row to transform\n");
2603 TRACE("modifying row [%d]:\n", row);
2604 TABLE_set_row( &tv->view, row, rec, mask );
2608 TRACE("deleting row [%d]:\n", row);
2609 TABLE_delete_row( &tv->view, row );
2612 if( TRACE_ON(msidb) ) dump_record( rec );
2613 msiobj_release( &rec->hdr );
2620 /* no need to free the table, it's associated with the database */
2621 msi_free( rawdata );
2623 tv->view.ops->delete( &tv->view );
2625 return ERROR_SUCCESS;
2629 * msi_table_apply_transform
2631 * Enumerate the table transforms in a transform storage and apply each one.
2633 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2635 struct list transforms;
2636 IEnumSTATSTG *stgenum = NULL;
2637 TRANSFORMDATA *transform;
2638 TRANSFORMDATA *tables = NULL, *columns = NULL;
2641 string_table *strings;
2642 UINT ret = ERROR_FUNCTION_FAILED;
2643 UINT bytes_per_strref;
2645 TRACE("%p %p\n", db, stg );
2647 strings = msi_load_string_table( stg, &bytes_per_strref );
2651 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2655 list_init(&transforms);
2659 MSITABLEVIEW *tv = NULL;
2663 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2664 if ( FAILED( r ) || !count )
2667 decode_streamname( stat.pwcsName, name );
2668 CoTaskMemFree( stat.pwcsName );
2669 if ( name[0] != 0x4840 )
2672 if ( !lstrcmpW( name+1, szStringPool ) ||
2673 !lstrcmpW( name+1, szStringData ) )
2676 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2680 list_add_tail( &transforms, &transform->entry );
2682 transform->name = strdupW( name + 1 );
2684 if ( !lstrcmpW( transform->name, szTables ) )
2686 else if (!lstrcmpW( transform->name, szColumns ) )
2687 columns = transform;
2689 TRACE("transform contains stream %s\n", debugstr_w(name));
2691 /* load the table */
2692 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2693 if( r != ERROR_SUCCESS )
2696 r = tv->view.ops->execute( &tv->view, NULL );
2697 if( r != ERROR_SUCCESS )
2699 tv->view.ops->delete( &tv->view );
2703 tv->view.ops->delete( &tv->view );
2707 * Apply _Tables and _Columns transforms first so that
2708 * the table metadata is correct, and empty tables exist.
2710 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2711 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2714 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2715 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2718 ret = ERROR_SUCCESS;
2720 while ( !list_empty( &transforms ) )
2722 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2724 if ( lstrcmpW( transform->name, szColumns ) &&
2725 lstrcmpW( transform->name, szTables ) &&
2726 ret == ERROR_SUCCESS )
2728 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2731 list_remove( &transform->entry );
2732 msi_free( transform->name );
2733 msi_free( transform );
2736 if ( ret == ERROR_SUCCESS )
2737 append_storage_to_db( db, stg );
2741 IEnumSTATSTG_Release( stgenum );
2743 msi_destroy_stringtable( strings );
2748 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
2752 t = msi_alloc( sizeof *t );
2754 IStorage_AddRef( stg );
2755 list_add_tail( &db->transforms, &t->entry );
2758 void msi_free_transforms( MSIDATABASE *db )
2760 while( !list_empty( &db->transforms ) )
2762 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2763 MSITRANSFORM, entry );
2764 list_remove( &t->entry );
2765 IStorage_Release( t->stg );