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 /* information for default tables */
838 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
839 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
840 static const WCHAR szName[] = { 'N','a','m','e',0 };
841 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
842 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
843 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
844 static const WCHAR szType[] = { 'T','y','p','e',0 };
846 struct standard_table {
851 } MSI_standard_tables[] =
853 { szTables, szName, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
854 { szColumns, szTable, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
855 { szColumns, szNumber, 2, MSITYPE_VALID | 2},
856 { szColumns, szName, 3, MSITYPE_VALID | MSITYPE_STRING | 32},
857 { szColumns, szType, 4, MSITYPE_VALID | 2},
860 #define STANDARD_TABLE_COUNT \
861 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
863 UINT get_defaulttablecolumns( LPCWSTR szTable, MSICOLUMNINFO *colinfo, UINT *sz)
867 for(i=0; i<STANDARD_TABLE_COUNT; i++)
869 if( lstrcmpW( szTable, MSI_standard_tables[i].tablename ) )
871 if(colinfo && (n < *sz) )
873 colinfo[n].tablename = strdupW(MSI_standard_tables[i].tablename);
874 colinfo[n].colname = strdupW(MSI_standard_tables[i].columnname);
875 colinfo[n].number = MSI_standard_tables[i].number;
876 colinfo[n].type = MSI_standard_tables[i].type;
877 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
878 debugstr_w(colinfo[n].colname)); */
880 colinfo[n].offset = colinfo[n-1].offset
881 + bytes_per_column( &colinfo[n-1] );
883 colinfo[n].offset = 0;
886 if( colinfo && (n >= *sz) )
890 return ERROR_SUCCESS;
893 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
898 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
899 if( r != ERROR_SUCCESS )
901 str = HeapAlloc( GetProcessHeap(), 0, sz*sizeof (WCHAR));
904 r = msi_id2stringW( db->strings, stringid, str, &sz );
905 if( r == ERROR_SUCCESS )
907 HeapFree( GetProcessHeap(), 0, str );
911 static UINT get_tablecolumns( MSIDATABASE *db,
912 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
914 UINT r, i, n=0, table_id, count, maxcount = *sz;
915 MSITABLE *table = NULL;
916 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
918 /* first check if there is a default table with that name */
919 r = get_defaulttablecolumns( szTableName, colinfo, sz );
920 if( ( r == ERROR_SUCCESS ) && *sz )
923 r = get_table( db, szColumns, &table);
924 if( r != ERROR_SUCCESS )
926 ERR("table %s not available\n", debugstr_w(szColumns));
930 /* convert table and column names to IDs from the string table */
931 r = msi_string2idW( db->strings, szTableName, &table_id );
932 if( r != ERROR_SUCCESS )
934 release_table( db, table );
935 ERR("Couldn't find id for %s\n", debugstr_w(szTableName));
939 TRACE("Table id is %d\n", table_id);
941 count = table->row_count;
942 for( i=0; i<count; i++ )
944 if( table->data[ i ][ 0 ] != table_id )
948 UINT id = table->data[ i ] [ 2 ];
949 colinfo[n].tablename = MSI_makestring( db, table_id );
950 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
951 colinfo[n].colname = MSI_makestring( db, id );
952 colinfo[n].type = table->data[ i ] [ 3 ];
953 /* this assumes that columns are in order in the table */
955 colinfo[n].offset = colinfo[n-1].offset
956 + bytes_per_column( &colinfo[n-1] );
958 colinfo[n].offset = 0;
959 TRACE("table %s column %d is [%s] (%d) with type %08x "
960 "offset %d at row %d\n", debugstr_w(szTableName),
961 colinfo[n].number, debugstr_w(colinfo[n].colname),
962 id, colinfo[n].type, colinfo[n].offset, i);
963 if( n != (colinfo[n].number-1) )
965 ERR("oops. data in the _Columns table isn't in the right "
966 "order for table %s\n", debugstr_w(szTableName));
967 return ERROR_FUNCTION_FAILED;
971 if( colinfo && ( n >= maxcount ) )
976 release_table( db, table );
978 return ERROR_SUCCESS;
981 /* try to find the table name in the _Tables table */
982 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
984 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
985 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
986 UINT r, table_id = 0, i, count;
987 MSITABLE *table = NULL;
989 if( !lstrcmpW( name, szTables ) )
991 if( !lstrcmpW( name, szColumns ) )
994 r = msi_string2idW( db->strings, name, &table_id );
995 if( r != ERROR_SUCCESS )
997 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1001 r = get_table( db, szTables, &table);
1002 if( r != ERROR_SUCCESS )
1004 ERR("table %s not available\n", debugstr_w(szTables));
1008 /* count = table->size/2; */
1009 count = table->row_count;
1010 for( i=0; i<count; i++ )
1011 if( table->data[ i ][ 0 ] == table_id )
1014 release_table( db, table );
1019 ERR("Searched %d tables, but %d was not found\n", count, table_id );
1024 /* below is the query interface to a table */
1026 typedef struct tagMSITABLEVIEW
1031 MSICOLUMNINFO *columns;
1037 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1039 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1040 UINT offset, num_rows, n;
1043 return ERROR_INVALID_PARAMETER;
1045 if( (col==0) || (col>tv->num_cols) )
1046 return ERROR_INVALID_PARAMETER;
1048 /* how many rows are there ? */
1049 num_rows = tv->table->row_count;
1050 if( row >= num_rows )
1051 return ERROR_NO_MORE_ITEMS;
1053 if( tv->columns[col-1].offset >= tv->row_size )
1055 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1056 ERR("%p %p\n", tv, tv->columns );
1057 return ERROR_FUNCTION_FAILED;
1060 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1061 n = bytes_per_column( &tv->columns[col-1] );
1065 offset = tv->columns[col-1].offset/2;
1066 *val = tv->table->data[row][offset] +
1067 (tv->table->data[row][offset + 1] << 16);
1070 offset = tv->columns[col-1].offset/2;
1071 *val = tv->table->data[row][offset];
1074 ERR("oops! what is %d bytes per column?\n", n );
1075 return ERROR_FUNCTION_FAILED;
1078 /* TRACE("Data [%d][%d] = %d \n", row, col, *val ); */
1080 return ERROR_SUCCESS;
1084 * We need a special case for streams, as we need to reference column with
1085 * the name of the stream in the same table, and the table name
1086 * which may not be available at higher levels of the query
1088 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1090 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1091 UINT ival = 0, refcol = 0, r;
1095 static const WCHAR szDot[] = { '.', 0 };
1097 if( !view->ops->fetch_int )
1098 return ERROR_INVALID_PARAMETER;
1101 * The column marked with the type stream data seems to have a single number
1102 * which references the column containing the name of the stream data
1104 * Fetch the column to reference first.
1106 r = view->ops->fetch_int( view, row, col, &ival );
1107 if( r != ERROR_SUCCESS )
1110 /* now get the column with the name of the stream */
1111 r = view->ops->fetch_int( view, row, ival, &refcol );
1112 if( r != ERROR_SUCCESS )
1115 /* lookup the string value from the string table */
1116 sval = MSI_makestring( tv->db, refcol );
1118 return ERROR_INVALID_PARAMETER;
1120 len = strlenW( tv->name ) + 2 + strlenW( sval );
1121 full_name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1122 strcpyW( full_name, tv->name );
1123 strcatW( full_name, szDot );
1124 strcatW( full_name, sval );
1126 r = db_get_raw_stream( tv->db, full_name, stm );
1128 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1129 HeapFree( GetProcessHeap(), 0, full_name );
1130 HeapFree( GetProcessHeap(), 0, sval );
1135 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1137 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1141 return ERROR_INVALID_PARAMETER;
1143 if( (col==0) || (col>tv->num_cols) )
1144 return ERROR_INVALID_PARAMETER;
1146 if( tv->columns[col-1].offset >= tv->row_size )
1148 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1149 ERR("%p %p\n", tv, tv->columns );
1150 return ERROR_FUNCTION_FAILED;
1153 n = bytes_per_column( &tv->columns[col-1] );
1157 offset = tv->columns[col-1].offset/2;
1158 tv->table->data[row][offset] = val & 0xffff;
1159 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1162 offset = tv->columns[col-1].offset/2;
1163 tv->table->data[row][offset] = val;
1166 ERR("oops! what is %d bytes per column?\n", n );
1167 return ERROR_FUNCTION_FAILED;
1169 return ERROR_SUCCESS;
1172 UINT TABLE_insert_row( struct tagMSIVIEW *view, UINT *num )
1174 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1178 TRACE("%p\n", view);
1181 return ERROR_INVALID_PARAMETER;
1183 row = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, tv->row_size );
1185 return ERROR_NOT_ENOUGH_MEMORY;
1187 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1188 if( tv->table->data )
1189 p = HeapReAlloc( GetProcessHeap(), 0, tv->table->data, sz );
1191 p = HeapAlloc( GetProcessHeap(), 0, sz );
1194 HeapFree( GetProcessHeap(), 0, row );
1195 return ERROR_NOT_ENOUGH_MEMORY;
1198 tv->table->data = p;
1199 tv->table->data[tv->table->row_count] = row;
1200 *num = tv->table->row_count;
1201 tv->table->row_count++;
1203 return ERROR_SUCCESS;
1206 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1208 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1211 TRACE("%p %p\n", tv, record);
1215 release_table( tv->db, tv->table );
1219 r = get_table( tv->db, tv->name, &tv->table );
1220 if( r != ERROR_SUCCESS )
1223 return ERROR_SUCCESS;
1226 static UINT TABLE_close( struct tagMSIVIEW *view )
1228 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1230 TRACE("%p\n", view );
1233 return ERROR_FUNCTION_FAILED;
1235 release_table( tv->db, tv->table );
1238 return ERROR_SUCCESS;
1241 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1243 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1245 TRACE("%p %p %p\n", view, rows, cols );
1248 *cols = tv->num_cols;
1252 return ERROR_INVALID_PARAMETER;
1253 *rows = tv->table->row_count;
1256 return ERROR_SUCCESS;
1259 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1260 UINT n, LPWSTR *name, UINT *type )
1262 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1264 TRACE("%p %d %p %p\n", tv, n, name, type );
1266 if( ( n == 0 ) || ( n > tv->num_cols ) )
1267 return ERROR_INVALID_PARAMETER;
1271 *name = strdupW( tv->columns[n-1].colname );
1273 return ERROR_FUNCTION_FAILED;
1276 *type = tv->columns[n-1].type;
1278 return ERROR_SUCCESS;
1281 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1284 FIXME("%p %d %p\n", view, eModifyMode, rec );
1285 return ERROR_CALL_NOT_IMPLEMENTED;
1288 static UINT TABLE_delete( struct tagMSIVIEW *view )
1290 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1292 TRACE("%p\n", view );
1295 release_table( tv->db, tv->table );
1301 for( i=0; i<tv->num_cols; i++)
1303 HeapFree( GetProcessHeap(), 0, tv->columns[i].colname );
1304 HeapFree( GetProcessHeap(), 0, tv->columns[i].tablename );
1306 HeapFree( GetProcessHeap(), 0, tv->columns );
1310 HeapFree( GetProcessHeap(), 0, tv );
1312 return ERROR_SUCCESS;
1316 MSIVIEWOPS table_ops =
1324 TABLE_get_dimensions,
1325 TABLE_get_column_info,
1330 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1333 UINT r, sz, column_count;
1334 MSICOLUMNINFO *columns, *last_col;
1336 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1338 /* get the number of columns in this table */
1340 r = get_tablecolumns( db, name, NULL, &column_count );
1341 if( r != ERROR_SUCCESS )
1344 /* if there's no columns, there's no table */
1345 if( column_count == 0 )
1346 return ERROR_INVALID_PARAMETER;
1348 TRACE("Table found\n");
1350 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1351 tv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sz );
1353 return ERROR_FUNCTION_FAILED;
1355 columns = HeapAlloc( GetProcessHeap(), 0, column_count*sizeof (MSICOLUMNINFO));
1358 HeapFree( GetProcessHeap(), 0, tv );
1359 return ERROR_FUNCTION_FAILED;
1362 r = get_tablecolumns( db, name, columns, &column_count );
1363 if( r != ERROR_SUCCESS )
1365 HeapFree( GetProcessHeap(), 0, columns );
1366 HeapFree( GetProcessHeap(), 0, tv );
1367 return ERROR_FUNCTION_FAILED;
1370 TRACE("Table has %d columns\n", column_count);
1372 last_col = &columns[column_count-1];
1374 /* fill the structure */
1375 tv->view.ops = &table_ops;
1377 tv->columns = columns;
1378 tv->num_cols = column_count;
1380 tv->row_size = last_col->offset + bytes_per_column( last_col );
1382 TRACE("one row is %d bytes\n", tv->row_size );
1384 *view = (MSIVIEW*) tv;
1385 lstrcpyW( tv->name, name );
1387 return ERROR_SUCCESS;
1390 UINT MSI_CommitTables( MSIDATABASE *db )
1393 MSITABLE *table = NULL;
1397 r = save_string_table( db );
1398 if( r != ERROR_SUCCESS )
1400 ERR("failed to save string table r=%08x\n",r);
1404 for( table = db->first_table; table; table = table->next )
1406 r = save_table( db, table );
1407 if( r != ERROR_SUCCESS )
1409 ERR("failed to save table %s (r=%08x)\n",
1410 debugstr_w(table->name), r);
1415 /* force everything to reload next time */
1416 free_cached_tables( db );
1418 return ERROR_SUCCESS;