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
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
30 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msidb
);
42 #define MSITABLE_HASH_TABLE_SIZE 37
44 typedef struct tagMSICOLUMNHASHENTRY
46 struct tagMSICOLUMNHASHENTRY
*next
;
51 typedef struct tagMSICOLUMNINFO
58 MSICOLUMNHASHENTRY
**hash_table
;
69 typedef struct tagMSITRANSFORM
{
74 #define MAX_STREAM_NAME 0x1f
76 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
77 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
78 static UINT
get_tablecolumns( MSIDATABASE
*db
,
79 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
80 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
);
82 static inline UINT
bytes_per_column( const MSICOLUMNINFO
*col
)
84 if( col
->type
& MSITYPE_STRING
)
86 if( (col
->type
& 0xff) > 4 )
87 ERR("Invalid column size!\n");
88 return col
->type
& 0xff;
91 static int utf2mime(int x
)
93 if( (x
>='0') && (x
<='9') )
95 if( (x
>='A') && (x
<='Z') )
97 if( (x
>='a') && (x
<='z') )
106 static LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
108 DWORD count
= MAX_STREAM_NAME
;
113 count
= lstrlenW( in
)+2;
114 out
= msi_alloc( count
*sizeof(WCHAR
) );
130 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
132 ch
= utf2mime(ch
) + 0x4800;
134 if( next
&& (next
<0x80) )
136 next
= utf2mime(next
);
147 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
152 static int mime2utf(int x
)
159 return x
- 10 - 26 + 'a';
160 if( x
== (10+26+26) )
165 static BOOL
decode_streamname(LPWSTR in
, LPWSTR out
)
170 while ( (ch
= *in
++) )
172 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
175 ch
= mime2utf(ch
-0x4800);
179 *out
++ = mime2utf(ch
&0x3f);
181 ch
= mime2utf((ch
>>6)&0x3f);
191 void enum_stream_names( IStorage
*stg
)
193 IEnumSTATSTG
*stgenum
= NULL
;
199 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
207 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
208 if( FAILED( r
) || !count
)
210 decode_streamname( stat
.pwcsName
, name
);
211 TRACE("stream %2ld -> %s %s\n", n
,
212 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
216 IEnumSTATSTG_Release( stgenum
);
219 static UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
,
220 USHORT
**pdata
, UINT
*psz
)
223 UINT ret
= ERROR_FUNCTION_FAILED
;
230 encname
= encode_streamname(TRUE
, stname
);
232 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
234 r
= IStorage_OpenStream(stg
, encname
, NULL
,
235 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
239 WARN("open stream failed r = %08lx - empty table?\n",r
);
243 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
246 WARN("open stream failed r = %08lx!\n",r
);
250 if( stat
.cbSize
.QuadPart
>> 32 )
256 sz
= stat
.cbSize
.QuadPart
;
257 data
= msi_alloc( sz
);
260 WARN("couldn't allocate memory r=%08lx!\n",r
);
261 ret
= ERROR_NOT_ENOUGH_MEMORY
;
265 r
= IStream_Read(stm
, data
, sz
, &count
);
266 if( FAILED( r
) || ( count
!= sz
) )
269 WARN("read stream failed r = %08lx!\n",r
);
278 IStream_Release( stm
);
283 UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
288 encname
= encode_streamname(FALSE
, stname
);
290 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
292 r
= IStorage_OpenStream(db
->storage
, encname
, NULL
,
293 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
296 MSITRANSFORM
*transform
;
298 LIST_FOR_EACH_ENTRY( transform
, &db
->transforms
, MSITRANSFORM
, entry
)
300 TRACE("looking for %s in transform storage\n", debugstr_w(stname
) );
301 r
= IStorage_OpenStream( transform
->stg
, encname
, NULL
,
302 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
310 return SUCCEEDED(r
) ? ERROR_SUCCESS
: ERROR_FUNCTION_FAILED
;
313 UINT
read_raw_stream_data( MSIDATABASE
*db
, LPCWSTR stname
,
314 USHORT
**pdata
, UINT
*psz
)
317 UINT ret
= ERROR_FUNCTION_FAILED
;
323 r
= db_get_raw_stream( db
, stname
, &stm
);
324 if( r
!= ERROR_SUCCESS
)
326 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
329 WARN("open stream failed r = %08lx!\n",r
);
333 if( stat
.cbSize
.QuadPart
>> 32 )
339 sz
= stat
.cbSize
.QuadPart
;
340 data
= msi_alloc( sz
);
343 WARN("couldn't allocate memory r=%08lx!\n",r
);
344 ret
= ERROR_NOT_ENOUGH_MEMORY
;
348 r
= IStream_Read(stm
, data
, sz
, &count
);
349 if( FAILED( r
) || ( count
!= sz
) )
352 WARN("read stream failed r = %08lx!\n",r
);
361 IStream_Release( stm
);
366 static UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
367 LPVOID data
, UINT sz
)
370 UINT ret
= ERROR_FUNCTION_FAILED
;
377 encname
= encode_streamname(TRUE
, stname
);
378 r
= IStorage_OpenStream( stg
, encname
, NULL
,
379 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
382 r
= IStorage_CreateStream( stg
, encname
,
383 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
388 WARN("open stream failed r = %08lx\n",r
);
393 r
= IStream_SetSize( stm
, size
);
396 WARN("Failed to SetSize\n");
401 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
404 WARN("Failed to Seek\n");
408 r
= IStream_Write(stm
, data
, sz
, &count
);
409 if( FAILED( r
) || ( count
!= sz
) )
411 WARN("Failed to Write\n");
418 IStream_Release( stm
);
423 static void free_table( MSITABLE
*table
)
426 for( i
=0; i
<table
->row_count
; i
++ )
427 msi_free( table
->data
[i
] );
428 msi_free( table
->data
);
432 static UINT
msi_table_get_row_size( const MSICOLUMNINFO
*cols
, UINT count
)
434 const MSICOLUMNINFO
*last_col
= &cols
[count
-1];
437 return last_col
->offset
+ bytes_per_column( last_col
);
440 /* add this table to the list of cached tables in the database */
441 static MSITABLE
*read_table_from_storage( IStorage
*stg
, LPCWSTR name
,
442 const MSICOLUMNINFO
*cols
, UINT num_cols
)
445 USHORT
*rawdata
= NULL
;
446 UINT rawsize
= 0, i
, j
, row_size
= 0;
448 TRACE("%s\n",debugstr_w(name
));
450 /* nonexistent tables should be interpreted as empty tables */
451 t
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
455 row_size
= msi_table_get_row_size( cols
, num_cols
);
459 lstrcpyW( t
->name
, name
);
461 /* if we can't read the table, just assume that it's empty */
462 read_stream_data( stg
, name
, &rawdata
, &rawsize
);
466 TRACE("Read %d bytes\n", rawsize
);
468 if( rawsize
% row_size
)
470 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
474 t
->row_count
= rawsize
/ row_size
;
475 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
479 /* transpose all the data */
480 TRACE("Transposing data from %d columns\n", t
->row_count
);
481 for( i
=0; i
<t
->row_count
; i
++ )
483 t
->data
[i
] = msi_alloc( row_size
);
487 for( j
=0; j
<num_cols
; j
++ )
489 UINT ofs
= cols
[j
].offset
/2;
490 UINT n
= bytes_per_column( &cols
[j
] );
495 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
];
498 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
*2 ];
499 t
->data
[i
][ofs
+1] = rawdata
[ofs
*t
->row_count
+ i
*2 + 1];
502 ERR("oops - unknown column width %d\n", n
);
516 void free_cached_tables( MSIDATABASE
*db
)
518 while( !list_empty( &db
->tables
) )
520 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
522 list_remove( &t
->entry
);
527 static MSITABLE
*find_cached_table( MSIDATABASE
*db
, LPCWSTR name
)
531 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
532 if( !lstrcmpW( name
, t
->name
) )
538 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
540 UINT r
, column_count
= 0;
541 MSICOLUMNINFO
*columns
;
543 /* get the number of columns in this table */
545 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
546 if( r
!= ERROR_SUCCESS
)
549 /* if there's no columns, there's no table */
550 if( column_count
== 0 )
551 return ERROR_INVALID_PARAMETER
;
553 TRACE("Table %s found\n", debugstr_w(name
) );
555 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
) );
557 return ERROR_FUNCTION_FAILED
;
559 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
560 if( r
!= ERROR_SUCCESS
)
563 return ERROR_FUNCTION_FAILED
;
567 *pcount
= column_count
;
572 static MSITABLE
*get_table( MSIDATABASE
*db
, LPCWSTR name
,
573 const MSICOLUMNINFO
*cols
, UINT num_cols
)
577 /* first, see if the table is cached */
578 table
= find_cached_table( db
, name
);
582 table
= read_table_from_storage( db
->storage
, name
, cols
, num_cols
);
584 list_add_head( &db
->tables
, &table
->entry
);
589 static UINT
save_table( MSIDATABASE
*db
, MSITABLE
*t
)
591 USHORT
*rawdata
= NULL
, *p
;
592 UINT rawsize
, r
, i
, j
, row_size
, num_cols
= 0;
593 MSICOLUMNINFO
*cols
= NULL
;
595 TRACE("Saving %s\n", debugstr_w( t
->name
) );
597 r
= table_get_column_info( db
, t
->name
, &cols
, &num_cols
);
598 if( r
!= ERROR_SUCCESS
)
601 row_size
= msi_table_get_row_size( cols
, num_cols
);
603 rawsize
= t
->row_count
* row_size
;
604 rawdata
= msi_alloc_zero( rawsize
);
607 r
= ERROR_NOT_ENOUGH_MEMORY
;
612 for( i
=0; i
<num_cols
; i
++ )
614 for( j
=0; j
<t
->row_count
; j
++ )
616 UINT offset
= cols
[i
].offset
;
618 *p
++ = t
->data
[j
][offset
/2];
619 if( 4 == bytes_per_column( &cols
[i
] ) )
620 *p
++ = t
->data
[j
][offset
/2+1];
624 TRACE("writing %d bytes\n", rawsize
);
625 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
);
628 msi_free_colinfo( cols
, num_cols
);
635 HRESULT
init_string_table( IStorage
*stg
)
638 static const WCHAR szStringData
[] = {
639 '_','S','t','r','i','n','g','D','a','t','a',0 };
640 static const WCHAR szStringPool
[] = {
641 '_','S','t','r','i','n','g','P','o','o','l',0 };
642 USHORT zero
[2] = { 0, 0 };
647 encname
= encode_streamname(TRUE
, szStringPool
);
649 /* create the StringPool stream... add the zero string to it*/
650 r
= IStorage_CreateStream( stg
, encname
,
651 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
659 r
= IStream_Write(stm
, zero
, sizeof zero
, &count
);
660 IStream_Release( stm
);
662 if( FAILED( r
) || ( count
!= sizeof zero
) )
668 /* create the StringData stream... make it zero length */
669 encname
= encode_streamname(TRUE
, szStringData
);
670 r
= IStorage_CreateStream( stg
, encname
,
671 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
678 IStream_Release( stm
);
683 string_table
*load_string_table( IStorage
*stg
)
685 string_table
*st
= NULL
;
688 UINT r
, datasize
= 0, poolsize
= 0, codepage
;
689 DWORD i
, count
, offset
, len
, n
;
690 static const WCHAR szStringData
[] = {
691 '_','S','t','r','i','n','g','D','a','t','a',0 };
692 static const WCHAR szStringPool
[] = {
693 '_','S','t','r','i','n','g','P','o','o','l',0 };
695 r
= read_stream_data( stg
, szStringPool
, &pool
, &poolsize
);
696 if( r
!= ERROR_SUCCESS
)
698 r
= read_stream_data( stg
, szStringData
, (USHORT
**)&data
, &datasize
);
699 if( r
!= ERROR_SUCCESS
)
704 codepage
= pool
[0] | ( pool
[1] << 16 );
707 st
= msi_init_stringtable( count
, codepage
);
711 for( i
=1; i
<count
; i
++ )
716 * If a string is over 64k, the previous string entry is made null
717 * and its the high word of the length is inserted in the null string's
718 * reference count field.
720 if( pool
[i
*2-2] == 0 )
721 len
+= pool
[i
*2-1] * 0x10000;
723 if( (offset
+ len
) > datasize
)
725 ERR("string table corrupt?\n");
729 /* don't add the high word of a string's length as a string */
730 if ( len
|| !pool
[i
*2+1] )
732 r
= msi_addstring( st
, n
, data
+offset
, len
, pool
[i
*2+1] );
734 ERR("Failed to add string %ld\n", n
);
741 if ( datasize
!= offset
)
742 ERR("string table load failed! (%08x != %08lx)\n", datasize
, offset
);
744 TRACE("Loaded %ld strings\n", count
);
753 static UINT
save_string_table( MSIDATABASE
*db
)
755 UINT i
, count
, datasize
, poolsize
, sz
, used
, r
, codepage
;
756 UINT ret
= ERROR_FUNCTION_FAILED
;
757 static const WCHAR szStringData
[] = {
758 '_','S','t','r','i','n','g','D','a','t','a',0 };
759 static const WCHAR szStringPool
[] = {
760 '_','S','t','r','i','n','g','P','o','o','l',0 };
766 /* construct the new table in memory first */
767 datasize
= msi_string_totalsize( db
->strings
, &count
);
768 poolsize
= (count
+ 1)*2*sizeof(USHORT
);
770 pool
= msi_alloc( poolsize
);
773 WARN("Failed to alloc pool %d bytes\n", poolsize
);
776 data
= msi_alloc( datasize
);
779 WARN("Failed to alloc data %d bytes\n", poolsize
);
784 codepage
= msi_string_get_codepage( db
->strings
);
785 pool
[0]=codepage
&0xffff;
786 pool
[1]=(codepage
>>16);
787 for( i
=1; i
<count
; i
++ )
789 sz
= datasize
- used
;
790 r
= msi_id2stringA( db
->strings
, i
, data
+used
, &sz
);
791 if( r
!= ERROR_SUCCESS
)
793 ERR("failed to fetch string\n");
796 if( sz
&& (sz
< (datasize
- used
) ) )
798 TRACE("adding %u bytes %s\n", sz
, debugstr_a(data
+used
) );
800 pool
[ i
*2 + 1 ] = msi_id_refcount( db
->strings
, i
);
802 if( used
> datasize
)
804 ERR("oops overran %d >= %d\n", used
, datasize
);
809 if( used
!= datasize
)
811 ERR("oops used %d != datasize %d\n", used
, datasize
);
815 /* write the streams */
816 r
= write_stream_data( db
->storage
, szStringData
, data
, datasize
);
817 TRACE("Wrote StringData r=%08x\n", r
);
820 r
= write_stream_data( db
->storage
, szStringPool
, pool
, poolsize
);
821 TRACE("Wrote StringPool r=%08x\n", r
);
834 /* information for default tables */
835 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
836 static const WCHAR szTable
[] = { 'T','a','b','l','e',0 };
837 static const WCHAR szName
[] = { 'N','a','m','e',0 };
838 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
839 static const WCHAR szColumn
[] = { 'C','o','l','u','m','n',0 };
840 static const WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
841 static const WCHAR szType
[] = { 'T','y','p','e',0 };
843 static const MSICOLUMNINFO _Columns_cols
[4] = {
844 { szColumns
, 1, szTable
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 0 },
845 { szColumns
, 2, szNumber
, MSITYPE_VALID
| 2, 2 },
846 { szColumns
, 3, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 4 },
847 { szColumns
, 4, szType
, MSITYPE_VALID
| 2, 6 },
849 static const MSICOLUMNINFO _Tables_cols
[1] = {
850 { szTables
, 1, szName
, MSITYPE_VALID
| MSITYPE_STRING
| 64, 0 },
853 static UINT
get_defaulttablecolumns( LPCWSTR name
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
855 const MSICOLUMNINFO
*p
;
858 TRACE("%s\n", debugstr_w(name
));
860 if (!lstrcmpW( name
, szTables
))
865 else if (!lstrcmpW( name
, szColumns
))
871 return ERROR_FUNCTION_FAILED
;
873 /* duplicate the string data so we can free it in msi_free_colinfo */
876 if (colinfo
&& (i
< *sz
) )
878 memcpy( &colinfo
[i
], &p
[i
], sizeof(MSICOLUMNINFO
) );
879 colinfo
[i
].tablename
= strdupW( p
[i
].tablename
);
880 colinfo
[i
].colname
= strdupW( p
[i
].colname
);
882 if( colinfo
&& (i
>= *sz
) )
886 return ERROR_SUCCESS
;
889 static void msi_free_colinfo( MSICOLUMNINFO
*colinfo
, UINT count
)
893 for( i
=0; i
<count
; i
++ )
895 msi_free( (LPWSTR
) colinfo
[i
].tablename
);
896 msi_free( (LPWSTR
) colinfo
[i
].colname
);
897 msi_free( colinfo
[i
].hash_table
);
901 LPWSTR
MSI_makestring( MSIDATABASE
*db
, UINT stringid
)
906 r
= msi_id2stringW( db
->strings
, stringid
, NULL
, &sz
);
907 if( r
!= ERROR_SUCCESS
)
909 str
= msi_alloc( sz
*sizeof (WCHAR
) );
912 r
= msi_id2stringW( db
->strings
, stringid
, str
, &sz
);
913 if( r
== ERROR_SUCCESS
)
919 static UINT
get_tablecolumns( MSIDATABASE
*db
,
920 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
922 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
923 MSITABLE
*table
= NULL
;
925 /* first check if there is a default table with that name */
926 r
= get_defaulttablecolumns( szTableName
, colinfo
, sz
);
927 if( ( r
== ERROR_SUCCESS
) && *sz
)
930 table
= get_table( db
, szColumns
, _Columns_cols
, 4 );
933 ERR("couldn't load _Columns table\n");
934 return ERROR_FUNCTION_FAILED
;
937 /* convert table and column names to IDs from the string table */
938 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
939 if( r
!= ERROR_SUCCESS
)
941 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
945 TRACE("Table id is %d, row count is %d\n", table_id
, table
->row_count
);
947 count
= table
->row_count
;
948 for( i
=0; i
<count
; i
++ )
950 if( table
->data
[ i
][ 0 ] != table_id
)
954 UINT id
= table
->data
[ i
] [ 2 ];
955 colinfo
[n
].tablename
= MSI_makestring( db
, table_id
);
956 colinfo
[n
].number
= table
->data
[ i
][ 1 ] - (1<<15);
957 colinfo
[n
].colname
= MSI_makestring( db
, id
);
958 colinfo
[n
].type
= table
->data
[ i
] [ 3 ] ^ 0x8000;
959 colinfo
[n
].hash_table
= NULL
;
960 /* this assumes that columns are in order in the table */
962 colinfo
[n
].offset
= colinfo
[n
-1].offset
963 + bytes_per_column( &colinfo
[n
-1] );
965 colinfo
[n
].offset
= 0;
966 TRACE("table %s column %d is [%s] (%d) with type %08x "
967 "offset %d at row %d\n", debugstr_w(szTableName
),
968 colinfo
[n
].number
, debugstr_w(colinfo
[n
].colname
),
969 id
, colinfo
[n
].type
, colinfo
[n
].offset
, i
);
970 if( n
!= (colinfo
[n
].number
-1) )
972 ERR("oops. data in the _Columns table isn't in the right "
973 "order for table %s\n", debugstr_w(szTableName
));
974 return ERROR_FUNCTION_FAILED
;
978 if( colinfo
&& ( n
>= maxcount
) )
983 return ERROR_SUCCESS
;
986 /* try to find the table name in the _Tables table */
987 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPWSTR name
)
989 UINT r
, table_id
= 0, i
, count
;
990 MSITABLE
*table
= NULL
;
992 if( !lstrcmpW( name
, szTables
) )
994 if( !lstrcmpW( name
, szColumns
) )
997 r
= msi_string2idW( db
->strings
, name
, &table_id
);
998 if( r
!= ERROR_SUCCESS
)
1000 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1004 table
= get_table( db
, szTables
, _Tables_cols
, 1 );
1007 TRACE("table %s not available\n", debugstr_w(szTables
));
1011 /* count = table->size/2; */
1012 count
= table
->row_count
;
1013 for( i
=0; i
<count
; i
++ )
1014 if( table
->data
[ i
][ 0 ] == table_id
)
1020 TRACE("Searched %d tables, but %d was not found\n", count
, table_id
);
1025 /* below is the query interface to a table */
1027 typedef struct tagMSITABLEVIEW
1032 MSICOLUMNINFO
*columns
;
1038 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1040 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1041 UINT offset
, num_rows
, n
;
1044 return ERROR_INVALID_PARAMETER
;
1046 if( (col
==0) || (col
>tv
->num_cols
) )
1047 return ERROR_INVALID_PARAMETER
;
1049 /* how many rows are there ? */
1050 num_rows
= tv
->table
->row_count
;
1051 if( row
>= num_rows
)
1052 return ERROR_NO_MORE_ITEMS
;
1054 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1056 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1057 ERR("%p %p\n", tv
, tv
->columns
);
1058 return ERROR_FUNCTION_FAILED
;
1061 offset
= row
+ (tv
->columns
[col
-1].offset
/2) * num_rows
;
1062 n
= bytes_per_column( &tv
->columns
[col
-1] );
1066 offset
= tv
->columns
[col
-1].offset
/2;
1067 *val
= tv
->table
->data
[row
][offset
] +
1068 (tv
->table
->data
[row
][offset
+ 1] << 16);
1071 offset
= tv
->columns
[col
-1].offset
/2;
1072 *val
= tv
->table
->data
[row
][offset
];
1075 ERR("oops! what is %d bytes per column?\n", n
);
1076 return ERROR_FUNCTION_FAILED
;
1079 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1081 return ERROR_SUCCESS
;
1085 * We need a special case for streams, as we need to reference column with
1086 * the name of the stream in the same table, and the table name
1087 * which may not be available at higher levels of the query
1089 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1091 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1092 UINT ival
= 0, refcol
= 0, r
;
1096 static const WCHAR szDot
[] = { '.', 0 };
1098 if( !view
->ops
->fetch_int
)
1099 return ERROR_INVALID_PARAMETER
;
1102 * The column marked with the type stream data seems to have a single number
1103 * which references the column containing the name of the stream data
1105 * Fetch the column to reference first.
1107 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1108 if( r
!= ERROR_SUCCESS
)
1111 /* now get the column with the name of the stream */
1112 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1113 if( r
!= ERROR_SUCCESS
)
1116 /* lookup the string value from the string table */
1117 sval
= MSI_makestring( tv
->db
, refcol
);
1119 return ERROR_INVALID_PARAMETER
;
1121 len
= lstrlenW( tv
->name
) + 2 + lstrlenW( sval
);
1122 full_name
= msi_alloc( len
*sizeof(WCHAR
) );
1123 lstrcpyW( full_name
, tv
->name
);
1124 lstrcatW( full_name
, szDot
);
1125 lstrcatW( full_name
, sval
);
1127 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1129 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1130 msi_free( full_name
);
1136 static UINT
TABLE_set_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT val
)
1138 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1142 return ERROR_INVALID_PARAMETER
;
1144 if( (col
==0) || (col
>tv
->num_cols
) )
1145 return ERROR_INVALID_PARAMETER
;
1147 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1149 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1150 ERR("%p %p\n", tv
, tv
->columns
);
1151 return ERROR_FUNCTION_FAILED
;
1154 n
= bytes_per_column( &tv
->columns
[col
-1] );
1158 offset
= tv
->columns
[col
-1].offset
/2;
1159 tv
->table
->data
[row
][offset
] = val
& 0xffff;
1160 tv
->table
->data
[row
][offset
+ 1] = (val
>>16)&0xffff;
1163 offset
= tv
->columns
[col
-1].offset
/2;
1164 tv
->table
->data
[row
][offset
] = val
;
1167 ERR("oops! what is %d bytes per column?\n", n
);
1168 return ERROR_FUNCTION_FAILED
;
1170 return ERROR_SUCCESS
;
1173 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
)
1175 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1179 TRACE("%p\n", view
);
1182 return ERROR_INVALID_PARAMETER
;
1184 row
= msi_alloc_zero( tv
->row_size
);
1186 return ERROR_NOT_ENOUGH_MEMORY
;
1188 sz
= (tv
->table
->row_count
+ 1) * sizeof (UINT
*);
1189 if( tv
->table
->data
)
1190 p
= msi_realloc( tv
->table
->data
, sz
);
1192 p
= msi_alloc( sz
);
1196 return ERROR_NOT_ENOUGH_MEMORY
;
1199 tv
->table
->data
= p
;
1200 tv
->table
->data
[tv
->table
->row_count
] = row
;
1201 *num
= tv
->table
->row_count
;
1202 tv
->table
->row_count
++;
1204 return ERROR_SUCCESS
;
1207 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1209 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1211 TRACE("%p %p\n", tv
, record
);
1213 TRACE("There are %d columns\n", tv
->num_cols
);
1214 tv
->table
= get_table( tv
->db
, tv
->name
, tv
->columns
, tv
->num_cols
);
1216 return ERROR_FUNCTION_FAILED
;
1218 return ERROR_SUCCESS
;
1221 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1223 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1225 TRACE("%p\n", view
);
1228 return ERROR_FUNCTION_FAILED
;
1232 return ERROR_SUCCESS
;
1235 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1237 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1239 TRACE("%p %p %p\n", view
, rows
, cols
);
1242 *cols
= tv
->num_cols
;
1246 return ERROR_INVALID_PARAMETER
;
1247 *rows
= tv
->table
->row_count
;
1250 return ERROR_SUCCESS
;
1253 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1254 UINT n
, LPWSTR
*name
, UINT
*type
)
1256 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1258 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1260 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1261 return ERROR_INVALID_PARAMETER
;
1265 *name
= strdupW( tv
->columns
[n
-1].colname
);
1267 return ERROR_FUNCTION_FAILED
;
1270 *type
= tv
->columns
[n
-1].type
;
1272 return ERROR_SUCCESS
;
1275 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
);
1277 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1281 r
= msi_table_find_row( tv
, rec
, &row
);
1282 if (r
!= ERROR_SUCCESS
)
1283 return ERROR_SUCCESS
;
1284 return ERROR_INVALID_DATA
;
1287 static UINT
msi_table_modify_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
,
1288 UINT row
, UINT mask
)
1290 UINT i
, val
, r
= ERROR_SUCCESS
;
1292 TRACE("%p %p %u %08x\n", tv
, rec
, row
, mask
);
1294 for( i
= 0; i
< tv
->num_cols
; i
++ )
1296 /* set keys or values specified in the mask */
1297 if( (~tv
->columns
[i
].type
& MSITYPE_KEY
) && (~mask
& (1<<i
)) )
1300 if( (tv
->columns
[i
].type
& MSITYPE_STRING
) &&
1301 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1303 const WCHAR
*str
= MSI_RecordGetString( rec
, i
+1 );
1304 val
= msi_addstringW( tv
->db
->strings
, 0, str
, -1, 1 );
1308 val
= MSI_RecordGetInteger( rec
, i
+1 );
1309 if ( 2 == bytes_per_column( &tv
->columns
[i
] ) )
1312 r
= TABLE_set_int( &tv
->view
, row
, i
+1, val
);
1320 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1322 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1325 TRACE("%p %p\n", tv
, rec
);
1327 r
= table_create_new_row( view
, &row
);
1328 TRACE("insert_row returned %08x\n", r
);
1329 if( r
!= ERROR_SUCCESS
)
1332 return msi_table_modify_row( tv
, rec
, row
, ~0 );
1335 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1338 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1341 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1345 r
= TABLE_execute( view
, NULL
);
1350 switch (eModifyMode
)
1352 case MSIMODIFY_VALIDATE_NEW
:
1353 r
= table_validate_new( tv
, rec
);
1356 case MSIMODIFY_INSERT_TEMPORARY
:
1357 r
= table_validate_new( tv
, rec
);
1358 if (r
!= ERROR_SUCCESS
)
1360 r
= TABLE_insert_row( view
, rec
);
1363 case MSIMODIFY_REFRESH
:
1364 case MSIMODIFY_INSERT
:
1365 case MSIMODIFY_UPDATE
:
1366 case MSIMODIFY_ASSIGN
:
1367 case MSIMODIFY_REPLACE
:
1368 case MSIMODIFY_MERGE
:
1369 case MSIMODIFY_DELETE
:
1370 case MSIMODIFY_VALIDATE
:
1371 case MSIMODIFY_VALIDATE_FIELD
:
1372 case MSIMODIFY_VALIDATE_DELETE
:
1373 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1374 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1378 r
= ERROR_INVALID_DATA
;
1384 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1386 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1388 TRACE("%p\n", view
);
1394 msi_free_colinfo( tv
->columns
, tv
->num_cols
);
1395 msi_free( tv
->columns
);
1401 return ERROR_SUCCESS
;
1404 static UINT
TABLE_find_matching_rows( struct tagMSIVIEW
*view
, UINT col
,
1405 UINT val
, UINT
*row
, MSIITERHANDLE
*handle
)
1407 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1408 const MSICOLUMNHASHENTRY
*entry
;
1410 TRACE("%p, %d, %u, %p\n", view
, col
, val
, *handle
);
1413 return ERROR_INVALID_PARAMETER
;
1415 if( (col
==0) || (col
> tv
->num_cols
) )
1416 return ERROR_INVALID_PARAMETER
;
1418 if( !tv
->columns
[col
-1].hash_table
)
1421 UINT num_rows
= tv
->table
->row_count
;
1422 MSICOLUMNHASHENTRY
**hash_table
;
1423 MSICOLUMNHASHENTRY
*new_entry
;
1425 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1427 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1428 ERR("%p %p\n", tv
, tv
->columns
);
1429 return ERROR_FUNCTION_FAILED
;
1432 /* allocate contiguous memory for the table and its entries so we
1433 * don't have to do an expensive cleanup */
1434 hash_table
= msi_alloc(MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*) +
1435 num_rows
* sizeof(MSICOLUMNHASHENTRY
));
1437 return ERROR_OUTOFMEMORY
;
1439 memset(hash_table
, 0, MSITABLE_HASH_TABLE_SIZE
* sizeof(MSICOLUMNHASHENTRY
*));
1440 tv
->columns
[col
-1].hash_table
= hash_table
;
1442 new_entry
= (MSICOLUMNHASHENTRY
*)(hash_table
+ MSITABLE_HASH_TABLE_SIZE
);
1444 for (i
= 0; i
< num_rows
; i
++, new_entry
++)
1447 UINT offset
= i
+ (tv
->columns
[col
-1].offset
/2) * num_rows
;
1448 n
= bytes_per_column( &tv
->columns
[col
-1] );
1452 offset
= tv
->columns
[col
-1].offset
/2;
1453 row_value
= tv
->table
->data
[i
][offset
] +
1454 (tv
->table
->data
[i
][offset
+ 1] << 16);
1457 offset
= tv
->columns
[col
-1].offset
/2;
1458 row_value
= tv
->table
->data
[i
][offset
];
1461 ERR("oops! what is %d bytes per column?\n", n
);
1465 new_entry
->next
= NULL
;
1466 new_entry
->value
= row_value
;
1468 if (hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
])
1470 MSICOLUMNHASHENTRY
*prev_entry
= hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
];
1471 while (prev_entry
->next
)
1472 prev_entry
= prev_entry
->next
;
1473 prev_entry
->next
= new_entry
;
1476 hash_table
[row_value
% MSITABLE_HASH_TABLE_SIZE
] = new_entry
;
1481 entry
= tv
->columns
[col
-1].hash_table
[val
% MSITABLE_HASH_TABLE_SIZE
];
1483 entry
= ((const MSICOLUMNHASHENTRY
*)*handle
)->next
;
1485 while (entry
&& entry
->value
!= val
)
1486 entry
= entry
->next
;
1488 *handle
= (MSIITERHANDLE
)entry
;
1490 return ERROR_NO_MORE_ITEMS
;
1493 return ERROR_SUCCESS
;
1497 MSIVIEWOPS table_ops
=
1505 TABLE_get_dimensions
,
1506 TABLE_get_column_info
,
1509 TABLE_find_matching_rows
1512 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
1515 UINT r
, sz
, column_count
;
1516 MSICOLUMNINFO
*columns
;
1518 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
1520 /* get the number of columns in this table */
1522 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
1523 if( r
!= ERROR_SUCCESS
)
1526 /* if there's no columns, there's no table */
1527 if( column_count
== 0 )
1528 return ERROR_INVALID_PARAMETER
;
1530 TRACE("Table found\n");
1532 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
1533 tv
= msi_alloc_zero( sz
);
1535 return ERROR_FUNCTION_FAILED
;
1537 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
));
1541 return ERROR_FUNCTION_FAILED
;
1544 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
1545 if( r
!= ERROR_SUCCESS
)
1547 msi_free( columns
);
1549 return ERROR_FUNCTION_FAILED
;
1552 TRACE("Table has %d columns\n", column_count
);
1554 /* fill the structure */
1555 tv
->view
.ops
= &table_ops
;
1557 tv
->columns
= columns
;
1558 tv
->num_cols
= column_count
;
1560 tv
->row_size
= msi_table_get_row_size( columns
, column_count
);
1562 TRACE("%s one row is %d bytes\n", debugstr_w(name
), tv
->row_size
);
1564 *view
= (MSIVIEW
*) tv
;
1565 lstrcpyW( tv
->name
, name
);
1567 return ERROR_SUCCESS
;
1570 UINT
MSI_CommitTables( MSIDATABASE
*db
)
1573 MSITABLE
*table
= NULL
;
1577 r
= save_string_table( db
);
1578 if( r
!= ERROR_SUCCESS
)
1580 WARN("failed to save string table r=%08x\n",r
);
1584 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
1586 r
= save_table( db
, table
);
1587 if( r
!= ERROR_SUCCESS
)
1589 WARN("failed to save table %s (r=%08x)\n",
1590 debugstr_w(table
->name
), r
);
1595 /* force everything to reload next time */
1596 free_cached_tables( db
);
1598 return ERROR_SUCCESS
;
1601 static MSIRECORD
*msi_get_transform_record( MSITABLEVIEW
*tv
, string_table
*st
, USHORT
*rawdata
)
1603 UINT i
, val
, ofs
= 0;
1604 USHORT mask
= *rawdata
++;
1605 MSICOLUMNINFO
*columns
= tv
->columns
;
1608 rec
= MSI_CreateRecord( tv
->num_cols
);
1613 for( i
=0; i
<tv
->num_cols
; i
++ )
1615 UINT n
= bytes_per_column( &columns
[i
] );
1617 if ( (mask
&1) && (i
>=(mask
>>8)) )
1619 /* all keys must be present */
1620 if ( (~mask
&1) && (~columns
[i
].type
& MSITYPE_KEY
) && ((1<<i
) & ~mask
) )
1627 if( (columns
[i
].type
& MSITYPE_STRING
) &&
1628 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1630 LPCWSTR sval
= msi_string_lookup_id( st
, val
);
1631 MSI_RecordSetStringW( rec
, i
+1, sval
);
1632 TRACE("[%s]", debugstr_w(sval
));
1637 MSI_RecordSetInteger( rec
, i
+1, val
);
1638 TRACE("[0x%04x]", val
);
1642 val
= rawdata
[ofs
] + (rawdata
[ofs
+ 1]<<16);
1643 /* val ^= 0x80000000; */
1644 MSI_RecordSetInteger( rec
, i
+1, val
);
1645 TRACE("[0x%08x]", val
);
1648 ERR("oops - unknown column width %d\n", n
);
1657 static void dump_record( MSIRECORD
*rec
)
1661 n
= MSI_RecordGetFieldCount( rec
);
1662 for( i
=1; i
<=n
; i
++ )
1664 LPCWSTR sval
= MSI_RecordGetString( rec
, i
);
1666 if( MSI_RecordIsNull( rec
, i
) )
1667 TRACE("row -> []\n");
1668 else if( (sval
= MSI_RecordGetString( rec
, i
)) )
1669 TRACE("row -> [%s]\n", debugstr_w(sval
));
1671 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec
, i
) );
1675 static void dump_table( string_table
*st
, USHORT
*rawdata
, UINT rawsize
)
1680 for( i
=0; i
<(rawsize
/2); i
++ )
1682 sval
= msi_string_lookup_id( st
, rawdata
[i
] );
1683 if( !sval
) sval
= (WCHAR
[]) {0};
1684 MESSAGE(" %04x %s\n", rawdata
[i
], debugstr_w(sval
) );
1688 static UINT
* msi_record_to_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1693 data
= msi_alloc( tv
->num_cols
*sizeof (UINT
) );
1694 for( i
=0; i
<tv
->num_cols
; i
++ )
1698 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
1701 /* turn the transform column value into a row value */
1702 if ( ( tv
->columns
[i
].type
& MSITYPE_STRING
) &&
1703 ! MSITYPE_IS_BINARY(tv
->columns
[i
].type
) )
1705 str
= MSI_RecordGetString( rec
, i
+1 );
1706 r
= msi_string2idW( tv
->db
->strings
, str
, &data
[i
] );
1708 /* if there's no matching string in the string table,
1709 these keys can't match any record, so fail now. */
1710 if( ERROR_SUCCESS
!= r
)
1717 data
[i
] = MSI_RecordGetInteger( rec
, i
+1 );
1722 static UINT
msi_row_matches( MSITABLEVIEW
*tv
, UINT row
, UINT
*data
)
1724 UINT i
, r
, x
, ret
= ERROR_FUNCTION_FAILED
;
1726 for( i
=0; i
<tv
->num_cols
; i
++ )
1728 if ( ~tv
->columns
[i
].type
& MSITYPE_KEY
)
1731 /* turn the transform column value into a row value */
1732 r
= TABLE_fetch_int( &tv
->view
, row
, i
+1, &x
);
1733 if ( r
!= ERROR_SUCCESS
)
1735 ERR("TABLE_fetch_int shouldn't fail here\n");
1739 /* if this key matches, move to the next column */
1742 ret
= ERROR_FUNCTION_FAILED
;
1746 ret
= ERROR_SUCCESS
;
1752 static UINT
msi_table_find_row( MSITABLEVIEW
*tv
, MSIRECORD
*rec
, UINT
*row
)
1754 UINT i
, r
= ERROR_FUNCTION_FAILED
, *data
;
1756 data
= msi_record_to_row( tv
, rec
);
1759 for( i
=0; i
<tv
->table
->row_count
; i
++ )
1761 r
= msi_row_matches( tv
, i
, data
);
1762 if( r
== ERROR_SUCCESS
)
1772 static UINT
msi_delete_row( MSITABLEVIEW
*tv
, UINT row
)
1775 for( i
=1; i
<=tv
->num_cols
; i
++ )
1776 tv
->view
.ops
->set_int( &tv
->view
, row
, i
, 0 );
1777 return ERROR_SUCCESS
;
1780 static UINT
msi_table_load_transform( MSIDATABASE
*db
, IStorage
*stg
,
1781 string_table
*st
, LPCWSTR name
)
1784 USHORT
*rawdata
= NULL
;
1785 MSITABLEVIEW
*tv
= NULL
;
1786 UINT r
, n
, sz
, i
, mask
;
1787 MSIRECORD
*rec
= NULL
;
1789 TRACE("%p %p %p %s\n", db
, stg
, st
, debugstr_w(name
) );
1791 /* create a table view */
1792 r
= TABLE_CreateView( db
, name
, (MSIVIEW
**) &tv
);
1793 if( r
!= ERROR_SUCCESS
)
1796 r
= tv
->view
.ops
->execute( &tv
->view
, NULL
);
1797 if( r
!= ERROR_SUCCESS
)
1800 /* read the transform data */
1801 r
= ERROR_FUNCTION_FAILED
;
1802 read_stream_data( stg
, name
, &rawdata
, &rawsize
);
1803 if( !rawdata
|| (rawsize
< 2) )
1805 ERR("odd sized transform for table %s\n", debugstr_w(name
));
1809 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1810 debugstr_w(name
), tv
->num_cols
, tv
->row_size
, rawsize
);
1812 /* interpret the data */
1814 for( n
=0; n
< (rawsize
/2); )
1821 * if the low bit is set, columns are continuous and
1822 * the number of columns is specified in the high byte
1824 sz
= 2 + tv
->row_size
;
1829 * If the low bit is not set, rowdata[n] is a bitmask.
1830 * Excepting for key fields, which are always present,
1831 * each bit indicates that a field is present in the transform record.
1833 * rawdata[n] == 0 is a special case ... only the keys will be present
1834 * and it means that this row should be deleted.
1837 for( i
=0; i
<tv
->num_cols
; i
++ )
1839 if( (tv
->columns
[i
].type
& MSITYPE_KEY
) || ((1<<i
)&mask
))
1840 sz
+= bytes_per_column( &tv
->columns
[i
] );
1844 /* check we didn't run of the end of the table */
1845 if ( (n
+sz
) > rawsize
)
1848 dump_table( st
, rawdata
, rawsize
);
1852 rec
= msi_get_transform_record( tv
, st
, &rawdata
[n
] );
1857 r
= msi_table_find_row( tv
, rec
, &row
);
1861 TRACE("insert [%d]: ", row
);
1862 TABLE_insert_row( &tv
->view
, rec
);
1864 else if( mask
& 0xff )
1866 TRACE("modify [%d]: ", row
);
1867 msi_table_modify_row( tv
, rec
, row
, mask
);
1871 TRACE("delete [%d]: ", row
);
1872 msi_delete_row( tv
, row
);
1874 if( TRACE_ON(msidb
) ) dump_record( rec
);
1875 msiobj_release( &rec
->hdr
);
1883 /* no need to free the table, it's associated with the database */
1884 msi_free( rawdata
);
1886 tv
->view
.ops
->delete( &tv
->view
);
1888 return ERROR_SUCCESS
;
1892 * msi_table_apply_transform
1894 * Enumerate the table transforms in a transform storage and apply each one.
1896 UINT
msi_table_apply_transform( MSIDATABASE
*db
, IStorage
*stg
)
1898 IEnumSTATSTG
*stgenum
= NULL
;
1903 string_table
*strings
;
1904 UINT ret
= ERROR_FUNCTION_FAILED
;
1906 TRACE("%p %p\n", db
, stg
);
1908 strings
= load_string_table( stg
);
1912 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
1917 ret
= ERROR_SUCCESS
;
1919 while( r
== ERROR_SUCCESS
)
1922 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
1923 if( FAILED( r
) || !count
)
1925 decode_streamname( stat
.pwcsName
, name
);
1926 if( ( name
[0] == 0x4840 ) && ( name
[1] != '_' ) )
1927 ret
= msi_table_load_transform( db
, stg
, strings
, name
+1 );
1929 TRACE("transform contains stream %s\n", debugstr_w(name
));
1933 if ( ret
== ERROR_SUCCESS
)
1937 t
= msi_alloc( sizeof *t
);
1939 IStorage_AddRef( stg
);
1940 list_add_tail( &db
->transforms
, &t
->entry
);
1945 IEnumSTATSTG_Release( stgenum
);
1947 msi_destroy_stringtable( strings
);
1952 void msi_free_transforms( MSIDATABASE
*db
)
1954 while( !list_empty( &db
->transforms
) )
1956 MSITRANSFORM
*t
= LIST_ENTRY( list_head( &db
->transforms
),
1957 MSITRANSFORM
, entry
);
1958 list_remove( &t
->entry
);
1959 IStorage_Release( t
->stg
);