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"
39 WINE_DEFAULT_DEBUG_CHANNEL(msi);
41 typedef struct tagMSICOLUMNINFO
55 struct tagMSITABLE *next;
56 struct tagMSITABLE *prev;
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 BOOL encode_streamname(BOOL bTable, LPCWSTR in, LPWSTR out)
93 DWORD count = MAX_STREAM_NAME;
109 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
111 ch = utf2mime(ch) + 0x4800;
113 if( next && (next<0x80) )
115 next = utf2mime(next);
129 static int mime2utf(int x)
136 return x - 10 - 26 + 'a';
137 if( x == (10+26+26) )
142 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
147 while ( (ch = *in++) )
149 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
152 ch = mime2utf(ch-0x4800);
156 *out++ = mime2utf(ch&0x3f);
158 ch = mime2utf((ch>>6)&0x3f);
168 void enum_stream_names( IStorage *stg )
170 IEnumSTATSTG *stgenum = NULL;
176 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
184 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
185 if( FAILED( r ) || !count )
187 decode_streamname( stat.pwcsName, name );
188 ERR("stream %2ld -> %s %s\n", n,
189 debugstr_w(stat.pwcsName), debugstr_w(name) );
193 IEnumSTATSTG_Release( stgenum );
196 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
197 USHORT **pdata, UINT *psz )
200 UINT ret = ERROR_FUNCTION_FAILED;
207 encode_streamname(TRUE, stname, encname);
209 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
211 r = IStorage_OpenStream(stg, encname, NULL,
212 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
215 WARN("open stream failed r = %08lx - empty table?\n",r);
219 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
222 ERR("open stream failed r = %08lx!\n",r);
226 if( stat.cbSize.QuadPart >> 32 )
232 sz = stat.cbSize.QuadPart;
233 data = HeapAlloc( GetProcessHeap(), 0, sz );
236 ERR("couldn't allocate memory r=%08lx!\n",r);
237 ret = ERROR_NOT_ENOUGH_MEMORY;
241 r = IStream_Read(stm, data, sz, &count );
242 if( FAILED( r ) || ( count != sz ) )
244 HeapFree( GetProcessHeap(), 0, data );
245 ERR("read stream failed r = %08lx!\n",r);
254 IStream_Release( stm );
259 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
264 encode_streamname(FALSE, stname, encname);
266 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
268 r = IStorage_OpenStream(db->storage, encname, NULL,
269 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
272 WARN("open stream failed r = %08lx - empty table?\n",r);
273 return ERROR_FUNCTION_FAILED;
276 return ERROR_SUCCESS;
279 /* FIXME: we should be passing around pointers to db structures internally */
280 UINT get_raw_stream( MSIHANDLE hdb, LPCWSTR stname, IStream **stm )
282 MSIDATABASE *db = msihandle2msiinfo(hdb, MSIHANDLETYPE_DATABASE );
285 return ERROR_INVALID_HANDLE;
287 return db_get_raw_stream( db, stname, stm );
290 UINT read_raw_stream_data( MSIHANDLE hdb, LPCWSTR stname,
291 USHORT **pdata, UINT *psz )
294 UINT ret = ERROR_FUNCTION_FAILED;
300 r = get_raw_stream( hdb, stname, &stm );
301 if( r != ERROR_SUCCESS)
303 ret = ERROR_FUNCTION_FAILED;
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;
356 encode_streamname(TRUE, stname, encname);
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);
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 encode_streamname(TRUE, szStringPool, encname);
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);
678 r = IStream_Write(stm, zero, sizeof zero, &count );
679 IStream_Release( stm );
681 if( FAILED( r ) || ( count != sizeof zero ) )
687 /* create the StringData stream... make it zero length */
688 encode_streamname(TRUE, szStringData, encname);
689 r = IStorage_CreateStream( stg, encname,
690 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
696 IStream_Release( stm );
701 UINT load_string_table( MSIDATABASE *db )
705 UINT r, ret = ERROR_FUNCTION_FAILED, datasize = 0, poolsize = 0, codepage;
706 DWORD i, count, offset, len, n;
707 static const WCHAR szStringData[] = {
708 '_','S','t','r','i','n','g','D','a','t','a',0 };
709 static const WCHAR szStringPool[] = {
710 '_','S','t','r','i','n','g','P','o','o','l',0 };
714 msi_destroy_stringtable( db->strings );
718 r = read_stream_data( db->storage, szStringPool, &pool, &poolsize );
719 if( r != ERROR_SUCCESS)
721 r = read_stream_data( db->storage, szStringData, (USHORT**)&data, &datasize );
722 if( r != ERROR_SUCCESS)
727 codepage = pool[0] | ( pool[1] << 16 );
730 db->strings = msi_init_stringtable( count, codepage );
733 for( i=1; i<count; i++ )
736 n = msi_addstring( db->strings, i, data+offset, len, pool[i*2+1] );
738 ERR("Failed to add string %ld\n", i );
742 TRACE("Loaded %ld strings\n", count);
748 HeapFree( GetProcessHeap(), 0, pool );
750 HeapFree( GetProcessHeap(), 0, data );
755 UINT save_string_table( MSIDATABASE *db )
757 UINT i, count, datasize, poolsize, sz, used, r, codepage;
758 UINT ret = ERROR_FUNCTION_FAILED;
759 static const WCHAR szStringData[] = {
760 '_','S','t','r','i','n','g','D','a','t','a',0 };
761 static const WCHAR szStringPool[] = {
762 '_','S','t','r','i','n','g','P','o','o','l',0 };
768 /* construct the new table in memory first */
769 datasize = msi_string_totalsize( db->strings, &count );
770 poolsize = count*2*sizeof(USHORT);
772 pool = HeapAlloc( GetProcessHeap(), 0, poolsize );
775 ERR("Failed to alloc pool %d bytes\n", poolsize );
778 data = HeapAlloc( GetProcessHeap(), 0, datasize );
781 ERR("Failed to alloc data %d bytes\n", poolsize );
786 codepage = msi_string_get_codepage( db->strings );
787 pool[0]=codepage&0xffff;
788 pool[1]=(codepage>>16);
789 for( i=1; i<count; i++ )
791 sz = datasize - used;
792 r = msi_id2stringA( db->strings, i, data+used, &sz );
793 if( r != ERROR_SUCCESS )
795 ERR("failed to fetch string\n");
798 if( sz && (sz < (datasize - used ) ) )
800 TRACE("adding %u bytes %s\n", sz, data+used );
802 pool[ i*2 + 1 ] = msi_id_refcount( db->strings, i );
804 if( used > datasize )
806 ERR("oops overran %d >= %d\n", used, datasize);
811 if( used != datasize )
813 ERR("oops used %d != datasize %d\n", used, datasize);
817 /* write the streams */
818 r = write_stream_data( db->storage, szStringData, data, datasize );
819 TRACE("Wrote StringData r=%08x\n", r);
822 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
823 TRACE("Wrote StringPool r=%08x\n", r);
831 HeapFree( GetProcessHeap(), 0, data );
833 HeapFree( GetProcessHeap(), 0, pool );
838 static LPWSTR strdupW( LPCWSTR str )
840 UINT len = lstrlenW( str ) + 1;
841 LPWSTR ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
843 lstrcpyW( ret, str );
847 /* information for default tables */
848 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
849 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
850 static const WCHAR szName[] = { 'N','a','m','e',0 };
851 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
852 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
853 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
854 static const WCHAR szType[] = { 'T','y','p','e',0 };
856 struct standard_table {
861 } MSI_standard_tables[] =
863 { szTables, szName, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
864 { szColumns, szTable, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
865 { szColumns, szNumber, 2, MSITYPE_VALID | 2},
866 { szColumns, szName, 3, MSITYPE_VALID | MSITYPE_STRING | 32},
867 { szColumns, szType, 4, MSITYPE_VALID | 2},
870 #define STANDARD_TABLE_COUNT \
871 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
873 UINT get_defaulttablecolumns( LPCWSTR szTable, MSICOLUMNINFO *colinfo, UINT *sz)
877 for(i=0; i<STANDARD_TABLE_COUNT; i++)
879 if( lstrcmpW( szTable, MSI_standard_tables[i].tablename ) )
881 if(colinfo && (n < *sz) )
883 colinfo[n].tablename = strdupW(MSI_standard_tables[i].tablename);
884 colinfo[n].colname = strdupW(MSI_standard_tables[i].columnname);
885 colinfo[n].number = MSI_standard_tables[i].number;
886 colinfo[n].type = MSI_standard_tables[i].type;
887 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
888 debugstr_w(colinfo[n].colname)); */
890 colinfo[n].offset = colinfo[n-1].offset
891 + bytes_per_column( &colinfo[n-1] );
893 colinfo[n].offset = 0;
896 if( colinfo && (n >= *sz) )
900 return ERROR_SUCCESS;
903 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
908 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
909 if( r != ERROR_SUCCESS )
911 str = HeapAlloc( GetProcessHeap(), 0, sz*sizeof (WCHAR));
914 r = msi_id2stringW( db->strings, stringid, str, &sz );
915 if( r == ERROR_SUCCESS )
917 HeapFree( GetProcessHeap(), 0, str );
921 static UINT get_tablecolumns( MSIDATABASE *db,
922 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
924 UINT r, i, n=0, table_id, count, maxcount = *sz;
925 MSITABLE *table = NULL;
926 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
928 /* first check if there is a default table with that name */
929 r = get_defaulttablecolumns( szTableName, colinfo, sz );
930 if( ( r == ERROR_SUCCESS ) && *sz )
933 r = get_table( db, szColumns, &table);
934 if( r != ERROR_SUCCESS )
936 ERR("table %s not available\n", debugstr_w(szColumns));
940 /* convert table and column names to IDs from the string table */
941 r = msi_string2idW( db->strings, szTableName, &table_id );
942 if( r != ERROR_SUCCESS )
944 release_table( db, table );
945 ERR("Couldn't find id for %s\n", debugstr_w(szTableName));
949 TRACE("Table id is %d\n", table_id);
951 count = table->row_count;
952 for( i=0; i<count; i++ )
954 if( table->data[ i ][ 0 ] != table_id )
958 UINT id = table->data[ i ] [ 2 ];
959 colinfo[n].tablename = MSI_makestring( db, table_id );
960 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
961 colinfo[n].colname = MSI_makestring( db, id );
962 colinfo[n].type = table->data[ i ] [ 3 ];
963 /* this assumes that columns are in order in the table */
965 colinfo[n].offset = colinfo[n-1].offset
966 + bytes_per_column( &colinfo[n-1] );
968 colinfo[n].offset = 0;
969 TRACE("table %s column %d is [%s] (%d) with type %08x "
970 "offset %d at row %d\n", debugstr_w(szTableName),
971 colinfo[n].number, debugstr_w(colinfo[n].colname),
972 id, colinfo[n].type, colinfo[n].offset, i);
973 if( n != (colinfo[n].number-1) )
975 ERR("oops. data in the _Columns table isn't in the right "
976 "order for table %s\n", debugstr_w(szTableName));
977 return ERROR_FUNCTION_FAILED;
981 if( colinfo && ( n >= maxcount ) )
986 release_table( db, table );
988 return ERROR_SUCCESS;
991 /* try to find the table name in the _Tables table */
992 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
994 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
995 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
996 UINT r, table_id = 0, i, count;
997 MSITABLE *table = NULL;
999 if( !lstrcmpW( name, szTables ) )
1001 if( !lstrcmpW( name, szColumns ) )
1004 r = msi_string2idW( db->strings, name, &table_id );
1005 if( r != ERROR_SUCCESS )
1007 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1011 r = get_table( db, szTables, &table);
1012 if( r != ERROR_SUCCESS )
1014 ERR("table %s not available\n", debugstr_w(szTables));
1018 /* count = table->size/2; */
1019 count = table->row_count;
1020 for( i=0; i<count; i++ )
1021 if( table->data[ i ][ 0 ] == table_id )
1024 release_table( db, table );
1029 ERR("Searched %d tables, but %d was not found\n", count, table_id );
1034 /* below is the query interface to a table */
1036 typedef struct tagMSITABLEVIEW
1041 MSICOLUMNINFO *columns;
1047 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1049 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1050 UINT offset, num_rows, n;
1053 return ERROR_INVALID_PARAMETER;
1055 if( (col==0) || (col>tv->num_cols) )
1056 return ERROR_INVALID_PARAMETER;
1058 /* how many rows are there ? */
1059 num_rows = tv->table->row_count;
1060 if( row >= num_rows )
1061 return ERROR_NO_MORE_ITEMS;
1063 if( tv->columns[col-1].offset >= tv->row_size )
1065 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1066 ERR("%p %p\n", tv, tv->columns );
1067 return ERROR_FUNCTION_FAILED;
1070 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1071 n = bytes_per_column( &tv->columns[col-1] );
1075 offset = tv->columns[col-1].offset/2;
1076 *val = tv->table->data[row][offset] +
1077 (tv->table->data[row][offset + 1] << 16);
1080 offset = tv->columns[col-1].offset/2;
1081 *val = tv->table->data[row][offset];
1084 ERR("oops! what is %d bytes per column?\n", n );
1085 return ERROR_FUNCTION_FAILED;
1088 TRACE("Data [%d][%d] = %d \n", row, col, *val );
1090 return ERROR_SUCCESS;
1093 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1095 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1096 UINT offset, num_rows, n;
1099 return ERROR_INVALID_PARAMETER;
1101 if( (col==0) || (col>tv->num_cols) )
1102 return ERROR_INVALID_PARAMETER;
1104 if( tv->columns[col-1].offset >= tv->row_size )
1106 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1107 ERR("%p %p\n", tv, tv->columns );
1108 return ERROR_FUNCTION_FAILED;
1111 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1112 n = bytes_per_column( &tv->columns[col-1] );
1116 offset = tv->columns[col-1].offset/2;
1117 tv->table->data[row][offset] = val & 0xffff;
1118 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1121 offset = tv->columns[col-1].offset/2;
1122 tv->table->data[row][offset] = val;
1125 ERR("oops! what is %d bytes per column?\n", n );
1126 return ERROR_FUNCTION_FAILED;
1128 return ERROR_SUCCESS;
1131 UINT TABLE_insert_row( struct tagMSIVIEW *view, UINT *num )
1133 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1137 TRACE("%p\n", view);
1140 return ERROR_INVALID_PARAMETER;
1142 row = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, tv->row_size );
1144 return ERROR_NOT_ENOUGH_MEMORY;
1146 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1147 if( tv->table->data )
1148 p = HeapReAlloc( GetProcessHeap(), 0, tv->table->data, sz );
1150 p = HeapAlloc( GetProcessHeap(), 0, sz );
1152 return ERROR_NOT_ENOUGH_MEMORY;
1154 tv->table->data = p;
1155 tv->table->data[tv->table->row_count] = row;
1156 *num = tv->table->row_count;
1157 tv->table->row_count++;
1159 return ERROR_SUCCESS;
1162 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIHANDLE record )
1164 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1167 TRACE("%p %ld\n", tv, record);
1170 return ERROR_FUNCTION_FAILED;
1172 r = get_table( tv->db, tv->name, &tv->table );
1173 if( r != ERROR_SUCCESS )
1176 return ERROR_SUCCESS;
1179 static UINT TABLE_close( struct tagMSIVIEW *view )
1181 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1183 TRACE("%p\n", view );
1186 return ERROR_FUNCTION_FAILED;
1188 release_table( tv->db, tv->table );
1191 return ERROR_SUCCESS;
1194 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1196 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1198 TRACE("%p %p %p\n", view, rows, cols );
1201 *cols = tv->num_cols;
1205 return ERROR_INVALID_PARAMETER;
1206 *rows = tv->table->row_count;
1209 return ERROR_SUCCESS;
1212 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1213 UINT n, LPWSTR *name, UINT *type )
1215 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1217 TRACE("%p %d %p %p\n", tv, n, name, type );
1219 if( ( n == 0 ) || ( n > tv->num_cols ) )
1220 return ERROR_INVALID_PARAMETER;
1224 *name = strdupW( tv->columns[n-1].colname );
1226 return ERROR_FUNCTION_FAILED;
1229 *type = tv->columns[n-1].type;
1231 return ERROR_SUCCESS;
1234 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIHANDLE hrec)
1236 FIXME("%p %d %ld\n", view, eModifyMode, hrec );
1237 return ERROR_CALL_NOT_IMPLEMENTED;
1240 static UINT TABLE_delete( struct tagMSIVIEW *view )
1242 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1244 TRACE("%p\n", view );
1247 release_table( tv->db, tv->table );
1253 for( i=0; i<tv->num_cols; i++)
1255 HeapFree( GetProcessHeap(), 0, tv->columns[i].colname );
1256 HeapFree( GetProcessHeap(), 0, tv->columns[i].tablename );
1258 HeapFree( GetProcessHeap(), 0, tv->columns );
1262 HeapFree( GetProcessHeap(), 0, tv );
1264 return ERROR_SUCCESS;
1268 MSIVIEWOPS table_ops =
1275 TABLE_get_dimensions,
1276 TABLE_get_column_info,
1281 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1284 UINT r, sz, column_count;
1285 MSICOLUMNINFO *columns, *last_col;
1287 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1289 /* get the number of columns in this table */
1291 r = get_tablecolumns( db, name, NULL, &column_count );
1292 if( r != ERROR_SUCCESS )
1295 /* if there's no columns, there's no table */
1296 if( column_count == 0 )
1297 return ERROR_INVALID_PARAMETER;
1299 TRACE("Table found\n");
1301 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1302 tv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sz );
1304 return ERROR_FUNCTION_FAILED;
1306 columns = HeapAlloc( GetProcessHeap(), 0, column_count*sizeof (MSICOLUMNINFO));
1309 HeapFree( GetProcessHeap(), 0, tv );
1310 return ERROR_FUNCTION_FAILED;
1313 r = get_tablecolumns( db, name, columns, &column_count );
1314 if( r != ERROR_SUCCESS )
1316 HeapFree( GetProcessHeap(), 0, columns );
1317 HeapFree( GetProcessHeap(), 0, tv );
1318 return ERROR_FUNCTION_FAILED;
1321 TRACE("Table has %d columns\n", column_count);
1323 last_col = &columns[column_count-1];
1325 /* fill the structure */
1326 tv->view.ops = &table_ops;
1328 tv->columns = columns;
1329 tv->num_cols = column_count;
1331 tv->row_size = last_col->offset + bytes_per_column( last_col );
1333 TRACE("one row is %d bytes\n", tv->row_size );
1335 *view = (MSIVIEW*) tv;
1336 lstrcpyW( tv->name, name );
1338 return ERROR_SUCCESS;
1341 UINT MSI_CommitTables( MSIDATABASE *db )
1344 MSITABLE *table = NULL;
1348 r = save_string_table( db );
1349 if( r != ERROR_SUCCESS )
1351 ERR("failed to save string table r=%08x\n",r);
1355 for( table = db->first_table; table; table = table->next )
1357 r = save_table( db, table );
1358 if( r != ERROR_SUCCESS )
1360 ERR("failed to save table %s (r=%08x)\n",
1361 debugstr_w(table->name), r);
1366 /* force everything to reload next time */
1367 free_cached_tables( db );
1369 return ERROR_SUCCESS;