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
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
29 #include "wine/debug.h"
37 #include "wine/unicode.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi
);
43 typedef struct tagMSICOLUMNINFO
57 struct tagMSITABLE
*next
;
58 struct tagMSITABLE
*prev
;
62 #define MAX_STREAM_NAME 0x1f
64 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
65 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
66 static UINT
get_tablecolumns( MSIDATABASE
*db
,
67 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
69 static inline UINT
bytes_per_column( MSICOLUMNINFO
*col
)
71 if( col
->type
& MSITYPE_STRING
)
73 if( (col
->type
& 0xff) > 4 )
74 ERR("Invalid column size!\n");
75 return col
->type
& 0xff;
78 static int utf2mime(int x
)
80 if( (x
>='0') && (x
<='9') )
82 if( (x
>='A') && (x
<='Z') )
84 if( (x
>='a') && (x
<='z') )
93 static LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
95 DWORD count
= MAX_STREAM_NAME
;
100 count
= strlenW( in
)+2;
101 out
= HeapAlloc( GetProcessHeap(), 0, count
*sizeof(WCHAR
) );
117 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
119 ch
= utf2mime(ch
) + 0x4800;
121 if( next
&& (next
<0x80) )
123 next
= utf2mime(next
);
134 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
135 HeapFree( GetProcessHeap(), 0, out
);
139 static int mime2utf(int x
)
146 return x
- 10 - 26 + 'a';
147 if( x
== (10+26+26) )
152 static BOOL
decode_streamname(LPWSTR in
, LPWSTR out
)
157 while ( (ch
= *in
++) )
159 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
162 ch
= mime2utf(ch
-0x4800);
166 *out
++ = mime2utf(ch
&0x3f);
168 ch
= mime2utf((ch
>>6)&0x3f);
178 void enum_stream_names( IStorage
*stg
)
180 IEnumSTATSTG
*stgenum
= NULL
;
186 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
194 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
195 if( FAILED( r
) || !count
)
197 decode_streamname( stat
.pwcsName
, name
);
198 ERR("stream %2ld -> %s %s\n", n
,
199 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
203 IEnumSTATSTG_Release( stgenum
);
206 static UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
,
207 USHORT
**pdata
, UINT
*psz
)
210 UINT ret
= ERROR_FUNCTION_FAILED
;
217 encname
= encode_streamname(TRUE
, stname
);
219 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
221 r
= IStorage_OpenStream(stg
, encname
, NULL
,
222 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
223 HeapFree( GetProcessHeap(), 0, encname
);
226 WARN("open stream failed r = %08lx - empty table?\n",r
);
230 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
233 ERR("open stream failed r = %08lx!\n",r
);
237 if( stat
.cbSize
.QuadPart
>> 32 )
243 sz
= stat
.cbSize
.QuadPart
;
244 data
= HeapAlloc( GetProcessHeap(), 0, sz
);
247 ERR("couldn't allocate memory r=%08lx!\n",r
);
248 ret
= ERROR_NOT_ENOUGH_MEMORY
;
252 r
= IStream_Read(stm
, data
, sz
, &count
);
253 if( FAILED( r
) || ( count
!= sz
) )
255 HeapFree( GetProcessHeap(), 0, data
);
256 ERR("read stream failed r = %08lx!\n",r
);
265 IStream_Release( stm
);
270 UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
275 encname
= encode_streamname(FALSE
, stname
);
277 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
279 r
= IStorage_OpenStream(db
->storage
, encname
, NULL
,
280 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
281 HeapFree( GetProcessHeap(), 0, encname
);
284 WARN("open stream failed r = %08lx - empty table?\n",r
);
285 return ERROR_FUNCTION_FAILED
;
288 return ERROR_SUCCESS
;
291 UINT
read_raw_stream_data( MSIDATABASE
*db
, LPCWSTR stname
,
292 USHORT
**pdata
, UINT
*psz
)
295 UINT ret
= ERROR_FUNCTION_FAILED
;
301 r
= db_get_raw_stream( db
, stname
, &stm
);
302 if( r
!= ERROR_SUCCESS
)
304 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
307 ERR("open stream failed r = %08lx!\n",r
);
311 if( stat
.cbSize
.QuadPart
>> 32 )
317 sz
= stat
.cbSize
.QuadPart
;
318 data
= HeapAlloc( GetProcessHeap(), 0, sz
);
321 ERR("couldn't allocate memory r=%08lx!\n",r
);
322 ret
= ERROR_NOT_ENOUGH_MEMORY
;
326 r
= IStream_Read(stm
, data
, sz
, &count
);
327 if( FAILED( r
) || ( count
!= sz
) )
329 HeapFree( GetProcessHeap(), 0, data
);
330 ERR("read stream failed r = %08lx!\n",r
);
339 IStream_Release( stm
);
344 static UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
345 LPVOID data
, UINT sz
)
348 UINT ret
= ERROR_FUNCTION_FAILED
;
355 encname
= encode_streamname(TRUE
, stname
);
356 r
= IStorage_OpenStream( stg
, encname
, NULL
,
357 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
358 HeapFree( GetProcessHeap(), 0, encname
);
361 r
= IStorage_CreateStream( stg
, encname
,
362 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
366 ERR("open stream failed r = %08lx\n",r
);
371 r
= IStream_SetSize( stm
, size
);
374 ERR("Failed to SetSize\n");
379 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
382 ERR("Failed to Seek\n");
386 r
= IStream_Write(stm
, data
, sz
, &count
);
387 if( FAILED( r
) || ( count
!= sz
) )
389 ERR("Failed to Write\n");
396 IStream_Release( stm
);
401 UINT
read_table_from_storage( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**ptable
)
404 USHORT
*rawdata
= NULL
;
405 UINT rawsize
= 0, r
, i
, j
, row_size
= 0, num_cols
= 0;
406 MSICOLUMNINFO
*cols
, *last_col
;
408 TRACE("%s\n",debugstr_w(name
));
410 /* non-existing tables should be interpretted as empty tables */
411 t
= HeapAlloc( GetProcessHeap(), 0,
412 sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
414 return ERROR_NOT_ENOUGH_MEMORY
;
416 r
= table_get_column_info( db
, name
, &cols
, &num_cols
);
417 if( r
!= ERROR_SUCCESS
)
419 HeapFree( GetProcessHeap(), 0, t
);
422 last_col
= &cols
[num_cols
-1];
423 row_size
= last_col
->offset
+ bytes_per_column( last_col
);
427 lstrcpyW( t
->name
, name
);
431 /* if we can't read the table, just assume that it's empty */
432 read_stream_data( db
->storage
, name
, &rawdata
, &rawsize
);
434 return ERROR_SUCCESS
;
436 TRACE("Read %d bytes\n", rawsize
);
438 if( rawsize
% row_size
)
440 ERR("Table size is invalid %d/%d\n", rawsize
, row_size
);
441 return ERROR_FUNCTION_FAILED
;
444 t
->row_count
= rawsize
/ row_size
;
445 t
->data
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
,
446 t
->row_count
* sizeof (USHORT
*) );
448 return ERROR_NOT_ENOUGH_MEMORY
; /* FIXME: memory leak */
450 /* transpose all the data */
451 TRACE("Transposing data from %d columns\n", t
->row_count
);
452 for( i
=0; i
<t
->row_count
; i
++ )
454 t
->data
[i
] = HeapAlloc( GetProcessHeap(), 0, row_size
);
456 return ERROR_NOT_ENOUGH_MEMORY
; /* FIXME: memory leak */
457 for( j
=0; j
<num_cols
; j
++ )
459 UINT ofs
= cols
[j
].offset
/2;
460 UINT n
= bytes_per_column( &cols
[j
] );
465 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
];
468 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
];
469 t
->data
[i
][ofs
+1] = rawdata
[ofs
*t
->row_count
+ i
+ 1];
472 ERR("oops - unknown column width %d\n", n
);
473 return ERROR_FUNCTION_FAILED
;
478 HeapFree( GetProcessHeap(), 0, cols
);
479 HeapFree( GetProcessHeap(), 0, rawdata
);
481 return ERROR_SUCCESS
;
484 /* add this table to the list of cached tables in the database */
485 void add_table(MSIDATABASE
*db
, MSITABLE
*table
)
487 table
->next
= db
->first_table
;
489 if( db
->first_table
)
490 db
->first_table
->prev
= table
;
492 db
->last_table
= table
;
493 db
->first_table
= table
;
496 /* remove from the list of cached tables */
497 void remove_table( MSIDATABASE
*db
, MSITABLE
*table
)
500 table
->next
->prev
= table
->prev
;
502 db
->last_table
= table
->prev
;
504 table
->prev
->next
= table
->next
;
506 db
->first_table
= table
->next
;
511 void release_table( MSIDATABASE
*db
, MSITABLE
*table
)
513 if( !table
->ref_count
)
514 ERR("Trying to destroy table with refcount 0\n");
516 if( !table
->ref_count
)
518 remove_table( db
, table
);
519 HeapFree( GetProcessHeap(), 0, table
->data
);
520 HeapFree( GetProcessHeap(), 0, table
);
521 TRACE("Destroyed table %s\n", debugstr_w(table
->name
));
525 void free_cached_tables( MSIDATABASE
*db
)
527 while( db
->first_table
)
529 MSITABLE
*t
= db
->first_table
;
531 if ( --t
->ref_count
)
532 ERR("table ref count not zero for %s\n", debugstr_w(t
->name
));
533 remove_table( db
, t
);
534 HeapFree( GetProcessHeap(), 0, t
->data
);
535 HeapFree( GetProcessHeap(), 0, t
);
539 UINT
find_cached_table(MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**ptable
)
543 for( t
= db
->first_table
; t
; t
=t
->next
)
545 if( !lstrcmpW( name
, t
->name
) )
548 return ERROR_SUCCESS
;
552 return ERROR_FUNCTION_FAILED
;
555 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
557 UINT r
, column_count
;
558 MSICOLUMNINFO
*columns
;
560 /* get the number of columns in this table */
562 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
563 if( r
!= ERROR_SUCCESS
)
566 /* if there's no columns, there's no table */
567 if( column_count
== 0 )
568 return ERROR_INVALID_PARAMETER
;
570 TRACE("Table %s found\n", debugstr_w(name
) );
572 columns
= HeapAlloc( GetProcessHeap(), 0, column_count
*sizeof (MSICOLUMNINFO
));
574 return ERROR_FUNCTION_FAILED
;
576 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
577 if( r
!= ERROR_SUCCESS
)
579 HeapFree( GetProcessHeap(), 0, columns
);
580 return ERROR_FUNCTION_FAILED
;
584 *pcount
= column_count
;
589 UINT
get_table(MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**ptable
)
595 /* first, see if the table is cached */
596 r
= find_cached_table( db
, name
, ptable
);
597 if( r
== ERROR_SUCCESS
)
599 (*ptable
)->ref_count
++;
603 r
= read_table_from_storage( db
, name
, ptable
);
604 if( r
!= ERROR_SUCCESS
)
607 /* add the table to the list */
608 add_table( db
, *ptable
);
609 (*ptable
)->ref_count
++;
611 return ERROR_SUCCESS
;
614 UINT
save_table( MSIDATABASE
*db
, MSITABLE
*t
)
616 USHORT
*rawdata
= NULL
, *p
;
617 UINT rawsize
, r
, i
, j
, row_size
, num_cols
= 0;
618 MSICOLUMNINFO
*cols
, *last_col
;
620 TRACE("Saving %s\n", debugstr_w( t
->name
) );
622 r
= table_get_column_info( db
, t
->name
, &cols
, &num_cols
);
623 if( r
!= ERROR_SUCCESS
)
626 last_col
= &cols
[num_cols
-1];
627 row_size
= last_col
->offset
+ bytes_per_column( last_col
);
629 rawsize
= t
->row_count
* row_size
;
630 rawdata
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, rawsize
);
632 return ERROR_NOT_ENOUGH_MEMORY
;
635 for( i
=0; i
<num_cols
; i
++ )
637 for( j
=0; j
<t
->row_count
; j
++ )
639 UINT offset
= cols
[i
].offset
;
641 *p
++ = t
->data
[j
][offset
/2];
642 if( 4 == bytes_per_column( &cols
[i
] ) )
643 *p
++ = t
->data
[j
][offset
/2+1];
647 TRACE("writing %d bytes\n", rawsize
);
648 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
);
650 HeapFree( GetProcessHeap(), 0, rawdata
);
655 HRESULT
init_string_table( IStorage
*stg
)
658 static const WCHAR szStringData
[] = {
659 '_','S','t','r','i','n','g','D','a','t','a',0 };
660 static const WCHAR szStringPool
[] = {
661 '_','S','t','r','i','n','g','P','o','o','l',0 };
662 USHORT zero
[2] = { 0, 0 };
667 encname
= encode_streamname(TRUE
, szStringPool
);
669 /* create the StringPool stream... add the zero string to it*/
670 r
= IStorage_CreateStream( stg
, encname
,
671 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
672 HeapFree( GetProcessHeap(), 0, encname
);
679 r
= IStream_Write(stm
, zero
, sizeof zero
, &count
);
680 IStream_Release( stm
);
682 if( FAILED( r
) || ( count
!= sizeof zero
) )
688 /* create the StringData stream... make it zero length */
689 encname
= encode_streamname(TRUE
, szStringData
);
690 r
= IStorage_CreateStream( stg
, encname
,
691 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
692 HeapFree( GetProcessHeap(), 0, encname
);
698 IStream_Release( stm
);
703 UINT
load_string_table( MSIDATABASE
*db
)
707 UINT r
, ret
= ERROR_FUNCTION_FAILED
, datasize
= 0, poolsize
= 0, codepage
;
708 DWORD i
, count
, offset
, len
, n
;
709 static const WCHAR szStringData
[] = {
710 '_','S','t','r','i','n','g','D','a','t','a',0 };
711 static const WCHAR szStringPool
[] = {
712 '_','S','t','r','i','n','g','P','o','o','l',0 };
716 msi_destroy_stringtable( db
->strings
);
720 r
= read_stream_data( db
->storage
, szStringPool
, &pool
, &poolsize
);
721 if( r
!= ERROR_SUCCESS
)
723 r
= read_stream_data( db
->storage
, szStringData
, (USHORT
**)&data
, &datasize
);
724 if( r
!= ERROR_SUCCESS
)
729 codepage
= pool
[0] | ( pool
[1] << 16 );
732 db
->strings
= msi_init_stringtable( count
, codepage
);
735 for( i
=1; i
<count
; i
++ )
738 n
= msi_addstring( db
->strings
, i
, data
+offset
, len
, pool
[i
*2+1] );
740 ERR("Failed to add string %ld\n", i
);
744 TRACE("Loaded %ld strings\n", count
);
750 HeapFree( GetProcessHeap(), 0, pool
);
752 HeapFree( GetProcessHeap(), 0, data
);
757 UINT
save_string_table( MSIDATABASE
*db
)
759 UINT i
, count
, datasize
, poolsize
, sz
, used
, r
, codepage
;
760 UINT ret
= ERROR_FUNCTION_FAILED
;
761 static const WCHAR szStringData
[] = {
762 '_','S','t','r','i','n','g','D','a','t','a',0 };
763 static const WCHAR szStringPool
[] = {
764 '_','S','t','r','i','n','g','P','o','o','l',0 };
770 /* construct the new table in memory first */
771 datasize
= msi_string_totalsize( db
->strings
, &count
);
772 poolsize
= count
*2*sizeof(USHORT
);
774 pool
= HeapAlloc( GetProcessHeap(), 0, poolsize
);
777 ERR("Failed to alloc pool %d bytes\n", poolsize
);
780 data
= HeapAlloc( GetProcessHeap(), 0, datasize
);
783 ERR("Failed to alloc data %d bytes\n", poolsize
);
788 codepage
= msi_string_get_codepage( db
->strings
);
789 pool
[0]=codepage
&0xffff;
790 pool
[1]=(codepage
>>16);
791 for( i
=1; i
<count
; i
++ )
793 sz
= datasize
- used
;
794 r
= msi_id2stringA( db
->strings
, i
, data
+used
, &sz
);
795 if( r
!= ERROR_SUCCESS
)
797 ERR("failed to fetch string\n");
800 if( sz
&& (sz
< (datasize
- used
) ) )
802 TRACE("adding %u bytes %s\n", sz
, data
+used
);
804 pool
[ i
*2 + 1 ] = msi_id_refcount( db
->strings
, i
);
806 if( used
> datasize
)
808 ERR("oops overran %d >= %d\n", used
, datasize
);
813 if( used
!= datasize
)
815 ERR("oops used %d != datasize %d\n", used
, datasize
);
819 /* write the streams */
820 r
= write_stream_data( db
->storage
, szStringData
, data
, datasize
);
821 TRACE("Wrote StringData r=%08x\n", r
);
824 r
= write_stream_data( db
->storage
, szStringPool
, pool
, poolsize
);
825 TRACE("Wrote StringPool r=%08x\n", r
);
833 HeapFree( GetProcessHeap(), 0, data
);
835 HeapFree( GetProcessHeap(), 0, pool
);
840 static LPWSTR
strdupW( LPCWSTR str
)
842 UINT len
= lstrlenW( str
) + 1;
843 LPWSTR ret
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof (WCHAR
) );
845 lstrcpyW( ret
, str
);
849 /* information for default tables */
850 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
851 static const WCHAR szTable
[] = { 'T','a','b','l','e',0 };
852 static const WCHAR szName
[] = { 'N','a','m','e',0 };
853 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
854 static const WCHAR szColumn
[] = { 'C','o','l','u','m','n',0 };
855 static const WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
856 static const WCHAR szType
[] = { 'T','y','p','e',0 };
858 struct standard_table
{
863 } MSI_standard_tables
[] =
865 { szTables
, szName
, 1, MSITYPE_VALID
| MSITYPE_STRING
| 32},
866 { szColumns
, szTable
, 1, MSITYPE_VALID
| MSITYPE_STRING
| 32},
867 { szColumns
, szNumber
, 2, MSITYPE_VALID
| 2},
868 { szColumns
, szName
, 3, MSITYPE_VALID
| MSITYPE_STRING
| 32},
869 { szColumns
, szType
, 4, MSITYPE_VALID
| 2},
872 #define STANDARD_TABLE_COUNT \
873 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
875 UINT
get_defaulttablecolumns( LPCWSTR szTable
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
879 for(i
=0; i
<STANDARD_TABLE_COUNT
; i
++)
881 if( lstrcmpW( szTable
, MSI_standard_tables
[i
].tablename
) )
883 if(colinfo
&& (n
< *sz
) )
885 colinfo
[n
].tablename
= strdupW(MSI_standard_tables
[i
].tablename
);
886 colinfo
[n
].colname
= strdupW(MSI_standard_tables
[i
].columnname
);
887 colinfo
[n
].number
= MSI_standard_tables
[i
].number
;
888 colinfo
[n
].type
= MSI_standard_tables
[i
].type
;
889 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
890 debugstr_w(colinfo[n].colname)); */
892 colinfo
[n
].offset
= colinfo
[n
-1].offset
893 + bytes_per_column( &colinfo
[n
-1] );
895 colinfo
[n
].offset
= 0;
898 if( colinfo
&& (n
>= *sz
) )
902 return ERROR_SUCCESS
;
905 LPWSTR
MSI_makestring( MSIDATABASE
*db
, UINT stringid
)
910 r
= msi_id2stringW( db
->strings
, stringid
, NULL
, &sz
);
911 if( r
!= ERROR_SUCCESS
)
913 str
= HeapAlloc( GetProcessHeap(), 0, sz
*sizeof (WCHAR
));
916 r
= msi_id2stringW( db
->strings
, stringid
, str
, &sz
);
917 if( r
== ERROR_SUCCESS
)
919 HeapFree( GetProcessHeap(), 0, str
);
923 static UINT
get_tablecolumns( MSIDATABASE
*db
,
924 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
926 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
927 MSITABLE
*table
= NULL
;
928 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
930 /* first check if there is a default table with that name */
931 r
= get_defaulttablecolumns( szTableName
, colinfo
, sz
);
932 if( ( r
== ERROR_SUCCESS
) && *sz
)
935 r
= get_table( db
, szColumns
, &table
);
936 if( r
!= ERROR_SUCCESS
)
938 ERR("table %s not available\n", debugstr_w(szColumns
));
942 /* convert table and column names to IDs from the string table */
943 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
944 if( r
!= ERROR_SUCCESS
)
946 release_table( db
, table
);
947 ERR("Couldn't find id for %s\n", debugstr_w(szTableName
));
951 TRACE("Table id is %d\n", table_id
);
953 count
= table
->row_count
;
954 for( i
=0; i
<count
; i
++ )
956 if( table
->data
[ i
][ 0 ] != table_id
)
960 UINT id
= table
->data
[ i
] [ 2 ];
961 colinfo
[n
].tablename
= MSI_makestring( db
, table_id
);
962 colinfo
[n
].number
= table
->data
[ i
][ 1 ] - (1<<15);
963 colinfo
[n
].colname
= MSI_makestring( db
, id
);
964 colinfo
[n
].type
= table
->data
[ i
] [ 3 ];
965 /* this assumes that columns are in order in the table */
967 colinfo
[n
].offset
= colinfo
[n
-1].offset
968 + bytes_per_column( &colinfo
[n
-1] );
970 colinfo
[n
].offset
= 0;
971 TRACE("table %s column %d is [%s] (%d) with type %08x "
972 "offset %d at row %d\n", debugstr_w(szTableName
),
973 colinfo
[n
].number
, debugstr_w(colinfo
[n
].colname
),
974 id
, colinfo
[n
].type
, colinfo
[n
].offset
, i
);
975 if( n
!= (colinfo
[n
].number
-1) )
977 ERR("oops. data in the _Columns table isn't in the right "
978 "order for table %s\n", debugstr_w(szTableName
));
979 return ERROR_FUNCTION_FAILED
;
983 if( colinfo
&& ( n
>= maxcount
) )
988 release_table( db
, table
);
990 return ERROR_SUCCESS
;
993 /* try to find the table name in the _Tables table */
994 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPWSTR name
)
996 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
997 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
998 UINT r
, table_id
= 0, i
, count
;
999 MSITABLE
*table
= NULL
;
1001 if( !lstrcmpW( name
, szTables
) )
1003 if( !lstrcmpW( name
, szColumns
) )
1006 r
= msi_string2idW( db
->strings
, name
, &table_id
);
1007 if( r
!= ERROR_SUCCESS
)
1009 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1013 r
= get_table( db
, szTables
, &table
);
1014 if( r
!= ERROR_SUCCESS
)
1016 ERR("table %s not available\n", debugstr_w(szTables
));
1020 /* count = table->size/2; */
1021 count
= table
->row_count
;
1022 for( i
=0; i
<count
; i
++ )
1023 if( table
->data
[ i
][ 0 ] == table_id
)
1026 release_table( db
, table
);
1031 ERR("Searched %d tables, but %d was not found\n", count
, table_id
);
1036 /* below is the query interface to a table */
1038 typedef struct tagMSITABLEVIEW
1043 MSICOLUMNINFO
*columns
;
1049 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1051 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1052 UINT offset
, num_rows
, n
;
1055 return ERROR_INVALID_PARAMETER
;
1057 if( (col
==0) || (col
>tv
->num_cols
) )
1058 return ERROR_INVALID_PARAMETER
;
1060 /* how many rows are there ? */
1061 num_rows
= tv
->table
->row_count
;
1062 if( row
>= num_rows
)
1063 return ERROR_NO_MORE_ITEMS
;
1065 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1067 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1068 ERR("%p %p\n", tv
, tv
->columns
);
1069 return ERROR_FUNCTION_FAILED
;
1072 offset
= row
+ (tv
->columns
[col
-1].offset
/2) * num_rows
;
1073 n
= bytes_per_column( &tv
->columns
[col
-1] );
1077 offset
= tv
->columns
[col
-1].offset
/2;
1078 *val
= tv
->table
->data
[row
][offset
] +
1079 (tv
->table
->data
[row
][offset
+ 1] << 16);
1082 offset
= tv
->columns
[col
-1].offset
/2;
1083 *val
= tv
->table
->data
[row
][offset
];
1086 ERR("oops! what is %d bytes per column?\n", n
);
1087 return ERROR_FUNCTION_FAILED
;
1090 TRACE("Data [%d][%d] = %d \n", row
, col
, *val
);
1092 return ERROR_SUCCESS
;
1096 * We need a special case for streams, as we need to reference column with
1097 * the name of the stream in the same table, and the table name
1098 * which may not be available at higher levels of the query
1100 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1102 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1103 UINT ival
= 0, refcol
= 0, r
;
1107 static const WCHAR szDot
[] = { '.', 0 };
1109 if( !view
->ops
->fetch_int
)
1110 return ERROR_INVALID_PARAMETER
;
1113 * The column marked with the type stream data seems to have a single number
1114 * which references the column containing the name of the stream data
1116 * Fetch the column to reference first.
1118 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1119 if( r
!= ERROR_SUCCESS
)
1122 /* now get the column with the name of the stream */
1123 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1124 if( r
!= ERROR_SUCCESS
)
1127 /* lookup the string value from the string table */
1128 sval
= MSI_makestring( tv
->db
, refcol
);
1130 return ERROR_INVALID_PARAMETER
;
1132 len
= strlenW( tv
->name
) + 2 + strlenW( sval
);
1133 full_name
= HeapAlloc( GetProcessHeap(), 0, len
*sizeof(WCHAR
) );
1134 strcpyW( full_name
, tv
->name
);
1135 strcatW( full_name
, szDot
);
1136 strcatW( full_name
, sval
);
1138 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1140 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1141 HeapFree( GetProcessHeap(), 0, full_name
);
1142 HeapFree( GetProcessHeap(), 0, sval
);
1147 static UINT
TABLE_set_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT val
)
1149 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1153 return ERROR_INVALID_PARAMETER
;
1155 if( (col
==0) || (col
>tv
->num_cols
) )
1156 return ERROR_INVALID_PARAMETER
;
1158 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1160 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1161 ERR("%p %p\n", tv
, tv
->columns
);
1162 return ERROR_FUNCTION_FAILED
;
1165 n
= bytes_per_column( &tv
->columns
[col
-1] );
1169 offset
= tv
->columns
[col
-1].offset
/2;
1170 tv
->table
->data
[row
][offset
] = val
& 0xffff;
1171 tv
->table
->data
[row
][offset
+ 1] = (val
>>16)&0xffff;
1174 offset
= tv
->columns
[col
-1].offset
/2;
1175 tv
->table
->data
[row
][offset
] = val
;
1178 ERR("oops! what is %d bytes per column?\n", n
);
1179 return ERROR_FUNCTION_FAILED
;
1181 return ERROR_SUCCESS
;
1184 UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, UINT
*num
)
1186 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1190 TRACE("%p\n", view
);
1193 return ERROR_INVALID_PARAMETER
;
1195 row
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, tv
->row_size
);
1197 return ERROR_NOT_ENOUGH_MEMORY
;
1199 sz
= (tv
->table
->row_count
+ 1) * sizeof (UINT
*);
1200 if( tv
->table
->data
)
1201 p
= HeapReAlloc( GetProcessHeap(), 0, tv
->table
->data
, sz
);
1203 p
= HeapAlloc( GetProcessHeap(), 0, sz
);
1205 return ERROR_NOT_ENOUGH_MEMORY
;
1207 tv
->table
->data
= p
;
1208 tv
->table
->data
[tv
->table
->row_count
] = row
;
1209 *num
= tv
->table
->row_count
;
1210 tv
->table
->row_count
++;
1212 return ERROR_SUCCESS
;
1215 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1217 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1220 TRACE("%p %p\n", tv
, record
);
1223 return ERROR_FUNCTION_FAILED
;
1225 r
= get_table( tv
->db
, tv
->name
, &tv
->table
);
1226 if( r
!= ERROR_SUCCESS
)
1229 return ERROR_SUCCESS
;
1232 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1234 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1236 TRACE("%p\n", view
);
1239 return ERROR_FUNCTION_FAILED
;
1241 release_table( tv
->db
, tv
->table
);
1244 return ERROR_SUCCESS
;
1247 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1249 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1251 TRACE("%p %p %p\n", view
, rows
, cols
);
1254 *cols
= tv
->num_cols
;
1258 return ERROR_INVALID_PARAMETER
;
1259 *rows
= tv
->table
->row_count
;
1262 return ERROR_SUCCESS
;
1265 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1266 UINT n
, LPWSTR
*name
, UINT
*type
)
1268 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1270 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1272 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1273 return ERROR_INVALID_PARAMETER
;
1277 *name
= strdupW( tv
->columns
[n
-1].colname
);
1279 return ERROR_FUNCTION_FAILED
;
1282 *type
= tv
->columns
[n
-1].type
;
1284 return ERROR_SUCCESS
;
1287 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
, MSIHANDLE hrec
)
1289 FIXME("%p %d %ld\n", view
, eModifyMode
, hrec
);
1290 return ERROR_CALL_NOT_IMPLEMENTED
;
1293 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1295 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1297 TRACE("%p\n", view
);
1300 release_table( tv
->db
, tv
->table
);
1306 for( i
=0; i
<tv
->num_cols
; i
++)
1308 HeapFree( GetProcessHeap(), 0, tv
->columns
[i
].colname
);
1309 HeapFree( GetProcessHeap(), 0, tv
->columns
[i
].tablename
);
1311 HeapFree( GetProcessHeap(), 0, tv
->columns
);
1315 HeapFree( GetProcessHeap(), 0, tv
);
1317 return ERROR_SUCCESS
;
1321 MSIVIEWOPS table_ops
=
1329 TABLE_get_dimensions
,
1330 TABLE_get_column_info
,
1335 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
1338 UINT r
, sz
, column_count
;
1339 MSICOLUMNINFO
*columns
, *last_col
;
1341 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
1343 /* get the number of columns in this table */
1345 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
1346 if( r
!= ERROR_SUCCESS
)
1349 /* if there's no columns, there's no table */
1350 if( column_count
== 0 )
1351 return ERROR_INVALID_PARAMETER
;
1353 TRACE("Table found\n");
1355 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
1356 tv
= HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY
, sz
);
1358 return ERROR_FUNCTION_FAILED
;
1360 columns
= HeapAlloc( GetProcessHeap(), 0, column_count
*sizeof (MSICOLUMNINFO
));
1363 HeapFree( GetProcessHeap(), 0, tv
);
1364 return ERROR_FUNCTION_FAILED
;
1367 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
1368 if( r
!= ERROR_SUCCESS
)
1370 HeapFree( GetProcessHeap(), 0, columns
);
1371 HeapFree( GetProcessHeap(), 0, tv
);
1372 return ERROR_FUNCTION_FAILED
;
1375 TRACE("Table has %d columns\n", column_count
);
1377 last_col
= &columns
[column_count
-1];
1379 /* fill the structure */
1380 tv
->view
.ops
= &table_ops
;
1382 tv
->columns
= columns
;
1383 tv
->num_cols
= column_count
;
1385 tv
->row_size
= last_col
->offset
+ bytes_per_column( last_col
);
1387 TRACE("one row is %d bytes\n", tv
->row_size
);
1389 *view
= (MSIVIEW
*) tv
;
1390 lstrcpyW( tv
->name
, name
);
1392 return ERROR_SUCCESS
;
1395 UINT
MSI_CommitTables( MSIDATABASE
*db
)
1398 MSITABLE
*table
= NULL
;
1402 r
= save_string_table( db
);
1403 if( r
!= ERROR_SUCCESS
)
1405 ERR("failed to save string table r=%08x\n",r
);
1409 for( table
= db
->first_table
; table
; table
= table
->next
)
1411 r
= save_table( db
, table
);
1412 if( r
!= ERROR_SUCCESS
)
1414 ERR("failed to save table %s (r=%08x)\n",
1415 debugstr_w(table
->name
), r
);
1420 /* force everything to reload next time */
1421 free_cached_tables( db
);
1423 return ERROR_SUCCESS
;