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) );
271 IEnumSTATSTG_Release( stgenum );
274 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
275 BYTE **pdata, UINT *psz )
278 UINT ret = ERROR_FUNCTION_FAILED;
285 encname = encode_streamname(table, stname);
287 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
289 r = IStorage_OpenStream(stg, encname, NULL,
290 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
294 WARN("open stream failed r = %08x - empty table?\n", r);
298 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
301 WARN("open stream failed r = %08x!\n", r);
305 if( stat.cbSize.QuadPart >> 32 )
311 sz = stat.cbSize.QuadPart;
312 data = msi_alloc( sz );
315 WARN("couldn't allocate memory r=%08x!\n", r);
316 ret = ERROR_NOT_ENOUGH_MEMORY;
320 r = IStream_Read(stm, data, sz, &count );
321 if( FAILED( r ) || ( count != sz ) )
324 WARN("read stream failed r = %08x!\n", r);
333 IStream_Release( stm );
338 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
343 encname = encode_streamname(FALSE, stname);
345 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
347 r = IStorage_OpenStream(db->storage, encname, NULL,
348 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
351 MSITRANSFORM *transform;
353 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
355 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
356 r = IStorage_OpenStream( transform->stg, encname, NULL,
357 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
365 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
368 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
369 USHORT **pdata, UINT *psz )
372 UINT ret = ERROR_FUNCTION_FAILED;
378 r = db_get_raw_stream( db, stname, &stm );
379 if( r != ERROR_SUCCESS)
381 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
384 WARN("open stream failed r = %08x!\n", r);
388 if( stat.cbSize.QuadPart >> 32 )
394 sz = stat.cbSize.QuadPart;
395 data = msi_alloc( sz );
398 WARN("couldn't allocate memory r=%08x!\n", r);
399 ret = ERROR_NOT_ENOUGH_MEMORY;
403 r = IStream_Read(stm, data, sz, &count );
404 if( FAILED( r ) || ( count != sz ) )
407 WARN("read stream failed r = %08x!\n", r);
416 IStream_Release( stm );
421 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
422 LPCVOID data, UINT sz, BOOL bTable )
425 UINT ret = ERROR_FUNCTION_FAILED;
432 encname = encode_streamname(bTable, stname );
433 r = IStorage_OpenStream( stg, encname, NULL,
434 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
437 r = IStorage_CreateStream( stg, encname,
438 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
443 WARN("open stream failed r = %08x\n", r);
448 r = IStream_SetSize( stm, size );
451 WARN("Failed to SetSize\n");
456 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
459 WARN("Failed to Seek\n");
465 r = IStream_Write(stm, data, sz, &count );
466 if( FAILED( r ) || ( count != sz ) )
468 WARN("Failed to Write\n");
476 IStream_Release( stm );
481 static void free_table( MSITABLE *table )
484 for( i=0; i<table->row_count; i++ )
485 msi_free( table->data[i] );
486 msi_free( table->data );
487 for( i=0; i<table->nonpersistent_row_count; i++ )
488 msi_free( table->nonpersistent_data[i] );
489 msi_free( table->nonpersistent_data );
490 if( (table->colinfo != _Tables_cols) &&
491 (table->colinfo != _Columns_cols) )
493 msi_free_colinfo( table->colinfo, table->col_count );
494 msi_free( table->colinfo );
499 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
501 const MSICOLUMNINFO *last_col = &cols[count-1];
504 return last_col->offset + bytes_per_column( last_col );
507 /* add this table to the list of cached tables in the database */
508 static UINT read_table_from_storage( MSITABLE *t, IStorage *stg )
510 BYTE *rawdata = NULL;
511 UINT rawsize = 0, i, j, row_size = 0;
513 TRACE("%s\n",debugstr_w(t->name));
515 row_size = msi_table_get_row_size( t->colinfo, t->col_count );
517 /* if we can't read the table, just assume that it's empty */
518 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
520 return ERROR_SUCCESS;
522 TRACE("Read %d bytes\n", rawsize );
524 if( rawsize % row_size )
526 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
530 t->row_count = rawsize / row_size;
531 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
535 /* transpose all the data */
536 TRACE("Transposing data from %d rows\n", t->row_count );
537 for( i=0; i<t->row_count; i++ )
539 t->data[i] = msi_alloc( row_size );
543 for( j=0; j<t->col_count; j++ )
545 UINT ofs = t->colinfo[j].offset;
546 UINT n = bytes_per_column( &t->colinfo[j] );
549 if ( n != 2 && n != 3 && n != 4 )
551 ERR("oops - unknown column width %d\n", n);
555 for ( k = 0; k < n; k++ )
556 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
561 return ERROR_SUCCESS;
564 return ERROR_FUNCTION_FAILED;
567 void free_cached_tables( MSIDATABASE *db )
569 while( !list_empty( &db->tables ) )
571 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
573 list_remove( &t->entry );
578 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
582 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
583 if( !lstrcmpW( name, t->name ) )
589 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
591 UINT r, column_count = 0;
592 MSICOLUMNINFO *columns;
594 /* get the number of columns in this table */
596 r = get_tablecolumns( db, name, NULL, &column_count );
597 if( r != ERROR_SUCCESS )
600 /* if there's no columns, there's no table */
601 if( column_count == 0 )
602 return ERROR_INVALID_PARAMETER;
604 TRACE("Table %s found\n", debugstr_w(name) );
606 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
608 return ERROR_FUNCTION_FAILED;
610 r = get_tablecolumns( db, name, columns, &column_count );
611 if( r != ERROR_SUCCESS )
614 return ERROR_FUNCTION_FAILED;
618 *pcount = column_count;
623 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
624 BOOL persistent, MSITABLE **table_ret)
628 MSIRECORD *rec = NULL;
633 /* only add tables that don't exist already */
634 if( TABLE_Exists(db, name ) )
636 WARN("table %s exists\n", debugstr_w(name));
637 return ERROR_BAD_QUERY_SYNTAX;
640 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
642 return ERROR_FUNCTION_FAILED;
644 table->ref_count = 1;
645 table->row_count = 0;
647 table->nonpersistent_row_count = 0;
648 table->nonpersistent_data = NULL;
649 table->colinfo = NULL;
650 table->col_count = 0;
651 table->persistent = persistent;
652 lstrcpyW( table->name, name );
654 for( col = col_info; col; col = col->next )
657 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
661 return ERROR_FUNCTION_FAILED;
664 for( i = 0, col = col_info; col; i++, col = col->next )
666 table->colinfo[ i ].tablename = strdupW( col->table );
667 table->colinfo[ i ].number = i + 1;
668 table->colinfo[ i ].colname = strdupW( col->column );
669 table->colinfo[ i ].type = col->type;
670 table->colinfo[ i ].offset = 0;
671 table->colinfo[ i ].ref_count = 0;
672 table->colinfo[ i ].hash_table = NULL;
674 table_calc_column_offsets( table->colinfo, table->col_count);
676 r = TABLE_CreateView( db, szTables, &tv );
677 TRACE("CreateView returned %x\n", r);
684 r = tv->ops->execute( tv, 0 );
685 TRACE("tv execute returned %x\n", r);
689 rec = MSI_CreateRecord( 1 );
693 r = MSI_RecordSetStringW( rec, 1, name );
697 r = tv->ops->insert_row( tv, rec, !persistent );
698 TRACE("insert_row returned %x\n", r);
702 tv->ops->delete( tv );
705 msiobj_release( &rec->hdr );
710 /* add each column to the _Columns table */
711 r = TABLE_CreateView( db, szColumns, &tv );
715 r = tv->ops->execute( tv, 0 );
716 TRACE("tv execute returned %x\n", r);
720 rec = MSI_CreateRecord( 4 );
724 r = MSI_RecordSetStringW( rec, 1, name );
729 * need to set the table, column number, col name and type
730 * for each column we enter in the table
733 for( col = col_info; col; col = col->next )
735 r = MSI_RecordSetInteger( rec, 2, nField );
739 r = MSI_RecordSetStringW( rec, 3, col->column );
743 r = MSI_RecordSetInteger( rec, 4, col->type );
747 r = tv->ops->insert_row( tv, rec, FALSE );
759 msiobj_release( &rec->hdr );
760 /* FIXME: remove values from the string table on error */
762 tv->ops->delete( tv );
764 if (r == ERROR_SUCCESS)
766 list_add_head( &db->tables, &table->entry );
775 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
780 /* first, see if the table is cached */
781 table = find_cached_table( db, name );
785 return ERROR_SUCCESS;
788 /* nonexistent tables should be interpreted as empty tables */
789 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
791 return ERROR_FUNCTION_FAILED;
793 table->row_count = 0;
795 table->nonpersistent_row_count = 0;
796 table->nonpersistent_data = NULL;
797 table->colinfo = NULL;
798 table->col_count = 0;
799 table->persistent = TRUE;
800 lstrcpyW( table->name, name );
802 /* these two tables are special - we know the column types already */
803 if( !lstrcmpW( name, szColumns ) )
805 table->colinfo = _Columns_cols;
806 table->col_count = sizeof(_Columns_cols)/sizeof(_Columns_cols[0]);
808 else if( !lstrcmpW( name, szTables ) )
810 table->colinfo = _Tables_cols;
811 table->col_count = sizeof(_Tables_cols)/sizeof(_Tables_cols[0]);
815 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
816 if (r != ERROR_SUCCESS)
818 free_table ( table );
823 r = read_table_from_storage( table, db->storage );
824 if( r != ERROR_SUCCESS )
830 list_add_head( &db->tables, &table->entry );
832 return ERROR_SUCCESS;
835 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
837 BYTE *rawdata = NULL, *p;
838 UINT rawsize, r, i, j, row_size;
840 /* Nothing to do for non-persistent tables */
842 return ERROR_SUCCESS;
844 TRACE("Saving %s\n", debugstr_w( t->name ) );
846 row_size = msi_table_get_row_size( t->colinfo, t->col_count );
848 rawsize = t->row_count * row_size;
849 rawdata = msi_alloc_zero( rawsize );
852 r = ERROR_NOT_ENOUGH_MEMORY;
857 for( i=0; i<t->col_count; i++ )
859 for( j=0; j<t->row_count; j++ )
861 UINT offset = t->colinfo[i].offset;
863 *p++ = t->data[j][offset];
864 *p++ = t->data[j][offset + 1];
865 if( 4 == bytes_per_column( &t->colinfo[i] ) )
867 *p++ = t->data[j][offset + 2];
868 *p++ = t->data[j][offset + 3];
873 TRACE("writing %d bytes\n", rawsize);
874 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
882 static void table_calc_column_offsets( MSICOLUMNINFO *colinfo, DWORD count )
886 for( i=0; colinfo && (i<count); i++ )
888 assert( (i+1) == colinfo[ i ].number );
890 colinfo[i].offset = colinfo[ i - 1 ].offset
891 + bytes_per_column( &colinfo[ i - 1 ] );
893 colinfo[i].offset = 0;
894 TRACE("column %d is [%s] with type %08x ofs %d\n",
895 colinfo[i].number, debugstr_w(colinfo[i].colname),
896 colinfo[i].type, colinfo[i].offset);
900 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
902 const MSICOLUMNINFO *p;
905 TRACE("%s\n", debugstr_w(name));
907 if (!lstrcmpW( name, szTables ))
912 else if (!lstrcmpW( name, szColumns ))
918 return ERROR_FUNCTION_FAILED;
920 /* duplicate the string data so we can free it in msi_free_colinfo */
923 if (colinfo && (i < *sz) )
925 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
926 colinfo[i].tablename = strdupW( p[i].tablename );
927 colinfo[i].colname = strdupW( p[i].colname );
929 if( colinfo && (i >= *sz) )
932 table_calc_column_offsets( colinfo, n );
934 return ERROR_SUCCESS;
937 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
941 for( i=0; i<count; i++ )
943 msi_free( colinfo[i].tablename );
944 msi_free( colinfo[i].colname );
945 msi_free( colinfo[i].hash_table );
949 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
951 return strdupW(msi_string_lookup_id( db->strings, stringid ));
954 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
958 for (i = 0; i < bytes; i++)
959 ret += (data[row][col + i] << i * 8);
964 static UINT get_tablecolumns( MSIDATABASE *db,
965 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
967 UINT r, i, n=0, table_id, count, maxcount = *sz;
968 MSITABLE *table = NULL;
970 TRACE("%s\n", debugstr_w(szTableName));
972 /* first check if there is a default table with that name */
973 r = get_defaulttablecolumns( szTableName, colinfo, sz );
974 if( ( r == ERROR_SUCCESS ) && *sz )
977 r = get_table( db, szColumns, &table );
978 if( r != ERROR_SUCCESS )
980 ERR("couldn't load _Columns table\n");
981 return ERROR_FUNCTION_FAILED;
984 /* convert table and column names to IDs from the string table */
985 r = msi_string2idW( db->strings, szTableName, &table_id );
986 if( r != ERROR_SUCCESS )
988 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
992 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
994 /* Note: _Columns table doesn't have non-persistent data */
996 /* if maxcount is non-zero, assume it's exactly right for this table */
997 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
998 count = table->row_count;
999 for( i=0; i<count; i++ )
1001 if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
1005 UINT id = read_table_int(table->data, i, _Columns_cols[2].offset, db->bytes_per_strref);
1006 UINT col = read_table_int(table->data, i, _Columns_cols[1].offset, sizeof(USHORT)) - (1<<15);
1008 /* check the column number is in range */
1009 if (col<1 || col>maxcount)
1011 ERR("column %d out of range\n", col);
1015 /* check if this column was already set */
1016 if (colinfo[ col - 1 ].number)
1018 ERR("duplicate column %d\n", col);
1022 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
1023 colinfo[ col - 1 ].number = col;
1024 colinfo[ col - 1 ].colname = msi_makestring( db, id );
1025 colinfo[ col - 1 ].type = read_table_int(table->data, i, _Columns_cols[3].offset, sizeof(USHORT)) - (1<<15);
1026 colinfo[ col - 1 ].offset = 0;
1027 colinfo[ col - 1 ].ref_count = 0;
1028 colinfo[ col - 1 ].hash_table = NULL;
1033 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
1035 if (colinfo && n != maxcount)
1037 ERR("missing column in table %s\n", debugstr_w(szTableName));
1038 msi_free_colinfo(colinfo, maxcount );
1039 return ERROR_FUNCTION_FAILED;
1042 table_calc_column_offsets( colinfo, n );
1045 return ERROR_SUCCESS;
1048 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
1051 UINT size, offset, old_count;
1054 table = find_cached_table( db, name );
1055 old_count = table->col_count;
1056 msi_free( table->colinfo );
1057 table_get_column_info( db, name, &table->colinfo, &table->col_count );
1059 size = msi_table_get_row_size( table->colinfo, table->col_count );
1060 offset = table->colinfo[table->col_count - 1].offset;
1062 for ( n = 0; n < table->row_count; n++ )
1064 table->data[n] = msi_realloc( table->data[n], size );
1065 if (old_count < table->col_count)
1066 memset( &table->data[n][offset], 0, size - offset );
1070 /* try to find the table name in the _Tables table */
1071 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
1073 UINT r, table_id = 0, i, count;
1074 MSITABLE *table = NULL;
1076 if( !lstrcmpW( name, szTables ) )
1078 if( !lstrcmpW( name, szColumns ) )
1081 r = msi_string2idW( db->strings, name, &table_id );
1082 if( r != ERROR_SUCCESS )
1084 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1088 r = get_table( db, szTables, &table );
1089 if( r != ERROR_SUCCESS )
1091 ERR("table %s not available\n", debugstr_w(szTables));
1095 count = table->row_count;
1096 for( i=0; i<count; i++ )
1097 if( table->data[ i ][ 0 ] == table_id )
1103 count = table->nonpersistent_row_count;
1104 for( i=0; i<count; i++ )
1105 if( table->nonpersistent_data[ i ][ 0 ] == table_id )
1111 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1116 /* below is the query interface to a table */
1118 typedef struct tagMSITABLEVIEW
1123 MSICOLUMNINFO *columns;
1124 MSIORDERINFO *order;
1130 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1132 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1137 return ERROR_INVALID_PARAMETER;
1139 if( (col==0) || (col>tv->num_cols) )
1140 return ERROR_INVALID_PARAMETER;
1142 /* how many rows are there ? */
1143 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1144 return ERROR_NO_MORE_ITEMS;
1146 if( tv->columns[col-1].offset >= tv->row_size )
1148 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1149 ERR("%p %p\n", tv, tv->columns );
1150 return ERROR_FUNCTION_FAILED;
1154 row = tv->order->reorder[row];
1156 if (row >= tv->table->row_count)
1158 row -= tv->table->row_count;
1159 data = tv->table->nonpersistent_data;
1162 data = tv->table->data;
1164 n = bytes_per_column( &tv->columns[col-1] );
1165 if (n != 2 && n != 3 && n != 4)
1167 ERR("oops! what is %d bytes per column?\n", n );
1168 return ERROR_FUNCTION_FAILED;
1171 offset = tv->columns[col-1].offset;
1172 *val = read_table_int(data, row, offset, n);
1174 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1176 return ERROR_SUCCESS;
1180 * We need a special case for streams, as we need to reference column with
1181 * the name of the stream in the same table, and the table name
1182 * which may not be available at higher levels of the query
1184 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1186 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1187 UINT ival = 0, refcol = 0, r;
1191 static const WCHAR szDot[] = { '.', 0 };
1194 if( !view->ops->fetch_int )
1195 return ERROR_INVALID_PARAMETER;
1198 * The column marked with the type stream data seems to have a single number
1199 * which references the column containing the name of the stream data
1201 * Fetch the column to reference first.
1203 r = view->ops->fetch_int( view, row, col, &ival );
1204 if( r != ERROR_SUCCESS )
1207 /* check the column value is in range */
1208 if (ival < 0 || ival > tv->num_cols || ival == col)
1210 ERR("bad column ref (%u) for stream\n", ival);
1211 return ERROR_FUNCTION_FAILED;
1214 if ( tv->columns[ival - 1].type & MSITYPE_STRING )
1216 /* now get the column with the name of the stream */
1217 r = view->ops->fetch_int( view, row, ival, &refcol );
1218 if ( r != ERROR_SUCCESS )
1221 /* lookup the string value from the string table */
1222 sval = msi_string_lookup_id( tv->db->strings, refcol );
1224 return ERROR_INVALID_PARAMETER;
1228 static const WCHAR fmt[] = { '%','d',0 };
1229 sprintfW( number, fmt, ival );
1233 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1234 full_name = msi_alloc( len*sizeof(WCHAR) );
1235 lstrcpyW( full_name, tv->name );
1236 lstrcatW( full_name, szDot );
1237 lstrcatW( full_name, sval );
1239 r = db_get_raw_stream( tv->db, full_name, stm );
1241 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1242 msi_free( full_name );
1247 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1253 return ERROR_INVALID_PARAMETER;
1255 if( (col==0) || (col>tv->num_cols) )
1256 return ERROR_INVALID_PARAMETER;
1258 if( row >= tv->table->row_count + tv->table->nonpersistent_row_count )
1259 return ERROR_INVALID_PARAMETER;
1261 if( tv->columns[col-1].offset >= tv->row_size )
1263 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1264 ERR("%p %p\n", tv, tv->columns );
1265 return ERROR_FUNCTION_FAILED;
1268 msi_free( tv->columns[col-1].hash_table );
1269 tv->columns[col-1].hash_table = NULL;
1271 if (row >= tv->table->row_count)
1273 row -= tv->table->row_count;
1274 data = tv->table->nonpersistent_data;
1277 data = tv->table->data;
1279 n = bytes_per_column( &tv->columns[col-1] );
1280 if ( n != 2 && n != 3 && n != 4 )
1282 ERR("oops! what is %d bytes per column?\n", n );
1283 return ERROR_FUNCTION_FAILED;
1286 offset = tv->columns[col-1].offset;
1287 for ( i = 0; i < n; i++ )
1288 data[row][offset + i] = (val >> i * 8) & 0xff;
1290 return ERROR_SUCCESS;
1293 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1295 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1298 return ERROR_INVALID_PARAMETER;
1301 row = tv->order->reorder[row];
1303 return msi_view_get_row(tv->db, view, row, rec);
1306 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1308 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1309 UINT i, val, r = ERROR_SUCCESS;
1312 return ERROR_INVALID_PARAMETER;
1314 /* test if any of the mask bits are invalid */
1315 if ( mask >= (1<<tv->num_cols) )
1316 return ERROR_INVALID_PARAMETER;
1318 for ( i = 0; i < tv->num_cols; i++ )
1322 /* only update the fields specified in the mask */
1323 if ( !(mask&(1<<i)) )
1326 /* if row >= tv->table->row_count then it is a non-persistent row */
1327 persistent = tv->table->persistent && (row < tv->table->row_count);
1328 /* FIXME: should we allow updating keys? */
1331 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1333 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1335 val = 1; /* refers to the first key column */
1337 else if ( tv->columns[i].type & MSITYPE_STRING )
1339 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1342 r = msi_string2idW(tv->db->strings, sval, &ival);
1343 if (r == ERROR_SUCCESS)
1345 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1350 val = msi_addstringW( tv->db->strings, 0, sval, -1, 1,
1351 persistent ? StringPersistent : StringNonPersistent );
1353 else if ( 2 == bytes_per_column( &tv->columns[ i ] ) )
1355 val = 0x8000 + MSI_RecordGetInteger( rec, i + 1 );
1356 if ( val & 0xffff0000 )
1358 ERR("field %u value %d out of range\n", i+1, val - 0x8000 );
1359 return ERROR_FUNCTION_FAILED;
1364 INT ival = MSI_RecordGetInteger( rec, i + 1 );
1365 val = ival ^ 0x80000000;
1369 r = TABLE_set_int( tv, row, i+1, val );
1370 if ( r != ERROR_SUCCESS )
1376 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1378 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1384 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1387 return ERROR_INVALID_PARAMETER;
1389 row = msi_alloc_zero( tv->row_size );
1391 return ERROR_NOT_ENOUGH_MEMORY;
1395 row_count = &tv->table->nonpersistent_row_count;
1396 data_ptr = &tv->table->nonpersistent_data;
1397 *num = tv->table->row_count + tv->table->nonpersistent_row_count;
1401 row_count = &tv->table->row_count;
1402 data_ptr = &tv->table->data;
1403 *num = tv->table->row_count;
1406 sz = (*row_count + 1) * sizeof (BYTE*);
1408 p = msi_realloc( *data_ptr, sz );
1410 p = msi_alloc( sz );
1414 return ERROR_NOT_ENOUGH_MEMORY;
1418 (*data_ptr)[*row_count] = row;
1421 return ERROR_SUCCESS;
1424 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1426 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1428 TRACE("%p %p\n", tv, record);
1430 TRACE("There are %d columns\n", tv->num_cols );
1432 return ERROR_SUCCESS;
1435 static UINT TABLE_close( struct tagMSIVIEW *view )
1437 TRACE("%p\n", view );
1439 return ERROR_SUCCESS;
1442 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1444 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1446 TRACE("%p %p %p\n", view, rows, cols );
1449 *cols = tv->num_cols;
1453 return ERROR_INVALID_PARAMETER;
1454 *rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1457 return ERROR_SUCCESS;
1460 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1461 UINT n, LPWSTR *name, UINT *type )
1463 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1465 TRACE("%p %d %p %p\n", tv, n, name, type );
1467 if( ( n == 0 ) || ( n > tv->num_cols ) )
1468 return ERROR_INVALID_PARAMETER;
1472 *name = strdupW( tv->columns[n-1].colname );
1474 return ERROR_FUNCTION_FAILED;
1477 *type = tv->columns[n-1].type;
1479 return ERROR_SUCCESS;
1482 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1484 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1488 /* check there's no null values where they're not allowed */
1489 for( i = 0; i < tv->num_cols; i++ )
1491 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1494 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1495 TRACE("skipping binary column\n");
1496 else if ( tv->columns[i].type & MSITYPE_STRING )
1500 str = MSI_RecordGetString( rec, i+1 );
1501 if (str == NULL || str[0] == 0)
1502 return ERROR_INVALID_DATA;
1508 n = MSI_RecordGetInteger( rec, i+1 );
1509 if (n == MSI_NULL_INTEGER)
1510 return ERROR_INVALID_DATA;
1514 /* check there's no duplicate keys */
1515 r = msi_table_find_row( tv, rec, &row );
1516 if (r == ERROR_SUCCESS)
1517 return ERROR_FUNCTION_FAILED;
1519 return ERROR_SUCCESS;
1522 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, BOOL temporary )
1524 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1527 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1529 /* check that the key is unique - can we find a matching row? */
1530 r = table_validate_new( tv, rec );
1531 if( r != ERROR_SUCCESS )
1532 return ERROR_FUNCTION_FAILED;
1534 r = table_create_new_row( view, &row, temporary );
1535 TRACE("insert_row returned %08x\n", r);
1536 if( r != ERROR_SUCCESS )
1539 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1542 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1544 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1545 UINT r, num_rows, num_cols, i;
1548 TRACE("%p %d\n", tv, row);
1551 return ERROR_INVALID_PARAMETER;
1553 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1554 if ( r != ERROR_SUCCESS )
1557 if ( row >= num_rows )
1558 return ERROR_FUNCTION_FAILED;
1560 if ( row < tv->table->row_count )
1562 num_rows = tv->table->row_count;
1563 tv->table->row_count--;
1564 data = tv->table->data;
1568 num_rows = tv->table->nonpersistent_row_count;
1569 row -= tv->table->row_count;
1570 tv->table->nonpersistent_row_count--;
1571 data = tv->table->nonpersistent_data;
1574 if ( row == num_rows - 1 )
1575 return ERROR_SUCCESS;
1577 for (i = row + 1; i < num_rows; i++)
1578 memcpy(data[i - 1], data[i], tv->row_size);
1580 return ERROR_SUCCESS;
1583 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1585 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1588 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1589 * sets the fetched record apart from other records
1593 return ERROR_INVALID_PARAMETER;
1595 r = msi_table_find_row(tv, rec, &new_row);
1596 if (r != ERROR_SUCCESS)
1598 ERR("can't find row to modify\n");
1599 return ERROR_FUNCTION_FAILED;
1602 /* the row cannot be changed */
1603 if (row != new_row + 1)
1604 return ERROR_FUNCTION_FAILED;
1606 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1609 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1611 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1614 r = msi_table_find_row(tv, rec, &row);
1615 if (r != ERROR_SUCCESS)
1618 return TABLE_delete_row(view, row);
1621 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1622 MSIRECORD *rec, UINT row)
1624 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1627 TRACE("%p %d %p\n", view, eModifyMode, rec );
1629 switch (eModifyMode)
1631 case MSIMODIFY_DELETE:
1632 r = modify_delete_row( view, rec );
1634 case MSIMODIFY_VALIDATE_NEW:
1635 r = table_validate_new( tv, rec );
1638 case MSIMODIFY_INSERT:
1639 r = table_validate_new( tv, rec );
1640 if (r != ERROR_SUCCESS)
1642 r = TABLE_insert_row( view, rec, FALSE );
1645 case MSIMODIFY_INSERT_TEMPORARY:
1646 r = table_validate_new( tv, rec );
1647 if (r != ERROR_SUCCESS)
1649 r = TABLE_insert_row( view, rec, TRUE );
1652 case MSIMODIFY_UPDATE:
1653 r = msi_table_update( view, rec, row );
1656 case MSIMODIFY_REFRESH:
1657 case MSIMODIFY_ASSIGN:
1658 case MSIMODIFY_REPLACE:
1659 case MSIMODIFY_MERGE:
1660 case MSIMODIFY_VALIDATE:
1661 case MSIMODIFY_VALIDATE_FIELD:
1662 case MSIMODIFY_VALIDATE_DELETE:
1663 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1664 r = ERROR_CALL_NOT_IMPLEMENTED;
1668 r = ERROR_INVALID_DATA;
1674 static UINT TABLE_delete( struct tagMSIVIEW *view )
1676 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1678 TRACE("%p\n", view );
1685 msi_free( tv->order->reorder );
1686 msi_free( tv->order );
1692 return ERROR_SUCCESS;
1695 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1696 UINT val, UINT *row, MSIITERHANDLE *handle )
1698 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1699 const MSICOLUMNHASHENTRY *entry;
1701 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1704 return ERROR_INVALID_PARAMETER;
1706 if( (col==0) || (col > tv->num_cols) )
1707 return ERROR_INVALID_PARAMETER;
1709 if( !tv->columns[col-1].hash_table )
1712 UINT num_rows = tv->table->row_count + tv->table->nonpersistent_row_count;
1713 MSICOLUMNHASHENTRY **hash_table;
1714 MSICOLUMNHASHENTRY *new_entry;
1716 if( tv->columns[col-1].offset >= tv->row_size )
1718 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1719 ERR("%p %p\n", tv, tv->columns );
1720 return ERROR_FUNCTION_FAILED;
1723 /* allocate contiguous memory for the table and its entries so we
1724 * don't have to do an expensive cleanup */
1725 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1726 num_rows * sizeof(MSICOLUMNHASHENTRY));
1728 return ERROR_OUTOFMEMORY;
1730 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1731 tv->columns[col-1].hash_table = hash_table;
1733 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1735 for (i = 0; i < num_rows; i++, new_entry++)
1739 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1742 new_entry->next = NULL;
1743 new_entry->value = row_value;
1745 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1747 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1748 while (prev_entry->next)
1749 prev_entry = prev_entry->next;
1750 prev_entry->next = new_entry;
1753 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1758 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1760 entry = (*handle)->next;
1762 while (entry && entry->value != val)
1763 entry = entry->next;
1767 return ERROR_NO_MORE_ITEMS;
1772 *row = tv->order->reorder[*row];
1774 return ERROR_SUCCESS;
1777 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1779 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1782 TRACE("%p %d\n", view, tv->table->ref_count);
1784 for (i = 0; i < tv->table->col_count; i++)
1786 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1787 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
1790 return InterlockedIncrement(&tv->table->ref_count);
1793 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
1795 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1796 MSIRECORD *rec = NULL;
1797 MSIVIEW *columns = NULL;
1800 rec = MSI_CreateRecord(2);
1802 return ERROR_OUTOFMEMORY;
1804 MSI_RecordSetStringW(rec, 1, table);
1805 MSI_RecordSetInteger(rec, 2, number);
1807 r = TABLE_CreateView(tv->db, szColumns, &columns);
1808 if (r != ERROR_SUCCESS)
1811 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row);
1812 if (r != ERROR_SUCCESS)
1815 r = TABLE_delete_row(columns, row);
1816 if (r != ERROR_SUCCESS)
1819 msi_update_table_columns(tv->db, table);
1822 msiobj_release(&rec->hdr);
1823 if (columns) columns->ops->delete(columns);
1827 static UINT TABLE_release(struct tagMSIVIEW *view)
1829 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1830 INT ref = tv->table->ref_count;
1834 TRACE("%p %d\n", view, ref);
1836 for (i = 0; i < tv->table->col_count; i++)
1838 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1840 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
1843 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
1844 tv->table->colinfo[i].number);
1845 if (r != ERROR_SUCCESS)
1851 ref = InterlockedDecrement(&tv->table->ref_count);
1854 if (!tv->table->row_count)
1856 list_remove(&tv->table->entry);
1857 free_table(tv->table);
1865 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
1866 LPCWSTR column, UINT type, BOOL hold)
1868 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1873 rec = MSI_CreateRecord(4);
1875 return ERROR_OUTOFMEMORY;
1877 MSI_RecordSetStringW(rec, 1, table);
1878 MSI_RecordSetInteger(rec, 2, number);
1879 MSI_RecordSetStringW(rec, 3, column);
1880 MSI_RecordSetInteger(rec, 4, type);
1882 r = TABLE_insert_row(&tv->view, rec, FALSE);
1883 if (r != ERROR_SUCCESS)
1886 msi_update_table_columns(tv->db, table);
1891 msitable = find_cached_table(tv->db, table);
1892 for (i = 0; i < msitable->col_count; i++)
1894 if (!lstrcmpW(msitable->colinfo[i].colname, column))
1896 InterlockedIncrement(&msitable->colinfo[i].ref_count);
1902 msiobj_release(&rec->hdr);
1906 static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
1910 r = TABLE_get_dimensions(view, NULL, &count);
1911 if (r != ERROR_SUCCESS)
1914 if (order->num_cols >= count)
1915 return ERROR_FUNCTION_FAILED;
1917 r = VIEW_find_column(view, name, &n);
1918 if (r != ERROR_SUCCESS)
1921 order->cols[order->num_cols] = n;
1922 TRACE("Ordering by column %s (%d)\n", debugstr_w(name), n);
1926 return ERROR_SUCCESS;
1929 static UINT order_compare(struct tagMSIVIEW *view, MSIORDERINFO *order,
1930 UINT a, UINT b, UINT *swap)
1932 UINT r, i, a_val = 0, b_val = 0;
1935 for (i = 0; i < order->num_cols; i++)
1937 r = TABLE_fetch_int(view, a, order->cols[i], &a_val);
1938 if (r != ERROR_SUCCESS)
1941 r = TABLE_fetch_int(view, b, order->cols[i], &b_val);
1942 if (r != ERROR_SUCCESS)
1953 return ERROR_SUCCESS;
1956 static UINT order_mergesort(struct tagMSIVIEW *view, MSIORDERINFO *order,
1957 UINT left, UINT right)
1960 UINT swap = 0, center = (left + right) / 2;
1961 UINT *array = order->reorder;
1964 return ERROR_SUCCESS;
1966 /* sort the left half */
1967 r = order_mergesort(view, order, left, center);
1968 if (r != ERROR_SUCCESS)
1971 /* sort the right half */
1972 r = order_mergesort(view, order, center + 1, right);
1973 if (r != ERROR_SUCCESS)
1976 for (i = left, j = center + 1; (i <= center) && (j <= right); i++)
1978 r = order_compare(view, order, array[i], array[j], &swap);
1979 if (r != ERROR_SUCCESS)
1985 memmove(&array[i + 1], &array[i], (j - i) * sizeof(UINT));
1992 return ERROR_SUCCESS;
1995 static UINT order_verify(struct tagMSIVIEW *view, MSIORDERINFO *order, UINT num_rows)
1999 for (i = 1; i < num_rows; i++)
2001 r = order_compare(view, order, order->reorder[i - 1],
2002 order->reorder[i], &swap);
2003 if (r != ERROR_SUCCESS)
2009 ERR("Bad order! %d\n", i);
2010 return ERROR_FUNCTION_FAILED;
2013 return ERROR_SUCCESS;
2016 static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
2018 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2019 MSIORDERINFO *order;
2024 TRACE("sorting table %s\n", debugstr_w(tv->name));
2026 r = TABLE_get_dimensions(view, &rows, &cols);
2027 if (r != ERROR_SUCCESS)
2030 order = msi_alloc_zero(sizeof(MSIORDERINFO) + sizeof(UINT) * cols);
2032 return ERROR_OUTOFMEMORY;
2034 for (ptr = columns; ptr; ptr = ptr->next)
2035 order_add_column(view, order, ptr->column);
2037 order->reorder = msi_alloc(rows * sizeof(UINT));
2038 if (!order->reorder)
2039 return ERROR_OUTOFMEMORY;
2041 for (i = 0; i < rows; i++)
2042 order->reorder[i] = i;
2044 r = order_mergesort(view, order, 0, rows - 1);
2045 if (r != ERROR_SUCCESS)
2048 r = order_verify(view, order, rows);
2049 if (r != ERROR_SUCCESS)
2054 return ERROR_SUCCESS;
2057 static const MSIVIEWOPS table_ops =
2067 TABLE_get_dimensions,
2068 TABLE_get_column_info,
2071 TABLE_find_matching_rows,
2075 TABLE_remove_column,
2079 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2084 static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
2086 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2088 if ( !lstrcmpW( name, Streams ) )
2089 return STREAMS_CreateView( db, view );
2091 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
2092 tv = msi_alloc_zero( sz );
2094 return ERROR_FUNCTION_FAILED;
2096 r = get_table( db, name, &tv->table );
2097 if( r != ERROR_SUCCESS )
2100 WARN("table not found\n");
2104 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2106 /* fill the structure */
2107 tv->view.ops = &table_ops;
2109 tv->columns = tv->table->colinfo;
2110 tv->num_cols = tv->table->col_count;
2111 tv->row_size = msi_table_get_row_size( tv->table->colinfo, tv->table->col_count );
2113 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2115 *view = (MSIVIEW*) tv;
2116 lstrcpyW( tv->name, name );
2118 return ERROR_SUCCESS;
2121 UINT MSI_CommitTables( MSIDATABASE *db )
2124 MSITABLE *table = NULL;
2128 r = msi_save_string_table( db->strings, db->storage );
2129 if( r != ERROR_SUCCESS )
2131 WARN("failed to save string table r=%08x\n",r);
2135 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2137 r = save_table( db, table );
2138 if( r != ERROR_SUCCESS )
2140 WARN("failed to save table %s (r=%08x)\n",
2141 debugstr_w(table->name), r);
2146 /* force everything to reload next time */
2147 free_cached_tables( db );
2149 return ERROR_SUCCESS;
2152 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2157 TRACE("%p %s\n", db, debugstr_w(table));
2160 return MSICONDITION_ERROR;
2162 r = get_table( db, table, &t );
2163 if (r != ERROR_SUCCESS)
2164 return MSICONDITION_NONE;
2167 return MSICONDITION_TRUE;
2169 return MSICONDITION_FALSE;
2172 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2176 for (i = 0; i < bytes; i++)
2177 ret += (data[col + i] << i * 8);
2182 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2183 const BYTE *rawdata, UINT bytes_per_strref )
2185 UINT i, val, ofs = 0;
2187 MSICOLUMNINFO *columns = tv->columns;
2190 mask = rawdata[0] | (rawdata[1] << 8);
2193 rec = MSI_CreateRecord( tv->num_cols );
2198 for( i=0; i<tv->num_cols; i++ )
2200 if ( (mask&1) && (i>=(mask>>8)) )
2202 /* all keys must be present */
2203 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2206 if( (columns[i].type & MSITYPE_STRING) &&
2207 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2211 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2212 sval = msi_string_lookup_id( st, val );
2213 MSI_RecordSetStringW( rec, i+1, sval );
2214 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
2215 ofs += bytes_per_strref;
2219 UINT n = bytes_per_column( &columns[i] );
2223 val = read_raw_int(rawdata, ofs, n);
2225 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
2226 TRACE(" field %d [0x%04x]\n", i+1, val );
2229 val = read_raw_int(rawdata, ofs, n);
2231 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2232 TRACE(" field %d [0x%08x]\n", i+1, val );
2235 ERR("oops - unknown column width %d\n", n);
2244 static void dump_record( MSIRECORD *rec )
2248 n = MSI_RecordGetFieldCount( rec );
2249 for( i=1; i<=n; i++ )
2251 LPCWSTR sval = MSI_RecordGetString( rec, i );
2253 if( MSI_RecordIsNull( rec, i ) )
2254 TRACE("row -> []\n");
2255 else if( (sval = MSI_RecordGetString( rec, i )) )
2256 TRACE("row -> [%s]\n", debugstr_w(sval));
2258 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2262 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2267 for( i=0; i<(rawsize/2); i++ )
2269 sval = msi_string_lookup_id( st, rawdata[i] );
2270 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
2274 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2279 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2280 for( i=0; i<tv->num_cols; i++ )
2284 if ( ~tv->columns[i].type & MSITYPE_KEY )
2287 /* turn the transform column value into a row value */
2288 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2289 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2291 str = MSI_RecordGetString( rec, i+1 );
2292 r = msi_string2idW( tv->db->strings, str, &data[i] );
2294 /* if there's no matching string in the string table,
2295 these keys can't match any record, so fail now. */
2296 if( ERROR_SUCCESS != r )
2304 data[i] = MSI_RecordGetInteger( rec, i+1 );
2305 if ((tv->columns[i].type&0xff) == 2)
2308 data[i] += 0x80000000;
2314 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
2316 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2318 for( i=0; i<tv->num_cols; i++ )
2320 if ( ~tv->columns[i].type & MSITYPE_KEY )
2323 /* turn the transform column value into a row value */
2324 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2325 if ( r != ERROR_SUCCESS )
2327 ERR("TABLE_fetch_int shouldn't fail here\n");
2331 /* if this key matches, move to the next column */
2334 ret = ERROR_FUNCTION_FAILED;
2338 ret = ERROR_SUCCESS;
2344 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
2346 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2348 data = msi_record_to_row( tv, rec );
2351 for( i = 0; i < tv->table->row_count + tv->table->nonpersistent_row_count; i++ )
2353 r = msi_row_matches( tv, i, data );
2354 if( r == ERROR_SUCCESS )
2370 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2371 string_table *st, TRANSFORMDATA *transform,
2372 UINT bytes_per_strref )
2375 BYTE *rawdata = NULL;
2376 MSITABLEVIEW *tv = NULL;
2377 UINT r, n, sz, i, mask;
2378 MSIRECORD *rec = NULL;
2384 return ERROR_SUCCESS;
2386 name = transform->name;
2389 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2391 /* read the transform data */
2392 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2395 TRACE("table %s empty\n", debugstr_w(name) );
2396 return ERROR_INVALID_TABLE;
2399 /* create a table view */
2400 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2401 if( r != ERROR_SUCCESS )
2404 r = tv->view.ops->execute( &tv->view, NULL );
2405 if( r != ERROR_SUCCESS )
2408 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2409 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2411 /* interpret the data */
2413 for( n=0; n < rawsize; )
2415 mask = rawdata[n] | (rawdata[n+1] << 8);
2420 * if the low bit is set, columns are continuous and
2421 * the number of columns is specified in the high byte
2424 for( i=0; i<tv->num_cols; i++ )
2426 if( (tv->columns[i].type & MSITYPE_STRING) &&
2427 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2428 sz += bytes_per_strref;
2430 sz += bytes_per_column( &tv->columns[i] );
2436 * If the low bit is not set, mask is a bitmask.
2437 * Excepting for key fields, which are always present,
2438 * each bit indicates that a field is present in the transform record.
2440 * mask == 0 is a special case ... only the keys will be present
2441 * and it means that this row should be deleted.
2444 for( i=0; i<tv->num_cols; i++ )
2446 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2448 if( (tv->columns[i].type & MSITYPE_STRING) &&
2449 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2450 sz += bytes_per_strref;
2452 sz += bytes_per_column( &tv->columns[i] );
2457 /* check we didn't run of the end of the table */
2458 if ( (n+sz) > rawsize )
2461 dump_table( st, (USHORT *)rawdata, rawsize );
2465 rec = msi_get_transform_record( tv, st, &rawdata[n], bytes_per_strref );
2472 UINT number = MSI_NULL_INTEGER;
2474 TRACE("inserting record\n");
2476 if (!lstrcmpW(name, szColumns))
2478 MSI_RecordGetStringW( rec, 1, table, &sz );
2479 number = MSI_RecordGetInteger( rec, 2 );
2482 * Native msi seems writes nul into the Number (2nd) column of
2483 * the _Columns table, only when the columns are from a new table
2485 if ( number == MSI_NULL_INTEGER )
2487 /* reset the column number on a new table */
2488 if ( lstrcmpW(coltable, table) )
2491 lstrcpyW( coltable, table );
2494 /* fix nul column numbers */
2495 MSI_RecordSetInteger( rec, 2, ++colcol );
2499 r = TABLE_insert_row( &tv->view, rec, FALSE );
2500 if (r != ERROR_SUCCESS)
2501 ERR("insert row failed\n");
2503 if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
2504 msi_update_table_columns( db, table );
2510 r = msi_table_find_row( tv, rec, &row );
2511 if (r != ERROR_SUCCESS)
2512 ERR("no matching row to transform\n");
2515 TRACE("modifying row [%d]:\n", row);
2516 TABLE_set_row( &tv->view, row, rec, mask );
2520 TRACE("deleting row [%d]:\n", row);
2521 TABLE_delete_row( &tv->view, row );
2524 if( TRACE_ON(msidb) ) dump_record( rec );
2525 msiobj_release( &rec->hdr );
2532 /* no need to free the table, it's associated with the database */
2533 msi_free( rawdata );
2535 tv->view.ops->delete( &tv->view );
2537 return ERROR_SUCCESS;
2541 * msi_table_apply_transform
2543 * Enumerate the table transforms in a transform storage and apply each one.
2545 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2547 struct list transforms;
2548 IEnumSTATSTG *stgenum = NULL;
2549 TRANSFORMDATA *transform;
2550 TRANSFORMDATA *tables = NULL, *columns = NULL;
2553 string_table *strings;
2554 UINT ret = ERROR_FUNCTION_FAILED;
2555 UINT bytes_per_strref;
2557 TRACE("%p %p\n", db, stg );
2559 strings = msi_load_string_table( stg, &bytes_per_strref );
2563 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2567 list_init(&transforms);
2571 MSITABLEVIEW *tv = NULL;
2575 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2576 if ( FAILED( r ) || !count )
2579 decode_streamname( stat.pwcsName, name );
2580 if ( name[0] != 0x4840 )
2583 if ( !lstrcmpW( name+1, szStringPool ) ||
2584 !lstrcmpW( name+1, szStringData ) )
2587 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2591 list_add_tail( &transforms, &transform->entry );
2593 transform->name = strdupW( name + 1 );
2595 if ( !lstrcmpW( transform->name, szTables ) )
2597 else if (!lstrcmpW( transform->name, szColumns ) )
2598 columns = transform;
2600 TRACE("transform contains stream %s\n", debugstr_w(name));
2602 /* load the table */
2603 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2604 if( r != ERROR_SUCCESS )
2607 r = tv->view.ops->execute( &tv->view, NULL );
2608 if( r != ERROR_SUCCESS )
2610 tv->view.ops->delete( &tv->view );
2614 tv->view.ops->delete( &tv->view );
2618 * Apply _Tables and _Columns transforms first so that
2619 * the table metadata is correct, and empty tables exist.
2621 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2622 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2625 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2626 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2629 ret = ERROR_SUCCESS;
2631 while ( !list_empty( &transforms ) )
2633 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2635 if ( lstrcmpW( transform->name, szColumns ) &&
2636 lstrcmpW( transform->name, szTables ) &&
2637 ret == ERROR_SUCCESS )
2639 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2642 list_remove( &transform->entry );
2643 msi_free( transform->name );
2644 msi_free( transform );
2647 if ( ret == ERROR_SUCCESS )
2648 append_storage_to_db( db, stg );
2652 IEnumSTATSTG_Release( stgenum );
2654 msi_destroy_stringtable( strings );
2659 void append_storage_to_db( MSIDATABASE *db, IStorage *stg )
2663 t = msi_alloc( sizeof *t );
2665 IStorage_AddRef( stg );
2666 list_add_tail( &db->transforms, &t->entry );
2669 void msi_free_transforms( MSIDATABASE *db )
2671 while( !list_empty( &db->transforms ) )
2673 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2674 MSITRANSFORM, entry );
2675 list_remove( &t->entry );
2676 IStorage_Release( t->stg );