Create the +msidb debug channel for msi database code.
[wine/multimedia.git] / dlls / msi / table.c
blobe8a76f62f1bcfed38714db31f070031ef5bede28
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 typedef struct tagMSICOLUMNINFO
44 LPCWSTR tablename;
45 UINT number;
46 LPCWSTR colname;
47 UINT type;
48 UINT offset;
49 } MSICOLUMNINFO;
51 struct tagMSITABLE
53 USHORT **data;
54 UINT row_count;
55 struct list entry;
56 WCHAR name[1];
59 typedef struct tagMSITRANSFORM {
60 struct list entry;
61 IStorage *stg;
62 } MSITRANSFORM;
64 #define MAX_STREAM_NAME 0x1f
66 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
67 MSICOLUMNINFO **pcols, UINT *pcount );
68 static UINT get_tablecolumns( MSIDATABASE *db,
69 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
70 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
72 static inline UINT bytes_per_column( const MSICOLUMNINFO *col )
74 if( col->type & MSITYPE_STRING )
75 return 2;
76 if( (col->type & 0xff) > 4 )
77 ERR("Invalid column size!\n");
78 return col->type & 0xff;
81 static int utf2mime(int x)
83 if( (x>='0') && (x<='9') )
84 return x-'0';
85 if( (x>='A') && (x<='Z') )
86 return x-'A'+10;
87 if( (x>='a') && (x<='z') )
88 return x-'a'+10+26;
89 if( x=='.' )
90 return 10+26+26;
91 if( x=='_' )
92 return 10+26+26+1;
93 return -1;
96 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
98 DWORD count = MAX_STREAM_NAME;
99 DWORD ch, next;
100 LPWSTR out, p;
102 if( !bTable )
103 count = lstrlenW( in )+2;
104 out = msi_alloc( count*sizeof(WCHAR) );
105 p = out;
107 if( bTable )
109 *p++ = 0x4840;
110 count --;
112 while( count -- )
114 ch = *in++;
115 if( !ch )
117 *p = ch;
118 return out;
120 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
122 ch = utf2mime(ch) + 0x4800;
123 next = *in;
124 if( next && (next<0x80) )
126 next = utf2mime(next);
127 if( next >= 0 )
129 next += 0x3ffffc0;
130 ch += (next<<6);
131 in++;
135 *p++ = ch;
137 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
138 msi_free( out );
139 return NULL;
142 static int mime2utf(int x)
144 if( x<10 )
145 return x + '0';
146 if( x<(10+26))
147 return x - 10 + 'A';
148 if( x<(10+26+26))
149 return x - 10 - 26 + 'a';
150 if( x == (10+26+26) )
151 return '.';
152 return '_';
155 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
157 WCHAR ch;
158 DWORD count = 0;
160 while ( (ch = *in++) )
162 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
164 if( ch >= 0x4800 )
165 ch = mime2utf(ch-0x4800);
166 else
168 ch -= 0x3800;
169 *out++ = mime2utf(ch&0x3f);
170 count++;
171 ch = mime2utf((ch>>6)&0x3f);
174 *out++ = ch;
175 count++;
177 *out = 0;
178 return count;
181 void enum_stream_names( IStorage *stg )
183 IEnumSTATSTG *stgenum = NULL;
184 HRESULT r;
185 STATSTG stat;
186 ULONG n, count;
187 WCHAR name[0x40];
189 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
190 if( FAILED( r ) )
191 return;
193 n = 0;
194 while( 1 )
196 count = 0;
197 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
198 if( FAILED( r ) || !count )
199 break;
200 decode_streamname( stat.pwcsName, name );
201 TRACE("stream %2ld -> %s %s\n", n,
202 debugstr_w(stat.pwcsName), debugstr_w(name) );
203 n++;
206 IEnumSTATSTG_Release( stgenum );
209 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
210 USHORT **pdata, UINT *psz )
212 HRESULT r;
213 UINT ret = ERROR_FUNCTION_FAILED;
214 VOID *data;
215 ULONG sz, count;
216 IStream *stm = NULL;
217 STATSTG stat;
218 LPWSTR encname;
220 encname = encode_streamname(TRUE, stname);
222 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
224 r = IStorage_OpenStream(stg, encname, NULL,
225 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
226 msi_free( encname );
227 if( FAILED( r ) )
229 WARN("open stream failed r = %08lx - empty table?\n",r);
230 return ret;
233 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
234 if( FAILED( r ) )
236 WARN("open stream failed r = %08lx!\n",r);
237 goto end;
240 if( stat.cbSize.QuadPart >> 32 )
242 WARN("Too big!\n");
243 goto end;
246 sz = stat.cbSize.QuadPart;
247 data = msi_alloc( sz );
248 if( !data )
250 WARN("couldn't allocate memory r=%08lx!\n",r);
251 ret = ERROR_NOT_ENOUGH_MEMORY;
252 goto end;
255 r = IStream_Read(stm, data, sz, &count );
256 if( FAILED( r ) || ( count != sz ) )
258 msi_free( data );
259 WARN("read stream failed r = %08lx!\n",r);
260 goto end;
263 *pdata = data;
264 *psz = sz;
265 ret = ERROR_SUCCESS;
267 end:
268 IStream_Release( stm );
270 return ret;
273 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
275 LPWSTR encname;
276 HRESULT r;
278 encname = encode_streamname(FALSE, stname);
280 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
282 r = IStorage_OpenStream(db->storage, encname, NULL,
283 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
284 if( FAILED( r ) )
286 MSITRANSFORM *transform;
288 LIST_FOR_EACH_ENTRY( transform, &db->transforms, MSITRANSFORM, entry )
290 TRACE("looking for %s in transform storage\n", debugstr_w(stname) );
291 r = IStorage_OpenStream( transform->stg, encname, NULL,
292 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm );
293 if (SUCCEEDED(r))
294 break;
298 msi_free( encname );
300 return SUCCEEDED(r) ? ERROR_SUCCESS : ERROR_FUNCTION_FAILED;
303 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
304 USHORT **pdata, UINT *psz )
306 HRESULT r;
307 UINT ret = ERROR_FUNCTION_FAILED;
308 VOID *data;
309 ULONG sz, count;
310 IStream *stm = NULL;
311 STATSTG stat;
313 r = db_get_raw_stream( db, stname, &stm );
314 if( r != ERROR_SUCCESS)
315 return ret;
316 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
317 if( FAILED( r ) )
319 WARN("open stream failed r = %08lx!\n",r);
320 goto end;
323 if( stat.cbSize.QuadPart >> 32 )
325 WARN("Too big!\n");
326 goto end;
329 sz = stat.cbSize.QuadPart;
330 data = msi_alloc( sz );
331 if( !data )
333 WARN("couldn't allocate memory r=%08lx!\n",r);
334 ret = ERROR_NOT_ENOUGH_MEMORY;
335 goto end;
338 r = IStream_Read(stm, data, sz, &count );
339 if( FAILED( r ) || ( count != sz ) )
341 msi_free( data );
342 WARN("read stream failed r = %08lx!\n",r);
343 goto end;
346 *pdata = data;
347 *psz = sz;
348 ret = ERROR_SUCCESS;
350 end:
351 IStream_Release( stm );
353 return ret;
356 static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
357 LPVOID data, UINT sz )
359 HRESULT r;
360 UINT ret = ERROR_FUNCTION_FAILED;
361 ULONG count;
362 IStream *stm = NULL;
363 ULARGE_INTEGER size;
364 LARGE_INTEGER pos;
365 LPWSTR encname;
367 encname = encode_streamname(TRUE, stname );
368 r = IStorage_OpenStream( stg, encname, NULL,
369 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
370 if( FAILED(r) )
372 r = IStorage_CreateStream( stg, encname,
373 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
375 msi_free( encname );
376 if( FAILED( r ) )
378 WARN("open stream failed r = %08lx\n",r);
379 return ret;
382 size.QuadPart = sz;
383 r = IStream_SetSize( stm, size );
384 if( FAILED( r ) )
386 WARN("Failed to SetSize\n");
387 goto end;
390 pos.QuadPart = 0;
391 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
392 if( FAILED( r ) )
394 WARN("Failed to Seek\n");
395 goto end;
398 r = IStream_Write(stm, data, sz, &count );
399 if( FAILED( r ) || ( count != sz ) )
401 WARN("Failed to Write\n");
402 goto end;
405 ret = ERROR_SUCCESS;
407 end:
408 IStream_Release( stm );
410 return ret;
413 static void free_table( MSITABLE *table )
415 int i;
416 for( i=0; i<table->row_count; i++ )
417 msi_free( table->data[i] );
418 msi_free( table->data );
419 msi_free( table );
422 static UINT msi_table_get_row_size( const MSICOLUMNINFO *cols, UINT count )
424 const MSICOLUMNINFO *last_col = &cols[count-1];
425 if (!count)
426 return 0;
427 return last_col->offset + bytes_per_column( last_col );
430 /* add this table to the list of cached tables in the database */
431 static MSITABLE *read_table_from_storage( IStorage *stg, LPCWSTR name,
432 const MSICOLUMNINFO *cols, UINT num_cols )
434 MSITABLE *t;
435 USHORT *rawdata = NULL;
436 UINT rawsize = 0, i, j, row_size = 0;
438 TRACE("%s\n",debugstr_w(name));
440 /* nonexistent tables should be interpreted as empty tables */
441 t = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
442 if( !t )
443 return t;
445 row_size = msi_table_get_row_size( cols, num_cols );
447 t->row_count = 0;
448 t->data = NULL;
449 lstrcpyW( t->name, name );
451 /* if we can't read the table, just assume that it's empty */
452 read_stream_data( stg, name, &rawdata, &rawsize );
453 if( !rawdata )
454 return t;
456 TRACE("Read %d bytes\n", rawsize );
458 if( rawsize % row_size )
460 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
461 goto err;
464 t->row_count = rawsize / row_size;
465 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
466 if( !t->data )
467 goto err;
469 /* transpose all the data */
470 TRACE("Transposing data from %d columns\n", t->row_count );
471 for( i=0; i<t->row_count; i++ )
473 t->data[i] = msi_alloc( row_size );
474 if( !t->data[i] )
475 goto err;
477 for( j=0; j<num_cols; j++ )
479 UINT ofs = cols[j].offset/2;
480 UINT n = bytes_per_column( &cols[j] );
482 switch( n )
484 case 2:
485 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
486 break;
487 case 4:
488 t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
489 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
490 break;
491 default:
492 ERR("oops - unknown column width %d\n", n);
493 goto err;
498 msi_free( rawdata );
499 return t;
500 err:
501 msi_free( rawdata );
502 free_table( t );
503 return NULL;
506 void free_cached_tables( MSIDATABASE *db )
508 while( !list_empty( &db->tables ) )
510 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
512 list_remove( &t->entry );
513 free_table( t );
517 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
519 MSITABLE *t;
521 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
522 if( !lstrcmpW( name, t->name ) )
523 return t;
525 return NULL;
528 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
530 UINT r, column_count = 0;
531 MSICOLUMNINFO *columns;
533 /* get the number of columns in this table */
534 column_count = 0;
535 r = get_tablecolumns( db, name, NULL, &column_count );
536 if( r != ERROR_SUCCESS )
537 return r;
539 /* if there's no columns, there's no table */
540 if( column_count == 0 )
541 return ERROR_INVALID_PARAMETER;
543 TRACE("Table %s found\n", debugstr_w(name) );
545 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
546 if( !columns )
547 return ERROR_FUNCTION_FAILED;
549 r = get_tablecolumns( db, name, columns, &column_count );
550 if( r != ERROR_SUCCESS )
552 msi_free( columns );
553 return ERROR_FUNCTION_FAILED;
556 *pcols = columns;
557 *pcount = column_count;
559 return r;
562 static MSITABLE *get_table( MSIDATABASE *db, LPCWSTR name,
563 const MSICOLUMNINFO *cols, UINT num_cols )
565 MSITABLE *table;
567 /* first, see if the table is cached */
568 table = find_cached_table( db, name );
569 if( table )
570 return table;
572 table = read_table_from_storage( db->storage, name, cols, num_cols );
573 if( table )
574 list_add_head( &db->tables, &table->entry );
576 return table;
579 static UINT save_table( MSIDATABASE *db, MSITABLE *t )
581 USHORT *rawdata = NULL, *p;
582 UINT rawsize, r, i, j, row_size, num_cols = 0;
583 MSICOLUMNINFO *cols = NULL;
585 TRACE("Saving %s\n", debugstr_w( t->name ) );
587 r = table_get_column_info( db, t->name, &cols, &num_cols );
588 if( r != ERROR_SUCCESS )
589 return r;
591 row_size = msi_table_get_row_size( cols, num_cols );
593 rawsize = t->row_count * row_size;
594 rawdata = msi_alloc_zero( rawsize );
595 if( !rawdata )
597 r = ERROR_NOT_ENOUGH_MEMORY;
598 goto err;
601 p = rawdata;
602 for( i=0; i<num_cols; i++ )
604 for( j=0; j<t->row_count; j++ )
606 UINT offset = cols[i].offset;
608 *p++ = t->data[j][offset/2];
609 if( 4 == bytes_per_column( &cols[i] ) )
610 *p++ = t->data[j][offset/2+1];
614 TRACE("writing %d bytes\n", rawsize);
615 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
617 err:
618 msi_free_colinfo( cols, num_cols );
619 msi_free( cols );
620 msi_free( rawdata );
622 return r;
625 HRESULT init_string_table( IStorage *stg )
627 HRESULT r;
628 static const WCHAR szStringData[] = {
629 '_','S','t','r','i','n','g','D','a','t','a',0 };
630 static const WCHAR szStringPool[] = {
631 '_','S','t','r','i','n','g','P','o','o','l',0 };
632 USHORT zero[2] = { 0, 0 };
633 ULONG count = 0;
634 IStream *stm = NULL;
635 LPWSTR encname;
637 encname = encode_streamname(TRUE, szStringPool );
639 /* create the StringPool stream... add the zero string to it*/
640 r = IStorage_CreateStream( stg, encname,
641 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
642 msi_free( encname );
643 if( r )
645 TRACE("Failed\n");
646 return r;
649 r = IStream_Write(stm, zero, sizeof zero, &count );
650 IStream_Release( stm );
652 if( FAILED( r ) || ( count != sizeof zero ) )
654 TRACE("Failed\n");
655 return E_FAIL;
658 /* create the StringData stream... make it zero length */
659 encname = encode_streamname(TRUE, szStringData );
660 r = IStorage_CreateStream( stg, encname,
661 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
662 msi_free( encname );
663 if( r )
665 TRACE("Failed\n");
666 return E_FAIL;
668 IStream_Release( stm );
670 return r;
673 string_table *load_string_table( IStorage *stg )
675 string_table *st = NULL;
676 CHAR *data;
677 USHORT *pool;
678 UINT r, datasize = 0, poolsize = 0, codepage;
679 DWORD i, count, offset, len, n;
680 static const WCHAR szStringData[] = {
681 '_','S','t','r','i','n','g','D','a','t','a',0 };
682 static const WCHAR szStringPool[] = {
683 '_','S','t','r','i','n','g','P','o','o','l',0 };
685 r = read_stream_data( stg, szStringPool, &pool, &poolsize );
686 if( r != ERROR_SUCCESS)
687 goto end;
688 r = read_stream_data( stg, szStringData, (USHORT**)&data, &datasize );
689 if( r != ERROR_SUCCESS)
690 goto end;
692 count = poolsize/4;
693 if( poolsize > 4 )
694 codepage = pool[0] | ( pool[1] << 16 );
695 else
696 codepage = CP_ACP;
697 st = msi_init_stringtable( count, codepage );
699 offset = 0;
700 n = 1;
701 for( i=1; i<count; i++ )
703 len = pool[i*2];
706 * If a string is over 64k, the previous string entry is made null
707 * and its the high word of the length is inserted in the null string's
708 * reference count field.
710 if( pool[i*2-2] == 0 )
711 len += pool[i*2-1] * 0x10000;
713 if( (offset + len) > datasize )
715 ERR("string table corrupt?\n");
716 break;
719 /* don't add the high word of a string's length as a string */
720 if ( len || !pool[i*2+1] )
722 r = msi_addstring( st, n, data+offset, len, pool[i*2+1] );
723 if( r != n )
724 ERR("Failed to add string %ld\n", n );
725 n++;
728 offset += len;
731 if ( datasize != offset )
732 ERR("string table load failed! (%08x != %08lx)\n", datasize, offset );
734 TRACE("Loaded %ld strings\n", count);
736 end:
737 msi_free( pool );
738 msi_free( data );
740 return st;
743 static UINT save_string_table( MSIDATABASE *db )
745 UINT i, count, datasize, poolsize, sz, used, r, codepage;
746 UINT ret = ERROR_FUNCTION_FAILED;
747 static const WCHAR szStringData[] = {
748 '_','S','t','r','i','n','g','D','a','t','a',0 };
749 static const WCHAR szStringPool[] = {
750 '_','S','t','r','i','n','g','P','o','o','l',0 };
751 CHAR *data = NULL;
752 USHORT *pool = NULL;
754 TRACE("\n");
756 /* construct the new table in memory first */
757 datasize = msi_string_totalsize( db->strings, &count );
758 poolsize = count*2*sizeof(USHORT);
760 pool = msi_alloc( poolsize );
761 if( ! pool )
763 WARN("Failed to alloc pool %d bytes\n", poolsize );
764 goto err;
766 data = msi_alloc( datasize );
767 if( ! data )
769 WARN("Failed to alloc data %d bytes\n", poolsize );
770 goto err;
773 used = 0;
774 codepage = msi_string_get_codepage( db->strings );
775 pool[0]=codepage&0xffff;
776 pool[1]=(codepage>>16);
777 for( i=1; i<count; i++ )
779 sz = datasize - used;
780 r = msi_id2stringA( db->strings, i, data+used, &sz );
781 if( r != ERROR_SUCCESS )
783 ERR("failed to fetch string\n");
784 sz = 0;
786 if( sz && (sz < (datasize - used ) ) )
787 sz--;
788 TRACE("adding %u bytes %s\n", sz, debugstr_a(data+used) );
789 pool[ i*2 ] = sz;
790 pool[ i*2 + 1 ] = msi_id_refcount( db->strings, i );
791 used += sz;
792 if( used > datasize )
794 ERR("oops overran %d >= %d\n", used, datasize);
795 goto err;
799 if( used != datasize )
801 ERR("oops used %d != datasize %d\n", used, datasize);
802 goto err;
805 /* write the streams */
806 r = write_stream_data( db->storage, szStringData, data, datasize );
807 TRACE("Wrote StringData r=%08x\n", r);
808 if( r )
809 goto err;
810 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
811 TRACE("Wrote StringPool r=%08x\n", r);
812 if( r )
813 goto err;
815 ret = ERROR_SUCCESS;
817 err:
818 msi_free( data );
819 msi_free( pool );
821 return ret;
824 /* information for default tables */
825 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
826 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
827 static const WCHAR szName[] = { 'N','a','m','e',0 };
828 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
829 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
830 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
831 static const WCHAR szType[] = { 'T','y','p','e',0 };
833 static const MSICOLUMNINFO _Columns_cols[4] = {
834 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
835 { szColumns, 2, szNumber, MSITYPE_VALID | 2, 2 },
836 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4 },
837 { szColumns, 4, szType, MSITYPE_VALID | 2, 6 },
839 static const MSICOLUMNINFO _Tables_cols[1] = {
840 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 0 },
843 static UINT get_defaulttablecolumns( LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz)
845 const MSICOLUMNINFO *p;
846 DWORD i, n;
848 TRACE("%s\n", debugstr_w(name));
850 if (!lstrcmpW( name, szTables ))
852 p = _Tables_cols;
853 n = 1;
855 else if (!lstrcmpW( name, szColumns ))
857 p = _Columns_cols;
858 n = 4;
860 else
861 return ERROR_FUNCTION_FAILED;
863 /* duplicate the string data so we can free it in msi_free_colinfo */
864 for (i=0; i<n; i++)
866 if (colinfo && (i < *sz) )
868 memcpy( &colinfo[i], &p[i], sizeof(MSICOLUMNINFO) );
869 colinfo[i].tablename = strdupW( p[i].tablename );
870 colinfo[i].colname = strdupW( p[i].colname );
872 if( colinfo && (i >= *sz) )
873 break;
875 *sz = n;
876 return ERROR_SUCCESS;
879 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
881 UINT i;
883 for( i=0; i<count; i++ )
885 msi_free( (LPWSTR) colinfo[i].tablename );
886 msi_free( (LPWSTR) colinfo[i].colname );
890 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
892 UINT sz=0, r;
893 LPWSTR str;
895 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
896 if( r != ERROR_SUCCESS )
897 return NULL;
898 str = msi_alloc( sz*sizeof (WCHAR) );
899 if( !str )
900 return str;
901 r = msi_id2stringW( db->strings, stringid, str, &sz );
902 if( r == ERROR_SUCCESS )
903 return str;
904 msi_free( str );
905 return NULL;
908 static UINT get_tablecolumns( MSIDATABASE *db,
909 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
911 UINT r, i, n=0, table_id, count, maxcount = *sz;
912 MSITABLE *table = NULL;
914 /* first check if there is a default table with that name */
915 r = get_defaulttablecolumns( szTableName, colinfo, sz );
916 if( ( r == ERROR_SUCCESS ) && *sz )
917 return r;
919 table = get_table( db, szColumns, _Columns_cols, 4 );
920 if( !table )
922 ERR("couldn't load _Columns table\n");
923 return ERROR_FUNCTION_FAILED;
926 /* convert table and column names to IDs from the string table */
927 r = msi_string2idW( db->strings, szTableName, &table_id );
928 if( r != ERROR_SUCCESS )
930 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
931 return r;
934 TRACE("Table id is %d\n", table_id);
936 count = table->row_count;
937 for( i=0; i<count; i++ )
939 if( table->data[ i ][ 0 ] != table_id )
940 continue;
941 if( colinfo )
943 UINT id = table->data[ i ] [ 2 ];
944 colinfo[n].tablename = MSI_makestring( db, table_id );
945 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
946 colinfo[n].colname = MSI_makestring( db, id );
947 colinfo[n].type = table->data[ i ] [ 3 ] ^ 0x8000;
948 /* this assumes that columns are in order in the table */
949 if( n )
950 colinfo[n].offset = colinfo[n-1].offset
951 + bytes_per_column( &colinfo[n-1] );
952 else
953 colinfo[n].offset = 0;
954 TRACE("table %s column %d is [%s] (%d) with type %08x "
955 "offset %d at row %d\n", debugstr_w(szTableName),
956 colinfo[n].number, debugstr_w(colinfo[n].colname),
957 id, colinfo[n].type, colinfo[n].offset, i);
958 if( n != (colinfo[n].number-1) )
960 ERR("oops. data in the _Columns table isn't in the right "
961 "order for table %s\n", debugstr_w(szTableName));
962 return ERROR_FUNCTION_FAILED;
965 n++;
966 if( colinfo && ( n >= maxcount ) )
967 break;
969 *sz = n;
971 return ERROR_SUCCESS;
974 /* try to find the table name in the _Tables table */
975 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
977 UINT r, table_id = 0, i, count;
978 MSITABLE *table = NULL;
980 if( !lstrcmpW( name, szTables ) )
981 return TRUE;
982 if( !lstrcmpW( name, szColumns ) )
983 return TRUE;
985 r = msi_string2idW( db->strings, name, &table_id );
986 if( r != ERROR_SUCCESS )
988 TRACE("Couldn't find id for %s\n", debugstr_w(name));
989 return FALSE;
992 table = get_table( db, szTables, _Tables_cols, 1 );
993 if( !table )
995 TRACE("table %s not available\n", debugstr_w(szTables));
996 return FALSE;
999 /* count = table->size/2; */
1000 count = table->row_count;
1001 for( i=0; i<count; i++ )
1002 if( table->data[ i ][ 0 ] == table_id )
1003 break;
1005 if (i!=count)
1006 return TRUE;
1008 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1010 return FALSE;
1013 /* below is the query interface to a table */
1015 typedef struct tagMSITABLEVIEW
1017 MSIVIEW view;
1018 MSIDATABASE *db;
1019 MSITABLE *table;
1020 MSICOLUMNINFO *columns;
1021 UINT num_cols;
1022 UINT row_size;
1023 WCHAR name[1];
1024 } MSITABLEVIEW;
1026 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1028 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1029 UINT offset, num_rows, n;
1031 if( !tv->table )
1032 return ERROR_INVALID_PARAMETER;
1034 if( (col==0) || (col>tv->num_cols) )
1035 return ERROR_INVALID_PARAMETER;
1037 /* how many rows are there ? */
1038 num_rows = tv->table->row_count;
1039 if( row >= num_rows )
1040 return ERROR_NO_MORE_ITEMS;
1042 if( tv->columns[col-1].offset >= tv->row_size )
1044 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1045 ERR("%p %p\n", tv, tv->columns );
1046 return ERROR_FUNCTION_FAILED;
1049 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1050 n = bytes_per_column( &tv->columns[col-1] );
1051 switch( n )
1053 case 4:
1054 offset = tv->columns[col-1].offset/2;
1055 *val = tv->table->data[row][offset] +
1056 (tv->table->data[row][offset + 1] << 16);
1057 break;
1058 case 2:
1059 offset = tv->columns[col-1].offset/2;
1060 *val = tv->table->data[row][offset];
1061 break;
1062 default:
1063 ERR("oops! what is %d bytes per column?\n", n );
1064 return ERROR_FUNCTION_FAILED;
1067 /* TRACE("Data [%d][%d] = %d \n", row, col, *val ); */
1069 return ERROR_SUCCESS;
1073 * We need a special case for streams, as we need to reference column with
1074 * the name of the stream in the same table, and the table name
1075 * which may not be available at higher levels of the query
1077 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1079 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1080 UINT ival = 0, refcol = 0, r;
1081 LPWSTR sval;
1082 LPWSTR full_name;
1083 DWORD len;
1084 static const WCHAR szDot[] = { '.', 0 };
1086 if( !view->ops->fetch_int )
1087 return ERROR_INVALID_PARAMETER;
1090 * The column marked with the type stream data seems to have a single number
1091 * which references the column containing the name of the stream data
1093 * Fetch the column to reference first.
1095 r = view->ops->fetch_int( view, row, col, &ival );
1096 if( r != ERROR_SUCCESS )
1097 return r;
1099 /* now get the column with the name of the stream */
1100 r = view->ops->fetch_int( view, row, ival, &refcol );
1101 if( r != ERROR_SUCCESS )
1102 return r;
1104 /* lookup the string value from the string table */
1105 sval = MSI_makestring( tv->db, refcol );
1106 if( !sval )
1107 return ERROR_INVALID_PARAMETER;
1109 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1110 full_name = msi_alloc( len*sizeof(WCHAR) );
1111 lstrcpyW( full_name, tv->name );
1112 lstrcatW( full_name, szDot );
1113 lstrcatW( full_name, sval );
1115 r = db_get_raw_stream( tv->db, full_name, stm );
1116 if( r )
1117 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1118 msi_free( full_name );
1119 msi_free( sval );
1121 return r;
1124 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1126 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1127 UINT offset, n;
1129 if( !tv->table )
1130 return ERROR_INVALID_PARAMETER;
1132 if( (col==0) || (col>tv->num_cols) )
1133 return ERROR_INVALID_PARAMETER;
1135 if( tv->columns[col-1].offset >= tv->row_size )
1137 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1138 ERR("%p %p\n", tv, tv->columns );
1139 return ERROR_FUNCTION_FAILED;
1142 n = bytes_per_column( &tv->columns[col-1] );
1143 switch( n )
1145 case 4:
1146 offset = tv->columns[col-1].offset/2;
1147 tv->table->data[row][offset] = val & 0xffff;
1148 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1149 break;
1150 case 2:
1151 offset = tv->columns[col-1].offset/2;
1152 tv->table->data[row][offset] = val;
1153 break;
1154 default:
1155 ERR("oops! what is %d bytes per column?\n", n );
1156 return ERROR_FUNCTION_FAILED;
1158 return ERROR_SUCCESS;
1161 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
1163 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1164 USHORT **p, *row;
1165 UINT sz;
1167 TRACE("%p\n", view);
1169 if( !tv->table )
1170 return ERROR_INVALID_PARAMETER;
1172 row = msi_alloc_zero( tv->row_size );
1173 if( !row )
1174 return ERROR_NOT_ENOUGH_MEMORY;
1176 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1177 if( tv->table->data )
1178 p = msi_realloc( tv->table->data, sz );
1179 else
1180 p = msi_alloc( sz );
1181 if( !p )
1183 msi_free( row );
1184 return ERROR_NOT_ENOUGH_MEMORY;
1187 tv->table->data = p;
1188 tv->table->data[tv->table->row_count] = row;
1189 *num = tv->table->row_count;
1190 tv->table->row_count++;
1192 return ERROR_SUCCESS;
1195 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1197 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1199 TRACE("%p %p\n", tv, record);
1201 TRACE("There are %d columns\n", tv->num_cols );
1202 tv->table = get_table( tv->db, tv->name, tv->columns, tv->num_cols );
1203 if( !tv->table )
1204 return ERROR_FUNCTION_FAILED;
1206 return ERROR_SUCCESS;
1209 static UINT TABLE_close( struct tagMSIVIEW *view )
1211 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1213 TRACE("%p\n", view );
1215 if( !tv->table )
1216 return ERROR_FUNCTION_FAILED;
1218 tv->table = NULL;
1220 return ERROR_SUCCESS;
1223 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1225 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1227 TRACE("%p %p %p\n", view, rows, cols );
1229 if( cols )
1230 *cols = tv->num_cols;
1231 if( rows )
1233 if( !tv->table )
1234 return ERROR_INVALID_PARAMETER;
1235 *rows = tv->table->row_count;
1238 return ERROR_SUCCESS;
1241 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1242 UINT n, LPWSTR *name, UINT *type )
1244 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1246 TRACE("%p %d %p %p\n", tv, n, name, type );
1248 if( ( n == 0 ) || ( n > tv->num_cols ) )
1249 return ERROR_INVALID_PARAMETER;
1251 if( name )
1253 *name = strdupW( tv->columns[n-1].colname );
1254 if( !*name )
1255 return ERROR_FUNCTION_FAILED;
1257 if( type )
1258 *type = tv->columns[n-1].type;
1260 return ERROR_SUCCESS;
1263 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1265 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1267 UINT r, row;
1269 r = msi_table_find_row( tv, rec, &row );
1270 if (r != ERROR_SUCCESS)
1271 return ERROR_SUCCESS;
1272 return ERROR_INVALID_DATA;
1275 static UINT msi_table_modify_row( MSITABLEVIEW *tv, MSIRECORD *rec,
1276 UINT row, UINT mask )
1278 UINT i, val, r = ERROR_SUCCESS;
1280 TRACE("%p %p %u %08x\n", tv, rec, row, mask );
1282 for( i = 0; i < tv->num_cols; i++ )
1284 /* set keys or values specified in the mask */
1285 if( (~tv->columns[i].type & MSITYPE_KEY) && (~mask & (1<<i)) )
1286 continue;
1288 if( (tv->columns[i].type & MSITYPE_STRING) &&
1289 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1291 const WCHAR *str = MSI_RecordGetString( rec, i+1 );
1292 val = msi_addstringW( tv->db->strings, 0, str, -1, 1 );
1294 else
1296 val = MSI_RecordGetInteger( rec, i+1 );
1297 if ( 2 == bytes_per_column( &tv->columns[i] ) )
1298 val ^= 0x8000;
1300 r = TABLE_set_int( &tv->view, row, i+1, val );
1301 if( r )
1302 break;
1305 return r;
1308 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1310 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1311 UINT r, row = -1;
1313 TRACE("%p %p\n", tv, rec );
1315 r = table_create_new_row( view, &row );
1316 TRACE("insert_row returned %08x\n", r);
1317 if( r != ERROR_SUCCESS )
1318 return r;
1320 return msi_table_modify_row( tv, rec, row, ~0 );
1323 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1324 MSIRECORD *rec)
1326 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1327 UINT r;
1329 TRACE("%p %d %p\n", view, eModifyMode, rec );
1331 if (!tv->table)
1333 r = TABLE_execute( view, NULL );
1334 if( r )
1335 return r;
1338 switch (eModifyMode)
1340 case MSIMODIFY_VALIDATE_NEW:
1341 r = table_validate_new( tv, rec );
1342 break;
1344 case MSIMODIFY_INSERT_TEMPORARY:
1345 r = table_validate_new( tv, rec );
1346 if (r != ERROR_SUCCESS)
1347 break;
1348 r = TABLE_insert_row( view, rec );
1349 break;
1351 case MSIMODIFY_REFRESH:
1352 case MSIMODIFY_INSERT:
1353 case MSIMODIFY_UPDATE:
1354 case MSIMODIFY_ASSIGN:
1355 case MSIMODIFY_REPLACE:
1356 case MSIMODIFY_MERGE:
1357 case MSIMODIFY_DELETE:
1358 case MSIMODIFY_VALIDATE:
1359 case MSIMODIFY_VALIDATE_FIELD:
1360 case MSIMODIFY_VALIDATE_DELETE:
1361 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1362 r = ERROR_CALL_NOT_IMPLEMENTED;
1363 break;
1365 default:
1366 r = ERROR_INVALID_DATA;
1369 return r;
1372 static UINT TABLE_delete( struct tagMSIVIEW *view )
1374 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1376 TRACE("%p\n", view );
1378 tv->table = NULL;
1380 if( tv->columns )
1382 msi_free_colinfo( tv->columns, tv->num_cols );
1383 msi_free( tv->columns );
1385 tv->columns = NULL;
1387 msi_free( tv );
1389 return ERROR_SUCCESS;
1393 MSIVIEWOPS table_ops =
1395 TABLE_fetch_int,
1396 TABLE_fetch_stream,
1397 TABLE_set_int,
1398 TABLE_insert_row,
1399 TABLE_execute,
1400 TABLE_close,
1401 TABLE_get_dimensions,
1402 TABLE_get_column_info,
1403 TABLE_modify,
1404 TABLE_delete
1407 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1409 MSITABLEVIEW *tv ;
1410 UINT r, sz, column_count;
1411 MSICOLUMNINFO *columns;
1413 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1415 /* get the number of columns in this table */
1416 column_count = 0;
1417 r = get_tablecolumns( db, name, NULL, &column_count );
1418 if( r != ERROR_SUCCESS )
1419 return r;
1421 /* if there's no columns, there's no table */
1422 if( column_count == 0 )
1423 return ERROR_INVALID_PARAMETER;
1425 TRACE("Table found\n");
1427 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1428 tv = msi_alloc_zero( sz );
1429 if( !tv )
1430 return ERROR_FUNCTION_FAILED;
1432 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO));
1433 if( !columns )
1435 msi_free( tv );
1436 return ERROR_FUNCTION_FAILED;
1439 r = get_tablecolumns( db, name, columns, &column_count );
1440 if( r != ERROR_SUCCESS )
1442 msi_free( columns );
1443 msi_free( tv );
1444 return ERROR_FUNCTION_FAILED;
1447 TRACE("Table has %d columns\n", column_count);
1449 /* fill the structure */
1450 tv->view.ops = &table_ops;
1451 tv->db = db;
1452 tv->columns = columns;
1453 tv->num_cols = column_count;
1454 tv->table = NULL;
1455 tv->row_size = msi_table_get_row_size( columns, column_count );
1457 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
1459 *view = (MSIVIEW*) tv;
1460 lstrcpyW( tv->name, name );
1462 return ERROR_SUCCESS;
1465 UINT MSI_CommitTables( MSIDATABASE *db )
1467 UINT r;
1468 MSITABLE *table = NULL;
1470 TRACE("%p\n",db);
1472 r = save_string_table( db );
1473 if( r != ERROR_SUCCESS )
1475 WARN("failed to save string table r=%08x\n",r);
1476 return r;
1479 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1481 r = save_table( db, table );
1482 if( r != ERROR_SUCCESS )
1484 WARN("failed to save table %s (r=%08x)\n",
1485 debugstr_w(table->name), r);
1486 return r;
1490 /* force everything to reload next time */
1491 free_cached_tables( db );
1493 return ERROR_SUCCESS;
1496 static MSIRECORD *msi_get_transform_record( MSITABLEVIEW *tv, string_table *st, USHORT *rawdata )
1498 UINT i, val, ofs = 0;
1499 USHORT mask = *rawdata++;
1500 MSICOLUMNINFO *columns = tv->columns;
1501 MSIRECORD *rec;
1502 const int debug_transform = 0;
1504 rec = MSI_CreateRecord( tv->num_cols );
1505 if( !rec )
1506 return rec;
1508 if( debug_transform ) MESSAGE("row -> ");
1509 for( i=0; i<tv->num_cols; i++ )
1511 UINT n = bytes_per_column( &columns[i] );
1513 if ( (mask&1) && (i>=(mask>>8)) )
1514 break;
1515 /* all keys must be present */
1516 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
1517 continue;
1519 switch( n )
1521 case 2:
1522 val = rawdata[ofs];
1523 if( (columns[i].type & MSITYPE_STRING) &&
1524 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1526 LPCWSTR sval = msi_string_lookup_id( st, val );
1527 MSI_RecordSetStringW( rec, i+1, sval );
1528 if( debug_transform ) MESSAGE("[%s]", debugstr_w(sval));
1530 else
1532 val ^= 0x8000;
1533 MSI_RecordSetInteger( rec, i+1, val );
1534 if( debug_transform) MESSAGE("[0x%04x]", val );
1536 break;
1537 case 4:
1538 val = rawdata[ofs] + (rawdata[ofs + 1]<<16);
1539 /* val ^= 0x80000000; */
1540 MSI_RecordSetInteger( rec, i+1, val );
1541 if( debug_transform ) MESSAGE("[0x%08x]", val );
1542 break;
1543 default:
1544 ERR("oops - unknown column width %d\n", n);
1545 break;
1547 ofs += n/2;
1549 if( debug_transform) MESSAGE("\n");
1550 return rec;
1553 static void dump_record( MSIRECORD *rec )
1555 UINT i, n;
1557 MESSAGE("row -> ");
1558 n = MSI_RecordGetFieldCount( rec );
1559 for( i=1; i<=n; i++ )
1561 LPCWSTR sval = MSI_RecordGetString( rec, i );
1563 if( MSI_RecordIsNull( rec, i ) )
1564 MESSAGE("[]");
1565 else if( (sval = MSI_RecordGetString( rec, i )) )
1566 MESSAGE("[%s]", debugstr_w(sval));
1567 else
1568 MESSAGE("[0x%08x]", MSI_RecordGetInteger( rec, i ) );
1570 MESSAGE("\n");
1573 static void dump_table( string_table *st, USHORT *rawdata, UINT rawsize )
1575 LPCWSTR sval;
1576 UINT i;
1578 for( i=0; i<(rawsize/2); i++ )
1580 sval = msi_string_lookup_id( st, rawdata[i] );
1581 if( !sval ) sval = (WCHAR[]) {0};
1582 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
1586 static UINT* msi_record_to_row( MSITABLEVIEW *tv, MSIRECORD *rec )
1588 LPCWSTR str;
1589 UINT i, r, *data;
1591 data = msi_alloc( tv->num_cols *sizeof (UINT) );
1592 for( i=0; i<tv->num_cols; i++ )
1594 data[i] = 0;
1596 if ( ~tv->columns[i].type & MSITYPE_KEY )
1597 continue;
1599 /* turn the transform column value into a row value */
1600 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
1601 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
1603 str = MSI_RecordGetString( rec, i+1 );
1604 r = msi_string2idW( tv->db->strings, str, &data[i] );
1606 /* if there's no matching string in the string table,
1607 these keys can't match any record, so fail now. */
1608 if( ERROR_SUCCESS != r )
1610 msi_free( data );
1611 return NULL;
1614 else
1615 data[i] = MSI_RecordGetInteger( rec, i+1 );
1617 return data;
1620 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, UINT *data )
1622 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
1624 for( i=0; i<tv->num_cols; i++ )
1626 if ( ~tv->columns[i].type & MSITYPE_KEY )
1627 continue;
1629 /* turn the transform column value into a row value */
1630 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
1631 if ( r != ERROR_SUCCESS )
1633 ERR("TABLE_fetch_int shouldn't fail here\n");
1634 break;
1637 /* if this key matches, move to the next column */
1638 if ( x != data[i] )
1640 ret = ERROR_FUNCTION_FAILED;
1641 break;
1644 ret = ERROR_SUCCESS;
1647 return ret;
1650 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
1652 UINT i, r = ERROR_FUNCTION_FAILED, *data;
1654 data = msi_record_to_row( tv, rec );
1655 if( !data )
1656 return r;
1657 for( i=0; i<tv->table->row_count; i++ )
1659 r = msi_row_matches( tv, i, data );
1660 if( r == ERROR_SUCCESS )
1662 *row = i;
1663 break;
1666 msi_free( data );
1667 return r;
1670 static UINT msi_delete_row( MSITABLEVIEW *tv, UINT row )
1672 UINT i;
1673 for( i=1; i<=tv->num_cols; i++ )
1674 tv->view.ops->set_int( &tv->view, row, i, 0 );
1675 return ERROR_SUCCESS;
1678 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
1679 string_table *st, LPCWSTR name )
1681 UINT rawsize = 0;
1682 USHORT *rawdata = NULL;
1683 MSITABLEVIEW *tv = NULL;
1684 UINT r, n, sz, i, mask;
1685 MSIRECORD *rec = NULL;
1686 const int debug_transform = 0;
1688 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
1690 /* create a table view */
1691 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
1692 if( r != ERROR_SUCCESS )
1693 goto err;
1695 r = tv->view.ops->execute( &tv->view, NULL );
1696 if( r != ERROR_SUCCESS )
1697 goto err;
1699 /* read the transform data */
1700 r = ERROR_FUNCTION_FAILED;
1701 read_stream_data( stg, name, &rawdata, &rawsize );
1702 if( !rawdata || (rawsize < 2) )
1704 ERR("odd sized transform for table %s\n", debugstr_w(name));
1705 goto err;
1708 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
1709 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
1711 /* interpret the data */
1712 r = ERROR_SUCCESS;
1713 for( n=0; n < (rawsize/2); )
1715 mask = rawdata[n];
1717 if (mask&1)
1720 * if the low bit is set, columns are continuous and
1721 * the number of columns is specified in the high byte
1723 sz = 2 + tv->row_size;
1725 else
1728 * If the low bit is not set, rowdata[n] is a bitmask.
1729 * Excepting for key fields, which are always present,
1730 * each bit indicates that a field is present in the transform record.
1732 * rawdata[n] == 0 is a special case ... only the keys will be present
1733 * and it means that this row should be deleted.
1735 sz = 2;
1736 for( i=0; i<tv->num_cols; i++ )
1738 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
1739 sz += bytes_per_column( &tv->columns[i] );
1743 /* check we didn't run of the end of the table */
1744 if ( (n+sz) > rawsize )
1746 ERR("borked.\n");
1747 dump_table( st, rawdata, rawsize );
1748 break;
1751 rec = msi_get_transform_record( tv, st, &rawdata[n] );
1752 if (rec)
1754 UINT row = 0;
1756 r = msi_table_find_row( tv, rec, &row );
1758 if( rawdata[n] & 1)
1760 if( debug_transform ) MESSAGE("insert [%d]: ", row);
1761 TABLE_insert_row( &tv->view, rec );
1763 else if( mask & 0xff )
1765 if( debug_transform ) MESSAGE("modify [%d]: ", row);
1766 msi_table_modify_row( tv, rec, row, mask );
1768 else
1770 if( debug_transform ) MESSAGE("delete [%d]: ", row);
1771 msi_delete_row( tv, row );
1773 if( debug_transform ) dump_record( rec );
1774 msiobj_release( &rec->hdr );
1777 n += sz/2;
1781 err:
1782 /* no need to free the table, it's associated with the database */
1783 msi_free( rawdata );
1784 if( tv )
1785 tv->view.ops->delete( &tv->view );
1787 return ERROR_SUCCESS;
1791 * msi_table_apply_transform
1793 * Enumerate the table transforms in a transform storage and apply each one.
1795 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
1797 IEnumSTATSTG *stgenum = NULL;
1798 HRESULT r;
1799 STATSTG stat;
1800 ULONG n, count;
1801 WCHAR name[0x40];
1802 string_table *strings;
1803 UINT ret = ERROR_FUNCTION_FAILED;
1805 TRACE("%p %p\n", db, stg );
1807 strings = load_string_table( stg );
1808 if( !strings )
1809 goto end;
1811 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
1812 if( FAILED( r ) )
1813 goto end;
1815 n = 0;
1816 ret = ERROR_SUCCESS;
1818 while( r == ERROR_SUCCESS )
1820 count = 0;
1821 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
1822 if( FAILED( r ) || !count )
1823 break;
1824 decode_streamname( stat.pwcsName, name );
1825 if( ( name[0] == 0x4840 ) && ( name[1] != '_' ) )
1826 ret = msi_table_load_transform( db, stg, strings, name+1 );
1827 else
1828 TRACE("transform contains stream %s\n", debugstr_w(name));
1829 n++;
1832 if ( ret == ERROR_SUCCESS )
1834 MSITRANSFORM *t;
1836 t = msi_alloc( sizeof *t );
1837 t->stg = stg;
1838 IStorage_AddRef( stg );
1839 list_add_tail( &db->transforms, &t->entry );
1842 end:
1843 if ( stgenum )
1844 IEnumSTATSTG_Release( stgenum );
1845 if ( strings )
1846 msi_destroy_stringtable( strings );
1848 return ret;
1851 void msi_free_transforms( MSIDATABASE *db )
1853 while( !list_empty( &db->transforms ) )
1855 MSITRANSFORM *t = LIST_ENTRY( list_head( &db->transforms ),
1856 MSITRANSFORM, entry );
1857 list_remove( &t->entry );
1858 IStorage_Release( t->stg );
1859 msi_free( t );