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 TRACE("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);
361 r = IStorage_CreateStream( stg, encname,
362 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
364 HeapFree( GetProcessHeap(), 0, encname );
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 /* nonexistent tables should be interpreted 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);
750 HeapFree( GetProcessHeap(), 0, pool );
751 HeapFree( GetProcessHeap(), 0, data );
756 UINT save_string_table( MSIDATABASE *db )
758 UINT i, count, datasize, poolsize, sz, used, r, codepage;
759 UINT ret = ERROR_FUNCTION_FAILED;
760 static const WCHAR szStringData[] = {
761 '_','S','t','r','i','n','g','D','a','t','a',0 };
762 static const WCHAR szStringPool[] = {
763 '_','S','t','r','i','n','g','P','o','o','l',0 };
769 /* construct the new table in memory first */
770 datasize = msi_string_totalsize( db->strings, &count );
771 poolsize = count*2*sizeof(USHORT);
773 pool = HeapAlloc( GetProcessHeap(), 0, poolsize );
776 ERR("Failed to alloc pool %d bytes\n", poolsize );
779 data = HeapAlloc( GetProcessHeap(), 0, datasize );
782 ERR("Failed to alloc data %d bytes\n", poolsize );
787 codepage = msi_string_get_codepage( db->strings );
788 pool[0]=codepage&0xffff;
789 pool[1]=(codepage>>16);
790 for( i=1; i<count; i++ )
792 sz = datasize - used;
793 r = msi_id2stringA( db->strings, i, data+used, &sz );
794 if( r != ERROR_SUCCESS )
796 ERR("failed to fetch string\n");
799 if( sz && (sz < (datasize - used ) ) )
801 TRACE("adding %u bytes %s\n", sz, data+used );
803 pool[ i*2 + 1 ] = msi_id_refcount( db->strings, i );
805 if( used > datasize )
807 ERR("oops overran %d >= %d\n", used, datasize);
812 if( used != datasize )
814 ERR("oops used %d != datasize %d\n", used, datasize);
818 /* write the streams */
819 r = write_stream_data( db->storage, szStringData, data, datasize );
820 TRACE("Wrote StringData r=%08x\n", r);
823 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
824 TRACE("Wrote StringPool r=%08x\n", r);
831 HeapFree( GetProcessHeap(), 0, data );
832 HeapFree( GetProcessHeap(), 0, pool );
837 static LPWSTR strdupW( LPCWSTR str )
839 UINT len = lstrlenW( str ) + 1;
840 LPWSTR ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
842 lstrcpyW( ret, str );
846 /* information for default tables */
847 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
848 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
849 static const WCHAR szName[] = { 'N','a','m','e',0 };
850 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
851 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
852 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
853 static const WCHAR szType[] = { 'T','y','p','e',0 };
855 struct standard_table {
860 } MSI_standard_tables[] =
862 { szTables, szName, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
863 { szColumns, szTable, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
864 { szColumns, szNumber, 2, MSITYPE_VALID | 2},
865 { szColumns, szName, 3, MSITYPE_VALID | MSITYPE_STRING | 32},
866 { szColumns, szType, 4, MSITYPE_VALID | 2},
869 #define STANDARD_TABLE_COUNT \
870 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
872 UINT get_defaulttablecolumns( LPCWSTR szTable, MSICOLUMNINFO *colinfo, UINT *sz)
876 for(i=0; i<STANDARD_TABLE_COUNT; i++)
878 if( lstrcmpW( szTable, MSI_standard_tables[i].tablename ) )
880 if(colinfo && (n < *sz) )
882 colinfo[n].tablename = strdupW(MSI_standard_tables[i].tablename);
883 colinfo[n].colname = strdupW(MSI_standard_tables[i].columnname);
884 colinfo[n].number = MSI_standard_tables[i].number;
885 colinfo[n].type = MSI_standard_tables[i].type;
886 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
887 debugstr_w(colinfo[n].colname)); */
889 colinfo[n].offset = colinfo[n-1].offset
890 + bytes_per_column( &colinfo[n-1] );
892 colinfo[n].offset = 0;
895 if( colinfo && (n >= *sz) )
899 return ERROR_SUCCESS;
902 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
907 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
908 if( r != ERROR_SUCCESS )
910 str = HeapAlloc( GetProcessHeap(), 0, sz*sizeof (WCHAR));
913 r = msi_id2stringW( db->strings, stringid, str, &sz );
914 if( r == ERROR_SUCCESS )
916 HeapFree( GetProcessHeap(), 0, str );
920 static UINT get_tablecolumns( MSIDATABASE *db,
921 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
923 UINT r, i, n=0, table_id, count, maxcount = *sz;
924 MSITABLE *table = NULL;
925 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
927 /* first check if there is a default table with that name */
928 r = get_defaulttablecolumns( szTableName, colinfo, sz );
929 if( ( r == ERROR_SUCCESS ) && *sz )
932 r = get_table( db, szColumns, &table);
933 if( r != ERROR_SUCCESS )
935 ERR("table %s not available\n", debugstr_w(szColumns));
939 /* convert table and column names to IDs from the string table */
940 r = msi_string2idW( db->strings, szTableName, &table_id );
941 if( r != ERROR_SUCCESS )
943 release_table( db, table );
944 ERR("Couldn't find id for %s\n", debugstr_w(szTableName));
948 TRACE("Table id is %d\n", table_id);
950 count = table->row_count;
951 for( i=0; i<count; i++ )
953 if( table->data[ i ][ 0 ] != table_id )
957 UINT id = table->data[ i ] [ 2 ];
958 colinfo[n].tablename = MSI_makestring( db, table_id );
959 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
960 colinfo[n].colname = MSI_makestring( db, id );
961 colinfo[n].type = table->data[ i ] [ 3 ];
962 /* this assumes that columns are in order in the table */
964 colinfo[n].offset = colinfo[n-1].offset
965 + bytes_per_column( &colinfo[n-1] );
967 colinfo[n].offset = 0;
968 TRACE("table %s column %d is [%s] (%d) with type %08x "
969 "offset %d at row %d\n", debugstr_w(szTableName),
970 colinfo[n].number, debugstr_w(colinfo[n].colname),
971 id, colinfo[n].type, colinfo[n].offset, i);
972 if( n != (colinfo[n].number-1) )
974 ERR("oops. data in the _Columns table isn't in the right "
975 "order for table %s\n", debugstr_w(szTableName));
976 return ERROR_FUNCTION_FAILED;
980 if( colinfo && ( n >= maxcount ) )
985 release_table( db, table );
987 return ERROR_SUCCESS;
990 /* try to find the table name in the _Tables table */
991 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
993 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
994 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
995 UINT r, table_id = 0, i, count;
996 MSITABLE *table = NULL;
998 if( !lstrcmpW( name, szTables ) )
1000 if( !lstrcmpW( name, szColumns ) )
1003 r = msi_string2idW( db->strings, name, &table_id );
1004 if( r != ERROR_SUCCESS )
1006 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1010 r = get_table( db, szTables, &table);
1011 if( r != ERROR_SUCCESS )
1013 ERR("table %s not available\n", debugstr_w(szTables));
1017 /* count = table->size/2; */
1018 count = table->row_count;
1019 for( i=0; i<count; i++ )
1020 if( table->data[ i ][ 0 ] == table_id )
1023 release_table( db, table );
1028 ERR("Searched %d tables, but %d was not found\n", count, table_id );
1033 /* below is the query interface to a table */
1035 typedef struct tagMSITABLEVIEW
1040 MSICOLUMNINFO *columns;
1046 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1048 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1049 UINT offset, num_rows, n;
1052 return ERROR_INVALID_PARAMETER;
1054 if( (col==0) || (col>tv->num_cols) )
1055 return ERROR_INVALID_PARAMETER;
1057 /* how many rows are there ? */
1058 num_rows = tv->table->row_count;
1059 if( row >= num_rows )
1060 return ERROR_NO_MORE_ITEMS;
1062 if( tv->columns[col-1].offset >= tv->row_size )
1064 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1065 ERR("%p %p\n", tv, tv->columns );
1066 return ERROR_FUNCTION_FAILED;
1069 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1070 n = bytes_per_column( &tv->columns[col-1] );
1074 offset = tv->columns[col-1].offset/2;
1075 *val = tv->table->data[row][offset] +
1076 (tv->table->data[row][offset + 1] << 16);
1079 offset = tv->columns[col-1].offset/2;
1080 *val = tv->table->data[row][offset];
1083 ERR("oops! what is %d bytes per column?\n", n );
1084 return ERROR_FUNCTION_FAILED;
1087 /* TRACE("Data [%d][%d] = %d \n", row, col, *val ); */
1089 return ERROR_SUCCESS;
1093 * We need a special case for streams, as we need to reference column with
1094 * the name of the stream in the same table, and the table name
1095 * which may not be available at higher levels of the query
1097 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1099 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1100 UINT ival = 0, refcol = 0, r;
1104 static const WCHAR szDot[] = { '.', 0 };
1106 if( !view->ops->fetch_int )
1107 return ERROR_INVALID_PARAMETER;
1110 * The column marked with the type stream data seems to have a single number
1111 * which references the column containing the name of the stream data
1113 * Fetch the column to reference first.
1115 r = view->ops->fetch_int( view, row, col, &ival );
1116 if( r != ERROR_SUCCESS )
1119 /* now get the column with the name of the stream */
1120 r = view->ops->fetch_int( view, row, ival, &refcol );
1121 if( r != ERROR_SUCCESS )
1124 /* lookup the string value from the string table */
1125 sval = MSI_makestring( tv->db, refcol );
1127 return ERROR_INVALID_PARAMETER;
1129 len = strlenW( tv->name ) + 2 + strlenW( sval );
1130 full_name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1131 strcpyW( full_name, tv->name );
1132 strcatW( full_name, szDot );
1133 strcatW( full_name, sval );
1135 r = db_get_raw_stream( tv->db, full_name, stm );
1137 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1138 HeapFree( GetProcessHeap(), 0, full_name );
1139 HeapFree( GetProcessHeap(), 0, sval );
1144 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1146 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1150 return ERROR_INVALID_PARAMETER;
1152 if( (col==0) || (col>tv->num_cols) )
1153 return ERROR_INVALID_PARAMETER;
1155 if( tv->columns[col-1].offset >= tv->row_size )
1157 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1158 ERR("%p %p\n", tv, tv->columns );
1159 return ERROR_FUNCTION_FAILED;
1162 n = bytes_per_column( &tv->columns[col-1] );
1166 offset = tv->columns[col-1].offset/2;
1167 tv->table->data[row][offset] = val & 0xffff;
1168 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1171 offset = tv->columns[col-1].offset/2;
1172 tv->table->data[row][offset] = val;
1175 ERR("oops! what is %d bytes per column?\n", n );
1176 return ERROR_FUNCTION_FAILED;
1178 return ERROR_SUCCESS;
1181 UINT TABLE_insert_row( struct tagMSIVIEW *view, UINT *num )
1183 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1187 TRACE("%p\n", view);
1190 return ERROR_INVALID_PARAMETER;
1192 row = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, tv->row_size );
1194 return ERROR_NOT_ENOUGH_MEMORY;
1196 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1197 if( tv->table->data )
1198 p = HeapReAlloc( GetProcessHeap(), 0, tv->table->data, sz );
1200 p = HeapAlloc( GetProcessHeap(), 0, sz );
1203 HeapFree( GetProcessHeap(), 0, row );
1204 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);
1224 release_table( tv->db, tv->table );
1228 r = get_table( tv->db, tv->name, &tv->table );
1229 if( r != ERROR_SUCCESS )
1232 return ERROR_SUCCESS;
1235 static UINT TABLE_close( struct tagMSIVIEW *view )
1237 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1239 TRACE("%p\n", view );
1242 return ERROR_FUNCTION_FAILED;
1244 release_table( tv->db, tv->table );
1247 return ERROR_SUCCESS;
1250 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1252 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1254 TRACE("%p %p %p\n", view, rows, cols );
1257 *cols = tv->num_cols;
1261 return ERROR_INVALID_PARAMETER;
1262 *rows = tv->table->row_count;
1265 return ERROR_SUCCESS;
1268 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1269 UINT n, LPWSTR *name, UINT *type )
1271 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1273 TRACE("%p %d %p %p\n", tv, n, name, type );
1275 if( ( n == 0 ) || ( n > tv->num_cols ) )
1276 return ERROR_INVALID_PARAMETER;
1280 *name = strdupW( tv->columns[n-1].colname );
1282 return ERROR_FUNCTION_FAILED;
1285 *type = tv->columns[n-1].type;
1287 return ERROR_SUCCESS;
1290 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1293 FIXME("%p %d %p\n", view, eModifyMode, rec );
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;