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
60 #define MAX_STREAM_NAME 0x1f
62 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
,
63 MSICOLUMNINFO
**pcols
, UINT
*pcount
);
64 static UINT
get_tablecolumns( MSIDATABASE
*db
,
65 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
);
67 static inline UINT
bytes_per_column( MSICOLUMNINFO
*col
)
69 if( col
->type
& MSITYPE_STRING
)
71 if( (col
->type
& 0xff) > 4 )
72 ERR("Invalid column size!\n");
73 return col
->type
& 0xff;
76 static int utf2mime(int x
)
78 if( (x
>='0') && (x
<='9') )
80 if( (x
>='A') && (x
<='Z') )
82 if( (x
>='a') && (x
<='z') )
91 static LPWSTR
encode_streamname(BOOL bTable
, LPCWSTR in
)
93 DWORD count
= MAX_STREAM_NAME
;
98 count
= lstrlenW( in
)+2;
99 out
= msi_alloc( count
*sizeof(WCHAR
) );
115 if( ( ch
< 0x80 ) && ( utf2mime(ch
) >= 0 ) )
117 ch
= utf2mime(ch
) + 0x4800;
119 if( next
&& (next
<0x80) )
121 next
= utf2mime(next
);
132 ERR("Failed to encode stream name (%s)\n",debugstr_w(in
));
137 static int mime2utf(int x
)
144 return x
- 10 - 26 + 'a';
145 if( x
== (10+26+26) )
150 static BOOL
decode_streamname(LPWSTR in
, LPWSTR out
)
155 while ( (ch
= *in
++) )
157 if( (ch
>= 0x3800 ) && (ch
< 0x4840 ) )
160 ch
= mime2utf(ch
-0x4800);
164 *out
++ = mime2utf(ch
&0x3f);
166 ch
= mime2utf((ch
>>6)&0x3f);
176 void enum_stream_names( IStorage
*stg
)
178 IEnumSTATSTG
*stgenum
= NULL
;
184 r
= IStorage_EnumElements( stg
, 0, NULL
, 0, &stgenum
);
192 r
= IEnumSTATSTG_Next( stgenum
, 1, &stat
, &count
);
193 if( FAILED( r
) || !count
)
195 decode_streamname( stat
.pwcsName
, name
);
196 TRACE("stream %2ld -> %s %s\n", n
,
197 debugstr_w(stat
.pwcsName
), debugstr_w(name
) );
201 IEnumSTATSTG_Release( stgenum
);
204 static UINT
read_stream_data( IStorage
*stg
, LPCWSTR stname
,
205 USHORT
**pdata
, UINT
*psz
)
208 UINT ret
= ERROR_FUNCTION_FAILED
;
215 encname
= encode_streamname(TRUE
, stname
);
217 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
219 r
= IStorage_OpenStream(stg
, encname
, NULL
,
220 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
224 WARN("open stream failed r = %08lx - empty table?\n",r
);
228 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
231 WARN("open stream failed r = %08lx!\n",r
);
235 if( stat
.cbSize
.QuadPart
>> 32 )
241 sz
= stat
.cbSize
.QuadPart
;
242 data
= msi_alloc( sz
);
245 WARN("couldn't allocate memory r=%08lx!\n",r
);
246 ret
= ERROR_NOT_ENOUGH_MEMORY
;
250 r
= IStream_Read(stm
, data
, sz
, &count
);
251 if( FAILED( r
) || ( count
!= sz
) )
254 WARN("read stream failed r = %08lx!\n",r
);
263 IStream_Release( stm
);
268 UINT
db_get_raw_stream( MSIDATABASE
*db
, LPCWSTR stname
, IStream
**stm
)
273 encname
= encode_streamname(FALSE
, stname
);
275 TRACE("%s -> %s\n",debugstr_w(stname
),debugstr_w(encname
));
277 r
= IStorage_OpenStream(db
->storage
, encname
, NULL
,
278 STGM_READ
| STGM_SHARE_EXCLUSIVE
, 0, stm
);
282 WARN("open stream failed r = %08lx - empty table?\n",r
);
283 return ERROR_FUNCTION_FAILED
;
286 return ERROR_SUCCESS
;
289 UINT
read_raw_stream_data( MSIDATABASE
*db
, LPCWSTR stname
,
290 USHORT
**pdata
, UINT
*psz
)
293 UINT ret
= ERROR_FUNCTION_FAILED
;
299 r
= db_get_raw_stream( db
, stname
, &stm
);
300 if( r
!= ERROR_SUCCESS
)
302 r
= IStream_Stat(stm
, &stat
, STATFLAG_NONAME
);
305 WARN("open stream failed r = %08lx!\n",r
);
309 if( stat
.cbSize
.QuadPart
>> 32 )
315 sz
= stat
.cbSize
.QuadPart
;
316 data
= msi_alloc( sz
);
319 WARN("couldn't allocate memory r=%08lx!\n",r
);
320 ret
= ERROR_NOT_ENOUGH_MEMORY
;
324 r
= IStream_Read(stm
, data
, sz
, &count
);
325 if( FAILED( r
) || ( count
!= sz
) )
328 WARN("read stream failed r = %08lx!\n",r
);
337 IStream_Release( stm
);
342 static UINT
write_stream_data( IStorage
*stg
, LPCWSTR stname
,
343 LPVOID data
, UINT sz
)
346 UINT ret
= ERROR_FUNCTION_FAILED
;
353 encname
= encode_streamname(TRUE
, stname
);
354 r
= IStorage_OpenStream( stg
, encname
, NULL
,
355 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, &stm
);
358 r
= IStorage_CreateStream( stg
, encname
,
359 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
364 WARN("open stream failed r = %08lx\n",r
);
369 r
= IStream_SetSize( stm
, size
);
372 WARN("Failed to SetSize\n");
377 r
= IStream_Seek( stm
, pos
, STREAM_SEEK_SET
, NULL
);
380 WARN("Failed to Seek\n");
384 r
= IStream_Write(stm
, data
, sz
, &count
);
385 if( FAILED( r
) || ( count
!= sz
) )
387 WARN("Failed to Write\n");
394 IStream_Release( stm
);
399 static void free_table( MSITABLE
*table
)
402 for( i
=0; i
<table
->row_count
; i
++ )
403 msi_free( table
->data
[i
] );
404 msi_free( table
->data
);
408 static UINT
read_table_from_storage( MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**ptable
)
411 USHORT
*rawdata
= NULL
;
412 UINT rawsize
= 0, r
, i
, j
, row_size
= 0, num_cols
= 0;
413 MSICOLUMNINFO
*cols
, *last_col
;
415 TRACE("%s\n",debugstr_w(name
));
417 /* nonexistent tables should be interpreted as empty tables */
418 t
= msi_alloc( sizeof (MSITABLE
) + lstrlenW(name
)*sizeof (WCHAR
) );
420 return ERROR_NOT_ENOUGH_MEMORY
;
422 r
= table_get_column_info( db
, name
, &cols
, &num_cols
);
423 if( r
!= ERROR_SUCCESS
)
428 last_col
= &cols
[num_cols
-1];
429 row_size
= last_col
->offset
+ bytes_per_column( last_col
);
433 lstrcpyW( t
->name
, name
);
437 /* if we can't read the table, just assume that it's empty */
438 read_stream_data( db
->storage
, name
, &rawdata
, &rawsize
);
440 return ERROR_SUCCESS
;
442 TRACE("Read %d bytes\n", rawsize
);
444 if( rawsize
% row_size
)
446 WARN("Table size is invalid %d/%d\n", rawsize
, row_size
);
447 return ERROR_FUNCTION_FAILED
;
450 t
->row_count
= rawsize
/ row_size
;
451 t
->data
= msi_alloc_zero( t
->row_count
* sizeof (USHORT
*) );
454 r
= ERROR_NOT_ENOUGH_MEMORY
;
458 /* transpose all the data */
459 TRACE("Transposing data from %d columns\n", t
->row_count
);
460 for( i
=0; i
<t
->row_count
; i
++ )
462 t
->data
[i
] = msi_alloc( row_size
);
465 r
= ERROR_NOT_ENOUGH_MEMORY
;
468 for( j
=0; j
<num_cols
; j
++ )
470 UINT ofs
= cols
[j
].offset
/2;
471 UINT n
= bytes_per_column( &cols
[j
] );
476 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
];
479 t
->data
[i
][ofs
] = rawdata
[ofs
*t
->row_count
+ i
*2 ];
480 t
->data
[i
][ofs
+1] = rawdata
[ofs
*t
->row_count
+ i
*2 + 1];
483 ERR("oops - unknown column width %d\n", n
);
484 r
= ERROR_FUNCTION_FAILED
;
493 return ERROR_SUCCESS
;
502 /* add this table to the list of cached tables in the database */
503 void add_table(MSIDATABASE
*db
, MSITABLE
*table
)
505 list_add_head( &db
->tables
, &table
->entry
);
508 /* remove from the list of cached tables */
509 void remove_table( MSIDATABASE
*db
, MSITABLE
*table
)
511 list_remove( &table
->entry
);
514 static void release_table( MSIDATABASE
*db
, MSITABLE
*table
)
516 if( !table
->ref_count
)
517 ERR("Trying to destroy table with refcount 0\n");
519 if( !table
->ref_count
)
521 remove_table( db
, table
);
522 TRACE("Destroying table %s\n", debugstr_w(table
->name
));
527 void free_cached_tables( MSIDATABASE
*db
)
529 while( !list_empty( &db
->tables
) )
531 MSITABLE
*t
= LIST_ENTRY( list_head( &db
->tables
), MSITABLE
, entry
);
533 list_remove( &t
->entry
);
534 if ( --t
->ref_count
)
535 ERR("table ref count not zero for %s\n", debugstr_w(t
->name
));
536 remove_table( db
, t
);
542 UINT
find_cached_table(MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**ptable
)
546 LIST_FOR_EACH_ENTRY( t
, &db
->tables
, MSITABLE
, entry
)
548 if( !lstrcmpW( name
, t
->name
) )
551 return ERROR_SUCCESS
;
555 return ERROR_FUNCTION_FAILED
;
558 static UINT
table_get_column_info( MSIDATABASE
*db
, LPCWSTR name
, MSICOLUMNINFO
**pcols
, UINT
*pcount
)
560 UINT r
, column_count
;
561 MSICOLUMNINFO
*columns
;
563 /* get the number of columns in this table */
565 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
566 if( r
!= ERROR_SUCCESS
)
569 /* if there's no columns, there's no table */
570 if( column_count
== 0 )
571 return ERROR_INVALID_PARAMETER
;
573 TRACE("Table %s found\n", debugstr_w(name
) );
575 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
));
577 return ERROR_FUNCTION_FAILED
;
579 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
580 if( r
!= ERROR_SUCCESS
)
583 return ERROR_FUNCTION_FAILED
;
587 *pcount
= column_count
;
592 UINT
get_table(MSIDATABASE
*db
, LPCWSTR name
, MSITABLE
**ptable
)
598 /* first, see if the table is cached */
599 r
= find_cached_table( db
, name
, ptable
);
600 if( r
== ERROR_SUCCESS
)
602 (*ptable
)->ref_count
++;
606 r
= read_table_from_storage( db
, name
, ptable
);
607 if( r
!= ERROR_SUCCESS
)
610 /* add the table to the list */
611 add_table( db
, *ptable
);
612 (*ptable
)->ref_count
++;
614 return ERROR_SUCCESS
;
617 static UINT
save_table( MSIDATABASE
*db
, MSITABLE
*t
)
619 USHORT
*rawdata
= NULL
, *p
;
620 UINT rawsize
, r
, i
, j
, row_size
, num_cols
= 0;
621 MSICOLUMNINFO
*cols
, *last_col
;
623 TRACE("Saving %s\n", debugstr_w( t
->name
) );
625 r
= table_get_column_info( db
, t
->name
, &cols
, &num_cols
);
626 if( r
!= ERROR_SUCCESS
)
629 last_col
= &cols
[num_cols
-1];
630 row_size
= last_col
->offset
+ bytes_per_column( last_col
);
632 rawsize
= t
->row_count
* row_size
;
633 rawdata
= msi_alloc_zero( rawsize
);
635 return ERROR_NOT_ENOUGH_MEMORY
;
638 for( i
=0; i
<num_cols
; i
++ )
640 for( j
=0; j
<t
->row_count
; j
++ )
642 UINT offset
= cols
[i
].offset
;
644 *p
++ = t
->data
[j
][offset
/2];
645 if( 4 == bytes_per_column( &cols
[i
] ) )
646 *p
++ = t
->data
[j
][offset
/2+1];
650 TRACE("writing %d bytes\n", rawsize
);
651 r
= write_stream_data( db
->storage
, t
->name
, rawdata
, rawsize
);
658 HRESULT
init_string_table( IStorage
*stg
)
661 static const WCHAR szStringData
[] = {
662 '_','S','t','r','i','n','g','D','a','t','a',0 };
663 static const WCHAR szStringPool
[] = {
664 '_','S','t','r','i','n','g','P','o','o','l',0 };
665 USHORT zero
[2] = { 0, 0 };
670 encname
= encode_streamname(TRUE
, szStringPool
);
672 /* create the StringPool stream... add the zero string to it*/
673 r
= IStorage_CreateStream( stg
, encname
,
674 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
682 r
= IStream_Write(stm
, zero
, sizeof zero
, &count
);
683 IStream_Release( stm
);
685 if( FAILED( r
) || ( count
!= sizeof zero
) )
691 /* create the StringData stream... make it zero length */
692 encname
= encode_streamname(TRUE
, szStringData
);
693 r
= IStorage_CreateStream( stg
, encname
,
694 STGM_WRITE
| STGM_SHARE_EXCLUSIVE
, 0, 0, &stm
);
701 IStream_Release( stm
);
706 string_table
*load_string_table( IStorage
*stg
)
708 string_table
*st
= NULL
;
711 UINT r
, datasize
= 0, poolsize
= 0, codepage
;
712 DWORD i
, count
, offset
, len
, n
;
713 static const WCHAR szStringData
[] = {
714 '_','S','t','r','i','n','g','D','a','t','a',0 };
715 static const WCHAR szStringPool
[] = {
716 '_','S','t','r','i','n','g','P','o','o','l',0 };
718 r
= read_stream_data( stg
, szStringPool
, &pool
, &poolsize
);
719 if( r
!= ERROR_SUCCESS
)
721 r
= read_stream_data( stg
, szStringData
, (USHORT
**)&data
, &datasize
);
722 if( r
!= ERROR_SUCCESS
)
727 codepage
= pool
[0] | ( pool
[1] << 16 );
730 st
= msi_init_stringtable( count
, codepage
);
734 for( i
=1; i
<count
; i
++ )
739 * If a string is over 64k, the previous string entry is made null
740 * and its the high word of the length is inserted in the null string's
741 * reference count field.
743 if( pool
[i
*2-2] == 0 )
744 len
+= pool
[i
*2-1] * 0x10000;
746 if( (offset
+ len
) > datasize
)
748 ERR("string table corrupt?\n");
752 /* don't add the high word of a string's length as a string */
753 if ( len
|| !pool
[i
*2+1] )
755 r
= msi_addstring( st
, n
, data
+offset
, len
, pool
[i
*2+1] );
757 ERR("Failed to add string %ld\n", n
);
764 if ( datasize
!= offset
)
765 ERR("string table load failed! (%08x != %08lx)\n", datasize
, offset
);
767 TRACE("Loaded %ld strings\n", count
);
776 static UINT
save_string_table( MSIDATABASE
*db
)
778 UINT i
, count
, datasize
, poolsize
, sz
, used
, r
, codepage
;
779 UINT ret
= ERROR_FUNCTION_FAILED
;
780 static const WCHAR szStringData
[] = {
781 '_','S','t','r','i','n','g','D','a','t','a',0 };
782 static const WCHAR szStringPool
[] = {
783 '_','S','t','r','i','n','g','P','o','o','l',0 };
789 /* construct the new table in memory first */
790 datasize
= msi_string_totalsize( db
->strings
, &count
);
791 poolsize
= count
*2*sizeof(USHORT
);
793 pool
= msi_alloc( poolsize
);
796 WARN("Failed to alloc pool %d bytes\n", poolsize
);
799 data
= msi_alloc( datasize
);
802 WARN("Failed to alloc data %d bytes\n", poolsize
);
807 codepage
= msi_string_get_codepage( db
->strings
);
808 pool
[0]=codepage
&0xffff;
809 pool
[1]=(codepage
>>16);
810 for( i
=1; i
<count
; i
++ )
812 sz
= datasize
- used
;
813 r
= msi_id2stringA( db
->strings
, i
, data
+used
, &sz
);
814 if( r
!= ERROR_SUCCESS
)
816 ERR("failed to fetch string\n");
819 if( sz
&& (sz
< (datasize
- used
) ) )
821 TRACE("adding %u bytes %s\n", sz
, debugstr_a(data
+used
) );
823 pool
[ i
*2 + 1 ] = msi_id_refcount( db
->strings
, i
);
825 if( used
> datasize
)
827 ERR("oops overran %d >= %d\n", used
, datasize
);
832 if( used
!= datasize
)
834 ERR("oops used %d != datasize %d\n", used
, datasize
);
838 /* write the streams */
839 r
= write_stream_data( db
->storage
, szStringData
, data
, datasize
);
840 TRACE("Wrote StringData r=%08x\n", r
);
843 r
= write_stream_data( db
->storage
, szStringPool
, pool
, poolsize
);
844 TRACE("Wrote StringPool r=%08x\n", r
);
857 /* information for default tables */
858 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
859 static const WCHAR szTable
[] = { 'T','a','b','l','e',0 };
860 static const WCHAR szName
[] = { 'N','a','m','e',0 };
861 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
862 static const WCHAR szColumn
[] = { 'C','o','l','u','m','n',0 };
863 static const WCHAR szNumber
[] = { 'N','u','m','b','e','r',0 };
864 static const WCHAR szType
[] = { 'T','y','p','e',0 };
866 struct standard_table
{
871 } MSI_standard_tables
[] =
873 { szTables
, szName
, 1, MSITYPE_VALID
| MSITYPE_STRING
| 32},
874 { szColumns
, szTable
, 1, MSITYPE_VALID
| MSITYPE_STRING
| 32},
875 { szColumns
, szNumber
, 2, MSITYPE_VALID
| 2},
876 { szColumns
, szName
, 3, MSITYPE_VALID
| MSITYPE_STRING
| 32},
877 { szColumns
, szType
, 4, MSITYPE_VALID
| 2},
880 #define STANDARD_TABLE_COUNT \
881 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
883 static UINT
get_defaulttablecolumns( LPCWSTR szTable
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
887 for(i
=0; i
<STANDARD_TABLE_COUNT
; i
++)
889 if( lstrcmpW( szTable
, MSI_standard_tables
[i
].tablename
) )
891 if(colinfo
&& (n
< *sz
) )
893 colinfo
[n
].tablename
= strdupW(MSI_standard_tables
[i
].tablename
);
894 colinfo
[n
].colname
= strdupW(MSI_standard_tables
[i
].columnname
);
895 colinfo
[n
].number
= MSI_standard_tables
[i
].number
;
896 colinfo
[n
].type
= MSI_standard_tables
[i
].type
;
897 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
898 debugstr_w(colinfo[n].colname)); */
900 colinfo
[n
].offset
= colinfo
[n
-1].offset
901 + bytes_per_column( &colinfo
[n
-1] );
903 colinfo
[n
].offset
= 0;
906 if( colinfo
&& (n
>= *sz
) )
910 return ERROR_SUCCESS
;
913 LPWSTR
MSI_makestring( MSIDATABASE
*db
, UINT stringid
)
918 r
= msi_id2stringW( db
->strings
, stringid
, NULL
, &sz
);
919 if( r
!= ERROR_SUCCESS
)
921 str
= msi_alloc( sz
*sizeof (WCHAR
));
924 r
= msi_id2stringW( db
->strings
, stringid
, str
, &sz
);
925 if( r
== ERROR_SUCCESS
)
931 static UINT
get_tablecolumns( MSIDATABASE
*db
,
932 LPCWSTR szTableName
, MSICOLUMNINFO
*colinfo
, UINT
*sz
)
934 UINT r
, i
, n
=0, table_id
, count
, maxcount
= *sz
;
935 MSITABLE
*table
= NULL
;
936 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
938 /* first check if there is a default table with that name */
939 r
= get_defaulttablecolumns( szTableName
, colinfo
, sz
);
940 if( ( r
== ERROR_SUCCESS
) && *sz
)
943 r
= get_table( db
, szColumns
, &table
);
944 if( r
!= ERROR_SUCCESS
)
946 WARN("table %s not available\n", debugstr_w(szColumns
));
950 /* convert table and column names to IDs from the string table */
951 r
= msi_string2idW( db
->strings
, szTableName
, &table_id
);
952 if( r
!= ERROR_SUCCESS
)
954 release_table( db
, table
);
955 WARN("Couldn't find id for %s\n", debugstr_w(szTableName
));
959 TRACE("Table id is %d\n", table_id
);
961 count
= table
->row_count
;
962 for( i
=0; i
<count
; i
++ )
964 if( table
->data
[ i
][ 0 ] != table_id
)
968 UINT id
= table
->data
[ i
] [ 2 ];
969 colinfo
[n
].tablename
= MSI_makestring( db
, table_id
);
970 colinfo
[n
].number
= table
->data
[ i
][ 1 ] - (1<<15);
971 colinfo
[n
].colname
= MSI_makestring( db
, id
);
972 colinfo
[n
].type
= table
->data
[ i
] [ 3 ];
973 /* this assumes that columns are in order in the table */
975 colinfo
[n
].offset
= colinfo
[n
-1].offset
976 + bytes_per_column( &colinfo
[n
-1] );
978 colinfo
[n
].offset
= 0;
979 TRACE("table %s column %d is [%s] (%d) with type %08x "
980 "offset %d at row %d\n", debugstr_w(szTableName
),
981 colinfo
[n
].number
, debugstr_w(colinfo
[n
].colname
),
982 id
, colinfo
[n
].type
, colinfo
[n
].offset
, i
);
983 if( n
!= (colinfo
[n
].number
-1) )
985 ERR("oops. data in the _Columns table isn't in the right "
986 "order for table %s\n", debugstr_w(szTableName
));
987 return ERROR_FUNCTION_FAILED
;
991 if( colinfo
&& ( n
>= maxcount
) )
996 release_table( db
, table
);
998 return ERROR_SUCCESS
;
1001 /* try to find the table name in the _Tables table */
1002 BOOL
TABLE_Exists( MSIDATABASE
*db
, LPWSTR name
)
1004 static const WCHAR szTables
[] = { '_','T','a','b','l','e','s',0 };
1005 static const WCHAR szColumns
[] = { '_','C','o','l','u','m','n','s',0 };
1006 UINT r
, table_id
= 0, i
, count
;
1007 MSITABLE
*table
= NULL
;
1009 if( !lstrcmpW( name
, szTables
) )
1011 if( !lstrcmpW( name
, szColumns
) )
1014 r
= msi_string2idW( db
->strings
, name
, &table_id
);
1015 if( r
!= ERROR_SUCCESS
)
1017 TRACE("Couldn't find id for %s\n", debugstr_w(name
));
1021 r
= get_table( db
, szTables
, &table
);
1022 if( r
!= ERROR_SUCCESS
)
1024 TRACE("table %s not available\n", debugstr_w(szTables
));
1028 /* count = table->size/2; */
1029 count
= table
->row_count
;
1030 for( i
=0; i
<count
; i
++ )
1031 if( table
->data
[ i
][ 0 ] == table_id
)
1034 release_table( db
, table
);
1039 TRACE("Searched %d tables, but %d was not found\n", count
, table_id
);
1044 /* below is the query interface to a table */
1046 typedef struct tagMSITABLEVIEW
1051 MSICOLUMNINFO
*columns
;
1057 static UINT
TABLE_fetch_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT
*val
)
1059 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1060 UINT offset
, num_rows
, n
;
1063 return ERROR_INVALID_PARAMETER
;
1065 if( (col
==0) || (col
>tv
->num_cols
) )
1066 return ERROR_INVALID_PARAMETER
;
1068 /* how many rows are there ? */
1069 num_rows
= tv
->table
->row_count
;
1070 if( row
>= num_rows
)
1071 return ERROR_NO_MORE_ITEMS
;
1073 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1075 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1076 ERR("%p %p\n", tv
, tv
->columns
);
1077 return ERROR_FUNCTION_FAILED
;
1080 offset
= row
+ (tv
->columns
[col
-1].offset
/2) * num_rows
;
1081 n
= bytes_per_column( &tv
->columns
[col
-1] );
1085 offset
= tv
->columns
[col
-1].offset
/2;
1086 *val
= tv
->table
->data
[row
][offset
] +
1087 (tv
->table
->data
[row
][offset
+ 1] << 16);
1090 offset
= tv
->columns
[col
-1].offset
/2;
1091 *val
= tv
->table
->data
[row
][offset
];
1094 ERR("oops! what is %d bytes per column?\n", n
);
1095 return ERROR_FUNCTION_FAILED
;
1098 /* TRACE("Data [%d][%d] = %d \n", row, col, *val ); */
1100 return ERROR_SUCCESS
;
1104 * We need a special case for streams, as we need to reference column with
1105 * the name of the stream in the same table, and the table name
1106 * which may not be available at higher levels of the query
1108 static UINT
TABLE_fetch_stream( struct tagMSIVIEW
*view
, UINT row
, UINT col
, IStream
**stm
)
1110 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1111 UINT ival
= 0, refcol
= 0, r
;
1115 static const WCHAR szDot
[] = { '.', 0 };
1117 if( !view
->ops
->fetch_int
)
1118 return ERROR_INVALID_PARAMETER
;
1121 * The column marked with the type stream data seems to have a single number
1122 * which references the column containing the name of the stream data
1124 * Fetch the column to reference first.
1126 r
= view
->ops
->fetch_int( view
, row
, col
, &ival
);
1127 if( r
!= ERROR_SUCCESS
)
1130 /* now get the column with the name of the stream */
1131 r
= view
->ops
->fetch_int( view
, row
, ival
, &refcol
);
1132 if( r
!= ERROR_SUCCESS
)
1135 /* lookup the string value from the string table */
1136 sval
= MSI_makestring( tv
->db
, refcol
);
1138 return ERROR_INVALID_PARAMETER
;
1140 len
= lstrlenW( tv
->name
) + 2 + lstrlenW( sval
);
1141 full_name
= msi_alloc( len
*sizeof(WCHAR
) );
1142 lstrcpyW( full_name
, tv
->name
);
1143 lstrcatW( full_name
, szDot
);
1144 lstrcatW( full_name
, sval
);
1146 r
= db_get_raw_stream( tv
->db
, full_name
, stm
);
1148 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name
), r
);
1149 msi_free( full_name
);
1155 static UINT
TABLE_set_int( struct tagMSIVIEW
*view
, UINT row
, UINT col
, UINT val
)
1157 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1161 return ERROR_INVALID_PARAMETER
;
1163 if( (col
==0) || (col
>tv
->num_cols
) )
1164 return ERROR_INVALID_PARAMETER
;
1166 if( tv
->columns
[col
-1].offset
>= tv
->row_size
)
1168 ERR("Stuffed up %d >= %d\n", tv
->columns
[col
-1].offset
, tv
->row_size
);
1169 ERR("%p %p\n", tv
, tv
->columns
);
1170 return ERROR_FUNCTION_FAILED
;
1173 n
= bytes_per_column( &tv
->columns
[col
-1] );
1177 offset
= tv
->columns
[col
-1].offset
/2;
1178 tv
->table
->data
[row
][offset
] = val
& 0xffff;
1179 tv
->table
->data
[row
][offset
+ 1] = (val
>>16)&0xffff;
1182 offset
= tv
->columns
[col
-1].offset
/2;
1183 tv
->table
->data
[row
][offset
] = val
;
1186 ERR("oops! what is %d bytes per column?\n", n
);
1187 return ERROR_FUNCTION_FAILED
;
1189 return ERROR_SUCCESS
;
1192 static UINT
table_create_new_row( struct tagMSIVIEW
*view
, UINT
*num
)
1194 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1198 TRACE("%p\n", view
);
1201 return ERROR_INVALID_PARAMETER
;
1203 row
= msi_alloc_zero( tv
->row_size
);
1205 return ERROR_NOT_ENOUGH_MEMORY
;
1207 sz
= (tv
->table
->row_count
+ 1) * sizeof (UINT
*);
1208 if( tv
->table
->data
)
1209 p
= msi_realloc( tv
->table
->data
, sz
);
1211 p
= msi_alloc( sz
);
1215 return ERROR_NOT_ENOUGH_MEMORY
;
1218 tv
->table
->data
= p
;
1219 tv
->table
->data
[tv
->table
->row_count
] = row
;
1220 *num
= tv
->table
->row_count
;
1221 tv
->table
->row_count
++;
1223 return ERROR_SUCCESS
;
1226 static UINT
TABLE_execute( struct tagMSIVIEW
*view
, MSIRECORD
*record
)
1228 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1231 TRACE("%p %p\n", tv
, record
);
1235 release_table( tv
->db
, tv
->table
);
1239 r
= get_table( tv
->db
, tv
->name
, &tv
->table
);
1240 if( r
!= ERROR_SUCCESS
)
1243 return ERROR_SUCCESS
;
1246 static UINT
TABLE_close( struct tagMSIVIEW
*view
)
1248 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1250 TRACE("%p\n", view
);
1253 return ERROR_FUNCTION_FAILED
;
1255 release_table( tv
->db
, tv
->table
);
1258 return ERROR_SUCCESS
;
1261 static UINT
TABLE_get_dimensions( struct tagMSIVIEW
*view
, UINT
*rows
, UINT
*cols
)
1263 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1265 TRACE("%p %p %p\n", view
, rows
, cols
);
1268 *cols
= tv
->num_cols
;
1272 return ERROR_INVALID_PARAMETER
;
1273 *rows
= tv
->table
->row_count
;
1276 return ERROR_SUCCESS
;
1279 static UINT
TABLE_get_column_info( struct tagMSIVIEW
*view
,
1280 UINT n
, LPWSTR
*name
, UINT
*type
)
1282 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1284 TRACE("%p %d %p %p\n", tv
, n
, name
, type
);
1286 if( ( n
== 0 ) || ( n
> tv
->num_cols
) )
1287 return ERROR_INVALID_PARAMETER
;
1291 *name
= strdupW( tv
->columns
[n
-1].colname
);
1293 return ERROR_FUNCTION_FAILED
;
1296 *type
= tv
->columns
[n
-1].type
;
1298 return ERROR_SUCCESS
;
1301 static UINT
table_find_in_column( MSITABLEVIEW
*tv
, UINT col
, UINT val
, UINT
*row
)
1305 for( i
=0; i
<tv
->table
->row_count
; i
++ )
1307 r
= TABLE_fetch_int( (struct tagMSIVIEW
*) tv
, i
, col
, &x
);
1308 if ( r
!= ERROR_SUCCESS
)
1310 ERR("TABLE_fetch_int shouldn't fail here\n");
1316 return ERROR_SUCCESS
;
1319 return ERROR_FUNCTION_FAILED
;
1322 static UINT
table_validate_new( MSITABLEVIEW
*tv
, MSIRECORD
*rec
)
1325 UINT i
, val
, r
, row
;
1326 BOOL has_key
= FALSE
;
1328 /* FIXME: set the MsiViewGetError value */
1330 for( i
= 0; i
<tv
->num_cols
; i
++ )
1332 /* check for duplicate keys */
1333 if( !( tv
->columns
[i
].type
& MSITYPE_KEY
) )
1338 TRACE("column %d (%s.%s)is a key\n", i
,
1339 debugstr_w(tv
->columns
[i
].tablename
),
1340 debugstr_w(tv
->columns
[i
].colname
) );
1343 if( tv
->columns
[i
].type
& MSITYPE_STRING
)
1345 /* keys can't be null */
1346 str
= MSI_RecordGetString( rec
, i
+1 );
1348 return ERROR_INVALID_DATA
;
1350 /* if the string doesn't exist in the string table yet, it's OK */
1351 r
= msi_string2idW( tv
->db
->strings
, str
, &val
);
1352 if( ERROR_SUCCESS
!= r
)
1353 return ERROR_SUCCESS
;
1357 val
= MSI_RecordGetInteger( rec
, i
+1 );
1361 /* if we find the same value in the table, it's a duplicate */
1363 r
= table_find_in_column( tv
, i
+1, val
, &row
);
1364 if( ERROR_SUCCESS
!= r
)
1365 return ERROR_SUCCESS
;
1367 TRACE("found in row %d\n", row
);
1371 return ERROR_INVALID_DATA
;
1373 return ERROR_SUCCESS
;
1376 static UINT
TABLE_insert_row( struct tagMSIVIEW
*view
, MSIRECORD
*rec
)
1378 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1379 UINT n
, type
, val
, r
, row
, col_count
= 0;
1381 r
= TABLE_get_dimensions( view
, NULL
, &col_count
);
1386 r
= table_create_new_row( view
, &row
);
1387 TRACE("insert_row returned %08x\n", r
);
1391 for( n
= 1; n
<= col_count
; n
++ )
1393 r
= TABLE_get_column_info( view
, n
, NULL
, &type
);
1397 if( type
& MSITYPE_STRING
)
1399 const WCHAR
*str
= MSI_RecordGetString( rec
, n
);
1400 val
= msi_addstringW( tv
->db
->strings
, 0, str
, -1, 1 );
1404 val
= MSI_RecordGetInteger( rec
, n
);
1407 r
= TABLE_set_int( view
, row
, n
, val
);
1415 static UINT
TABLE_modify( struct tagMSIVIEW
*view
, MSIMODIFY eModifyMode
,
1418 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1421 TRACE("%p %d %p\n", view
, eModifyMode
, rec
);
1425 r
= TABLE_execute( view
, NULL
);
1430 switch (eModifyMode
)
1432 case MSIMODIFY_VALIDATE_NEW
:
1433 r
= table_validate_new( tv
, rec
);
1436 case MSIMODIFY_INSERT_TEMPORARY
:
1437 r
= table_validate_new( tv
, rec
);
1438 if (r
!= ERROR_SUCCESS
)
1440 r
= TABLE_insert_row( view
, rec
);
1443 case MSIMODIFY_REFRESH
:
1444 case MSIMODIFY_INSERT
:
1445 case MSIMODIFY_UPDATE
:
1446 case MSIMODIFY_ASSIGN
:
1447 case MSIMODIFY_REPLACE
:
1448 case MSIMODIFY_MERGE
:
1449 case MSIMODIFY_DELETE
:
1450 case MSIMODIFY_VALIDATE
:
1451 case MSIMODIFY_VALIDATE_FIELD
:
1452 case MSIMODIFY_VALIDATE_DELETE
:
1453 FIXME("%p %d %p - mode not implemented\n", view
, eModifyMode
, rec
);
1454 r
= ERROR_CALL_NOT_IMPLEMENTED
;
1458 r
= ERROR_INVALID_DATA
;
1464 static UINT
TABLE_delete( struct tagMSIVIEW
*view
)
1466 MSITABLEVIEW
*tv
= (MSITABLEVIEW
*)view
;
1468 TRACE("%p\n", view
);
1471 release_table( tv
->db
, tv
->table
);
1477 for( i
=0; i
<tv
->num_cols
; i
++)
1479 msi_free( tv
->columns
[i
].colname
);
1480 msi_free( tv
->columns
[i
].tablename
);
1482 msi_free( tv
->columns
);
1488 return ERROR_SUCCESS
;
1492 MSIVIEWOPS table_ops
=
1500 TABLE_get_dimensions
,
1501 TABLE_get_column_info
,
1506 UINT
TABLE_CreateView( MSIDATABASE
*db
, LPCWSTR name
, MSIVIEW
**view
)
1509 UINT r
, sz
, column_count
;
1510 MSICOLUMNINFO
*columns
, *last_col
;
1512 TRACE("%p %s %p\n", db
, debugstr_w(name
), view
);
1514 /* get the number of columns in this table */
1516 r
= get_tablecolumns( db
, name
, NULL
, &column_count
);
1517 if( r
!= ERROR_SUCCESS
)
1520 /* if there's no columns, there's no table */
1521 if( column_count
== 0 )
1522 return ERROR_INVALID_PARAMETER
;
1524 TRACE("Table found\n");
1526 sz
= sizeof *tv
+ lstrlenW(name
)*sizeof name
[0] ;
1527 tv
= msi_alloc_zero( sz
);
1529 return ERROR_FUNCTION_FAILED
;
1531 columns
= msi_alloc( column_count
*sizeof (MSICOLUMNINFO
));
1535 return ERROR_FUNCTION_FAILED
;
1538 r
= get_tablecolumns( db
, name
, columns
, &column_count
);
1539 if( r
!= ERROR_SUCCESS
)
1541 msi_free( columns
);
1543 return ERROR_FUNCTION_FAILED
;
1546 TRACE("Table has %d columns\n", column_count
);
1548 last_col
= &columns
[column_count
-1];
1550 /* fill the structure */
1551 tv
->view
.ops
= &table_ops
;
1553 tv
->columns
= columns
;
1554 tv
->num_cols
= column_count
;
1556 tv
->row_size
= last_col
->offset
+ bytes_per_column( last_col
);
1558 TRACE("one row is %d bytes\n", tv
->row_size
);
1560 *view
= (MSIVIEW
*) tv
;
1561 lstrcpyW( tv
->name
, name
);
1563 return ERROR_SUCCESS
;
1566 UINT
MSI_CommitTables( MSIDATABASE
*db
)
1569 MSITABLE
*table
= NULL
;
1573 r
= save_string_table( db
);
1574 if( r
!= ERROR_SUCCESS
)
1576 WARN("failed to save string table r=%08x\n",r
);
1580 LIST_FOR_EACH_ENTRY( table
, &db
->tables
, MSITABLE
, entry
)
1582 r
= save_table( db
, table
);
1583 if( r
!= ERROR_SUCCESS
)
1585 WARN("failed to save table %s (r=%08x)\n",
1586 debugstr_w(table
->name
), r
);
1591 /* force everything to reload next time */
1592 free_cached_tables( db
);
1594 return ERROR_SUCCESS
;