msi: Fix strings with lengths that are exact multiples of 2^16.
[wine/multimedia.git] / dlls / msi / table.c
blob57cd4df97464d188ab9b2bd3673adb4951f562c8
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, poolsize, sz, used, r, codepage;
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 datasize = msi_string_totalsize( db->strings, &count );
781 poolsize = (count + 1)*2*sizeof(USHORT);
783 pool = msi_alloc( poolsize );
784 if( ! pool )
786 WARN("Failed to alloc pool %d bytes\n", poolsize );
787 goto err;
789 data = msi_alloc( datasize );
790 if( ! data )
792 WARN("Failed to alloc data %d bytes\n", poolsize );
793 goto err;
796 used = 0;
797 codepage = msi_string_get_codepage( db->strings );
798 pool[0]=codepage&0xffff;
799 pool[1]=(codepage>>16);
800 for( i=1; i<count; i++ )
802 sz = datasize - used;
803 r = msi_id2stringA( db->strings, i, data+used, &sz );
804 if( r != ERROR_SUCCESS )
806 ERR("failed to fetch string\n");
807 sz = 0;
809 if( sz && (sz < (datasize - used ) ) )
810 sz--;
811 TRACE("adding %u bytes %s\n", sz, debugstr_a(data+used) );
812 pool[ i*2 ] = sz;
813 pool[ i*2 + 1 ] = msi_id_refcount( db->strings, i );
814 used += sz;
815 if( used > datasize )
817 ERR("oops overran %d >= %d\n", used, datasize);
818 goto err;
822 if( used != datasize )
824 ERR("oops used %d != datasize %d\n", used, datasize);
825 goto err;
828 /* write the streams */
829 r = write_stream_data( db->storage, szStringData, data, datasize );
830 TRACE("Wrote StringData r=%08x\n", r);
831 if( r )
832 goto err;
833 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
834 TRACE("Wrote StringPool r=%08x\n", r);
835 if( r )
836 goto err;
838 ret = ERROR_SUCCESS;
840 err:
841 msi_free( data );
842 msi_free( pool );
844 return ret;
847 /* information for default tables */
848 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
849 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
850 static const WCHAR szName[] = { 'N','a','m','e',0 };
851 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
852 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
853 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
854 static const WCHAR szType[] = { 'T','y','p','e',0 };
856 static const MSICOLUMNINFO _Columns_cols[4] = {
857 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
858 { szColumns, 2, szNumber, MSITYPE_VALID | 2, 2 },
859 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
860 { szColumns, 4, szType, MSITYPE_VALID | 2, 6 },
862 static const MSICOLUMNINFO _Tables_cols[1] = {
863 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
866 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
868 const MSICOLUMNINFO *p;
869 DWORD i, n;
871 TRACE("%s\n", debugstr_w(name));
873 if (!lstrcmpW( name, szTables ))
875 p = _Tables_cols;
876 n = 1;
878 else if (!lstrcmpW( name, szColumns ))
880 p = _Columns_cols;
881 n = 4;
883 else
884 return ERROR_FUNCTION_FAILED;
886 /* duplicate the string data so we can free it in msi_free_colinfo */
887 for (i=0; i<n; i++)
889 if (colinfo && (i < *sz) )
891 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
892 colinfo[i].tablename = strdupW( p[i].tablename );
893 colinfo[i].colname = strdupW( p[i].colname );
895 if( colinfo && (i >= *sz) )
896 break;
898 *sz = n;
899 return ERROR_SUCCESS;
902 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
904 UINT i;
906 for( i=0; i<count; i++ )
908 msi_free( (LPWSTR) colinfo[i].tablename );
909 msi_free( (LPWSTR) colinfo[i].colname );
910 msi_free( colinfo[i].hash_table );
914 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
916 UINT sz=0, r;
917 LPWSTR str;
919 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
920 if( r != ERROR_SUCCESS )
921 return NULL;
922 str = msi_alloc( sz*sizeof (WCHAR) );
923 if( !str )
924 return str;
925 r = msi_id2stringW( db->strings, stringid, str, &sz );
926 if( r == ERROR_SUCCESS )
927 return str;
928 msi_free( str );
929 return NULL;
932 static UINT get_tablecolumns( MSIDATABASE *db,
933 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
935 UINT r, i, n=0, table_id, count, maxcount = *sz;
936 MSITABLE *table = NULL;
938 /* first check if there is a default table with that name */
939 r = get_defaulttablecolumns( szTableName, colinfo, sz );
940 if( ( r == ERROR_SUCCESS ) && *sz )
941 return r;
943 table = get_table( db, szColumns, _Columns_cols, 4 );
944 if( !table )
946 ERR("couldn't load _Columns table\n");
947 return ERROR_FUNCTION_FAILED;
950 /* convert table and column names to IDs from the string table */
951 r = msi_string2idW( db->strings, szTableName, &table_id );
952 if( r != ERROR_SUCCESS )
954 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
955 return r;
958 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
960 count = table->row_count;
961 for( i=0; i<count; i++ )
963 if( table->data[ i ][ 0 ] != table_id )
964 continue;
965 if( colinfo )
967 UINT id = table->data[ i ] [ 2 ];
968 colinfo[n].tablename = MSI_makestring( db, table_id );
969 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
970 colinfo[n].colname = MSI_makestring( db, id );
971 colinfo[n].type = table->data[ i ] [ 3 ] ^ 0x8000;
972 colinfo[n].hash_table = NULL;
973 /* this assumes that columns are in order in the table */
974 if( n )
975 colinfo[n].offset = colinfo[n-1].offset
976 + bytes_per_column( &colinfo[n-1] );
977 else
978 colinfo[n].offset = 0;
979 TRACE("table %s column %d is [%s] (%d) with type %08x "
980 "offset %d at row %d\n", debugstr_w(szTableName),
981 colinfo[n].number, debugstr_w(colinfo[n].colname),
982 id, colinfo[n].type, colinfo[n].offset, i);
983 if( n != (colinfo[n].number-1) )
985 ERR("oops. data in the _Columns table isn't in the right "
986 "order for table %s\n", debugstr_w(szTableName));
987 return ERROR_FUNCTION_FAILED;
990 n++;
991 if( colinfo && ( n >= maxcount ) )
992 break;
994 *sz = n;
996 return ERROR_SUCCESS;
999 /* try to find the table name in the _Tables table */
1000 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
1002 UINT r, table_id = 0, i, count;
1003 MSITABLE *table = NULL;
1005 if( !lstrcmpW( name, szTables ) )
1006 return TRUE;
1007 if( !lstrcmpW( name, szColumns ) )
1008 return TRUE;
1010 r = msi_string2idW( db->strings, name, &table_id );
1011 if( r != ERROR_SUCCESS )
1013 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1014 return FALSE;
1017 table = get_table( db, szTables, _Tables_cols, 1 );
1018 if( !table )
1020 TRACE("table %s not available\n", debugstr_w(szTables));
1021 return FALSE;
1024 /* count = table->size/2; */
1025 count = table->row_count;
1026 for( i=0; i<count; i++ )
1027 if( table->data[ i ][ 0 ] == table_id )
1028 break;
1030 if (i!=count)
1031 return TRUE;
1033 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1035 return FALSE;
1038 /* below is the query interface to a table */
1040 typedef struct tagMSITABLEVIEW
1042 MSIVIEW view;
1043 MSIDATABASE *db;
1044 MSITABLE *table;
1045 MSICOLUMNINFO *columns;
1046 UINT num_cols;
1047 UINT row_size;
1048 WCHAR name[1];
1049 } MSITABLEVIEW;
1051 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1053 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1054 UINT offset, num_rows, n;
1056 if( !tv->table )
1057 return ERROR_INVALID_PARAMETER;
1059 if( (col==0) || (col>tv->num_cols) )
1060 return ERROR_INVALID_PARAMETER;
1062 /* how many rows are there ? */
1063 num_rows = tv->table->row_count;
1064 if( row >= num_rows )
1065 return ERROR_NO_MORE_ITEMS;
1067 if( tv->columns[col-1].offset >= tv->row_size )
1069 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1070 ERR("%p %p\n", tv, tv->columns );
1071 return ERROR_FUNCTION_FAILED;
1074 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1075 n = bytes_per_column( &tv->columns[col-1] );
1076 switch( n )
1078 case 4:
1079 offset = tv->columns[col-1].offset/2;
1080 *val = tv->table->data[row][offset] +
1081 (tv->table->data[row][offset + 1] << 16);
1082 break;
1083 case 2:
1084 offset = tv->columns[col-1].offset/2;
1085 *val = tv->table->data[row][offset];
1086 break;
1087 default:
1088 ERR("oops! what is %d bytes per column?\n", n );
1089 return ERROR_FUNCTION_FAILED;
1092 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1094 return ERROR_SUCCESS;
1098 * We need a special case for streams, as we need to reference column with
1099 * the name of the stream in the same table, and the table name
1100 * which may not be available at higher levels of the query
1102 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1104 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1105 UINT ival = 0, refcol = 0, r;
1106 LPWSTR sval;
1107 LPWSTR full_name;
1108 DWORD len;
1109 static const WCHAR szDot[] = { '.', 0 };
1111 if( !view->ops->fetch_int )
1112 return ERROR_INVALID_PARAMETER;
1115 * The column marked with the type stream data seems to have a single number
1116 * which references the column containing the name of the stream data
1118 * Fetch the column to reference first.
1120 r = view->ops->fetch_int( view, row, col, &ival );
1121 if( r != ERROR_SUCCESS )
1122 return r;
1124 /* now get the column with the name of the stream */
1125 r = view->ops->fetch_int( view, row, ival, &refcol );
1126 if( r != ERROR_SUCCESS )
1127 return r;
1129 /* lookup the string value from the string table */
1130 sval = MSI_makestring( tv->db, refcol );
1131 if( !sval )
1132 return ERROR_INVALID_PARAMETER;
1134 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1135 full_name = msi_alloc( len*sizeof(WCHAR) );
1136 lstrcpyW( full_name, tv->name );
1137 lstrcatW( full_name, szDot );
1138 lstrcatW( full_name, sval );
1140 r = db_get_raw_stream( tv->db, full_name, stm );
1141 if( r )
1142 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1143 msi_free( full_name );
1144 msi_free( sval );
1146 return r;
1149 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1151 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1152 UINT offset, n;
1154 if( !tv->table )
1155 return ERROR_INVALID_PARAMETER;
1157 if( (col==0) || (col>tv->num_cols) )
1158 return ERROR_INVALID_PARAMETER;
1160 if( tv->columns[col-1].offset >= tv->row_size )
1162 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1163 ERR("%p %p\n", tv, tv->columns );
1164 return ERROR_FUNCTION_FAILED;
1167 n = bytes_per_column( &tv->columns[col-1] );
1168 switch( n )
1170 case 4:
1171 offset = tv->columns[col-1].offset/2;
1172 tv->table->data[row][offset] = val & 0xffff;
1173 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1174 break;
1175 case 2:
1176 offset = tv->columns[col-1].offset/2;
1177 tv->table->data[row][offset] = val;
1178 break;
1179 default:
1180 ERR("oops! what is %d bytes per column?\n", n );
1181 return ERROR_FUNCTION_FAILED;
1183 return ERROR_SUCCESS;
1186 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
1188 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1189 USHORT **p, *row;
1190 UINT sz;
1192 TRACE("%p\n", view);
1194 if( !tv->table )
1195 return ERROR_INVALID_PARAMETER;
1197 row = msi_alloc_zero( tv->row_size );
1198 if( !row )
1199 return ERROR_NOT_ENOUGH_MEMORY;
1201 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1202 if( tv->table->data )
1203 p = msi_realloc( tv->table->data, sz );
1204 else
1205 p = msi_alloc( sz );
1206 if( !p )
1208 msi_free( row );
1209 return ERROR_NOT_ENOUGH_MEMORY;
1212 tv->table->data = p;
1213 tv->table->data[tv->table->row_count] = row;
1214 *num = tv->table->row_count;
1215 tv->table->row_count++;
1217 return ERROR_SUCCESS;
1220 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1222 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1224 TRACE("%p %p\n", tv, record);
1226 TRACE("There are %d columns\n", tv->num_cols );
1227 tv->table = get_table( tv->db, tv->name, tv->columns, tv->num_cols );
1228 if( !tv->table )
1229 return ERROR_FUNCTION_FAILED;
1231 return ERROR_SUCCESS;
1234 static UINT TABLE_close( struct tagMSIVIEW *view )
1236 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1238 TRACE("%p\n", view );
1240 if( !tv->table )
1241 return ERROR_FUNCTION_FAILED;
1243 tv->table = NULL;
1245 return ERROR_SUCCESS;
1248 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1250 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1252 TRACE("%p %p %p\n", view, rows, cols );
1254 if( cols )
1255 *cols = tv->num_cols;
1256 if( rows )
1258 if( !tv->table )
1259 return ERROR_INVALID_PARAMETER;
1260 *rows = tv->table->row_count;
1263 return ERROR_SUCCESS;
1266 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1267 UINT n, LPWSTR *name, UINT *type )
1269 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1271 TRACE("%p %d %p %p\n", tv, n, name, type );
1273 if( ( n == 0 ) || ( n > tv->num_cols ) )
1274 return ERROR_INVALID_PARAMETER;
1276 if( name )
1278 *name = strdupW( tv->columns[n-1].colname );
1279 if( !*name )
1280 return ERROR_FUNCTION_FAILED;
1282 if( type )
1283 *type = tv->columns[n-1].type;
1285 return ERROR_SUCCESS;
1288 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1290 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1292 UINT r, row;
1294 r = msi_table_find_row( tv, rec, &row );
1295 if (r != ERROR_SUCCESS)
1296 return ERROR_SUCCESS;
1297 return ERROR_INVALID_DATA;
1300 static UINT msi_table_modify_row( MSITABLEVIEW *tv, MSIRECORD *rec,
1301 UINT row, UINT mask )
1303 UINT i, val, r = ERROR_SUCCESS;
1305 TRACE("%p %p %u %08x\n", tv, rec, row, mask );
1307 for( i = 0; i < tv->num_cols; i++ )
1309 /* set keys or values specified in the mask */
1310 if( (~tv->columns[i].type & MSITYPE_KEY) && (~mask & (1<<i)) )
1311 continue;
1313 if( (tv->columns[i].type & MSITYPE_STRING) &&
1314 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1316 const WCHAR *str = MSI_RecordGetString( rec, i+1 );
1317 val = msi_addstringW( tv->db->strings, 0, str, -1, 1 );
1319 else
1321 val = MSI_RecordGetInteger( rec, i+1 );
1322 if ( 2 == bytes_per_column( &tv->columns[i] ) )
1323 val ^= 0x8000;
1324 else
1325 val ^= 0x80000000;
1327 r = TABLE_set_int( &tv->view, row, i+1, val );
1328 if( r )
1329 break;
1332 return r;
1335 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1337 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1338 UINT r, row = -1;
1340 TRACE("%p %p\n", tv, rec );
1342 r = table_create_new_row( view, &row );
1343 TRACE("insert_row returned %08x\n", r);
1344 if( r != ERROR_SUCCESS )
1345 return r;
1347 return msi_table_modify_row( tv, rec, row, ~0 );
1350 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1351 MSIRECORD *rec)
1353 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1354 UINT r;
1356 TRACE("%p %d %p\n", view, eModifyMode, rec );
1358 if (!tv->table)
1360 r = TABLE_execute( view, NULL );
1361 if( r )
1362 return r;
1365 switch (eModifyMode)
1367 case MSIMODIFY_VALIDATE_NEW:
1368 r = table_validate_new( tv, rec );
1369 break;
1371 case MSIMODIFY_INSERT_TEMPORARY:
1372 r = table_validate_new( tv, rec );
1373 if (r != ERROR_SUCCESS)
1374 break;
1375 r = TABLE_insert_row( view, rec );
1376 break;
1378 case MSIMODIFY_REFRESH:
1379 case MSIMODIFY_INSERT:
1380 case MSIMODIFY_UPDATE:
1381 case MSIMODIFY_ASSIGN:
1382 case MSIMODIFY_REPLACE:
1383 case MSIMODIFY_MERGE:
1384 case MSIMODIFY_DELETE:
1385 case MSIMODIFY_VALIDATE:
1386 case MSIMODIFY_VALIDATE_FIELD:
1387 case MSIMODIFY_VALIDATE_DELETE:
1388 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1389 r = ERROR_CALL_NOT_IMPLEMENTED;
1390 break;
1392 default:
1393 r = ERROR_INVALID_DATA;
1396 return r;
1399 static UINT TABLE_delete( struct tagMSIVIEW *view )
1401 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1403 TRACE("%p\n", view );
1405 tv->table = NULL;
1407 if( tv->columns )
1409 msi_free_colinfo( tv->columns, tv->num_cols );
1410 msi_free( tv->columns );
1412 tv->columns = NULL;
1414 msi_free( tv );
1416 return ERROR_SUCCESS;
1419 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1420 UINT val, UINT *row, MSIITERHANDLE *handle )
1422 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1423 const MSICOLUMNHASHENTRY *entry;
1425 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1427 if( !tv->table )
1428 return ERROR_INVALID_PARAMETER;
1430 if( (col==0) || (col > tv->num_cols) )
1431 return ERROR_INVALID_PARAMETER;
1433 if( !tv->columns[col-1].hash_table )
1435 UINT i;
1436 UINT num_rows = tv->table->row_count;
1437 MSICOLUMNHASHENTRY **hash_table;
1438 MSICOLUMNHASHENTRY *new_entry;
1440 if( tv->columns[col-1].offset >= tv->row_size )
1442 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1443 ERR("%p %p\n", tv, tv->columns );
1444 return ERROR_FUNCTION_FAILED;
1447 /* allocate contiguous memory for the table and its entries so we
1448 * don't have to do an expensive cleanup */
1449 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1450 num_rows * sizeof(MSICOLUMNHASHENTRY));
1451 if (!hash_table)
1452 return ERROR_OUTOFMEMORY;
1454 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1455 tv->columns[col-1].hash_table = hash_table;
1457 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1459 for (i = 0; i < num_rows; i++, new_entry++)
1461 UINT row_value, n;
1462 UINT offset = i + (tv->columns[col-1].offset/2) * num_rows;
1463 n = bytes_per_column( &tv->columns[col-1] );
1464 switch( n )
1466 case 4:
1467 offset = tv->columns[col-1].offset/2;
1468 row_value = tv->table->data[i][offset] +
1469 (tv->table->data[i][offset + 1] << 16);
1470 break;
1471 case 2:
1472 offset = tv->columns[col-1].offset/2;
1473 row_value = tv->table->data[i][offset];
1474 break;
1475 default:
1476 ERR("oops! what is %d bytes per column?\n", n );
1477 continue;
1480 new_entry->next = NULL;
1481 new_entry->value = row_value;
1482 new_entry->row = i;
1483 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1485 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1486 while (prev_entry->next)
1487 prev_entry = prev_entry->next;
1488 prev_entry->next = new_entry;
1490 else
1491 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1495 if( !*handle )
1496 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1497 else
1498 entry = ((const MSICOLUMNHASHENTRY *)*handle)->next;
1500 while (entry && entry->value != val)
1501 entry = entry->next;
1503 *handle = (MSIITERHANDLE)entry;
1504 if (!entry)
1505 return ERROR_NO_MORE_ITEMS;
1507 *row = entry->row;
1508 return ERROR_SUCCESS;
1512 static const MSIVIEWOPS table_ops =
1514 TABLE_fetch_int,
1515 TABLE_fetch_stream,
1516 TABLE_set_int,
1517 TABLE_insert_row,
1518 TABLE_execute,
1519 TABLE_close,
1520 TABLE_get_dimensions,
1521 TABLE_get_column_info,
1522 TABLE_modify,
1523 TABLE_delete,
1524 TABLE_find_matching_rows
1527 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1529 MSITABLEVIEW *tv ;
1530 UINT r, sz, column_count;
1531 MSICOLUMNINFO *columns;
1533 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1535 /* get the number of columns in this table */
1536 column_count = 0;
1537 r = get_tablecolumns( db, name, NULL, &column_count );
1538 if( r != ERROR_SUCCESS )
1539 return r;
1541 /* if there's no columns, there's no table */
1542 if( column_count == 0 )
1543 return ERROR_INVALID_PARAMETER;
1545 TRACE("Table found\n");
1547 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1548 tv = msi_alloc_zero( sz );
1549 if( !tv )
1550 return ERROR_FUNCTION_FAILED;
1552 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO));
1553 if( !columns )
1555 msi_free( tv );
1556 return ERROR_FUNCTION_FAILED;
1559 r = get_tablecolumns( db, name, columns, &column_count );
1560 if( r != ERROR_SUCCESS )
1562 msi_free( columns );
1563 msi_free( tv );
1564 return ERROR_FUNCTION_FAILED;
1567 TRACE("Table has %d columns\n", column_count);
1569 /* fill the structure */
1570 tv->view.ops = &table_ops;
1571 tv->db = db;
1572 tv->columns = columns;
1573 tv->num_cols = column_count;
1574 tv->table = NULL;
1575 tv->row_size = msi_table_get_row_size( columns, column_count );
1577 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
1579 *view = (MSIVIEW*) tv;
1580 lstrcpyW( tv->name, name );
1582 return ERROR_SUCCESS;
1585 UINT MSI_CommitTables( MSIDATABASE *db )
1587 UINT r;
1588 MSITABLE *table = NULL;
1590 TRACE("%p\n",db);
1592 r = save_string_table( db );
1593 if( r != ERROR_SUCCESS )
1595 WARN("failed to save string table r=%08x\n",r);
1596 return r;
1599 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1601 r = save_table( db, table );
1602 if( r != ERROR_SUCCESS )
1604 WARN("failed to save table %s (r=%08x)\n",
1605 debugstr_w(table->name), r);
1606 return r;
1610 /* force everything to reload next time */
1611 free_cached_tables( db );
1613 return ERROR_SUCCESS;
1616 static MSIRECORD *msi_get_transform_record( MSITABLEVIEW *tv, string_table *st, USHORT *rawdata )
1618 UINT i, val, ofs = 0;
1619 USHORT mask = *rawdata++;
1620 MSICOLUMNINFO *columns = tv->columns;
1621 MSIRECORD *rec;
1623 rec = MSI_CreateRecord( tv->num_cols );
1624 if( !rec )
1625 return rec;
1627 TRACE("row -> ");
1628 for( i=0; i<tv->num_cols; i++ )
1630 UINT n = bytes_per_column( &columns[i] );
1632 if ( (mask&1) && (i>=(mask>>8)) )
1633 break;
1634 /* all keys must be present */
1635 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
1636 continue;
1638 switch( n )
1640 case 2:
1641 val = rawdata[ofs];
1642 if( (columns[i].type & MSITYPE_STRING) &&
1643 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1645 LPCWSTR sval = msi_string_lookup_id( st, val );
1646 MSI_RecordSetStringW( rec, i+1, sval );
1647 TRACE("[%s]", debugstr_w(sval));
1649 else
1651 val ^= 0x8000;
1652 MSI_RecordSetInteger( rec, i+1, val );
1653 TRACE("[0x%04x]", val );
1655 break;
1656 case 4:
1657 val = rawdata[ofs] + (rawdata[ofs + 1]<<16);
1658 /* val ^= 0x80000000; */
1659 MSI_RecordSetInteger( rec, i+1, val );
1660 TRACE("[0x%08x]", val );
1661 break;
1662 default:
1663 ERR("oops - unknown column width %d\n", n);
1664 break;
1666 ofs += n/2;
1668 TRACE("\n");
1669 return rec;
1672 static void dump_record( MSIRECORD *rec )
1674 UINT i, n;
1676 n = MSI_RecordGetFieldCount( rec );
1677 for( i=1; i<=n; i++ )
1679 LPCWSTR sval = MSI_RecordGetString( rec, i );
1681 if( MSI_RecordIsNull( rec, i ) )
1682 TRACE("row -> []\n");
1683 else if( (sval = MSI_RecordGetString( rec, i )) )
1684 TRACE("row -> [%s]\n", debugstr_w(sval));
1685 else
1686 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
1690 static void dump_table( string_table *st, USHORT *rawdata, UINT rawsize )
1692 LPCWSTR sval;
1693 UINT i;
1695 for( i=0; i<(rawsize/2); i++ )
1697 sval = msi_string_lookup_id( st, rawdata[i] );
1698 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
1702 static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
1704 LPCWSTR str;
1705 UINT i, r, *data;
1707 data = msi_alloc( tv->num_cols *sizeof (UINT) );
1708 for( i=0; i<tv->num_cols; i++ )
1710 data[i] = 0;
1712 if ( ~tv->columns[i].type & MSITYPE_KEY )
1713 continue;
1715 /* turn the transform column value into a row value */
1716 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
1717 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1719 str = MSI_RecordGetString( rec, i+1 );
1720 r = msi_string2idW( tv->db->strings, str, &data[i] );
1722 /* if there's no matching string in the string table,
1723 these keys can't match any record, so fail now. */
1724 if( ERROR_SUCCESS != r )
1726 msi_free( data );
1727 return NULL;
1730 else
1731 data[i] = MSI_RecordGetInteger( rec, i+1 );
1733 return data;
1736 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, UINT *data )
1738 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
1740 for( i=0; i<tv->num_cols; i++ )
1742 if ( ~tv->columns[i].type & MSITYPE_KEY )
1743 continue;
1745 /* turn the transform column value into a row value */
1746 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
1747 if ( r != ERROR_SUCCESS )
1749 ERR("TABLE_fetch_int shouldn't fail here\n");
1750 break;
1753 /* if this key matches, move to the next column */
1754 if ( x != data[i] )
1756 ret = ERROR_FUNCTION_FAILED;
1757 break;
1760 ret = ERROR_SUCCESS;
1763 return ret;
1766 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
1768 UINT i, r = ERROR_FUNCTION_FAILED, *data;
1770 data = msi_record_to_row( tv, rec );
1771 if( !data )
1772 return r;
1773 for( i=0; i<tv->table->row_count; i++ )
1775 r = msi_row_matches( tv, i, data );
1776 if( r == ERROR_SUCCESS )
1778 *row = i;
1779 break;
1782 msi_free( data );
1783 return r;
1786 static UINT msi_delete_row( MSITABLEVIEW *tv, UINT row )
1788 UINT i;
1789 for( i=1; i<=tv->num_cols; i++ )
1790 tv->view.ops->set_int( &tv->view, row, i, 0 );
1791 return ERROR_SUCCESS;
1794 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
1795 string_table *st, LPCWSTR name )
1797 UINT rawsize = 0;
1798 USHORT *rawdata = NULL;
1799 MSITABLEVIEW *tv = NULL;
1800 UINT r, n, sz, i, mask;
1801 MSIRECORD *rec = NULL;
1803 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
1805 /* create a table view */
1806 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
1807 if( r != ERROR_SUCCESS )
1808 goto err;
1810 r = tv->view.ops->execute( &tv->view, NULL );
1811 if( r != ERROR_SUCCESS )
1812 goto err;
1814 /* read the transform data */
1815 r = ERROR_FUNCTION_FAILED;
1816 read_stream_data( stg, name, &rawdata, &rawsize );
1817 if( !rawdata || (rawsize < 2) )
1819 ERR("odd sized transform for table %s\n", debugstr_w(name));
1820 goto err;
1823 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1824 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
1826 /* interpret the data */
1827 r = ERROR_SUCCESS;
1828 for( n=0; n < (rawsize/2); )
1830 mask = rawdata[n];
1832 if (mask&1)
1835 * if the low bit is set, columns are continuous and
1836 * the number of columns is specified in the high byte
1838 sz = 2 + tv->row_size;
1840 else
1843 * If the low bit is not set, rowdata[n] is a bitmask.
1844 * Excepting for key fields, which are always present,
1845 * each bit indicates that a field is present in the transform record.
1847 * rawdata[n] == 0 is a special case ... only the keys will be present
1848 * and it means that this row should be deleted.
1850 sz = 2;
1851 for( i=0; i<tv->num_cols; i++ )
1853 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
1854 sz += bytes_per_column( &tv->columns[i] );
1858 /* check we didn't run of the end of the table */
1859 if ( (n+sz) > rawsize )
1861 ERR("borked.\n");
1862 dump_table( st, rawdata, rawsize );
1863 break;
1866 rec = msi_get_transform_record( tv, st, &rawdata[n] );
1867 if (rec)
1869 UINT row = 0;
1871 r = msi_table_find_row( tv, rec, &row );
1873 if( rawdata[n] & 1)
1875 TRACE("insert [%d]: ", row);
1876 TABLE_insert_row( &tv->view, rec );
1878 else if( mask & 0xff )
1880 TRACE("modify [%d]: ", row);
1881 msi_table_modify_row( tv, rec, row, mask );
1883 else
1885 TRACE("delete [%d]: ", row);
1886 msi_delete_row( tv, row );
1888 if( TRACE_ON(msidb) ) dump_record( rec );
1889 msiobj_release( &rec->hdr );
1892 n += sz/2;
1896 err:
1897 /* no need to free the table, it's associated with the database */
1898 msi_free( rawdata );
1899 if( tv )
1900 tv->view.ops->delete( &tv->view );
1902 return ERROR_SUCCESS;
1906 * msi_table_apply_transform
1908 * Enumerate the table transforms in a transform storage and apply each one.
1910 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
1912 IEnumSTATSTG *stgenum = NULL;
1913 HRESULT r;
1914 STATSTG stat;
1915 ULONG n, count;
1916 WCHAR name[0x40];
1917 string_table *strings;
1918 UINT ret = ERROR_FUNCTION_FAILED;
1920 TRACE("%p %p\n", db, stg );
1922 strings = load_string_table( stg );
1923 if( !strings )
1924 goto end;
1926 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
1927 if( FAILED( r ) )
1928 goto end;
1930 n = 0;
1931 ret = ERROR_SUCCESS;
1933 while( r == ERROR_SUCCESS )
1935 count = 0;
1936 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
1937 if( FAILED( r ) || !count )
1938 break;
1939 decode_streamname( stat.pwcsName, name );
1940 if( ( name[0] == 0x4840 ) && ( name[1] != '_' ) )
1941 ret = msi_table_load_transform( db, stg, strings, name+1 );
1942 else
1943 TRACE("transform contains stream %s\n", debugstr_w(name));
1944 n++;
1947 if ( ret == ERROR_SUCCESS )
1949 MSITRANSFORM *t;
1951 t = msi_alloc( sizeof *t );
1952 t->stg = stg;
1953 IStorage_AddRef( stg );
1954 list_add_tail( &db->transforms, &t->entry );
1957 end:
1958 if ( stgenum )
1959 IEnumSTATSTG_Release( stgenum );
1960 if ( strings )
1961 msi_destroy_stringtable( strings );
1963 return ret;
1966 void msi_free_transforms( MSIDATABASE *db )
1968 while( !list_empty( &db->transforms ) )
1970 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
1971 MSITRANSFORM, entry );
1972 list_remove( &t->entry );
1973 IStorage_Release( t->stg );
1974 msi_free( t );