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
);
734 for( i
=1; i
<count
; i
++ )
737 n
= msi_addstring( db
->strings
, i
, data
+offset
, len
, pool
[i
*2+1] );
739 ERR("Failed to add string %ld\n", i
);
743 TRACE("Loaded %ld strings\n", count
);
748 HeapFree( GetProcessHeap(), 0, pool
);
749 HeapFree( GetProcessHeap(), 0, data
);
754 static UINT
save_string_table( MSIDATABASE
*db
)
756 UINT i
, count
, datasize
, poolsize
, sz
, used
, r
, codepage
;
757 UINT ret
= ERROR_FUNCTION_FAILED
;
758 static const WCHAR szStringData
[] = {
759 '_','S','t','r','i','n','g','D','a','t','a',0 };
760 static const WCHAR szStringPool
[] = {
761 '_','S','t','r','i','n','g','P','o','o','l',0 };
767 /* construct the new table in memory first */
768 datasize
= msi_string_totalsize( db
->strings
, &count
);
769 poolsize
= count
*2*sizeof(USHORT
);
771 pool
= HeapAlloc( GetProcessHeap(), 0, poolsize
);
774 WARN("Failed to alloc pool %d bytes\n", poolsize
);
777 data
= HeapAlloc( GetProcessHeap(), 0, datasize
);
780 WARN("Failed to alloc data %d bytes\n", poolsize
);
785 codepage
= msi_string_get_codepage( db
->strings
);
786 pool
[0]=codepage
&0xffff;
787 pool
[1]=(codepage
>>16);
788 for( i
=1; i
<count
; i
++ )
790 sz
= datasize
- used
;
791 r
= msi_id2stringA( db
->strings
, i
, data
+used
, &sz
);
792 if( r
!= ERROR_SUCCESS
)
794 ERR("failed to fetch string\n");
797 if( sz
&& (sz
< (datasize
- used
) ) )
799 TRACE("adding %u bytes %s\n", sz
, data
+used
);
801 pool
[ i
*2 + 1 ] = msi_id_refcount( db
->strings
, i
);
803 if( used
> datasize
)
805 ERR("oops overran %d >= %d\n", used
, datasize
);
810 if( used
!= datasize
)
812 ERR("oops used %d != datasize %d\n", used
, datasize
);
816 /* write the streams */
817 r
= write_stream_data( db
->storage
, szStringData
, data
, datasize
);
818 TRACE("Wrote StringData r=%08x\n", r
);
821 r
= write_stream_data( db
->storage
, szStringPool
, pool
, poolsize
);
822 TRACE("Wrote StringPool r=%08x\n", r
);
829 HeapFree( GetProcessHeap(), 0, data
);
830 HeapFree( GetProcessHeap(), 0, pool
);
835 /* information for default tables */
836 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
837 static const WCHAR szTable
[] = { 'T','a','b','l','e',0 };
838 static const WCHAR szName
[] = { 'N','a','m','e',0 };
839 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
840 static const WCHAR szColumn
[] = { 'C','o','l','u','m','n',0 };
841 static const WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
842 static const WCHAR szType
[] = { 'T','y','p','e',0 };
844 struct standard_table
{
849 } MSI_standard_tables
[] =
851 { szTables
, szName
, 1, MSITYPE_VALID
| MSITYPE_STRING
| 32},
852 { szColumns
, szTable
, 1, MSITYPE_VALID
| MSITYPE_STRING
| 32},
853 { szColumns
, szNumber
, 2, MSITYPE_VALID
| 2},
854 { szColumns
, szName
, 3, MSITYPE_VALID
| MSITYPE_STRING
| 32},
855 { szColumns
, szType
, 4, MSITYPE_VALID
| 2},
858 #define STANDARD_TABLE_COUNT \
859 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
861 static UINT
get_defaulttablecolumns( LPCWSTR szTable
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
865 for(i
=0; i
<STANDARD_TABLE_COUNT
; i
++)
867 if( lstrcmpW( szTable
, MSI_standard_tables
[i
].tablename
) )
869 if(colinfo
&& (n
< *sz
) )
871 colinfo
[n
].tablename
= strdupW(MSI_standard_tables
[i
].tablename
);
872 colinfo
[n
].colname
= strdupW(MSI_standard_tables
[i
].columnname
);
873 colinfo
[n
].number
= MSI_standard_tables
[i
].number
;
874 colinfo
[n
].type
= MSI_standard_tables
[i
].type
;
875 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
876 debugstr_w(colinfo[n].colname)); */
878 colinfo
[n
].offset
= colinfo
[n
-1].offset
879 + bytes_per_column( &colinfo
[n
-1] );
881 colinfo
[n
].offset
= 0;
884 if( colinfo
&& (n
>= *sz
) )
888 return ERROR_SUCCESS
;
891 LPWSTR
MSI_makestring( MSIDATABASE
*db
, UINT stringid
)
896 r
= msi_id2stringW( db
->strings
, stringid
, NULL
, &sz
);
897 if( r
!= ERROR_SUCCESS
)
899 str
= HeapAlloc( GetProcessHeap(), 0, sz
*sizeof (WCHAR
));
902 r
= msi_id2stringW( db
->strings
, stringid
, str
, &sz
);
903 if( r
== ERROR_SUCCESS
)
905 HeapFree( GetProcessHeap(), 0, str
);
909 static UINT
get_tablecolumns( MSIDATABASE
*db
,
910 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
912 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
913 MSITABLE
*table
= NULL
;
914 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
916 /* first check if there is a default table with that name */
917 r
= get_defaulttablecolumns( szTableName
, colinfo
, sz
);
918 if( ( r
== ERROR_SUCCESS
) && *sz
)
921 r
= get_table( db
, szColumns
, &table
);
922 if( r
!= ERROR_SUCCESS
)
924 WARN("table %s not available\n", debugstr_w(szColumns
));
928 /* convert table and column names to IDs from the string table */
929 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
930 if( r
!= ERROR_SUCCESS
)
932 release_table( db
, table
);
933 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
937 TRACE("Table id is %d\n", table_id
);
939 count
= table
->row_count
;
940 for( i
=0; i
<count
; i
++ )
942 if( table
->data
[ i
][ 0 ] != table_id
)
946 UINT id
= table
->data
[ i
] [ 2 ];
947 colinfo
[n
].tablename
= MSI_makestring( db
, table_id
);
948 colinfo
[n
].number
= table
->data
[ i
][ 1 ] - (1<<15);
949 colinfo
[n
].colname
= MSI_makestring( db
, id
);
950 colinfo
[n
].type
= table
->data
[ i
] [ 3 ];
951 /* this assumes that columns are in order in the table */
953 colinfo
[n
].offset
= colinfo
[n
-1].offset
954 + bytes_per_column( &colinfo
[n
-1] );
956 colinfo
[n
].offset
= 0;
957 TRACE("table %s column %d is [%s] (%d) with type %08x "
958 "offset %d at row %d\n", debugstr_w(szTableName
),
959 colinfo
[n
].number
, debugstr_w(colinfo
[n
].colname
),
960 id
, colinfo
[n
].type
, colinfo
[n
].offset
, i
);
961 if( n
!= (colinfo
[n
].number
-1) )
963 ERR("oops. data in the _Columns table isn't in the right "
964 "order for table %s\n", debugstr_w(szTableName
));
965 return ERROR_FUNCTION_FAILED
;
969 if( colinfo
&& ( n
>= maxcount
) )
974 release_table( db
, table
);
976 return ERROR_SUCCESS
;
979 /* try to find the table name in the _Tables table */
980 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPWSTR name
)
982 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
983 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
984 UINT r
, table_id
= 0, i
, count
;
985 MSITABLE
*table
= NULL
;
987 if( !lstrcmpW( name
, szTables
) )
989 if( !lstrcmpW( name
, szColumns
) )
992 r
= msi_string2idW( db
->strings
, name
, &table_id
);
993 if( r
!= ERROR_SUCCESS
)
995 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
999 r
= get_table( db
, szTables
, &table
);
1000 if( r
!= ERROR_SUCCESS
)
1002 TRACE("table %s not available\n", debugstr_w(szTables
));
1006 /* count = table->size/2; */
1007 count
= table
->row_count
;
1008 for( i
=0; i
<count
; i
++ )
1009 if( table
->data
[ i
][ 0 ] == table_id
)
1012 release_table( db
, table
);
1017 TRACE("Searched %d tables, but %d was not found\n", count
, table_id
);
1022 /* below is the query interface to a table */
1024 typedef struct tagMSITABLEVIEW
1029 MSICOLUMNINFO
*columns
;
1035 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1037 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1038 UINT offset
, num_rows
, n
;
1041 return ERROR_INVALID_PARAMETER
;
1043 if( (col
==0) || (col
>tv
->num_cols
) )
1044 return ERROR_INVALID_PARAMETER
;
1046 /* how many rows are there ? */
1047 num_rows
= tv
->table
->row_count
;
1048 if( row
>= num_rows
)
1049 return ERROR_NO_MORE_ITEMS
;
1051 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1053 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1054 ERR("%p %p\n", tv
, tv
->columns
);
1055 return ERROR_FUNCTION_FAILED
;
1058 offset
= row
+ (tv
->columns
[col
-1].offset
/2) * num_rows
;
1059 n
= bytes_per_column( &tv
->columns
[col
-1] );
1063 offset
= tv
->columns
[col
-1].offset
/2;
1064 *val
= tv
->table
->data
[row
][offset
] +
1065 (tv
->table
->data
[row
][offset
+ 1] << 16);
1068 offset
= tv
->columns
[col
-1].offset
/2;
1069 *val
= tv
->table
->data
[row
][offset
];
1072 ERR("oops! what is %d bytes per column?\n", n
);
1073 return ERROR_FUNCTION_FAILED
;
1076 /* TRACE("Data [%d][%d] = %d \n", row, col, *val ); */
1078 return ERROR_SUCCESS
;
1082 * We need a special case for streams, as we need to reference column with
1083 * the name of the stream in the same table, and the table name
1084 * which may not be available at higher levels of the query
1086 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1088 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1089 UINT ival
= 0, refcol
= 0, r
;
1093 static const WCHAR szDot
[] = { '.', 0 };
1095 if( !view
->ops
->fetch_int
)
1096 return ERROR_INVALID_PARAMETER
;
1099 * The column marked with the type stream data seems to have a single number
1100 * which references the column containing the name of the stream data
1102 * Fetch the column to reference first.
1104 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1105 if( r
!= ERROR_SUCCESS
)
1108 /* now get the column with the name of the stream */
1109 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1110 if( r
!= ERROR_SUCCESS
)
1113 /* lookup the string value from the string table */
1114 sval
= MSI_makestring( tv
->db
, refcol
);
1116 return ERROR_INVALID_PARAMETER
;
1118 len
= lstrlenW( tv
->name
) + 2 + lstrlenW( sval
);
1119 full_name
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1120 lstrcpyW( full_name
, tv
->name
);
1121 lstrcatW( full_name
, szDot
);
1122 lstrcatW( full_name
, sval
);
1124 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1126 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1127 HeapFree( GetProcessHeap(), 0, full_name
);
1128 HeapFree( GetProcessHeap(), 0, sval
);
1133 static UINT
TABLE_set_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT val
)
1135 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1139 return ERROR_INVALID_PARAMETER
;
1141 if( (col
==0) || (col
>tv
->num_cols
) )
1142 return ERROR_INVALID_PARAMETER
;
1144 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1146 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1147 ERR("%p %p\n", tv
, tv
->columns
);
1148 return ERROR_FUNCTION_FAILED
;
1151 n
= bytes_per_column( &tv
->columns
[col
-1] );
1155 offset
= tv
->columns
[col
-1].offset
/2;
1156 tv
->table
->data
[row
][offset
] = val
& 0xffff;
1157 tv
->table
->data
[row
][offset
+ 1] = (val
>>16)&0xffff;
1160 offset
= tv
->columns
[col
-1].offset
/2;
1161 tv
->table
->data
[row
][offset
] = val
;
1164 ERR("oops! what is %d bytes per column?\n", n
);
1165 return ERROR_FUNCTION_FAILED
;
1167 return ERROR_SUCCESS
;
1170 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, UINT
*num
)
1172 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1176 TRACE("%p\n", view
);
1179 return ERROR_INVALID_PARAMETER
;
1181 row
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, tv
->row_size
);
1183 return ERROR_NOT_ENOUGH_MEMORY
;
1185 sz
= (tv
->table
->row_count
+ 1) * sizeof (UINT
*);
1186 if( tv
->table
->data
)
1187 p
= HeapReAlloc( GetProcessHeap(), 0, tv
->table
->data
, sz
);
1189 p
= HeapAlloc( GetProcessHeap(), 0, sz
);
1192 HeapFree( GetProcessHeap(), 0, row
);
1193 return ERROR_NOT_ENOUGH_MEMORY
;
1196 tv
->table
->data
= p
;
1197 tv
->table
->data
[tv
->table
->row_count
] = row
;
1198 *num
= tv
->table
->row_count
;
1199 tv
->table
->row_count
++;
1201 return ERROR_SUCCESS
;
1204 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1206 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1209 TRACE("%p %p\n", tv
, record
);
1213 release_table( tv
->db
, tv
->table
);
1217 r
= get_table( tv
->db
, tv
->name
, &tv
->table
);
1218 if( r
!= ERROR_SUCCESS
)
1221 return ERROR_SUCCESS
;
1224 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1226 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1228 TRACE("%p\n", view
);
1231 return ERROR_FUNCTION_FAILED
;
1233 release_table( tv
->db
, tv
->table
);
1236 return ERROR_SUCCESS
;
1239 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1241 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1243 TRACE("%p %p %p\n", view
, rows
, cols
);
1246 *cols
= tv
->num_cols
;
1250 return ERROR_INVALID_PARAMETER
;
1251 *rows
= tv
->table
->row_count
;
1254 return ERROR_SUCCESS
;
1257 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1258 UINT n
, LPWSTR
*name
, UINT
*type
)
1260 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1262 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1264 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1265 return ERROR_INVALID_PARAMETER
;
1269 *name
= strdupW( tv
->columns
[n
-1].colname
);
1271 return ERROR_FUNCTION_FAILED
;
1274 *type
= tv
->columns
[n
-1].type
;
1276 return ERROR_SUCCESS
;
1279 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1282 FIXME("%p %d %p\n", view
, eModifyMode
, rec
);
1283 return ERROR_CALL_NOT_IMPLEMENTED
;
1286 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1288 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1290 TRACE("%p\n", view
);
1293 release_table( tv
->db
, tv
->table
);
1299 for( i
=0; i
<tv
->num_cols
; i
++)
1301 HeapFree( GetProcessHeap(), 0, tv
->columns
[i
].colname
);
1302 HeapFree( GetProcessHeap(), 0, tv
->columns
[i
].tablename
);
1304 HeapFree( GetProcessHeap(), 0, tv
->columns
);
1308 HeapFree( GetProcessHeap(), 0, tv
);
1310 return ERROR_SUCCESS
;
1314 MSIVIEWOPS table_ops
=
1322 TABLE_get_dimensions
,
1323 TABLE_get_column_info
,
1328 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
1331 UINT r
, sz
, column_count
;
1332 MSICOLUMNINFO
*columns
, *last_col
;
1334 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
1336 /* get the number of columns in this table */
1338 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
1339 if( r
!= ERROR_SUCCESS
)
1342 /* if there's no columns, there's no table */
1343 if( column_count
== 0 )
1344 return ERROR_INVALID_PARAMETER
;
1346 TRACE("Table found\n");
1348 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
1349 tv
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sz
);
1351 return ERROR_FUNCTION_FAILED
;
1353 columns
= HeapAlloc( GetProcessHeap(), 0, column_count
*sizeof (MSICOLUMNINFO
));
1356 HeapFree( GetProcessHeap(), 0, tv
);
1357 return ERROR_FUNCTION_FAILED
;
1360 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
1361 if( r
!= ERROR_SUCCESS
)
1363 HeapFree( GetProcessHeap(), 0, columns
);
1364 HeapFree( GetProcessHeap(), 0, tv
);
1365 return ERROR_FUNCTION_FAILED
;
1368 TRACE("Table has %d columns\n", column_count
);
1370 last_col
= &columns
[column_count
-1];
1372 /* fill the structure */
1373 tv
->view
.ops
= &table_ops
;
1375 tv
->columns
= columns
;
1376 tv
->num_cols
= column_count
;
1378 tv
->row_size
= last_col
->offset
+ bytes_per_column( last_col
);
1380 TRACE("one row is %d bytes\n", tv
->row_size
);
1382 *view
= (MSIVIEW
*) tv
;
1383 lstrcpyW( tv
->name
, name
);
1385 return ERROR_SUCCESS
;
1388 UINT
MSI_CommitTables( MSIDATABASE
*db
)
1391 MSITABLE
*table
= NULL
;
1395 r
= save_string_table( db
);
1396 if( r
!= ERROR_SUCCESS
)
1398 WARN("failed to save string table r=%08x\n",r
);
1402 for( table
= db
->first_table
; table
; table
= table
->next
)
1404 r
= save_table( db
, table
);
1405 if( r
!= ERROR_SUCCESS
)
1407 WARN("failed to save table %s (r=%08x)\n",
1408 debugstr_w(table
->name
), r
);
1413 /* force everything to reload next time */
1414 free_cached_tables( db
);
1416 return ERROR_SUCCESS
;