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
61 MSICOLUMNHASHENTRY
**hash_table
;
68 BYTE
**nonpersistent_data
;
69 UINT nonpersistent_row_count
;
71 MSICOLUMNINFO
*colinfo
;
77 typedef struct tagMSITRANSFORM
{
82 static const WCHAR szStringData
[] = {
83 '_','S','t','r','i','n','g','D','a','t','a',0 };
84 static const WCHAR szStringPool
[] = {
85 '_','S','t','r','i','n','g','P','o','o','l',0 };
87 /* information for default tables */
88 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
89 static const WCHAR szTable
[] = { 'T','a','b','l','e',0 };
90 static const WCHAR szName
[] = { 'N','a','m','e',0 };
91 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
92 static const WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
93 static const WCHAR szType
[] = { 'T','y','p','e',0 };
95 /* These tables are written into (the .hash_table part).
96 * Do not mark them const.
98 static MSICOLUMNINFO _Columns_cols
[4] = {
99 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 0 },
100 { szColumns
, 2, szNumber
, MSITYPE_VALID
| 2, 2 },
101 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4 },
102 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6 },
104 static MSICOLUMNINFO _Tables_cols
[1] = {
105 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 0 },
108 #define MAX_STREAM_NAME 0x1f
110 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
111 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
112 static void table_calc_column_offsets( MSICOLUMNINFO
*colinfo
, DWORD count
);
113 static UINT
get_tablecolumns( MSIDATABASE
*db
,
114 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
115 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
);
118 void msi_table_set_strref(UINT bytes_per_strref
)
120 _Columns_cols
[0].offset
= 0;
121 _Columns_cols
[1].offset
= bytes_per_strref
;
122 _Columns_cols
[2].offset
= _Columns_cols
[1].offset
+ sizeof(USHORT
);
123 _Columns_cols
[3].offset
= _Columns_cols
[2].offset
+ bytes_per_strref
;
126 static inline UINT
bytes_per_column( const MSICOLUMNINFO
*col
)
128 if( MSITYPE_IS_BINARY(col
->type
) )
130 if( col
->type
& MSITYPE_STRING
)
131 return _Columns_cols
[1].offset
;
132 if( (col
->type
& 0xff) > 4 )
133 ERR("Invalid column size!\n");
134 return col
->type
& 0xff;
137 static int utf2mime(int x
)
139 if( (x
>='0') && (x
<='9') )
141 if( (x
>='A') && (x
<='Z') )
143 if( (x
>='a') && (x
<='z') )
152 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
154 DWORD count
= MAX_STREAM_NAME
;
159 count
= lstrlenW( in
)+2;
160 out
= msi_alloc( count
*sizeof(WCHAR
) );
176 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
178 ch
= utf2mime(ch
) + 0x4800;
180 if( next
&& (next
<0x80) )
182 next
= utf2mime(next
);
193 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
198 static int mime2utf(int x
)
205 return x
- 10 - 26 + 'a';
206 if( x
== (10+26+26) )
211 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
216 while ( (ch
= *in
++) )
218 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
221 ch
= mime2utf(ch
-0x4800);
225 *out
++ = mime2utf(ch
&0x3f);
227 ch
= mime2utf((ch
>>6)&0x3f);
237 void enum_stream_names( IStorage
*stg
)
239 IEnumSTATSTG
*stgenum
= NULL
;
245 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
253 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
254 if( FAILED( r
) || !count
)
256 decode_streamname( stat
.pwcsName
, name
);
257 TRACE("stream %2d -> %s %s\n", n
,
258 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
262 IEnumSTATSTG_Release( stgenum
);
265 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
266 BYTE
**pdata
, UINT
*psz
)
269 UINT ret
= ERROR_FUNCTION_FAILED
;
276 encname
= encode_streamname(table
, stname
);
278 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
280 r
= IStorage_OpenStream(stg
, encname
, NULL
,
281 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
285 WARN("open stream failed r = %08x - empty table?\n", r
);
289 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
292 WARN("open stream failed r = %08x!\n", r
);
296 if( stat
.cbSize
.QuadPart
>> 32 )
302 sz
= stat
.cbSize
.QuadPart
;
303 data
= msi_alloc( sz
);
306 WARN("couldn't allocate memory r=%08x!\n", r
);
307 ret
= ERROR_NOT_ENOUGH_MEMORY
;
311 r
= IStream_Read(stm
, data
, sz
, &count
);
312 if( FAILED( r
) || ( count
!= sz
) )
315 WARN("read stream failed r = %08x!\n", r
);
324 IStream_Release( stm
);
329 UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
334 encname
= encode_streamname(FALSE
, stname
);
336 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
338 r
= IStorage_OpenStream(db
->storage
, encname
, NULL
,
339 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
342 MSITRANSFORM
*transform
;
344 LIST_FOR_EACH_ENTRY( transform
, &db
->transforms
, MSITRANSFORM
, entry
)
346 TRACE("looking for %s in transform storage\n", debugstr_w(stname
) );
347 r
= IStorage_OpenStream( transform
->stg
, encname
, NULL
,
348 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
356 return SUCCEEDED(r
) ? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
359 UINT
read_raw_stream_data( MSIDATABASE
*db
, LPCWSTR stname
,
360 USHORT
**pdata
, UINT
*psz
)
363 UINT ret
= ERROR_FUNCTION_FAILED
;
369 r
= db_get_raw_stream( db
, stname
, &stm
);
370 if( r
!= ERROR_SUCCESS
)
372 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
375 WARN("open stream failed r = %08x!\n", r
);
379 if( stat
.cbSize
.QuadPart
>> 32 )
385 sz
= stat
.cbSize
.QuadPart
;
386 data
= msi_alloc( sz
);
389 WARN("couldn't allocate memory r=%08x!\n", r
);
390 ret
= ERROR_NOT_ENOUGH_MEMORY
;
394 r
= IStream_Read(stm
, data
, sz
, &count
);
395 if( FAILED( r
) || ( count
!= sz
) )
398 WARN("read stream failed r = %08x!\n", r
);
407 IStream_Release( stm
);
412 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
413 LPCVOID data
, UINT sz
, BOOL bTable
)
416 UINT ret
= ERROR_FUNCTION_FAILED
;
423 encname
= encode_streamname(bTable
, stname
);
424 r
= IStorage_OpenStream( stg
, encname
, NULL
,
425 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
428 r
= IStorage_CreateStream( stg
, encname
,
429 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
434 WARN("open stream failed r = %08x\n", r
);
439 r
= IStream_SetSize( stm
, size
);
442 WARN("Failed to SetSize\n");
447 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
450 WARN("Failed to Seek\n");
456 r
= IStream_Write(stm
, data
, sz
, &count
);
457 if( FAILED( r
) || ( count
!= sz
) )
459 WARN("Failed to Write\n");
467 IStream_Release( stm
);
472 static void free_table( MSITABLE
*table
)
475 for( i
=0; i
<table
->row_count
; i
++ )
476 msi_free( table
->data
[i
] );
477 msi_free( table
->data
);
478 for( i
=0; i
<table
->nonpersistent_row_count
; i
++ )
479 msi_free( table
->nonpersistent_data
[i
] );
480 msi_free( table
->nonpersistent_data
);
481 if( (table
->colinfo
!= _Tables_cols
) &&
482 (table
->colinfo
!= _Columns_cols
) )
484 msi_free_colinfo( table
->colinfo
, table
->col_count
);
485 msi_free( table
->colinfo
);
490 static UINT
msi_table_get_row_size( const MSICOLUMNINFO
*cols
, UINT count
)
492 const MSICOLUMNINFO
*last_col
= &cols
[count
-1];
495 return last_col
->offset
+ bytes_per_column( last_col
);
498 /* add this table to the list of cached tables in the database */
499 static UINT
read_table_from_storage( MSITABLE
*t
, IStorage
*stg
)
501 BYTE
*rawdata
= NULL
;
502 UINT rawsize
= 0, i
, j
, row_size
= 0;
504 TRACE("%s\n",debugstr_w(t
->name
));
506 row_size
= msi_table_get_row_size( t
->colinfo
, t
->col_count
);
508 /* if we can't read the table, just assume that it's empty */
509 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
511 return ERROR_SUCCESS
;
513 TRACE("Read %d bytes\n", rawsize
);
515 if( rawsize
% row_size
)
517 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
521 t
->row_count
= rawsize
/ row_size
;
522 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
526 /* transpose all the data */
527 TRACE("Transposing data from %d rows\n", t
->row_count
);
528 for( i
=0; i
<t
->row_count
; i
++ )
530 t
->data
[i
] = msi_alloc( row_size
);
534 for( j
=0; j
<t
->col_count
; j
++ )
536 UINT ofs
= t
->colinfo
[j
].offset
;
537 UINT n
= bytes_per_column( &t
->colinfo
[j
] );
540 if ( n
!= 2 && n
!= 3 && n
!= 4 )
542 ERR("oops - unknown column width %d\n", n
);
546 for ( k
= 0; k
< n
; k
++ )
547 t
->data
[i
][ofs
+ k
] = rawdata
[ofs
*t
->row_count
+ i
* n
+ k
];
552 return ERROR_SUCCESS
;
555 return ERROR_FUNCTION_FAILED
;
558 void free_cached_tables( MSIDATABASE
*db
)
560 while( !list_empty( &db
->tables
) )
562 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
564 list_remove( &t
->entry
);
569 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
573 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
574 if( !lstrcmpW( name
, t
->name
) )
580 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
582 UINT r
, column_count
= 0;
583 MSICOLUMNINFO
*columns
;
585 /* get the number of columns in this table */
587 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
588 if( r
!= ERROR_SUCCESS
)
591 /* if there's no columns, there's no table */
592 if( column_count
== 0 )
593 return ERROR_INVALID_PARAMETER
;
595 TRACE("Table %s found\n", debugstr_w(name
) );
597 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
) );
599 return ERROR_FUNCTION_FAILED
;
601 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
602 if( r
!= ERROR_SUCCESS
)
605 return ERROR_FUNCTION_FAILED
;
609 *pcount
= column_count
;
614 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
615 BOOL persistent
, MSITABLE
**table_ret
)
619 MSIRECORD
*rec
= NULL
;
624 /* only add tables that don't exist already */
625 if( TABLE_Exists(db
, name
) )
627 WARN("table %s exists\n", debugstr_w(name
));
628 return ERROR_BAD_QUERY_SYNTAX
;
631 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
633 return ERROR_FUNCTION_FAILED
;
635 table
->row_count
= 0;
637 table
->nonpersistent_row_count
= 0;
638 table
->nonpersistent_data
= NULL
;
639 table
->colinfo
= NULL
;
640 table
->col_count
= 0;
641 table
->persistent
= persistent
;
642 lstrcpyW( table
->name
, name
);
644 for( col
= col_info
; col
; col
= col
->next
)
647 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
651 return ERROR_FUNCTION_FAILED
;
654 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
656 table
->colinfo
[ i
].tablename
= strdupW( col
->table
);
657 table
->colinfo
[ i
].number
= i
+ 1;
658 table
->colinfo
[ i
].colname
= strdupW( col
->column
);
659 table
->colinfo
[ i
].type
= col
->type
;
660 table
->colinfo
[ i
].offset
= 0;
661 table
->colinfo
[ i
].hash_table
= NULL
;
663 table_calc_column_offsets( table
->colinfo
, table
->col_count
);
665 r
= TABLE_CreateView( db
, szTables
, &tv
);
666 TRACE("CreateView returned %x\n", r
);
673 r
= tv
->ops
->execute( tv
, 0 );
674 TRACE("tv execute returned %x\n", r
);
678 rec
= MSI_CreateRecord( 1 );
682 r
= MSI_RecordSetStringW( rec
, 1, name
);
686 r
= tv
->ops
->insert_row( tv
, rec
, !persistent
);
687 TRACE("insert_row returned %x\n", r
);
691 tv
->ops
->delete( tv
);
694 msiobj_release( &rec
->hdr
);
699 /* add each column to the _Columns table */
700 r
= TABLE_CreateView( db
, szColumns
, &tv
);
704 r
= tv
->ops
->execute( tv
, 0 );
705 TRACE("tv execute returned %x\n", r
);
709 rec
= MSI_CreateRecord( 4 );
713 r
= MSI_RecordSetStringW( rec
, 1, name
);
718 * need to set the table, column number, col name and type
719 * for each column we enter in the table
722 for( col
= col_info
; col
; col
= col
->next
)
724 r
= MSI_RecordSetInteger( rec
, 2, nField
);
728 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
732 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
736 r
= tv
->ops
->insert_row( tv
, rec
, FALSE
);
748 msiobj_release( &rec
->hdr
);
749 /* FIXME: remove values from the string table on error */
751 tv
->ops
->delete( tv
);
753 if (r
== ERROR_SUCCESS
)
755 list_add_head( &db
->tables
, &table
->entry
);
764 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
769 /* first, see if the table is cached */
770 table
= find_cached_table( db
, name
);
774 return ERROR_SUCCESS
;
777 /* nonexistent tables should be interpreted as empty tables */
778 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
780 return ERROR_FUNCTION_FAILED
;
782 table
->row_count
= 0;
784 table
->nonpersistent_row_count
= 0;
785 table
->nonpersistent_data
= NULL
;
786 table
->colinfo
= NULL
;
787 table
->col_count
= 0;
788 table
->persistent
= TRUE
;
789 lstrcpyW( table
->name
, name
);
791 /* these two tables are special - we know the column types already */
792 if( !lstrcmpW( name
, szColumns
) )
794 table
->colinfo
= _Columns_cols
;
795 table
->col_count
= sizeof(_Columns_cols
)/sizeof(_Columns_cols
[0]);
797 else if( !lstrcmpW( name
, szTables
) )
799 table
->colinfo
= _Tables_cols
;
800 table
->col_count
= sizeof(_Tables_cols
)/sizeof(_Tables_cols
[0]);
804 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
805 if (r
!= ERROR_SUCCESS
)
807 free_table ( table
);
812 r
= read_table_from_storage( table
, db
->storage
);
813 if( r
!= ERROR_SUCCESS
)
819 list_add_head( &db
->tables
, &table
->entry
);
821 return ERROR_SUCCESS
;
824 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
)
826 BYTE
*rawdata
= NULL
, *p
;
827 UINT rawsize
, r
, i
, j
, row_size
;
829 /* Nothing to do for non-persistent tables */
831 return ERROR_SUCCESS
;
833 TRACE("Saving %s\n", debugstr_w( t
->name
) );
835 row_size
= msi_table_get_row_size( t
->colinfo
, t
->col_count
);
837 rawsize
= t
->row_count
* row_size
;
838 rawdata
= msi_alloc_zero( rawsize
);
841 r
= ERROR_NOT_ENOUGH_MEMORY
;
846 for( i
=0; i
<t
->col_count
; i
++ )
848 for( j
=0; j
<t
->row_count
; j
++ )
850 UINT offset
= t
->colinfo
[i
].offset
;
852 *p
++ = t
->data
[j
][offset
];
853 *p
++ = t
->data
[j
][offset
+ 1];
854 if( 4 == bytes_per_column( &t
->colinfo
[i
] ) )
856 *p
++ = t
->data
[j
][offset
+ 2];
857 *p
++ = t
->data
[j
][offset
+ 3];
862 TRACE("writing %d bytes\n", rawsize
);
863 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
871 static void table_calc_column_offsets( MSICOLUMNINFO
*colinfo
, DWORD count
)
875 for( i
=0; colinfo
&& (i
<count
); i
++ )
877 assert( (i
+1) == colinfo
[ i
].number
);
879 colinfo
[i
].offset
= colinfo
[ i
- 1 ].offset
880 + bytes_per_column( &colinfo
[ i
- 1 ] );
882 colinfo
[i
].offset
= 0;
883 TRACE("column %d is [%s] with type %08x ofs %d\n",
884 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
885 colinfo
[i
].type
, colinfo
[i
].offset
);
889 static UINT
get_defaulttablecolumns( LPCWSTR name
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
891 const MSICOLUMNINFO
*p
;
894 TRACE("%s\n", debugstr_w(name
));
896 if (!lstrcmpW( name
, szTables
))
901 else if (!lstrcmpW( name
, szColumns
))
907 return ERROR_FUNCTION_FAILED
;
909 /* duplicate the string data so we can free it in msi_free_colinfo */
912 if (colinfo
&& (i
< *sz
) )
914 memcpy( &colinfo
[i
], &p
[i
], sizeof(MSICOLUMNINFO
) );
915 colinfo
[i
].tablename
= strdupW( p
[i
].tablename
);
916 colinfo
[i
].colname
= strdupW( p
[i
].colname
);
918 if( colinfo
&& (i
>= *sz
) )
921 table_calc_column_offsets( colinfo
, n
);
923 return ERROR_SUCCESS
;
926 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
930 for( i
=0; i
<count
; i
++ )
932 msi_free( (LPWSTR
) colinfo
[i
].tablename
);
933 msi_free( (LPWSTR
) colinfo
[i
].colname
);
934 msi_free( colinfo
[i
].hash_table
);
938 static LPWSTR
msi_makestring( const MSIDATABASE
*db
, UINT stringid
)
940 return strdupW(msi_string_lookup_id( db
->strings
, stringid
));
943 static UINT
read_table_int(BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
947 for (i
= 0; i
< bytes
; i
++)
948 ret
+= (data
[row
][col
+ i
] << i
* 8);
953 static UINT
get_tablecolumns( MSIDATABASE
*db
,
954 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
956 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
957 MSITABLE
*table
= NULL
;
959 TRACE("%s\n", debugstr_w(szTableName
));
961 /* first check if there is a default table with that name */
962 r
= get_defaulttablecolumns( szTableName
, colinfo
, sz
);
963 if( ( r
== ERROR_SUCCESS
) && *sz
)
966 r
= get_table( db
, szColumns
, &table
);
967 if( r
!= ERROR_SUCCESS
)
969 ERR("couldn't load _Columns table\n");
970 return ERROR_FUNCTION_FAILED
;
973 /* convert table and column names to IDs from the string table */
974 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
975 if( r
!= ERROR_SUCCESS
)
977 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
981 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
983 /* Note: _Columns table doesn't have non-persistent data */
985 /* if maxcount is non-zero, assume it's exactly right for this table */
986 memset( colinfo
, 0, maxcount
*sizeof(*colinfo
) );
987 count
= table
->row_count
;
988 for( i
=0; i
<count
; i
++ )
990 if( read_table_int(table
->data
, i
, 0, db
->bytes_per_strref
) != table_id
)
994 UINT id
= read_table_int(table
->data
, i
, _Columns_cols
[2].offset
, db
->bytes_per_strref
);
995 UINT col
= read_table_int(table
->data
, i
, _Columns_cols
[1].offset
, sizeof(USHORT
)) - (1<<15);
997 /* check the column number is in range */
998 if (col
<1 || col
>maxcount
)
1000 ERR("column %d out of range\n", col
);
1004 /* check if this column was already set */
1005 if (colinfo
[ col
- 1 ].number
)
1007 ERR("duplicate column %d\n", col
);
1011 colinfo
[ col
- 1 ].tablename
= msi_makestring( db
, table_id
);
1012 colinfo
[ col
- 1 ].number
= col
;
1013 colinfo
[ col
- 1 ].colname
= msi_makestring( db
, id
);
1014 colinfo
[ col
- 1 ].type
= read_table_int(table
->data
, i
, _Columns_cols
[3].offset
, sizeof(USHORT
)) - (1<<15);
1015 colinfo
[ col
- 1 ].offset
= 0;
1016 colinfo
[ col
- 1 ].hash_table
= NULL
;
1021 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
1023 if (colinfo
&& n
!= maxcount
)
1025 ERR("missing column in table %s\n", debugstr_w(szTableName
));
1026 msi_free_colinfo(colinfo
, maxcount
);
1027 return ERROR_FUNCTION_FAILED
;
1030 table_calc_column_offsets( colinfo
, n
);
1033 return ERROR_SUCCESS
;
1036 /* try to find the table name in the _Tables table */
1037 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
1039 UINT r
, table_id
= 0, i
, count
;
1040 MSITABLE
*table
= NULL
;
1042 if( !lstrcmpW( name
, szTables
) )
1044 if( !lstrcmpW( name
, szColumns
) )
1047 r
= msi_string2idW( db
->strings
, name
, &table_id
);
1048 if( r
!= ERROR_SUCCESS
)
1050 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1054 r
= get_table( db
, szTables
, &table
);
1055 if( r
!= ERROR_SUCCESS
)
1057 ERR("table %s not available\n", debugstr_w(szTables
));
1061 count
= table
->row_count
;
1062 for( i
=0; i
<count
; i
++ )
1063 if( table
->data
[ i
][ 0 ] == table_id
)
1069 count
= table
->nonpersistent_row_count
;
1070 for( i
=0; i
<count
; i
++ )
1071 if( table
->nonpersistent_data
[ i
][ 0 ] == table_id
)
1077 TRACE("Searched %d tables, but %d was not found\n", count
, table_id
);
1082 /* below is the query interface to a table */
1084 typedef struct tagMSITABLEVIEW
1089 MSICOLUMNINFO
*columns
;
1095 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1097 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1102 return ERROR_INVALID_PARAMETER
;
1104 if( (col
==0) || (col
>tv
->num_cols
) )
1105 return ERROR_INVALID_PARAMETER
;
1107 /* how many rows are there ? */
1108 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1109 return ERROR_NO_MORE_ITEMS
;
1111 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1113 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1114 ERR("%p %p\n", tv
, tv
->columns
);
1115 return ERROR_FUNCTION_FAILED
;
1118 if (row
>= tv
->table
->row_count
)
1120 row
-= tv
->table
->row_count
;
1121 data
= tv
->table
->nonpersistent_data
;
1124 data
= tv
->table
->data
;
1126 n
= bytes_per_column( &tv
->columns
[col
-1] );
1127 if (n
!= 2 && n
!= 3 && n
!= 4)
1129 ERR("oops! what is %d bytes per column?\n", n
);
1130 return ERROR_FUNCTION_FAILED
;
1133 offset
= tv
->columns
[col
-1].offset
;
1134 *val
= read_table_int(data
, row
, offset
, n
);
1136 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1138 return ERROR_SUCCESS
;
1142 * We need a special case for streams, as we need to reference column with
1143 * the name of the stream in the same table, and the table name
1144 * which may not be available at higher levels of the query
1146 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1148 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1149 UINT ival
= 0, refcol
= 0, r
;
1153 static const WCHAR szDot
[] = { '.', 0 };
1156 if( !view
->ops
->fetch_int
)
1157 return ERROR_INVALID_PARAMETER
;
1160 * The column marked with the type stream data seems to have a single number
1161 * which references the column containing the name of the stream data
1163 * Fetch the column to reference first.
1165 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1166 if( r
!= ERROR_SUCCESS
)
1169 /* check the column value is in range */
1170 if (ival
< 0 || ival
> tv
->num_cols
|| ival
== col
)
1172 ERR("bad column ref (%u) for stream\n", ival
);
1173 return ERROR_FUNCTION_FAILED
;
1176 if ( tv
->columns
[ival
- 1].type
& MSITYPE_STRING
)
1178 /* now get the column with the name of the stream */
1179 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1180 if ( r
!= ERROR_SUCCESS
)
1183 /* lookup the string value from the string table */
1184 sval
= msi_string_lookup_id( tv
->db
->strings
, refcol
);
1186 return ERROR_INVALID_PARAMETER
;
1190 static const WCHAR fmt
[] = { '%','d',0 };
1191 sprintfW( number
, fmt
, ival
);
1195 len
= lstrlenW( tv
->name
) + 2 + lstrlenW( sval
);
1196 full_name
= msi_alloc( len
*sizeof(WCHAR
) );
1197 lstrcpyW( full_name
, tv
->name
);
1198 lstrcatW( full_name
, szDot
);
1199 lstrcatW( full_name
, sval
);
1201 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1203 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1204 msi_free( full_name
);
1209 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1215 return ERROR_INVALID_PARAMETER
;
1217 if( (col
==0) || (col
>tv
->num_cols
) )
1218 return ERROR_INVALID_PARAMETER
;
1220 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1221 return ERROR_INVALID_PARAMETER
;
1223 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1225 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1226 ERR("%p %p\n", tv
, tv
->columns
);
1227 return ERROR_FUNCTION_FAILED
;
1230 msi_free( tv
->columns
[col
-1].hash_table
);
1231 tv
->columns
[col
-1].hash_table
= NULL
;
1233 if (row
>= tv
->table
->row_count
)
1235 row
-= tv
->table
->row_count
;
1236 data
= tv
->table
->nonpersistent_data
;
1239 data
= tv
->table
->data
;
1241 n
= bytes_per_column( &tv
->columns
[col
-1] );
1242 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1244 ERR("oops! what is %d bytes per column?\n", n
);
1245 return ERROR_FUNCTION_FAILED
;
1248 offset
= tv
->columns
[col
-1].offset
;
1249 for ( i
= 0; i
< n
; i
++ )
1250 data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1252 return ERROR_SUCCESS
;
1255 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1257 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1258 UINT i
, val
, r
= ERROR_SUCCESS
;
1261 return ERROR_INVALID_PARAMETER
;
1263 /* test if any of the mask bits are invalid */
1264 if ( mask
>= (1<<tv
->num_cols
) )
1265 return ERROR_INVALID_PARAMETER
;
1267 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1271 /* only update the fields specified in the mask */
1272 if ( !(mask
&(1<<i
)) )
1275 /* if row >= tv->table->row_count then it is a non-persistent row */
1276 persistent
= tv
->table
->persistent
&& (row
< tv
->table
->row_count
);
1277 /* FIXME: should we allow updating keys? */
1280 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1282 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1284 val
= 1; /* refers to the first key column */
1286 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1288 LPCWSTR sval
= MSI_RecordGetString( rec
, i
+ 1 );
1289 val
= msi_addstringW( tv
->db
->strings
, 0, sval
, -1, 1,
1290 persistent
? StringPersistent
: StringNonPersistent
);
1292 else if ( 2 == bytes_per_column( &tv
->columns
[ i
] ) )
1294 val
= 0x8000 + MSI_RecordGetInteger( rec
, i
+ 1 );
1295 if ( val
& 0xffff0000 )
1297 ERR("field %u value %d out of range\n", i
+1, val
- 0x8000 );
1298 return ERROR_FUNCTION_FAILED
;
1303 INT ival
= MSI_RecordGetInteger( rec
, i
+ 1 );
1304 val
= ival
^ 0x80000000;
1308 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1309 if ( r
!= ERROR_SUCCESS
)
1315 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1317 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1323 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1326 return ERROR_INVALID_PARAMETER
;
1328 row
= msi_alloc_zero( tv
->row_size
);
1330 return ERROR_NOT_ENOUGH_MEMORY
;
1334 row_count
= &tv
->table
->nonpersistent_row_count
;
1335 data_ptr
= &tv
->table
->nonpersistent_data
;
1336 *num
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1340 row_count
= &tv
->table
->row_count
;
1341 data_ptr
= &tv
->table
->data
;
1342 *num
= tv
->table
->row_count
;
1345 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1347 p
= msi_realloc( *data_ptr
, sz
);
1349 p
= msi_alloc( sz
);
1353 return ERROR_NOT_ENOUGH_MEMORY
;
1357 (*data_ptr
)[*row_count
] = row
;
1360 return ERROR_SUCCESS
;
1363 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1365 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1367 TRACE("%p %p\n", tv
, record
);
1369 TRACE("There are %d columns\n", tv
->num_cols
);
1371 return ERROR_SUCCESS
;
1374 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1376 TRACE("%p\n", view
);
1378 return ERROR_SUCCESS
;
1381 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1383 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1385 TRACE("%p %p %p\n", view
, rows
, cols
);
1388 *cols
= tv
->num_cols
;
1392 return ERROR_INVALID_PARAMETER
;
1393 *rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1396 return ERROR_SUCCESS
;
1399 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1400 UINT n
, LPWSTR
*name
, UINT
*type
)
1402 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1404 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1406 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1407 return ERROR_INVALID_PARAMETER
;
1411 *name
= strdupW( tv
->columns
[n
-1].colname
);
1413 return ERROR_FUNCTION_FAILED
;
1416 *type
= tv
->columns
[n
-1].type
;
1418 return ERROR_SUCCESS
;
1421 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
);
1423 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1427 /* check there's no null values where they're not allowed */
1428 for( i
= 0; i
< tv
->num_cols
; i
++ )
1430 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1433 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1434 TRACE("skipping binary column\n");
1435 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1439 str
= MSI_RecordGetString( rec
, i
+1 );
1440 if (str
== NULL
|| str
[0] == 0)
1441 return ERROR_INVALID_DATA
;
1447 n
= MSI_RecordGetInteger( rec
, i
+1 );
1448 if (n
== MSI_NULL_INTEGER
)
1449 return ERROR_INVALID_DATA
;
1453 /* check there's no duplicate keys */
1454 r
= msi_table_find_row( tv
, rec
, &row
);
1455 if (r
== ERROR_SUCCESS
)
1456 return ERROR_INVALID_DATA
;
1458 return ERROR_SUCCESS
;
1461 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, BOOL temporary
)
1463 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1466 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1468 /* check that the key is unique - can we find a matching row? */
1469 r
= table_validate_new( tv
, rec
);
1470 if( r
!= ERROR_SUCCESS
)
1471 return ERROR_FUNCTION_FAILED
;
1473 r
= table_create_new_row( view
, &row
, temporary
);
1474 TRACE("insert_row returned %08x\n", r
);
1475 if( r
!= ERROR_SUCCESS
)
1478 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1481 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1483 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1484 UINT r
, num_rows
, num_cols
;
1487 TRACE("%p %d\n", tv
, row
);
1490 return ERROR_INVALID_PARAMETER
;
1492 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1493 if ( r
!= ERROR_SUCCESS
)
1496 if ( row
>= num_rows
)
1497 return ERROR_FUNCTION_FAILED
;
1499 tv
->table
->row_count
--;
1501 if ( row
== num_rows
- 1 )
1502 return ERROR_SUCCESS
;
1504 dest
= tv
->table
->data
[row
];
1505 src
= tv
->table
->data
[row
+ 1];
1506 memmove(dest
, src
, (num_rows
- row
- 1) * tv
->row_size
);
1507 return ERROR_SUCCESS
;
1510 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1513 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1516 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1518 switch (eModifyMode
)
1520 case MSIMODIFY_VALIDATE_NEW
:
1521 r
= table_validate_new( tv
, rec
);
1524 case MSIMODIFY_INSERT_TEMPORARY
:
1525 r
= table_validate_new( tv
, rec
);
1526 if (r
!= ERROR_SUCCESS
)
1528 r
= TABLE_insert_row( view
, rec
, TRUE
);
1531 case MSIMODIFY_REFRESH
:
1532 case MSIMODIFY_INSERT
:
1533 case MSIMODIFY_UPDATE
:
1534 case MSIMODIFY_ASSIGN
:
1535 case MSIMODIFY_REPLACE
:
1536 case MSIMODIFY_MERGE
:
1537 case MSIMODIFY_DELETE
:
1538 case MSIMODIFY_VALIDATE
:
1539 case MSIMODIFY_VALIDATE_FIELD
:
1540 case MSIMODIFY_VALIDATE_DELETE
:
1541 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1542 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1546 r
= ERROR_INVALID_DATA
;
1552 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1554 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1556 TRACE("%p\n", view
);
1564 return ERROR_SUCCESS
;
1567 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1568 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1570 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1571 const MSICOLUMNHASHENTRY
*entry
;
1573 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1576 return ERROR_INVALID_PARAMETER
;
1578 if( (col
==0) || (col
> tv
->num_cols
) )
1579 return ERROR_INVALID_PARAMETER
;
1581 if( !tv
->columns
[col
-1].hash_table
)
1584 UINT num_rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1585 MSICOLUMNHASHENTRY
**hash_table
;
1586 MSICOLUMNHASHENTRY
*new_entry
;
1588 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1590 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1591 ERR("%p %p\n", tv
, tv
->columns
);
1592 return ERROR_FUNCTION_FAILED
;
1595 /* allocate contiguous memory for the table and its entries so we
1596 * don't have to do an expensive cleanup */
1597 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1598 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1600 return ERROR_OUTOFMEMORY
;
1602 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1603 tv
->columns
[col
-1].hash_table
= hash_table
;
1605 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1607 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1611 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1614 new_entry
->next
= NULL
;
1615 new_entry
->value
= row_value
;
1617 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1619 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1620 while (prev_entry
->next
)
1621 prev_entry
= prev_entry
->next
;
1622 prev_entry
->next
= new_entry
;
1625 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1630 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1632 entry
= (*handle
)->next
;
1634 while (entry
&& entry
->value
!= val
)
1635 entry
= entry
->next
;
1639 return ERROR_NO_MORE_ITEMS
;
1642 return ERROR_SUCCESS
;
1646 static const MSIVIEWOPS table_ops
=
1655 TABLE_get_dimensions
,
1656 TABLE_get_column_info
,
1659 TABLE_find_matching_rows
1662 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
1667 static const WCHAR Streams
[] = {'_','S','t','r','e','a','m','s',0};
1669 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
1671 if ( !lstrcmpW( name
, Streams
) )
1672 return STREAMS_CreateView( db
, view
);
1674 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
1675 tv
= msi_alloc_zero( sz
);
1677 return ERROR_FUNCTION_FAILED
;
1679 r
= get_table( db
, name
, &tv
->table
);
1680 if( r
!= ERROR_SUCCESS
)
1683 WARN("table not found\n");
1687 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
1689 /* fill the structure */
1690 tv
->view
.ops
= &table_ops
;
1692 tv
->columns
= tv
->table
->colinfo
;
1693 tv
->num_cols
= tv
->table
->col_count
;
1694 tv
->row_size
= msi_table_get_row_size( tv
->table
->colinfo
, tv
->table
->col_count
);
1696 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
1698 *view
= (MSIVIEW
*) tv
;
1699 lstrcpyW( tv
->name
, name
);
1701 return ERROR_SUCCESS
;
1704 UINT
MSI_CommitTables( MSIDATABASE
*db
)
1707 MSITABLE
*table
= NULL
;
1711 r
= msi_save_string_table( db
->strings
, db
->storage
);
1712 if( r
!= ERROR_SUCCESS
)
1714 WARN("failed to save string table r=%08x\n",r
);
1718 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
1720 r
= save_table( db
, table
);
1721 if( r
!= ERROR_SUCCESS
)
1723 WARN("failed to save table %s (r=%08x)\n",
1724 debugstr_w(table
->name
), r
);
1729 /* force everything to reload next time */
1730 free_cached_tables( db
);
1732 return ERROR_SUCCESS
;
1735 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
1740 TRACE("%p %s\n", db
, debugstr_w(table
));
1743 return MSICONDITION_ERROR
;
1745 r
= get_table( db
, table
, &t
);
1746 if (r
!= ERROR_SUCCESS
)
1747 return MSICONDITION_NONE
;
1750 return MSICONDITION_TRUE
;
1752 return MSICONDITION_FALSE
;
1755 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
1759 for (i
= 0; i
< bytes
; i
++)
1760 ret
+= (data
[col
+ i
] << i
* 8);
1765 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
1766 const BYTE
*rawdata
, UINT bytes_per_strref
)
1768 UINT i
, val
, ofs
= 0;
1770 MSICOLUMNINFO
*columns
= tv
->columns
;
1773 mask
= rawdata
[0] | (rawdata
[1] << 8);
1776 rec
= MSI_CreateRecord( tv
->num_cols
);
1781 for( i
=0; i
<tv
->num_cols
; i
++ )
1783 if ( (mask
&1) && (i
>=(mask
>>8)) )
1785 /* all keys must be present */
1786 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
1789 if( (columns
[i
].type
& MSITYPE_STRING
) &&
1790 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1794 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
1795 sval
= msi_string_lookup_id( st
, val
);
1796 MSI_RecordSetStringW( rec
, i
+1, sval
);
1797 TRACE(" field %d [%s]\n", i
+1, debugstr_w(sval
));
1798 ofs
+= bytes_per_strref
;
1802 UINT n
= bytes_per_column( &columns
[i
] );
1806 val
= read_raw_int(rawdata
, ofs
, n
);
1808 MSI_RecordSetInteger( rec
, i
+1, val
^0x8000 );
1809 TRACE(" field %d [0x%04x]\n", i
+1, val
);
1812 val
= read_raw_int(rawdata
, ofs
, n
);
1814 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
1815 TRACE(" field %d [0x%08x]\n", i
+1, val
);
1818 ERR("oops - unknown column width %d\n", n
);
1827 static void dump_record( MSIRECORD
*rec
)
1831 n
= MSI_RecordGetFieldCount( rec
);
1832 for( i
=1; i
<=n
; i
++ )
1834 LPCWSTR sval
= MSI_RecordGetString( rec
, i
);
1836 if( MSI_RecordIsNull( rec
, i
) )
1837 TRACE("row -> []\n");
1838 else if( (sval
= MSI_RecordGetString( rec
, i
)) )
1839 TRACE("row -> [%s]\n", debugstr_w(sval
));
1841 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
1845 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
1850 for( i
=0; i
<(rawsize
/2); i
++ )
1852 sval
= msi_string_lookup_id( st
, rawdata
[i
] );
1853 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_w(sval
) );
1857 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1862 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
1863 for( i
=0; i
<tv
->num_cols
; i
++ )
1867 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
1870 /* turn the transform column value into a row value */
1871 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
1872 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1874 str
= MSI_RecordGetString( rec
, i
+1 );
1875 r
= msi_string2idW( tv
->db
->strings
, str
, &data
[i
] );
1877 /* if there's no matching string in the string table,
1878 these keys can't match any record, so fail now. */
1879 if( ERROR_SUCCESS
!= r
)
1887 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
1888 if ((tv
->columns
[i
].type
&0xff) == 2)
1891 data
[i
] += 0x80000000;
1897 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
)
1899 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
1901 for( i
=0; i
<tv
->num_cols
; i
++ )
1903 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
1906 /* turn the transform column value into a row value */
1907 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
1908 if ( r
!= ERROR_SUCCESS
)
1910 ERR("TABLE_fetch_int shouldn't fail here\n");
1914 /* if this key matches, move to the next column */
1917 ret
= ERROR_FUNCTION_FAILED
;
1921 ret
= ERROR_SUCCESS
;
1927 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
)
1929 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
1931 data
= msi_record_to_row( tv
, rec
);
1934 for( i
= 0; i
< tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
; i
++ )
1936 r
= msi_row_matches( tv
, i
, data
);
1937 if( r
== ERROR_SUCCESS
)
1947 static void msi_update_table_columns( MSIDATABASE
*db
, LPWSTR name
)
1953 table
= find_cached_table( db
, name
);
1954 msi_free( table
->colinfo
);
1955 table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
1957 size
= msi_table_get_row_size( table
->colinfo
, table
->col_count
);
1958 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
1960 for ( n
= 0; n
< table
->row_count
; n
++ )
1962 table
->data
[n
] = msi_realloc( table
->data
[n
], size
);
1963 table
->data
[n
][offset
] = (BYTE
)MSI_NULL_INTEGER
;
1973 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
1974 string_table
*st
, TRANSFORMDATA
*transform
,
1975 UINT bytes_per_strref
)
1978 BYTE
*rawdata
= NULL
;
1979 MSITABLEVIEW
*tv
= NULL
;
1980 UINT r
, n
, sz
, i
, mask
;
1981 MSIRECORD
*rec
= NULL
;
1987 return ERROR_SUCCESS
;
1989 name
= transform
->name
;
1992 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
1994 /* read the transform data */
1995 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
1998 TRACE("table %s empty\n", debugstr_w(name
) );
1999 return ERROR_INVALID_TABLE
;
2002 /* create a table view */
2003 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2004 if( r
!= ERROR_SUCCESS
)
2007 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2008 if( r
!= ERROR_SUCCESS
)
2011 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2012 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2014 /* interpret the data */
2016 for( n
=0; n
< rawsize
; )
2018 mask
= rawdata
[n
] | (rawdata
[n
+1] << 8);
2023 * if the low bit is set, columns are continuous and
2024 * the number of columns is specified in the high byte
2027 for( i
=0; i
<tv
->num_cols
; i
++ )
2029 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2030 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2031 sz
+= bytes_per_strref
;
2033 sz
+= bytes_per_column( &tv
->columns
[i
] );
2039 * If the low bit is not set, mask is a bitmask.
2040 * Excepting for key fields, which are always present,
2041 * each bit indicates that a field is present in the transform record.
2043 * mask == 0 is a special case ... only the keys will be present
2044 * and it means that this row should be deleted.
2047 for( i
=0; i
<tv
->num_cols
; i
++ )
2049 if( (tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1<<i
)&mask
))
2051 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2052 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2053 sz
+= bytes_per_strref
;
2055 sz
+= bytes_per_column( &tv
->columns
[i
] );
2060 /* check we didn't run of the end of the table */
2061 if ( (n
+sz
) > rawsize
)
2064 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2068 rec
= msi_get_transform_record( tv
, st
, &rawdata
[n
], bytes_per_strref
);
2075 UINT number
= MSI_NULL_INTEGER
;
2077 TRACE("inserting record\n");
2079 if (!lstrcmpW(name
, szColumns
))
2081 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2082 number
= MSI_RecordGetInteger( rec
, 2 );
2085 * Native msi seems writes nul into the Number (2nd) column of
2086 * the _Columns table, only when the columns are from a new table
2088 if ( number
== MSI_NULL_INTEGER
)
2090 /* reset the column number on a new table */
2091 if ( lstrcmpW(coltable
, table
) )
2094 lstrcpyW( coltable
, table
);
2097 /* fix nul column numbers */
2098 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2102 r
= TABLE_insert_row( &tv
->view
, rec
, FALSE
);
2103 if (r
!= ERROR_SUCCESS
)
2104 ERR("insert row failed\n");
2106 if ( number
!= MSI_NULL_INTEGER
&& !lstrcmpW(name
, szColumns
) )
2107 msi_update_table_columns( db
, table
);
2113 r
= msi_table_find_row( tv
, rec
, &row
);
2114 if (r
!= ERROR_SUCCESS
)
2115 ERR("no matching row to transform\n");
2118 TRACE("modifying row [%d]:\n", row
);
2119 TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2123 TRACE("deleting row [%d]:\n", row
);
2124 TABLE_delete_row( &tv
->view
, row
);
2127 if( TRACE_ON(msidb
) ) dump_record( rec
);
2128 msiobj_release( &rec
->hdr
);
2135 /* no need to free the table, it's associated with the database */
2136 msi_free( rawdata
);
2138 tv
->view
.ops
->delete( &tv
->view
);
2140 return ERROR_SUCCESS
;
2144 * msi_table_apply_transform
2146 * Enumerate the table transforms in a transform storage and apply each one.
2148 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2150 struct list transforms
;
2151 IEnumSTATSTG
*stgenum
= NULL
;
2152 TRANSFORMDATA
*transform
;
2153 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2156 string_table
*strings
;
2157 UINT ret
= ERROR_FUNCTION_FAILED
;
2158 UINT bytes_per_strref
;
2160 TRACE("%p %p\n", db
, stg
);
2162 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2166 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2170 list_init(&transforms
);
2174 MSITABLEVIEW
*tv
= NULL
;
2178 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2179 if ( FAILED( r
) || !count
)
2182 decode_streamname( stat
.pwcsName
, name
);
2183 if ( name
[0] != 0x4840 )
2186 if ( !lstrcmpW( name
+1, szStringPool
) ||
2187 !lstrcmpW( name
+1, szStringData
) )
2190 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2194 list_add_tail( &transforms
, &transform
->entry
);
2196 transform
->name
= strdupW( name
+ 1 );
2198 if ( !lstrcmpW( transform
->name
, szTables
) )
2200 else if (!lstrcmpW( transform
->name
, szColumns
) )
2201 columns
= transform
;
2203 TRACE("transform contains stream %s\n", debugstr_w(name
));
2205 /* load the table */
2206 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2207 if( r
!= ERROR_SUCCESS
)
2210 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2211 if( r
!= ERROR_SUCCESS
)
2213 tv
->view
.ops
->delete( &tv
->view
);
2217 tv
->view
.ops
->delete( &tv
->view
);
2221 * Apply _Tables and _Columns transforms first so that
2222 * the table metadata is correct, and empty tables exist.
2224 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2225 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2228 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2229 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2232 ret
= ERROR_SUCCESS
;
2234 while ( !list_empty( &transforms
) )
2236 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2238 if ( lstrcmpW( transform
->name
, szColumns
) &&
2239 lstrcmpW( transform
->name
, szTables
) &&
2240 ret
== ERROR_SUCCESS
)
2242 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2245 list_remove( &transform
->entry
);
2246 msi_free( transform
->name
);
2247 msi_free( transform
);
2250 if ( ret
== ERROR_SUCCESS
)
2251 append_storage_to_db( db
, stg
);
2255 IEnumSTATSTG_Release( stgenum
);
2257 msi_destroy_stringtable( strings
);
2262 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
2266 t
= msi_alloc( sizeof *t
);
2268 IStorage_AddRef( stg
);
2269 list_add_tail( &db
->transforms
, &t
->entry
);
2272 void msi_free_transforms( MSIDATABASE
*db
)
2274 while( !list_empty( &db
->transforms
) )
2276 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
),
2277 MSITRANSFORM
, entry
);
2278 list_remove( &t
->entry
);
2279 IStorage_Release( t
->stg
);