ntoskrnl.exe: Add -norelay flag for _vsnprintf.
[wine.git] / dlls / msi / table.c
blobc0ffa99e1d698b481ec98403abb1442c5c045941
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>
22 #include <assert.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "msi.h"
30 #include "msiquery.h"
31 #include "objbase.h"
32 #include "objidl.h"
33 #include "winnls.h"
34 #include "msipriv.h"
35 #include "query.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
41 #define MSITABLE_HASH_TABLE_SIZE 37
43 typedef struct tagMSICOLUMNHASHENTRY
45 struct tagMSICOLUMNHASHENTRY *next;
46 UINT value;
47 UINT row;
48 } MSICOLUMNHASHENTRY;
50 typedef struct tagMSICOLUMNINFO
52 LPCWSTR tablename;
53 UINT number;
54 LPCWSTR colname;
55 UINT type;
56 UINT offset;
57 INT ref_count;
58 BOOL temporary;
59 MSICOLUMNHASHENTRY **hash_table;
60 } MSICOLUMNINFO;
62 struct tagMSITABLE
64 BYTE **data;
65 BOOL *data_persistent;
66 UINT row_count;
67 struct list entry;
68 MSICOLUMNINFO *colinfo;
69 UINT col_count;
70 MSICONDITION persistent;
71 INT ref_count;
72 WCHAR name[1];
75 /* information for default tables */
76 static const WCHAR szTables[] = {'_','T','a','b','l','e','s',0};
77 static const WCHAR szTable[] = {'T','a','b','l','e',0};
78 static const WCHAR szColumns[] = {'_','C','o','l','u','m','n','s',0};
79 static const WCHAR szNumber[] = {'N','u','m','b','e','r',0};
80 static const WCHAR szType[] = {'T','y','p','e',0};
82 static const MSICOLUMNINFO _Columns_cols[4] = {
83 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
84 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2, 0, 0, NULL },
85 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
86 { szColumns, 4, szType, MSITYPE_VALID | 2, 6, 0, 0, NULL },
89 static const MSICOLUMNINFO _Tables_cols[1] = {
90 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
93 #define MAX_STREAM_NAME 0x1f
95 static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col, UINT bytes_per_strref )
97 if( MSITYPE_IS_BINARY(col->type) )
98 return 2;
100 if( col->type & MSITYPE_STRING )
101 return bytes_per_strref;
103 if( (col->type & 0xff) <= 2)
104 return 2;
106 if( (col->type & 0xff) != 4 )
107 ERR("Invalid column size %u\n", col->type & 0xff);
109 return 4;
112 static int utf2mime(int x)
114 if( (x>='0') && (x<='9') )
115 return x-'0';
116 if( (x>='A') && (x<='Z') )
117 return x-'A'+10;
118 if( (x>='a') && (x<='z') )
119 return x-'a'+10+26;
120 if( x=='.' )
121 return 10+26+26;
122 if( x=='_' )
123 return 10+26+26+1;
124 return -1;
127 LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
129 DWORD count = MAX_STREAM_NAME;
130 DWORD ch, next;
131 LPWSTR out, p;
133 if( !bTable )
134 count = lstrlenW( in )+2;
135 if (!(out = msi_alloc( count*sizeof(WCHAR) ))) return NULL;
136 p = out;
138 if( bTable )
140 *p++ = 0x4840;
141 count --;
143 while( count -- )
145 ch = *in++;
146 if( !ch )
148 *p = ch;
149 return out;
151 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
153 ch = utf2mime(ch) + 0x4800;
154 next = *in;
155 if( next && (next<0x80) )
157 next = utf2mime(next);
158 if( next != -1 )
160 next += 0x3ffffc0;
161 ch += (next<<6);
162 in++;
166 *p++ = ch;
168 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
169 msi_free( out );
170 return NULL;
173 static int mime2utf(int x)
175 if( x<10 )
176 return x + '0';
177 if( x<(10+26))
178 return x - 10 + 'A';
179 if( x<(10+26+26))
180 return x - 10 - 26 + 'a';
181 if( x == (10+26+26) )
182 return '.';
183 return '_';
186 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
188 WCHAR ch;
189 DWORD count = 0;
191 while ( (ch = *in++) )
193 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
195 if( ch >= 0x4800 )
196 ch = mime2utf(ch-0x4800);
197 else
199 ch -= 0x3800;
200 *out++ = mime2utf(ch&0x3f);
201 count++;
202 ch = mime2utf((ch>>6)&0x3f);
205 *out++ = ch;
206 count++;
208 *out = 0;
209 return count;
212 void enum_stream_names( IStorage *stg )
214 IEnumSTATSTG *stgenum = NULL;
215 HRESULT r;
216 STATSTG stat;
217 ULONG n, count;
218 WCHAR name[0x40];
220 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
221 if( FAILED( r ) )
222 return;
224 n = 0;
225 while( 1 )
227 count = 0;
228 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
229 if( FAILED( r ) || !count )
230 break;
231 decode_streamname( stat.pwcsName, name );
232 TRACE("stream %2d -> %s %s\n", n,
233 debugstr_w(stat.pwcsName), debugstr_w(name) );
234 CoTaskMemFree( stat.pwcsName );
235 n++;
238 IEnumSTATSTG_Release( stgenum );
241 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
242 BYTE **pdata, UINT *psz )
244 HRESULT r;
245 UINT ret = ERROR_FUNCTION_FAILED;
246 VOID *data;
247 ULONG sz, count;
248 IStream *stm = NULL;
249 STATSTG stat;
250 LPWSTR encname;
252 encname = encode_streamname(table, stname);
254 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
256 r = IStorage_OpenStream(stg, encname, NULL,
257 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
258 msi_free( encname );
259 if( FAILED( r ) )
261 WARN("open stream failed r = %08x - empty table?\n", r);
262 return ret;
265 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
266 if( FAILED( r ) )
268 WARN("open stream failed r = %08x!\n", r);
269 goto end;
272 if( stat.cbSize.QuadPart >> 32 )
274 WARN("Too big!\n");
275 goto end;
278 sz = stat.cbSize.QuadPart;
279 data = msi_alloc( sz );
280 if( !data )
282 WARN("couldn't allocate memory r=%08x!\n", r);
283 ret = ERROR_NOT_ENOUGH_MEMORY;
284 goto end;
287 r = IStream_Read(stm, data, sz, &count );
288 if( FAILED( r ) || ( count != sz ) )
290 msi_free( data );
291 WARN("read stream failed r = %08x!\n", r);
292 goto end;
295 *pdata = data;
296 *psz = sz;
297 ret = ERROR_SUCCESS;
299 end:
300 IStream_Release( stm );
302 return ret;
305 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
306 LPCVOID data, UINT sz, BOOL bTable )
308 HRESULT r;
309 UINT ret = ERROR_FUNCTION_FAILED;
310 ULONG count;
311 IStream *stm = NULL;
312 ULARGE_INTEGER size;
313 LARGE_INTEGER pos;
314 LPWSTR encname;
316 encname = encode_streamname(bTable, stname );
317 r = IStorage_OpenStream( stg, encname, NULL,
318 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
319 if( FAILED(r) )
321 r = IStorage_CreateStream( stg, encname,
322 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
324 msi_free( encname );
325 if( FAILED( r ) )
327 WARN("open stream failed r = %08x\n", r);
328 return ret;
331 size.QuadPart = sz;
332 r = IStream_SetSize( stm, size );
333 if( FAILED( r ) )
335 WARN("Failed to SetSize\n");
336 goto end;
339 pos.QuadPart = 0;
340 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
341 if( FAILED( r ) )
343 WARN("Failed to Seek\n");
344 goto end;
347 if (sz)
349 r = IStream_Write(stm, data, sz, &count );
350 if( FAILED( r ) || ( count != sz ) )
352 WARN("Failed to Write\n");
353 goto end;
357 ret = ERROR_SUCCESS;
359 end:
360 IStream_Release( stm );
362 return ret;
365 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
367 UINT i;
368 for (i = 0; i < count; i++) msi_free( colinfo[i].hash_table );
371 static void free_table( MSITABLE *table )
373 UINT i;
374 for( i=0; i<table->row_count; i++ )
375 msi_free( table->data[i] );
376 msi_free( table->data );
377 msi_free( table->data_persistent );
378 msi_free_colinfo( table->colinfo, table->col_count );
379 msi_free( table->colinfo );
380 msi_free( table );
383 static UINT msi_table_get_row_size( MSIDATABASE *db, const MSICOLUMNINFO *cols, UINT count, UINT bytes_per_strref )
385 const MSICOLUMNINFO *last_col;
387 if (!count)
388 return 0;
390 if (bytes_per_strref != LONG_STR_BYTES)
392 UINT i, size = 0;
393 for (i = 0; i < count; i++) size += bytes_per_column( db, &cols[i], bytes_per_strref );
394 return size;
396 last_col = &cols[count - 1];
397 return last_col->offset + bytes_per_column( db, last_col, bytes_per_strref );
400 /* add this table to the list of cached tables in the database */
401 static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
403 BYTE *rawdata = NULL;
404 UINT rawsize = 0, i, j, row_size, row_size_mem;
406 TRACE("%s\n",debugstr_w(t->name));
408 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count, db->bytes_per_strref );
409 row_size_mem = msi_table_get_row_size( db, t->colinfo, t->col_count, LONG_STR_BYTES );
411 /* if we can't read the table, just assume that it's empty */
412 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
413 if( !rawdata )
414 return ERROR_SUCCESS;
416 TRACE("Read %d bytes\n", rawsize );
418 if( rawsize % row_size )
420 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
421 goto err;
424 if ((t->row_count = rawsize / row_size))
426 if (!(t->data = msi_alloc_zero( t->row_count * sizeof(USHORT *) ))) goto err;
427 if (!(t->data_persistent = msi_alloc_zero( t->row_count * sizeof(BOOL) ))) goto err;
430 /* transpose all the data */
431 TRACE("Transposing data from %d rows\n", t->row_count );
432 for (i = 0; i < t->row_count; i++)
434 UINT ofs = 0, ofs_mem = 0;
436 t->data[i] = msi_alloc( row_size_mem );
437 if( !t->data[i] )
438 goto err;
439 t->data_persistent[i] = TRUE;
441 for (j = 0; j < t->col_count; j++)
443 UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
444 UINT n = bytes_per_column( db, &t->colinfo[j], db->bytes_per_strref );
445 UINT k;
447 if ( n != 2 && n != 3 && n != 4 )
449 ERR("oops - unknown column width %d\n", n);
450 goto err;
452 if (t->colinfo[j].type & MSITYPE_STRING && n < m)
454 for (k = 0; k < m; k++)
456 if (k < n)
457 t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
458 else
459 t->data[i][ofs_mem + k] = 0;
462 else
464 for (k = 0; k < n; k++)
465 t->data[i][ofs_mem + k] = rawdata[ofs * t->row_count + i * n + k];
467 ofs_mem += m;
468 ofs += n;
472 msi_free( rawdata );
473 return ERROR_SUCCESS;
474 err:
475 msi_free( rawdata );
476 return ERROR_FUNCTION_FAILED;
479 void free_cached_tables( MSIDATABASE *db )
481 while( !list_empty( &db->tables ) )
483 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
485 list_remove( &t->entry );
486 free_table( t );
490 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
492 MSITABLE *t;
494 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
495 if( !wcscmp( name, t->name ) )
496 return t;
498 return NULL;
501 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo, DWORD count )
503 DWORD i;
505 for (i = 0; colinfo && i < count; i++)
507 assert( i + 1 == colinfo[i].number );
508 if (i) colinfo[i].offset = colinfo[i - 1].offset +
509 bytes_per_column( db, &colinfo[i - 1], LONG_STR_BYTES );
510 else colinfo[i].offset = 0;
512 TRACE("column %d is [%s] with type %08x ofs %d\n",
513 colinfo[i].number, debugstr_w(colinfo[i].colname),
514 colinfo[i].type, colinfo[i].offset);
518 static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO *colinfo, UINT *sz )
520 const MSICOLUMNINFO *p;
521 DWORD i, n;
523 TRACE("%s\n", debugstr_w(name));
525 if (!wcscmp( name, szTables ))
527 p = _Tables_cols;
528 n = 1;
530 else if (!wcscmp( name, szColumns ))
532 p = _Columns_cols;
533 n = 4;
535 else return ERROR_FUNCTION_FAILED;
537 for (i = 0; i < n; i++)
539 if (colinfo && i < *sz) colinfo[i] = p[i];
540 if (colinfo && i >= *sz) break;
542 table_calc_column_offsets( db, colinfo, n );
543 *sz = n;
544 return ERROR_SUCCESS;
547 static UINT get_tablecolumns( MSIDATABASE *db, LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz );
549 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
551 UINT r, column_count = 0;
552 MSICOLUMNINFO *columns;
554 /* get the number of columns in this table */
555 column_count = 0;
556 r = get_tablecolumns( db, name, NULL, &column_count );
557 if (r != ERROR_SUCCESS)
558 return r;
560 *pcount = column_count;
562 /* if there are no columns, there's no table */
563 if (!column_count)
564 return ERROR_INVALID_PARAMETER;
566 TRACE("table %s found\n", debugstr_w(name));
568 columns = msi_alloc( column_count * sizeof(MSICOLUMNINFO) );
569 if (!columns)
570 return ERROR_FUNCTION_FAILED;
572 r = get_tablecolumns( db, name, columns, &column_count );
573 if (r != ERROR_SUCCESS)
575 msi_free( columns );
576 return ERROR_FUNCTION_FAILED;
578 *pcols = columns;
579 return r;
582 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
584 MSITABLE *table;
585 UINT r;
587 /* first, see if the table is cached */
588 table = find_cached_table( db, name );
589 if (table)
591 *table_ret = table;
592 return ERROR_SUCCESS;
595 /* nonexistent tables should be interpreted as empty tables */
596 table = msi_alloc( sizeof(MSITABLE) + lstrlenW( name ) * sizeof(WCHAR) );
597 if (!table)
598 return ERROR_FUNCTION_FAILED;
600 table->row_count = 0;
601 table->data = NULL;
602 table->data_persistent = NULL;
603 table->colinfo = NULL;
604 table->col_count = 0;
605 table->persistent = MSICONDITION_TRUE;
606 lstrcpyW( table->name, name );
608 if (!wcscmp( name, szTables ) || !wcscmp( name, szColumns ))
609 table->persistent = MSICONDITION_NONE;
611 r = table_get_column_info( db, name, &table->colinfo, &table->col_count );
612 if (r != ERROR_SUCCESS)
614 free_table( table );
615 return r;
617 r = read_table_from_storage( db, table, db->storage );
618 if (r != ERROR_SUCCESS)
620 free_table( table );
621 return r;
623 list_add_head( &db->tables, &table->entry );
624 *table_ret = table;
625 return ERROR_SUCCESS;
628 static UINT read_table_int( BYTE *const *data, UINT row, UINT col, UINT bytes )
630 UINT ret = 0, i;
632 for (i = 0; i < bytes; i++)
633 ret += data[row][col + i] << i * 8;
635 return ret;
638 static UINT get_tablecolumns( MSIDATABASE *db, LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz )
640 UINT r, i, n = 0, table_id, count, maxcount = *sz;
641 MSITABLE *table = NULL;
643 TRACE("%s\n", debugstr_w(szTableName));
645 /* first check if there is a default table with that name */
646 r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
647 if (r == ERROR_SUCCESS && *sz)
648 return r;
650 r = get_table( db, szColumns, &table );
651 if (r != ERROR_SUCCESS)
653 ERR("couldn't load _Columns table\n");
654 return ERROR_FUNCTION_FAILED;
657 /* convert table and column names to IDs from the string table */
658 r = msi_string2id( db->strings, szTableName, -1, &table_id );
659 if (r != ERROR_SUCCESS)
661 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
662 return r;
664 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
666 /* Note: _Columns table doesn't have non-persistent data */
668 /* if maxcount is non-zero, assume it's exactly right for this table */
669 if (colinfo) memset( colinfo, 0, maxcount * sizeof(*colinfo) );
670 count = table->row_count;
671 for (i = 0; i < count; i++)
673 if (read_table_int( table->data, i, 0, LONG_STR_BYTES) != table_id) continue;
674 if (colinfo)
676 UINT id = read_table_int( table->data, i, table->colinfo[2].offset, LONG_STR_BYTES );
677 UINT col = read_table_int( table->data, i, table->colinfo[1].offset, sizeof(USHORT) ) - (1 << 15);
679 /* check the column number is in range */
680 if (col < 1 || col > maxcount)
682 ERR("column %d out of range (maxcount: %d)\n", col, maxcount);
683 continue;
685 /* check if this column was already set */
686 if (colinfo[col - 1].number)
688 ERR("duplicate column %d\n", col);
689 continue;
691 colinfo[col - 1].tablename = msi_string_lookup( db->strings, table_id, NULL );
692 colinfo[col - 1].number = col;
693 colinfo[col - 1].colname = msi_string_lookup( db->strings, id, NULL );
694 colinfo[col - 1].type = read_table_int( table->data, i, table->colinfo[3].offset,
695 sizeof(USHORT) ) - (1 << 15);
696 colinfo[col - 1].offset = 0;
697 colinfo[col - 1].ref_count = 0;
698 colinfo[col - 1].hash_table = NULL;
700 n++;
702 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
704 if (colinfo && n != maxcount)
706 ERR("missing column in table %s\n", debugstr_w(szTableName));
707 msi_free_colinfo( colinfo, maxcount );
708 return ERROR_FUNCTION_FAILED;
710 table_calc_column_offsets( db, colinfo, n );
711 *sz = n;
712 return ERROR_SUCCESS;
715 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
716 MSICONDITION persistent )
718 UINT r, nField;
719 MSIVIEW *tv = NULL;
720 MSIRECORD *rec = NULL;
721 column_info *col;
722 MSITABLE *table;
723 UINT i;
725 /* only add tables that don't exist already */
726 if( TABLE_Exists(db, name ) )
728 WARN("table %s exists\n", debugstr_w(name));
729 return ERROR_BAD_QUERY_SYNTAX;
732 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
733 if( !table )
734 return ERROR_FUNCTION_FAILED;
736 table->ref_count = 1;
737 table->row_count = 0;
738 table->data = NULL;
739 table->data_persistent = NULL;
740 table->colinfo = NULL;
741 table->col_count = 0;
742 table->persistent = persistent;
743 lstrcpyW( table->name, name );
745 for( col = col_info; col; col = col->next )
746 table->col_count++;
748 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
749 if (!table->colinfo)
751 free_table( table );
752 return ERROR_FUNCTION_FAILED;
755 for( i = 0, col = col_info; col; i++, col = col->next )
757 UINT table_id = msi_add_string( db->strings, col->table, -1, persistent );
758 UINT col_id = msi_add_string( db->strings, col->column, -1, persistent );
760 table->colinfo[ i ].tablename = msi_string_lookup( db->strings, table_id, NULL );
761 table->colinfo[ i ].number = i + 1;
762 table->colinfo[ i ].colname = msi_string_lookup( db->strings, col_id, NULL );
763 table->colinfo[ i ].type = col->type;
764 table->colinfo[ i ].offset = 0;
765 table->colinfo[ i ].ref_count = 0;
766 table->colinfo[ i ].hash_table = NULL;
767 table->colinfo[ i ].temporary = col->temporary;
769 table_calc_column_offsets( db, table->colinfo, table->col_count);
771 r = TABLE_CreateView( db, szTables, &tv );
772 TRACE("CreateView returned %x\n", r);
773 if( r )
775 free_table( table );
776 return r;
779 r = tv->ops->execute( tv, 0 );
780 TRACE("tv execute returned %x\n", r);
781 if( r )
782 goto err;
784 rec = MSI_CreateRecord( 1 );
785 if( !rec )
786 goto err;
788 r = MSI_RecordSetStringW( rec, 1, name );
789 if( r )
790 goto err;
792 r = tv->ops->insert_row( tv, rec, -1, persistent == MSICONDITION_FALSE );
793 TRACE("insert_row returned %x\n", r);
794 if( r )
795 goto err;
797 tv->ops->delete( tv );
798 tv = NULL;
800 msiobj_release( &rec->hdr );
801 rec = NULL;
803 if( persistent != MSICONDITION_FALSE )
805 /* add each column to the _Columns table */
806 r = TABLE_CreateView( db, szColumns, &tv );
807 if( r )
808 goto err;
810 r = tv->ops->execute( tv, 0 );
811 TRACE("tv execute returned %x\n", r);
812 if( r )
813 goto err;
815 rec = MSI_CreateRecord( 4 );
816 if( !rec )
817 goto err;
819 r = MSI_RecordSetStringW( rec, 1, name );
820 if( r )
821 goto err;
824 * need to set the table, column number, col name and type
825 * for each column we enter in the table
827 nField = 1;
828 for( col = col_info; col; col = col->next )
830 r = MSI_RecordSetInteger( rec, 2, nField );
831 if( r )
832 goto err;
834 r = MSI_RecordSetStringW( rec, 3, col->column );
835 if( r )
836 goto err;
838 r = MSI_RecordSetInteger( rec, 4, col->type );
839 if( r )
840 goto err;
842 r = tv->ops->insert_row( tv, rec, -1, FALSE );
843 if( r )
844 goto err;
846 nField++;
848 if( !col )
849 r = ERROR_SUCCESS;
852 err:
853 if (rec)
854 msiobj_release( &rec->hdr );
855 /* FIXME: remove values from the string table on error */
856 if( tv )
857 tv->ops->delete( tv );
859 if (r == ERROR_SUCCESS)
860 list_add_head( &db->tables, &table->entry );
861 else
862 free_table( table );
864 return r;
867 static UINT save_table( MSIDATABASE *db, const MSITABLE *t, UINT bytes_per_strref )
869 BYTE *rawdata = NULL;
870 UINT rawsize, i, j, row_size, row_count;
871 UINT r = ERROR_FUNCTION_FAILED;
873 /* Nothing to do for non-persistent tables */
874 if( t->persistent == MSICONDITION_FALSE )
875 return ERROR_SUCCESS;
877 TRACE("Saving %s\n", debugstr_w( t->name ) );
879 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count, bytes_per_strref );
880 row_count = t->row_count;
881 for (i = 0; i < t->row_count; i++)
883 if (!t->data_persistent[i])
885 row_count = 1; /* yes, this is bizarre */
886 break;
889 rawsize = row_count * row_size;
890 rawdata = msi_alloc_zero( rawsize );
891 if( !rawdata )
893 r = ERROR_NOT_ENOUGH_MEMORY;
894 goto err;
897 rawsize = 0;
898 for (i = 0; i < row_count; i++)
900 UINT ofs = 0, ofs_mem = 0;
902 if (!t->data_persistent[i]) break;
904 for (j = 0; j < t->col_count; j++)
906 UINT m = bytes_per_column( db, &t->colinfo[j], LONG_STR_BYTES );
907 UINT n = bytes_per_column( db, &t->colinfo[j], bytes_per_strref );
908 UINT k;
910 if (n != 2 && n != 3 && n != 4)
912 ERR("oops - unknown column width %d\n", n);
913 goto err;
915 if (t->colinfo[j].type & MSITYPE_STRING && n < m)
917 UINT id = read_table_int( t->data, i, ofs_mem, LONG_STR_BYTES );
918 if (id > 1 << bytes_per_strref * 8)
920 ERR("string id %u out of range\n", id);
921 goto err;
924 for (k = 0; k < n; k++)
926 rawdata[ofs * row_count + i * n + k] = t->data[i][ofs_mem + k];
928 ofs_mem += m;
929 ofs += n;
931 rawsize += row_size;
934 TRACE("writing %d bytes\n", rawsize);
935 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
937 err:
938 msi_free( rawdata );
939 return r;
942 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
944 MSITABLE *table;
945 UINT size, offset, old_count;
946 UINT n;
948 if (!(table = find_cached_table( db, name ))) return;
949 old_count = table->col_count;
950 msi_free_colinfo( table->colinfo, table->col_count );
951 msi_free( table->colinfo );
952 table->colinfo = NULL;
954 table_get_column_info( db, name, &table->colinfo, &table->col_count );
955 if (!table->col_count) return;
957 size = msi_table_get_row_size( db, table->colinfo, table->col_count, LONG_STR_BYTES );
958 offset = table->colinfo[table->col_count - 1].offset;
960 for ( n = 0; n < table->row_count; n++ )
962 table->data[n] = msi_realloc( table->data[n], size );
963 if (old_count < table->col_count)
964 memset( &table->data[n][offset], 0, size - offset );
968 /* try to find the table name in the _Tables table */
969 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
971 UINT r, table_id, i;
972 MSITABLE *table;
974 if( !wcscmp( name, szTables ) || !wcscmp( name, szColumns ) ||
975 !wcscmp( name, szStreams ) || !wcscmp( name, szStorages ) )
976 return TRUE;
978 r = msi_string2id( db->strings, name, -1, &table_id );
979 if( r != ERROR_SUCCESS )
981 TRACE("Couldn't find id for %s\n", debugstr_w(name));
982 return FALSE;
985 r = get_table( db, szTables, &table );
986 if( r != ERROR_SUCCESS )
988 ERR("table %s not available\n", debugstr_w(szTables));
989 return FALSE;
992 for( i = 0; i < table->row_count; i++ )
994 if( read_table_int( table->data, i, 0, LONG_STR_BYTES ) == table_id )
995 return TRUE;
998 return FALSE;
1001 /* below is the query interface to a table */
1003 typedef struct tagMSITABLEVIEW
1005 MSIVIEW view;
1006 MSIDATABASE *db;
1007 MSITABLE *table;
1008 MSICOLUMNINFO *columns;
1009 UINT num_cols;
1010 UINT row_size;
1011 WCHAR name[1];
1012 } MSITABLEVIEW;
1014 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1016 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1017 UINT offset, n;
1019 if( !tv->table )
1020 return ERROR_INVALID_PARAMETER;
1022 if( (col==0) || (col>tv->num_cols) )
1023 return ERROR_INVALID_PARAMETER;
1025 /* how many rows are there ? */
1026 if( row >= tv->table->row_count )
1027 return ERROR_NO_MORE_ITEMS;
1029 if( tv->columns[col-1].offset >= tv->row_size )
1031 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1032 ERR("%p %p\n", tv, tv->columns );
1033 return ERROR_FUNCTION_FAILED;
1036 n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
1037 if (n != 2 && n != 3 && n != 4)
1039 ERR("oops! what is %d bytes per column?\n", n );
1040 return ERROR_FUNCTION_FAILED;
1043 offset = tv->columns[col-1].offset;
1044 *val = read_table_int(tv->table->data, row, offset, n);
1046 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1048 return ERROR_SUCCESS;
1051 static UINT get_stream_name( const MSITABLEVIEW *tv, UINT row, WCHAR **pstname )
1053 LPWSTR p, stname = NULL;
1054 UINT i, r, type, ival;
1055 DWORD len;
1056 LPCWSTR sval;
1057 MSIVIEW *view = (MSIVIEW *) tv;
1059 TRACE("%p %d\n", tv, row);
1061 len = lstrlenW( tv->name ) + 1;
1062 stname = msi_alloc( len*sizeof(WCHAR) );
1063 if ( !stname )
1065 r = ERROR_OUTOFMEMORY;
1066 goto err;
1069 lstrcpyW( stname, tv->name );
1071 for ( i = 0; i < tv->num_cols; i++ )
1073 type = tv->columns[i].type;
1074 if ( type & MSITYPE_KEY )
1076 WCHAR number[0x20];
1078 r = TABLE_fetch_int( view, row, i+1, &ival );
1079 if ( r != ERROR_SUCCESS )
1080 goto err;
1082 if ( tv->columns[i].type & MSITYPE_STRING )
1084 sval = msi_string_lookup( tv->db->strings, ival, NULL );
1085 if ( !sval )
1087 r = ERROR_INVALID_PARAMETER;
1088 goto err;
1091 else
1093 static const WCHAR fmt[] = { '%','d',0 };
1094 UINT n = bytes_per_column( tv->db, &tv->columns[i], LONG_STR_BYTES );
1096 switch( n )
1098 case 2:
1099 swprintf( number, ARRAY_SIZE(number), fmt, ival-0x8000 );
1100 break;
1101 case 4:
1102 swprintf( number, ARRAY_SIZE(number), fmt, ival^0x80000000 );
1103 break;
1104 default:
1105 ERR( "oops - unknown column width %d\n", n );
1106 r = ERROR_FUNCTION_FAILED;
1107 goto err;
1109 sval = number;
1112 len += lstrlenW( szDot ) + lstrlenW( sval );
1113 p = msi_realloc ( stname, len*sizeof(WCHAR) );
1114 if ( !p )
1116 r = ERROR_OUTOFMEMORY;
1117 goto err;
1119 stname = p;
1121 lstrcatW( stname, szDot );
1122 lstrcatW( stname, sval );
1124 else
1125 continue;
1128 *pstname = stname;
1129 return ERROR_SUCCESS;
1131 err:
1132 msi_free( stname );
1133 *pstname = NULL;
1134 return r;
1138 * We need a special case for streams, as we need to reference column with
1139 * the name of the stream in the same table, and the table name
1140 * which may not be available at higher levels of the query
1142 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1144 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1145 UINT r;
1146 WCHAR *name;
1148 if( !view->ops->fetch_int )
1149 return ERROR_INVALID_PARAMETER;
1151 r = get_stream_name( tv, row, &name );
1152 if (r != ERROR_SUCCESS)
1154 ERR("fetching stream, error = %u\n", r);
1155 return r;
1158 r = msi_get_stream( tv->db, name, stm );
1159 if (r != ERROR_SUCCESS)
1160 ERR("fetching stream %s, error = %u\n", debugstr_w(name), r);
1162 msi_free( name );
1163 return r;
1166 /* Set a table value, i.e. preadjusted integer or string ID. */
1167 static UINT table_set_bytes( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1169 UINT offset, n, i;
1171 if( !tv->table )
1172 return ERROR_INVALID_PARAMETER;
1174 if( (col==0) || (col>tv->num_cols) )
1175 return ERROR_INVALID_PARAMETER;
1177 if( row >= tv->table->row_count )
1178 return ERROR_INVALID_PARAMETER;
1180 if( tv->columns[col-1].offset >= tv->row_size )
1182 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1183 ERR("%p %p\n", tv, tv->columns );
1184 return ERROR_FUNCTION_FAILED;
1187 msi_free( tv->columns[col-1].hash_table );
1188 tv->columns[col-1].hash_table = NULL;
1190 n = bytes_per_column( tv->db, &tv->columns[col - 1], LONG_STR_BYTES );
1191 if ( n != 2 && n != 3 && n != 4 )
1193 ERR("oops! what is %d bytes per column?\n", n );
1194 return ERROR_FUNCTION_FAILED;
1197 offset = tv->columns[col-1].offset;
1198 for ( i = 0; i < n; i++ )
1199 tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
1201 return ERROR_SUCCESS;
1204 static UINT int_to_table_storage( const MSITABLEVIEW *tv, UINT col, int val, UINT *ret )
1206 if ((tv->columns[col-1].type & MSI_DATASIZEMASK) == 2)
1208 if (val == MSI_NULL_INTEGER)
1209 *ret = 0;
1210 else if ((val + 0x8000) & 0xffff0000)
1212 ERR("value %d out of range\n", val);
1213 return ERROR_FUNCTION_FAILED;
1215 else
1216 *ret = val + 0x8000;
1218 else
1219 *ret = val ^ 0x80000000;
1221 return ERROR_SUCCESS;
1224 static UINT TABLE_set_int( MSIVIEW *view, UINT row, UINT col, int val )
1226 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1227 UINT r, table_int;
1229 TRACE("row %u, col %u, val %d.\n", row, col, val);
1231 if ((r = int_to_table_storage( tv, col, val, &table_int )))
1232 return r;
1234 if (tv->columns[col-1].type & MSITYPE_KEY)
1236 UINT key;
1238 if ((r = TABLE_fetch_int( view, row, col, &key )))
1239 return r;
1240 if (key != table_int)
1242 ERR("Cannot modify primary key %s.%s.\n",
1243 debugstr_w(tv->table->name), debugstr_w(tv->columns[col-1].colname));
1244 return ERROR_FUNCTION_FAILED;
1248 return table_set_bytes( tv, row, col, table_int );
1251 static UINT TABLE_set_string( MSIVIEW *view, UINT row, UINT col, const WCHAR *val, int len )
1253 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1254 BOOL persistent;
1255 UINT id, r;
1257 TRACE("row %u, col %u, val %s.\n", row, col, debugstr_wn(val, len));
1259 persistent = (tv->table->persistent != MSICONDITION_FALSE)
1260 && tv->table->data_persistent[row];
1262 if (val)
1264 r = msi_string2id( tv->db->strings, val, len, &id );
1265 if (r != ERROR_SUCCESS)
1266 id = msi_add_string( tv->db->strings, val, len, persistent );
1268 else
1269 id = 0;
1271 if (tv->columns[col-1].type & MSITYPE_KEY)
1273 UINT key;
1275 if ((r = TABLE_fetch_int( view, row, col, &key )))
1276 return r;
1277 if (key != id)
1279 ERR("Cannot modify primary key %s.%s.\n",
1280 debugstr_w(tv->table->name), debugstr_w(tv->columns[col-1].colname));
1281 return ERROR_FUNCTION_FAILED;
1285 return table_set_bytes( tv, row, col, id );
1288 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1290 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1292 if (!tv->table)
1293 return ERROR_INVALID_PARAMETER;
1295 return msi_view_get_row(tv->db, view, row, rec);
1298 static UINT add_stream( MSIDATABASE *db, const WCHAR *name, IStream *data )
1300 static const WCHAR insert[] = {
1301 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1302 '`','_','S','t','r','e','a','m','s','`',' ',
1303 '(','`','N','a','m','e','`',',','`','D','a','t','a','`',')',' ',
1304 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1305 static const WCHAR update[] = {
1306 'U','P','D','A','T','E',' ','`','_','S','t','r','e','a','m','s','`',' ',
1307 'S','E','T',' ','`','D','a','t','a','`',' ','=',' ','?',' ',
1308 'W','H','E','R','E',' ','`','N','a','m','e','`',' ','=',' ','?',0};
1309 MSIQUERY *query;
1310 MSIRECORD *rec;
1311 UINT r;
1313 TRACE("%p %s %p\n", db, debugstr_w(name), data);
1315 if (!(rec = MSI_CreateRecord( 2 )))
1316 return ERROR_OUTOFMEMORY;
1318 r = MSI_RecordSetStringW( rec, 1, name );
1319 if (r != ERROR_SUCCESS)
1320 goto done;
1322 r = MSI_RecordSetIStream( rec, 2, data );
1323 if (r != ERROR_SUCCESS)
1324 goto done;
1326 r = MSI_DatabaseOpenViewW( db, insert, &query );
1327 if (r != ERROR_SUCCESS)
1328 goto done;
1330 r = MSI_ViewExecute( query, rec );
1331 msiobj_release( &query->hdr );
1332 if (r == ERROR_SUCCESS)
1333 goto done;
1335 msiobj_release( &rec->hdr );
1336 if (!(rec = MSI_CreateRecord( 2 )))
1337 return ERROR_OUTOFMEMORY;
1339 r = MSI_RecordSetIStream( rec, 1, data );
1340 if (r != ERROR_SUCCESS)
1341 goto done;
1343 r = MSI_RecordSetStringW( rec, 2, name );
1344 if (r != ERROR_SUCCESS)
1345 goto done;
1347 r = MSI_DatabaseOpenViewW( db, update, &query );
1348 if (r != ERROR_SUCCESS)
1349 goto done;
1351 r = MSI_ViewExecute( query, rec );
1352 msiobj_release( &query->hdr );
1354 done:
1355 msiobj_release( &rec->hdr );
1356 return r;
1359 static UINT TABLE_set_stream( MSIVIEW *view, UINT row, UINT col, IStream *stream )
1361 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1362 WCHAR *name;
1363 UINT r;
1365 TRACE("row %u, col %u, stream %p.\n", row, col, stream);
1367 if ((r = get_stream_name( tv, row - 1, &name )))
1368 return r;
1370 r = add_stream( tv->db, name, stream );
1371 msi_free( name );
1372 return r;
1375 static UINT get_table_value_from_record( MSITABLEVIEW *tv, MSIRECORD *rec, UINT iField, UINT *pvalue )
1377 MSICOLUMNINFO columninfo;
1378 UINT r;
1380 if (!iField || iField > tv->num_cols || MSI_RecordIsNull( rec, iField ))
1381 return ERROR_FUNCTION_FAILED;
1383 columninfo = tv->columns[ iField - 1 ];
1385 if ( MSITYPE_IS_BINARY(columninfo.type) )
1387 *pvalue = 1; /* refers to the first key column */
1389 else if ( columninfo.type & MSITYPE_STRING )
1391 int len;
1392 const WCHAR *sval = msi_record_get_string( rec, iField, &len );
1393 if (sval)
1395 r = msi_string2id( tv->db->strings, sval, len, pvalue );
1396 if (r != ERROR_SUCCESS)
1397 return ERROR_NOT_FOUND;
1399 else *pvalue = 0;
1401 else
1402 return int_to_table_storage( tv, iField, MSI_RecordGetInteger( rec, iField ), pvalue );
1404 return ERROR_SUCCESS;
1407 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1409 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1410 UINT i, val, r = ERROR_SUCCESS;
1412 if ( !tv->table )
1413 return ERROR_INVALID_PARAMETER;
1415 /* test if any of the mask bits are invalid */
1416 if ( mask >= (1<<tv->num_cols) )
1417 return ERROR_INVALID_PARAMETER;
1419 for ( i = 0; i < tv->num_cols; i++ )
1421 BOOL persistent;
1423 /* only update the fields specified in the mask */
1424 if ( !(mask&(1<<i)) )
1425 continue;
1427 persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1428 (tv->table->data_persistent[row]);
1429 /* FIXME: should we allow updating keys? */
1431 val = 0;
1432 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1434 r = get_table_value_from_record (tv, rec, i + 1, &val);
1435 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1437 IStream *stm;
1438 LPWSTR stname;
1440 if ( r != ERROR_SUCCESS )
1441 return ERROR_FUNCTION_FAILED;
1443 r = MSI_RecordGetIStream( rec, i + 1, &stm );
1444 if ( r != ERROR_SUCCESS )
1445 return r;
1447 r = get_stream_name( tv, row, &stname );
1448 if ( r != ERROR_SUCCESS )
1450 IStream_Release( stm );
1451 return r;
1454 r = add_stream( tv->db, stname, stm );
1455 IStream_Release( stm );
1456 msi_free ( stname );
1458 if ( r != ERROR_SUCCESS )
1459 return r;
1461 else if ( tv->columns[i].type & MSITYPE_STRING )
1463 UINT x;
1465 if ( r != ERROR_SUCCESS )
1467 int len;
1468 const WCHAR *sval = msi_record_get_string( rec, i + 1, &len );
1469 val = msi_add_string( tv->db->strings, sval, len, persistent );
1471 else
1473 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1474 if (val == x)
1475 continue;
1478 else
1480 if ( r != ERROR_SUCCESS )
1481 return ERROR_FUNCTION_FAILED;
1485 r = table_set_bytes( tv, row, i+1, val );
1486 if ( r != ERROR_SUCCESS )
1487 break;
1489 return r;
1492 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1494 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1495 BYTE **p, *row;
1496 BOOL *b;
1497 UINT sz;
1498 BYTE ***data_ptr;
1499 BOOL **data_persist_ptr;
1500 UINT *row_count;
1502 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1504 if( !tv->table )
1505 return ERROR_INVALID_PARAMETER;
1507 row = msi_alloc_zero( tv->row_size );
1508 if( !row )
1509 return ERROR_NOT_ENOUGH_MEMORY;
1511 row_count = &tv->table->row_count;
1512 data_ptr = &tv->table->data;
1513 data_persist_ptr = &tv->table->data_persistent;
1514 if (*num == -1)
1515 *num = tv->table->row_count;
1517 sz = (*row_count + 1) * sizeof (BYTE*);
1518 if( *data_ptr )
1519 p = msi_realloc( *data_ptr, sz );
1520 else
1521 p = msi_alloc( sz );
1522 if( !p )
1524 msi_free( row );
1525 return ERROR_NOT_ENOUGH_MEMORY;
1528 sz = (*row_count + 1) * sizeof (BOOL);
1529 if( *data_persist_ptr )
1530 b = msi_realloc( *data_persist_ptr, sz );
1531 else
1532 b = msi_alloc( sz );
1533 if( !b )
1535 msi_free( row );
1536 msi_free( p );
1537 return ERROR_NOT_ENOUGH_MEMORY;
1540 *data_ptr = p;
1541 (*data_ptr)[*row_count] = row;
1543 *data_persist_ptr = b;
1544 (*data_persist_ptr)[*row_count] = !temporary;
1546 (*row_count)++;
1548 return ERROR_SUCCESS;
1551 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1553 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1555 TRACE("%p %p\n", tv, record);
1557 TRACE("There are %d columns\n", tv->num_cols );
1559 return ERROR_SUCCESS;
1562 static UINT TABLE_close( struct tagMSIVIEW *view )
1564 TRACE("%p\n", view );
1566 return ERROR_SUCCESS;
1569 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1571 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1573 TRACE("%p %p %p\n", view, rows, cols );
1575 if( cols )
1576 *cols = tv->num_cols;
1577 if( rows )
1579 if( !tv->table )
1580 return ERROR_INVALID_PARAMETER;
1581 *rows = tv->table->row_count;
1584 return ERROR_SUCCESS;
1587 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1588 UINT n, LPCWSTR *name, UINT *type, BOOL *temporary,
1589 LPCWSTR *table_name )
1591 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1593 TRACE("%p %d %p %p\n", tv, n, name, type );
1595 if( ( n == 0 ) || ( n > tv->num_cols ) )
1596 return ERROR_INVALID_PARAMETER;
1598 if( name )
1600 *name = tv->columns[n-1].colname;
1601 if( !*name )
1602 return ERROR_FUNCTION_FAILED;
1605 if( table_name )
1607 *table_name = tv->columns[n-1].tablename;
1608 if( !*table_name )
1609 return ERROR_FUNCTION_FAILED;
1612 if( type )
1613 *type = tv->columns[n-1].type;
1615 if( temporary )
1616 *temporary = tv->columns[n-1].temporary;
1618 return ERROR_SUCCESS;
1621 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row, UINT *column );
1623 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *column )
1625 UINT r, row, i;
1627 /* check there are no null values where they're not allowed */
1628 for( i = 0; i < tv->num_cols; i++ )
1630 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1631 continue;
1633 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1634 TRACE("skipping binary column\n");
1635 else if ( tv->columns[i].type & MSITYPE_STRING )
1637 int len;
1638 const WCHAR *str = msi_record_get_string( rec, i+1, &len );
1640 if (!str || (!str[0] && !len))
1642 if (column) *column = i;
1643 return ERROR_INVALID_DATA;
1646 else
1648 UINT n;
1650 n = MSI_RecordGetInteger( rec, i+1 );
1651 if (n == MSI_NULL_INTEGER)
1653 if (column) *column = i;
1654 return ERROR_INVALID_DATA;
1659 /* check there are no duplicate keys */
1660 r = msi_table_find_row( tv, rec, &row, column );
1661 if (r == ERROR_SUCCESS)
1662 return ERROR_FUNCTION_FAILED;
1664 return ERROR_SUCCESS;
1667 static int compare_record( MSITABLEVIEW *tv, UINT row, MSIRECORD *rec )
1669 UINT r, i, ivalue, x;
1671 for (i = 0; i < tv->num_cols; i++ )
1673 if (!(tv->columns[i].type & MSITYPE_KEY)) continue;
1675 r = get_table_value_from_record( tv, rec, i + 1, &ivalue );
1676 if (r != ERROR_SUCCESS)
1677 return 1;
1679 r = TABLE_fetch_int( &tv->view, row, i + 1, &x );
1680 if (r != ERROR_SUCCESS)
1682 WARN("TABLE_fetch_int should not fail here %u\n", r);
1683 return -1;
1685 if (ivalue > x)
1687 return 1;
1689 else if (ivalue == x)
1691 if (i < tv->num_cols - 1) continue;
1692 return 0;
1694 else
1695 return -1;
1697 return 1;
1700 static int find_insert_index( MSITABLEVIEW *tv, MSIRECORD *rec )
1702 int idx, c, low = 0, high = tv->table->row_count - 1;
1704 TRACE("%p %p\n", tv, rec);
1706 while (low <= high)
1708 idx = (low + high) / 2;
1709 c = compare_record( tv, idx, rec );
1711 if (c < 0)
1712 high = idx - 1;
1713 else if (c > 0)
1714 low = idx + 1;
1715 else
1717 TRACE("found %u\n", idx);
1718 return idx;
1721 TRACE("found %u\n", high + 1);
1722 return high + 1;
1725 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1727 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1728 UINT i, r;
1730 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1732 /* check that the key is unique - can we find a matching row? */
1733 r = table_validate_new( tv, rec, NULL );
1734 if( r != ERROR_SUCCESS )
1735 return ERROR_FUNCTION_FAILED;
1737 if (row == -1)
1738 row = find_insert_index( tv, rec );
1740 r = table_create_new_row( view, &row, temporary );
1741 TRACE("insert_row returned %08x\n", r);
1742 if( r != ERROR_SUCCESS )
1743 return r;
1745 /* shift the rows to make room for the new row */
1746 for (i = tv->table->row_count - 1; i > row; i--)
1748 memmove(&(tv->table->data[i][0]),
1749 &(tv->table->data[i - 1][0]), tv->row_size);
1750 tv->table->data_persistent[i] = tv->table->data_persistent[i - 1];
1753 /* Re-set the persistence flag */
1754 tv->table->data_persistent[row] = !temporary;
1755 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1758 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1760 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1761 UINT r, num_rows, num_cols, i;
1763 TRACE("%p %d\n", tv, row);
1765 if ( !tv->table )
1766 return ERROR_INVALID_PARAMETER;
1768 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1769 if ( r != ERROR_SUCCESS )
1770 return r;
1772 if ( row >= num_rows )
1773 return ERROR_FUNCTION_FAILED;
1775 num_rows = tv->table->row_count;
1776 tv->table->row_count--;
1778 /* reset the hash tables */
1779 for (i = 0; i < tv->num_cols; i++)
1781 msi_free( tv->columns[i].hash_table );
1782 tv->columns[i].hash_table = NULL;
1785 for (i = row + 1; i < num_rows; i++)
1787 memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
1788 tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
1791 msi_free(tv->table->data[num_rows - 1]);
1793 return ERROR_SUCCESS;
1796 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1798 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1799 UINT r, new_row;
1801 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1802 * sets the fetched record apart from other records
1805 if (!tv->table)
1806 return ERROR_INVALID_PARAMETER;
1808 r = msi_table_find_row(tv, rec, &new_row, NULL);
1809 if (r != ERROR_SUCCESS)
1811 ERR("can't find row to modify\n");
1812 return ERROR_FUNCTION_FAILED;
1815 /* the row cannot be changed */
1816 if (row != new_row)
1817 return ERROR_FUNCTION_FAILED;
1819 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1822 static UINT msi_table_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
1824 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1825 UINT r, row;
1827 if (!tv->table)
1828 return ERROR_INVALID_PARAMETER;
1830 r = msi_table_find_row(tv, rec, &row, NULL);
1831 if (r == ERROR_SUCCESS)
1832 return TABLE_set_row(view, row, rec, (1 << tv->num_cols) - 1);
1833 else
1834 return TABLE_insert_row( view, rec, -1, FALSE );
1837 static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1839 MSIRECORD *curr;
1840 UINT r, i, count;
1842 r = TABLE_get_row(view, row, &curr);
1843 if (r != ERROR_SUCCESS)
1844 return r;
1846 /* Close the original record */
1847 MSI_CloseRecord(&rec->hdr);
1849 count = MSI_RecordGetFieldCount(rec);
1850 for (i = 0; i < count; i++)
1851 MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1853 msiobj_release(&curr->hdr);
1854 return ERROR_SUCCESS;
1857 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1858 MSIRECORD *rec, UINT row)
1860 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1861 UINT r, frow, column;
1863 TRACE("%p %d %p\n", view, eModifyMode, rec );
1865 switch (eModifyMode)
1867 case MSIMODIFY_DELETE:
1868 r = TABLE_delete_row( view, row );
1869 break;
1870 case MSIMODIFY_VALIDATE_NEW:
1871 r = table_validate_new( tv, rec, &column );
1872 if (r != ERROR_SUCCESS)
1874 tv->view.error = MSIDBERROR_DUPLICATEKEY;
1875 tv->view.error_column = tv->columns[column].colname;
1876 r = ERROR_INVALID_DATA;
1878 break;
1880 case MSIMODIFY_INSERT:
1881 r = table_validate_new( tv, rec, NULL );
1882 if (r != ERROR_SUCCESS)
1883 break;
1884 r = TABLE_insert_row( view, rec, -1, FALSE );
1885 break;
1887 case MSIMODIFY_INSERT_TEMPORARY:
1888 r = table_validate_new( tv, rec, NULL );
1889 if (r != ERROR_SUCCESS)
1890 break;
1891 r = TABLE_insert_row( view, rec, -1, TRUE );
1892 break;
1894 case MSIMODIFY_REFRESH:
1895 r = msi_refresh_record( view, rec, row );
1896 break;
1898 case MSIMODIFY_UPDATE:
1899 r = msi_table_update( view, rec, row );
1900 break;
1902 case MSIMODIFY_ASSIGN:
1903 r = msi_table_assign( view, rec );
1904 break;
1906 case MSIMODIFY_MERGE:
1907 /* check row that matches this record */
1908 r = msi_table_find_row( tv, rec, &frow, &column );
1909 if (r != ERROR_SUCCESS)
1911 r = table_validate_new( tv, rec, NULL );
1912 if (r == ERROR_SUCCESS)
1913 r = TABLE_insert_row( view, rec, -1, FALSE );
1915 break;
1917 case MSIMODIFY_REPLACE:
1918 case MSIMODIFY_VALIDATE:
1919 case MSIMODIFY_VALIDATE_FIELD:
1920 case MSIMODIFY_VALIDATE_DELETE:
1921 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1922 r = ERROR_CALL_NOT_IMPLEMENTED;
1923 break;
1925 default:
1926 r = ERROR_INVALID_DATA;
1929 return r;
1932 static UINT TABLE_delete( struct tagMSIVIEW *view )
1934 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1936 TRACE("%p\n", view );
1938 tv->table = NULL;
1939 tv->columns = NULL;
1941 msi_free( tv );
1943 return ERROR_SUCCESS;
1946 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1948 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1949 UINT i;
1951 TRACE("%p %d\n", view, tv->table->ref_count);
1953 for (i = 0; i < tv->table->col_count; i++)
1955 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1956 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
1959 return InterlockedIncrement(&tv->table->ref_count);
1962 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
1964 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1965 MSIRECORD *rec = NULL;
1966 MSIVIEW *columns = NULL;
1967 UINT row, r;
1969 rec = MSI_CreateRecord(2);
1970 if (!rec)
1971 return ERROR_OUTOFMEMORY;
1973 MSI_RecordSetStringW(rec, 1, table);
1974 MSI_RecordSetInteger(rec, 2, number);
1976 r = TABLE_CreateView(tv->db, szColumns, &columns);
1977 if (r != ERROR_SUCCESS)
1979 msiobj_release(&rec->hdr);
1980 return r;
1983 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row, NULL);
1984 if (r != ERROR_SUCCESS)
1985 goto done;
1987 r = TABLE_delete_row(columns, row);
1988 if (r != ERROR_SUCCESS)
1989 goto done;
1991 msi_update_table_columns(tv->db, table);
1993 done:
1994 msiobj_release(&rec->hdr);
1995 columns->ops->delete(columns);
1996 return r;
1999 static UINT TABLE_release(struct tagMSIVIEW *view)
2001 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2002 INT ref = tv->table->ref_count;
2003 UINT i, r;
2005 TRACE("%p %d\n", view, ref);
2007 for (i = 0; i < tv->table->col_count; i++)
2009 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
2011 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
2012 if (ref == 0)
2014 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2015 tv->table->colinfo[i].number);
2016 if (r != ERROR_SUCCESS)
2017 break;
2022 ref = InterlockedDecrement(&tv->table->ref_count);
2023 if (ref == 0)
2025 if (!tv->table->row_count)
2027 list_remove(&tv->table->entry);
2028 free_table(tv->table);
2029 TABLE_delete(view);
2033 return ref;
2036 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
2037 LPCWSTR column, UINT type, BOOL hold)
2039 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2040 MSITABLE *msitable;
2041 MSIRECORD *rec;
2042 UINT r, i;
2044 rec = MSI_CreateRecord(4);
2045 if (!rec)
2046 return ERROR_OUTOFMEMORY;
2048 MSI_RecordSetStringW(rec, 1, table);
2049 MSI_RecordSetInteger(rec, 2, number);
2050 MSI_RecordSetStringW(rec, 3, column);
2051 MSI_RecordSetInteger(rec, 4, type);
2053 r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
2054 if (r != ERROR_SUCCESS)
2055 goto done;
2057 msi_update_table_columns(tv->db, table);
2059 if (!hold)
2060 goto done;
2062 msitable = find_cached_table(tv->db, table);
2063 for (i = 0; i < msitable->col_count; i++)
2065 if (!wcscmp( msitable->colinfo[i].colname, column ))
2067 InterlockedIncrement(&msitable->colinfo[i].ref_count);
2068 break;
2072 done:
2073 msiobj_release(&rec->hdr);
2074 return r;
2077 static UINT TABLE_drop(struct tagMSIVIEW *view)
2079 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2080 MSIVIEW *tables = NULL;
2081 MSIRECORD *rec = NULL;
2082 UINT r, row;
2083 INT i;
2085 TRACE("dropping table %s\n", debugstr_w(tv->name));
2087 for (i = tv->table->col_count - 1; i >= 0; i--)
2089 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2090 tv->table->colinfo[i].number);
2091 if (r != ERROR_SUCCESS)
2092 return r;
2095 rec = MSI_CreateRecord(1);
2096 if (!rec)
2097 return ERROR_OUTOFMEMORY;
2099 MSI_RecordSetStringW(rec, 1, tv->name);
2101 r = TABLE_CreateView(tv->db, szTables, &tables);
2102 if (r != ERROR_SUCCESS)
2104 msiobj_release(&rec->hdr);
2105 return r;
2108 r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row, NULL);
2109 if (r != ERROR_SUCCESS)
2110 goto done;
2112 r = TABLE_delete_row(tables, row);
2113 if (r != ERROR_SUCCESS)
2114 goto done;
2116 list_remove(&tv->table->entry);
2117 free_table(tv->table);
2119 done:
2120 msiobj_release(&rec->hdr);
2121 tables->ops->delete(tables);
2123 return r;
2126 static const MSIVIEWOPS table_ops =
2128 TABLE_fetch_int,
2129 TABLE_fetch_stream,
2130 TABLE_set_int,
2131 TABLE_set_string,
2132 TABLE_set_stream,
2133 TABLE_set_row,
2134 TABLE_insert_row,
2135 TABLE_delete_row,
2136 TABLE_execute,
2137 TABLE_close,
2138 TABLE_get_dimensions,
2139 TABLE_get_column_info,
2140 TABLE_modify,
2141 TABLE_delete,
2142 TABLE_add_ref,
2143 TABLE_release,
2144 TABLE_add_column,
2145 NULL,
2146 TABLE_drop,
2149 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2151 MSITABLEVIEW *tv ;
2152 UINT r, sz;
2154 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2156 if ( !wcscmp( name, szStreams ) )
2157 return STREAMS_CreateView( db, view );
2158 else if ( !wcscmp( name, szStorages ) )
2159 return STORAGES_CreateView( db, view );
2161 sz = FIELD_OFFSET( MSITABLEVIEW, name[lstrlenW( name ) + 1] );
2162 tv = msi_alloc_zero( sz );
2163 if( !tv )
2164 return ERROR_FUNCTION_FAILED;
2166 r = get_table( db, name, &tv->table );
2167 if( r != ERROR_SUCCESS )
2169 msi_free( tv );
2170 WARN("table not found\n");
2171 return r;
2174 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2176 /* fill the structure */
2177 tv->view.ops = &table_ops;
2178 tv->db = db;
2179 tv->columns = tv->table->colinfo;
2180 tv->num_cols = tv->table->col_count;
2181 tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count, LONG_STR_BYTES );
2183 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2185 *view = (MSIVIEW*) tv;
2186 lstrcpyW( tv->name, name );
2188 return ERROR_SUCCESS;
2191 UINT MSI_CommitTables( MSIDATABASE *db )
2193 UINT r, bytes_per_strref;
2194 HRESULT hr;
2195 MSITABLE *table = NULL;
2197 TRACE("%p\n",db);
2199 r = msi_save_string_table( db->strings, db->storage, &bytes_per_strref );
2200 if( r != ERROR_SUCCESS )
2202 WARN("failed to save string table r=%08x\n",r);
2203 return r;
2206 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2208 r = save_table( db, table, bytes_per_strref );
2209 if( r != ERROR_SUCCESS )
2211 WARN("failed to save table %s (r=%08x)\n",
2212 debugstr_w(table->name), r);
2213 return r;
2217 hr = IStorage_Commit( db->storage, 0 );
2218 if (FAILED( hr ))
2220 WARN("failed to commit changes 0x%08x\n", hr);
2221 r = ERROR_FUNCTION_FAILED;
2223 return r;
2226 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2228 MSITABLE *t;
2229 UINT r;
2231 TRACE("%p %s\n", db, debugstr_w(table));
2233 if (!table)
2234 return MSICONDITION_ERROR;
2236 r = get_table( db, table, &t );
2237 if (r != ERROR_SUCCESS)
2238 return MSICONDITION_NONE;
2240 return t->persistent;
2243 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2245 UINT ret = 0, i;
2247 for (i = 0; i < bytes; i++)
2248 ret += (data[col + i] << i * 8);
2250 return ret;
2253 static UINT msi_record_encoded_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR *pstname )
2255 LPWSTR stname = NULL, sval, p;
2256 DWORD len;
2257 UINT i, r;
2259 TRACE("%p %p\n", tv, rec);
2261 len = lstrlenW( tv->name ) + 1;
2262 stname = msi_alloc( len*sizeof(WCHAR) );
2263 if ( !stname )
2265 r = ERROR_OUTOFMEMORY;
2266 goto err;
2269 lstrcpyW( stname, tv->name );
2271 for ( i = 0; i < tv->num_cols; i++ )
2273 if ( tv->columns[i].type & MSITYPE_KEY )
2275 sval = msi_dup_record_field( rec, i + 1 );
2276 if ( !sval )
2278 r = ERROR_OUTOFMEMORY;
2279 goto err;
2282 len += lstrlenW( szDot ) + lstrlenW ( sval );
2283 p = msi_realloc ( stname, len*sizeof(WCHAR) );
2284 if ( !p )
2286 r = ERROR_OUTOFMEMORY;
2287 msi_free(sval);
2288 goto err;
2290 stname = p;
2292 lstrcatW( stname, szDot );
2293 lstrcatW( stname, sval );
2295 msi_free( sval );
2297 else
2298 continue;
2301 *pstname = encode_streamname( FALSE, stname );
2302 msi_free( stname );
2304 return ERROR_SUCCESS;
2306 err:
2307 msi_free ( stname );
2308 *pstname = NULL;
2309 return r;
2312 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2313 IStorage *stg, const BYTE *rawdata, UINT bytes_per_strref )
2315 UINT i, val, ofs = 0;
2316 USHORT mask;
2317 MSICOLUMNINFO *columns = tv->columns;
2318 MSIRECORD *rec;
2320 mask = rawdata[0] | (rawdata[1] << 8);
2321 rawdata += 2;
2323 rec = MSI_CreateRecord( tv->num_cols );
2324 if( !rec )
2325 return rec;
2327 TRACE("row ->\n");
2328 for( i=0; i<tv->num_cols; i++ )
2330 if ( (mask&1) && (i>=(mask>>8)) )
2331 break;
2332 /* all keys must be present */
2333 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2334 continue;
2336 if( MSITYPE_IS_BINARY(tv->columns[i].type) )
2338 LPWSTR encname;
2339 IStream *stm = NULL;
2340 UINT r;
2342 ofs += bytes_per_column( tv->db, &columns[i], bytes_per_strref );
2344 r = msi_record_encoded_stream_name( tv, rec, &encname );
2345 if ( r != ERROR_SUCCESS )
2347 msiobj_release( &rec->hdr );
2348 return NULL;
2350 r = IStorage_OpenStream( stg, encname, NULL, STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm );
2351 if ( r != ERROR_SUCCESS )
2353 msiobj_release( &rec->hdr );
2354 msi_free( encname );
2355 return NULL;
2358 MSI_RecordSetStream( rec, i+1, stm );
2359 TRACE(" field %d [%s]\n", i+1, debugstr_w(encname));
2360 msi_free( encname );
2362 else if( columns[i].type & MSITYPE_STRING )
2364 int len;
2365 const WCHAR *sval;
2367 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2368 sval = msi_string_lookup( st, val, &len );
2369 msi_record_set_string( rec, i+1, sval, len );
2370 TRACE(" field %d [%s]\n", i+1, debugstr_wn(sval, len));
2371 ofs += bytes_per_strref;
2373 else
2375 UINT n = bytes_per_column( tv->db, &columns[i], bytes_per_strref );
2376 switch( n )
2378 case 2:
2379 val = read_raw_int(rawdata, ofs, n);
2380 if (val)
2381 MSI_RecordSetInteger( rec, i+1, val-0x8000 );
2382 TRACE(" field %d [0x%04x]\n", i+1, val );
2383 break;
2384 case 4:
2385 val = read_raw_int(rawdata, ofs, n);
2386 if (val)
2387 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2388 TRACE(" field %d [0x%08x]\n", i+1, val );
2389 break;
2390 default:
2391 ERR("oops - unknown column width %d\n", n);
2392 break;
2394 ofs += n;
2397 return rec;
2400 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2402 UINT i;
2403 for (i = 0; i < rawsize / 2; i++)
2405 int len;
2406 const WCHAR *sval = msi_string_lookup( st, rawdata[i], &len );
2407 MESSAGE(" %04x %s\n", rawdata[i], debugstr_wn(sval, len) );
2411 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2413 UINT i, r, *data;
2415 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2416 for( i=0; i<tv->num_cols; i++ )
2418 data[i] = 0;
2420 if ( ~tv->columns[i].type & MSITYPE_KEY )
2421 continue;
2423 /* turn the transform column value into a row value */
2424 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2425 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2427 int len;
2428 const WCHAR *str = msi_record_get_string( rec, i+1, &len );
2429 if (str)
2431 r = msi_string2id( tv->db->strings, str, len, &data[i] );
2433 /* if there's no matching string in the string table,
2434 these keys can't match any record, so fail now. */
2435 if (r != ERROR_SUCCESS)
2437 msi_free( data );
2438 return NULL;
2441 else data[i] = 0;
2443 else
2445 if (int_to_table_storage( tv, i + 1, MSI_RecordGetInteger( rec, i + 1 ), &data[i] ))
2447 msi_free( data );
2448 return NULL;
2452 return data;
2455 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data, UINT *column )
2457 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2459 for( i=0; i<tv->num_cols; i++ )
2461 if ( ~tv->columns[i].type & MSITYPE_KEY )
2462 continue;
2464 /* turn the transform column value into a row value */
2465 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2466 if ( r != ERROR_SUCCESS )
2468 ERR("TABLE_fetch_int shouldn't fail here\n");
2469 break;
2472 /* if this key matches, move to the next column */
2473 if ( x != data[i] )
2475 ret = ERROR_FUNCTION_FAILED;
2476 break;
2478 if (column) *column = i;
2479 ret = ERROR_SUCCESS;
2481 return ret;
2484 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row, UINT *column )
2486 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2488 data = msi_record_to_row( tv, rec );
2489 if( !data )
2490 return r;
2491 for( i = 0; i < tv->table->row_count; i++ )
2493 r = msi_row_matches( tv, i, data, column );
2494 if( r == ERROR_SUCCESS )
2496 *row = i;
2497 break;
2500 msi_free( data );
2501 return r;
2504 typedef struct
2506 struct list entry;
2507 LPWSTR name;
2508 } TRANSFORMDATA;
2510 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2511 string_table *st, TRANSFORMDATA *transform,
2512 UINT bytes_per_strref )
2514 BYTE *rawdata = NULL;
2515 MSITABLEVIEW *tv = NULL;
2516 UINT r, n, sz, i, mask, num_cols, colcol = 0, rawsize = 0;
2517 MSIRECORD *rec = NULL;
2518 WCHAR coltable[32];
2519 const WCHAR *name;
2521 if (!transform)
2522 return ERROR_SUCCESS;
2524 name = transform->name;
2526 coltable[0] = 0;
2527 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2529 /* read the transform data */
2530 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2531 if ( !rawdata )
2533 TRACE("table %s empty\n", debugstr_w(name) );
2534 return ERROR_INVALID_TABLE;
2537 /* create a table view */
2538 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2539 if( r != ERROR_SUCCESS )
2540 goto err;
2542 r = tv->view.ops->execute( &tv->view, NULL );
2543 if( r != ERROR_SUCCESS )
2544 goto err;
2546 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2547 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2549 /* interpret the data */
2550 for (n = 0; n < rawsize;)
2552 mask = rawdata[n] | (rawdata[n + 1] << 8);
2553 if (mask & 1)
2556 * if the low bit is set, columns are continuous and
2557 * the number of columns is specified in the high byte
2559 sz = 2;
2560 num_cols = mask >> 8;
2561 if (num_cols > tv->num_cols)
2563 ERR("excess columns in transform: %u > %u\n", num_cols, tv->num_cols);
2564 break;
2567 for (i = 0; i < num_cols; i++)
2569 if( (tv->columns[i].type & MSITYPE_STRING) &&
2570 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2571 sz += bytes_per_strref;
2572 else
2573 sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
2576 else
2579 * If the low bit is not set, mask is a bitmask.
2580 * Excepting for key fields, which are always present,
2581 * each bit indicates that a field is present in the transform record.
2583 * mask == 0 is a special case ... only the keys will be present
2584 * and it means that this row should be deleted.
2586 sz = 2;
2587 num_cols = tv->num_cols;
2588 for (i = 0; i < num_cols; i++)
2590 if ((tv->columns[i].type & MSITYPE_KEY) || ((1 << i) & mask))
2592 if ((tv->columns[i].type & MSITYPE_STRING) &&
2593 !MSITYPE_IS_BINARY(tv->columns[i].type))
2594 sz += bytes_per_strref;
2595 else
2596 sz += bytes_per_column( tv->db, &tv->columns[i], bytes_per_strref );
2601 /* check we didn't run of the end of the table */
2602 if (n + sz > rawsize)
2604 ERR("borked.\n");
2605 dump_table( st, (USHORT *)rawdata, rawsize );
2606 break;
2609 rec = msi_get_transform_record( tv, st, stg, &rawdata[n], bytes_per_strref );
2610 if (rec)
2612 WCHAR table[32];
2613 DWORD sz = 32;
2614 UINT number = MSI_NULL_INTEGER;
2615 UINT row = 0;
2617 if (!wcscmp( name, szColumns ))
2619 MSI_RecordGetStringW( rec, 1, table, &sz );
2620 number = MSI_RecordGetInteger( rec, 2 );
2623 * Native msi seems writes nul into the Number (2nd) column of
2624 * the _Columns table when there are new columns
2626 if ( number == MSI_NULL_INTEGER )
2628 /* reset the column number on a new table */
2629 if (wcscmp( coltable, table ))
2631 colcol = 0;
2632 lstrcpyW( coltable, table );
2635 /* fix nul column numbers */
2636 MSI_RecordSetInteger( rec, 2, ++colcol );
2640 if (TRACE_ON(msidb)) dump_record( rec );
2642 r = msi_table_find_row( tv, rec, &row, NULL );
2643 if (r == ERROR_SUCCESS)
2645 if (!mask)
2647 TRACE("deleting row [%d]:\n", row);
2648 r = TABLE_delete_row( &tv->view, row );
2649 if (r != ERROR_SUCCESS)
2650 WARN("failed to delete row %u\n", r);
2652 else if (mask & 1)
2654 TRACE("modifying full row [%d]:\n", row);
2655 r = TABLE_set_row( &tv->view, row, rec, (1 << tv->num_cols) - 1 );
2656 if (r != ERROR_SUCCESS)
2657 WARN("failed to modify row %u\n", r);
2659 else
2661 TRACE("modifying masked row [%d]:\n", row);
2662 r = TABLE_set_row( &tv->view, row, rec, mask );
2663 if (r != ERROR_SUCCESS)
2664 WARN("failed to modify row %u\n", r);
2667 else
2669 TRACE("inserting row\n");
2670 r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
2671 if (r != ERROR_SUCCESS)
2672 WARN("failed to insert row %u\n", r);
2675 if (!wcscmp( name, szColumns ))
2676 msi_update_table_columns( db, table );
2678 msiobj_release( &rec->hdr );
2681 n += sz;
2684 err:
2685 /* no need to free the table, it's associated with the database */
2686 msi_free( rawdata );
2687 if( tv )
2688 tv->view.ops->delete( &tv->view );
2690 return ERROR_SUCCESS;
2694 * msi_table_apply_transform
2696 * Enumerate the table transforms in a transform storage and apply each one.
2698 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2700 struct list transforms;
2701 IEnumSTATSTG *stgenum = NULL;
2702 TRANSFORMDATA *transform;
2703 TRANSFORMDATA *tables = NULL, *columns = NULL;
2704 HRESULT hr;
2705 STATSTG stat;
2706 string_table *strings;
2707 UINT ret = ERROR_FUNCTION_FAILED;
2708 UINT bytes_per_strref;
2709 BOOL property_update = FALSE;
2711 TRACE("%p %p\n", db, stg );
2713 strings = msi_load_string_table( stg, &bytes_per_strref );
2714 if( !strings )
2715 goto end;
2717 hr = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2718 if (FAILED( hr ))
2719 goto end;
2721 list_init(&transforms);
2723 while ( TRUE )
2725 MSITABLEVIEW *tv = NULL;
2726 WCHAR name[0x40];
2727 ULONG count = 0;
2729 hr = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2730 if (FAILED( hr ) || !count)
2731 break;
2733 decode_streamname( stat.pwcsName, name );
2734 CoTaskMemFree( stat.pwcsName );
2735 if ( name[0] != 0x4840 )
2736 continue;
2738 if ( !wcscmp( name+1, szStringPool ) ||
2739 !wcscmp( name+1, szStringData ) )
2740 continue;
2742 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2743 if ( !transform )
2744 break;
2746 list_add_tail( &transforms, &transform->entry );
2748 transform->name = strdupW( name + 1 );
2750 if ( !wcscmp( transform->name, szTables ) )
2751 tables = transform;
2752 else if (!wcscmp( transform->name, szColumns ) )
2753 columns = transform;
2754 else if (!wcscmp( transform->name, szProperty ))
2755 property_update = TRUE;
2757 TRACE("transform contains stream %s\n", debugstr_w(name));
2759 /* load the table */
2760 if (TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv ) != ERROR_SUCCESS)
2761 continue;
2763 if (tv->view.ops->execute( &tv->view, NULL ) != ERROR_SUCCESS)
2765 tv->view.ops->delete( &tv->view );
2766 continue;
2769 tv->view.ops->delete( &tv->view );
2773 * Apply _Tables and _Columns transforms first so that
2774 * the table metadata is correct, and empty tables exist.
2776 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2777 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2778 goto end;
2780 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2781 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2782 goto end;
2784 ret = ERROR_SUCCESS;
2786 while ( !list_empty( &transforms ) )
2788 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2790 if ( wcscmp( transform->name, szColumns ) &&
2791 wcscmp( transform->name, szTables ) &&
2792 ret == ERROR_SUCCESS )
2794 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2797 list_remove( &transform->entry );
2798 msi_free( transform->name );
2799 msi_free( transform );
2802 if ( ret == ERROR_SUCCESS )
2804 append_storage_to_db( db, stg );
2805 if (property_update) msi_clone_properties( db );
2808 end:
2809 if ( stgenum )
2810 IEnumSTATSTG_Release( stgenum );
2811 if ( strings )
2812 msi_destroy_stringtable( strings );
2814 return ret;