2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
30 #include "wine/debug.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
42 typedef struct tagMSICOLUMNINFO
56 struct tagMSITABLE
*next
;
57 struct tagMSITABLE
*prev
;
61 #define MAX_STREAM_NAME 0x1f
63 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
64 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
65 static UINT
get_tablecolumns( MSIDATABASE
*db
,
66 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
68 static inline UINT
bytes_per_column( MSICOLUMNINFO
*col
)
70 if( col
->type
& MSITYPE_STRING
)
72 if( (col
->type
& 0xff) > 4 )
73 ERR("Invalid column size!\n");
74 return col
->type
& 0xff;
77 static int utf2mime(int x
)
79 if( (x
>='0') && (x
<='9') )
81 if( (x
>='A') && (x
<='Z') )
83 if( (x
>='a') && (x
<='z') )
92 static LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
94 DWORD count
= MAX_STREAM_NAME
;
99 count
= lstrlenW( in
)+2;
100 out
= HeapAlloc( GetProcessHeap(), 0, count
*sizeof(WCHAR
) );
116 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
118 ch
= utf2mime(ch
) + 0x4800;
120 if( next
&& (next
<0x80) )
122 next
= utf2mime(next
);
133 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
134 HeapFree( GetProcessHeap(), 0, out
);
138 static int mime2utf(int x
)
145 return x
- 10 - 26 + 'a';
146 if( x
== (10+26+26) )
151 static BOOL
decode_streamname(LPWSTR in
, LPWSTR out
)
156 while ( (ch
= *in
++) )
158 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
161 ch
= mime2utf(ch
-0x4800);
165 *out
++ = mime2utf(ch
&0x3f);
167 ch
= mime2utf((ch
>>6)&0x3f);
177 void enum_stream_names( IStorage
*stg
)
179 IEnumSTATSTG
*stgenum
= NULL
;
185 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
193 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
194 if( FAILED( r
) || !count
)
196 decode_streamname( stat
.pwcsName
, name
);
197 TRACE("stream %2ld -> %s %s\n", n
,
198 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
202 IEnumSTATSTG_Release( stgenum
);
205 static UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
,
206 USHORT
**pdata
, UINT
*psz
)
209 UINT ret
= ERROR_FUNCTION_FAILED
;
216 encname
= encode_streamname(TRUE
, stname
);
218 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
220 r
= IStorage_OpenStream(stg
, encname
, NULL
,
221 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
222 HeapFree( GetProcessHeap(), 0, encname
);
225 WARN("open stream failed r = %08lx - empty table?\n",r
);
229 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
232 WARN("open stream failed r = %08lx!\n",r
);
236 if( stat
.cbSize
.QuadPart
>> 32 )
242 sz
= stat
.cbSize
.QuadPart
;
243 data
= HeapAlloc( GetProcessHeap(), 0, sz
);
246 WARN("couldn't allocate memory r=%08lx!\n",r
);
247 ret
= ERROR_NOT_ENOUGH_MEMORY
;
251 r
= IStream_Read(stm
, data
, sz
, &count
);
252 if( FAILED( r
) || ( count
!= sz
) )
254 HeapFree( GetProcessHeap(), 0, data
);
255 WARN("read stream failed r = %08lx!\n",r
);
264 IStream_Release( stm
);
269 UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
274 encname
= encode_streamname(FALSE
, stname
);
276 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
278 r
= IStorage_OpenStream(db
->storage
, encname
, NULL
,
279 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
280 HeapFree( GetProcessHeap(), 0, encname
);
283 WARN("open stream failed r = %08lx - empty table?\n",r
);
284 return ERROR_FUNCTION_FAILED
;
287 return ERROR_SUCCESS
;
290 UINT
read_raw_stream_data( MSIDATABASE
*db
, LPCWSTR stname
,
291 USHORT
**pdata
, UINT
*psz
)
294 UINT ret
= ERROR_FUNCTION_FAILED
;
300 r
= db_get_raw_stream( db
, stname
, &stm
);
301 if( r
!= ERROR_SUCCESS
)
303 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
306 WARN("open stream failed r = %08lx!\n",r
);
310 if( stat
.cbSize
.QuadPart
>> 32 )
316 sz
= stat
.cbSize
.QuadPart
;
317 data
= HeapAlloc( GetProcessHeap(), 0, sz
);
320 WARN("couldn't allocate memory r=%08lx!\n",r
);
321 ret
= ERROR_NOT_ENOUGH_MEMORY
;
325 r
= IStream_Read(stm
, data
, sz
, &count
);
326 if( FAILED( r
) || ( count
!= sz
) )
328 HeapFree( GetProcessHeap(), 0, data
);
329 WARN("read stream failed r = %08lx!\n",r
);
338 IStream_Release( stm
);
343 static UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
344 LPVOID data
, UINT sz
)
347 UINT ret
= ERROR_FUNCTION_FAILED
;
354 encname
= encode_streamname(TRUE
, stname
);
355 r
= IStorage_OpenStream( stg
, encname
, NULL
,
356 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
359 r
= IStorage_CreateStream( stg
, encname
,
360 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
362 HeapFree( GetProcessHeap(), 0, encname
);
365 WARN("open stream failed r = %08lx\n",r
);
370 r
= IStream_SetSize( stm
, size
);
373 WARN("Failed to SetSize\n");
378 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
381 WARN("Failed to Seek\n");
385 r
= IStream_Write(stm
, data
, sz
, &count
);
386 if( FAILED( r
) || ( count
!= sz
) )
388 WARN("Failed to Write\n");
395 IStream_Release( stm
);
400 static UINT
read_table_from_storage( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**ptable
)
403 USHORT
*rawdata
= NULL
;
404 UINT rawsize
= 0, r
, i
, j
, row_size
= 0, num_cols
= 0;
405 MSICOLUMNINFO
*cols
, *last_col
;
407 TRACE("%s\n",debugstr_w(name
));
409 /* nonexistent tables should be interpreted as empty tables */
410 t
= HeapAlloc( GetProcessHeap(), 0,
411 sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
413 return ERROR_NOT_ENOUGH_MEMORY
;
415 r
= table_get_column_info( db
, name
, &cols
, &num_cols
);
416 if( r
!= ERROR_SUCCESS
)
418 HeapFree( GetProcessHeap(), 0, t
);
421 last_col
= &cols
[num_cols
-1];
422 row_size
= last_col
->offset
+ bytes_per_column( last_col
);
426 lstrcpyW( t
->name
, name
);
430 /* if we can't read the table, just assume that it's empty */
431 read_stream_data( db
->storage
, name
, &rawdata
, &rawsize
);
433 return ERROR_SUCCESS
;
435 TRACE("Read %d bytes\n", rawsize
);
437 if( rawsize
% row_size
)
439 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
440 return ERROR_FUNCTION_FAILED
;
443 t
->row_count
= rawsize
/ row_size
;
444 t
->data
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
445 t
->row_count
* sizeof (USHORT
*) );
447 return ERROR_NOT_ENOUGH_MEMORY
; /* FIXME: memory leak */
449 /* transpose all the data */
450 TRACE("Transposing data from %d columns\n", t
->row_count
);
451 for( i
=0; i
<t
->row_count
; i
++ )
453 t
->data
[i
] = HeapAlloc( GetProcessHeap(), 0, row_size
);
455 return ERROR_NOT_ENOUGH_MEMORY
; /* FIXME: memory leak */
456 for( j
=0; j
<num_cols
; j
++ )
458 UINT ofs
= cols
[j
].offset
/2;
459 UINT n
= bytes_per_column( &cols
[j
] );
464 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
];
467 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
*2 ];
468 t
->data
[i
][ofs
+1] = rawdata
[ofs
*t
->row_count
+ i
*2 + 1];
471 ERR("oops - unknown column width %d\n", n
);
472 return ERROR_FUNCTION_FAILED
;
477 HeapFree( GetProcessHeap(), 0, cols
);
478 HeapFree( GetProcessHeap(), 0, rawdata
);
480 return ERROR_SUCCESS
;
483 /* add this table to the list of cached tables in the database */
484 void add_table(MSIDATABASE
*db
, MSITABLE
*table
)
486 table
->next
= db
->first_table
;
488 if( db
->first_table
)
489 db
->first_table
->prev
= table
;
491 db
->last_table
= table
;
492 db
->first_table
= table
;
495 /* remove from the list of cached tables */
496 void remove_table( MSIDATABASE
*db
, MSITABLE
*table
)
499 table
->next
->prev
= table
->prev
;
501 db
->last_table
= table
->prev
;
503 table
->prev
->next
= table
->next
;
505 db
->first_table
= table
->next
;
510 static void release_table( MSIDATABASE
*db
, MSITABLE
*table
)
512 if( !table
->ref_count
)
513 ERR("Trying to destroy table with refcount 0\n");
515 if( !table
->ref_count
)
517 remove_table( db
, table
);
518 HeapFree( GetProcessHeap(), 0, table
->data
);
519 HeapFree( GetProcessHeap(), 0, table
);
520 TRACE("Destroyed table %s\n", debugstr_w(table
->name
));
524 void free_cached_tables( MSIDATABASE
*db
)
526 while( db
->first_table
)
528 MSITABLE
*t
= db
->first_table
;
530 if ( --t
->ref_count
)
531 ERR("table ref count not zero for %s\n", debugstr_w(t
->name
));
532 remove_table( db
, t
);
533 HeapFree( GetProcessHeap(), 0, t
->data
);
534 HeapFree( GetProcessHeap(), 0, t
);
538 UINT
find_cached_table(MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**ptable
)
542 for( t
= db
->first_table
; t
; t
=t
->next
)
544 if( !lstrcmpW( name
, t
->name
) )
547 return ERROR_SUCCESS
;
551 return ERROR_FUNCTION_FAILED
;
554 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
556 UINT r
, column_count
;
557 MSICOLUMNINFO
*columns
;
559 /* get the number of columns in this table */
561 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
562 if( r
!= ERROR_SUCCESS
)
565 /* if there's no columns, there's no table */
566 if( column_count
== 0 )
567 return ERROR_INVALID_PARAMETER
;
569 TRACE("Table %s found\n", debugstr_w(name
) );
571 columns
= HeapAlloc( GetProcessHeap(), 0, column_count
*sizeof (MSICOLUMNINFO
));
573 return ERROR_FUNCTION_FAILED
;
575 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
576 if( r
!= ERROR_SUCCESS
)
578 HeapFree( GetProcessHeap(), 0, columns
);
579 return ERROR_FUNCTION_FAILED
;
583 *pcount
= column_count
;
588 UINT
get_table(MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**ptable
)
594 /* first, see if the table is cached */
595 r
= find_cached_table( db
, name
, ptable
);
596 if( r
== ERROR_SUCCESS
)
598 (*ptable
)->ref_count
++;
602 r
= read_table_from_storage( db
, name
, ptable
);
603 if( r
!= ERROR_SUCCESS
)
606 /* add the table to the list */
607 add_table( db
, *ptable
);
608 (*ptable
)->ref_count
++;
610 return ERROR_SUCCESS
;
613 static UINT
save_table( MSIDATABASE
*db
, MSITABLE
*t
)
615 USHORT
*rawdata
= NULL
, *p
;
616 UINT rawsize
, r
, i
, j
, row_size
, num_cols
= 0;
617 MSICOLUMNINFO
*cols
, *last_col
;
619 TRACE("Saving %s\n", debugstr_w( t
->name
) );
621 r
= table_get_column_info( db
, t
->name
, &cols
, &num_cols
);
622 if( r
!= ERROR_SUCCESS
)
625 last_col
= &cols
[num_cols
-1];
626 row_size
= last_col
->offset
+ bytes_per_column( last_col
);
628 rawsize
= t
->row_count
* row_size
;
629 rawdata
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, rawsize
);
631 return ERROR_NOT_ENOUGH_MEMORY
;
634 for( i
=0; i
<num_cols
; i
++ )
636 for( j
=0; j
<t
->row_count
; j
++ )
638 UINT offset
= cols
[i
].offset
;
640 *p
++ = t
->data
[j
][offset
/2];
641 if( 4 == bytes_per_column( &cols
[i
] ) )
642 *p
++ = t
->data
[j
][offset
/2+1];
646 TRACE("writing %d bytes\n", rawsize
);
647 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
);
649 HeapFree( GetProcessHeap(), 0, rawdata
);
654 HRESULT
init_string_table( IStorage
*stg
)
657 static const WCHAR szStringData
[] = {
658 '_','S','t','r','i','n','g','D','a','t','a',0 };
659 static const WCHAR szStringPool
[] = {
660 '_','S','t','r','i','n','g','P','o','o','l',0 };
661 USHORT zero
[2] = { 0, 0 };
666 encname
= encode_streamname(TRUE
, szStringPool
);
668 /* create the StringPool stream... add the zero string to it*/
669 r
= IStorage_CreateStream( stg
, encname
,
670 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
671 HeapFree( GetProcessHeap(), 0, encname
);
678 r
= IStream_Write(stm
, zero
, sizeof zero
, &count
);
679 IStream_Release( stm
);
681 if( FAILED( r
) || ( count
!= sizeof zero
) )
687 /* create the StringData stream... make it zero length */
688 encname
= encode_streamname(TRUE
, szStringData
);
689 r
= IStorage_CreateStream( stg
, encname
,
690 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
691 HeapFree( GetProcessHeap(), 0, encname
);
697 IStream_Release( stm
);
702 UINT
load_string_table( MSIDATABASE
*db
)
706 UINT r
, ret
= ERROR_FUNCTION_FAILED
, datasize
= 0, poolsize
= 0, codepage
;
707 DWORD i
, count
, offset
, len
, n
;
708 static const WCHAR szStringData
[] = {
709 '_','S','t','r','i','n','g','D','a','t','a',0 };
710 static const WCHAR szStringPool
[] = {
711 '_','S','t','r','i','n','g','P','o','o','l',0 };
715 msi_destroy_stringtable( db
->strings
);
719 r
= read_stream_data( db
->storage
, szStringPool
, &pool
, &poolsize
);
720 if( r
!= ERROR_SUCCESS
)
722 r
= read_stream_data( db
->storage
, szStringData
, (USHORT
**)&data
, &datasize
);
723 if( r
!= ERROR_SUCCESS
)
728 codepage
= pool
[0] | ( pool
[1] << 16 );
731 db
->strings
= msi_init_stringtable( count
, codepage
);
735 for( i
=1; i
<count
; i
++ )
740 * If a string is over 64k, the previous string entry is made null
741 * and its the high word of the length is inserted in the null string's
742 * reference count field.
744 if( pool
[i
*2-2] == 0 )
745 len
+= pool
[i
*2-1] * 0x10000;
747 if( (offset
+ len
) > datasize
)
749 ERR("string table corrupt?\n");
753 /* don't add the high word of a string's length as a string */
754 if ( len
|| !pool
[i
*2+1] )
756 r
= msi_addstring( db
->strings
, n
, data
+offset
, len
, pool
[i
*2+1] );
758 ERR("Failed to add string %ld\n", n
);
765 if ( datasize
!= offset
)
766 ERR("string table load failed! (%08x != %08lx)\n", datasize
, offset
);
768 TRACE("Loaded %ld strings\n", count
);
773 HeapFree( GetProcessHeap(), 0, pool
);
774 HeapFree( GetProcessHeap(), 0, data
);
779 static UINT
save_string_table( MSIDATABASE
*db
)
781 UINT i
, count
, datasize
, poolsize
, sz
, used
, r
, codepage
;
782 UINT ret
= ERROR_FUNCTION_FAILED
;
783 static const WCHAR szStringData
[] = {
784 '_','S','t','r','i','n','g','D','a','t','a',0 };
785 static const WCHAR szStringPool
[] = {
786 '_','S','t','r','i','n','g','P','o','o','l',0 };
792 /* construct the new table in memory first */
793 datasize
= msi_string_totalsize( db
->strings
, &count
);
794 poolsize
= count
*2*sizeof(USHORT
);
796 pool
= HeapAlloc( GetProcessHeap(), 0, poolsize
);
799 WARN("Failed to alloc pool %d bytes\n", poolsize
);
802 data
= HeapAlloc( GetProcessHeap(), 0, datasize
);
805 WARN("Failed to alloc data %d bytes\n", poolsize
);
810 codepage
= msi_string_get_codepage( db
->strings
);
811 pool
[0]=codepage
&0xffff;
812 pool
[1]=(codepage
>>16);
813 for( i
=1; i
<count
; i
++ )
815 sz
= datasize
- used
;
816 r
= msi_id2stringA( db
->strings
, i
, data
+used
, &sz
);
817 if( r
!= ERROR_SUCCESS
)
819 ERR("failed to fetch string\n");
822 if( sz
&& (sz
< (datasize
- used
) ) )
824 TRACE("adding %u bytes %s\n", sz
, data
+used
);
826 pool
[ i
*2 + 1 ] = msi_id_refcount( db
->strings
, i
);
828 if( used
> datasize
)
830 ERR("oops overran %d >= %d\n", used
, datasize
);
835 if( used
!= datasize
)
837 ERR("oops used %d != datasize %d\n", used
, datasize
);
841 /* write the streams */
842 r
= write_stream_data( db
->storage
, szStringData
, data
, datasize
);
843 TRACE("Wrote StringData r=%08x\n", r
);
846 r
= write_stream_data( db
->storage
, szStringPool
, pool
, poolsize
);
847 TRACE("Wrote StringPool r=%08x\n", r
);
854 HeapFree( GetProcessHeap(), 0, data
);
855 HeapFree( GetProcessHeap(), 0, pool
);
860 /* information for default tables */
861 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
862 static const WCHAR szTable
[] = { 'T','a','b','l','e',0 };
863 static const WCHAR szName
[] = { 'N','a','m','e',0 };
864 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
865 static const WCHAR szColumn
[] = { 'C','o','l','u','m','n',0 };
866 static const WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
867 static const WCHAR szType
[] = { 'T','y','p','e',0 };
869 struct standard_table
{
874 } MSI_standard_tables
[] =
876 { szTables
, szName
, 1, MSITYPE_VALID
| MSITYPE_STRING
| 32},
877 { szColumns
, szTable
, 1, MSITYPE_VALID
| MSITYPE_STRING
| 32},
878 { szColumns
, szNumber
, 2, MSITYPE_VALID
| 2},
879 { szColumns
, szName
, 3, MSITYPE_VALID
| MSITYPE_STRING
| 32},
880 { szColumns
, szType
, 4, MSITYPE_VALID
| 2},
883 #define STANDARD_TABLE_COUNT \
884 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
886 static UINT
get_defaulttablecolumns( LPCWSTR szTable
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
890 for(i
=0; i
<STANDARD_TABLE_COUNT
; i
++)
892 if( lstrcmpW( szTable
, MSI_standard_tables
[i
].tablename
) )
894 if(colinfo
&& (n
< *sz
) )
896 colinfo
[n
].tablename
= strdupW(MSI_standard_tables
[i
].tablename
);
897 colinfo
[n
].colname
= strdupW(MSI_standard_tables
[i
].columnname
);
898 colinfo
[n
].number
= MSI_standard_tables
[i
].number
;
899 colinfo
[n
].type
= MSI_standard_tables
[i
].type
;
900 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
901 debugstr_w(colinfo[n].colname)); */
903 colinfo
[n
].offset
= colinfo
[n
-1].offset
904 + bytes_per_column( &colinfo
[n
-1] );
906 colinfo
[n
].offset
= 0;
909 if( colinfo
&& (n
>= *sz
) )
913 return ERROR_SUCCESS
;
916 LPWSTR
MSI_makestring( MSIDATABASE
*db
, UINT stringid
)
921 r
= msi_id2stringW( db
->strings
, stringid
, NULL
, &sz
);
922 if( r
!= ERROR_SUCCESS
)
924 str
= HeapAlloc( GetProcessHeap(), 0, sz
*sizeof (WCHAR
));
927 r
= msi_id2stringW( db
->strings
, stringid
, str
, &sz
);
928 if( r
== ERROR_SUCCESS
)
930 HeapFree( GetProcessHeap(), 0, str
);
934 static UINT
get_tablecolumns( MSIDATABASE
*db
,
935 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
937 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
938 MSITABLE
*table
= NULL
;
939 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
941 /* first check if there is a default table with that name */
942 r
= get_defaulttablecolumns( szTableName
, colinfo
, sz
);
943 if( ( r
== ERROR_SUCCESS
) && *sz
)
946 r
= get_table( db
, szColumns
, &table
);
947 if( r
!= ERROR_SUCCESS
)
949 WARN("table %s not available\n", debugstr_w(szColumns
));
953 /* convert table and column names to IDs from the string table */
954 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
955 if( r
!= ERROR_SUCCESS
)
957 release_table( db
, table
);
958 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
962 TRACE("Table id is %d\n", table_id
);
964 count
= table
->row_count
;
965 for( i
=0; i
<count
; i
++ )
967 if( table
->data
[ i
][ 0 ] != table_id
)
971 UINT id
= table
->data
[ i
] [ 2 ];
972 colinfo
[n
].tablename
= MSI_makestring( db
, table_id
);
973 colinfo
[n
].number
= table
->data
[ i
][ 1 ] - (1<<15);
974 colinfo
[n
].colname
= MSI_makestring( db
, id
);
975 colinfo
[n
].type
= table
->data
[ i
] [ 3 ];
976 /* this assumes that columns are in order in the table */
978 colinfo
[n
].offset
= colinfo
[n
-1].offset
979 + bytes_per_column( &colinfo
[n
-1] );
981 colinfo
[n
].offset
= 0;
982 TRACE("table %s column %d is [%s] (%d) with type %08x "
983 "offset %d at row %d\n", debugstr_w(szTableName
),
984 colinfo
[n
].number
, debugstr_w(colinfo
[n
].colname
),
985 id
, colinfo
[n
].type
, colinfo
[n
].offset
, i
);
986 if( n
!= (colinfo
[n
].number
-1) )
988 ERR("oops. data in the _Columns table isn't in the right "
989 "order for table %s\n", debugstr_w(szTableName
));
990 return ERROR_FUNCTION_FAILED
;
994 if( colinfo
&& ( n
>= maxcount
) )
999 release_table( db
, table
);
1001 return ERROR_SUCCESS
;
1004 /* try to find the table name in the _Tables table */
1005 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPWSTR name
)
1007 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
1008 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
1009 UINT r
, table_id
= 0, i
, count
;
1010 MSITABLE
*table
= NULL
;
1012 if( !lstrcmpW( name
, szTables
) )
1014 if( !lstrcmpW( name
, szColumns
) )
1017 r
= msi_string2idW( db
->strings
, name
, &table_id
);
1018 if( r
!= ERROR_SUCCESS
)
1020 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1024 r
= get_table( db
, szTables
, &table
);
1025 if( r
!= ERROR_SUCCESS
)
1027 TRACE("table %s not available\n", debugstr_w(szTables
));
1031 /* count = table->size/2; */
1032 count
= table
->row_count
;
1033 for( i
=0; i
<count
; i
++ )
1034 if( table
->data
[ i
][ 0 ] == table_id
)
1037 release_table( db
, table
);
1042 TRACE("Searched %d tables, but %d was not found\n", count
, table_id
);
1047 /* below is the query interface to a table */
1049 typedef struct tagMSITABLEVIEW
1054 MSICOLUMNINFO
*columns
;
1060 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1062 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1063 UINT offset
, num_rows
, n
;
1066 return ERROR_INVALID_PARAMETER
;
1068 if( (col
==0) || (col
>tv
->num_cols
) )
1069 return ERROR_INVALID_PARAMETER
;
1071 /* how many rows are there ? */
1072 num_rows
= tv
->table
->row_count
;
1073 if( row
>= num_rows
)
1074 return ERROR_NO_MORE_ITEMS
;
1076 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1078 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1079 ERR("%p %p\n", tv
, tv
->columns
);
1080 return ERROR_FUNCTION_FAILED
;
1083 offset
= row
+ (tv
->columns
[col
-1].offset
/2) * num_rows
;
1084 n
= bytes_per_column( &tv
->columns
[col
-1] );
1088 offset
= tv
->columns
[col
-1].offset
/2;
1089 *val
= tv
->table
->data
[row
][offset
] +
1090 (tv
->table
->data
[row
][offset
+ 1] << 16);
1093 offset
= tv
->columns
[col
-1].offset
/2;
1094 *val
= tv
->table
->data
[row
][offset
];
1097 ERR("oops! what is %d bytes per column?\n", n
);
1098 return ERROR_FUNCTION_FAILED
;
1101 /* TRACE("Data [%d][%d] = %d \n", row, col, *val ); */
1103 return ERROR_SUCCESS
;
1107 * We need a special case for streams, as we need to reference column with
1108 * the name of the stream in the same table, and the table name
1109 * which may not be available at higher levels of the query
1111 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1113 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1114 UINT ival
= 0, refcol
= 0, r
;
1118 static const WCHAR szDot
[] = { '.', 0 };
1120 if( !view
->ops
->fetch_int
)
1121 return ERROR_INVALID_PARAMETER
;
1124 * The column marked with the type stream data seems to have a single number
1125 * which references the column containing the name of the stream data
1127 * Fetch the column to reference first.
1129 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1130 if( r
!= ERROR_SUCCESS
)
1133 /* now get the column with the name of the stream */
1134 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1135 if( r
!= ERROR_SUCCESS
)
1138 /* lookup the string value from the string table */
1139 sval
= MSI_makestring( tv
->db
, refcol
);
1141 return ERROR_INVALID_PARAMETER
;
1143 len
= lstrlenW( tv
->name
) + 2 + lstrlenW( sval
);
1144 full_name
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1145 lstrcpyW( full_name
, tv
->name
);
1146 lstrcatW( full_name
, szDot
);
1147 lstrcatW( full_name
, sval
);
1149 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1151 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1152 HeapFree( GetProcessHeap(), 0, full_name
);
1153 HeapFree( GetProcessHeap(), 0, sval
);
1158 static UINT
TABLE_set_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT val
)
1160 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1164 return ERROR_INVALID_PARAMETER
;
1166 if( (col
==0) || (col
>tv
->num_cols
) )
1167 return ERROR_INVALID_PARAMETER
;
1169 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1171 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1172 ERR("%p %p\n", tv
, tv
->columns
);
1173 return ERROR_FUNCTION_FAILED
;
1176 n
= bytes_per_column( &tv
->columns
[col
-1] );
1180 offset
= tv
->columns
[col
-1].offset
/2;
1181 tv
->table
->data
[row
][offset
] = val
& 0xffff;
1182 tv
->table
->data
[row
][offset
+ 1] = (val
>>16)&0xffff;
1185 offset
= tv
->columns
[col
-1].offset
/2;
1186 tv
->table
->data
[row
][offset
] = val
;
1189 ERR("oops! what is %d bytes per column?\n", n
);
1190 return ERROR_FUNCTION_FAILED
;
1192 return ERROR_SUCCESS
;
1195 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
)
1197 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1201 TRACE("%p\n", view
);
1204 return ERROR_INVALID_PARAMETER
;
1206 row
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, tv
->row_size
);
1208 return ERROR_NOT_ENOUGH_MEMORY
;
1210 sz
= (tv
->table
->row_count
+ 1) * sizeof (UINT
*);
1211 if( tv
->table
->data
)
1212 p
= HeapReAlloc( GetProcessHeap(), 0, tv
->table
->data
, sz
);
1214 p
= HeapAlloc( GetProcessHeap(), 0, sz
);
1217 HeapFree( GetProcessHeap(), 0, row
);
1218 return ERROR_NOT_ENOUGH_MEMORY
;
1221 tv
->table
->data
= p
;
1222 tv
->table
->data
[tv
->table
->row_count
] = row
;
1223 *num
= tv
->table
->row_count
;
1224 tv
->table
->row_count
++;
1226 return ERROR_SUCCESS
;
1229 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1231 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1234 TRACE("%p %p\n", tv
, record
);
1238 release_table( tv
->db
, tv
->table
);
1242 r
= get_table( tv
->db
, tv
->name
, &tv
->table
);
1243 if( r
!= ERROR_SUCCESS
)
1246 return ERROR_SUCCESS
;
1249 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1251 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1253 TRACE("%p\n", view
);
1256 return ERROR_FUNCTION_FAILED
;
1258 release_table( tv
->db
, tv
->table
);
1261 return ERROR_SUCCESS
;
1264 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1266 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1268 TRACE("%p %p %p\n", view
, rows
, cols
);
1271 *cols
= tv
->num_cols
;
1275 return ERROR_INVALID_PARAMETER
;
1276 *rows
= tv
->table
->row_count
;
1279 return ERROR_SUCCESS
;
1282 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1283 UINT n
, LPWSTR
*name
, UINT
*type
)
1285 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1287 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1289 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1290 return ERROR_INVALID_PARAMETER
;
1294 *name
= strdupW( tv
->columns
[n
-1].colname
);
1296 return ERROR_FUNCTION_FAILED
;
1299 *type
= tv
->columns
[n
-1].type
;
1301 return ERROR_SUCCESS
;
1304 static UINT
table_find_in_column( MSITABLEVIEW
*tv
, UINT col
, UINT val
, UINT
*row
)
1308 for( i
=0; i
<tv
->table
->row_count
; i
++ )
1310 r
= TABLE_fetch_int( (struct tagMSIVIEW
*) tv
, i
, col
, &x
);
1311 if ( r
!= ERROR_SUCCESS
)
1313 ERR("TABLE_fetch_int shouldn't fail here\n");
1319 return ERROR_SUCCESS
;
1322 return ERROR_FUNCTION_FAILED
;
1325 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1328 UINT i
, val
, r
, row
;
1329 BOOL has_key
= FALSE
;
1331 /* FIXME: set the MsiViewGetError value */
1333 for( i
= 0; i
<tv
->num_cols
; i
++ )
1335 /* check for duplicate keys */
1336 if( !( tv
->columns
[i
].type
& MSITYPE_KEY
) )
1341 TRACE("column %d (%s.%s)is a key\n", i
,
1342 debugstr_w(tv
->columns
[i
].tablename
),
1343 debugstr_w(tv
->columns
[i
].colname
) );
1346 if( tv
->columns
[i
].type
& MSITYPE_STRING
)
1348 /* keys can't be null */
1349 str
= MSI_RecordGetString( rec
, i
+1 );
1351 return ERROR_INVALID_DATA
;
1353 /* if the string doesn't exist in the string table yet, it's OK */
1354 r
= msi_string2idW( tv
->db
->strings
, str
, &val
);
1355 if( ERROR_SUCCESS
!= r
)
1356 return ERROR_SUCCESS
;
1360 val
= MSI_RecordGetInteger( rec
, i
+1 );
1364 /* if we find the same value in the table, it's a duplicate */
1366 r
= table_find_in_column( tv
, i
+1, val
, &row
);
1367 if( ERROR_SUCCESS
!= r
)
1368 return ERROR_SUCCESS
;
1370 TRACE("found in row %d\n", row
);
1374 return ERROR_INVALID_DATA
;
1376 return ERROR_SUCCESS
;
1379 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1381 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1382 UINT n
, type
, val
, r
, row
, col_count
= 0;
1384 r
= TABLE_get_dimensions( view
, NULL
, &col_count
);
1389 r
= table_create_new_row( view
, &row
);
1390 TRACE("insert_row returned %08x\n", r
);
1394 for( n
= 1; n
<= col_count
; n
++ )
1396 r
= TABLE_get_column_info( view
, n
, NULL
, &type
);
1400 if( type
& MSITYPE_STRING
)
1402 const WCHAR
*str
= MSI_RecordGetString( rec
, n
);
1403 val
= msi_addstringW( tv
->db
->strings
, 0, str
, -1, 1 );
1407 val
= MSI_RecordGetInteger( rec
, n
);
1410 r
= TABLE_set_int( view
, row
, n
, val
);
1418 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1421 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1424 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1428 r
= TABLE_execute( view
, NULL
);
1433 switch (eModifyMode
)
1435 case MSIMODIFY_VALIDATE_NEW
:
1436 r
= table_validate_new( tv
, rec
);
1439 case MSIMODIFY_INSERT_TEMPORARY
:
1440 r
= table_validate_new( tv
, rec
);
1441 if (r
!= ERROR_SUCCESS
)
1443 r
= TABLE_insert_row( view
, rec
);
1446 case MSIMODIFY_REFRESH
:
1447 case MSIMODIFY_INSERT
:
1448 case MSIMODIFY_UPDATE
:
1449 case MSIMODIFY_ASSIGN
:
1450 case MSIMODIFY_REPLACE
:
1451 case MSIMODIFY_MERGE
:
1452 case MSIMODIFY_DELETE
:
1453 case MSIMODIFY_VALIDATE
:
1454 case MSIMODIFY_VALIDATE_FIELD
:
1455 case MSIMODIFY_VALIDATE_DELETE
:
1456 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1457 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1461 r
= ERROR_INVALID_DATA
;
1467 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1469 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1471 TRACE("%p\n", view
);
1474 release_table( tv
->db
, tv
->table
);
1480 for( i
=0; i
<tv
->num_cols
; i
++)
1482 HeapFree( GetProcessHeap(), 0, tv
->columns
[i
].colname
);
1483 HeapFree( GetProcessHeap(), 0, tv
->columns
[i
].tablename
);
1485 HeapFree( GetProcessHeap(), 0, tv
->columns
);
1489 HeapFree( GetProcessHeap(), 0, tv
);
1491 return ERROR_SUCCESS
;
1495 MSIVIEWOPS table_ops
=
1503 TABLE_get_dimensions
,
1504 TABLE_get_column_info
,
1509 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
1512 UINT r
, sz
, column_count
;
1513 MSICOLUMNINFO
*columns
, *last_col
;
1515 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
1517 /* get the number of columns in this table */
1519 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
1520 if( r
!= ERROR_SUCCESS
)
1523 /* if there's no columns, there's no table */
1524 if( column_count
== 0 )
1525 return ERROR_INVALID_PARAMETER
;
1527 TRACE("Table found\n");
1529 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
1530 tv
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sz
);
1532 return ERROR_FUNCTION_FAILED
;
1534 columns
= HeapAlloc( GetProcessHeap(), 0, column_count
*sizeof (MSICOLUMNINFO
));
1537 HeapFree( GetProcessHeap(), 0, tv
);
1538 return ERROR_FUNCTION_FAILED
;
1541 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
1542 if( r
!= ERROR_SUCCESS
)
1544 HeapFree( GetProcessHeap(), 0, columns
);
1545 HeapFree( GetProcessHeap(), 0, tv
);
1546 return ERROR_FUNCTION_FAILED
;
1549 TRACE("Table has %d columns\n", column_count
);
1551 last_col
= &columns
[column_count
-1];
1553 /* fill the structure */
1554 tv
->view
.ops
= &table_ops
;
1556 tv
->columns
= columns
;
1557 tv
->num_cols
= column_count
;
1559 tv
->row_size
= last_col
->offset
+ bytes_per_column( last_col
);
1561 TRACE("one row is %d bytes\n", tv
->row_size
);
1563 *view
= (MSIVIEW
*) tv
;
1564 lstrcpyW( tv
->name
, name
);
1566 return ERROR_SUCCESS
;
1569 UINT
MSI_CommitTables( MSIDATABASE
*db
)
1572 MSITABLE
*table
= NULL
;
1576 r
= save_string_table( db
);
1577 if( r
!= ERROR_SUCCESS
)
1579 WARN("failed to save string table r=%08x\n",r
);
1583 for( table
= db
->first_table
; table
; table
= table
->next
)
1585 r
= save_table( db
, table
);
1586 if( r
!= ERROR_SUCCESS
)
1588 WARN("failed to save table %s (r=%08x)\n",
1589 debugstr_w(table
->name
), r
);
1594 /* force everything to reload next time */
1595 free_cached_tables( db
);
1597 return ERROR_SUCCESS
;