msi: Validate database fields before inserting them.
[wine/gsoc_dplay.git] / dlls / msi / table.c
blob51986d7a6eef901e95e91463ebfb0f20ec0324a2
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 #define MAX_STREAM_NAME 0x1f
76 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
77 MSICOLUMNINFO **pcols, UINT *pcount );
78 static UINT get_tablecolumns( MSIDATABASE *db,
79 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
80 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
82 static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
84 if( col->type & MSITYPE_STRING )
85 return 2;
86 if( (col->type & 0xff) > 4 )
87 ERR("Invalid column size!\n");
88 return col->type & 0xff;
91 static int utf2mime(int x)
93 if( (x>='0') && (x<='9') )
94 return x-'0';
95 if( (x>='A') && (x<='Z') )
96 return x-'A'+10;
97 if( (x>='a') && (x<='z') )
98 return x-'a'+10+26;
99 if( x=='.' )
100 return 10+26+26;
101 if( x=='_' )
102 return 10+26+26+1;
103 return -1;
106 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
108 DWORD count = MAX_STREAM_NAME;
109 DWORD ch, next;
110 LPWSTR out, p;
112 if( !bTable )
113 count = lstrlenW( in )+2;
114 out = msi_alloc( count*sizeof(WCHAR) );
115 p = out;
117 if( bTable )
119 *p++ = 0x4840;
120 count --;
122 while( count -- )
124 ch = *in++;
125 if( !ch )
127 *p = ch;
128 return out;
130 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
132 ch = utf2mime(ch) + 0x4800;
133 next = *in;
134 if( next && (next<0x80) )
136 next = utf2mime(next);
137 if( next >= 0 )
139 next += 0x3ffffc0;
140 ch += (next<<6);
141 in++;
145 *p++ = ch;
147 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
148 msi_free( out );
149 return NULL;
152 static int mime2utf(int x)
154 if( x<10 )
155 return x + '0';
156 if( x<(10+26))
157 return x - 10 + 'A';
158 if( x<(10+26+26))
159 return x - 10 - 26 + 'a';
160 if( x == (10+26+26) )
161 return '.';
162 return '_';
165 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
167 WCHAR ch;
168 DWORD count = 0;
170 while ( (ch = *in++) )
172 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
174 if( ch >= 0x4800 )
175 ch = mime2utf(ch-0x4800);
176 else
178 ch -= 0x3800;
179 *out++ = mime2utf(ch&0x3f);
180 count++;
181 ch = mime2utf((ch>>6)&0x3f);
184 *out++ = ch;
185 count++;
187 *out = 0;
188 return count;
191 void enum_stream_names( IStorage *stg )
193 IEnumSTATSTG *stgenum = NULL;
194 HRESULT r;
195 STATSTG stat;
196 ULONG n, count;
197 WCHAR name[0x40];
199 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
200 if( FAILED( r ) )
201 return;
203 n = 0;
204 while( 1 )
206 count = 0;
207 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
208 if( FAILED( r ) || !count )
209 break;
210 decode_streamname( stat.pwcsName, name );
211 TRACE("stream %2ld -> %s %s\n", n,
212 debugstr_w(stat.pwcsName), debugstr_w(name) );
213 n++;
216 IEnumSTATSTG_Release( stgenum );
219 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
220 USHORT **pdata, UINT *psz )
222 HRESULT r;
223 UINT ret = ERROR_FUNCTION_FAILED;
224 VOID *data;
225 ULONG sz, count;
226 IStream *stm = NULL;
227 STATSTG stat;
228 LPWSTR encname;
230 encname = encode_streamname(TRUE, stname);
232 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
234 r = IStorage_OpenStream(stg, encname, NULL,
235 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
236 msi_free( encname );
237 if( FAILED( r ) )
239 WARN("open stream failed r = %08lx - empty table?\n",r);
240 return ret;
243 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
244 if( FAILED( r ) )
246 WARN("open stream failed r = %08lx!\n",r);
247 goto end;
250 if( stat.cbSize.QuadPart >> 32 )
252 WARN("Too big!\n");
253 goto end;
256 sz = stat.cbSize.QuadPart;
257 data = msi_alloc( sz );
258 if( !data )
260 WARN("couldn't allocate memory r=%08lx!\n",r);
261 ret = ERROR_NOT_ENOUGH_MEMORY;
262 goto end;
265 r = IStream_Read(stm, data, sz, &count );
266 if( FAILED( r ) || ( count != sz ) )
268 msi_free( data );
269 WARN("read stream failed r = %08lx!\n",r);
270 goto end;
273 *pdata = data;
274 *psz = sz;
275 ret = ERROR_SUCCESS;
277 end:
278 IStream_Release( stm );
280 return ret;
283 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
285 LPWSTR encname;
286 HRESULT r;
288 encname = encode_streamname(FALSE, stname);
290 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
292 r = IStorage_OpenStream(db->storage, encname, NULL,
293 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
294 if( FAILED( r ) )
296 MSITRANSFORM *transform;
298 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
300 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
301 r = IStorage_OpenStream( transform->stg, encname, NULL,
302 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
303 if (SUCCEEDED(r))
304 break;
308 msi_free( encname );
310 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
313 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
314 USHORT **pdata, UINT *psz )
316 HRESULT r;
317 UINT ret = ERROR_FUNCTION_FAILED;
318 VOID *data;
319 ULONG sz, count;
320 IStream *stm = NULL;
321 STATSTG stat;
323 r = db_get_raw_stream( db, stname, &stm );
324 if( r != ERROR_SUCCESS)
325 return ret;
326 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
327 if( FAILED( r ) )
329 WARN("open stream failed r = %08lx!\n",r);
330 goto end;
333 if( stat.cbSize.QuadPart >> 32 )
335 WARN("Too big!\n");
336 goto end;
339 sz = stat.cbSize.QuadPart;
340 data = msi_alloc( sz );
341 if( !data )
343 WARN("couldn't allocate memory r=%08lx!\n",r);
344 ret = ERROR_NOT_ENOUGH_MEMORY;
345 goto end;
348 r = IStream_Read(stm, data, sz, &count );
349 if( FAILED( r ) || ( count != sz ) )
351 msi_free( data );
352 WARN("read stream failed r = %08lx!\n",r);
353 goto end;
356 *pdata = data;
357 *psz = sz;
358 ret = ERROR_SUCCESS;
360 end:
361 IStream_Release( stm );
363 return ret;
366 static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
367 LPVOID data, UINT sz )
369 HRESULT r;
370 UINT ret = ERROR_FUNCTION_FAILED;
371 ULONG count;
372 IStream *stm = NULL;
373 ULARGE_INTEGER size;
374 LARGE_INTEGER pos;
375 LPWSTR encname;
377 encname = encode_streamname(TRUE, stname );
378 r = IStorage_OpenStream( stg, encname, NULL,
379 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
380 if( FAILED(r) )
382 r = IStorage_CreateStream( stg, encname,
383 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
385 msi_free( encname );
386 if( FAILED( r ) )
388 WARN("open stream failed r = %08lx\n",r);
389 return ret;
392 size.QuadPart = sz;
393 r = IStream_SetSize( stm, size );
394 if( FAILED( r ) )
396 WARN("Failed to SetSize\n");
397 goto end;
400 pos.QuadPart = 0;
401 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
402 if( FAILED( r ) )
404 WARN("Failed to Seek\n");
405 goto end;
408 r = IStream_Write(stm, data, sz, &count );
409 if( FAILED( r ) || ( count != sz ) )
411 WARN("Failed to Write\n");
412 goto end;
415 ret = ERROR_SUCCESS;
417 end:
418 IStream_Release( stm );
420 return ret;
423 static void free_table( MSITABLE *table )
425 int i;
426 for( i=0; i<table->row_count; i++ )
427 msi_free( table->data[i] );
428 msi_free( table->data );
429 msi_free( table );
432 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
434 const MSICOLUMNINFO *last_col = &cols[count-1];
435 if (!count)
436 return 0;
437 return last_col->offset + bytes_per_column( last_col );
440 /* add this table to the list of cached tables in the database */
441 static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
442 const MSICOLUMNINFO *cols, UINT num_cols )
444 MSITABLE *t;
445 USHORT *rawdata = NULL;
446 UINT rawsize = 0, i, j, row_size = 0;
448 TRACE("%s\n",debugstr_w(name));
450 /* nonexistent tables should be interpreted as empty tables */
451 t = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
452 if( !t )
453 return t;
455 row_size = msi_table_get_row_size( cols, num_cols );
457 t->row_count = 0;
458 t->data = NULL;
459 lstrcpyW( t->name, name );
461 /* if we can't read the table, just assume that it's empty */
462 read_stream_data( stg, name, &rawdata, &rawsize );
463 if( !rawdata )
464 return t;
466 TRACE("Read %d bytes\n", rawsize );
468 if( rawsize % row_size )
470 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
471 goto err;
474 t->row_count = rawsize / row_size;
475 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
476 if( !t->data )
477 goto err;
479 /* transpose all the data */
480 TRACE("Transposing data from %d rows\n", t->row_count );
481 for( i=0; i<t->row_count; i++ )
483 t->data[i] = msi_alloc( row_size );
484 if( !t->data[i] )
485 goto err;
487 for( j=0; j<num_cols; j++ )
489 UINT ofs = cols[j].offset/2;
490 UINT n = bytes_per_column( &cols[j] );
492 switch( n )
494 case 2:
495 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
496 break;
497 case 4:
498 t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
499 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
500 break;
501 default:
502 ERR("oops - unknown column width %d\n", n);
503 goto err;
508 msi_free( rawdata );
509 return t;
510 err:
511 msi_free( rawdata );
512 free_table( t );
513 return NULL;
516 void free_cached_tables( MSIDATABASE *db )
518 while( !list_empty( &db->tables ) )
520 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
522 list_remove( &t->entry );
523 free_table( t );
527 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
529 MSITABLE *t;
531 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
532 if( !lstrcmpW( name, t->name ) )
533 return t;
535 return NULL;
538 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
540 UINT r, column_count = 0;
541 MSICOLUMNINFO *columns;
543 /* get the number of columns in this table */
544 column_count = 0;
545 r = get_tablecolumns( db, name, NULL, &column_count );
546 if( r != ERROR_SUCCESS )
547 return r;
549 /* if there's no columns, there's no table */
550 if( column_count == 0 )
551 return ERROR_INVALID_PARAMETER;
553 TRACE("Table %s found\n", debugstr_w(name) );
555 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
556 if( !columns )
557 return ERROR_FUNCTION_FAILED;
559 r = get_tablecolumns( db, name, columns, &column_count );
560 if( r != ERROR_SUCCESS )
562 msi_free( columns );
563 return ERROR_FUNCTION_FAILED;
566 *pcols = columns;
567 *pcount = column_count;
569 return r;
572 static MSITABLE *get_table( MSIDATABASE *db, LPCWSTR name,
573 const MSICOLUMNINFO *cols, UINT num_cols )
575 MSITABLE *table;
577 /* first, see if the table is cached */
578 table = find_cached_table( db, name );
579 if( table )
580 return table;
582 table = read_table_from_storage( db->storage, name, cols, num_cols );
583 if( table )
584 list_add_head( &db->tables, &table->entry );
586 return table;
589 static UINT save_table( MSIDATABASE *db, MSITABLE *t )
591 USHORT *rawdata = NULL, *p;
592 UINT rawsize, r, i, j, row_size, num_cols = 0;
593 MSICOLUMNINFO *cols = NULL;
595 TRACE("Saving %s\n", debugstr_w( t->name ) );
597 r = table_get_column_info( db, t->name, &cols, &num_cols );
598 if( r != ERROR_SUCCESS )
599 return r;
601 row_size = msi_table_get_row_size( cols, num_cols );
603 rawsize = t->row_count * row_size;
604 rawdata = msi_alloc_zero( rawsize );
605 if( !rawdata )
607 r = ERROR_NOT_ENOUGH_MEMORY;
608 goto err;
611 p = rawdata;
612 for( i=0; i<num_cols; i++ )
614 for( j=0; j<t->row_count; j++ )
616 UINT offset = cols[i].offset;
618 *p++ = t->data[j][offset/2];
619 if( 4 == bytes_per_column( &cols[i] ) )
620 *p++ = t->data[j][offset/2+1];
624 TRACE("writing %d bytes\n", rawsize);
625 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
627 err:
628 msi_free_colinfo( cols, num_cols );
629 msi_free( cols );
630 msi_free( rawdata );
632 return r;
635 HRESULT init_string_table( IStorage *stg )
637 HRESULT r;
638 static const WCHAR szStringData[] = {
639 '_','S','t','r','i','n','g','D','a','t','a',0 };
640 static const WCHAR szStringPool[] = {
641 '_','S','t','r','i','n','g','P','o','o','l',0 };
642 USHORT zero[2] = { 0, 0 };
643 ULONG count = 0;
644 IStream *stm = NULL;
645 LPWSTR encname;
647 encname = encode_streamname(TRUE, szStringPool );
649 /* create the StringPool stream... add the zero string to it*/
650 r = IStorage_CreateStream( stg, encname,
651 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
652 msi_free( encname );
653 if( r )
655 TRACE("Failed\n");
656 return r;
659 r = IStream_Write(stm, zero, sizeof zero, &count );
660 IStream_Release( stm );
662 if( FAILED( r ) || ( count != sizeof zero ) )
664 TRACE("Failed\n");
665 return E_FAIL;
668 /* create the StringData stream... make it zero length */
669 encname = encode_streamname(TRUE, szStringData );
670 r = IStorage_CreateStream( stg, encname,
671 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
672 msi_free( encname );
673 if( r )
675 TRACE("Failed\n");
676 return E_FAIL;
678 IStream_Release( stm );
680 return r;
683 string_table *load_string_table( IStorage *stg )
685 string_table *st = NULL;
686 CHAR *data = NULL;
687 USHORT *pool = NULL;
688 UINT r, datasize = 0, poolsize = 0, codepage;
689 DWORD i, count, offset, len, n, refs;
690 static const WCHAR szStringData[] = {
691 '_','S','t','r','i','n','g','D','a','t','a',0 };
692 static const WCHAR szStringPool[] = {
693 '_','S','t','r','i','n','g','P','o','o','l',0 };
695 r = read_stream_data( stg, szStringPool, &pool, &poolsize );
696 if( r != ERROR_SUCCESS)
697 goto end;
698 r = read_stream_data( stg, szStringData, (USHORT**)&data, &datasize );
699 if( r != ERROR_SUCCESS)
700 goto end;
702 count = poolsize/4;
703 if( poolsize > 4 )
704 codepage = pool[0] | ( pool[1] << 16 );
705 else
706 codepage = CP_ACP;
707 st = msi_init_stringtable( count, codepage );
709 offset = 0;
710 n = 1;
711 i = 1;
712 while( i<count )
714 /* the string reference count is always the second word */
715 refs = pool[i*2+1];
717 /* empty entries have two zeros, still have a string id */
718 if (pool[i*2] == 0 && refs == 0)
720 i++;
721 n++;
722 continue;
726 * If a string is over 64k, the previous string entry is made null
727 * and its the high word of the length is inserted in the null string's
728 * reference count field.
730 if( pool[i*2] == 0)
732 len = (pool[i*2+3] << 16) + pool[i*2+2];
733 i += 2;
735 else
737 len = pool[i*2];
738 i += 1;
741 if ( (offset + len) > datasize )
743 ERR("string table corrupt?\n");
744 break;
747 r = msi_addstring( st, n, data+offset, len, refs );
748 if( r != n )
749 ERR("Failed to add string %ld\n", n );
750 n++;
751 offset += len;
754 if ( datasize != offset )
755 ERR("string table load failed! (%08x != %08lx), please report\n", datasize, offset );
757 TRACE("Loaded %ld strings\n", count);
759 end:
760 msi_free( pool );
761 msi_free( data );
763 return st;
766 static UINT save_string_table( MSIDATABASE *db )
768 UINT i, count, datasize = 0, poolsize = 0, sz, used, r, codepage, n;
769 UINT ret = ERROR_FUNCTION_FAILED;
770 static const WCHAR szStringData[] = {
771 '_','S','t','r','i','n','g','D','a','t','a',0 };
772 static const WCHAR szStringPool[] = {
773 '_','S','t','r','i','n','g','P','o','o','l',0 };
774 CHAR *data = NULL;
775 USHORT *pool = NULL;
777 TRACE("\n");
779 /* construct the new table in memory first */
780 count = msi_string_totalsize( db->strings, &datasize, &poolsize );
782 TRACE("%u %u %u\n", count, datasize, poolsize );
784 pool = msi_alloc( poolsize );
785 if( ! pool )
787 WARN("Failed to alloc pool %d bytes\n", poolsize );
788 goto err;
790 data = msi_alloc( datasize );
791 if( ! data )
793 WARN("Failed to alloc data %d bytes\n", poolsize );
794 goto err;
797 used = 0;
798 codepage = msi_string_get_codepage( db->strings );
799 pool[0]=codepage&0xffff;
800 pool[1]=(codepage>>16);
801 n = 1;
802 for( i=1; i<count; i++ )
804 sz = datasize - used;
805 r = msi_id2stringA( db->strings, i, data+used, &sz );
806 if( r != ERROR_SUCCESS )
808 ERR("failed to fetch string\n");
809 sz = 0;
811 if( sz && (sz < (datasize - used ) ) )
812 sz--;
814 pool[ n*2 + 1 ] = msi_id_refcount( db->strings, i );
815 if (sz < 0x10000)
817 pool[ n*2 ] = sz;
818 n++;
820 else
822 pool[ n*2 ] = 0;
823 pool[ n*2 + 2 ] = sz&0xffff;
824 pool[ n*2 + 3 ] = (sz>>16);
825 n += 2;
827 used += sz;
828 if( used > datasize )
830 ERR("oops overran %d >= %d\n", used, datasize);
831 goto err;
835 if( used != datasize )
837 ERR("oops used %d != datasize %d\n", used, datasize);
838 goto err;
841 /* write the streams */
842 r = write_stream_data( db->storage, szStringData, data, datasize );
843 TRACE("Wrote StringData r=%08x\n", r);
844 if( r )
845 goto err;
846 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
847 TRACE("Wrote StringPool r=%08x\n", r);
848 if( r )
849 goto err;
851 ret = ERROR_SUCCESS;
853 err:
854 msi_free( data );
855 msi_free( pool );
857 return ret;
860 /* information for default tables */
861 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
862 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
863 static const WCHAR szName[] = { 'N','a','m','e',0 };
864 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
865 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
866 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
867 static const WCHAR szType[] = { 'T','y','p','e',0 };
869 static const MSICOLUMNINFO _Columns_cols[4] = {
870 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
871 { szColumns, 2, szNumber, MSITYPE_VALID | 2, 2 },
872 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
873 { szColumns, 4, szType, MSITYPE_VALID | 2, 6 },
875 static const MSICOLUMNINFO _Tables_cols[1] = {
876 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
879 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
881 const MSICOLUMNINFO *p;
882 DWORD i, n;
884 TRACE("%s\n", debugstr_w(name));
886 if (!lstrcmpW( name, szTables ))
888 p = _Tables_cols;
889 n = 1;
891 else if (!lstrcmpW( name, szColumns ))
893 p = _Columns_cols;
894 n = 4;
896 else
897 return ERROR_FUNCTION_FAILED;
899 /* duplicate the string data so we can free it in msi_free_colinfo */
900 for (i=0; i<n; i++)
902 if (colinfo && (i < *sz) )
904 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
905 colinfo[i].tablename = strdupW( p[i].tablename );
906 colinfo[i].colname = strdupW( p[i].colname );
908 if( colinfo && (i >= *sz) )
909 break;
911 *sz = n;
912 return ERROR_SUCCESS;
915 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
917 UINT i;
919 for( i=0; i<count; i++ )
921 msi_free( (LPWSTR) colinfo[i].tablename );
922 msi_free( (LPWSTR) colinfo[i].colname );
923 msi_free( colinfo[i].hash_table );
927 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
929 UINT sz=0, r;
930 LPWSTR str;
932 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
933 if( r != ERROR_SUCCESS )
934 return NULL;
935 str = msi_alloc( sz*sizeof (WCHAR) );
936 if( !str )
937 return str;
938 r = msi_id2stringW( db->strings, stringid, str, &sz );
939 if( r == ERROR_SUCCESS )
940 return str;
941 msi_free( str );
942 return NULL;
945 static UINT get_tablecolumns( MSIDATABASE *db,
946 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
948 UINT r, i, n=0, table_id, count, maxcount = *sz;
949 MSITABLE *table = NULL;
951 /* first check if there is a default table with that name */
952 r = get_defaulttablecolumns( szTableName, colinfo, sz );
953 if( ( r == ERROR_SUCCESS ) && *sz )
954 return r;
956 table = get_table( db, szColumns, _Columns_cols, 4 );
957 if( !table )
959 ERR("couldn't load _Columns table\n");
960 return ERROR_FUNCTION_FAILED;
963 /* convert table and column names to IDs from the string table */
964 r = msi_string2idW( db->strings, szTableName, &table_id );
965 if( r != ERROR_SUCCESS )
967 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
968 return r;
971 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
973 count = table->row_count;
974 for( i=0; i<count; i++ )
976 if( table->data[ i ][ 0 ] != table_id )
977 continue;
978 if( colinfo )
980 UINT id = table->data[ i ] [ 2 ];
981 colinfo[n].tablename = MSI_makestring( db, table_id );
982 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
983 colinfo[n].colname = MSI_makestring( db, id );
984 colinfo[n].type = table->data[ i ] [ 3 ] ^ 0x8000;
985 colinfo[n].hash_table = NULL;
986 /* this assumes that columns are in order in the table */
987 if( n )
988 colinfo[n].offset = colinfo[n-1].offset
989 + bytes_per_column( &colinfo[n-1] );
990 else
991 colinfo[n].offset = 0;
992 TRACE("table %s column %d is [%s] (%d) with type %08x "
993 "offset %d at row %d\n", debugstr_w(szTableName),
994 colinfo[n].number, debugstr_w(colinfo[n].colname),
995 id, colinfo[n].type, colinfo[n].offset, i);
996 if( n != (colinfo[n].number-1) )
998 ERR("oops. data in the _Columns table isn't in the right "
999 "order for table %s\n", debugstr_w(szTableName));
1000 return ERROR_FUNCTION_FAILED;
1003 n++;
1004 if( colinfo && ( n >= maxcount ) )
1005 break;
1007 *sz = n;
1009 return ERROR_SUCCESS;
1012 /* try to find the table name in the _Tables table */
1013 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
1015 UINT r, table_id = 0, i, count;
1016 MSITABLE *table = NULL;
1018 if( !lstrcmpW( name, szTables ) )
1019 return TRUE;
1020 if( !lstrcmpW( name, szColumns ) )
1021 return TRUE;
1023 r = msi_string2idW( db->strings, name, &table_id );
1024 if( r != ERROR_SUCCESS )
1026 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1027 return FALSE;
1030 table = get_table( db, szTables, _Tables_cols, 1 );
1031 if( !table )
1033 TRACE("table %s not available\n", debugstr_w(szTables));
1034 return FALSE;
1037 /* count = table->size/2; */
1038 count = table->row_count;
1039 for( i=0; i<count; i++ )
1040 if( table->data[ i ][ 0 ] == table_id )
1041 break;
1043 if (i!=count)
1044 return TRUE;
1046 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1048 return FALSE;
1051 /* below is the query interface to a table */
1053 typedef struct tagMSITABLEVIEW
1055 MSIVIEW view;
1056 MSIDATABASE *db;
1057 MSITABLE *table;
1058 MSICOLUMNINFO *columns;
1059 UINT num_cols;
1060 UINT row_size;
1061 WCHAR name[1];
1062 } MSITABLEVIEW;
1064 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1066 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1067 UINT offset, num_rows, n;
1069 if( !tv->table )
1070 return ERROR_INVALID_PARAMETER;
1072 if( (col==0) || (col>tv->num_cols) )
1073 return ERROR_INVALID_PARAMETER;
1075 /* how many rows are there ? */
1076 num_rows = tv->table->row_count;
1077 if( row >= num_rows )
1078 return ERROR_NO_MORE_ITEMS;
1080 if( tv->columns[col-1].offset >= tv->row_size )
1082 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1083 ERR("%p %p\n", tv, tv->columns );
1084 return ERROR_FUNCTION_FAILED;
1087 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1088 n = bytes_per_column( &tv->columns[col-1] );
1089 switch( n )
1091 case 4:
1092 offset = tv->columns[col-1].offset/2;
1093 *val = tv->table->data[row][offset] +
1094 (tv->table->data[row][offset + 1] << 16);
1095 break;
1096 case 2:
1097 offset = tv->columns[col-1].offset/2;
1098 *val = tv->table->data[row][offset];
1099 break;
1100 default:
1101 ERR("oops! what is %d bytes per column?\n", n );
1102 return ERROR_FUNCTION_FAILED;
1105 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1107 return ERROR_SUCCESS;
1111 * We need a special case for streams, as we need to reference column with
1112 * the name of the stream in the same table, and the table name
1113 * which may not be available at higher levels of the query
1115 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1117 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1118 UINT ival = 0, refcol = 0, r;
1119 LPWSTR sval;
1120 LPWSTR full_name;
1121 DWORD len;
1122 static const WCHAR szDot[] = { '.', 0 };
1124 if( !view->ops->fetch_int )
1125 return ERROR_INVALID_PARAMETER;
1128 * The column marked with the type stream data seems to have a single number
1129 * which references the column containing the name of the stream data
1131 * Fetch the column to reference first.
1133 r = view->ops->fetch_int( view, row, col, &ival );
1134 if( r != ERROR_SUCCESS )
1135 return r;
1137 /* now get the column with the name of the stream */
1138 r = view->ops->fetch_int( view, row, ival, &refcol );
1139 if( r != ERROR_SUCCESS )
1140 return r;
1142 /* lookup the string value from the string table */
1143 sval = MSI_makestring( tv->db, refcol );
1144 if( !sval )
1145 return ERROR_INVALID_PARAMETER;
1147 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1148 full_name = msi_alloc( len*sizeof(WCHAR) );
1149 lstrcpyW( full_name, tv->name );
1150 lstrcatW( full_name, szDot );
1151 lstrcatW( full_name, sval );
1153 r = db_get_raw_stream( tv->db, full_name, stm );
1154 if( r )
1155 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1156 msi_free( full_name );
1157 msi_free( sval );
1159 return r;
1162 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1164 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1165 UINT offset, n;
1167 if( !tv->table )
1168 return ERROR_INVALID_PARAMETER;
1170 if( (col==0) || (col>tv->num_cols) )
1171 return ERROR_INVALID_PARAMETER;
1173 if( tv->columns[col-1].offset >= tv->row_size )
1175 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1176 ERR("%p %p\n", tv, tv->columns );
1177 return ERROR_FUNCTION_FAILED;
1180 n = bytes_per_column( &tv->columns[col-1] );
1181 switch( n )
1183 case 4:
1184 offset = tv->columns[col-1].offset/2;
1185 tv->table->data[row][offset] = val & 0xffff;
1186 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1187 break;
1188 case 2:
1189 offset = tv->columns[col-1].offset/2;
1190 tv->table->data[row][offset] = val;
1191 break;
1192 default:
1193 ERR("oops! what is %d bytes per column?\n", n );
1194 return ERROR_FUNCTION_FAILED;
1196 return ERROR_SUCCESS;
1199 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
1201 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1202 USHORT **p, *row;
1203 UINT sz;
1205 TRACE("%p\n", view);
1207 if( !tv->table )
1208 return ERROR_INVALID_PARAMETER;
1210 row = msi_alloc_zero( tv->row_size );
1211 if( !row )
1212 return ERROR_NOT_ENOUGH_MEMORY;
1214 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1215 if( tv->table->data )
1216 p = msi_realloc( tv->table->data, sz );
1217 else
1218 p = msi_alloc( sz );
1219 if( !p )
1221 msi_free( row );
1222 return ERROR_NOT_ENOUGH_MEMORY;
1225 tv->table->data = p;
1226 tv->table->data[tv->table->row_count] = row;
1227 *num = tv->table->row_count;
1228 tv->table->row_count++;
1230 return ERROR_SUCCESS;
1233 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1235 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1237 TRACE("%p %p\n", tv, record);
1239 TRACE("There are %d columns\n", tv->num_cols );
1240 tv->table = get_table( tv->db, tv->name, tv->columns, tv->num_cols );
1241 if( !tv->table )
1242 return ERROR_FUNCTION_FAILED;
1244 return ERROR_SUCCESS;
1247 static UINT TABLE_close( struct tagMSIVIEW *view )
1249 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1251 TRACE("%p\n", view );
1253 if( !tv->table )
1254 return ERROR_FUNCTION_FAILED;
1256 tv->table = NULL;
1258 return ERROR_SUCCESS;
1261 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1263 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1265 TRACE("%p %p %p\n", view, rows, cols );
1267 if( cols )
1268 *cols = tv->num_cols;
1269 if( rows )
1271 if( !tv->table )
1272 return ERROR_INVALID_PARAMETER;
1273 *rows = tv->table->row_count;
1276 return ERROR_SUCCESS;
1279 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1280 UINT n, LPWSTR *name, UINT *type )
1282 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1284 TRACE("%p %d %p %p\n", tv, n, name, type );
1286 if( ( n == 0 ) || ( n > tv->num_cols ) )
1287 return ERROR_INVALID_PARAMETER;
1289 if( name )
1291 *name = strdupW( tv->columns[n-1].colname );
1292 if( !*name )
1293 return ERROR_FUNCTION_FAILED;
1295 if( type )
1296 *type = tv->columns[n-1].type;
1298 return ERROR_SUCCESS;
1301 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1303 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1305 UINT r, row, i;
1307 /* check there's no null values where they're not allowed */
1308 for( i = 0; i < tv->num_cols; i++ )
1310 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1311 continue;
1313 if ( tv->columns[i].type & MSITYPE_STRING )
1315 LPCWSTR str;
1317 str = MSI_RecordGetString( rec, i+1 );
1318 if (str == NULL || str[0] == 0)
1319 return ERROR_INVALID_DATA;
1321 else
1323 UINT n;
1325 n = MSI_RecordGetInteger( rec, i+1 );
1326 if (n == MSI_NULL_INTEGER)
1327 return ERROR_INVALID_DATA;
1331 /* check there's no duplicate keys */
1332 r = msi_table_find_row( tv, rec, &row );
1333 if (r == ERROR_SUCCESS)
1334 return ERROR_INVALID_DATA;
1336 return ERROR_SUCCESS;
1339 static UINT msi_table_modify_row( MSITABLEVIEW *tv, MSIRECORD *rec,
1340 UINT row, UINT mask )
1342 UINT i, val, r = ERROR_SUCCESS;
1344 TRACE("%p %p %u %08x\n", tv, rec, row, mask );
1346 for( i = 0; i < tv->num_cols; i++ )
1348 /* set keys or values specified in the mask */
1349 if( (~tv->columns[i].type & MSITYPE_KEY) && (~mask & (1<<i)) )
1350 continue;
1352 if( (tv->columns[i].type & MSITYPE_STRING) &&
1353 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1355 const WCHAR *str = MSI_RecordGetString( rec, i+1 );
1356 val = msi_addstringW( tv->db->strings, 0, str, -1, 1 );
1358 else
1360 val = MSI_RecordGetInteger( rec, i+1 );
1361 if ( 2 == bytes_per_column( &tv->columns[i] ) )
1362 val ^= 0x8000;
1363 else
1364 val ^= 0x80000000;
1366 r = TABLE_set_int( &tv->view, row, i+1, val );
1367 if( r )
1368 break;
1371 return r;
1374 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1376 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1377 UINT r, row = -1;
1379 TRACE("%p %p\n", tv, rec );
1381 /* check that the key is unique - can we find a matching row? */
1382 r = table_validate_new( tv, rec );
1383 if( r != ERROR_SUCCESS )
1384 return ERROR_FUNCTION_FAILED;
1386 r = table_create_new_row( view, &row );
1387 TRACE("insert_row returned %08x\n", r);
1388 if( r != ERROR_SUCCESS )
1389 return r;
1391 return msi_table_modify_row( tv, rec, row, ~0 );
1394 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1395 MSIRECORD *rec)
1397 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1398 UINT r;
1400 TRACE("%p %d %p\n", view, eModifyMode, rec );
1402 if (!tv->table)
1404 r = TABLE_execute( view, NULL );
1405 if( r )
1406 return r;
1409 switch (eModifyMode)
1411 case MSIMODIFY_VALIDATE_NEW:
1412 r = table_validate_new( tv, rec );
1413 break;
1415 case MSIMODIFY_INSERT_TEMPORARY:
1416 r = table_validate_new( tv, rec );
1417 if (r != ERROR_SUCCESS)
1418 break;
1419 r = TABLE_insert_row( view, rec );
1420 break;
1422 case MSIMODIFY_REFRESH:
1423 case MSIMODIFY_INSERT:
1424 case MSIMODIFY_UPDATE:
1425 case MSIMODIFY_ASSIGN:
1426 case MSIMODIFY_REPLACE:
1427 case MSIMODIFY_MERGE:
1428 case MSIMODIFY_DELETE:
1429 case MSIMODIFY_VALIDATE:
1430 case MSIMODIFY_VALIDATE_FIELD:
1431 case MSIMODIFY_VALIDATE_DELETE:
1432 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1433 r = ERROR_CALL_NOT_IMPLEMENTED;
1434 break;
1436 default:
1437 r = ERROR_INVALID_DATA;
1440 return r;
1443 static UINT TABLE_delete( struct tagMSIVIEW *view )
1445 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1447 TRACE("%p\n", view );
1449 tv->table = NULL;
1451 if( tv->columns )
1453 msi_free_colinfo( tv->columns, tv->num_cols );
1454 msi_free( tv->columns );
1456 tv->columns = NULL;
1458 msi_free( tv );
1460 return ERROR_SUCCESS;
1463 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1464 UINT val, UINT *row, MSIITERHANDLE *handle )
1466 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1467 const MSICOLUMNHASHENTRY *entry;
1469 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1471 if( !tv->table )
1472 return ERROR_INVALID_PARAMETER;
1474 if( (col==0) || (col > tv->num_cols) )
1475 return ERROR_INVALID_PARAMETER;
1477 if( !tv->columns[col-1].hash_table )
1479 UINT i;
1480 UINT num_rows = tv->table->row_count;
1481 MSICOLUMNHASHENTRY **hash_table;
1482 MSICOLUMNHASHENTRY *new_entry;
1484 if( tv->columns[col-1].offset >= tv->row_size )
1486 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1487 ERR("%p %p\n", tv, tv->columns );
1488 return ERROR_FUNCTION_FAILED;
1491 /* allocate contiguous memory for the table and its entries so we
1492 * don't have to do an expensive cleanup */
1493 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1494 num_rows * sizeof(MSICOLUMNHASHENTRY));
1495 if (!hash_table)
1496 return ERROR_OUTOFMEMORY;
1498 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1499 tv->columns[col-1].hash_table = hash_table;
1501 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1503 for (i = 0; i < num_rows; i++, new_entry++)
1505 UINT row_value, n;
1506 UINT offset = i + (tv->columns[col-1].offset/2) * num_rows;
1507 n = bytes_per_column( &tv->columns[col-1] );
1508 switch( n )
1510 case 4:
1511 offset = tv->columns[col-1].offset/2;
1512 row_value = tv->table->data[i][offset] +
1513 (tv->table->data[i][offset + 1] << 16);
1514 break;
1515 case 2:
1516 offset = tv->columns[col-1].offset/2;
1517 row_value = tv->table->data[i][offset];
1518 break;
1519 default:
1520 ERR("oops! what is %d bytes per column?\n", n );
1521 continue;
1524 new_entry->next = NULL;
1525 new_entry->value = row_value;
1526 new_entry->row = i;
1527 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1529 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1530 while (prev_entry->next)
1531 prev_entry = prev_entry->next;
1532 prev_entry->next = new_entry;
1534 else
1535 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1539 if( !*handle )
1540 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1541 else
1542 entry = ((const MSICOLUMNHASHENTRY *)*handle)->next;
1544 while (entry && entry->value != val)
1545 entry = entry->next;
1547 *handle = (MSIITERHANDLE)entry;
1548 if (!entry)
1549 return ERROR_NO_MORE_ITEMS;
1551 *row = entry->row;
1552 return ERROR_SUCCESS;
1556 static const MSIVIEWOPS table_ops =
1558 TABLE_fetch_int,
1559 TABLE_fetch_stream,
1560 TABLE_set_int,
1561 TABLE_insert_row,
1562 TABLE_execute,
1563 TABLE_close,
1564 TABLE_get_dimensions,
1565 TABLE_get_column_info,
1566 TABLE_modify,
1567 TABLE_delete,
1568 TABLE_find_matching_rows
1571 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1573 MSITABLEVIEW *tv ;
1574 UINT r, sz, column_count;
1575 MSICOLUMNINFO *columns;
1577 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1579 /* get the number of columns in this table */
1580 column_count = 0;
1581 r = get_tablecolumns( db, name, NULL, &column_count );
1582 if( r != ERROR_SUCCESS )
1583 return r;
1585 /* if there's no columns, there's no table */
1586 if( column_count == 0 )
1587 return ERROR_INVALID_PARAMETER;
1589 TRACE("Table found\n");
1591 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1592 tv = msi_alloc_zero( sz );
1593 if( !tv )
1594 return ERROR_FUNCTION_FAILED;
1596 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO));
1597 if( !columns )
1599 msi_free( tv );
1600 return ERROR_FUNCTION_FAILED;
1603 r = get_tablecolumns( db, name, columns, &column_count );
1604 if( r != ERROR_SUCCESS )
1606 msi_free( columns );
1607 msi_free( tv );
1608 return ERROR_FUNCTION_FAILED;
1611 TRACE("Table has %d columns\n", column_count);
1613 /* fill the structure */
1614 tv->view.ops = &table_ops;
1615 tv->db = db;
1616 tv->columns = columns;
1617 tv->num_cols = column_count;
1618 tv->table = NULL;
1619 tv->row_size = msi_table_get_row_size( columns, column_count );
1621 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
1623 *view = (MSIVIEW*) tv;
1624 lstrcpyW( tv->name, name );
1626 return ERROR_SUCCESS;
1629 UINT MSI_CommitTables( MSIDATABASE *db )
1631 UINT r;
1632 MSITABLE *table = NULL;
1634 TRACE("%p\n",db);
1636 r = save_string_table( db );
1637 if( r != ERROR_SUCCESS )
1639 WARN("failed to save string table r=%08x\n",r);
1640 return r;
1643 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1645 r = save_table( db, table );
1646 if( r != ERROR_SUCCESS )
1648 WARN("failed to save table %s (r=%08x)\n",
1649 debugstr_w(table->name), r);
1650 return r;
1654 /* force everything to reload next time */
1655 free_cached_tables( db );
1657 return ERROR_SUCCESS;
1660 static MSIRECORD *msi_get_transform_record( MSITABLEVIEW *tv, string_table *st, USHORT *rawdata )
1662 UINT i, val, ofs = 0;
1663 USHORT mask = *rawdata++;
1664 MSICOLUMNINFO *columns = tv->columns;
1665 MSIRECORD *rec;
1667 rec = MSI_CreateRecord( tv->num_cols );
1668 if( !rec )
1669 return rec;
1671 TRACE("row -> ");
1672 for( i=0; i<tv->num_cols; i++ )
1674 UINT n = bytes_per_column( &columns[i] );
1676 if ( (mask&1) && (i>=(mask>>8)) )
1677 break;
1678 /* all keys must be present */
1679 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
1680 continue;
1682 switch( n )
1684 case 2:
1685 val = rawdata[ofs];
1686 if( (columns[i].type & MSITYPE_STRING) &&
1687 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1689 LPCWSTR sval = msi_string_lookup_id( st, val );
1690 MSI_RecordSetStringW( rec, i+1, sval );
1691 TRACE("[%s]", debugstr_w(sval));
1693 else
1695 val ^= 0x8000;
1696 MSI_RecordSetInteger( rec, i+1, val );
1697 TRACE("[0x%04x]", val );
1699 break;
1700 case 4:
1701 val = rawdata[ofs] + (rawdata[ofs + 1]<<16);
1702 /* val ^= 0x80000000; */
1703 MSI_RecordSetInteger( rec, i+1, val );
1704 TRACE("[0x%08x]", val );
1705 break;
1706 default:
1707 ERR("oops - unknown column width %d\n", n);
1708 break;
1710 ofs += n/2;
1712 TRACE("\n");
1713 return rec;
1716 static void dump_record( MSIRECORD *rec )
1718 UINT i, n;
1720 n = MSI_RecordGetFieldCount( rec );
1721 for( i=1; i<=n; i++ )
1723 LPCWSTR sval = MSI_RecordGetString( rec, i );
1725 if( MSI_RecordIsNull( rec, i ) )
1726 TRACE("row -> []\n");
1727 else if( (sval = MSI_RecordGetString( rec, i )) )
1728 TRACE("row -> [%s]\n", debugstr_w(sval));
1729 else
1730 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
1734 static void dump_table( string_table *st, USHORT *rawdata, UINT rawsize )
1736 LPCWSTR sval;
1737 UINT i;
1739 for( i=0; i<(rawsize/2); i++ )
1741 sval = msi_string_lookup_id( st, rawdata[i] );
1742 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
1746 static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
1748 LPCWSTR str;
1749 UINT i, r, *data;
1751 data = msi_alloc( tv->num_cols *sizeof (UINT) );
1752 for( i=0; i<tv->num_cols; i++ )
1754 data[i] = 0;
1756 if ( ~tv->columns[i].type & MSITYPE_KEY )
1757 continue;
1759 /* turn the transform column value into a row value */
1760 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
1761 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1763 str = MSI_RecordGetString( rec, i+1 );
1764 r = msi_string2idW( tv->db->strings, str, &data[i] );
1766 /* if there's no matching string in the string table,
1767 these keys can't match any record, so fail now. */
1768 if( ERROR_SUCCESS != r )
1770 msi_free( data );
1771 return NULL;
1774 else
1775 data[i] = MSI_RecordGetInteger( rec, i+1 );
1777 return data;
1780 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, UINT *data )
1782 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
1784 for( i=0; i<tv->num_cols; i++ )
1786 if ( ~tv->columns[i].type & MSITYPE_KEY )
1787 continue;
1789 /* turn the transform column value into a row value */
1790 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
1791 if ( r != ERROR_SUCCESS )
1793 ERR("TABLE_fetch_int shouldn't fail here\n");
1794 break;
1797 /* if this key matches, move to the next column */
1798 if ( x != data[i] )
1800 ret = ERROR_FUNCTION_FAILED;
1801 break;
1804 ret = ERROR_SUCCESS;
1807 return ret;
1810 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
1812 UINT i, r = ERROR_FUNCTION_FAILED, *data;
1814 data = msi_record_to_row( tv, rec );
1815 if( !data )
1816 return r;
1817 for( i=0; i<tv->table->row_count; i++ )
1819 r = msi_row_matches( tv, i, data );
1820 if( r == ERROR_SUCCESS )
1822 *row = i;
1823 break;
1826 msi_free( data );
1827 return r;
1830 static UINT msi_delete_row( MSITABLEVIEW *tv, UINT row )
1832 UINT i;
1833 for( i=1; i<=tv->num_cols; i++ )
1834 tv->view.ops->set_int( &tv->view, row, i, 0 );
1835 return ERROR_SUCCESS;
1838 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
1839 string_table *st, LPCWSTR name )
1841 UINT rawsize = 0;
1842 USHORT *rawdata = NULL;
1843 MSITABLEVIEW *tv = NULL;
1844 UINT r, n, sz, i, mask;
1845 MSIRECORD *rec = NULL;
1847 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
1849 /* create a table view */
1850 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
1851 if( r != ERROR_SUCCESS )
1852 goto err;
1854 r = tv->view.ops->execute( &tv->view, NULL );
1855 if( r != ERROR_SUCCESS )
1856 goto err;
1858 /* read the transform data */
1859 r = ERROR_FUNCTION_FAILED;
1860 read_stream_data( stg, name, &rawdata, &rawsize );
1861 if( !rawdata || (rawsize < 2) )
1863 ERR("odd sized transform for table %s\n", debugstr_w(name));
1864 goto err;
1867 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1868 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
1870 /* interpret the data */
1871 r = ERROR_SUCCESS;
1872 for( n=0; n < (rawsize/2); )
1874 mask = rawdata[n];
1876 if (mask&1)
1879 * if the low bit is set, columns are continuous and
1880 * the number of columns is specified in the high byte
1882 sz = 2 + tv->row_size;
1884 else
1887 * If the low bit is not set, rowdata[n] is a bitmask.
1888 * Excepting for key fields, which are always present,
1889 * each bit indicates that a field is present in the transform record.
1891 * rawdata[n] == 0 is a special case ... only the keys will be present
1892 * and it means that this row should be deleted.
1894 sz = 2;
1895 for( i=0; i<tv->num_cols; i++ )
1897 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
1898 sz += bytes_per_column( &tv->columns[i] );
1902 /* check we didn't run of the end of the table */
1903 if ( (n+sz) > rawsize )
1905 ERR("borked.\n");
1906 dump_table( st, rawdata, rawsize );
1907 break;
1910 rec = msi_get_transform_record( tv, st, &rawdata[n] );
1911 if (rec)
1913 UINT row = 0;
1915 r = msi_table_find_row( tv, rec, &row );
1917 if( rawdata[n] & 1)
1919 TRACE("insert [%d]: ", row);
1920 TABLE_insert_row( &tv->view, rec );
1922 else if( mask & 0xff )
1924 TRACE("modify [%d]: ", row);
1925 msi_table_modify_row( tv, rec, row, mask );
1927 else
1929 TRACE("delete [%d]: ", row);
1930 msi_delete_row( tv, row );
1932 if( TRACE_ON(msidb) ) dump_record( rec );
1933 msiobj_release( &rec->hdr );
1936 n += sz/2;
1940 err:
1941 /* no need to free the table, it's associated with the database */
1942 msi_free( rawdata );
1943 if( tv )
1944 tv->view.ops->delete( &tv->view );
1946 return ERROR_SUCCESS;
1950 * msi_table_apply_transform
1952 * Enumerate the table transforms in a transform storage and apply each one.
1954 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
1956 IEnumSTATSTG *stgenum = NULL;
1957 HRESULT r;
1958 STATSTG stat;
1959 ULONG n, count;
1960 WCHAR name[0x40];
1961 string_table *strings;
1962 UINT ret = ERROR_FUNCTION_FAILED;
1964 TRACE("%p %p\n", db, stg );
1966 strings = load_string_table( stg );
1967 if( !strings )
1968 goto end;
1970 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
1971 if( FAILED( r ) )
1972 goto end;
1974 n = 0;
1975 ret = ERROR_SUCCESS;
1977 while( r == ERROR_SUCCESS )
1979 count = 0;
1980 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
1981 if( FAILED( r ) || !count )
1982 break;
1983 decode_streamname( stat.pwcsName, name );
1984 if( ( name[0] == 0x4840 ) && ( name[1] != '_' ) )
1985 ret = msi_table_load_transform( db, stg, strings, name+1 );
1986 else
1987 TRACE("transform contains stream %s\n", debugstr_w(name));
1988 n++;
1991 if ( ret == ERROR_SUCCESS )
1993 MSITRANSFORM *t;
1995 t = msi_alloc( sizeof *t );
1996 t->stg = stg;
1997 IStorage_AddRef( stg );
1998 list_add_tail( &db->transforms, &t->entry );
2001 end:
2002 if ( stgenum )
2003 IEnumSTATSTG_Release( stgenum );
2004 if ( strings )
2005 msi_destroy_stringtable( strings );
2007 return ret;
2010 void msi_free_transforms( MSIDATABASE *db )
2012 while( !list_empty( &db->transforms ) )
2014 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
2015 MSITRANSFORM, entry );
2016 list_remove( &t->entry );
2017 IStorage_Release( t->stg );
2018 msi_free( t );