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
62 MSICOLUMNHASHENTRY **hash_table;
65 typedef struct tagMSIORDERINFO
76 BYTE **nonpersistent_data;
77 UINT nonpersistent_row_count;
79 MSICOLUMNINFO *colinfo;
86 typedef struct tagMSITRANSFORM {
91 static const WCHAR szStringData[] = {
92 '_','S','t','r','i','n','g','D','a','t','a',0 };
93 static const WCHAR szStringPool[] = {
94 '_','S','t','r','i','n','g','P','o','o','l',0 };
96 /* information for default tables */
97 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
98 static WCHAR szTable[] = { 'T','a','b','l','e',0 };
99 static WCHAR szName[] = { 'N','a','m','e',0 };
100 static WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
101 static WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
102 static WCHAR szType[] = { 'T','y','p','e',0 };
104 /* These tables are written into (the .hash_table part).
105 * Do not mark them const.
107 static MSICOLUMNINFO _Columns_cols[4] = {
108 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0 },
109 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2 },
110 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
111 { szColumns, 4, szType, MSITYPE_VALID | 2, 6 },
113 static MSICOLUMNINFO _Tables_cols[1] = {
114 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
117 #define MAX_STREAM_NAME 0x1f
119 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
120 MSICOLUMNINFO **pcols, UINT *pcount );
121 static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count );
122 static UINT get_tablecolumns( MSIDATABASE *db,
123 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
124 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
127 void msi_table_set_strref(UINT bytes_per_strref)
129 _Columns_cols[0].offset = 0;
130 _Columns_cols[1].offset = bytes_per_strref;
131 _Columns_cols[2].offset = _Columns_cols[1].offset + sizeof(USHORT);
132 _Columns_cols[3].offset = _Columns_cols[2].offset + bytes_per_strref;
135 static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
137 if( MSITYPE_IS_BINARY(col->type) )
139 if( col->type & MSITYPE_STRING )
140 return _Columns_cols[1].offset;
141 if( (col->type & 0xff) > 4 )
142 ERR("Invalid column size!\n");
143 return col->type & 0xff;
146 static int utf2mime(int x)
148 if( (x>='0') && (x<='9') )
150 if( (x>='A') && (x<='Z') )
152 if( (x>='a') && (x<='z') )
161 LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
163 DWORD count = MAX_STREAM_NAME;
168 count = lstrlenW( in )+2;
169 out = msi_alloc( count*sizeof(WCHAR) );
185 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
187 ch = utf2mime(ch) + 0x4800;
189 if( next && (next<0x80) )
191 next = utf2mime(next);
202 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
207 static int mime2utf(int x)
214 return x - 10 - 26 + 'a';
215 if( x == (10+26+26) )
220 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
225 while ( (ch = *in++) )
227 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
230 ch = mime2utf(ch-0x4800);
234 *out++ = mime2utf(ch&0x3f);
236 ch = mime2utf((ch>>6)&0x3f);
246 void enum_stream_names( IStorage *stg )
248 IEnumSTATSTG *stgenum = NULL;
254 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
262 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
263 if( FAILED( r ) || !count )
265 decode_streamname( stat.pwcsName, name );
266 TRACE("stream %2d -> %s %s\n", n,
267 debugstr_w(stat.pwcsName), debugstr_w(name) );
268 CoTaskMemFree( stat.pwcsName );
272 IEnumSTATSTG_Release( stgenum );
275 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
276 BYTE **pdata, UINT *psz )
279 UINT ret = ERROR_FUNCTION_FAILED;
286 encname = encode_streamname(table, stname);
288 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
290 r = IStorage_OpenStream(stg, encname, NULL,
291 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
295 WARN("open stream failed r = %08x - empty table?\n", r);
299 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
302 WARN("open stream failed r = %08x!\n", r);
306 if( stat.cbSize.QuadPart >> 32 )
312 sz = stat.cbSize.QuadPart;
313 data = msi_alloc( sz );
316 WARN("couldn't allocate memory r=%08x!\n", r);
317 ret = ERROR_NOT_ENOUGH_MEMORY;
321 r = IStream_Read(stm, data, sz, &count );
322 if( FAILED( r ) || ( count != sz ) )
325 WARN("read stream failed r = %08x!\n", r);
334 IStream_Release( stm );
339 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
344 encname = encode_streamname(FALSE, stname);
346 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
348 r = IStorage_OpenStream(db->storage, encname, NULL,
349 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
352 MSITRANSFORM *transform;
354 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
356 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
357 r = IStorage_OpenStream( transform->stg, encname, NULL,
358 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
366 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
369 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
370 USHORT **pdata, UINT *psz )
373 UINT ret = ERROR_FUNCTION_FAILED;
379 r = db_get_raw_stream( db, stname, &stm );
380 if( r != ERROR_SUCCESS)
382 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
385 WARN("open stream failed r = %08x!\n", r);
389 if( stat.cbSize.QuadPart >> 32 )
395 sz = stat.cbSize.QuadPart;
396 data = msi_alloc( sz );
399 WARN("couldn't allocate memory r=%08x!\n", r);
400 ret = ERROR_NOT_ENOUGH_MEMORY;
404 r = IStream_Read(stm, data, sz, &count );
405 if( FAILED( r ) || ( count != sz ) )
408 WARN("read stream failed r = %08x!\n", r);
417 IStream_Release( stm );
422 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
423 LPCVOID data, UINT sz, BOOL bTable )
426 UINT ret = ERROR_FUNCTION_FAILED;
433 encname = encode_streamname(bTable, stname );
434 r = IStorage_OpenStream( stg, encname, NULL,
435 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
438 r = IStorage_CreateStream( stg, encname,
439 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
444 WARN("open stream failed r = %08x\n", r);
449 r = IStream_SetSize( stm, size );
452 WARN("Failed to SetSize\n");
457 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
460 WARN("Failed to Seek\n");
466 r = IStream_Write(stm, data, sz, &count );
467 if( FAILED( r ) || ( count != sz ) )
469 WARN("Failed to Write\n");
477 IStream_Release( stm );
482 static void free_table( MSITABLE *table )
485 for( i=0; i<table->row_count; i++ )
486 msi_free( table->data[i] );
487 msi_free( table->data );
488 for( i=0; i<table->nonpersistent_row_count; i++ )
489 msi_free( table->nonpersistent_data[i] );
490 msi_free( table->nonpersistent_data );
491 if( (table->colinfo != _Tables_cols) &&
492 (table->colinfo != _Columns_cols) )
494 msi_free_colinfo( table->colinfo, table->col_count );
495 msi_free( table->colinfo );
500 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
502 const MSICOLUMNINFO *last_col = &cols[count-1];
505 return last_col->offset + bytes_per_column( last_col );
508 /* add this table to the list of cached tables in the database */
509 static UINT read_table_from_storage( MSITABLE *t, IStorage *stg )
511 BYTE *rawdata = NULL;
512 UINT rawsize = 0, i, j, row_size = 0;
514 TRACE("%s\n",debugstr_w(t->name));
516 row_size = msi_table_get_row_size( t->colinfo, t->col_count );
518 /* if we can't read the table, just assume that it's empty */
519 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
521 return ERROR_SUCCESS;
523 TRACE("Read %d bytes\n", rawsize );
525 if( rawsize % row_size )
527 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
531 t->row_count = rawsize / row_size;
532 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
536 /* transpose all the data */
537 TRACE("Transposing data from %d rows\n", t->row_count );
538 for( i=0; i<t->row_count; i++ )
540 t->data[i] = msi_alloc( row_size );
544 for( j=0; j<t->col_count; j++ )
546 UINT ofs = t->colinfo[j].offset;
547 UINT n = bytes_per_column( &t->colinfo[j] );
550 if ( n != 2 && n != 3 && n != 4 )
552 ERR("oops - unknown column width %d\n", n);
556 for ( k = 0; k < n; k++ )
557 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
562 return ERROR_SUCCESS;
565 return ERROR_FUNCTION_FAILED;
568 void free_cached_tables( MSIDATABASE *db )
570 while( !list_empty( &db->tables ) )
572 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
574 list_remove( &t->entry );
579 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
583 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
584 if( !lstrcmpW( name, t->name ) )
590 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
592 UINT r, column_count = 0;
593 MSICOLUMNINFO *columns;
595 /* get the number of columns in this table */
597 r = get_tablecolumns( db, name, NULL, &column_count );
598 if( r != ERROR_SUCCESS )
601 /* if there's no columns, there's no table */
602 if( column_count == 0 )
603 return ERROR_INVALID_PARAMETER;
605 TRACE("Table %s found\n", debugstr_w(name) );
607 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
609 return ERROR_FUNCTION_FAILED;
611 r = get_tablecolumns( db, name, columns, &column_count );
612 if( r != ERROR_SUCCESS )
615 return ERROR_FUNCTION_FAILED;
619 *pcount = column_count;
624 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
625 BOOL persistent, MSITABLE **table_ret)
629 MSIRECORD *rec = NULL;
634 /* only add tables that don't exist already */
635 if( TABLE_Exists(db, name ) )
637 WARN("table %s exists\n", debugstr_w(name));
638 return ERROR_BAD_QUERY_SYNTAX;
641 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
643 return ERROR_FUNCTION_FAILED;
645 table->ref_count = 1;
646 table->row_count = 0;
648 table->nonpersistent_row_count = 0;
649 table->nonpersistent_data = NULL;
650 table->colinfo = NULL;
651 table->col_count = 0;
652 table->persistent = persistent;
653 lstrcpyW( table->name, name );
655 for( col = col_info; col; col = col->next )
658 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
662 return ERROR_FUNCTION_FAILED;
665 for( i = 0, col = col_info; col; i++, col = col->next )
667 table->colinfo[ i ].tablename = strdupW( col->table );
668 table->colinfo[ i ].number = i + 1;
669 table->colinfo[ i ].colname = strdupW( col->column );
670 table->colinfo[ i ].type = col->type;
671 table->colinfo[ i ].offset = 0;
672 table->colinfo[ i ].ref_count = 0;
673 table->colinfo[ i ].hash_table = NULL;
675 table_calc_column_offsets( table->colinfo, table->col_count);
677 r = TABLE_CreateView( db, szTables, &tv );
678 TRACE("CreateView returned %x\n", r);
685 r = tv->ops->execute( tv, 0 );
686 TRACE("tv execute returned %x\n", r);
690 rec = MSI_CreateRecord( 1 );
694 r = MSI_RecordSetStringW( rec, 1, name );
698 r = tv->ops->insert_row( tv, rec, !persistent );
699 TRACE("insert_row returned %x\n", r);
703 tv->ops->delete( tv );
706 msiobj_release( &rec->hdr );
711 /* add each column to the _Columns table */
712 r = TABLE_CreateView( db, szColumns, &tv );
716 r = tv->ops->execute( tv, 0 );
717 TRACE("tv execute returned %x\n", r);
721 rec = MSI_CreateRecord( 4 );
725 r = MSI_RecordSetStringW( rec, 1, name );
730 * need to set the table, column number, col name and type
731 * for each column we enter in the table
734 for( col = col_info; col; col = col->next )
736 r = MSI_RecordSetInteger( rec, 2, nField );
740 r = MSI_RecordSetStringW( rec, 3, col->column );
744 r = MSI_RecordSetInteger( rec, 4, col->type );
748 r = tv->ops->insert_row( tv, rec, FALSE );
760 msiobj_release( &rec->hdr );
761 /* FIXME: remove values from the string table on error */
763 tv->ops->delete( tv );
765 if (r == ERROR_SUCCESS)
767 list_add_head( &db->tables, &table->entry );
776 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
781 /* first, see if the table is cached */
782 table = find_cached_table( db, name );
786 return ERROR_SUCCESS;
789 /* nonexistent tables should be interpreted as empty tables */
790 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
792 return ERROR_FUNCTION_FAILED;
794 table->row_count = 0;
796 table->nonpersistent_row_count = 0;
797 table->nonpersistent_data = NULL;
798 table->colinfo = NULL;
799 table->col_count = 0;
800 table->persistent = TRUE;
801 lstrcpyW( table->name, name );
803 /* these two tables are special - we know the column types already */
804 if( !lstrcmpW( name, szColumns ) )
806 table->colinfo = _Columns_cols;
807 table->col_count = sizeof(_Columns_cols)/sizeof(_Columns_cols[0]);
809 else if( !lstrcmpW( name, szTables ) )
811 table->colinfo = _Tables_cols;
812 table->col_count = sizeof(_Tables_cols)/sizeof(_Tables_cols[0]);
816 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
817 if (r != ERROR_SUCCESS)
819 free_table ( table );
824 r = read_table_from_storage( table, db->storage );
825 if( r != ERROR_SUCCESS )
831 list_add_head( &db->tables, &table->entry );
833 return ERROR_SUCCESS;
836 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
838 BYTE *rawdata = NULL, *p;
839 UINT rawsize, r, i, j, row_size;
841 /* Nothing to do for non-persistent tables */
843 return ERROR_SUCCESS;
845 TRACE("Saving %s\n", debugstr_w( t->name ) );
847 row_size = msi_table_get_row_size( t->colinfo, t->col_count );
849 rawsize = t->row_count * row_size;
850 rawdata = msi_alloc_zero( rawsize );
853 r = ERROR_NOT_ENOUGH_MEMORY;
858 for( i=0; i<t->col_count; i++ )
860 for( j=0; j<t->row_count; j++ )
862 UINT offset = t->colinfo[i].offset;
864 *p++ = t->data[j][offset];
865 *p++ = t->data[j][offset + 1];
866 if( 4 == bytes_per_column( &t->colinfo[i] ) )
868 *p++ = t->data[j][offset + 2];
869 *p++ = t->data[j][offset + 3];
874 TRACE("writing %d bytes\n", rawsize);
875 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
883 static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count )
887 for( i=0; colinfo && (i<count); i++ )
889 assert( (i+1) == colinfo[ i ].number );
891 colinfo[i].offset = colinfo[ i - 1 ].offset
892 + bytes_per_column( &colinfo[ i - 1 ] );
894 colinfo[i].offset = 0;
895 TRACE("column %d is [%s] with type %08x ofs %d\n",
896 colinfo[i].number, debugstr_w(colinfo[i].colname),
897 colinfo[i].type, colinfo[i].offset);
901 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
903 const MSICOLUMNINFO *p;
906 TRACE("%s\n", debugstr_w(name));
908 if (!lstrcmpW( name, szTables ))
913 else if (!lstrcmpW( name, szColumns ))
919 return ERROR_FUNCTION_FAILED;
921 /* duplicate the string data so we can free it in msi_free_colinfo */
924 if (colinfo && (i < *sz) )
926 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
927 colinfo[i].tablename = strdupW( p[i].tablename );
928 colinfo[i].colname = strdupW( p[i].colname );
930 if( colinfo && (i >= *sz) )
933 table_calc_column_offsets( colinfo, n );
935 return ERROR_SUCCESS;
938 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
942 for( i=0; i<count; i++ )
944 msi_free( colinfo[i].tablename );
945 msi_free( colinfo[i].colname );
946 msi_free( colinfo[i].hash_table );
950 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
952 return strdupW(msi_string_lookup_id( db->strings, stringid ));
955 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
959 for (i = 0; i < bytes; i++)
960 ret += (data[row][col + i] << i * 8);
965 static UINT get_tablecolumns( MSIDATABASE *db,
966 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
968 UINT r, i, n=0, table_id, count, maxcount = *sz;
969 MSITABLE *table = NULL;
971 TRACE("%s\n", debugstr_w(szTableName));
973 /* first check if there is a default table with that name */
974 r = get_defaulttablecolumns( szTableName, colinfo, sz );
975 if( ( r == ERROR_SUCCESS ) && *sz )
978 r = get_table( db, szColumns, &table );
979 if( r != ERROR_SUCCESS )
981 ERR("couldn't load _Columns table\n");
982 return ERROR_FUNCTION_FAILED;
985 /* convert table and column names to IDs from the string table */
986 r = msi_string2idW( db->strings, szTableName, &table_id );
987 if( r != ERROR_SUCCESS )
989 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
993 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
995 /* Note: _Columns table doesn't have non-persistent data */
997 /* if maxcount is non-zero, assume it's exactly right for this table */
998 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
999 count = table->row_count;
1000 for( i=0; i<count; i++ )
1002 if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
1006 UINT id = read_table_int(table->data, i, _Columns_cols[2].offset, db->bytes_per_strref);
1007 UINT col = read_table_int(table->data, i, _Columns_cols[1].offset, sizeof(USHORT)) - (1<<15);
1009 /* check the column number is in range */
1010 if (col<1 || col>maxcount)
1012 ERR("column %d out of range\n", col);
1016 /* check if this column was already set */
1017 if (colinfo[ col - 1 ].number)
1019 ERR("duplicate column %d\n", col);
1023 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
1024 colinfo[ col - 1 ].number = col;
1025 colinfo[ col - 1 ].colname = msi_makestring( db, id );
1026 colinfo[ col - 1 ].type = read_table_int(table->data, i, _Columns_cols[3].offset, sizeof(USHORT)) - (1<<15);
1027 colinfo[ col - 1 ].offset = 0;
1028 colinfo[ col - 1 ].ref_count = 0;
1029 colinfo[ col - 1 ].hash_table = NULL;
1034 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
1036 if (colinfo && n != maxcount)
1038 ERR("missing column in table %s\n", debugstr_w(szTableName));
1039 msi_free_colinfo(colinfo, maxcount );
1040 return ERROR_FUNCTION_FAILED;
1043 table_calc_column_offsets( colinfo, n );
1046 return ERROR_SUCCESS;
1049 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
1052 UINT size, offset, old_count;
1055 table = find_cached_table( db, name );
1056 old_count = table->col_count;
1057 msi_free( table->colinfo );
1058 table_get_column_info( db, name, &table->colinfo, &table->col_count );
1060 size = msi_table_get_row_size( table->colinfo, table->col_count );
1061 offset = table->colinfo[table->col_count - 1].offset;
1063 for ( n = 0; n < table->row_count; n++ )
1065 table->data[n] = msi_realloc( table->data[n], size );
1066 if (old_count < table->col_count)
1067 memset( &table->data[n][offset], 0, size - offset );
1071 /* try to find the table name in the _Tables table */
1072 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1074 UINT r, table_id = 0, i, count;
1075 MSITABLE *table = NULL;
1077 if( !lstrcmpW( name, szTables ) )
1079 if( !lstrcmpW( name, szColumns ) )
1082 r = msi_string2idW( db->strings, name, &table_id );
1083 if( r != ERROR_SUCCESS )
1085 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1089 r = get_table( db, szTables, &table );
1090 if( r != ERROR_SUCCESS )
1092 ERR("table %s not available\n", debugstr_w(szTables));
1096 count = table->row_count;
1097 for( i=0; i<count; i++ )
1098 if( table->data[ i ][ 0 ] == table_id )
1104 count = table->nonpersistent_row_count;
1105 for( i=0; i<count; i++ )
1106 if( table->nonpersistent_data[ i ][ 0 ] == table_id )
1112 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1117 /* below is the query interface to a table */
1119 typedef struct tagMSITABLEVIEW
1124 MSICOLUMNINFO *columns;
1125 MSIORDERINFO *order;
1131 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1133 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1138 return ERROR_INVALID_PARAMETER;
1140 if( (col==0) || (col>tv->num_cols) )
1141 return ERROR_INVALID_PARAMETER;
1143 /* how many rows are there ? */
1144 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1145 return ERROR_NO_MORE_ITEMS;
1147 if( tv->columns[col-1].offset >= tv->row_size )
1149 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1150 ERR("%p %p\n", tv, tv->columns );
1151 return ERROR_FUNCTION_FAILED;
1155 row = tv->order->reorder[row];
1157 if (row >= tv->table->row_count)
1159 row -= tv->table->row_count;
1160 data = tv->table->nonpersistent_data;
1163 data = tv->table->data;
1165 n = bytes_per_column( &tv->columns[col-1] );
1166 if (n != 2 && n != 3 && n != 4)
1168 ERR("oops! what is %d bytes per column?\n", n );
1169 return ERROR_FUNCTION_FAILED;
1172 offset = tv->columns[col-1].offset;
1173 *val = read_table_int(data, row, offset, n);
1175 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1177 return ERROR_SUCCESS;
1181 * We need a special case for streams, as we need to reference column with
1182 * the name of the stream in the same table, and the table name
1183 * which may not be available at higher levels of the query
1185 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1187 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1188 UINT ival = 0, refcol = 0, r;
1192 static const WCHAR szDot[] = { '.', 0 };
1195 if( !view->ops->fetch_int )
1196 return ERROR_INVALID_PARAMETER;
1199 * The column marked with the type stream data seems to have a single number
1200 * which references the column containing the name of the stream data
1202 * Fetch the column to reference first.
1204 r = view->ops->fetch_int( view, row, col, &ival );
1205 if( r != ERROR_SUCCESS )
1208 /* check the column value is in range */
1209 if (ival > tv->num_cols || ival == col)
1211 ERR("bad column ref (%u) for stream\n", ival);
1212 return ERROR_FUNCTION_FAILED;
1215 if ( tv->columns[ival - 1].type & MSITYPE_STRING )
1217 /* now get the column with the name of the stream */
1218 r = view->ops->fetch_int( view, row, ival, &refcol );
1219 if ( r != ERROR_SUCCESS )
1222 /* lookup the string value from the string table */
1223 sval = msi_string_lookup_id( tv->db->strings, refcol );
1225 return ERROR_INVALID_PARAMETER;
1229 static const WCHAR fmt[] = { '%','d',0 };
1230 sprintfW( number, fmt, ival );
1234 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1235 full_name = msi_alloc( len*sizeof(WCHAR) );
1236 lstrcpyW( full_name, tv->name );
1237 lstrcatW( full_name, szDot );
1238 lstrcatW( full_name, sval );
1240 r = db_get_raw_stream( tv->db, full_name, stm );
1242 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1243 msi_free( full_name );
1248 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1254 return ERROR_INVALID_PARAMETER;
1256 if( (col==0) || (col>tv->num_cols) )
1257 return ERROR_INVALID_PARAMETER;
1259 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1260 return ERROR_INVALID_PARAMETER;
1262 if( tv->columns[col-1].offset >= tv->row_size )
1264 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1265 ERR("%p %p\n", tv, tv->columns );
1266 return ERROR_FUNCTION_FAILED;
1269 msi_free( tv->columns[col-1].hash_table );
1270 tv->columns[col-1].hash_table = NULL;
1272 if (row >= tv->table->row_count)
1274 row -= tv->table->row_count;
1275 data = tv->table->nonpersistent_data;
1278 data = tv->table->data;
1280 n = bytes_per_column( &tv->columns[col-1] );
1281 if ( n != 2 && n != 3 && n != 4 )
1283 ERR("oops! what is %d bytes per column?\n", n );
1284 return ERROR_FUNCTION_FAILED;
1287 offset = tv->columns[col-1].offset;
1288 for ( i = 0; i < n; i++ )
1289 data[row][offset + i] = (val >> i * 8) & 0xff;
1291 return ERROR_SUCCESS;
1294 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1296 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1299 return ERROR_INVALID_PARAMETER;
1302 row = tv->order->reorder[row];
1304 return msi_view_get_row(tv->db, view, row, rec);
1307 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1309 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1310 UINT i, val, r = ERROR_SUCCESS;
1313 return ERROR_INVALID_PARAMETER;
1315 /* test if any of the mask bits are invalid */
1316 if ( mask >= (1<<tv->num_cols) )
1317 return ERROR_INVALID_PARAMETER;
1319 for ( i = 0; i < tv->num_cols; i++ )
1323 /* only update the fields specified in the mask */
1324 if ( !(mask&(1<<i)) )
1327 /* if row >= tv->table->row_count then it is a non-persistent row */
1328 persistent = tv->table->persistent && (row < tv->table->row_count);
1329 /* FIXME: should we allow updating keys? */
1332 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1334 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1336 val = 1; /* refers to the first key column */
1338 else if ( tv->columns[i].type & MSITYPE_STRING )
1340 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1343 r = msi_string2idW(tv->db->strings, sval, &ival);
1344 if (r == ERROR_SUCCESS)
1346 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1351 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
1352 persistent ? StringPersistent : StringNonPersistent );
1354 else if ( 2 == bytes_per_column( &tv->columns[ i ] ) )
1356 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1357 if ( val & 0xffff0000 )
1359 ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1360 return ERROR_FUNCTION_FAILED;
1365 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1366 val = ival ^ 0x80000000;
1370 r = TABLE_set_int( tv, row, i+1, val );
1371 if ( r != ERROR_SUCCESS )
1377 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1379 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1385 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1388 return ERROR_INVALID_PARAMETER;
1390 row = msi_alloc_zero( tv->row_size );
1392 return ERROR_NOT_ENOUGH_MEMORY;
1396 row_count = &tv->table->nonpersistent_row_count;
1397 data_ptr = &tv->table->nonpersistent_data;
1398 *num = tv->table->row_count + tv->table->nonpersistent_row_count;
1402 row_count = &tv->table->row_count;
1403 data_ptr = &tv->table->data;
1404 *num = tv->table->row_count;
1407 sz = (*row_count + 1) * sizeof (BYTE*);
1409 p = msi_realloc( *data_ptr, sz );
1411 p = msi_alloc( sz );
1415 return ERROR_NOT_ENOUGH_MEMORY;
1419 (*data_ptr)[*row_count] = row;
1422 return ERROR_SUCCESS;
1425 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1427 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1429 TRACE("%p %p\n", tv, record);
1431 TRACE("There are %d columns\n", tv->num_cols );
1433 return ERROR_SUCCESS;
1436 static UINT TABLE_close( struct tagMSIVIEW *view )
1438 TRACE("%p\n", view );
1440 return ERROR_SUCCESS;
1443 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1445 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1447 TRACE("%p %p %p\n", view, rows, cols );
1450 *cols = tv->num_cols;
1454 return ERROR_INVALID_PARAMETER;
1455 *rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1458 return ERROR_SUCCESS;
1461 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1462 UINT n, LPWSTR *name, UINT *type )
1464 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1466 TRACE("%p %d %p %p\n", tv, n, name, type );
1468 if( ( n == 0 ) || ( n > tv->num_cols ) )
1469 return ERROR_INVALID_PARAMETER;
1473 *name = strdupW( tv->columns[n-1].colname );
1475 return ERROR_FUNCTION_FAILED;
1478 *type = tv->columns[n-1].type;
1480 return ERROR_SUCCESS;
1483 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1485 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1489 /* check there's no null values where they're not allowed */
1490 for( i = 0; i < tv->num_cols; i++ )
1492 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1495 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1496 TRACE("skipping binary column\n");
1497 else if ( tv->columns[i].type & MSITYPE_STRING )
1501 str = MSI_RecordGetString( rec, i+1 );
1502 if (str == NULL || str[0] == 0)
1503 return ERROR_INVALID_DATA;
1509 n = MSI_RecordGetInteger( rec, i+1 );
1510 if (n == MSI_NULL_INTEGER)
1511 return ERROR_INVALID_DATA;
1515 /* check there's no duplicate keys */
1516 r = msi_table_find_row( tv, rec, &row );
1517 if (r == ERROR_SUCCESS)
1518 return ERROR_FUNCTION_FAILED;
1520 return ERROR_SUCCESS;
1523 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary )
1525 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1528 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1530 /* check that the key is unique - can we find a matching row? */
1531 r = table_validate_new( tv, rec );
1532 if( r != ERROR_SUCCESS )
1533 return ERROR_FUNCTION_FAILED;
1535 r = table_create_new_row( view, &row, temporary );
1536 TRACE("insert_row returned %08x\n", r);
1537 if( r != ERROR_SUCCESS )
1540 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1543 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1545 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1546 UINT r, num_rows, num_cols, i;
1549 TRACE("%p %d\n", tv, row);
1552 return ERROR_INVALID_PARAMETER;
1554 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1555 if ( r != ERROR_SUCCESS )
1558 if ( row >= num_rows )
1559 return ERROR_FUNCTION_FAILED;
1561 if ( row < tv->table->row_count )
1563 num_rows = tv->table->row_count;
1564 tv->table->row_count--;
1565 data = tv->table->data;
1569 num_rows = tv->table->nonpersistent_row_count;
1570 row -= tv->table->row_count;
1571 tv->table->nonpersistent_row_count--;
1572 data = tv->table->nonpersistent_data;
1575 if ( row == num_rows - 1 )
1576 return ERROR_SUCCESS;
1578 for (i = row + 1; i < num_rows; i++)
1579 memcpy(data[i - 1], data[i], tv->row_size);
1581 return ERROR_SUCCESS;
1584 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1586 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1589 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1590 * sets the fetched record apart from other records
1594 return ERROR_INVALID_PARAMETER;
1596 r = msi_table_find_row(tv, rec, &new_row);
1597 if (r != ERROR_SUCCESS)
1599 ERR("can't find row to modify\n");
1600 return ERROR_FUNCTION_FAILED;
1603 /* the row cannot be changed */
1604 if (row != new_row + 1)
1605 return ERROR_FUNCTION_FAILED;
1607 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1610 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1612 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1615 r = msi_table_find_row(tv, rec, &row);
1616 if (r != ERROR_SUCCESS)
1619 return TABLE_delete_row(view, row);
1622 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1623 MSIRECORD *rec, UINT row)
1625 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1628 TRACE("%p %d %p\n", view, eModifyMode, rec );
1630 switch (eModifyMode)
1632 case MSIMODIFY_DELETE:
1633 r = modify_delete_row( view, rec );
1635 case MSIMODIFY_VALIDATE_NEW:
1636 r = table_validate_new( tv, rec );
1639 case MSIMODIFY_INSERT:
1640 r = table_validate_new( tv, rec );
1641 if (r != ERROR_SUCCESS)
1643 r = TABLE_insert_row( view, rec, FALSE );
1646 case MSIMODIFY_INSERT_TEMPORARY:
1647 r = table_validate_new( tv, rec );
1648 if (r != ERROR_SUCCESS)
1650 r = TABLE_insert_row( view, rec, TRUE );
1653 case MSIMODIFY_UPDATE:
1654 r = msi_table_update( view, rec, row );
1657 case MSIMODIFY_REFRESH:
1658 case MSIMODIFY_ASSIGN:
1659 case MSIMODIFY_REPLACE:
1660 case MSIMODIFY_MERGE:
1661 case MSIMODIFY_VALIDATE:
1662 case MSIMODIFY_VALIDATE_FIELD:
1663 case MSIMODIFY_VALIDATE_DELETE:
1664 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1665 r = ERROR_CALL_NOT_IMPLEMENTED;
1669 r = ERROR_INVALID_DATA;
1675 static UINT TABLE_delete( struct tagMSIVIEW *view )
1677 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1679 TRACE("%p\n", view );
1686 msi_free( tv->order->reorder );
1687 msi_free( tv->order );
1693 return ERROR_SUCCESS;
1696 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1697 UINT val, UINT *row, MSIITERHANDLE *handle )
1699 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1700 const MSICOLUMNHASHENTRY *entry;
1702 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1705 return ERROR_INVALID_PARAMETER;
1707 if( (col==0) || (col > tv->num_cols) )
1708 return ERROR_INVALID_PARAMETER;
1710 if( !tv->columns[col-1].hash_table )
1713 UINT num_rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1714 MSICOLUMNHASHENTRY **hash_table;
1715 MSICOLUMNHASHENTRY *new_entry;
1717 if( tv->columns[col-1].offset >= tv->row_size )
1719 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1720 ERR("%p %p\n", tv, tv->columns );
1721 return ERROR_FUNCTION_FAILED;
1724 /* allocate contiguous memory for the table and its entries so we
1725 * don't have to do an expensive cleanup */
1726 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1727 num_rows * sizeof(MSICOLUMNHASHENTRY));
1729 return ERROR_OUTOFMEMORY;
1731 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1732 tv->columns[col-1].hash_table = hash_table;
1734 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1736 for (i = 0; i < num_rows; i++, new_entry++)
1740 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1743 new_entry->next = NULL;
1744 new_entry->value = row_value;
1746 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1748 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1749 while (prev_entry->next)
1750 prev_entry = prev_entry->next;
1751 prev_entry->next = new_entry;
1754 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1759 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1761 entry = (*handle)->next;
1763 while (entry && entry->value != val)
1764 entry = entry->next;
1768 return ERROR_NO_MORE_ITEMS;
1773 *row = tv->order->reorder[*row];
1775 return ERROR_SUCCESS;
1778 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1780 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1783 TRACE("%p %d\n", view, tv->table->ref_count);
1785 for (i = 0; i < tv->table->col_count; i++)
1787 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1788 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
1791 return InterlockedIncrement(&tv->table->ref_count);
1794 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
1796 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1797 MSIRECORD *rec = NULL;
1798 MSIVIEW *columns = NULL;
1801 rec = MSI_CreateRecord(2);
1803 return ERROR_OUTOFMEMORY;
1805 MSI_RecordSetStringW(rec, 1, table);
1806 MSI_RecordSetInteger(rec, 2, number);
1808 r = TABLE_CreateView(tv->db, szColumns, &columns);
1809 if (r != ERROR_SUCCESS)
1812 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row);
1813 if (r != ERROR_SUCCESS)
1816 r = TABLE_delete_row(columns, row);
1817 if (r != ERROR_SUCCESS)
1820 msi_update_table_columns(tv->db, table);
1823 msiobj_release(&rec->hdr);
1824 if (columns) columns->ops->delete(columns);
1828 static UINT TABLE_release(struct tagMSIVIEW *view)
1830 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1831 INT ref = tv->table->ref_count;
1835 TRACE("%p %d\n", view, ref);
1837 for (i = 0; i < tv->table->col_count; i++)
1839 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1841 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
1844 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
1845 tv->table->colinfo[i].number);
1846 if (r != ERROR_SUCCESS)
1852 ref = InterlockedDecrement(&tv->table->ref_count);
1855 if (!tv->table->row_count)
1857 list_remove(&tv->table->entry);
1858 free_table(tv->table);
1866 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
1867 LPCWSTR column, UINT type, BOOL hold)
1869 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1874 rec = MSI_CreateRecord(4);
1876 return ERROR_OUTOFMEMORY;
1878 MSI_RecordSetStringW(rec, 1, table);
1879 MSI_RecordSetInteger(rec, 2, number);
1880 MSI_RecordSetStringW(rec, 3, column);
1881 MSI_RecordSetInteger(rec, 4, type);
1883 r = TABLE_insert_row(&tv->view, rec, FALSE);
1884 if (r != ERROR_SUCCESS)
1887 msi_update_table_columns(tv->db, table);
1892 msitable = find_cached_table(tv->db, table);
1893 for (i = 0; i < msitable->col_count; i++)
1895 if (!lstrcmpW(msitable->colinfo[i].colname, column))
1897 InterlockedIncrement(&msitable->colinfo[i].ref_count);
1903 msiobj_release(&rec->hdr);
1907 static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
1911 r = TABLE_get_dimensions(view, NULL, &count);
1912 if (r != ERROR_SUCCESS)
1915 if (order->num_cols >= count)
1916 return ERROR_FUNCTION_FAILED;
1918 r = VIEW_find_column(view, name, &n);
1919 if (r != ERROR_SUCCESS)
1922 order->cols[order->num_cols] = n;
1923 TRACE("Ordering by column %s (%d)\n", debugstr_w(name), n);
1927 return ERROR_SUCCESS;
1930 static UINT order_compare(struct tagMSIVIEW *view, MSIORDERINFO *order,
1931 UINT a, UINT b, UINT *swap)
1933 UINT r, i, a_val = 0, b_val = 0;
1936 for (i = 0; i < order->num_cols; i++)
1938 r = TABLE_fetch_int(view, a, order->cols[i], &a_val);
1939 if (r != ERROR_SUCCESS)
1942 r = TABLE_fetch_int(view, b, order->cols[i], &b_val);
1943 if (r != ERROR_SUCCESS)
1954 return ERROR_SUCCESS;
1957 static UINT order_mergesort(struct tagMSIVIEW *view, MSIORDERINFO *order,
1958 UINT left, UINT right)
1961 UINT swap = 0, center = (left + right) / 2;
1962 UINT *array = order->reorder;
1965 return ERROR_SUCCESS;
1967 /* sort the left half */
1968 r = order_mergesort(view, order, left, center);
1969 if (r != ERROR_SUCCESS)
1972 /* sort the right half */
1973 r = order_mergesort(view, order, center + 1, right);
1974 if (r != ERROR_SUCCESS)
1977 for (i = left, j = center + 1; (i <= center) && (j <= right); i++)
1979 r = order_compare(view, order, array[i], array[j], &swap);
1980 if (r != ERROR_SUCCESS)
1986 memmove(&array[i + 1], &array[i], (j - i) * sizeof(UINT));
1993 return ERROR_SUCCESS;
1996 static UINT order_verify(struct tagMSIVIEW *view, MSIORDERINFO *order, UINT num_rows)
2000 for (i = 1; i < num_rows; i++)
2002 r = order_compare(view, order, order->reorder[i - 1],
2003 order->reorder[i], &swap);
2004 if (r != ERROR_SUCCESS)
2010 ERR("Bad order! %d\n", i);
2011 return ERROR_FUNCTION_FAILED;
2014 return ERROR_SUCCESS;
2017 static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
2019 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2020 MSIORDERINFO *order;
2025 TRACE("sorting table %s\n", debugstr_w(tv->name));
2027 r = TABLE_get_dimensions(view, &rows, &cols);
2028 if (r != ERROR_SUCCESS)
2031 order = msi_alloc_zero(sizeof(MSIORDERINFO) + sizeof(UINT) * cols);
2033 return ERROR_OUTOFMEMORY;
2035 for (ptr = columns; ptr; ptr = ptr->next)
2036 order_add_column(view, order, ptr->column);
2038 order->reorder = msi_alloc(rows * sizeof(UINT));
2039 if (!order->reorder)
2040 return ERROR_OUTOFMEMORY;
2042 for (i = 0; i < rows; i++)
2043 order->reorder[i] = i;
2045 r = order_mergesort(view, order, 0, rows - 1);
2046 if (r != ERROR_SUCCESS)
2049 r = order_verify(view, order, rows);
2050 if (r != ERROR_SUCCESS)
2055 return ERROR_SUCCESS;
2058 static const MSIVIEWOPS table_ops =
2068 TABLE_get_dimensions,
2069 TABLE_get_column_info,
2072 TABLE_find_matching_rows,
2076 TABLE_remove_column,
2080 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2085 static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
2087 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2089 if ( !lstrcmpW( name, Streams ) )
2090 return STREAMS_CreateView( db, view );
2092 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
2093 tv = msi_alloc_zero( sz );
2095 return ERROR_FUNCTION_FAILED;
2097 r = get_table( db, name, &tv->table );
2098 if( r != ERROR_SUCCESS )
2101 WARN("table not found\n");
2105 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2107 /* fill the structure */
2108 tv->view.ops = &table_ops;
2110 tv->columns = tv->table->colinfo;
2111 tv->num_cols = tv->table->col_count;
2112 tv->row_size = msi_table_get_row_size( tv->table->colinfo, tv->table->col_count );
2114 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2116 *view = (MSIVIEW*) tv;
2117 lstrcpyW( tv->name, name );
2119 return ERROR_SUCCESS;
2122 UINT MSI_CommitTables( MSIDATABASE *db )
2125 MSITABLE *table = NULL;
2129 r = msi_save_string_table( db->strings, db->storage );
2130 if( r != ERROR_SUCCESS )
2132 WARN("failed to save string table r=%08x\n",r);
2136 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2138 r = save_table( db, table );
2139 if( r != ERROR_SUCCESS )
2141 WARN("failed to save table %s (r=%08x)\n",
2142 debugstr_w(table->name), r);
2147 /* force everything to reload next time */
2148 free_cached_tables( db );
2150 return ERROR_SUCCESS;
2153 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2158 TRACE("%p %s\n", db, debugstr_w(table));
2161 return MSICONDITION_ERROR;
2163 r = get_table( db, table, &t );
2164 if (r != ERROR_SUCCESS)
2165 return MSICONDITION_NONE;
2168 return MSICONDITION_TRUE;
2170 return MSICONDITION_FALSE;
2173 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2177 for (i = 0; i < bytes; i++)
2178 ret += (data[col + i] << i * 8);
2183 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2184 const BYTE *rawdata, UINT bytes_per_strref )
2186 UINT i, val, ofs = 0;
2188 MSICOLUMNINFO *columns = tv->columns;
2191 mask = rawdata[0] | (rawdata[1] << 8);
2194 rec = MSI_CreateRecord( tv->num_cols );
2199 for( i=0; i<tv->num_cols; i++ )
2201 if ( (mask&1) && (i>=(mask>>8)) )
2203 /* all keys must be present */
2204 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2207 if( (columns[i].type & MSITYPE_STRING) &&
2208 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2212 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2213 sval = msi_string_lookup_id( st, val );
2214 MSI_RecordSetStringW( rec, i+1, sval );
2215 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
2216 ofs += bytes_per_strref;
2220 UINT n = bytes_per_column( &columns[i] );
2224 val = read_raw_int(rawdata, ofs, n);
2226 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
2227 TRACE(" field %d [0x%04x]\n", i+1, val );
2230 val = read_raw_int(rawdata, ofs, n);
2232 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2233 TRACE(" field %d [0x%08x]\n", i+1, val );
2236 ERR("oops - unknown column width %d\n", n);
2245 static void dump_record( MSIRECORD *rec )
2249 n = MSI_RecordGetFieldCount( rec );
2250 for( i=1; i<=n; i++ )
2252 LPCWSTR sval = MSI_RecordGetString( rec, i );
2254 if( MSI_RecordIsNull( rec, i ) )
2255 TRACE("row -> []\n");
2256 else if( (sval = MSI_RecordGetString( rec, i )) )
2257 TRACE("row -> [%s]\n", debugstr_w(sval));
2259 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2263 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2268 for( i=0; i<(rawsize/2); i++ )
2270 sval = msi_string_lookup_id( st, rawdata[i] );
2271 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
2275 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2280 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2281 for( i=0; i<tv->num_cols; i++ )
2285 if ( ~tv->columns[i].type & MSITYPE_KEY )
2288 /* turn the transform column value into a row value */
2289 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2290 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2292 str = MSI_RecordGetString( rec, i+1 );
2293 r = msi_string2idW( tv->db->strings, str, &data[i] );
2295 /* if there's no matching string in the string table,
2296 these keys can't match any record, so fail now. */
2297 if( ERROR_SUCCESS != r )
2305 data[i] = MSI_RecordGetInteger( rec, i+1 );
2306 if ((tv->columns[i].type&0xff) == 2)
2309 data[i] += 0x80000000;
2315 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
2317 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2319 for( i=0; i<tv->num_cols; i++ )
2321 if ( ~tv->columns[i].type & MSITYPE_KEY )
2324 /* turn the transform column value into a row value */
2325 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2326 if ( r != ERROR_SUCCESS )
2328 ERR("TABLE_fetch_int shouldn't fail here\n");
2332 /* if this key matches, move to the next column */
2335 ret = ERROR_FUNCTION_FAILED;
2339 ret = ERROR_SUCCESS;
2345 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
2347 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2349 data = msi_record_to_row( tv, rec );
2352 for( i = 0; i < tv->table->row_count + tv->table->nonpersistent_row_count; i++ )
2354 r = msi_row_matches( tv, i, data );
2355 if( r == ERROR_SUCCESS )
2371 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2372 string_table *st, TRANSFORMDATA *transform,
2373 UINT bytes_per_strref )
2376 BYTE *rawdata = NULL;
2377 MSITABLEVIEW *tv = NULL;
2378 UINT r, n, sz, i, mask;
2379 MSIRECORD *rec = NULL;
2385 return ERROR_SUCCESS;
2387 name = transform->name;
2390 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2392 /* read the transform data */
2393 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2396 TRACE("table %s empty\n", debugstr_w(name) );
2397 return ERROR_INVALID_TABLE;
2400 /* create a table view */
2401 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2402 if( r != ERROR_SUCCESS )
2405 r = tv->view.ops->execute( &tv->view, NULL );
2406 if( r != ERROR_SUCCESS )
2409 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2410 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2412 /* interpret the data */
2414 for( n=0; n < rawsize; )
2416 mask = rawdata[n] | (rawdata[n+1] << 8);
2421 * if the low bit is set, columns are continuous and
2422 * the number of columns is specified in the high byte
2425 for( i=0; i<tv->num_cols; i++ )
2427 if( (tv->columns[i].type & MSITYPE_STRING) &&
2428 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2429 sz += bytes_per_strref;
2431 sz += bytes_per_column( &tv->columns[i] );
2437 * If the low bit is not set, mask is a bitmask.
2438 * Excepting for key fields, which are always present,
2439 * each bit indicates that a field is present in the transform record.
2441 * mask == 0 is a special case ... only the keys will be present
2442 * and it means that this row should be deleted.
2445 for( i=0; i<tv->num_cols; i++ )
2447 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2449 if( (tv->columns[i].type & MSITYPE_STRING) &&
2450 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2451 sz += bytes_per_strref;
2453 sz += bytes_per_column( &tv->columns[i] );
2458 /* check we didn't run of the end of the table */
2459 if ( (n+sz) > rawsize )
2462 dump_table( st, (USHORT *)rawdata, rawsize );
2466 rec = msi_get_transform_record( tv, st, &rawdata[n], bytes_per_strref );
2473 UINT number = MSI_NULL_INTEGER;
2475 TRACE("inserting record\n");
2477 if (!lstrcmpW(name, szColumns))
2479 MSI_RecordGetStringW( rec, 1, table, &sz );
2480 number = MSI_RecordGetInteger( rec, 2 );
2483 * Native msi seems writes nul into the Number (2nd) column of
2484 * the _Columns table, only when the columns are from a new table
2486 if ( number == MSI_NULL_INTEGER )
2488 /* reset the column number on a new table */
2489 if ( lstrcmpW(coltable, table) )
2492 lstrcpyW( coltable, table );
2495 /* fix nul column numbers */
2496 MSI_RecordSetInteger( rec, 2, ++colcol );
2500 r = TABLE_insert_row( &tv->view, rec, FALSE );
2501 if (r != ERROR_SUCCESS)
2502 ERR("insert row failed\n");
2504 if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
2505 msi_update_table_columns( db, table );
2511 r = msi_table_find_row( tv, rec, &row );
2512 if (r != ERROR_SUCCESS)
2513 ERR("no matching row to transform\n");
2516 TRACE("modifying row [%d]:\n", row);
2517 TABLE_set_row( &tv->view, row, rec, mask );
2521 TRACE("deleting row [%d]:\n", row);
2522 TABLE_delete_row( &tv->view, row );
2525 if( TRACE_ON(msidb) ) dump_record( rec );
2526 msiobj_release( &rec->hdr );
2533 /* no need to free the table, it's associated with the database */
2534 msi_free( rawdata );
2536 tv->view.ops->delete( &tv->view );
2538 return ERROR_SUCCESS;
2542 * msi_table_apply_transform
2544 * Enumerate the table transforms in a transform storage and apply each one.
2546 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2548 struct list transforms;
2549 IEnumSTATSTG *stgenum = NULL;
2550 TRANSFORMDATA *transform;
2551 TRANSFORMDATA *tables = NULL, *columns = NULL;
2554 string_table *strings;
2555 UINT ret = ERROR_FUNCTION_FAILED;
2556 UINT bytes_per_strref;
2558 TRACE("%p %p\n", db, stg );
2560 strings = msi_load_string_table( stg, &bytes_per_strref );
2564 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2568 list_init(&transforms);
2572 MSITABLEVIEW *tv = NULL;
2576 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2577 if ( FAILED( r ) || !count )
2580 decode_streamname( stat.pwcsName, name );
2581 CoTaskMemFree( stat.pwcsName );
2582 if ( name[0] != 0x4840 )
2585 if ( !lstrcmpW( name+1, szStringPool ) ||
2586 !lstrcmpW( name+1, szStringData ) )
2589 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2593 list_add_tail( &transforms, &transform->entry );
2595 transform->name = strdupW( name + 1 );
2597 if ( !lstrcmpW( transform->name, szTables ) )
2599 else if (!lstrcmpW( transform->name, szColumns ) )
2600 columns = transform;
2602 TRACE("transform contains stream %s\n", debugstr_w(name));
2604 /* load the table */
2605 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2606 if( r != ERROR_SUCCESS )
2609 r = tv->view.ops->execute( &tv->view, NULL );
2610 if( r != ERROR_SUCCESS )
2612 tv->view.ops->delete( &tv->view );
2616 tv->view.ops->delete( &tv->view );
2620 * Apply _Tables and _Columns transforms first so that
2621 * the table metadata is correct, and empty tables exist.
2623 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2624 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2627 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2628 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2631 ret = ERROR_SUCCESS;
2633 while ( !list_empty( &transforms ) )
2635 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2637 if ( lstrcmpW( transform->name, szColumns ) &&
2638 lstrcmpW( transform->name, szTables ) &&
2639 ret == ERROR_SUCCESS )
2641 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2644 list_remove( &transform->entry );
2645 msi_free( transform->name );
2646 msi_free( transform );
2649 if ( ret == ERROR_SUCCESS )
2650 append_storage_to_db( db, stg );
2654 IEnumSTATSTG_Release( stgenum );
2656 msi_destroy_stringtable( strings );
2661 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
2665 t = msi_alloc( sizeof *t );
2667 IStorage_AddRef( stg );
2668 list_add_tail( &db->transforms, &t->entry );
2671 void msi_free_transforms( MSIDATABASE *db )
2673 while( !list_empty( &db->transforms ) )
2675 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2676 MSITRANSFORM, entry );
2677 list_remove( &t->entry );
2678 IStorage_Release( t->stg );