msi: Improve error checking when applying transform data.
[wine/dibdrv.git] / dlls / msi / table.c
blob1ef7d0e03564b842a4054e35e23a67ed148bf20b
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2005 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "wine/debug.h"
31 #include "msi.h"
32 #include "msiquery.h"
33 #include "objbase.h"
34 #include "objidl.h"
35 #include "msipriv.h"
36 #include "winnls.h"
38 #include "query.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
42 #define MSITABLE_HASH_TABLE_SIZE 37
44 typedef struct tagMSICOLUMNHASHENTRY
46 struct tagMSICOLUMNHASHENTRY *next;
47 UINT value;
48 UINT row;
49 } MSICOLUMNHASHENTRY;
51 typedef struct tagMSICOLUMNINFO
53 LPCWSTR tablename;
54 UINT number;
55 LPCWSTR colname;
56 UINT type;
57 UINT offset;
58 MSICOLUMNHASHENTRY **hash_table;
59 } MSICOLUMNINFO;
61 struct tagMSITABLE
63 USHORT **data;
64 UINT row_count;
65 struct list entry;
66 WCHAR name[1];
69 typedef struct tagMSITRANSFORM {
70 struct list entry;
71 IStorage *stg;
72 } MSITRANSFORM;
74 static const WCHAR szStringData[] = {
75 '_','S','t','r','i','n','g','D','a','t','a',0 };
76 static const WCHAR szStringPool[] = {
77 '_','S','t','r','i','n','g','P','o','o','l',0 };
79 #define MAX_STREAM_NAME 0x1f
81 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
82 MSICOLUMNINFO **pcols, UINT *pcount );
83 static UINT get_tablecolumns( MSIDATABASE *db,
84 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
85 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
87 static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
89 if( col->type & MSITYPE_STRING )
90 return 2;
91 if( (col->type & 0xff) > 4 )
92 ERR("Invalid column size!\n");
93 return col->type & 0xff;
96 static int utf2mime(int x)
98 if( (x>='0') && (x<='9') )
99 return x-'0';
100 if( (x>='A') && (x<='Z') )
101 return x-'A'+10;
102 if( (x>='a') && (x<='z') )
103 return x-'a'+10+26;
104 if( x=='.' )
105 return 10+26+26;
106 if( x=='_' )
107 return 10+26+26+1;
108 return -1;
111 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
113 DWORD count = MAX_STREAM_NAME;
114 DWORD ch, next;
115 LPWSTR out, p;
117 if( !bTable )
118 count = lstrlenW( in )+2;
119 out = msi_alloc( count*sizeof(WCHAR) );
120 p = out;
122 if( bTable )
124 *p++ = 0x4840;
125 count --;
127 while( count -- )
129 ch = *in++;
130 if( !ch )
132 *p = ch;
133 return out;
135 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
137 ch = utf2mime(ch) + 0x4800;
138 next = *in;
139 if( next && (next<0x80) )
141 next = utf2mime(next);
142 if( next >= 0 )
144 next += 0x3ffffc0;
145 ch += (next<<6);
146 in++;
150 *p++ = ch;
152 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
153 msi_free( out );
154 return NULL;
157 static int mime2utf(int x)
159 if( x<10 )
160 return x + '0';
161 if( x<(10+26))
162 return x - 10 + 'A';
163 if( x<(10+26+26))
164 return x - 10 - 26 + 'a';
165 if( x == (10+26+26) )
166 return '.';
167 return '_';
170 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
172 WCHAR ch;
173 DWORD count = 0;
175 while ( (ch = *in++) )
177 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
179 if( ch >= 0x4800 )
180 ch = mime2utf(ch-0x4800);
181 else
183 ch -= 0x3800;
184 *out++ = mime2utf(ch&0x3f);
185 count++;
186 ch = mime2utf((ch>>6)&0x3f);
189 *out++ = ch;
190 count++;
192 *out = 0;
193 return count;
196 void enum_stream_names( IStorage *stg )
198 IEnumSTATSTG *stgenum = NULL;
199 HRESULT r;
200 STATSTG stat;
201 ULONG n, count;
202 WCHAR name[0x40];
204 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
205 if( FAILED( r ) )
206 return;
208 n = 0;
209 while( 1 )
211 count = 0;
212 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
213 if( FAILED( r ) || !count )
214 break;
215 decode_streamname( stat.pwcsName, name );
216 TRACE("stream %2d -> %s %s\n", n,
217 debugstr_w(stat.pwcsName), debugstr_w(name) );
218 n++;
221 IEnumSTATSTG_Release( stgenum );
224 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
225 USHORT **pdata, UINT *psz )
227 HRESULT r;
228 UINT ret = ERROR_FUNCTION_FAILED;
229 VOID *data;
230 ULONG sz, count;
231 IStream *stm = NULL;
232 STATSTG stat;
233 LPWSTR encname;
235 encname = encode_streamname(TRUE, stname);
237 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
239 r = IStorage_OpenStream(stg, encname, NULL,
240 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
241 msi_free( encname );
242 if( FAILED( r ) )
244 WARN("open stream failed r = %08x - empty table?\n", r);
245 return ret;
248 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
249 if( FAILED( r ) )
251 WARN("open stream failed r = %08x!\n", r);
252 goto end;
255 if( stat.cbSize.QuadPart >> 32 )
257 WARN("Too big!\n");
258 goto end;
261 sz = stat.cbSize.QuadPart;
262 data = msi_alloc( sz );
263 if( !data )
265 WARN("couldn't allocate memory r=%08x!\n", r);
266 ret = ERROR_NOT_ENOUGH_MEMORY;
267 goto end;
270 r = IStream_Read(stm, data, sz, &count );
271 if( FAILED( r ) || ( count != sz ) )
273 msi_free( data );
274 WARN("read stream failed r = %08x!\n", r);
275 goto end;
278 *pdata = data;
279 *psz = sz;
280 ret = ERROR_SUCCESS;
282 end:
283 IStream_Release( stm );
285 return ret;
288 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
290 LPWSTR encname;
291 HRESULT r;
293 encname = encode_streamname(FALSE, stname);
295 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
297 r = IStorage_OpenStream(db->storage, encname, NULL,
298 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
299 if( FAILED( r ) )
301 MSITRANSFORM *transform;
303 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
305 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
306 r = IStorage_OpenStream( transform->stg, encname, NULL,
307 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
308 if (SUCCEEDED(r))
309 break;
313 msi_free( encname );
315 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
318 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
319 USHORT **pdata, UINT *psz )
321 HRESULT r;
322 UINT ret = ERROR_FUNCTION_FAILED;
323 VOID *data;
324 ULONG sz, count;
325 IStream *stm = NULL;
326 STATSTG stat;
328 r = db_get_raw_stream( db, stname, &stm );
329 if( r != ERROR_SUCCESS)
330 return ret;
331 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
332 if( FAILED( r ) )
334 WARN("open stream failed r = %08x!\n", r);
335 goto end;
338 if( stat.cbSize.QuadPart >> 32 )
340 WARN("Too big!\n");
341 goto end;
344 sz = stat.cbSize.QuadPart;
345 data = msi_alloc( sz );
346 if( !data )
348 WARN("couldn't allocate memory r=%08x!\n", r);
349 ret = ERROR_NOT_ENOUGH_MEMORY;
350 goto end;
353 r = IStream_Read(stm, data, sz, &count );
354 if( FAILED( r ) || ( count != sz ) )
356 msi_free( data );
357 WARN("read stream failed r = %08x!\n", r);
358 goto end;
361 *pdata = data;
362 *psz = sz;
363 ret = ERROR_SUCCESS;
365 end:
366 IStream_Release( stm );
368 return ret;
371 static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
372 LPVOID data, UINT sz )
374 HRESULT r;
375 UINT ret = ERROR_FUNCTION_FAILED;
376 ULONG count;
377 IStream *stm = NULL;
378 ULARGE_INTEGER size;
379 LARGE_INTEGER pos;
380 LPWSTR encname;
382 encname = encode_streamname(TRUE, stname );
383 r = IStorage_OpenStream( stg, encname, NULL,
384 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
385 if( FAILED(r) )
387 r = IStorage_CreateStream( stg, encname,
388 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
390 msi_free( encname );
391 if( FAILED( r ) )
393 WARN("open stream failed r = %08x\n", r);
394 return ret;
397 size.QuadPart = sz;
398 r = IStream_SetSize( stm, size );
399 if( FAILED( r ) )
401 WARN("Failed to SetSize\n");
402 goto end;
405 pos.QuadPart = 0;
406 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
407 if( FAILED( r ) )
409 WARN("Failed to Seek\n");
410 goto end;
413 r = IStream_Write(stm, data, sz, &count );
414 if( FAILED( r ) || ( count != sz ) )
416 WARN("Failed to Write\n");
417 goto end;
420 ret = ERROR_SUCCESS;
422 end:
423 IStream_Release( stm );
425 return ret;
428 static void free_table( MSITABLE *table )
430 int i;
431 for( i=0; i<table->row_count; i++ )
432 msi_free( table->data[i] );
433 msi_free( table->data );
434 msi_free( table );
437 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
439 const MSICOLUMNINFO *last_col = &cols[count-1];
440 if (!count)
441 return 0;
442 return last_col->offset + bytes_per_column( last_col );
445 /* add this table to the list of cached tables in the database */
446 static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
447 const MSICOLUMNINFO *cols, UINT num_cols )
449 MSITABLE *t;
450 USHORT *rawdata = NULL;
451 UINT rawsize = 0, i, j, row_size = 0;
453 TRACE("%s\n",debugstr_w(name));
455 /* nonexistent tables should be interpreted as empty tables */
456 t = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
457 if( !t )
458 return t;
460 row_size = msi_table_get_row_size( cols, num_cols );
462 t->row_count = 0;
463 t->data = NULL;
464 lstrcpyW( t->name, name );
466 /* if we can't read the table, just assume that it's empty */
467 read_stream_data( stg, name, &rawdata, &rawsize );
468 if( !rawdata )
469 return t;
471 TRACE("Read %d bytes\n", rawsize );
473 if( rawsize % row_size )
475 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
476 goto err;
479 t->row_count = rawsize / row_size;
480 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
481 if( !t->data )
482 goto err;
484 /* transpose all the data */
485 TRACE("Transposing data from %d rows\n", t->row_count );
486 for( i=0; i<t->row_count; i++ )
488 t->data[i] = msi_alloc( row_size );
489 if( !t->data[i] )
490 goto err;
492 for( j=0; j<num_cols; j++ )
494 UINT ofs = cols[j].offset/2;
495 UINT n = bytes_per_column( &cols[j] );
497 switch( n )
499 case 2:
500 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
501 break;
502 case 4:
503 t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
504 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
505 break;
506 default:
507 ERR("oops - unknown column width %d\n", n);
508 goto err;
513 msi_free( rawdata );
514 return t;
515 err:
516 msi_free( rawdata );
517 free_table( t );
518 return NULL;
521 void free_cached_tables( MSIDATABASE *db )
523 while( !list_empty( &db->tables ) )
525 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
527 list_remove( &t->entry );
528 free_table( t );
532 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
534 MSITABLE *t;
536 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
537 if( !lstrcmpW( name, t->name ) )
538 return t;
540 return NULL;
543 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
545 UINT r, column_count = 0;
546 MSICOLUMNINFO *columns;
548 /* get the number of columns in this table */
549 column_count = 0;
550 r = get_tablecolumns( db, name, NULL, &column_count );
551 if( r != ERROR_SUCCESS )
552 return r;
554 /* if there's no columns, there's no table */
555 if( column_count == 0 )
556 return ERROR_INVALID_PARAMETER;
558 TRACE("Table %s found\n", debugstr_w(name) );
560 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
561 if( !columns )
562 return ERROR_FUNCTION_FAILED;
564 r = get_tablecolumns( db, name, columns, &column_count );
565 if( r != ERROR_SUCCESS )
567 msi_free( columns );
568 return ERROR_FUNCTION_FAILED;
571 *pcols = columns;
572 *pcount = column_count;
574 return r;
577 static MSITABLE *get_table( MSIDATABASE *db, LPCWSTR name,
578 const MSICOLUMNINFO *cols, UINT num_cols )
580 MSITABLE *table;
582 /* first, see if the table is cached */
583 table = find_cached_table( db, name );
584 if( table )
585 return table;
587 table = read_table_from_storage( db->storage, name, cols, num_cols );
588 if( table )
589 list_add_head( &db->tables, &table->entry );
591 return table;
594 static UINT save_table( MSIDATABASE *db, MSITABLE *t )
596 USHORT *rawdata = NULL, *p;
597 UINT rawsize, r, i, j, row_size, num_cols = 0;
598 MSICOLUMNINFO *cols = NULL;
600 TRACE("Saving %s\n", debugstr_w( t->name ) );
602 r = table_get_column_info( db, t->name, &cols, &num_cols );
603 if( r != ERROR_SUCCESS )
604 return r;
606 row_size = msi_table_get_row_size( cols, num_cols );
608 rawsize = t->row_count * row_size;
609 rawdata = msi_alloc_zero( rawsize );
610 if( !rawdata )
612 r = ERROR_NOT_ENOUGH_MEMORY;
613 goto err;
616 p = rawdata;
617 for( i=0; i<num_cols; i++ )
619 for( j=0; j<t->row_count; j++ )
621 UINT offset = cols[i].offset;
623 *p++ = t->data[j][offset/2];
624 if( 4 == bytes_per_column( &cols[i] ) )
625 *p++ = t->data[j][offset/2+1];
629 TRACE("writing %d bytes\n", rawsize);
630 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
632 err:
633 msi_free_colinfo( cols, num_cols );
634 msi_free( cols );
635 msi_free( rawdata );
637 return r;
640 HRESULT init_string_table( IStorage *stg )
642 HRESULT r;
643 USHORT zero[2] = { 0, 0 };
644 ULONG count = 0;
645 IStream *stm = NULL;
646 LPWSTR encname;
648 encname = encode_streamname(TRUE, szStringPool );
650 /* create the StringPool stream... add the zero string to it*/
651 r = IStorage_CreateStream( stg, encname,
652 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
653 msi_free( encname );
654 if( r )
656 TRACE("Failed\n");
657 return r;
660 r = IStream_Write(stm, zero, sizeof zero, &count );
661 IStream_Release( stm );
663 if( FAILED( r ) || ( count != sizeof zero ) )
665 TRACE("Failed\n");
666 return E_FAIL;
669 /* create the StringData stream... make it zero length */
670 encname = encode_streamname(TRUE, szStringData );
671 r = IStorage_CreateStream( stg, encname,
672 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
673 msi_free( encname );
674 if( r )
676 TRACE("Failed\n");
677 return E_FAIL;
679 IStream_Release( stm );
681 return r;
684 string_table *load_string_table( IStorage *stg )
686 string_table *st = NULL;
687 CHAR *data = NULL;
688 USHORT *pool = NULL;
689 UINT r, datasize = 0, poolsize = 0, codepage;
690 DWORD i, count, offset, len, n, refs;
692 r = read_stream_data( stg, szStringPool, &pool, &poolsize );
693 if( r != ERROR_SUCCESS)
694 goto end;
695 r = read_stream_data( stg, szStringData, (USHORT**)&data, &datasize );
696 if( r != ERROR_SUCCESS)
697 goto end;
699 count = poolsize/4;
700 if( poolsize > 4 )
701 codepage = pool[0] | ( pool[1] << 16 );
702 else
703 codepage = CP_ACP;
704 st = msi_init_stringtable( count, codepage );
706 offset = 0;
707 n = 1;
708 i = 1;
709 while( i<count )
711 /* the string reference count is always the second word */
712 refs = pool[i*2+1];
714 /* empty entries have two zeros, still have a string id */
715 if (pool[i*2] == 0 && refs == 0)
717 i++;
718 n++;
719 continue;
723 * If a string is over 64k, the previous string entry is made null
724 * and its the high word of the length is inserted in the null string's
725 * reference count field.
727 if( pool[i*2] == 0)
729 len = (pool[i*2+3] << 16) + pool[i*2+2];
730 i += 2;
732 else
734 len = pool[i*2];
735 i += 1;
738 if ( (offset + len) > datasize )
740 ERR("string table corrupt?\n");
741 break;
744 r = msi_addstring( st, n, data+offset, len, refs );
745 if( r != n )
746 ERR("Failed to add string %d\n", n );
747 n++;
748 offset += len;
751 if ( datasize != offset )
752 ERR("string table load failed! (%08x != %08x), please report\n", datasize, offset );
754 TRACE("Loaded %d strings\n", count);
756 end:
757 msi_free( pool );
758 msi_free( data );
760 return st;
763 static UINT save_string_table( MSIDATABASE *db )
765 UINT i, count, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
766 UINT ret = ERROR_FUNCTION_FAILED;
767 CHAR *data = NULL;
768 USHORT *pool = NULL;
770 TRACE("\n");
772 /* construct the new table in memory first */
773 count = msi_string_totalsize( db->strings, &datasize, &poolsize );
775 TRACE("%u %u %u\n", count, datasize, poolsize );
777 pool = msi_alloc( poolsize );
778 if( ! pool )
780 WARN("Failed to alloc pool %d bytes\n", poolsize );
781 goto err;
783 data = msi_alloc( datasize );
784 if( ! data )
786 WARN("Failed to alloc data %d bytes\n", poolsize );
787 goto err;
790 used = 0;
791 codepage = msi_string_get_codepage( db->strings );
792 pool[0]=codepage&0xffff;
793 pool[1]=(codepage>>16);
794 n = 1;
795 for( i=1; i<count; i++ )
797 sz = datasize - used;
798 r = msi_id2stringA( db->strings, i, data+used, &sz );
799 if( r != ERROR_SUCCESS )
801 ERR("failed to fetch string\n");
802 sz = 0;
804 if( sz && (sz < (datasize - used ) ) )
805 sz--;
807 if (sz)
808 pool[ n*2 + 1 ] = msi_id_refcount( db->strings, i );
809 else
810 pool[ n*2 + 1 ] = 0;
811 if (sz < 0x10000)
813 pool[ n*2 ] = sz;
814 n++;
816 else
818 pool[ n*2 ] = 0;
819 pool[ n*2 + 2 ] = sz&0xffff;
820 pool[ n*2 + 3 ] = (sz>>16);
821 n += 2;
823 used += sz;
824 if( used > datasize )
826 ERR("oops overran %d >= %d\n", used, datasize);
827 goto err;
831 if( used != datasize )
833 ERR("oops used %d != datasize %d\n", used, datasize);
834 goto err;
837 /* write the streams */
838 r = write_stream_data( db->storage, szStringData, data, datasize );
839 TRACE("Wrote StringData r=%08x\n", r);
840 if( r )
841 goto err;
842 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
843 TRACE("Wrote StringPool r=%08x\n", r);
844 if( r )
845 goto err;
847 ret = ERROR_SUCCESS;
849 err:
850 msi_free( data );
851 msi_free( pool );
853 return ret;
856 /* information for default tables */
857 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
858 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
859 static const WCHAR szName[] = { 'N','a','m','e',0 };
860 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
861 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
862 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
863 static const WCHAR szType[] = { 'T','y','p','e',0 };
865 static const MSICOLUMNINFO _Columns_cols[4] = {
866 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
867 { szColumns, 2, szNumber, MSITYPE_VALID | 2, 2 },
868 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
869 { szColumns, 4, szType, MSITYPE_VALID | 2, 6 },
871 static const MSICOLUMNINFO _Tables_cols[1] = {
872 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
875 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
877 const MSICOLUMNINFO *p;
878 DWORD i, n;
880 TRACE("%s\n", debugstr_w(name));
882 if (!lstrcmpW( name, szTables ))
884 p = _Tables_cols;
885 n = 1;
887 else if (!lstrcmpW( name, szColumns ))
889 p = _Columns_cols;
890 n = 4;
892 else
893 return ERROR_FUNCTION_FAILED;
895 /* duplicate the string data so we can free it in msi_free_colinfo */
896 for (i=0; i<n; i++)
898 if (colinfo && (i < *sz) )
900 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
901 colinfo[i].tablename = strdupW( p[i].tablename );
902 colinfo[i].colname = strdupW( p[i].colname );
904 if( colinfo && (i >= *sz) )
905 break;
907 *sz = n;
908 return ERROR_SUCCESS;
911 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
913 UINT i;
915 for( i=0; i<count; i++ )
917 msi_free( (LPWSTR) colinfo[i].tablename );
918 msi_free( (LPWSTR) colinfo[i].colname );
919 msi_free( colinfo[i].hash_table );
923 static LPWSTR msi_makestring( MSIDATABASE *db, UINT stringid)
925 return strdupW(msi_string_lookup_id( db->strings, stringid ));
928 static UINT get_tablecolumns( MSIDATABASE *db,
929 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
931 UINT r, i, n=0, table_id, count, maxcount = *sz;
932 MSITABLE *table = NULL;
934 /* first check if there is a default table with that name */
935 r = get_defaulttablecolumns( szTableName, colinfo, sz );
936 if( ( r == ERROR_SUCCESS ) && *sz )
937 return r;
939 table = get_table( db, szColumns, _Columns_cols, 4 );
940 if( !table )
942 ERR("couldn't load _Columns table\n");
943 return ERROR_FUNCTION_FAILED;
946 /* convert table and column names to IDs from the string table */
947 r = msi_string2idW( db->strings, szTableName, &table_id );
948 if( r != ERROR_SUCCESS )
950 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
951 return r;
954 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
956 count = table->row_count;
957 for( i=0; i<count; i++ )
959 if( table->data[ i ][ 0 ] != table_id )
960 continue;
961 if( colinfo )
963 UINT id = table->data[ i ] [ 2 ];
964 colinfo[n].tablename = msi_makestring( db, table_id );
965 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
966 colinfo[n].colname = msi_makestring( db, id );
967 colinfo[n].type = table->data[ i ] [ 3 ] ^ 0x8000;
968 colinfo[n].hash_table = NULL;
969 /* this assumes that columns are in order in the table */
970 if( n )
971 colinfo[n].offset = colinfo[n-1].offset
972 + bytes_per_column( &colinfo[n-1] );
973 else
974 colinfo[n].offset = 0;
975 TRACE("table %s column %d is [%s] (%d) with type %08x "
976 "offset %d at row %d\n", debugstr_w(szTableName),
977 colinfo[n].number, debugstr_w(colinfo[n].colname),
978 id, colinfo[n].type, colinfo[n].offset, i);
979 if( n != (colinfo[n].number-1) )
981 ERR("oops. data in the _Columns table isn't in the right "
982 "order for table %s\n", debugstr_w(szTableName));
983 msi_free_colinfo(colinfo, n+1 );
984 return ERROR_FUNCTION_FAILED;
987 n++;
988 if( colinfo && ( n >= maxcount ) )
989 break;
991 *sz = n;
993 return ERROR_SUCCESS;
996 /* try to find the table name in the _Tables table */
997 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
999 UINT r, table_id = 0, i, count;
1000 MSITABLE *table = NULL;
1002 if( !lstrcmpW( name, szTables ) )
1003 return TRUE;
1004 if( !lstrcmpW( name, szColumns ) )
1005 return TRUE;
1007 r = msi_string2idW( db->strings, name, &table_id );
1008 if( r != ERROR_SUCCESS )
1010 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1011 return FALSE;
1014 table = get_table( db, szTables, _Tables_cols, 1 );
1015 if( !table )
1017 TRACE("table %s not available\n", debugstr_w(szTables));
1018 return FALSE;
1021 /* count = table->size/2; */
1022 count = table->row_count;
1023 for( i=0; i<count; i++ )
1024 if( table->data[ i ][ 0 ] == table_id )
1025 break;
1027 if (i!=count)
1028 return TRUE;
1030 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1032 return FALSE;
1035 /* below is the query interface to a table */
1037 typedef struct tagMSITABLEVIEW
1039 MSIVIEW view;
1040 MSIDATABASE *db;
1041 MSITABLE *table;
1042 MSICOLUMNINFO *columns;
1043 UINT num_cols;
1044 UINT row_size;
1045 WCHAR name[1];
1046 } MSITABLEVIEW;
1048 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1050 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1051 UINT offset, num_rows, n;
1053 if( !tv->table )
1054 return ERROR_INVALID_PARAMETER;
1056 if( (col==0) || (col>tv->num_cols) )
1057 return ERROR_INVALID_PARAMETER;
1059 /* how many rows are there ? */
1060 num_rows = tv->table->row_count;
1061 if( row >= num_rows )
1062 return ERROR_NO_MORE_ITEMS;
1064 if( tv->columns[col-1].offset >= tv->row_size )
1066 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1067 ERR("%p %p\n", tv, tv->columns );
1068 return ERROR_FUNCTION_FAILED;
1071 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1072 n = bytes_per_column( &tv->columns[col-1] );
1073 switch( n )
1075 case 4:
1076 offset = tv->columns[col-1].offset/2;
1077 *val = tv->table->data[row][offset] +
1078 (tv->table->data[row][offset + 1] << 16);
1079 break;
1080 case 2:
1081 offset = tv->columns[col-1].offset/2;
1082 *val = tv->table->data[row][offset];
1083 break;
1084 default:
1085 ERR("oops! what is %d bytes per column?\n", n );
1086 return ERROR_FUNCTION_FAILED;
1089 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1091 return ERROR_SUCCESS;
1095 * We need a special case for streams, as we need to reference column with
1096 * the name of the stream in the same table, and the table name
1097 * which may not be available at higher levels of the query
1099 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1101 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1102 UINT ival = 0, refcol = 0, r;
1103 LPCWSTR sval;
1104 LPWSTR full_name;
1105 DWORD len;
1106 static const WCHAR szDot[] = { '.', 0 };
1108 if( !view->ops->fetch_int )
1109 return ERROR_INVALID_PARAMETER;
1112 * The column marked with the type stream data seems to have a single number
1113 * which references the column containing the name of the stream data
1115 * Fetch the column to reference first.
1117 r = view->ops->fetch_int( view, row, col, &ival );
1118 if( r != ERROR_SUCCESS )
1119 return r;
1121 /* now get the column with the name of the stream */
1122 r = view->ops->fetch_int( view, row, ival, &refcol );
1123 if( r != ERROR_SUCCESS )
1124 return r;
1126 /* lookup the string value from the string table */
1127 sval = msi_string_lookup_id( tv->db->strings, refcol );
1128 if( !sval )
1129 return ERROR_INVALID_PARAMETER;
1131 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1132 full_name = msi_alloc( len*sizeof(WCHAR) );
1133 lstrcpyW( full_name, tv->name );
1134 lstrcatW( full_name, szDot );
1135 lstrcatW( full_name, sval );
1137 r = db_get_raw_stream( tv->db, full_name, stm );
1138 if( r )
1139 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1140 msi_free( full_name );
1142 return r;
1145 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1147 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1148 UINT offset, n;
1150 if( !tv->table )
1151 return ERROR_INVALID_PARAMETER;
1153 if( (col==0) || (col>tv->num_cols) )
1154 return ERROR_INVALID_PARAMETER;
1156 if( tv->columns[col-1].offset >= tv->row_size )
1158 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1159 ERR("%p %p\n", tv, tv->columns );
1160 return ERROR_FUNCTION_FAILED;
1163 n = bytes_per_column( &tv->columns[col-1] );
1164 switch( n )
1166 case 4:
1167 offset = tv->columns[col-1].offset/2;
1168 tv->table->data[row][offset] = val & 0xffff;
1169 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1170 break;
1171 case 2:
1172 offset = tv->columns[col-1].offset/2;
1173 tv->table->data[row][offset] = val;
1174 break;
1175 default:
1176 ERR("oops! what is %d bytes per column?\n", n );
1177 return ERROR_FUNCTION_FAILED;
1179 return ERROR_SUCCESS;
1182 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
1184 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1185 USHORT **p, *row;
1186 UINT sz;
1188 TRACE("%p\n", view);
1190 if( !tv->table )
1191 return ERROR_INVALID_PARAMETER;
1193 row = msi_alloc_zero( tv->row_size );
1194 if( !row )
1195 return ERROR_NOT_ENOUGH_MEMORY;
1197 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1198 if( tv->table->data )
1199 p = msi_realloc( tv->table->data, sz );
1200 else
1201 p = msi_alloc( sz );
1202 if( !p )
1204 msi_free( row );
1205 return ERROR_NOT_ENOUGH_MEMORY;
1208 tv->table->data = p;
1209 tv->table->data[tv->table->row_count] = row;
1210 *num = tv->table->row_count;
1211 tv->table->row_count++;
1213 return ERROR_SUCCESS;
1216 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1218 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1220 TRACE("%p %p\n", tv, record);
1222 TRACE("There are %d columns\n", tv->num_cols );
1223 tv->table = get_table( tv->db, tv->name, tv->columns, tv->num_cols );
1224 if( !tv->table )
1225 return ERROR_FUNCTION_FAILED;
1227 return ERROR_SUCCESS;
1230 static UINT TABLE_close( struct tagMSIVIEW *view )
1232 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1234 TRACE("%p\n", view );
1236 if( !tv->table )
1237 return ERROR_FUNCTION_FAILED;
1239 tv->table = NULL;
1241 return ERROR_SUCCESS;
1244 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1246 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1248 TRACE("%p %p %p\n", view, rows, cols );
1250 if( cols )
1251 *cols = tv->num_cols;
1252 if( rows )
1254 if( !tv->table )
1255 return ERROR_INVALID_PARAMETER;
1256 *rows = tv->table->row_count;
1259 return ERROR_SUCCESS;
1262 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1263 UINT n, LPWSTR *name, UINT *type )
1265 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1267 TRACE("%p %d %p %p\n", tv, n, name, type );
1269 if( ( n == 0 ) || ( n > tv->num_cols ) )
1270 return ERROR_INVALID_PARAMETER;
1272 if( name )
1274 *name = strdupW( tv->columns[n-1].colname );
1275 if( !*name )
1276 return ERROR_FUNCTION_FAILED;
1278 if( type )
1279 *type = tv->columns[n-1].type;
1281 return ERROR_SUCCESS;
1284 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1286 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1288 UINT r, row, i;
1290 /* check there's no null values where they're not allowed */
1291 for( i = 0; i < tv->num_cols; i++ )
1293 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1294 continue;
1296 if ( tv->columns[i].type & MSITYPE_STRING )
1298 LPCWSTR str;
1300 str = MSI_RecordGetString( rec, i+1 );
1301 if (str == NULL || str[0] == 0)
1302 return ERROR_INVALID_DATA;
1304 else
1306 UINT n;
1308 n = MSI_RecordGetInteger( rec, i+1 );
1309 if (n == MSI_NULL_INTEGER)
1310 return ERROR_INVALID_DATA;
1314 /* check there's no duplicate keys */
1315 r = msi_table_find_row( tv, rec, &row );
1316 if (r == ERROR_SUCCESS)
1317 return ERROR_INVALID_DATA;
1319 return ERROR_SUCCESS;
1322 static UINT msi_table_modify_row( MSITABLEVIEW *tv, MSIRECORD *rec,
1323 UINT row, UINT mask )
1325 UINT i, val, r = ERROR_SUCCESS;
1327 TRACE("%p %p %u %08x\n", tv, rec, row, mask );
1329 for( i = 0; i < tv->num_cols; i++ )
1331 /* set keys or values specified in the mask */
1332 if( (~tv->columns[i].type & MSITYPE_KEY) && (~mask & (1<<i)) )
1333 continue;
1335 if( (tv->columns[i].type & MSITYPE_STRING) &&
1336 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1338 const WCHAR *str = MSI_RecordGetString( rec, i+1 );
1339 val = msi_addstringW( tv->db->strings, 0, str, -1, 1 );
1341 else
1343 val = MSI_RecordGetInteger( rec, i+1 );
1344 if ( 2 == bytes_per_column( &tv->columns[i] ) )
1345 val ^= 0x8000;
1346 else
1347 val ^= 0x80000000;
1349 r = TABLE_set_int( &tv->view, row, i+1, val );
1350 if( r )
1351 break;
1354 return r;
1357 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1359 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1360 UINT r, row = -1;
1362 TRACE("%p %p\n", tv, rec );
1364 /* check that the key is unique - can we find a matching row? */
1365 r = table_validate_new( tv, rec );
1366 if( r != ERROR_SUCCESS )
1367 return ERROR_FUNCTION_FAILED;
1369 r = table_create_new_row( view, &row );
1370 TRACE("insert_row returned %08x\n", r);
1371 if( r != ERROR_SUCCESS )
1372 return r;
1374 return msi_table_modify_row( tv, rec, row, ~0 );
1377 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1378 MSIRECORD *rec)
1380 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1381 UINT r;
1383 TRACE("%p %d %p\n", view, eModifyMode, rec );
1385 if (!tv->table)
1387 r = TABLE_execute( view, NULL );
1388 if( r )
1389 return r;
1392 switch (eModifyMode)
1394 case MSIMODIFY_VALIDATE_NEW:
1395 r = table_validate_new( tv, rec );
1396 break;
1398 case MSIMODIFY_INSERT_TEMPORARY:
1399 r = table_validate_new( tv, rec );
1400 if (r != ERROR_SUCCESS)
1401 break;
1402 r = TABLE_insert_row( view, rec );
1403 break;
1405 case MSIMODIFY_REFRESH:
1406 case MSIMODIFY_INSERT:
1407 case MSIMODIFY_UPDATE:
1408 case MSIMODIFY_ASSIGN:
1409 case MSIMODIFY_REPLACE:
1410 case MSIMODIFY_MERGE:
1411 case MSIMODIFY_DELETE:
1412 case MSIMODIFY_VALIDATE:
1413 case MSIMODIFY_VALIDATE_FIELD:
1414 case MSIMODIFY_VALIDATE_DELETE:
1415 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1416 r = ERROR_CALL_NOT_IMPLEMENTED;
1417 break;
1419 default:
1420 r = ERROR_INVALID_DATA;
1423 return r;
1426 static UINT TABLE_delete( struct tagMSIVIEW *view )
1428 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1430 TRACE("%p\n", view );
1432 tv->table = NULL;
1434 if( tv->columns )
1436 msi_free_colinfo( tv->columns, tv->num_cols );
1437 msi_free( tv->columns );
1439 tv->columns = NULL;
1441 msi_free( tv );
1443 return ERROR_SUCCESS;
1446 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1447 UINT val, UINT *row, MSIITERHANDLE *handle )
1449 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1450 const MSICOLUMNHASHENTRY *entry;
1452 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1454 if( !tv->table )
1455 return ERROR_INVALID_PARAMETER;
1457 if( (col==0) || (col > tv->num_cols) )
1458 return ERROR_INVALID_PARAMETER;
1460 if( !tv->columns[col-1].hash_table )
1462 UINT i;
1463 UINT num_rows = tv->table->row_count;
1464 MSICOLUMNHASHENTRY **hash_table;
1465 MSICOLUMNHASHENTRY *new_entry;
1467 if( tv->columns[col-1].offset >= tv->row_size )
1469 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1470 ERR("%p %p\n", tv, tv->columns );
1471 return ERROR_FUNCTION_FAILED;
1474 /* allocate contiguous memory for the table and its entries so we
1475 * don't have to do an expensive cleanup */
1476 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1477 num_rows * sizeof(MSICOLUMNHASHENTRY));
1478 if (!hash_table)
1479 return ERROR_OUTOFMEMORY;
1481 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1482 tv->columns[col-1].hash_table = hash_table;
1484 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1486 for (i = 0; i < num_rows; i++, new_entry++)
1488 UINT row_value, n;
1489 UINT offset = i + (tv->columns[col-1].offset/2) * num_rows;
1490 n = bytes_per_column( &tv->columns[col-1] );
1491 switch( n )
1493 case 4:
1494 offset = tv->columns[col-1].offset/2;
1495 row_value = tv->table->data[i][offset] +
1496 (tv->table->data[i][offset + 1] << 16);
1497 break;
1498 case 2:
1499 offset = tv->columns[col-1].offset/2;
1500 row_value = tv->table->data[i][offset];
1501 break;
1502 default:
1503 ERR("oops! what is %d bytes per column?\n", n );
1504 continue;
1507 new_entry->next = NULL;
1508 new_entry->value = row_value;
1509 new_entry->row = i;
1510 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1512 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1513 while (prev_entry->next)
1514 prev_entry = prev_entry->next;
1515 prev_entry->next = new_entry;
1517 else
1518 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1522 if( !*handle )
1523 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1524 else
1525 entry = ((const MSICOLUMNHASHENTRY *)*handle)->next;
1527 while (entry && entry->value != val)
1528 entry = entry->next;
1530 *handle = (MSIITERHANDLE)entry;
1531 if (!entry)
1532 return ERROR_NO_MORE_ITEMS;
1534 *row = entry->row;
1535 return ERROR_SUCCESS;
1539 static const MSIVIEWOPS table_ops =
1541 TABLE_fetch_int,
1542 TABLE_fetch_stream,
1543 TABLE_set_int,
1544 TABLE_insert_row,
1545 TABLE_execute,
1546 TABLE_close,
1547 TABLE_get_dimensions,
1548 TABLE_get_column_info,
1549 TABLE_modify,
1550 TABLE_delete,
1551 TABLE_find_matching_rows
1554 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1556 MSITABLEVIEW *tv ;
1557 UINT r, sz, column_count;
1558 MSICOLUMNINFO *columns;
1560 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1562 /* get the number of columns in this table */
1563 column_count = 0;
1564 r = get_tablecolumns( db, name, NULL, &column_count );
1565 if( r != ERROR_SUCCESS )
1566 return r;
1568 /* if there's no columns, there's no table */
1569 if( column_count == 0 )
1570 return ERROR_INVALID_PARAMETER;
1572 TRACE("Table found\n");
1574 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1575 tv = msi_alloc_zero( sz );
1576 if( !tv )
1577 return ERROR_FUNCTION_FAILED;
1579 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO));
1580 if( !columns )
1582 msi_free( tv );
1583 return ERROR_FUNCTION_FAILED;
1586 r = get_tablecolumns( db, name, columns, &column_count );
1587 if( r != ERROR_SUCCESS )
1589 msi_free( columns );
1590 msi_free( tv );
1591 return ERROR_FUNCTION_FAILED;
1594 TRACE("Table has %d columns\n", column_count);
1596 /* fill the structure */
1597 tv->view.ops = &table_ops;
1598 tv->db = db;
1599 tv->columns = columns;
1600 tv->num_cols = column_count;
1601 tv->table = NULL;
1602 tv->row_size = msi_table_get_row_size( columns, column_count );
1604 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
1606 *view = (MSIVIEW*) tv;
1607 lstrcpyW( tv->name, name );
1609 return ERROR_SUCCESS;
1612 UINT MSI_CommitTables( MSIDATABASE *db )
1614 UINT r;
1615 MSITABLE *table = NULL;
1617 TRACE("%p\n",db);
1619 r = save_string_table( db );
1620 if( r != ERROR_SUCCESS )
1622 WARN("failed to save string table r=%08x\n",r);
1623 return r;
1626 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1628 r = save_table( db, table );
1629 if( r != ERROR_SUCCESS )
1631 WARN("failed to save table %s (r=%08x)\n",
1632 debugstr_w(table->name), r);
1633 return r;
1637 /* force everything to reload next time */
1638 free_cached_tables( db );
1640 return ERROR_SUCCESS;
1643 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
1645 if (!table)
1646 return MSICONDITION_ERROR;
1648 return MSICONDITION_FALSE;
1651 static MSIRECORD *msi_get_transform_record( MSITABLEVIEW *tv, string_table *st, USHORT *rawdata )
1653 UINT i, val, ofs = 0;
1654 USHORT mask = *rawdata++;
1655 MSICOLUMNINFO *columns = tv->columns;
1656 MSIRECORD *rec;
1658 rec = MSI_CreateRecord( tv->num_cols );
1659 if( !rec )
1660 return rec;
1662 TRACE("row -> \n");
1663 for( i=0; i<tv->num_cols; i++ )
1665 UINT n = bytes_per_column( &columns[i] );
1667 if ( (mask&1) && (i>=(mask>>8)) )
1668 break;
1669 /* all keys must be present */
1670 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
1671 continue;
1673 switch( n )
1675 case 2:
1676 val = rawdata[ofs];
1677 if( (columns[i].type & MSITYPE_STRING) &&
1678 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1680 LPCWSTR sval = msi_string_lookup_id( st, val );
1681 MSI_RecordSetStringW( rec, i+1, sval );
1682 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
1684 else
1686 if (val)
1687 MSI_RecordSetInteger( rec, i+1, val^0x8000 );
1688 TRACE(" field %d [0x%04x]\n", i+1, val );
1690 break;
1691 case 4:
1692 val = (rawdata[ofs] + (rawdata[ofs + 1]<<16));
1693 if (val)
1694 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
1695 TRACE(" field %d [0x%08x]\n", i+1, val );
1696 break;
1697 default:
1698 ERR("oops - unknown column width %d\n", n);
1699 break;
1701 ofs += n/2;
1703 return rec;
1706 static void dump_record( MSIRECORD *rec )
1708 UINT i, n;
1710 n = MSI_RecordGetFieldCount( rec );
1711 for( i=1; i<=n; i++ )
1713 LPCWSTR sval = MSI_RecordGetString( rec, i );
1715 if( MSI_RecordIsNull( rec, i ) )
1716 TRACE("row -> []\n");
1717 else if( (sval = MSI_RecordGetString( rec, i )) )
1718 TRACE("row -> [%s]\n", debugstr_w(sval));
1719 else
1720 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
1724 static void dump_table( string_table *st, USHORT *rawdata, UINT rawsize )
1726 LPCWSTR sval;
1727 UINT i;
1729 for( i=0; i<(rawsize/2); i++ )
1731 sval = msi_string_lookup_id( st, rawdata[i] );
1732 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
1736 static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
1738 LPCWSTR str;
1739 UINT i, r, *data;
1741 data = msi_alloc( tv->num_cols *sizeof (UINT) );
1742 for( i=0; i<tv->num_cols; i++ )
1744 data[i] = 0;
1746 if ( ~tv->columns[i].type & MSITYPE_KEY )
1747 continue;
1749 /* turn the transform column value into a row value */
1750 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
1751 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1753 str = MSI_RecordGetString( rec, i+1 );
1754 r = msi_string2idW( tv->db->strings, str, &data[i] );
1756 /* if there's no matching string in the string table,
1757 these keys can't match any record, so fail now. */
1758 if( ERROR_SUCCESS != r )
1760 msi_free( data );
1761 return NULL;
1764 else
1765 data[i] = MSI_RecordGetInteger( rec, i+1 );
1767 return data;
1770 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, UINT *data )
1772 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
1774 for( i=0; i<tv->num_cols; i++ )
1776 if ( ~tv->columns[i].type & MSITYPE_KEY )
1777 continue;
1779 /* turn the transform column value into a row value */
1780 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
1781 if ( r != ERROR_SUCCESS )
1783 ERR("TABLE_fetch_int shouldn't fail here\n");
1784 break;
1787 /* if this key matches, move to the next column */
1788 if ( x != data[i] )
1790 ret = ERROR_FUNCTION_FAILED;
1791 break;
1794 ret = ERROR_SUCCESS;
1797 return ret;
1800 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
1802 UINT i, r = ERROR_FUNCTION_FAILED, *data;
1804 data = msi_record_to_row( tv, rec );
1805 if( !data )
1806 return r;
1807 for( i=0; i<tv->table->row_count; i++ )
1809 r = msi_row_matches( tv, i, data );
1810 if( r == ERROR_SUCCESS )
1812 *row = i;
1813 break;
1816 msi_free( data );
1817 return r;
1820 static UINT msi_delete_row( MSITABLEVIEW *tv, UINT row )
1822 UINT i;
1823 for( i=1; i<=tv->num_cols; i++ )
1824 tv->view.ops->set_int( &tv->view, row, i, 0 );
1825 return ERROR_SUCCESS;
1828 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
1829 string_table *st, LPCWSTR name )
1831 UINT rawsize = 0;
1832 USHORT *rawdata = NULL;
1833 MSITABLEVIEW *tv = NULL;
1834 UINT r, n, sz, i, mask;
1835 MSIRECORD *rec = NULL;
1836 UINT colcol = 0;
1838 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
1840 /* read the transform data */
1841 read_stream_data( stg, name, &rawdata, &rawsize );
1842 if ( !rawdata )
1844 TRACE("table %s empty\n", debugstr_w(name) );
1845 return ERROR_INVALID_TABLE;
1848 /* create a table view */
1849 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
1850 if( r != ERROR_SUCCESS )
1851 goto err;
1853 r = tv->view.ops->execute( &tv->view, NULL );
1854 if( r != ERROR_SUCCESS )
1855 goto err;
1857 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1858 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
1860 /* interpret the data */
1861 r = ERROR_SUCCESS;
1862 for( n=0; n < (rawsize/2); )
1864 mask = rawdata[n];
1866 if (mask&1)
1869 * if the low bit is set, columns are continuous and
1870 * the number of columns is specified in the high byte
1872 sz = 2 + tv->row_size;
1874 else
1877 * If the low bit is not set, rowdata[n] is a bitmask.
1878 * Excepting for key fields, which are always present,
1879 * each bit indicates that a field is present in the transform record.
1881 * rawdata[n] == 0 is a special case ... only the keys will be present
1882 * and it means that this row should be deleted.
1884 sz = 2;
1885 for( i=0; i<tv->num_cols; i++ )
1887 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
1888 sz += bytes_per_column( &tv->columns[i] );
1892 /* check we didn't run of the end of the table */
1893 if ( (n+sz) > rawsize )
1895 ERR("borked.\n");
1896 dump_table( st, rawdata, rawsize );
1897 break;
1900 rec = msi_get_transform_record( tv, st, &rawdata[n] );
1901 if (rec)
1903 if ( mask & 1 )
1905 TRACE("inserting record\n");
1908 * Native msi seems writes nul into the
1909 * Number (2nd) column of the _Columns table.
1910 * Not sure that it's deliberate...
1912 if (!lstrcmpW(name, szColumns))
1914 if ( MSI_RecordIsNull( rec, 2 ) )
1915 MSI_RecordSetInteger( rec, 2, ++colcol );
1916 else
1917 ERR("_Columns has non-null data...\n");
1920 r = TABLE_insert_row( &tv->view, rec );
1921 if (r != ERROR_SUCCESS)
1922 ERR("insert row failed\n");
1924 else
1926 UINT row = 0;
1928 r = msi_table_find_row( tv, rec, &row );
1929 if (r != ERROR_SUCCESS)
1930 ERR("no matching row to transform\n");
1931 else if ( mask )
1933 TRACE("modifying row [%d]: \n", row);
1934 msi_table_modify_row( tv, rec, row, mask );
1936 else
1938 TRACE("deleting row [%d]: \n", row);
1939 msi_delete_row( tv, row );
1942 if( TRACE_ON(msidb) ) dump_record( rec );
1943 msiobj_release( &rec->hdr );
1946 n += sz/2;
1949 err:
1950 /* no need to free the table, it's associated with the database */
1951 msi_free( rawdata );
1952 if( tv )
1953 tv->view.ops->delete( &tv->view );
1955 return ERROR_SUCCESS;
1959 * msi_table_apply_transform
1961 * Enumerate the table transforms in a transform storage and apply each one.
1963 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
1965 IEnumSTATSTG *stgenum = NULL;
1966 HRESULT r;
1967 STATSTG stat;
1968 ULONG count;
1969 WCHAR name[0x40];
1970 string_table *strings;
1971 UINT ret = ERROR_FUNCTION_FAILED;
1973 TRACE("%p %p\n", db, stg );
1975 strings = load_string_table( stg );
1976 if( !strings )
1977 goto end;
1979 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
1980 if( FAILED( r ) )
1981 goto end;
1984 * Apply _Tables and _Coluimns transforms first so that
1985 * the table metadata is correct, and empty tables exist.
1987 ret = msi_table_load_transform( db, stg, strings, szTables );
1988 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
1989 goto end;
1991 ret = msi_table_load_transform( db, stg, strings, szColumns );
1992 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
1993 goto end;
1995 ret = ERROR_SUCCESS;
1997 while( r == ERROR_SUCCESS )
1999 count = 0;
2000 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2001 if( FAILED( r ) || !count )
2002 break;
2004 decode_streamname( stat.pwcsName, name );
2005 if ( name[0] != 0x4840 )
2006 continue;
2008 TRACE("transform contains stream %s\n", debugstr_w(name));
2010 if ( !lstrcmpW( name+1, szStringPool ) ||
2011 !lstrcmpW( name+1, szStringData ) ||
2012 !lstrcmpW( name+1, szColumns ) ||
2013 !lstrcmpW( name+1, szTables ) )
2014 continue;
2016 ret = msi_table_load_transform( db, stg, strings, name+1 );
2019 if ( ret == ERROR_SUCCESS )
2021 MSITRANSFORM *t;
2023 t = msi_alloc( sizeof *t );
2024 t->stg = stg;
2025 IStorage_AddRef( stg );
2026 list_add_tail( &db->transforms, &t->entry );
2029 end:
2030 if ( stgenum )
2031 IEnumSTATSTG_Release( stgenum );
2032 if ( strings )
2033 msi_destroy_stringtable( strings );
2035 return ret;
2038 void msi_free_transforms( MSIDATABASE *db )
2040 while( !list_empty( &db->transforms ) )
2042 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2043 MSITRANSFORM, entry );
2044 list_remove( &t->entry );
2045 IStorage_Release( t->stg );
2046 msi_free( t );