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);
130 static int mime2utf(int x)
137 return x - 10 - 26 + 'a';
138 if( x == (10+26+26) )
143 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
148 while ( (ch = *in++) )
150 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
153 ch = mime2utf(ch-0x4800);
157 *out++ = mime2utf(ch&0x3f);
159 ch = mime2utf((ch>>6)&0x3f);
170 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
171 USHORT **pdata, UINT *psz )
174 UINT ret = ERROR_FUNCTION_FAILED;
181 encode_streamname(TRUE, stname, encname);
183 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
185 r = IStorage_OpenStream(stg, encname, NULL,
186 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
189 WARN("open stream failed r = %08lx - empty table?\n",r);
193 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
196 ERR("open stream failed r = %08lx!\n",r);
200 if( stat.cbSize.QuadPart >> 32 )
206 sz = stat.cbSize.QuadPart;
207 data = HeapAlloc( GetProcessHeap(), 0, sz );
210 ERR("couldn't allocate memory r=%08lx!\n",r);
211 ret = ERROR_NOT_ENOUGH_MEMORY;
215 r = IStream_Read(stm, data, sz, &count );
216 if( FAILED( r ) || ( count != sz ) )
218 HeapFree( GetProcessHeap(), 0, data );
219 ERR("read stream failed r = %08lx!\n",r);
228 IStream_Release( stm );
233 static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
234 LPVOID data, UINT sz )
237 UINT ret = ERROR_FUNCTION_FAILED;
245 encode_streamname(TRUE, stname, encname);
246 r = IStorage_OpenStream( stg, encname, NULL,
247 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
250 r = IStorage_CreateStream( stg, encname,
251 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
255 ERR("open stream failed r = %08lx\n",r);
260 r = IStream_SetSize( stm, size );
263 ERR("Failed to SetSize\n");
268 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
271 ERR("Failed to Seek\n");
275 r = IStream_Write(stm, data, sz, &count );
276 if( FAILED( r ) || ( count != sz ) )
278 ERR("Failed to Write\n");
285 IStream_Release( stm );
290 UINT read_table_from_storage( MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
293 USHORT *rawdata = NULL;
294 UINT rawsize = 0, r, i, j, row_size = 0, num_cols = 0;
295 MSICOLUMNINFO *cols, *last_col;
297 TRACE("%s\n",debugstr_w(name));
299 /* non-existing tables should be interpretted as empty tables */
300 t = HeapAlloc( GetProcessHeap(), 0,
301 sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
303 return ERROR_NOT_ENOUGH_MEMORY;
305 r = table_get_column_info( db, name, &cols, &num_cols );
306 if( r != ERROR_SUCCESS )
308 HeapFree( GetProcessHeap(), 0, t );
311 last_col = &cols[num_cols-1];
312 row_size = last_col->offset + bytes_per_column( last_col );
316 lstrcpyW( t->name, name );
320 /* if we can't read the table, just assume that it's empty */
321 read_stream_data( db->storage, name, &rawdata, &rawsize );
323 return ERROR_SUCCESS;
325 TRACE("Read %d bytes\n", rawsize );
327 if( rawsize % row_size )
329 ERR("Table size is invalid %d/%d\n", rawsize, row_size );
330 return ERROR_FUNCTION_FAILED;
333 t->row_count = rawsize / row_size;
334 t->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
335 t->row_count * sizeof (USHORT*) );
337 return ERROR_NOT_ENOUGH_MEMORY; /* FIXME: memory leak */
339 /* transpose all the data */
340 TRACE("Transposing data from %d columns\n", t->row_count );
341 for( i=0; i<t->row_count; i++ )
343 t->data[i] = HeapAlloc( GetProcessHeap(), 0, row_size );
345 return ERROR_NOT_ENOUGH_MEMORY; /* FIXME: memory leak */
346 for( j=0; j<num_cols; j++ )
348 UINT ofs = cols[j].offset/2;
349 UINT n = bytes_per_column( &cols[j] );
354 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
357 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
358 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i + 1];
361 ERR("oops - unknown column width %d\n", n);
362 return ERROR_FUNCTION_FAILED;
367 HeapFree( GetProcessHeap(), 0, cols );
368 HeapFree( GetProcessHeap(), 0, rawdata );
370 return ERROR_SUCCESS;
373 /* add this table to the list of cached tables in the database */
374 void add_table(MSIDATABASE *db, MSITABLE *table)
376 table->next = db->first_table;
378 if( db->first_table )
379 db->first_table->prev = table;
381 db->last_table = table;
382 db->first_table = table;
385 /* remove from the list of cached tables */
386 void remove_table( MSIDATABASE *db, MSITABLE *table )
389 table->next->prev = table->prev;
391 db->last_table = table->prev;
393 table->prev->next = table->next;
395 db->first_table = table->next;
400 void release_table( MSIDATABASE *db, MSITABLE *table )
402 if( !table->ref_count )
403 ERR("Trying to destroy table with refcount 0\n");
405 if( !table->ref_count )
407 remove_table( db, table );
408 HeapFree( GetProcessHeap(), 0, table->data );
409 HeapFree( GetProcessHeap(), 0, table );
410 TRACE("Destroyed table %s\n", debugstr_w(table->name));
414 void free_cached_tables( MSIDATABASE *db )
416 while( db->first_table )
418 MSITABLE *t = db->first_table;
420 if ( --t->ref_count )
421 ERR("table ref count not zero for %s\n", debugstr_w(t->name));
422 remove_table( db, t );
423 HeapFree( GetProcessHeap(), 0, t->data );
424 HeapFree( GetProcessHeap(), 0, t );
428 UINT find_cached_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
432 for( t = db->first_table; t; t=t->next )
434 if( !lstrcmpW( name, t->name ) )
437 return ERROR_SUCCESS;
441 return ERROR_FUNCTION_FAILED;
444 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
446 UINT r, column_count;
447 MSICOLUMNINFO *columns;
449 /* get the number of columns in this table */
451 r = get_tablecolumns( db, name, NULL, &column_count );
452 if( r != ERROR_SUCCESS )
455 /* if there's no columns, there's no table */
456 if( column_count == 0 )
457 return ERROR_INVALID_PARAMETER;
459 TRACE("Table %s found\n", debugstr_w(name) );
461 columns = HeapAlloc( GetProcessHeap(), 0, column_count*sizeof (MSICOLUMNINFO));
463 return ERROR_FUNCTION_FAILED;
465 r = get_tablecolumns( db, name, columns, &column_count );
466 if( r != ERROR_SUCCESS )
468 HeapFree( GetProcessHeap(), 0, columns );
469 return ERROR_FUNCTION_FAILED;
473 *pcount = column_count;
478 UINT get_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
484 /* first, see if the table is cached */
485 r = find_cached_table( db, name, ptable );
486 if( r == ERROR_SUCCESS )
488 (*ptable)->ref_count++;
492 r = read_table_from_storage( db, name, ptable );
493 if( r != ERROR_SUCCESS )
496 /* add the table to the list */
497 add_table( db, *ptable );
498 (*ptable)->ref_count++;
500 return ERROR_SUCCESS;
503 UINT save_table( MSIDATABASE *db, MSITABLE *t )
505 USHORT *rawdata = NULL, *p;
506 UINT rawsize, r, i, j, row_size, num_cols = 0;
507 MSICOLUMNINFO *cols, *last_col;
509 TRACE("Saving %s\n", debugstr_w( t->name ) );
511 r = table_get_column_info( db, t->name, &cols, &num_cols );
512 if( r != ERROR_SUCCESS )
515 last_col = &cols[num_cols-1];
516 row_size = last_col->offset + bytes_per_column( last_col );
518 rawsize = t->row_count * row_size;
519 rawdata = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, rawsize );
521 return ERROR_NOT_ENOUGH_MEMORY;
524 for( i=0; i<num_cols; i++ )
526 for( j=0; j<t->row_count; j++ )
528 UINT offset = cols[i].offset;
530 *p++ = t->data[j][offset/2];
531 if( 4 == bytes_per_column( &cols[i] ) )
532 *p++ = t->data[j][offset/2+1];
536 TRACE("writing %d bytes\n", rawsize);
537 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
539 HeapFree( GetProcessHeap(), 0, rawdata );
544 HRESULT init_string_table( IStorage *stg )
547 static const WCHAR szStringData[] = {
548 '_','S','t','r','i','n','g','D','a','t','a',0 };
549 static const WCHAR szStringPool[] = {
550 '_','S','t','r','i','n','g','P','o','o','l',0 };
551 USHORT zero[2] = { 0, 0 };
556 encode_streamname(TRUE, szStringPool, encname);
558 /* create the StringPool stream... add the zero string to it*/
559 r = IStorage_CreateStream( stg, encname,
560 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
567 r = IStream_Write(stm, zero, sizeof zero, &count );
568 IStream_Release( stm );
570 if( FAILED( r ) || ( count != sizeof zero ) )
576 /* create the StringData stream... make it zero length */
577 encode_streamname(TRUE, szStringData, encname);
578 r = IStorage_CreateStream( stg, encname,
579 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
585 IStream_Release( stm );
590 UINT load_string_table( MSIDATABASE *db )
594 UINT r, ret = ERROR_FUNCTION_FAILED, datasize = 0, poolsize = 0;
595 DWORD i, count, offset, len, n;
596 static const WCHAR szStringData[] = {
597 '_','S','t','r','i','n','g','D','a','t','a',0 };
598 static const WCHAR szStringPool[] = {
599 '_','S','t','r','i','n','g','P','o','o','l',0 };
603 msi_destroy_stringtable( db->strings );
607 r = read_stream_data( db->storage, szStringPool, &pool, &poolsize );
608 if( r != ERROR_SUCCESS)
610 r = read_stream_data( db->storage, szStringData, (USHORT**)&data, &datasize );
611 if( r != ERROR_SUCCESS)
615 db->strings = msi_init_stringtable( count );
617 if( pool[0] || pool[1] )
618 ERR("The first string should be nul, but isn't\n");
620 for( i=1; i<count; i++ )
623 n = msi_addstring( db->strings, i, data+offset, len, pool[i*2+1] );
625 ERR("Failed to add string %ld\n", i );
629 TRACE("Loaded %ld strings\n", count);
635 HeapFree( GetProcessHeap(), 0, pool );
637 HeapFree( GetProcessHeap(), 0, data );
642 UINT save_string_table( MSIDATABASE *db )
644 UINT i, count, datasize, poolsize, sz, used, r;
645 UINT ret = ERROR_FUNCTION_FAILED;
646 static const WCHAR szStringData[] = {
647 '_','S','t','r','i','n','g','D','a','t','a',0 };
648 static const WCHAR szStringPool[] = {
649 '_','S','t','r','i','n','g','P','o','o','l',0 };
655 /* construct the new table in memory first */
656 count = msi_string_count( db->strings );
657 poolsize = count*2*sizeof(USHORT);
658 datasize = msi_string_totalsize( db->strings );
660 pool = HeapAlloc( GetProcessHeap(), 0, poolsize );
663 ERR("Failed to alloc pool %d bytes\n", poolsize );
666 data = HeapAlloc( GetProcessHeap(), 0, datasize );
669 ERR("Failed to alloc data %d bytes\n", poolsize );
674 pool[0]=0; /* the first element is always zero */
676 for( i=1; i<count; i++ )
678 sz = datasize - used;
679 r = msi_id2stringA( db->strings, i, data+used, &sz );
680 if( r != ERROR_SUCCESS )
682 ERR("failed to fetch string\n");
688 pool[ i*2 + 1 ] = msi_id_refcount( db->strings, i );
690 if( used > datasize )
692 ERR("oops overran %d >= %d\n", used, datasize);
697 if( used != datasize )
699 ERR("oops used %d != datasize %d\n", used, datasize);
703 /* write the streams */
704 r = write_stream_data( db->storage, szStringData, data, datasize );
705 TRACE("Wrote StringData r=%08x\n", r);
708 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
709 TRACE("Wrote StringPool r=%08x\n", r);
717 HeapFree( GetProcessHeap(), 0, data );
719 HeapFree( GetProcessHeap(), 0, pool );
724 static LPWSTR strdupW( LPCWSTR str )
726 UINT len = lstrlenW( str ) + 1;
727 LPWSTR ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
729 lstrcpyW( ret, str );
733 /* information for default tables */
734 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
735 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
736 static const WCHAR szName[] = { 'N','a','m','e',0 };
737 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
738 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
739 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
740 static const WCHAR szType[] = { 'T','y','p','e',0 };
742 struct standard_table {
747 } MSI_standard_tables[] =
749 { szTables, szName, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
750 { szColumns, szTable, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
751 { szColumns, szNumber, 2, MSITYPE_VALID | 2},
752 { szColumns, szName, 3, MSITYPE_VALID | MSITYPE_STRING | 32},
753 { szColumns, szType, 4, MSITYPE_VALID | 2},
756 #define STANDARD_TABLE_COUNT \
757 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
759 UINT get_defaulttablecolumns( LPCWSTR szTable, MSICOLUMNINFO *colinfo, UINT *sz)
763 for(i=0; i<STANDARD_TABLE_COUNT; i++)
765 if( lstrcmpW( szTable, MSI_standard_tables[i].tablename ) )
767 if(colinfo && (n < *sz) )
769 colinfo[n].tablename = strdupW(MSI_standard_tables[i].tablename);
770 colinfo[n].colname = strdupW(MSI_standard_tables[i].columnname);
771 colinfo[n].number = MSI_standard_tables[i].number;
772 colinfo[n].type = MSI_standard_tables[i].type;
773 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
774 debugstr_w(colinfo[n].colname)); */
776 colinfo[n].offset = colinfo[n-1].offset
777 + bytes_per_column( &colinfo[n-1] );
779 colinfo[n].offset = 0;
782 if( colinfo && (n >= *sz) )
786 return ERROR_SUCCESS;
789 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
794 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
795 if( r != ERROR_SUCCESS )
797 str = HeapAlloc( GetProcessHeap(), 0, sz*sizeof (WCHAR));
800 r = msi_id2stringW( db->strings, stringid, str, &sz );
801 if( r == ERROR_SUCCESS )
803 HeapFree( GetProcessHeap(), 0, str );
807 static UINT get_tablecolumns( MSIDATABASE *db,
808 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
810 UINT r, i, n=0, table_id, count, maxcount = *sz;
811 MSITABLE *table = NULL;
812 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
814 /* first check if there is a default table with that name */
815 r = get_defaulttablecolumns( szTableName, colinfo, sz );
816 if( ( r == ERROR_SUCCESS ) && *sz )
819 r = get_table( db, szColumns, &table);
820 if( r != ERROR_SUCCESS )
822 ERR("table %s not available\n", debugstr_w(szColumns));
826 /* convert table and column names to IDs from the string table */
827 r = msi_string2id( db->strings, szTableName, &table_id );
828 if( r != ERROR_SUCCESS )
830 release_table( db, table );
831 ERR("Couldn't find id for %s\n", debugstr_w(szTableName));
835 TRACE("Table id is %d\n", table_id);
837 count = table->row_count;
838 for( i=0; i<count; i++ )
840 if( table->data[ i ][ 0 ] != table_id )
844 UINT id = table->data[ i ] [ 2 ];
845 colinfo[n].tablename = MSI_makestring( db, table_id );
846 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
847 colinfo[n].colname = MSI_makestring( db, id );
848 colinfo[n].type = table->data[ i ] [ 3 ];
849 /* this assumes that columns are in order in the table */
851 colinfo[n].offset = colinfo[n-1].offset
852 + bytes_per_column( &colinfo[n-1] );
854 colinfo[n].offset = 0;
855 TRACE("table %s column %d is [%s] (%d) with type %08x "
856 "offset %d at row %d\n", debugstr_w(szTableName),
857 colinfo[n].number, debugstr_w(colinfo[n].colname),
858 id, colinfo[n].type, colinfo[n].offset, i);
859 if( n != (colinfo[n].number-1) )
861 ERR("oops. data in the _Columns table isn't in the right "
862 "order for table %s\n", debugstr_w(szTableName));
863 return ERROR_FUNCTION_FAILED;
867 if( colinfo && ( n >= maxcount ) )
872 release_table( db, table );
874 return ERROR_SUCCESS;
877 /* try to find the table name in the _Tables table */
878 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
880 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
881 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
882 UINT r, table_id = 0, i, count;
883 MSITABLE *table = NULL;
885 if( !lstrcmpW( name, szTables ) )
887 if( !lstrcmpW( name, szColumns ) )
890 r = msi_string2id( db->strings, name, &table_id );
891 if( r != ERROR_SUCCESS )
893 TRACE("Couldn't find id for %s\n", debugstr_w(name));
897 r = get_table( db, szTables, &table);
898 if( r != ERROR_SUCCESS )
900 ERR("table %s not available\n", debugstr_w(szTables));
904 /* count = table->size/2; */
905 count = table->row_count;
906 for( i=0; i<count; i++ )
907 if( table->data[ i ][ 0 ] == table_id )
910 release_table( db, table );
915 ERR("Searched %d tables, but %d was not found\n", count, table_id );
920 /* below is the query interface to a table */
922 typedef struct tagMSITABLEVIEW
927 MSICOLUMNINFO *columns;
933 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
935 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
936 UINT offset, num_rows, n;
939 return ERROR_INVALID_PARAMETER;
941 if( (col==0) || (col>tv->num_cols) )
942 return ERROR_INVALID_PARAMETER;
944 /* how many rows are there ? */
945 num_rows = tv->table->row_count;
946 if( row >= num_rows )
947 return ERROR_NO_MORE_ITEMS;
949 if( tv->columns[col-1].offset >= tv->row_size )
951 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
952 ERR("%p %p\n", tv, tv->columns );
953 return ERROR_FUNCTION_FAILED;
956 offset = row + (tv->columns[col-1].offset/2) * num_rows;
957 n = bytes_per_column( &tv->columns[col-1] );
961 offset = tv->columns[col-1].offset/2;
962 *val = tv->table->data[row][offset] +
963 (tv->table->data[row][offset + 1] << 16);
966 offset = tv->columns[col-1].offset/2;
967 *val = tv->table->data[row][offset];
970 ERR("oops! what is %d bytes per column?\n", n );
971 return ERROR_FUNCTION_FAILED;
974 TRACE("Data [%d][%d] = %d \n", row, col, *val );
976 return ERROR_SUCCESS;
979 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
981 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
982 UINT offset, num_rows, n;
985 return ERROR_INVALID_PARAMETER;
987 if( (col==0) || (col>tv->num_cols) )
988 return ERROR_INVALID_PARAMETER;
990 if( tv->columns[col-1].offset >= tv->row_size )
992 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
993 ERR("%p %p\n", tv, tv->columns );
994 return ERROR_FUNCTION_FAILED;
997 offset = row + (tv->columns[col-1].offset/2) * num_rows;
998 n = bytes_per_column( &tv->columns[col-1] );
1002 offset = tv->columns[col-1].offset/2;
1003 tv->table->data[row][offset] = val & 0xffff;
1004 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1007 offset = tv->columns[col-1].offset/2;
1008 tv->table->data[row][offset] = val;
1011 ERR("oops! what is %d bytes per column?\n", n );
1012 return ERROR_FUNCTION_FAILED;
1014 return ERROR_SUCCESS;
1017 UINT TABLE_insert_row( struct tagMSIVIEW *view, UINT *num )
1019 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1023 TRACE("%p\n", view);
1026 return ERROR_INVALID_PARAMETER;
1028 row = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, tv->row_size );
1030 return ERROR_NOT_ENOUGH_MEMORY;
1032 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1033 if( tv->table->data )
1034 p = HeapReAlloc( GetProcessHeap(), 0, tv->table->data, sz );
1036 p = HeapAlloc( GetProcessHeap(), 0, sz );
1038 return ERROR_NOT_ENOUGH_MEMORY;
1040 tv->table->data = p;
1041 tv->table->data[tv->table->row_count] = row;
1042 *num = tv->table->row_count;
1043 tv->table->row_count++;
1045 return ERROR_SUCCESS;
1048 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIHANDLE record )
1050 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1053 TRACE("%p %ld\n", tv, record);
1056 return ERROR_FUNCTION_FAILED;
1058 r = get_table( tv->db, tv->name, &tv->table );
1059 if( r != ERROR_SUCCESS )
1062 return ERROR_SUCCESS;
1065 static UINT TABLE_close( struct tagMSIVIEW *view )
1067 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1069 TRACE("%p\n", view );
1072 return ERROR_FUNCTION_FAILED;
1074 release_table( tv->db, tv->table );
1077 return ERROR_SUCCESS;
1080 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1082 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1084 TRACE("%p %p %p\n", view, rows, cols );
1087 *cols = tv->num_cols;
1091 return ERROR_INVALID_PARAMETER;
1092 *rows = tv->table->row_count;
1095 return ERROR_SUCCESS;
1098 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1099 UINT n, LPWSTR *name, UINT *type )
1101 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1103 TRACE("%p %d %p %p\n", tv, n, name, type );
1105 if( ( n == 0 ) || ( n > tv->num_cols ) )
1106 return ERROR_INVALID_PARAMETER;
1110 *name = strdupW( tv->columns[n-1].colname );
1112 return ERROR_FUNCTION_FAILED;
1115 *type = tv->columns[n-1].type;
1117 return ERROR_SUCCESS;
1120 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIHANDLE hrec)
1122 FIXME("%p %d %ld\n", view, eModifyMode, hrec );
1123 return ERROR_CALL_NOT_IMPLEMENTED;
1126 static UINT TABLE_delete( struct tagMSIVIEW *view )
1128 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1130 TRACE("%p\n", view );
1133 release_table( tv->db, tv->table );
1139 for( i=0; i<tv->num_cols; i++)
1141 HeapFree( GetProcessHeap(), 0, tv->columns[i].colname );
1142 HeapFree( GetProcessHeap(), 0, tv->columns[i].tablename );
1144 HeapFree( GetProcessHeap(), 0, tv->columns );
1148 HeapFree( GetProcessHeap(), 0, tv );
1150 return ERROR_SUCCESS;
1154 MSIVIEWOPS table_ops =
1161 TABLE_get_dimensions,
1162 TABLE_get_column_info,
1167 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1170 UINT r, sz, column_count;
1171 MSICOLUMNINFO *columns, *last_col;
1173 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1175 /* get the number of columns in this table */
1177 r = get_tablecolumns( db, name, NULL, &column_count );
1178 if( r != ERROR_SUCCESS )
1181 /* if there's no columns, there's no table */
1182 if( column_count == 0 )
1183 return ERROR_INVALID_PARAMETER;
1185 TRACE("Table found\n");
1187 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1188 tv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sz );
1190 return ERROR_FUNCTION_FAILED;
1192 columns = HeapAlloc( GetProcessHeap(), 0, column_count*sizeof (MSICOLUMNINFO));
1195 HeapFree( GetProcessHeap(), 0, tv );
1196 return ERROR_FUNCTION_FAILED;
1199 r = get_tablecolumns( db, name, columns, &column_count );
1200 if( r != ERROR_SUCCESS )
1202 HeapFree( GetProcessHeap(), 0, columns );
1203 HeapFree( GetProcessHeap(), 0, tv );
1204 return ERROR_FUNCTION_FAILED;
1207 TRACE("Table has %d columns\n", column_count);
1209 last_col = &columns[column_count-1];
1211 /* fill the structure */
1212 tv->view.ops = &table_ops;
1214 tv->columns = columns;
1215 tv->num_cols = column_count;
1217 tv->row_size = last_col->offset + bytes_per_column( last_col );
1219 TRACE("one row is %d bytes\n", tv->row_size );
1221 *view = (MSIVIEW*) tv;
1222 lstrcpyW( tv->name, name );
1224 return ERROR_SUCCESS;
1227 UINT MSI_CommitTables( MSIDATABASE *db )
1230 MSITABLE *table = NULL;
1234 r = save_string_table( db );
1235 if( r != ERROR_SUCCESS )
1237 ERR("failed to save string table r=%08x\n",r);
1241 for( table = db->first_table; table; table = table->next )
1243 r = save_table( db, table );
1244 if( r != ERROR_SUCCESS )
1246 ERR("failed to save table %s (r=%08x)\n",
1247 debugstr_w(table->name), r);
1252 /* force everything to reload next time */
1253 free_cached_tables( db );
1255 return ERROR_SUCCESS;