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
;
78 typedef struct tagMSITRANSFORM
{
83 static const WCHAR szStringData
[] = {
84 '_','S','t','r','i','n','g','D','a','t','a',0 };
85 static const WCHAR szStringPool
[] = {
86 '_','S','t','r','i','n','g','P','o','o','l',0 };
88 /* information for default tables */
89 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
90 static const WCHAR szTable
[] = { 'T','a','b','l','e',0 };
91 static const WCHAR szName
[] = { 'N','a','m','e',0 };
92 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
93 static const WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
94 static const WCHAR szType
[] = { 'T','y','p','e',0 };
96 /* These tables are written into (the .hash_table part).
97 * Do not mark them const.
99 static MSICOLUMNINFO _Columns_cols
[4] = {
100 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| MSITYPE_KEY
| 64, 0 },
101 { szColumns
, 2, szNumber
, MSITYPE_VALID
| MSITYPE_KEY
| 2, 2 },
102 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4 },
103 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6 },
105 static MSICOLUMNINFO _Tables_cols
[1] = {
106 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 0 },
109 #define MAX_STREAM_NAME 0x1f
111 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
112 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
113 static void table_calc_column_offsets( MSICOLUMNINFO
*colinfo
, DWORD count
);
114 static UINT
get_tablecolumns( MSIDATABASE
*db
,
115 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
116 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
);
119 void msi_table_set_strref(UINT bytes_per_strref
)
121 _Columns_cols
[0].offset
= 0;
122 _Columns_cols
[1].offset
= bytes_per_strref
;
123 _Columns_cols
[2].offset
= _Columns_cols
[1].offset
+ sizeof(USHORT
);
124 _Columns_cols
[3].offset
= _Columns_cols
[2].offset
+ bytes_per_strref
;
127 static inline UINT
bytes_per_column( const MSICOLUMNINFO
*col
)
129 if( MSITYPE_IS_BINARY(col
->type
) )
131 if( col
->type
& MSITYPE_STRING
)
132 return _Columns_cols
[1].offset
;
133 if( (col
->type
& 0xff) > 4 )
134 ERR("Invalid column size!\n");
135 return col
->type
& 0xff;
138 static int utf2mime(int x
)
140 if( (x
>='0') && (x
<='9') )
142 if( (x
>='A') && (x
<='Z') )
144 if( (x
>='a') && (x
<='z') )
153 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
155 DWORD count
= MAX_STREAM_NAME
;
160 count
= lstrlenW( in
)+2;
161 out
= msi_alloc( count
*sizeof(WCHAR
) );
177 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
179 ch
= utf2mime(ch
) + 0x4800;
181 if( next
&& (next
<0x80) )
183 next
= utf2mime(next
);
194 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
199 static int mime2utf(int x
)
206 return x
- 10 - 26 + 'a';
207 if( x
== (10+26+26) )
212 BOOL
decode_streamname(LPCWSTR in
, LPWSTR out
)
217 while ( (ch
= *in
++) )
219 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
222 ch
= mime2utf(ch
-0x4800);
226 *out
++ = mime2utf(ch
&0x3f);
228 ch
= mime2utf((ch
>>6)&0x3f);
238 void enum_stream_names( IStorage
*stg
)
240 IEnumSTATSTG
*stgenum
= NULL
;
246 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
254 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
255 if( FAILED( r
) || !count
)
257 decode_streamname( stat
.pwcsName
, name
);
258 TRACE("stream %2d -> %s %s\n", n
,
259 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
263 IEnumSTATSTG_Release( stgenum
);
266 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
, BOOL table
,
267 BYTE
**pdata
, UINT
*psz
)
270 UINT ret
= ERROR_FUNCTION_FAILED
;
277 encname
= encode_streamname(table
, stname
);
279 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
281 r
= IStorage_OpenStream(stg
, encname
, NULL
,
282 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
286 WARN("open stream failed r = %08x - empty table?\n", r
);
290 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
293 WARN("open stream failed r = %08x!\n", r
);
297 if( stat
.cbSize
.QuadPart
>> 32 )
303 sz
= stat
.cbSize
.QuadPart
;
304 data
= msi_alloc( sz
);
307 WARN("couldn't allocate memory r=%08x!\n", r
);
308 ret
= ERROR_NOT_ENOUGH_MEMORY
;
312 r
= IStream_Read(stm
, data
, sz
, &count
);
313 if( FAILED( r
) || ( count
!= sz
) )
316 WARN("read stream failed r = %08x!\n", r
);
325 IStream_Release( stm
);
330 UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
335 encname
= encode_streamname(FALSE
, stname
);
337 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
339 r
= IStorage_OpenStream(db
->storage
, encname
, NULL
,
340 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
343 MSITRANSFORM
*transform
;
345 LIST_FOR_EACH_ENTRY( transform
, &db
->transforms
, MSITRANSFORM
, entry
)
347 TRACE("looking for %s in transform storage\n", debugstr_w(stname
) );
348 r
= IStorage_OpenStream( transform
->stg
, encname
, NULL
,
349 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
357 return SUCCEEDED(r
) ? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
360 UINT
read_raw_stream_data( MSIDATABASE
*db
, LPCWSTR stname
,
361 USHORT
**pdata
, UINT
*psz
)
364 UINT ret
= ERROR_FUNCTION_FAILED
;
370 r
= db_get_raw_stream( db
, stname
, &stm
);
371 if( r
!= ERROR_SUCCESS
)
373 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
376 WARN("open stream failed r = %08x!\n", r
);
380 if( stat
.cbSize
.QuadPart
>> 32 )
386 sz
= stat
.cbSize
.QuadPart
;
387 data
= msi_alloc( sz
);
390 WARN("couldn't allocate memory r=%08x!\n", r
);
391 ret
= ERROR_NOT_ENOUGH_MEMORY
;
395 r
= IStream_Read(stm
, data
, sz
, &count
);
396 if( FAILED( r
) || ( count
!= sz
) )
399 WARN("read stream failed r = %08x!\n", r
);
408 IStream_Release( stm
);
413 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
414 LPCVOID data
, UINT sz
, BOOL bTable
)
417 UINT ret
= ERROR_FUNCTION_FAILED
;
424 encname
= encode_streamname(bTable
, stname
);
425 r
= IStorage_OpenStream( stg
, encname
, NULL
,
426 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
429 r
= IStorage_CreateStream( stg
, encname
,
430 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
435 WARN("open stream failed r = %08x\n", r
);
440 r
= IStream_SetSize( stm
, size
);
443 WARN("Failed to SetSize\n");
448 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
451 WARN("Failed to Seek\n");
457 r
= IStream_Write(stm
, data
, sz
, &count
);
458 if( FAILED( r
) || ( count
!= sz
) )
460 WARN("Failed to Write\n");
468 IStream_Release( stm
);
473 static void free_table( MSITABLE
*table
)
476 for( i
=0; i
<table
->row_count
; i
++ )
477 msi_free( table
->data
[i
] );
478 msi_free( table
->data
);
479 for( i
=0; i
<table
->nonpersistent_row_count
; i
++ )
480 msi_free( table
->nonpersistent_data
[i
] );
481 msi_free( table
->nonpersistent_data
);
482 if( (table
->colinfo
!= _Tables_cols
) &&
483 (table
->colinfo
!= _Columns_cols
) )
485 msi_free_colinfo( table
->colinfo
, table
->col_count
);
486 msi_free( table
->colinfo
);
491 static UINT
msi_table_get_row_size( const MSICOLUMNINFO
*cols
, UINT count
)
493 const MSICOLUMNINFO
*last_col
= &cols
[count
-1];
496 return last_col
->offset
+ bytes_per_column( last_col
);
499 /* add this table to the list of cached tables in the database */
500 static UINT
read_table_from_storage( MSITABLE
*t
, IStorage
*stg
)
502 BYTE
*rawdata
= NULL
;
503 UINT rawsize
= 0, i
, j
, row_size
= 0;
505 TRACE("%s\n",debugstr_w(t
->name
));
507 row_size
= msi_table_get_row_size( t
->colinfo
, t
->col_count
);
509 /* if we can't read the table, just assume that it's empty */
510 read_stream_data( stg
, t
->name
, TRUE
, &rawdata
, &rawsize
);
512 return ERROR_SUCCESS
;
514 TRACE("Read %d bytes\n", rawsize
);
516 if( rawsize
% row_size
)
518 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
522 t
->row_count
= rawsize
/ row_size
;
523 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
527 /* transpose all the data */
528 TRACE("Transposing data from %d rows\n", t
->row_count
);
529 for( i
=0; i
<t
->row_count
; i
++ )
531 t
->data
[i
] = msi_alloc( row_size
);
535 for( j
=0; j
<t
->col_count
; j
++ )
537 UINT ofs
= t
->colinfo
[j
].offset
;
538 UINT n
= bytes_per_column( &t
->colinfo
[j
] );
541 if ( n
!= 2 && n
!= 3 && n
!= 4 )
543 ERR("oops - unknown column width %d\n", n
);
547 for ( k
= 0; k
< n
; k
++ )
548 t
->data
[i
][ofs
+ k
] = rawdata
[ofs
*t
->row_count
+ i
* n
+ k
];
553 return ERROR_SUCCESS
;
556 return ERROR_FUNCTION_FAILED
;
559 void free_cached_tables( MSIDATABASE
*db
)
561 while( !list_empty( &db
->tables
) )
563 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
565 list_remove( &t
->entry
);
570 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
574 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
575 if( !lstrcmpW( name
, t
->name
) )
581 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
583 UINT r
, column_count
= 0;
584 MSICOLUMNINFO
*columns
;
586 /* get the number of columns in this table */
588 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
589 if( r
!= ERROR_SUCCESS
)
592 /* if there's no columns, there's no table */
593 if( column_count
== 0 )
594 return ERROR_INVALID_PARAMETER
;
596 TRACE("Table %s found\n", debugstr_w(name
) );
598 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
) );
600 return ERROR_FUNCTION_FAILED
;
602 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
603 if( r
!= ERROR_SUCCESS
)
606 return ERROR_FUNCTION_FAILED
;
610 *pcount
= column_count
;
615 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
616 BOOL persistent
, MSITABLE
**table_ret
)
620 MSIRECORD
*rec
= NULL
;
625 /* only add tables that don't exist already */
626 if( TABLE_Exists(db
, name
) )
628 WARN("table %s exists\n", debugstr_w(name
));
629 return ERROR_BAD_QUERY_SYNTAX
;
632 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
634 return ERROR_FUNCTION_FAILED
;
636 table
->ref_count
= 1;
637 table
->row_count
= 0;
639 table
->nonpersistent_row_count
= 0;
640 table
->nonpersistent_data
= NULL
;
641 table
->colinfo
= NULL
;
642 table
->col_count
= 0;
643 table
->persistent
= persistent
;
644 lstrcpyW( table
->name
, name
);
646 for( col
= col_info
; col
; col
= col
->next
)
649 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
653 return ERROR_FUNCTION_FAILED
;
656 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
658 table
->colinfo
[ i
].tablename
= strdupW( col
->table
);
659 table
->colinfo
[ i
].number
= i
+ 1;
660 table
->colinfo
[ i
].colname
= strdupW( col
->column
);
661 table
->colinfo
[ i
].type
= col
->type
;
662 table
->colinfo
[ i
].offset
= 0;
663 table
->colinfo
[ i
].hash_table
= NULL
;
665 table_calc_column_offsets( table
->colinfo
, table
->col_count
);
667 r
= TABLE_CreateView( db
, szTables
, &tv
);
668 TRACE("CreateView returned %x\n", r
);
675 r
= tv
->ops
->execute( tv
, 0 );
676 TRACE("tv execute returned %x\n", r
);
680 rec
= MSI_CreateRecord( 1 );
684 r
= MSI_RecordSetStringW( rec
, 1, name
);
688 r
= tv
->ops
->insert_row( tv
, rec
, !persistent
);
689 TRACE("insert_row returned %x\n", r
);
693 tv
->ops
->delete( tv
);
696 msiobj_release( &rec
->hdr
);
701 /* add each column to the _Columns table */
702 r
= TABLE_CreateView( db
, szColumns
, &tv
);
706 r
= tv
->ops
->execute( tv
, 0 );
707 TRACE("tv execute returned %x\n", r
);
711 rec
= MSI_CreateRecord( 4 );
715 r
= MSI_RecordSetStringW( rec
, 1, name
);
720 * need to set the table, column number, col name and type
721 * for each column we enter in the table
724 for( col
= col_info
; col
; col
= col
->next
)
726 r
= MSI_RecordSetInteger( rec
, 2, nField
);
730 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
734 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
738 r
= tv
->ops
->insert_row( tv
, rec
, FALSE
);
750 msiobj_release( &rec
->hdr
);
751 /* FIXME: remove values from the string table on error */
753 tv
->ops
->delete( tv
);
755 if (r
== ERROR_SUCCESS
)
757 list_add_head( &db
->tables
, &table
->entry
);
766 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
771 /* first, see if the table is cached */
772 table
= find_cached_table( db
, name
);
776 return ERROR_SUCCESS
;
779 /* nonexistent tables should be interpreted as empty tables */
780 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
782 return ERROR_FUNCTION_FAILED
;
784 table
->row_count
= 0;
786 table
->nonpersistent_row_count
= 0;
787 table
->nonpersistent_data
= NULL
;
788 table
->colinfo
= NULL
;
789 table
->col_count
= 0;
790 table
->persistent
= TRUE
;
791 lstrcpyW( table
->name
, name
);
793 /* these two tables are special - we know the column types already */
794 if( !lstrcmpW( name
, szColumns
) )
796 table
->colinfo
= _Columns_cols
;
797 table
->col_count
= sizeof(_Columns_cols
)/sizeof(_Columns_cols
[0]);
799 else if( !lstrcmpW( name
, szTables
) )
801 table
->colinfo
= _Tables_cols
;
802 table
->col_count
= sizeof(_Tables_cols
)/sizeof(_Tables_cols
[0]);
806 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
807 if (r
!= ERROR_SUCCESS
)
809 free_table ( table
);
814 r
= read_table_from_storage( table
, db
->storage
);
815 if( r
!= ERROR_SUCCESS
)
821 list_add_head( &db
->tables
, &table
->entry
);
823 return ERROR_SUCCESS
;
826 static UINT
save_table( MSIDATABASE
*db
, const MSITABLE
*t
)
828 BYTE
*rawdata
= NULL
, *p
;
829 UINT rawsize
, r
, i
, j
, row_size
;
831 /* Nothing to do for non-persistent tables */
833 return ERROR_SUCCESS
;
835 TRACE("Saving %s\n", debugstr_w( t
->name
) );
837 row_size
= msi_table_get_row_size( t
->colinfo
, t
->col_count
);
839 rawsize
= t
->row_count
* row_size
;
840 rawdata
= msi_alloc_zero( rawsize
);
843 r
= ERROR_NOT_ENOUGH_MEMORY
;
848 for( i
=0; i
<t
->col_count
; i
++ )
850 for( j
=0; j
<t
->row_count
; j
++ )
852 UINT offset
= t
->colinfo
[i
].offset
;
854 *p
++ = t
->data
[j
][offset
];
855 *p
++ = t
->data
[j
][offset
+ 1];
856 if( 4 == bytes_per_column( &t
->colinfo
[i
] ) )
858 *p
++ = t
->data
[j
][offset
+ 2];
859 *p
++ = t
->data
[j
][offset
+ 3];
864 TRACE("writing %d bytes\n", rawsize
);
865 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
873 static void table_calc_column_offsets( MSICOLUMNINFO
*colinfo
, DWORD count
)
877 for( i
=0; colinfo
&& (i
<count
); i
++ )
879 assert( (i
+1) == colinfo
[ i
].number
);
881 colinfo
[i
].offset
= colinfo
[ i
- 1 ].offset
882 + bytes_per_column( &colinfo
[ i
- 1 ] );
884 colinfo
[i
].offset
= 0;
885 TRACE("column %d is [%s] with type %08x ofs %d\n",
886 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
887 colinfo
[i
].type
, colinfo
[i
].offset
);
891 static UINT
get_defaulttablecolumns( LPCWSTR name
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
893 const MSICOLUMNINFO
*p
;
896 TRACE("%s\n", debugstr_w(name
));
898 if (!lstrcmpW( name
, szTables
))
903 else if (!lstrcmpW( name
, szColumns
))
909 return ERROR_FUNCTION_FAILED
;
911 /* duplicate the string data so we can free it in msi_free_colinfo */
914 if (colinfo
&& (i
< *sz
) )
916 memcpy( &colinfo
[i
], &p
[i
], sizeof(MSICOLUMNINFO
) );
917 colinfo
[i
].tablename
= strdupW( p
[i
].tablename
);
918 colinfo
[i
].colname
= strdupW( p
[i
].colname
);
920 if( colinfo
&& (i
>= *sz
) )
923 table_calc_column_offsets( colinfo
, n
);
925 return ERROR_SUCCESS
;
928 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
932 for( i
=0; i
<count
; i
++ )
934 msi_free( (LPWSTR
) colinfo
[i
].tablename
);
935 msi_free( (LPWSTR
) colinfo
[i
].colname
);
936 msi_free( colinfo
[i
].hash_table
);
940 static LPWSTR
msi_makestring( const MSIDATABASE
*db
, UINT stringid
)
942 return strdupW(msi_string_lookup_id( db
->strings
, stringid
));
945 static UINT
read_table_int(BYTE
*const *data
, UINT row
, UINT col
, UINT bytes
)
949 for (i
= 0; i
< bytes
; i
++)
950 ret
+= (data
[row
][col
+ i
] << i
* 8);
955 static UINT
get_tablecolumns( MSIDATABASE
*db
,
956 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
958 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
959 MSITABLE
*table
= NULL
;
961 TRACE("%s\n", debugstr_w(szTableName
));
963 /* first check if there is a default table with that name */
964 r
= get_defaulttablecolumns( szTableName
, colinfo
, sz
);
965 if( ( r
== ERROR_SUCCESS
) && *sz
)
968 r
= get_table( db
, szColumns
, &table
);
969 if( r
!= ERROR_SUCCESS
)
971 ERR("couldn't load _Columns table\n");
972 return ERROR_FUNCTION_FAILED
;
975 /* convert table and column names to IDs from the string table */
976 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
977 if( r
!= ERROR_SUCCESS
)
979 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
983 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
985 /* Note: _Columns table doesn't have non-persistent data */
987 /* if maxcount is non-zero, assume it's exactly right for this table */
988 memset( colinfo
, 0, maxcount
*sizeof(*colinfo
) );
989 count
= table
->row_count
;
990 for( i
=0; i
<count
; i
++ )
992 if( read_table_int(table
->data
, i
, 0, db
->bytes_per_strref
) != table_id
)
996 UINT id
= read_table_int(table
->data
, i
, _Columns_cols
[2].offset
, db
->bytes_per_strref
);
997 UINT col
= read_table_int(table
->data
, i
, _Columns_cols
[1].offset
, sizeof(USHORT
)) - (1<<15);
999 /* check the column number is in range */
1000 if (col
<1 || col
>maxcount
)
1002 ERR("column %d out of range\n", col
);
1006 /* check if this column was already set */
1007 if (colinfo
[ col
- 1 ].number
)
1009 ERR("duplicate column %d\n", col
);
1013 colinfo
[ col
- 1 ].tablename
= msi_makestring( db
, table_id
);
1014 colinfo
[ col
- 1 ].number
= col
;
1015 colinfo
[ col
- 1 ].colname
= msi_makestring( db
, id
);
1016 colinfo
[ col
- 1 ].type
= read_table_int(table
->data
, i
, _Columns_cols
[3].offset
, sizeof(USHORT
)) - (1<<15);
1017 colinfo
[ col
- 1 ].offset
= 0;
1018 colinfo
[ col
- 1 ].hash_table
= NULL
;
1023 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
1025 if (colinfo
&& n
!= maxcount
)
1027 ERR("missing column in table %s\n", debugstr_w(szTableName
));
1028 msi_free_colinfo(colinfo
, maxcount
);
1029 return ERROR_FUNCTION_FAILED
;
1032 table_calc_column_offsets( colinfo
, n
);
1035 return ERROR_SUCCESS
;
1038 static void msi_update_table_columns( MSIDATABASE
*db
, LPCWSTR name
)
1044 table
= find_cached_table( db
, name
);
1045 msi_free( table
->colinfo
);
1046 table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
1048 size
= msi_table_get_row_size( table
->colinfo
, table
->col_count
);
1049 offset
= table
->colinfo
[table
->col_count
- 1].offset
;
1051 for ( n
= 0; n
< table
->row_count
; n
++ )
1053 table
->data
[n
] = msi_realloc( table
->data
[n
], size
);
1054 table
->data
[n
][offset
] = (BYTE
)MSI_NULL_INTEGER
;
1058 /* try to find the table name in the _Tables table */
1059 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
1061 UINT r
, table_id
= 0, i
, count
;
1062 MSITABLE
*table
= NULL
;
1064 if( !lstrcmpW( name
, szTables
) )
1066 if( !lstrcmpW( name
, szColumns
) )
1069 r
= msi_string2idW( db
->strings
, name
, &table_id
);
1070 if( r
!= ERROR_SUCCESS
)
1072 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1076 r
= get_table( db
, szTables
, &table
);
1077 if( r
!= ERROR_SUCCESS
)
1079 ERR("table %s not available\n", debugstr_w(szTables
));
1083 count
= table
->row_count
;
1084 for( i
=0; i
<count
; i
++ )
1085 if( table
->data
[ i
][ 0 ] == table_id
)
1091 count
= table
->nonpersistent_row_count
;
1092 for( i
=0; i
<count
; i
++ )
1093 if( table
->nonpersistent_data
[ i
][ 0 ] == table_id
)
1099 TRACE("Searched %d tables, but %d was not found\n", count
, table_id
);
1104 /* below is the query interface to a table */
1106 typedef struct tagMSITABLEVIEW
1111 MSICOLUMNINFO
*columns
;
1117 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1119 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1124 return ERROR_INVALID_PARAMETER
;
1126 if( (col
==0) || (col
>tv
->num_cols
) )
1127 return ERROR_INVALID_PARAMETER
;
1129 /* how many rows are there ? */
1130 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1131 return ERROR_NO_MORE_ITEMS
;
1133 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1135 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1136 ERR("%p %p\n", tv
, tv
->columns
);
1137 return ERROR_FUNCTION_FAILED
;
1140 if (row
>= tv
->table
->row_count
)
1142 row
-= tv
->table
->row_count
;
1143 data
= tv
->table
->nonpersistent_data
;
1146 data
= tv
->table
->data
;
1148 n
= bytes_per_column( &tv
->columns
[col
-1] );
1149 if (n
!= 2 && n
!= 3 && n
!= 4)
1151 ERR("oops! what is %d bytes per column?\n", n
);
1152 return ERROR_FUNCTION_FAILED
;
1155 offset
= tv
->columns
[col
-1].offset
;
1156 *val
= read_table_int(data
, row
, offset
, n
);
1158 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1160 return ERROR_SUCCESS
;
1164 * We need a special case for streams, as we need to reference column with
1165 * the name of the stream in the same table, and the table name
1166 * which may not be available at higher levels of the query
1168 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1170 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1171 UINT ival
= 0, refcol
= 0, r
;
1175 static const WCHAR szDot
[] = { '.', 0 };
1178 if( !view
->ops
->fetch_int
)
1179 return ERROR_INVALID_PARAMETER
;
1182 * The column marked with the type stream data seems to have a single number
1183 * which references the column containing the name of the stream data
1185 * Fetch the column to reference first.
1187 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1188 if( r
!= ERROR_SUCCESS
)
1191 /* check the column value is in range */
1192 if (ival
< 0 || ival
> tv
->num_cols
|| ival
== col
)
1194 ERR("bad column ref (%u) for stream\n", ival
);
1195 return ERROR_FUNCTION_FAILED
;
1198 if ( tv
->columns
[ival
- 1].type
& MSITYPE_STRING
)
1200 /* now get the column with the name of the stream */
1201 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1202 if ( r
!= ERROR_SUCCESS
)
1205 /* lookup the string value from the string table */
1206 sval
= msi_string_lookup_id( tv
->db
->strings
, refcol
);
1208 return ERROR_INVALID_PARAMETER
;
1212 static const WCHAR fmt
[] = { '%','d',0 };
1213 sprintfW( number
, fmt
, ival
);
1217 len
= lstrlenW( tv
->name
) + 2 + lstrlenW( sval
);
1218 full_name
= msi_alloc( len
*sizeof(WCHAR
) );
1219 lstrcpyW( full_name
, tv
->name
);
1220 lstrcatW( full_name
, szDot
);
1221 lstrcatW( full_name
, sval
);
1223 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1225 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1226 msi_free( full_name
);
1231 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1237 return ERROR_INVALID_PARAMETER
;
1239 if( (col
==0) || (col
>tv
->num_cols
) )
1240 return ERROR_INVALID_PARAMETER
;
1242 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1243 return ERROR_INVALID_PARAMETER
;
1245 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1247 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1248 ERR("%p %p\n", tv
, tv
->columns
);
1249 return ERROR_FUNCTION_FAILED
;
1252 msi_free( tv
->columns
[col
-1].hash_table
);
1253 tv
->columns
[col
-1].hash_table
= NULL
;
1255 if (row
>= tv
->table
->row_count
)
1257 row
-= tv
->table
->row_count
;
1258 data
= tv
->table
->nonpersistent_data
;
1261 data
= tv
->table
->data
;
1263 n
= bytes_per_column( &tv
->columns
[col
-1] );
1264 if ( n
!= 2 && n
!= 3 && n
!= 4 )
1266 ERR("oops! what is %d bytes per column?\n", n
);
1267 return ERROR_FUNCTION_FAILED
;
1270 offset
= tv
->columns
[col
-1].offset
;
1271 for ( i
= 0; i
< n
; i
++ )
1272 data
[row
][offset
+ i
] = (val
>> i
* 8) & 0xff;
1274 return ERROR_SUCCESS
;
1277 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1279 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1280 UINT i
, val
, r
= ERROR_SUCCESS
;
1283 return ERROR_INVALID_PARAMETER
;
1285 /* test if any of the mask bits are invalid */
1286 if ( mask
>= (1<<tv
->num_cols
) )
1287 return ERROR_INVALID_PARAMETER
;
1289 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1293 /* only update the fields specified in the mask */
1294 if ( !(mask
&(1<<i
)) )
1297 /* if row >= tv->table->row_count then it is a non-persistent row */
1298 persistent
= tv
->table
->persistent
&& (row
< tv
->table
->row_count
);
1299 /* FIXME: should we allow updating keys? */
1302 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1304 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1306 val
= 1; /* refers to the first key column */
1308 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1310 LPCWSTR sval
= MSI_RecordGetString( rec
, i
+ 1 );
1311 val
= msi_addstringW( tv
->db
->strings
, 0, sval
, -1, 1,
1312 persistent
? StringPersistent
: StringNonPersistent
);
1314 else if ( 2 == bytes_per_column( &tv
->columns
[ i
] ) )
1316 val
= 0x8000 + MSI_RecordGetInteger( rec
, i
+ 1 );
1317 if ( val
& 0xffff0000 )
1319 ERR("field %u value %d out of range\n", i
+1, val
- 0x8000 );
1320 return ERROR_FUNCTION_FAILED
;
1325 INT ival
= MSI_RecordGetInteger( rec
, i
+ 1 );
1326 val
= ival
^ 0x80000000;
1330 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1331 if ( r
!= ERROR_SUCCESS
)
1337 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1339 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1345 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1348 return ERROR_INVALID_PARAMETER
;
1350 row
= msi_alloc_zero( tv
->row_size
);
1352 return ERROR_NOT_ENOUGH_MEMORY
;
1356 row_count
= &tv
->table
->nonpersistent_row_count
;
1357 data_ptr
= &tv
->table
->nonpersistent_data
;
1358 *num
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1362 row_count
= &tv
->table
->row_count
;
1363 data_ptr
= &tv
->table
->data
;
1364 *num
= tv
->table
->row_count
;
1367 sz
= (*row_count
+ 1) * sizeof (BYTE
*);
1369 p
= msi_realloc( *data_ptr
, sz
);
1371 p
= msi_alloc( sz
);
1375 return ERROR_NOT_ENOUGH_MEMORY
;
1379 (*data_ptr
)[*row_count
] = row
;
1382 return ERROR_SUCCESS
;
1385 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1387 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1389 TRACE("%p %p\n", tv
, record
);
1391 TRACE("There are %d columns\n", tv
->num_cols
);
1393 return ERROR_SUCCESS
;
1396 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1398 TRACE("%p\n", view
);
1400 return ERROR_SUCCESS
;
1403 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1405 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1407 TRACE("%p %p %p\n", view
, rows
, cols
);
1410 *cols
= tv
->num_cols
;
1414 return ERROR_INVALID_PARAMETER
;
1415 *rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1418 return ERROR_SUCCESS
;
1421 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1422 UINT n
, LPWSTR
*name
, UINT
*type
)
1424 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1426 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1428 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1429 return ERROR_INVALID_PARAMETER
;
1433 *name
= strdupW( tv
->columns
[n
-1].colname
);
1435 return ERROR_FUNCTION_FAILED
;
1438 *type
= tv
->columns
[n
-1].type
;
1440 return ERROR_SUCCESS
;
1443 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
);
1445 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1449 /* check there's no null values where they're not allowed */
1450 for( i
= 0; i
< tv
->num_cols
; i
++ )
1452 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1455 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1456 TRACE("skipping binary column\n");
1457 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1461 str
= MSI_RecordGetString( rec
, i
+1 );
1462 if (str
== NULL
|| str
[0] == 0)
1463 return ERROR_INVALID_DATA
;
1469 n
= MSI_RecordGetInteger( rec
, i
+1 );
1470 if (n
== MSI_NULL_INTEGER
)
1471 return ERROR_INVALID_DATA
;
1475 /* check there's no duplicate keys */
1476 r
= msi_table_find_row( tv
, rec
, &row
);
1477 if (r
== ERROR_SUCCESS
)
1478 return ERROR_INVALID_DATA
;
1480 return ERROR_SUCCESS
;
1483 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, BOOL temporary
)
1485 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1488 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1490 /* check that the key is unique - can we find a matching row? */
1491 r
= table_validate_new( tv
, rec
);
1492 if( r
!= ERROR_SUCCESS
)
1493 return ERROR_FUNCTION_FAILED
;
1495 r
= table_create_new_row( view
, &row
, temporary
);
1496 TRACE("insert_row returned %08x\n", r
);
1497 if( r
!= ERROR_SUCCESS
)
1500 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1503 static UINT
TABLE_delete_row( struct tagMSIVIEW
*view
, UINT row
)
1505 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1506 UINT r
, num_rows
, num_cols
;
1509 TRACE("%p %d\n", tv
, row
);
1512 return ERROR_INVALID_PARAMETER
;
1514 r
= TABLE_get_dimensions( view
, &num_rows
, &num_cols
);
1515 if ( r
!= ERROR_SUCCESS
)
1518 if ( row
>= num_rows
)
1519 return ERROR_FUNCTION_FAILED
;
1521 tv
->table
->row_count
--;
1523 if ( row
== num_rows
- 1 )
1524 return ERROR_SUCCESS
;
1526 dest
= tv
->table
->data
[row
];
1527 src
= tv
->table
->data
[row
+ 1];
1528 memmove(dest
, src
, (num_rows
- row
- 1) * tv
->row_size
);
1529 return ERROR_SUCCESS
;
1532 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1535 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1538 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1540 switch (eModifyMode
)
1542 case MSIMODIFY_VALIDATE_NEW
:
1543 r
= table_validate_new( tv
, rec
);
1546 case MSIMODIFY_INSERT_TEMPORARY
:
1547 r
= table_validate_new( tv
, rec
);
1548 if (r
!= ERROR_SUCCESS
)
1550 r
= TABLE_insert_row( view
, rec
, TRUE
);
1553 case MSIMODIFY_REFRESH
:
1554 case MSIMODIFY_INSERT
:
1555 case MSIMODIFY_UPDATE
:
1556 case MSIMODIFY_ASSIGN
:
1557 case MSIMODIFY_REPLACE
:
1558 case MSIMODIFY_MERGE
:
1559 case MSIMODIFY_DELETE
:
1560 case MSIMODIFY_VALIDATE
:
1561 case MSIMODIFY_VALIDATE_FIELD
:
1562 case MSIMODIFY_VALIDATE_DELETE
:
1563 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1564 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1568 r
= ERROR_INVALID_DATA
;
1574 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1576 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1578 TRACE("%p\n", view
);
1586 return ERROR_SUCCESS
;
1589 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1590 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1592 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1593 const MSICOLUMNHASHENTRY
*entry
;
1595 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1598 return ERROR_INVALID_PARAMETER
;
1600 if( (col
==0) || (col
> tv
->num_cols
) )
1601 return ERROR_INVALID_PARAMETER
;
1603 if( !tv
->columns
[col
-1].hash_table
)
1606 UINT num_rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1607 MSICOLUMNHASHENTRY
**hash_table
;
1608 MSICOLUMNHASHENTRY
*new_entry
;
1610 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1612 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1613 ERR("%p %p\n", tv
, tv
->columns
);
1614 return ERROR_FUNCTION_FAILED
;
1617 /* allocate contiguous memory for the table and its entries so we
1618 * don't have to do an expensive cleanup */
1619 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1620 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1622 return ERROR_OUTOFMEMORY
;
1624 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1625 tv
->columns
[col
-1].hash_table
= hash_table
;
1627 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1629 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1633 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1636 new_entry
->next
= NULL
;
1637 new_entry
->value
= row_value
;
1639 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1641 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1642 while (prev_entry
->next
)
1643 prev_entry
= prev_entry
->next
;
1644 prev_entry
->next
= new_entry
;
1647 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1652 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1654 entry
= (*handle
)->next
;
1656 while (entry
&& entry
->value
!= val
)
1657 entry
= entry
->next
;
1661 return ERROR_NO_MORE_ITEMS
;
1664 return ERROR_SUCCESS
;
1667 static UINT
TABLE_add_ref(struct tagMSIVIEW
*view
)
1669 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1671 TRACE("%p %d\n", view
, tv
->table
->ref_count
);
1673 return InterlockedIncrement(&tv
->table
->ref_count
);
1676 static UINT
TABLE_release(struct tagMSIVIEW
*view
)
1678 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1679 INT ref
= tv
->table
->ref_count
;
1681 TRACE("%p %d\n", view
, ref
);
1683 ref
= InterlockedDecrement(&tv
->table
->ref_count
);
1686 if (!tv
->table
->row_count
)
1688 list_remove(&tv
->table
->entry
);
1689 free_table(tv
->table
);
1697 static UINT
TABLE_add_column(struct tagMSIVIEW
*view
, LPCWSTR table
, UINT number
, LPCWSTR column
, UINT type
)
1699 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1703 rec
= MSI_CreateRecord(4);
1705 return ERROR_OUTOFMEMORY
;
1707 MSI_RecordSetStringW(rec
, 1, table
);
1708 MSI_RecordSetInteger(rec
, 2, number
);
1709 MSI_RecordSetStringW(rec
, 3, column
);
1710 MSI_RecordSetInteger(rec
, 4, type
);
1712 r
= TABLE_insert_row(&tv
->view
, rec
, FALSE
);
1713 if (r
!= ERROR_SUCCESS
)
1716 msi_update_table_columns(tv
->db
, table
);
1719 msiobj_release(&rec
->hdr
);
1723 static const MSIVIEWOPS table_ops
=
1732 TABLE_get_dimensions
,
1733 TABLE_get_column_info
,
1736 TABLE_find_matching_rows
,
1742 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
1747 static const WCHAR Streams
[] = {'_','S','t','r','e','a','m','s',0};
1749 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
1751 if ( !lstrcmpW( name
, Streams
) )
1752 return STREAMS_CreateView( db
, view
);
1754 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
1755 tv
= msi_alloc_zero( sz
);
1757 return ERROR_FUNCTION_FAILED
;
1759 r
= get_table( db
, name
, &tv
->table
);
1760 if( r
!= ERROR_SUCCESS
)
1763 WARN("table not found\n");
1767 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
1769 /* fill the structure */
1770 tv
->view
.ops
= &table_ops
;
1772 tv
->columns
= tv
->table
->colinfo
;
1773 tv
->num_cols
= tv
->table
->col_count
;
1774 tv
->row_size
= msi_table_get_row_size( tv
->table
->colinfo
, tv
->table
->col_count
);
1776 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
1778 *view
= (MSIVIEW
*) tv
;
1779 lstrcpyW( tv
->name
, name
);
1781 return ERROR_SUCCESS
;
1784 UINT
MSI_CommitTables( MSIDATABASE
*db
)
1787 MSITABLE
*table
= NULL
;
1791 r
= msi_save_string_table( db
->strings
, db
->storage
);
1792 if( r
!= ERROR_SUCCESS
)
1794 WARN("failed to save string table r=%08x\n",r
);
1798 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
1800 r
= save_table( db
, table
);
1801 if( r
!= ERROR_SUCCESS
)
1803 WARN("failed to save table %s (r=%08x)\n",
1804 debugstr_w(table
->name
), r
);
1809 /* force everything to reload next time */
1810 free_cached_tables( db
);
1812 return ERROR_SUCCESS
;
1815 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
1820 TRACE("%p %s\n", db
, debugstr_w(table
));
1823 return MSICONDITION_ERROR
;
1825 r
= get_table( db
, table
, &t
);
1826 if (r
!= ERROR_SUCCESS
)
1827 return MSICONDITION_NONE
;
1830 return MSICONDITION_TRUE
;
1832 return MSICONDITION_FALSE
;
1835 static UINT
read_raw_int(const BYTE
*data
, UINT col
, UINT bytes
)
1839 for (i
= 0; i
< bytes
; i
++)
1840 ret
+= (data
[col
+ i
] << i
* 8);
1845 static MSIRECORD
*msi_get_transform_record( const MSITABLEVIEW
*tv
, const string_table
*st
,
1846 const BYTE
*rawdata
, UINT bytes_per_strref
)
1848 UINT i
, val
, ofs
= 0;
1850 MSICOLUMNINFO
*columns
= tv
->columns
;
1853 mask
= rawdata
[0] | (rawdata
[1] << 8);
1856 rec
= MSI_CreateRecord( tv
->num_cols
);
1861 for( i
=0; i
<tv
->num_cols
; i
++ )
1863 if ( (mask
&1) && (i
>=(mask
>>8)) )
1865 /* all keys must be present */
1866 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
1869 if( (columns
[i
].type
& MSITYPE_STRING
) &&
1870 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1874 val
= read_raw_int(rawdata
, ofs
, bytes_per_strref
);
1875 sval
= msi_string_lookup_id( st
, val
);
1876 MSI_RecordSetStringW( rec
, i
+1, sval
);
1877 TRACE(" field %d [%s]\n", i
+1, debugstr_w(sval
));
1878 ofs
+= bytes_per_strref
;
1882 UINT n
= bytes_per_column( &columns
[i
] );
1886 val
= read_raw_int(rawdata
, ofs
, n
);
1888 MSI_RecordSetInteger( rec
, i
+1, val
^0x8000 );
1889 TRACE(" field %d [0x%04x]\n", i
+1, val
);
1892 val
= read_raw_int(rawdata
, ofs
, n
);
1894 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
1895 TRACE(" field %d [0x%08x]\n", i
+1, val
);
1898 ERR("oops - unknown column width %d\n", n
);
1907 static void dump_record( MSIRECORD
*rec
)
1911 n
= MSI_RecordGetFieldCount( rec
);
1912 for( i
=1; i
<=n
; i
++ )
1914 LPCWSTR sval
= MSI_RecordGetString( rec
, i
);
1916 if( MSI_RecordIsNull( rec
, i
) )
1917 TRACE("row -> []\n");
1918 else if( (sval
= MSI_RecordGetString( rec
, i
)) )
1919 TRACE("row -> [%s]\n", debugstr_w(sval
));
1921 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
1925 static void dump_table( const string_table
*st
, const USHORT
*rawdata
, UINT rawsize
)
1930 for( i
=0; i
<(rawsize
/2); i
++ )
1932 sval
= msi_string_lookup_id( st
, rawdata
[i
] );
1933 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_w(sval
) );
1937 static UINT
* msi_record_to_row( const MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1942 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
1943 for( i
=0; i
<tv
->num_cols
; i
++ )
1947 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
1950 /* turn the transform column value into a row value */
1951 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
1952 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1954 str
= MSI_RecordGetString( rec
, i
+1 );
1955 r
= msi_string2idW( tv
->db
->strings
, str
, &data
[i
] );
1957 /* if there's no matching string in the string table,
1958 these keys can't match any record, so fail now. */
1959 if( ERROR_SUCCESS
!= r
)
1967 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
1968 if ((tv
->columns
[i
].type
&0xff) == 2)
1971 data
[i
] += 0x80000000;
1977 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, const UINT
*data
)
1979 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
1981 for( i
=0; i
<tv
->num_cols
; i
++ )
1983 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
1986 /* turn the transform column value into a row value */
1987 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
1988 if ( r
!= ERROR_SUCCESS
)
1990 ERR("TABLE_fetch_int shouldn't fail here\n");
1994 /* if this key matches, move to the next column */
1997 ret
= ERROR_FUNCTION_FAILED
;
2001 ret
= ERROR_SUCCESS
;
2007 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
)
2009 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
2011 data
= msi_record_to_row( tv
, rec
);
2014 for( i
= 0; i
< tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
; i
++ )
2016 r
= msi_row_matches( tv
, i
, data
);
2017 if( r
== ERROR_SUCCESS
)
2033 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
2034 string_table
*st
, TRANSFORMDATA
*transform
,
2035 UINT bytes_per_strref
)
2038 BYTE
*rawdata
= NULL
;
2039 MSITABLEVIEW
*tv
= NULL
;
2040 UINT r
, n
, sz
, i
, mask
;
2041 MSIRECORD
*rec
= NULL
;
2047 return ERROR_SUCCESS
;
2049 name
= transform
->name
;
2052 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
2054 /* read the transform data */
2055 read_stream_data( stg
, name
, TRUE
, &rawdata
, &rawsize
);
2058 TRACE("table %s empty\n", debugstr_w(name
) );
2059 return ERROR_INVALID_TABLE
;
2062 /* create a table view */
2063 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
2064 if( r
!= ERROR_SUCCESS
)
2067 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2068 if( r
!= ERROR_SUCCESS
)
2071 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2072 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
2074 /* interpret the data */
2076 for( n
=0; n
< rawsize
; )
2078 mask
= rawdata
[n
] | (rawdata
[n
+1] << 8);
2083 * if the low bit is set, columns are continuous and
2084 * the number of columns is specified in the high byte
2087 for( i
=0; i
<tv
->num_cols
; i
++ )
2089 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2090 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2091 sz
+= bytes_per_strref
;
2093 sz
+= bytes_per_column( &tv
->columns
[i
] );
2099 * If the low bit is not set, mask is a bitmask.
2100 * Excepting for key fields, which are always present,
2101 * each bit indicates that a field is present in the transform record.
2103 * mask == 0 is a special case ... only the keys will be present
2104 * and it means that this row should be deleted.
2107 for( i
=0; i
<tv
->num_cols
; i
++ )
2109 if( (tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1<<i
)&mask
))
2111 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
2112 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
2113 sz
+= bytes_per_strref
;
2115 sz
+= bytes_per_column( &tv
->columns
[i
] );
2120 /* check we didn't run of the end of the table */
2121 if ( (n
+sz
) > rawsize
)
2124 dump_table( st
, (USHORT
*)rawdata
, rawsize
);
2128 rec
= msi_get_transform_record( tv
, st
, &rawdata
[n
], bytes_per_strref
);
2135 UINT number
= MSI_NULL_INTEGER
;
2137 TRACE("inserting record\n");
2139 if (!lstrcmpW(name
, szColumns
))
2141 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
2142 number
= MSI_RecordGetInteger( rec
, 2 );
2145 * Native msi seems writes nul into the Number (2nd) column of
2146 * the _Columns table, only when the columns are from a new table
2148 if ( number
== MSI_NULL_INTEGER
)
2150 /* reset the column number on a new table */
2151 if ( lstrcmpW(coltable
, table
) )
2154 lstrcpyW( coltable
, table
);
2157 /* fix nul column numbers */
2158 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2162 r
= TABLE_insert_row( &tv
->view
, rec
, FALSE
);
2163 if (r
!= ERROR_SUCCESS
)
2164 ERR("insert row failed\n");
2166 if ( number
!= MSI_NULL_INTEGER
&& !lstrcmpW(name
, szColumns
) )
2167 msi_update_table_columns( db
, table
);
2173 r
= msi_table_find_row( tv
, rec
, &row
);
2174 if (r
!= ERROR_SUCCESS
)
2175 ERR("no matching row to transform\n");
2178 TRACE("modifying row [%d]:\n", row
);
2179 TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2183 TRACE("deleting row [%d]:\n", row
);
2184 TABLE_delete_row( &tv
->view
, row
);
2187 if( TRACE_ON(msidb
) ) dump_record( rec
);
2188 msiobj_release( &rec
->hdr
);
2195 /* no need to free the table, it's associated with the database */
2196 msi_free( rawdata
);
2198 tv
->view
.ops
->delete( &tv
->view
);
2200 return ERROR_SUCCESS
;
2204 * msi_table_apply_transform
2206 * Enumerate the table transforms in a transform storage and apply each one.
2208 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2210 struct list transforms
;
2211 IEnumSTATSTG
*stgenum
= NULL
;
2212 TRANSFORMDATA
*transform
;
2213 TRANSFORMDATA
*tables
= NULL
, *columns
= NULL
;
2216 string_table
*strings
;
2217 UINT ret
= ERROR_FUNCTION_FAILED
;
2218 UINT bytes_per_strref
;
2220 TRACE("%p %p\n", db
, stg
);
2222 strings
= msi_load_string_table( stg
, &bytes_per_strref
);
2226 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2230 list_init(&transforms
);
2234 MSITABLEVIEW
*tv
= NULL
;
2238 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2239 if ( FAILED( r
) || !count
)
2242 decode_streamname( stat
.pwcsName
, name
);
2243 if ( name
[0] != 0x4840 )
2246 if ( !lstrcmpW( name
+1, szStringPool
) ||
2247 !lstrcmpW( name
+1, szStringData
) )
2250 transform
= msi_alloc_zero( sizeof(TRANSFORMDATA
) );
2254 list_add_tail( &transforms
, &transform
->entry
);
2256 transform
->name
= strdupW( name
+ 1 );
2258 if ( !lstrcmpW( transform
->name
, szTables
) )
2260 else if (!lstrcmpW( transform
->name
, szColumns
) )
2261 columns
= transform
;
2263 TRACE("transform contains stream %s\n", debugstr_w(name
));
2265 /* load the table */
2266 r
= TABLE_CreateView( db
, transform
->name
, (MSIVIEW
**) &tv
);
2267 if( r
!= ERROR_SUCCESS
)
2270 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
2271 if( r
!= ERROR_SUCCESS
)
2273 tv
->view
.ops
->delete( &tv
->view
);
2277 tv
->view
.ops
->delete( &tv
->view
);
2281 * Apply _Tables and _Columns transforms first so that
2282 * the table metadata is correct, and empty tables exist.
2284 ret
= msi_table_load_transform( db
, stg
, strings
, tables
, bytes_per_strref
);
2285 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2288 ret
= msi_table_load_transform( db
, stg
, strings
, columns
, bytes_per_strref
);
2289 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2292 ret
= ERROR_SUCCESS
;
2294 while ( !list_empty( &transforms
) )
2296 transform
= LIST_ENTRY( list_head( &transforms
), TRANSFORMDATA
, entry
);
2298 if ( lstrcmpW( transform
->name
, szColumns
) &&
2299 lstrcmpW( transform
->name
, szTables
) &&
2300 ret
== ERROR_SUCCESS
)
2302 ret
= msi_table_load_transform( db
, stg
, strings
, transform
, bytes_per_strref
);
2305 list_remove( &transform
->entry
);
2306 msi_free( transform
->name
);
2307 msi_free( transform
);
2310 if ( ret
== ERROR_SUCCESS
)
2311 append_storage_to_db( db
, stg
);
2315 IEnumSTATSTG_Release( stgenum
);
2317 msi_destroy_stringtable( strings
);
2322 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
2326 t
= msi_alloc( sizeof *t
);
2328 IStorage_AddRef( stg
);
2329 list_add_tail( &db
->transforms
, &t
->entry
);
2332 void msi_free_transforms( MSIDATABASE
*db
)
2334 while( !list_empty( &db
->transforms
) )
2336 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
),
2337 MSITRANSFORM
, entry
);
2338 list_remove( &t
->entry
);
2339 IStorage_Release( t
->stg
);