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
46 typedef struct tagMSICOLUMNHASHENTRY
48 struct tagMSICOLUMNHASHENTRY
*next
;
53 typedef struct tagMSICOLUMNINFO
60 MSICOLUMNHASHENTRY
**hash_table
;
67 USHORT
**nonpersistent_data
;
68 UINT nonpersistent_row_count
;
70 MSICOLUMNINFO
*colinfo
;
76 typedef struct tagMSITRANSFORM
{
81 static const WCHAR szStringData
[] = {
82 '_','S','t','r','i','n','g','D','a','t','a',0 };
83 static const WCHAR szStringPool
[] = {
84 '_','S','t','r','i','n','g','P','o','o','l',0 };
86 /* information for default tables */
87 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
88 static const WCHAR szTable
[] = { 'T','a','b','l','e',0 };
89 static const WCHAR szName
[] = { 'N','a','m','e',0 };
90 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
91 static const WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
92 static const WCHAR szType
[] = { 'T','y','p','e',0 };
94 /* These tables are written into (the .hash_table part).
95 * Do not mark them const.
97 static MSICOLUMNINFO _Columns_cols
[4] = {
98 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 0 },
99 { szColumns
, 2, szNumber
, MSITYPE_VALID
| 2, 2 },
100 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4 },
101 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6 },
103 static MSICOLUMNINFO _Tables_cols
[1] = {
104 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 0 },
107 #define MAX_STREAM_NAME 0x1f
109 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
110 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
111 static void table_calc_column_offsets( MSICOLUMNINFO
*colinfo
, DWORD count
);
112 static UINT
get_tablecolumns( MSIDATABASE
*db
,
113 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
114 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
);
116 static inline UINT
bytes_per_column( const MSICOLUMNINFO
*col
)
118 if( col
->type
& MSITYPE_STRING
)
120 if( (col
->type
& 0xff) > 4 )
121 ERR("Invalid column size!\n");
122 return col
->type
& 0xff;
125 static int utf2mime(int x
)
127 if( (x
>='0') && (x
<='9') )
129 if( (x
>='A') && (x
<='Z') )
131 if( (x
>='a') && (x
<='z') )
140 LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
142 DWORD count
= MAX_STREAM_NAME
;
147 count
= lstrlenW( in
)+2;
148 out
= msi_alloc( count
*sizeof(WCHAR
) );
164 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
166 ch
= utf2mime(ch
) + 0x4800;
168 if( next
&& (next
<0x80) )
170 next
= utf2mime(next
);
181 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
186 static int mime2utf(int x
)
193 return x
- 10 - 26 + 'a';
194 if( x
== (10+26+26) )
199 BOOL
decode_streamname(LPWSTR in
, LPWSTR out
)
204 while ( (ch
= *in
++) )
206 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
209 ch
= mime2utf(ch
-0x4800);
213 *out
++ = mime2utf(ch
&0x3f);
215 ch
= mime2utf((ch
>>6)&0x3f);
225 void enum_stream_names( IStorage
*stg
)
227 IEnumSTATSTG
*stgenum
= NULL
;
233 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
241 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
242 if( FAILED( r
) || !count
)
244 decode_streamname( stat
.pwcsName
, name
);
245 TRACE("stream %2d -> %s %s\n", n
,
246 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
250 IEnumSTATSTG_Release( stgenum
);
253 UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
,
254 USHORT
**pdata
, UINT
*psz
)
257 UINT ret
= ERROR_FUNCTION_FAILED
;
264 encname
= encode_streamname(TRUE
, stname
);
266 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
268 r
= IStorage_OpenStream(stg
, encname
, NULL
,
269 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
273 WARN("open stream failed r = %08x - empty table?\n", r
);
277 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
280 WARN("open stream failed r = %08x!\n", r
);
284 if( stat
.cbSize
.QuadPart
>> 32 )
290 sz
= stat
.cbSize
.QuadPart
;
291 data
= msi_alloc( sz
);
294 WARN("couldn't allocate memory r=%08x!\n", r
);
295 ret
= ERROR_NOT_ENOUGH_MEMORY
;
299 r
= IStream_Read(stm
, data
, sz
, &count
);
300 if( FAILED( r
) || ( count
!= sz
) )
303 WARN("read stream failed r = %08x!\n", r
);
312 IStream_Release( stm
);
317 UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
322 encname
= encode_streamname(FALSE
, stname
);
324 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
326 r
= IStorage_OpenStream(db
->storage
, encname
, NULL
,
327 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
330 MSITRANSFORM
*transform
;
332 LIST_FOR_EACH_ENTRY( transform
, &db
->transforms
, MSITRANSFORM
, entry
)
334 TRACE("looking for %s in transform storage\n", debugstr_w(stname
) );
335 r
= IStorage_OpenStream( transform
->stg
, encname
, NULL
,
336 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
344 return SUCCEEDED(r
) ? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
347 UINT
read_raw_stream_data( MSIDATABASE
*db
, LPCWSTR stname
,
348 USHORT
**pdata
, UINT
*psz
)
351 UINT ret
= ERROR_FUNCTION_FAILED
;
357 r
= db_get_raw_stream( db
, stname
, &stm
);
358 if( r
!= ERROR_SUCCESS
)
360 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
363 WARN("open stream failed r = %08x!\n", r
);
367 if( stat
.cbSize
.QuadPart
>> 32 )
373 sz
= stat
.cbSize
.QuadPart
;
374 data
= msi_alloc( sz
);
377 WARN("couldn't allocate memory r=%08x!\n", r
);
378 ret
= ERROR_NOT_ENOUGH_MEMORY
;
382 r
= IStream_Read(stm
, data
, sz
, &count
);
383 if( FAILED( r
) || ( count
!= sz
) )
386 WARN("read stream failed r = %08x!\n", r
);
395 IStream_Release( stm
);
400 UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
401 LPVOID data
, UINT sz
, BOOL bTable
)
404 UINT ret
= ERROR_FUNCTION_FAILED
;
411 encname
= encode_streamname(bTable
, stname
);
412 r
= IStorage_OpenStream( stg
, encname
, NULL
,
413 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
416 r
= IStorage_CreateStream( stg
, encname
,
417 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
422 WARN("open stream failed r = %08x\n", r
);
427 r
= IStream_SetSize( stm
, size
);
430 WARN("Failed to SetSize\n");
435 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
438 WARN("Failed to Seek\n");
444 r
= IStream_Write(stm
, data
, sz
, &count
);
445 if( FAILED( r
) || ( count
!= sz
) )
447 WARN("Failed to Write\n");
455 IStream_Release( stm
);
460 static void free_table( MSITABLE
*table
)
463 for( i
=0; i
<table
->row_count
; i
++ )
464 msi_free( table
->data
[i
] );
465 msi_free( table
->data
);
466 for( i
=0; i
<table
->nonpersistent_row_count
; i
++ )
467 msi_free( table
->nonpersistent_data
[i
] );
468 msi_free( table
->nonpersistent_data
);
469 if( (table
->colinfo
!= _Tables_cols
) &&
470 (table
->colinfo
!= _Columns_cols
) )
472 msi_free_colinfo( table
->colinfo
, table
->col_count
);
473 msi_free( table
->colinfo
);
478 static UINT
msi_table_get_row_size( const MSICOLUMNINFO
*cols
, UINT count
)
480 const MSICOLUMNINFO
*last_col
= &cols
[count
-1];
483 return last_col
->offset
+ bytes_per_column( last_col
);
486 /* add this table to the list of cached tables in the database */
487 static UINT
read_table_from_storage( MSITABLE
*t
, IStorage
*stg
)
489 USHORT
*rawdata
= NULL
;
490 UINT rawsize
= 0, i
, j
, row_size
= 0;
492 TRACE("%s\n",debugstr_w(t
->name
));
494 row_size
= msi_table_get_row_size( t
->colinfo
, t
->col_count
);
496 /* if we can't read the table, just assume that it's empty */
497 read_stream_data( stg
, t
->name
, &rawdata
, &rawsize
);
499 return ERROR_SUCCESS
;
501 TRACE("Read %d bytes\n", rawsize
);
503 if( rawsize
% row_size
)
505 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
509 t
->row_count
= rawsize
/ row_size
;
510 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
514 /* transpose all the data */
515 TRACE("Transposing data from %d rows\n", t
->row_count
);
516 for( i
=0; i
<t
->row_count
; i
++ )
518 t
->data
[i
] = msi_alloc( row_size
);
522 for( j
=0; j
<t
->col_count
; j
++ )
524 UINT ofs
= t
->colinfo
[j
].offset
/2;
525 UINT n
= bytes_per_column( &t
->colinfo
[j
] );
530 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
];
533 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
*2 ];
534 t
->data
[i
][ofs
+1] = rawdata
[ofs
*t
->row_count
+ i
*2 + 1];
537 ERR("oops - unknown column width %d\n", n
);
544 return ERROR_SUCCESS
;
547 return ERROR_FUNCTION_FAILED
;
550 void free_cached_tables( MSIDATABASE
*db
)
552 while( !list_empty( &db
->tables
) )
554 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
556 list_remove( &t
->entry
);
561 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
565 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
566 if( !lstrcmpW( name
, t
->name
) )
572 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
574 UINT r
, column_count
= 0;
575 MSICOLUMNINFO
*columns
;
577 /* get the number of columns in this table */
579 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
580 if( r
!= ERROR_SUCCESS
)
583 /* if there's no columns, there's no table */
584 if( column_count
== 0 )
585 return ERROR_INVALID_PARAMETER
;
587 TRACE("Table %s found\n", debugstr_w(name
) );
589 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
) );
591 return ERROR_FUNCTION_FAILED
;
593 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
594 if( r
!= ERROR_SUCCESS
)
597 return ERROR_FUNCTION_FAILED
;
601 *pcount
= column_count
;
606 UINT
msi_create_table( MSIDATABASE
*db
, LPCWSTR name
, column_info
*col_info
,
607 BOOL persistent
, MSITABLE
**table_ret
)
611 MSIRECORD
*rec
= NULL
;
616 /* only add tables that don't exist already */
617 if( TABLE_Exists(db
, name
) )
619 WARN("table %s exists\n", debugstr_w(name
));
620 return ERROR_BAD_QUERY_SYNTAX
;
623 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
625 return ERROR_FUNCTION_FAILED
;
627 table
->row_count
= 0;
629 table
->nonpersistent_row_count
= 0;
630 table
->nonpersistent_data
= NULL
;
631 table
->colinfo
= NULL
;
632 table
->col_count
= 0;
633 table
->persistent
= persistent
;
634 lstrcpyW( table
->name
, name
);
636 for( col
= col_info
; col
; col
= col
->next
)
639 table
->colinfo
= msi_alloc( table
->col_count
* sizeof(MSICOLUMNINFO
) );
643 return ERROR_FUNCTION_FAILED
;
646 for( i
= 0, col
= col_info
; col
; i
++, col
= col
->next
)
648 table
->colinfo
[ i
].tablename
= strdupW( col
->table
);
649 table
->colinfo
[ i
].number
= i
+ 1;
650 table
->colinfo
[ i
].colname
= strdupW( col
->column
);
651 table
->colinfo
[ i
].type
= col
->type
;
652 table
->colinfo
[ i
].offset
= 0;
653 table
->colinfo
[ i
].hash_table
= NULL
;
655 table_calc_column_offsets( table
->colinfo
, table
->col_count
);
657 r
= TABLE_CreateView( db
, szTables
, &tv
);
658 TRACE("CreateView returned %x\n", r
);
665 r
= tv
->ops
->execute( tv
, 0 );
666 TRACE("tv execute returned %x\n", r
);
670 rec
= MSI_CreateRecord( 1 );
674 r
= MSI_RecordSetStringW( rec
, 1, name
);
678 r
= tv
->ops
->insert_row( tv
, rec
, !persistent
);
679 TRACE("insert_row returned %x\n", r
);
683 tv
->ops
->delete( tv
);
686 msiobj_release( &rec
->hdr
);
691 /* add each column to the _Columns table */
692 r
= TABLE_CreateView( db
, szColumns
, &tv
);
696 r
= tv
->ops
->execute( tv
, 0 );
697 TRACE("tv execute returned %x\n", r
);
701 rec
= MSI_CreateRecord( 4 );
705 r
= MSI_RecordSetStringW( rec
, 1, name
);
710 * need to set the table, column number, col name and type
711 * for each column we enter in the table
714 for( col
= col_info
; col
; col
= col
->next
)
716 r
= MSI_RecordSetInteger( rec
, 2, nField
);
720 r
= MSI_RecordSetStringW( rec
, 3, col
->column
);
724 r
= MSI_RecordSetInteger( rec
, 4, col
->type
);
728 r
= tv
->ops
->insert_row( tv
, rec
, FALSE
);
740 msiobj_release( &rec
->hdr
);
741 /* FIXME: remove values from the string table on error */
743 tv
->ops
->delete( tv
);
745 if (r
== ERROR_SUCCESS
)
747 list_add_head( &db
->tables
, &table
->entry
);
756 static UINT
get_table( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**table_ret
)
761 /* first, see if the table is cached */
762 table
= find_cached_table( db
, name
);
766 return ERROR_SUCCESS
;
769 /* nonexistent tables should be interpreted as empty tables */
770 table
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
772 return ERROR_FUNCTION_FAILED
;
774 table
->row_count
= 0;
776 table
->nonpersistent_row_count
= 0;
777 table
->nonpersistent_data
= NULL
;
778 table
->colinfo
= NULL
;
779 table
->col_count
= 0;
780 table
->persistent
= TRUE
;
781 lstrcpyW( table
->name
, name
);
783 /* these two tables are special - we know the column types already */
784 if( !lstrcmpW( name
, szColumns
) )
786 table
->colinfo
= _Columns_cols
;
787 table
->col_count
= sizeof(_Columns_cols
)/sizeof(_Columns_cols
[0]);
789 else if( !lstrcmpW( name
, szTables
) )
791 table
->colinfo
= _Tables_cols
;
792 table
->col_count
= sizeof(_Tables_cols
)/sizeof(_Tables_cols
[0]);
796 r
= table_get_column_info( db
, name
, &table
->colinfo
, &table
->col_count
);
797 if (r
!= ERROR_SUCCESS
)
799 free_table ( table
);
804 r
= read_table_from_storage( table
, db
->storage
);
805 if( r
!= ERROR_SUCCESS
)
811 list_add_head( &db
->tables
, &table
->entry
);
813 return ERROR_SUCCESS
;
816 static UINT
save_table( MSIDATABASE
*db
, MSITABLE
*t
)
818 USHORT
*rawdata
= NULL
, *p
;
819 UINT rawsize
, r
, i
, j
, row_size
;
821 /* Nothing to do for non-persistent tables */
823 return ERROR_SUCCESS
;
825 TRACE("Saving %s\n", debugstr_w( t
->name
) );
827 row_size
= msi_table_get_row_size( t
->colinfo
, t
->col_count
);
829 rawsize
= t
->row_count
* row_size
;
830 rawdata
= msi_alloc_zero( rawsize
);
833 r
= ERROR_NOT_ENOUGH_MEMORY
;
838 for( i
=0; i
<t
->col_count
; i
++ )
840 for( j
=0; j
<t
->row_count
; j
++ )
842 UINT offset
= t
->colinfo
[i
].offset
;
844 *p
++ = t
->data
[j
][offset
/2];
845 if( 4 == bytes_per_column( &t
->colinfo
[i
] ) )
846 *p
++ = t
->data
[j
][offset
/2+1];
850 TRACE("writing %d bytes\n", rawsize
);
851 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
, TRUE
);
859 static void table_calc_column_offsets( MSICOLUMNINFO
*colinfo
, DWORD count
)
863 for( i
=0; colinfo
&& (i
<count
); i
++ )
865 assert( (i
+1) == colinfo
[ i
].number
);
867 colinfo
[i
].offset
= colinfo
[ i
- 1 ].offset
868 + bytes_per_column( &colinfo
[ i
- 1 ] );
870 colinfo
[i
].offset
= 0;
871 TRACE("column %d is [%s] with type %08x ofs %d\n",
872 colinfo
[i
].number
, debugstr_w(colinfo
[i
].colname
),
873 colinfo
[i
].type
, colinfo
[i
].offset
);
877 static UINT
get_defaulttablecolumns( LPCWSTR name
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
879 const MSICOLUMNINFO
*p
;
882 TRACE("%s\n", debugstr_w(name
));
884 if (!lstrcmpW( name
, szTables
))
889 else if (!lstrcmpW( name
, szColumns
))
895 return ERROR_FUNCTION_FAILED
;
897 /* duplicate the string data so we can free it in msi_free_colinfo */
900 if (colinfo
&& (i
< *sz
) )
902 memcpy( &colinfo
[i
], &p
[i
], sizeof(MSICOLUMNINFO
) );
903 colinfo
[i
].tablename
= strdupW( p
[i
].tablename
);
904 colinfo
[i
].colname
= strdupW( p
[i
].colname
);
906 if( colinfo
&& (i
>= *sz
) )
909 table_calc_column_offsets( colinfo
, n
);
911 return ERROR_SUCCESS
;
914 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
918 for( i
=0; i
<count
; i
++ )
920 msi_free( (LPWSTR
) colinfo
[i
].tablename
);
921 msi_free( (LPWSTR
) colinfo
[i
].colname
);
922 msi_free( colinfo
[i
].hash_table
);
926 static LPWSTR
msi_makestring( MSIDATABASE
*db
, UINT stringid
)
928 return strdupW(msi_string_lookup_id( db
->strings
, stringid
));
931 static UINT
get_tablecolumns( MSIDATABASE
*db
,
932 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
934 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
935 MSITABLE
*table
= NULL
;
937 TRACE("%s\n", debugstr_w(szTableName
));
939 /* first check if there is a default table with that name */
940 r
= get_defaulttablecolumns( szTableName
, colinfo
, sz
);
941 if( ( r
== ERROR_SUCCESS
) && *sz
)
944 r
= get_table( db
, szColumns
, &table
);
945 if( r
!= ERROR_SUCCESS
)
947 ERR("couldn't load _Columns table\n");
948 return ERROR_FUNCTION_FAILED
;
951 /* convert table and column names to IDs from the string table */
952 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
953 if( r
!= ERROR_SUCCESS
)
955 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
959 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
961 /* Note: _Columns table doesn't have non-persistent data */
963 /* if maxcount is non-zero, assume it's exactly right for this table */
964 memset( colinfo
, 0, maxcount
*sizeof(*colinfo
) );
965 count
= table
->row_count
;
966 for( i
=0; i
<count
; i
++ )
968 if( table
->data
[ i
][ 0 ] != table_id
)
972 UINT id
= table
->data
[ i
] [ 2 ];
973 UINT col
= table
->data
[ i
][ 1 ] - (1<<15);
975 /* check the column number is in range */
976 if (col
<1 || col
>maxcount
)
978 ERR("column %d out of range\n", col
);
982 /* check if this column was already set */
983 if (colinfo
[ col
- 1 ].number
)
985 ERR("duplicate column %d\n", col
);
989 colinfo
[ col
- 1 ].tablename
= msi_makestring( db
, table_id
);
990 colinfo
[ col
- 1 ].number
= col
;
991 colinfo
[ col
- 1 ].colname
= msi_makestring( db
, id
);
992 colinfo
[ col
- 1 ].type
= table
->data
[ i
] [ 3 ] - (1<<15);
993 colinfo
[ col
- 1 ].offset
= 0;
994 colinfo
[ col
- 1 ].hash_table
= NULL
;
999 TRACE("%s has %d columns\n", debugstr_w(szTableName
), n
);
1001 if (colinfo
&& n
!= maxcount
)
1003 ERR("missing column in table %s\n", debugstr_w(szTableName
));
1004 msi_free_colinfo(colinfo
, maxcount
);
1005 return ERROR_FUNCTION_FAILED
;
1008 table_calc_column_offsets( colinfo
, n
);
1011 return ERROR_SUCCESS
;
1014 /* try to find the table name in the _Tables table */
1015 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPCWSTR name
)
1017 UINT r
, table_id
= 0, i
, count
;
1018 MSITABLE
*table
= NULL
;
1020 if( !lstrcmpW( name
, szTables
) )
1022 if( !lstrcmpW( name
, szColumns
) )
1025 r
= msi_string2idW( db
->strings
, name
, &table_id
);
1026 if( r
!= ERROR_SUCCESS
)
1028 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1032 r
= get_table( db
, szTables
, &table
);
1033 if( r
!= ERROR_SUCCESS
)
1035 ERR("table %s not available\n", debugstr_w(szTables
));
1039 count
= table
->row_count
;
1040 for( i
=0; i
<count
; i
++ )
1041 if( table
->data
[ i
][ 0 ] == table_id
)
1047 count
= table
->nonpersistent_row_count
;
1048 for( i
=0; i
<count
; i
++ )
1049 if( table
->nonpersistent_data
[ i
][ 0 ] == table_id
)
1055 TRACE("Searched %d tables, but %d was not found\n", count
, table_id
);
1060 /* below is the query interface to a table */
1062 typedef struct tagMSITABLEVIEW
1067 MSICOLUMNINFO
*columns
;
1073 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1075 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1080 return ERROR_INVALID_PARAMETER
;
1082 if( (col
==0) || (col
>tv
->num_cols
) )
1083 return ERROR_INVALID_PARAMETER
;
1085 /* how many rows are there ? */
1086 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1087 return ERROR_NO_MORE_ITEMS
;
1089 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1091 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1092 ERR("%p %p\n", tv
, tv
->columns
);
1093 return ERROR_FUNCTION_FAILED
;
1096 if (row
>= tv
->table
->row_count
)
1098 row
-= tv
->table
->row_count
;
1099 data
= tv
->table
->nonpersistent_data
;
1102 data
= tv
->table
->data
;
1103 n
= bytes_per_column( &tv
->columns
[col
-1] );
1107 offset
= tv
->columns
[col
-1].offset
/2;
1108 *val
= data
[row
][offset
] +
1109 (data
[row
][offset
+ 1] << 16);
1112 offset
= tv
->columns
[col
-1].offset
/2;
1113 *val
= data
[row
][offset
];
1116 ERR("oops! what is %d bytes per column?\n", n
);
1117 return ERROR_FUNCTION_FAILED
;
1120 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1122 return ERROR_SUCCESS
;
1126 * We need a special case for streams, as we need to reference column with
1127 * the name of the stream in the same table, and the table name
1128 * which may not be available at higher levels of the query
1130 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1132 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1133 UINT ival
= 0, refcol
= 0, r
;
1137 static const WCHAR szDot
[] = { '.', 0 };
1140 if( !view
->ops
->fetch_int
)
1141 return ERROR_INVALID_PARAMETER
;
1144 * The column marked with the type stream data seems to have a single number
1145 * which references the column containing the name of the stream data
1147 * Fetch the column to reference first.
1149 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1150 if( r
!= ERROR_SUCCESS
)
1153 /* check the column value is in range */
1154 if (ival
< 0 || ival
> tv
->num_cols
|| ival
== col
)
1156 ERR("bad column ref (%u) for stream\n", ival
);
1157 return ERROR_FUNCTION_FAILED
;
1160 if ( tv
->columns
[ival
- 1].type
& MSITYPE_STRING
)
1162 /* now get the column with the name of the stream */
1163 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1164 if ( r
!= ERROR_SUCCESS
)
1167 /* lookup the string value from the string table */
1168 sval
= msi_string_lookup_id( tv
->db
->strings
, refcol
);
1170 return ERROR_INVALID_PARAMETER
;
1174 static const WCHAR fmt
[] = { '%','d',0 };
1175 sprintfW( number
, fmt
, ival
);
1179 len
= lstrlenW( tv
->name
) + 2 + lstrlenW( sval
);
1180 full_name
= msi_alloc( len
*sizeof(WCHAR
) );
1181 lstrcpyW( full_name
, tv
->name
);
1182 lstrcatW( full_name
, szDot
);
1183 lstrcatW( full_name
, sval
);
1185 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1187 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1188 msi_free( full_name
);
1193 static UINT
TABLE_set_int( MSITABLEVIEW
*tv
, UINT row
, UINT col
, UINT val
)
1199 return ERROR_INVALID_PARAMETER
;
1201 if( (col
==0) || (col
>tv
->num_cols
) )
1202 return ERROR_INVALID_PARAMETER
;
1204 if( row
>= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
)
1205 return ERROR_INVALID_PARAMETER
;
1207 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1209 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1210 ERR("%p %p\n", tv
, tv
->columns
);
1211 return ERROR_FUNCTION_FAILED
;
1214 msi_free( tv
->columns
[col
-1].hash_table
);
1215 tv
->columns
[col
-1].hash_table
= NULL
;
1217 if (row
>= tv
->table
->row_count
)
1219 row
-= tv
->table
->row_count
;
1220 data
= tv
->table
->nonpersistent_data
;
1223 data
= tv
->table
->data
;
1224 n
= bytes_per_column( &tv
->columns
[col
-1] );
1228 offset
= tv
->columns
[col
-1].offset
/2;
1229 data
[row
][offset
] = val
& 0xffff;
1230 data
[row
][offset
+ 1] = (val
>>16)&0xffff;
1233 offset
= tv
->columns
[col
-1].offset
/2;
1234 data
[row
][offset
] = val
;
1237 ERR("oops! what is %d bytes per column?\n", n
);
1238 return ERROR_FUNCTION_FAILED
;
1240 return ERROR_SUCCESS
;
1243 static UINT
TABLE_set_row( struct tagMSIVIEW
*view
, UINT row
, MSIRECORD
*rec
, UINT mask
)
1245 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1246 UINT i
, val
, r
= ERROR_SUCCESS
;
1249 return ERROR_INVALID_PARAMETER
;
1251 /* test if any of the mask bits are invalid */
1252 if ( mask
>= (1<<tv
->num_cols
) )
1253 return ERROR_INVALID_PARAMETER
;
1255 for ( i
= 0; i
< tv
->num_cols
; i
++ )
1259 /* only update the fields specified in the mask */
1260 if ( !(mask
&(1<<i
)) )
1263 /* if row >= tv->table->row_count then it is a non-persistent row */
1264 persistent
= tv
->table
->persistent
&& (row
< tv
->table
->row_count
);
1265 /* FIXME: should we allow updating keys? */
1268 if ( !MSI_RecordIsNull( rec
, i
+ 1 ) )
1270 if ( MSITYPE_IS_BINARY(tv
->columns
[ i
].type
) )
1272 val
= 1; /* refers to the first key column */
1274 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1276 LPCWSTR sval
= MSI_RecordGetString( rec
, i
+ 1 );
1277 val
= msi_addstringW( tv
->db
->strings
, 0, sval
, -1, 1,
1278 persistent
? StringPersistent
: StringNonPersistent
);
1280 else if ( 2 == bytes_per_column( &tv
->columns
[ i
] ) )
1282 val
= 0x8000 + MSI_RecordGetInteger( rec
, i
+ 1 );
1283 if ( val
& 0xffff0000 )
1285 ERR("field %u value %d out of range\n", i
+1, val
- 0x8000 );
1286 return ERROR_FUNCTION_FAILED
;
1291 INT ival
= MSI_RecordGetInteger( rec
, i
+ 1 );
1292 val
= ival
^ 0x80000000;
1296 r
= TABLE_set_int( tv
, row
, i
+1, val
);
1297 if ( r
!= ERROR_SUCCESS
)
1303 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
, BOOL temporary
)
1305 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1311 TRACE("%p %s\n", view
, temporary
? "TRUE" : "FALSE");
1314 return ERROR_INVALID_PARAMETER
;
1316 row
= msi_alloc_zero( tv
->row_size
);
1318 return ERROR_NOT_ENOUGH_MEMORY
;
1322 row_count
= &tv
->table
->nonpersistent_row_count
;
1323 data_ptr
= &tv
->table
->nonpersistent_data
;
1324 *num
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1328 row_count
= &tv
->table
->row_count
;
1329 data_ptr
= &tv
->table
->data
;
1330 *num
= tv
->table
->row_count
;
1333 sz
= (*row_count
+ 1) * sizeof (UINT
*);
1335 p
= msi_realloc( *data_ptr
, sz
);
1337 p
= msi_alloc( sz
);
1341 return ERROR_NOT_ENOUGH_MEMORY
;
1345 (*data_ptr
)[*row_count
] = row
;
1348 return ERROR_SUCCESS
;
1351 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1353 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1355 TRACE("%p %p\n", tv
, record
);
1357 TRACE("There are %d columns\n", tv
->num_cols
);
1359 return ERROR_SUCCESS
;
1362 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1364 TRACE("%p\n", view
);
1366 return ERROR_SUCCESS
;
1369 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1371 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1373 TRACE("%p %p %p\n", view
, rows
, cols
);
1376 *cols
= tv
->num_cols
;
1380 return ERROR_INVALID_PARAMETER
;
1381 *rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1384 return ERROR_SUCCESS
;
1387 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1388 UINT n
, LPWSTR
*name
, UINT
*type
)
1390 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1392 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1394 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1395 return ERROR_INVALID_PARAMETER
;
1399 *name
= strdupW( tv
->columns
[n
-1].colname
);
1401 return ERROR_FUNCTION_FAILED
;
1404 *type
= tv
->columns
[n
-1].type
;
1406 return ERROR_SUCCESS
;
1409 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
);
1411 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1415 /* check there's no null values where they're not allowed */
1416 for( i
= 0; i
< tv
->num_cols
; i
++ )
1418 if ( tv
->columns
[i
].type
& MSITYPE_NULLABLE
)
1421 if ( MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1422 TRACE("skipping binary column\n");
1423 else if ( tv
->columns
[i
].type
& MSITYPE_STRING
)
1427 str
= MSI_RecordGetString( rec
, i
+1 );
1428 if (str
== NULL
|| str
[0] == 0)
1429 return ERROR_INVALID_DATA
;
1435 n
= MSI_RecordGetInteger( rec
, i
+1 );
1436 if (n
== MSI_NULL_INTEGER
)
1437 return ERROR_INVALID_DATA
;
1441 /* check there's no duplicate keys */
1442 r
= msi_table_find_row( tv
, rec
, &row
);
1443 if (r
== ERROR_SUCCESS
)
1444 return ERROR_INVALID_DATA
;
1446 return ERROR_SUCCESS
;
1449 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
, BOOL temporary
)
1451 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1454 TRACE("%p %p %s\n", tv
, rec
, temporary
? "TRUE" : "FALSE" );
1456 /* check that the key is unique - can we find a matching row? */
1457 r
= table_validate_new( tv
, rec
);
1458 if( r
!= ERROR_SUCCESS
)
1459 return ERROR_FUNCTION_FAILED
;
1461 r
= table_create_new_row( view
, &row
, temporary
);
1462 TRACE("insert_row returned %08x\n", r
);
1463 if( r
!= ERROR_SUCCESS
)
1466 return TABLE_set_row( view
, row
, rec
, (1<<tv
->num_cols
) - 1 );
1469 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1472 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1475 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1477 switch (eModifyMode
)
1479 case MSIMODIFY_VALIDATE_NEW
:
1480 r
= table_validate_new( tv
, rec
);
1483 case MSIMODIFY_INSERT_TEMPORARY
:
1484 r
= table_validate_new( tv
, rec
);
1485 if (r
!= ERROR_SUCCESS
)
1487 r
= TABLE_insert_row( view
, rec
, TRUE
);
1490 case MSIMODIFY_REFRESH
:
1491 case MSIMODIFY_INSERT
:
1492 case MSIMODIFY_UPDATE
:
1493 case MSIMODIFY_ASSIGN
:
1494 case MSIMODIFY_REPLACE
:
1495 case MSIMODIFY_MERGE
:
1496 case MSIMODIFY_DELETE
:
1497 case MSIMODIFY_VALIDATE
:
1498 case MSIMODIFY_VALIDATE_FIELD
:
1499 case MSIMODIFY_VALIDATE_DELETE
:
1500 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1501 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1505 r
= ERROR_INVALID_DATA
;
1511 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1513 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1515 TRACE("%p\n", view
);
1523 return ERROR_SUCCESS
;
1526 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1527 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1529 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1530 const MSICOLUMNHASHENTRY
*entry
;
1532 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1535 return ERROR_INVALID_PARAMETER
;
1537 if( (col
==0) || (col
> tv
->num_cols
) )
1538 return ERROR_INVALID_PARAMETER
;
1540 if( !tv
->columns
[col
-1].hash_table
)
1543 UINT num_rows
= tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
;
1544 MSICOLUMNHASHENTRY
**hash_table
;
1545 MSICOLUMNHASHENTRY
*new_entry
;
1547 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1549 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1550 ERR("%p %p\n", tv
, tv
->columns
);
1551 return ERROR_FUNCTION_FAILED
;
1554 /* allocate contiguous memory for the table and its entries so we
1555 * don't have to do an expensive cleanup */
1556 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1557 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1559 return ERROR_OUTOFMEMORY
;
1561 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1562 tv
->columns
[col
-1].hash_table
= hash_table
;
1564 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1566 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1570 if (view
->ops
->fetch_int( view
, i
, col
, &row_value
) != ERROR_SUCCESS
)
1573 new_entry
->next
= NULL
;
1574 new_entry
->value
= row_value
;
1576 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1578 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1579 while (prev_entry
->next
)
1580 prev_entry
= prev_entry
->next
;
1581 prev_entry
->next
= new_entry
;
1584 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1589 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1591 entry
= (*handle
)->next
;
1593 while (entry
&& entry
->value
!= val
)
1594 entry
= entry
->next
;
1598 return ERROR_NO_MORE_ITEMS
;
1601 return ERROR_SUCCESS
;
1605 static const MSIVIEWOPS table_ops
=
1613 TABLE_get_dimensions
,
1614 TABLE_get_column_info
,
1617 TABLE_find_matching_rows
1620 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
1625 static const WCHAR Streams
[] = {'_','S','t','r','e','a','m','s',0};
1627 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
1629 if ( !lstrcmpW( name
, Streams
) )
1630 return STREAMS_CreateView( db
, view
);
1632 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
1633 tv
= msi_alloc_zero( sz
);
1635 return ERROR_FUNCTION_FAILED
;
1637 r
= get_table( db
, name
, &tv
->table
);
1638 if( r
!= ERROR_SUCCESS
)
1641 WARN("table not found\n");
1645 TRACE("table %p found with %d columns\n", tv
->table
, tv
->table
->col_count
);
1647 /* fill the structure */
1648 tv
->view
.ops
= &table_ops
;
1650 tv
->columns
= tv
->table
->colinfo
;
1651 tv
->num_cols
= tv
->table
->col_count
;
1652 tv
->row_size
= msi_table_get_row_size( tv
->table
->colinfo
, tv
->table
->col_count
);
1654 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
1656 *view
= (MSIVIEW
*) tv
;
1657 lstrcpyW( tv
->name
, name
);
1659 return ERROR_SUCCESS
;
1662 UINT
MSI_CommitTables( MSIDATABASE
*db
)
1665 MSITABLE
*table
= NULL
;
1669 r
= msi_save_string_table( db
->strings
, db
->storage
);
1670 if( r
!= ERROR_SUCCESS
)
1672 WARN("failed to save string table r=%08x\n",r
);
1676 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
1678 r
= save_table( db
, table
);
1679 if( r
!= ERROR_SUCCESS
)
1681 WARN("failed to save table %s (r=%08x)\n",
1682 debugstr_w(table
->name
), r
);
1687 /* force everything to reload next time */
1688 free_cached_tables( db
);
1690 return ERROR_SUCCESS
;
1693 MSICONDITION
MSI_DatabaseIsTablePersistent( MSIDATABASE
*db
, LPCWSTR table
)
1698 TRACE("%p %s\n", db
, debugstr_w(table
));
1701 return MSICONDITION_ERROR
;
1703 r
= get_table( db
, table
, &t
);
1704 if (r
!= ERROR_SUCCESS
)
1705 return MSICONDITION_NONE
;
1708 return MSICONDITION_TRUE
;
1710 return MSICONDITION_FALSE
;
1713 static MSIRECORD
*msi_get_transform_record( MSITABLEVIEW
*tv
, string_table
*st
, USHORT
*rawdata
)
1715 UINT i
, val
, ofs
= 0;
1716 USHORT mask
= *rawdata
++;
1717 MSICOLUMNINFO
*columns
= tv
->columns
;
1720 rec
= MSI_CreateRecord( tv
->num_cols
);
1725 for( i
=0; i
<tv
->num_cols
; i
++ )
1727 UINT n
= bytes_per_column( &columns
[i
] );
1729 if ( (mask
&1) && (i
>=(mask
>>8)) )
1731 /* all keys must be present */
1732 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
1739 if( (columns
[i
].type
& MSITYPE_STRING
) &&
1740 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1742 LPCWSTR sval
= msi_string_lookup_id( st
, val
);
1743 MSI_RecordSetStringW( rec
, i
+1, sval
);
1744 TRACE(" field %d [%s]\n", i
+1, debugstr_w(sval
));
1749 MSI_RecordSetInteger( rec
, i
+1, val
^0x8000 );
1750 TRACE(" field %d [0x%04x]\n", i
+1, val
);
1754 val
= (rawdata
[ofs
] + (rawdata
[ofs
+ 1]<<16));
1756 MSI_RecordSetInteger( rec
, i
+1, val
^0x80000000 );
1757 TRACE(" field %d [0x%08x]\n", i
+1, val
);
1760 ERR("oops - unknown column width %d\n", n
);
1768 static void dump_record( MSIRECORD
*rec
)
1772 n
= MSI_RecordGetFieldCount( rec
);
1773 for( i
=1; i
<=n
; i
++ )
1775 LPCWSTR sval
= MSI_RecordGetString( rec
, i
);
1777 if( MSI_RecordIsNull( rec
, i
) )
1778 TRACE("row -> []\n");
1779 else if( (sval
= MSI_RecordGetString( rec
, i
)) )
1780 TRACE("row -> [%s]\n", debugstr_w(sval
));
1782 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
1786 static void dump_table( string_table
*st
, USHORT
*rawdata
, UINT rawsize
)
1791 for( i
=0; i
<(rawsize
/2); i
++ )
1793 sval
= msi_string_lookup_id( st
, rawdata
[i
] );
1794 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_w(sval
) );
1798 static UINT
* msi_record_to_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1803 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
1804 for( i
=0; i
<tv
->num_cols
; i
++ )
1808 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
1811 /* turn the transform column value into a row value */
1812 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
1813 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1815 str
= MSI_RecordGetString( rec
, i
+1 );
1816 r
= msi_string2idW( tv
->db
->strings
, str
, &data
[i
] );
1818 /* if there's no matching string in the string table,
1819 these keys can't match any record, so fail now. */
1820 if( ERROR_SUCCESS
!= r
)
1828 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
1829 if ((tv
->columns
[i
].type
&0xff) == 2)
1832 data
[i
] += 0x80000000;
1838 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, UINT
*data
)
1840 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
1842 for( i
=0; i
<tv
->num_cols
; i
++ )
1844 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
1847 /* turn the transform column value into a row value */
1848 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
1849 if ( r
!= ERROR_SUCCESS
)
1851 ERR("TABLE_fetch_int shouldn't fail here\n");
1855 /* if this key matches, move to the next column */
1858 ret
= ERROR_FUNCTION_FAILED
;
1862 ret
= ERROR_SUCCESS
;
1868 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
)
1870 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
1872 data
= msi_record_to_row( tv
, rec
);
1875 for( i
= 0; i
< tv
->table
->row_count
+ tv
->table
->nonpersistent_row_count
; i
++ )
1877 r
= msi_row_matches( tv
, i
, data
);
1878 if( r
== ERROR_SUCCESS
)
1888 static UINT
msi_delete_row( MSITABLEVIEW
*tv
, UINT row
)
1892 for( i
=1; i
<=tv
->num_cols
; i
++ )
1893 TABLE_set_int( tv
, row
, i
, 0 );
1894 return ERROR_SUCCESS
;
1897 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
1898 string_table
*st
, LPCWSTR name
)
1901 USHORT
*rawdata
= NULL
;
1902 MSITABLEVIEW
*tv
= NULL
;
1903 UINT r
, n
, sz
, i
, mask
;
1904 MSIRECORD
*rec
= NULL
;
1909 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
1911 /* read the transform data */
1912 read_stream_data( stg
, name
, &rawdata
, &rawsize
);
1915 TRACE("table %s empty\n", debugstr_w(name
) );
1916 return ERROR_INVALID_TABLE
;
1919 /* create a table view */
1920 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
1921 if( r
!= ERROR_SUCCESS
)
1924 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
1925 if( r
!= ERROR_SUCCESS
)
1928 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1929 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
1931 /* interpret the data */
1933 for( n
=0; n
< (rawsize
/2); )
1940 * if the low bit is set, columns are continuous and
1941 * the number of columns is specified in the high byte
1943 sz
= 2 + tv
->row_size
;
1948 * If the low bit is not set, rowdata[n] is a bitmask.
1949 * Excepting for key fields, which are always present,
1950 * each bit indicates that a field is present in the transform record.
1952 * rawdata[n] == 0 is a special case ... only the keys will be present
1953 * and it means that this row should be deleted.
1956 for( i
=0; i
<tv
->num_cols
; i
++ )
1958 if( (tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1<<i
)&mask
))
1959 sz
+= bytes_per_column( &tv
->columns
[i
] );
1963 /* check we didn't run of the end of the table */
1964 if ( (n
+sz
) > rawsize
)
1967 dump_table( st
, rawdata
, rawsize
);
1971 rec
= msi_get_transform_record( tv
, st
, &rawdata
[n
] );
1976 TRACE("inserting record\n");
1979 * Native msi seems writes nul into the
1980 * Number (2nd) column of the _Columns table.
1981 * Not sure that it's deliberate...
1983 if (!lstrcmpW(name
, szColumns
))
1988 MSI_RecordGetStringW( rec
, 1, table
, &sz
);
1990 /* reset the column number on a new table */
1991 if ( lstrcmpW(coltable
, table
) )
1994 lstrcpyW( coltable
, table
);
1997 /* fix nul column numbers */
1998 MSI_RecordSetInteger( rec
, 2, ++colcol
);
2001 r
= TABLE_insert_row( &tv
->view
, rec
, FALSE
);
2002 if (r
!= ERROR_SUCCESS
)
2003 ERR("insert row failed\n");
2009 r
= msi_table_find_row( tv
, rec
, &row
);
2010 if (r
!= ERROR_SUCCESS
)
2011 ERR("no matching row to transform\n");
2014 TRACE("modifying row [%d]:\n", row
);
2015 TABLE_set_row( &tv
->view
, row
, rec
, mask
);
2019 TRACE("deleting row [%d]:\n", row
);
2020 msi_delete_row( tv
, row
);
2023 if( TRACE_ON(msidb
) ) dump_record( rec
);
2024 msiobj_release( &rec
->hdr
);
2031 /* no need to free the table, it's associated with the database */
2032 msi_free( rawdata
);
2034 tv
->view
.ops
->delete( &tv
->view
);
2036 return ERROR_SUCCESS
;
2040 * msi_table_apply_transform
2042 * Enumerate the table transforms in a transform storage and apply each one.
2044 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
2046 IEnumSTATSTG
*stgenum
= NULL
;
2051 string_table
*strings
;
2052 UINT ret
= ERROR_FUNCTION_FAILED
;
2054 TRACE("%p %p\n", db
, stg
);
2056 strings
= msi_load_string_table( stg
);
2060 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
2065 * Apply _Tables and _Columns transforms first so that
2066 * the table metadata is correct, and empty tables exist.
2068 ret
= msi_table_load_transform( db
, stg
, strings
, szTables
);
2069 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2072 ret
= msi_table_load_transform( db
, stg
, strings
, szColumns
);
2073 if (ret
!= ERROR_SUCCESS
&& ret
!= ERROR_INVALID_TABLE
)
2076 ret
= ERROR_SUCCESS
;
2078 while( r
== ERROR_SUCCESS
)
2081 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
2082 if( FAILED( r
) || !count
)
2085 decode_streamname( stat
.pwcsName
, name
);
2086 if ( name
[0] != 0x4840 )
2089 TRACE("transform contains stream %s\n", debugstr_w(name
));
2091 if ( !lstrcmpW( name
+1, szStringPool
) ||
2092 !lstrcmpW( name
+1, szStringData
) ||
2093 !lstrcmpW( name
+1, szColumns
) ||
2094 !lstrcmpW( name
+1, szTables
) )
2097 ret
= msi_table_load_transform( db
, stg
, strings
, name
+1 );
2100 if ( ret
== ERROR_SUCCESS
)
2101 append_storage_to_db( db
, stg
);
2105 IEnumSTATSTG_Release( stgenum
);
2107 msi_destroy_stringtable( strings
);
2112 void append_storage_to_db( MSIDATABASE
*db
, IStorage
*stg
)
2116 t
= msi_alloc( sizeof *t
);
2118 IStorage_AddRef( stg
);
2119 list_add_tail( &db
->transforms
, &t
->entry
);
2122 void msi_free_transforms( MSIDATABASE
*db
)
2124 while( !list_empty( &db
->transforms
) )
2126 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
),
2127 MSITRANSFORM
, entry
);
2128 list_remove( &t
->entry
);
2129 IStorage_Release( t
->stg
);