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"
38 #include "wine/unicode.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(msi);
44 typedef struct tagMSICOLUMNINFO
58 struct tagMSITABLE *next;
59 struct tagMSITABLE *prev;
63 #define MAX_STREAM_NAME 0x1f
65 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
66 MSICOLUMNINFO **pcols, UINT *pcount );
67 static UINT get_tablecolumns( MSIDATABASE *db,
68 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
70 static inline UINT bytes_per_column( MSICOLUMNINFO *col )
72 if( col->type & MSITYPE_STRING )
74 if( (col->type & 0xff) > 4 )
75 ERR("Invalid column size!\n");
76 return col->type & 0xff;
79 static int utf2mime(int x)
81 if( (x>='0') && (x<='9') )
83 if( (x>='A') && (x<='Z') )
85 if( (x>='a') && (x<='z') )
94 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
96 DWORD count = MAX_STREAM_NAME;
101 count = strlenW( in )+2;
102 out = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
118 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
120 ch = utf2mime(ch) + 0x4800;
122 if( next && (next<0x80) )
124 next = utf2mime(next);
135 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
136 HeapFree( GetProcessHeap(), 0, out );
140 static int mime2utf(int x)
147 return x - 10 - 26 + 'a';
148 if( x == (10+26+26) )
153 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
158 while ( (ch = *in++) )
160 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
163 ch = mime2utf(ch-0x4800);
167 *out++ = mime2utf(ch&0x3f);
169 ch = mime2utf((ch>>6)&0x3f);
179 void enum_stream_names( IStorage *stg )
181 IEnumSTATSTG *stgenum = NULL;
187 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
195 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
196 if( FAILED( r ) || !count )
198 decode_streamname( stat.pwcsName, name );
199 ERR("stream %2ld -> %s %s\n", n,
200 debugstr_w(stat.pwcsName), debugstr_w(name) );
204 IEnumSTATSTG_Release( stgenum );
207 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
208 USHORT **pdata, UINT *psz )
211 UINT ret = ERROR_FUNCTION_FAILED;
218 encname = encode_streamname(TRUE, stname);
220 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
222 r = IStorage_OpenStream(stg, encname, NULL,
223 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
224 HeapFree( GetProcessHeap(), 0, encname );
227 WARN("open stream failed r = %08lx - empty table?\n",r);
231 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
234 ERR("open stream failed r = %08lx!\n",r);
238 if( stat.cbSize.QuadPart >> 32 )
244 sz = stat.cbSize.QuadPart;
245 data = HeapAlloc( GetProcessHeap(), 0, sz );
248 ERR("couldn't allocate memory r=%08lx!\n",r);
249 ret = ERROR_NOT_ENOUGH_MEMORY;
253 r = IStream_Read(stm, data, sz, &count );
254 if( FAILED( r ) || ( count != sz ) )
256 HeapFree( GetProcessHeap(), 0, data );
257 ERR("read stream failed r = %08lx!\n",r);
266 IStream_Release( stm );
271 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
276 encname = encode_streamname(FALSE, stname);
278 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
280 r = IStorage_OpenStream(db->storage, encname, NULL,
281 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
282 HeapFree( GetProcessHeap(), 0, encname );
285 WARN("open stream failed r = %08lx - empty table?\n",r);
286 return ERROR_FUNCTION_FAILED;
289 return ERROR_SUCCESS;
292 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
293 USHORT **pdata, UINT *psz )
296 UINT ret = ERROR_FUNCTION_FAILED;
302 r = db_get_raw_stream( db, stname, &stm );
303 if( r != ERROR_SUCCESS)
305 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
308 ERR("open stream failed r = %08lx!\n",r);
312 if( stat.cbSize.QuadPart >> 32 )
318 sz = stat.cbSize.QuadPart;
319 data = HeapAlloc( GetProcessHeap(), 0, sz );
322 ERR("couldn't allocate memory r=%08lx!\n",r);
323 ret = ERROR_NOT_ENOUGH_MEMORY;
327 r = IStream_Read(stm, data, sz, &count );
328 if( FAILED( r ) || ( count != sz ) )
330 HeapFree( GetProcessHeap(), 0, data );
331 ERR("read stream failed r = %08lx!\n",r);
340 IStream_Release( stm );
345 static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
346 LPVOID data, UINT sz )
349 UINT ret = ERROR_FUNCTION_FAILED;
356 encname = encode_streamname(TRUE, stname );
357 r = IStorage_OpenStream( stg, encname, NULL,
358 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
359 HeapFree( GetProcessHeap(), 0, encname );
362 r = IStorage_CreateStream( stg, encname,
363 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
367 ERR("open stream failed r = %08lx\n",r);
372 r = IStream_SetSize( stm, size );
375 ERR("Failed to SetSize\n");
380 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
383 ERR("Failed to Seek\n");
387 r = IStream_Write(stm, data, sz, &count );
388 if( FAILED( r ) || ( count != sz ) )
390 ERR("Failed to Write\n");
397 IStream_Release( stm );
402 UINT read_table_from_storage( MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
405 USHORT *rawdata = NULL;
406 UINT rawsize = 0, r, i, j, row_size = 0, num_cols = 0;
407 MSICOLUMNINFO *cols, *last_col;
409 TRACE("%s\n",debugstr_w(name));
411 /* non-existing tables should be interpretted as empty tables */
412 t = HeapAlloc( GetProcessHeap(), 0,
413 sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
415 return ERROR_NOT_ENOUGH_MEMORY;
417 r = table_get_column_info( db, name, &cols, &num_cols );
418 if( r != ERROR_SUCCESS )
420 HeapFree( GetProcessHeap(), 0, t );
423 last_col = &cols[num_cols-1];
424 row_size = last_col->offset + bytes_per_column( last_col );
428 lstrcpyW( t->name, name );
432 /* if we can't read the table, just assume that it's empty */
433 read_stream_data( db->storage, name, &rawdata, &rawsize );
435 return ERROR_SUCCESS;
437 TRACE("Read %d bytes\n", rawsize );
439 if( rawsize % row_size )
441 ERR("Table size is invalid %d/%d\n", rawsize, row_size );
442 return ERROR_FUNCTION_FAILED;
445 t->row_count = rawsize / row_size;
446 t->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
447 t->row_count * sizeof (USHORT*) );
449 return ERROR_NOT_ENOUGH_MEMORY; /* FIXME: memory leak */
451 /* transpose all the data */
452 TRACE("Transposing data from %d columns\n", t->row_count );
453 for( i=0; i<t->row_count; i++ )
455 t->data[i] = HeapAlloc( GetProcessHeap(), 0, row_size );
457 return ERROR_NOT_ENOUGH_MEMORY; /* FIXME: memory leak */
458 for( j=0; j<num_cols; j++ )
460 UINT ofs = cols[j].offset/2;
461 UINT n = bytes_per_column( &cols[j] );
466 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
469 t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
470 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
473 ERR("oops - unknown column width %d\n", n);
474 return ERROR_FUNCTION_FAILED;
479 HeapFree( GetProcessHeap(), 0, cols );
480 HeapFree( GetProcessHeap(), 0, rawdata );
482 return ERROR_SUCCESS;
485 /* add this table to the list of cached tables in the database */
486 void add_table(MSIDATABASE *db, MSITABLE *table)
488 table->next = db->first_table;
490 if( db->first_table )
491 db->first_table->prev = table;
493 db->last_table = table;
494 db->first_table = table;
497 /* remove from the list of cached tables */
498 void remove_table( MSIDATABASE *db, MSITABLE *table )
501 table->next->prev = table->prev;
503 db->last_table = table->prev;
505 table->prev->next = table->next;
507 db->first_table = table->next;
512 void release_table( MSIDATABASE *db, MSITABLE *table )
514 if( !table->ref_count )
515 ERR("Trying to destroy table with refcount 0\n");
517 if( !table->ref_count )
519 remove_table( db, table );
520 HeapFree( GetProcessHeap(), 0, table->data );
521 HeapFree( GetProcessHeap(), 0, table );
522 TRACE("Destroyed table %s\n", debugstr_w(table->name));
526 void free_cached_tables( MSIDATABASE *db )
528 while( db->first_table )
530 MSITABLE *t = db->first_table;
532 if ( --t->ref_count )
533 ERR("table ref count not zero for %s\n", debugstr_w(t->name));
534 remove_table( db, t );
535 HeapFree( GetProcessHeap(), 0, t->data );
536 HeapFree( GetProcessHeap(), 0, t );
540 UINT find_cached_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
544 for( t = db->first_table; t; t=t->next )
546 if( !lstrcmpW( name, t->name ) )
549 return ERROR_SUCCESS;
553 return ERROR_FUNCTION_FAILED;
556 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
558 UINT r, column_count;
559 MSICOLUMNINFO *columns;
561 /* get the number of columns in this table */
563 r = get_tablecolumns( db, name, NULL, &column_count );
564 if( r != ERROR_SUCCESS )
567 /* if there's no columns, there's no table */
568 if( column_count == 0 )
569 return ERROR_INVALID_PARAMETER;
571 TRACE("Table %s found\n", debugstr_w(name) );
573 columns = HeapAlloc( GetProcessHeap(), 0, column_count*sizeof (MSICOLUMNINFO));
575 return ERROR_FUNCTION_FAILED;
577 r = get_tablecolumns( db, name, columns, &column_count );
578 if( r != ERROR_SUCCESS )
580 HeapFree( GetProcessHeap(), 0, columns );
581 return ERROR_FUNCTION_FAILED;
585 *pcount = column_count;
590 UINT get_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
596 /* first, see if the table is cached */
597 r = find_cached_table( db, name, ptable );
598 if( r == ERROR_SUCCESS )
600 (*ptable)->ref_count++;
604 r = read_table_from_storage( db, name, ptable );
605 if( r != ERROR_SUCCESS )
608 /* add the table to the list */
609 add_table( db, *ptable );
610 (*ptable)->ref_count++;
612 return ERROR_SUCCESS;
615 UINT save_table( MSIDATABASE *db, MSITABLE *t )
617 USHORT *rawdata = NULL, *p;
618 UINT rawsize, r, i, j, row_size, num_cols = 0;
619 MSICOLUMNINFO *cols, *last_col;
621 TRACE("Saving %s\n", debugstr_w( t->name ) );
623 r = table_get_column_info( db, t->name, &cols, &num_cols );
624 if( r != ERROR_SUCCESS )
627 last_col = &cols[num_cols-1];
628 row_size = last_col->offset + bytes_per_column( last_col );
630 rawsize = t->row_count * row_size;
631 rawdata = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, rawsize );
633 return ERROR_NOT_ENOUGH_MEMORY;
636 for( i=0; i<num_cols; i++ )
638 for( j=0; j<t->row_count; j++ )
640 UINT offset = cols[i].offset;
642 *p++ = t->data[j][offset/2];
643 if( 4 == bytes_per_column( &cols[i] ) )
644 *p++ = t->data[j][offset/2+1];
648 TRACE("writing %d bytes\n", rawsize);
649 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
651 HeapFree( GetProcessHeap(), 0, rawdata );
656 HRESULT init_string_table( IStorage *stg )
659 static const WCHAR szStringData[] = {
660 '_','S','t','r','i','n','g','D','a','t','a',0 };
661 static const WCHAR szStringPool[] = {
662 '_','S','t','r','i','n','g','P','o','o','l',0 };
663 USHORT zero[2] = { 0, 0 };
668 encname = encode_streamname(TRUE, szStringPool );
670 /* create the StringPool stream... add the zero string to it*/
671 r = IStorage_CreateStream( stg, encname,
672 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
673 HeapFree( GetProcessHeap(), 0, encname );
680 r = IStream_Write(stm, zero, sizeof zero, &count );
681 IStream_Release( stm );
683 if( FAILED( r ) || ( count != sizeof zero ) )
689 /* create the StringData stream... make it zero length */
690 encname = encode_streamname(TRUE, szStringData );
691 r = IStorage_CreateStream( stg, encname,
692 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
693 HeapFree( GetProcessHeap(), 0, encname );
699 IStream_Release( stm );
704 UINT load_string_table( MSIDATABASE *db )
708 UINT r, ret = ERROR_FUNCTION_FAILED, datasize = 0, poolsize = 0, codepage;
709 DWORD i, count, offset, len, n;
710 static const WCHAR szStringData[] = {
711 '_','S','t','r','i','n','g','D','a','t','a',0 };
712 static const WCHAR szStringPool[] = {
713 '_','S','t','r','i','n','g','P','o','o','l',0 };
717 msi_destroy_stringtable( db->strings );
721 r = read_stream_data( db->storage, szStringPool, &pool, &poolsize );
722 if( r != ERROR_SUCCESS)
724 r = read_stream_data( db->storage, szStringData, (USHORT**)&data, &datasize );
725 if( r != ERROR_SUCCESS)
730 codepage = pool[0] | ( pool[1] << 16 );
733 db->strings = msi_init_stringtable( count, codepage );
736 for( i=1; i<count; i++ )
739 n = msi_addstring( db->strings, i, data+offset, len, pool[i*2+1] );
741 ERR("Failed to add string %ld\n", i );
745 TRACE("Loaded %ld strings\n", count);
751 HeapFree( GetProcessHeap(), 0, pool );
753 HeapFree( GetProcessHeap(), 0, data );
758 UINT save_string_table( MSIDATABASE *db )
760 UINT i, count, datasize, poolsize, sz, used, r, codepage;
761 UINT ret = ERROR_FUNCTION_FAILED;
762 static const WCHAR szStringData[] = {
763 '_','S','t','r','i','n','g','D','a','t','a',0 };
764 static const WCHAR szStringPool[] = {
765 '_','S','t','r','i','n','g','P','o','o','l',0 };
771 /* construct the new table in memory first */
772 datasize = msi_string_totalsize( db->strings, &count );
773 poolsize = count*2*sizeof(USHORT);
775 pool = HeapAlloc( GetProcessHeap(), 0, poolsize );
778 ERR("Failed to alloc pool %d bytes\n", poolsize );
781 data = HeapAlloc( GetProcessHeap(), 0, datasize );
784 ERR("Failed to alloc data %d bytes\n", poolsize );
789 codepage = msi_string_get_codepage( db->strings );
790 pool[0]=codepage&0xffff;
791 pool[1]=(codepage>>16);
792 for( i=1; i<count; i++ )
794 sz = datasize - used;
795 r = msi_id2stringA( db->strings, i, data+used, &sz );
796 if( r != ERROR_SUCCESS )
798 ERR("failed to fetch string\n");
801 if( sz && (sz < (datasize - used ) ) )
803 TRACE("adding %u bytes %s\n", sz, data+used );
805 pool[ i*2 + 1 ] = msi_id_refcount( db->strings, i );
807 if( used > datasize )
809 ERR("oops overran %d >= %d\n", used, datasize);
814 if( used != datasize )
816 ERR("oops used %d != datasize %d\n", used, datasize);
820 /* write the streams */
821 r = write_stream_data( db->storage, szStringData, data, datasize );
822 TRACE("Wrote StringData r=%08x\n", r);
825 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
826 TRACE("Wrote StringPool r=%08x\n", r);
834 HeapFree( GetProcessHeap(), 0, data );
836 HeapFree( GetProcessHeap(), 0, pool );
841 static LPWSTR strdupW( LPCWSTR str )
843 UINT len = lstrlenW( str ) + 1;
844 LPWSTR ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
846 lstrcpyW( ret, str );
850 /* information for default tables */
851 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
852 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
853 static const WCHAR szName[] = { 'N','a','m','e',0 };
854 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
855 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
856 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
857 static const WCHAR szType[] = { 'T','y','p','e',0 };
859 struct standard_table {
864 } MSI_standard_tables[] =
866 { szTables, szName, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
867 { szColumns, szTable, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
868 { szColumns, szNumber, 2, MSITYPE_VALID | 2},
869 { szColumns, szName, 3, MSITYPE_VALID | MSITYPE_STRING | 32},
870 { szColumns, szType, 4, MSITYPE_VALID | 2},
873 #define STANDARD_TABLE_COUNT \
874 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
876 UINT get_defaulttablecolumns( LPCWSTR szTable, MSICOLUMNINFO *colinfo, UINT *sz)
880 for(i=0; i<STANDARD_TABLE_COUNT; i++)
882 if( lstrcmpW( szTable, MSI_standard_tables[i].tablename ) )
884 if(colinfo && (n < *sz) )
886 colinfo[n].tablename = strdupW(MSI_standard_tables[i].tablename);
887 colinfo[n].colname = strdupW(MSI_standard_tables[i].columnname);
888 colinfo[n].number = MSI_standard_tables[i].number;
889 colinfo[n].type = MSI_standard_tables[i].type;
890 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
891 debugstr_w(colinfo[n].colname)); */
893 colinfo[n].offset = colinfo[n-1].offset
894 + bytes_per_column( &colinfo[n-1] );
896 colinfo[n].offset = 0;
899 if( colinfo && (n >= *sz) )
903 return ERROR_SUCCESS;
906 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
911 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
912 if( r != ERROR_SUCCESS )
914 str = HeapAlloc( GetProcessHeap(), 0, sz*sizeof (WCHAR));
917 r = msi_id2stringW( db->strings, stringid, str, &sz );
918 if( r == ERROR_SUCCESS )
920 HeapFree( GetProcessHeap(), 0, str );
924 static UINT get_tablecolumns( MSIDATABASE *db,
925 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
927 UINT r, i, n=0, table_id, count, maxcount = *sz;
928 MSITABLE *table = NULL;
929 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
931 /* first check if there is a default table with that name */
932 r = get_defaulttablecolumns( szTableName, colinfo, sz );
933 if( ( r == ERROR_SUCCESS ) && *sz )
936 r = get_table( db, szColumns, &table);
937 if( r != ERROR_SUCCESS )
939 ERR("table %s not available\n", debugstr_w(szColumns));
943 /* convert table and column names to IDs from the string table */
944 r = msi_string2idW( db->strings, szTableName, &table_id );
945 if( r != ERROR_SUCCESS )
947 release_table( db, table );
948 ERR("Couldn't find id for %s\n", debugstr_w(szTableName));
952 TRACE("Table id is %d\n", table_id);
954 count = table->row_count;
955 for( i=0; i<count; i++ )
957 if( table->data[ i ][ 0 ] != table_id )
961 UINT id = table->data[ i ] [ 2 ];
962 colinfo[n].tablename = MSI_makestring( db, table_id );
963 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
964 colinfo[n].colname = MSI_makestring( db, id );
965 colinfo[n].type = table->data[ i ] [ 3 ];
966 /* this assumes that columns are in order in the table */
968 colinfo[n].offset = colinfo[n-1].offset
969 + bytes_per_column( &colinfo[n-1] );
971 colinfo[n].offset = 0;
972 TRACE("table %s column %d is [%s] (%d) with type %08x "
973 "offset %d at row %d\n", debugstr_w(szTableName),
974 colinfo[n].number, debugstr_w(colinfo[n].colname),
975 id, colinfo[n].type, colinfo[n].offset, i);
976 if( n != (colinfo[n].number-1) )
978 ERR("oops. data in the _Columns table isn't in the right "
979 "order for table %s\n", debugstr_w(szTableName));
980 return ERROR_FUNCTION_FAILED;
984 if( colinfo && ( n >= maxcount ) )
989 release_table( db, table );
991 return ERROR_SUCCESS;
994 /* try to find the table name in the _Tables table */
995 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
997 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
998 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
999 UINT r, table_id = 0, i, count;
1000 MSITABLE *table = NULL;
1002 if( !lstrcmpW( name, szTables ) )
1004 if( !lstrcmpW( name, szColumns ) )
1007 r = msi_string2idW( db->strings, name, &table_id );
1008 if( r != ERROR_SUCCESS )
1010 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1014 r = get_table( db, szTables, &table);
1015 if( r != ERROR_SUCCESS )
1017 ERR("table %s not available\n", debugstr_w(szTables));
1021 /* count = table->size/2; */
1022 count = table->row_count;
1023 for( i=0; i<count; i++ )
1024 if( table->data[ i ][ 0 ] == table_id )
1027 release_table( db, table );
1032 ERR("Searched %d tables, but %d was not found\n", count, table_id );
1037 /* below is the query interface to a table */
1039 typedef struct tagMSITABLEVIEW
1044 MSICOLUMNINFO *columns;
1050 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1052 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1053 UINT offset, num_rows, n;
1056 return ERROR_INVALID_PARAMETER;
1058 if( (col==0) || (col>tv->num_cols) )
1059 return ERROR_INVALID_PARAMETER;
1061 /* how many rows are there ? */
1062 num_rows = tv->table->row_count;
1063 if( row >= num_rows )
1064 return ERROR_NO_MORE_ITEMS;
1066 if( tv->columns[col-1].offset >= tv->row_size )
1068 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1069 ERR("%p %p\n", tv, tv->columns );
1070 return ERROR_FUNCTION_FAILED;
1073 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1074 n = bytes_per_column( &tv->columns[col-1] );
1078 offset = tv->columns[col-1].offset/2;
1079 *val = tv->table->data[row][offset] +
1080 (tv->table->data[row][offset + 1] << 16);
1083 offset = tv->columns[col-1].offset/2;
1084 *val = tv->table->data[row][offset];
1087 ERR("oops! what is %d bytes per column?\n", n );
1088 return ERROR_FUNCTION_FAILED;
1091 TRACE("Data [%d][%d] = %d \n", row, col, *val );
1093 return ERROR_SUCCESS;
1097 * We need a special case for streams, as we need to reference column with
1098 * the name of the stream in the same table, and the table name
1099 * which may not be available at higher levels of the query
1101 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1103 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1104 UINT ival = 0, refcol = 0, r;
1108 static const WCHAR szDot[] = { '.', 0 };
1110 if( !view->ops->fetch_int )
1111 return ERROR_INVALID_PARAMETER;
1114 * The column marked with the type stream data seems to have a single number
1115 * which references the column containing the name of the stream data
1117 * Fetch the column to reference first.
1119 r = view->ops->fetch_int( view, row, col, &ival );
1120 if( r != ERROR_SUCCESS )
1123 /* now get the column with the name of the stream */
1124 r = view->ops->fetch_int( view, row, ival, &refcol );
1125 if( r != ERROR_SUCCESS )
1128 /* lookup the string value from the string table */
1129 sval = MSI_makestring( tv->db, refcol );
1131 return ERROR_INVALID_PARAMETER;
1133 len = strlenW( tv->name ) + 2 + strlenW( sval );
1134 full_name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1135 strcpyW( full_name, tv->name );
1136 strcatW( full_name, szDot );
1137 strcatW( full_name, sval );
1139 r = db_get_raw_stream( tv->db, full_name, stm );
1141 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1142 HeapFree( GetProcessHeap(), 0, full_name );
1143 HeapFree( GetProcessHeap(), 0, sval );
1148 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1150 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1154 return ERROR_INVALID_PARAMETER;
1156 if( (col==0) || (col>tv->num_cols) )
1157 return ERROR_INVALID_PARAMETER;
1159 if( tv->columns[col-1].offset >= tv->row_size )
1161 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1162 ERR("%p %p\n", tv, tv->columns );
1163 return ERROR_FUNCTION_FAILED;
1166 n = bytes_per_column( &tv->columns[col-1] );
1170 offset = tv->columns[col-1].offset/2;
1171 tv->table->data[row][offset] = val & 0xffff;
1172 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1175 offset = tv->columns[col-1].offset/2;
1176 tv->table->data[row][offset] = val;
1179 ERR("oops! what is %d bytes per column?\n", n );
1180 return ERROR_FUNCTION_FAILED;
1182 return ERROR_SUCCESS;
1185 UINT TABLE_insert_row( struct tagMSIVIEW *view, UINT *num )
1187 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1191 TRACE("%p\n", view);
1194 return ERROR_INVALID_PARAMETER;
1196 row = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, tv->row_size );
1198 return ERROR_NOT_ENOUGH_MEMORY;
1200 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1201 if( tv->table->data )
1202 p = HeapReAlloc( GetProcessHeap(), 0, tv->table->data, sz );
1204 p = HeapAlloc( GetProcessHeap(), 0, sz );
1207 HeapFree( GetProcessHeap(), 0, row );
1208 return ERROR_NOT_ENOUGH_MEMORY;
1211 tv->table->data = p;
1212 tv->table->data[tv->table->row_count] = row;
1213 *num = tv->table->row_count;
1214 tv->table->row_count++;
1216 return ERROR_SUCCESS;
1219 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1221 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1224 TRACE("%p %p\n", tv, record);
1227 return ERROR_FUNCTION_FAILED;
1229 r = get_table( tv->db, tv->name, &tv->table );
1230 if( r != ERROR_SUCCESS )
1233 return ERROR_SUCCESS;
1236 static UINT TABLE_close( struct tagMSIVIEW *view )
1238 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1240 TRACE("%p\n", view );
1243 return ERROR_FUNCTION_FAILED;
1245 release_table( tv->db, tv->table );
1248 return ERROR_SUCCESS;
1251 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1253 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1255 TRACE("%p %p %p\n", view, rows, cols );
1258 *cols = tv->num_cols;
1262 return ERROR_INVALID_PARAMETER;
1263 *rows = tv->table->row_count;
1266 return ERROR_SUCCESS;
1269 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1270 UINT n, LPWSTR *name, UINT *type )
1272 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1274 TRACE("%p %d %p %p\n", tv, n, name, type );
1276 if( ( n == 0 ) || ( n > tv->num_cols ) )
1277 return ERROR_INVALID_PARAMETER;
1281 *name = strdupW( tv->columns[n-1].colname );
1283 return ERROR_FUNCTION_FAILED;
1286 *type = tv->columns[n-1].type;
1288 return ERROR_SUCCESS;
1291 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIHANDLE hrec)
1293 FIXME("%p %d %ld\n", view, eModifyMode, hrec );
1294 return ERROR_CALL_NOT_IMPLEMENTED;
1297 static UINT TABLE_delete( struct tagMSIVIEW *view )
1299 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1301 TRACE("%p\n", view );
1304 release_table( tv->db, tv->table );
1310 for( i=0; i<tv->num_cols; i++)
1312 HeapFree( GetProcessHeap(), 0, tv->columns[i].colname );
1313 HeapFree( GetProcessHeap(), 0, tv->columns[i].tablename );
1315 HeapFree( GetProcessHeap(), 0, tv->columns );
1319 HeapFree( GetProcessHeap(), 0, tv );
1321 return ERROR_SUCCESS;
1325 MSIVIEWOPS table_ops =
1333 TABLE_get_dimensions,
1334 TABLE_get_column_info,
1339 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1342 UINT r, sz, column_count;
1343 MSICOLUMNINFO *columns, *last_col;
1345 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1347 /* get the number of columns in this table */
1349 r = get_tablecolumns( db, name, NULL, &column_count );
1350 if( r != ERROR_SUCCESS )
1353 /* if there's no columns, there's no table */
1354 if( column_count == 0 )
1355 return ERROR_INVALID_PARAMETER;
1357 TRACE("Table found\n");
1359 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1360 tv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sz );
1362 return ERROR_FUNCTION_FAILED;
1364 columns = HeapAlloc( GetProcessHeap(), 0, column_count*sizeof (MSICOLUMNINFO));
1367 HeapFree( GetProcessHeap(), 0, tv );
1368 return ERROR_FUNCTION_FAILED;
1371 r = get_tablecolumns( db, name, columns, &column_count );
1372 if( r != ERROR_SUCCESS )
1374 HeapFree( GetProcessHeap(), 0, columns );
1375 HeapFree( GetProcessHeap(), 0, tv );
1376 return ERROR_FUNCTION_FAILED;
1379 TRACE("Table has %d columns\n", column_count);
1381 last_col = &columns[column_count-1];
1383 /* fill the structure */
1384 tv->view.ops = &table_ops;
1386 tv->columns = columns;
1387 tv->num_cols = column_count;
1389 tv->row_size = last_col->offset + bytes_per_column( last_col );
1391 TRACE("one row is %d bytes\n", tv->row_size );
1393 *view = (MSIVIEW*) tv;
1394 lstrcpyW( tv->name, name );
1396 return ERROR_SUCCESS;
1399 UINT MSI_CommitTables( MSIDATABASE *db )
1402 MSITABLE *table = NULL;
1406 r = save_string_table( db );
1407 if( r != ERROR_SUCCESS )
1409 ERR("failed to save string table r=%08x\n",r);
1413 for( table = db->first_table; table; table = table->next )
1415 r = save_table( db, table );
1416 if( r != ERROR_SUCCESS )
1418 ERR("failed to save table %s (r=%08x)\n",
1419 debugstr_w(table->name), r);
1424 /* force everything to reload next time */
1425 free_cached_tables( db );
1427 return ERROR_SUCCESS;