Sync to trunk head (r47736)
[reactos.git] / dll / win32 / msi / table.c
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
3 *
4 * Copyright 2002-2005 Mike McCormack for CodeWeavers
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include <stdarg.h>
22 #include <assert.h>
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winerror.h"
31 #include "msi.h"
32 #include "msiquery.h"
33 #include "objbase.h"
34 #include "objidl.h"
35 #include "winnls.h"
36 #include "msipriv.h"
37 #include "query.h"
38
39 #include "wine/debug.h"
40 #include "wine/unicode.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
43
44 #define MSITABLE_HASH_TABLE_SIZE 37
45 #define LONG_STR_BYTES 3
46
47 typedef struct tagMSICOLUMNHASHENTRY
48 {
49 struct tagMSICOLUMNHASHENTRY *next;
50 UINT value;
51 UINT row;
52 } MSICOLUMNHASHENTRY;
53
54 typedef struct tagMSICOLUMNINFO
55 {
56 LPWSTR tablename;
57 UINT number;
58 LPWSTR colname;
59 UINT type;
60 UINT offset;
61 INT ref_count;
62 BOOL temporary;
63 MSICOLUMNHASHENTRY **hash_table;
64 } MSICOLUMNINFO;
65
66 typedef struct tagMSIORDERINFO
67 {
68 UINT *reorder;
69 UINT num_cols;
70 UINT cols[1];
71 } MSIORDERINFO;
72
73 struct tagMSITABLE
74 {
75 BYTE **data;
76 BOOL *data_persistent;
77 UINT row_count;
78 struct list entry;
79 MSICOLUMNINFO *colinfo;
80 UINT col_count;
81 MSICONDITION persistent;
82 INT ref_count;
83 WCHAR name[1];
84 };
85
86 static const WCHAR szStringData[] = {
87 '_','S','t','r','i','n','g','D','a','t','a',0 };
88 static const WCHAR szStringPool[] = {
89 '_','S','t','r','i','n','g','P','o','o','l',0 };
90
91 /* information for default tables */
92 static WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
93 static WCHAR szTable[] = { 'T','a','b','l','e',0 };
94 static WCHAR szName[] = { 'N','a','m','e',0 };
95 static WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
96 static WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
97 static WCHAR szType[] = { 'T','y','p','e',0 };
98
99 static const MSICOLUMNINFO _Columns_cols[4] = {
100 { szColumns, 1, szTable, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
101 { szColumns, 2, szNumber, MSITYPE_VALID | MSITYPE_KEY | 2, 2, 0, 0, NULL },
102 { szColumns, 3, szName, MSITYPE_VALID | MSITYPE_STRING | 64, 4, 0, 0, NULL },
103 { szColumns, 4, szType, MSITYPE_VALID | 2, 6, 0, 0, NULL },
104 };
105
106 static const MSICOLUMNINFO _Tables_cols[1] = {
107 { szTables, 1, szName, MSITYPE_VALID | MSITYPE_STRING | MSITYPE_KEY | 64, 0, 0, 0, NULL },
108 };
109
110 #define MAX_STREAM_NAME 0x1f
111
112 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
113 MSICOLUMNINFO **pcols, UINT *pcount );
114 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
115 DWORD count );
116 static UINT get_tablecolumns( MSIDATABASE *db,
117 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
118 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count );
119
120 static inline UINT bytes_per_column( MSIDATABASE *db, const MSICOLUMNINFO *col )
121 {
122 if( MSITYPE_IS_BINARY(col->type) )
123 return 2;
124
125 if( col->type & MSITYPE_STRING )
126 return db->bytes_per_strref;
127
128 if( (col->type & 0xff) <= 2)
129 return 2;
130
131 if( (col->type & 0xff) != 4 )
132 ERR("Invalid column size!\n");
133
134 return 4;
135 }
136
137 static int utf2mime(int x)
138 {
139 if( (x>='0') && (x<='9') )
140 return x-'0';
141 if( (x>='A') && (x<='Z') )
142 return x-'A'+10;
143 if( (x>='a') && (x<='z') )
144 return x-'a'+10+26;
145 if( x=='.' )
146 return 10+26+26;
147 if( x=='_' )
148 return 10+26+26+1;
149 return -1;
150 }
151
152 LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
153 {
154 DWORD count = MAX_STREAM_NAME;
155 DWORD ch, next;
156 LPWSTR out, p;
157
158 if( !bTable )
159 count = lstrlenW( in )+2;
160 out = msi_alloc( count*sizeof(WCHAR) );
161 p = out;
162
163 if( bTable )
164 {
165 *p++ = 0x4840;
166 count --;
167 }
168 while( count -- )
169 {
170 ch = *in++;
171 if( !ch )
172 {
173 *p = ch;
174 return out;
175 }
176 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
177 {
178 ch = utf2mime(ch) + 0x4800;
179 next = *in;
180 if( next && (next<0x80) )
181 {
182 next = utf2mime(next);
183 if( next != -1 )
184 {
185 next += 0x3ffffc0;
186 ch += (next<<6);
187 in++;
188 }
189 }
190 }
191 *p++ = ch;
192 }
193 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
194 msi_free( out );
195 return NULL;
196 }
197
198 static int mime2utf(int x)
199 {
200 if( x<10 )
201 return x + '0';
202 if( x<(10+26))
203 return x - 10 + 'A';
204 if( x<(10+26+26))
205 return x - 10 - 26 + 'a';
206 if( x == (10+26+26) )
207 return '.';
208 return '_';
209 }
210
211 BOOL decode_streamname(LPCWSTR in, LPWSTR out)
212 {
213 WCHAR ch;
214 DWORD count = 0;
215
216 while ( (ch = *in++) )
217 {
218 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
219 {
220 if( ch >= 0x4800 )
221 ch = mime2utf(ch-0x4800);
222 else
223 {
224 ch -= 0x3800;
225 *out++ = mime2utf(ch&0x3f);
226 count++;
227 ch = mime2utf((ch>>6)&0x3f);
228 }
229 }
230 *out++ = ch;
231 count++;
232 }
233 *out = 0;
234 return count;
235 }
236
237 void enum_stream_names( IStorage *stg )
238 {
239 IEnumSTATSTG *stgenum = NULL;
240 HRESULT r;
241 STATSTG stat;
242 ULONG n, count;
243 WCHAR name[0x40];
244
245 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
246 if( FAILED( r ) )
247 return;
248
249 n = 0;
250 while( 1 )
251 {
252 count = 0;
253 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
254 if( FAILED( r ) || !count )
255 break;
256 decode_streamname( stat.pwcsName, name );
257 TRACE("stream %2d -> %s %s\n", n,
258 debugstr_w(stat.pwcsName), debugstr_w(name) );
259 CoTaskMemFree( stat.pwcsName );
260 n++;
261 }
262
263 IEnumSTATSTG_Release( stgenum );
264 }
265
266 UINT read_stream_data( IStorage *stg, LPCWSTR stname, BOOL table,
267 BYTE **pdata, UINT *psz )
268 {
269 HRESULT r;
270 UINT ret = ERROR_FUNCTION_FAILED;
271 VOID *data;
272 ULONG sz, count;
273 IStream *stm = NULL;
274 STATSTG stat;
275 LPWSTR encname;
276
277 encname = encode_streamname(table, stname);
278
279 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
280
281 r = IStorage_OpenStream(stg, encname, NULL,
282 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
283 msi_free( encname );
284 if( FAILED( r ) )
285 {
286 WARN("open stream failed r = %08x - empty table?\n", r);
287 return ret;
288 }
289
290 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
291 if( FAILED( r ) )
292 {
293 WARN("open stream failed r = %08x!\n", r);
294 goto end;
295 }
296
297 if( stat.cbSize.QuadPart >> 32 )
298 {
299 WARN("Too big!\n");
300 goto end;
301 }
302
303 sz = stat.cbSize.QuadPart;
304 data = msi_alloc( sz );
305 if( !data )
306 {
307 WARN("couldn't allocate memory r=%08x!\n", r);
308 ret = ERROR_NOT_ENOUGH_MEMORY;
309 goto end;
310 }
311
312 r = IStream_Read(stm, data, sz, &count );
313 if( FAILED( r ) || ( count != sz ) )
314 {
315 msi_free( data );
316 WARN("read stream failed r = %08x!\n", r);
317 goto end;
318 }
319
320 *pdata = data;
321 *psz = sz;
322 ret = ERROR_SUCCESS;
323
324 end:
325 IStream_Release( stm );
326
327 return ret;
328 }
329
330 UINT write_stream_data( IStorage *stg, LPCWSTR stname,
331 LPCVOID data, UINT sz, BOOL bTable )
332 {
333 HRESULT r;
334 UINT ret = ERROR_FUNCTION_FAILED;
335 ULONG count;
336 IStream *stm = NULL;
337 ULARGE_INTEGER size;
338 LARGE_INTEGER pos;
339 LPWSTR encname;
340
341 encname = encode_streamname(bTable, stname );
342 r = IStorage_OpenStream( stg, encname, NULL,
343 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
344 if( FAILED(r) )
345 {
346 r = IStorage_CreateStream( stg, encname,
347 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
348 }
349 msi_free( encname );
350 if( FAILED( r ) )
351 {
352 WARN("open stream failed r = %08x\n", r);
353 return ret;
354 }
355
356 size.QuadPart = sz;
357 r = IStream_SetSize( stm, size );
358 if( FAILED( r ) )
359 {
360 WARN("Failed to SetSize\n");
361 goto end;
362 }
363
364 pos.QuadPart = 0;
365 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
366 if( FAILED( r ) )
367 {
368 WARN("Failed to Seek\n");
369 goto end;
370 }
371
372 if (sz)
373 {
374 r = IStream_Write(stm, data, sz, &count );
375 if( FAILED( r ) || ( count != sz ) )
376 {
377 WARN("Failed to Write\n");
378 goto end;
379 }
380 }
381
382 ret = ERROR_SUCCESS;
383
384 end:
385 IStream_Release( stm );
386
387 return ret;
388 }
389
390 static void free_table( MSITABLE *table )
391 {
392 UINT i;
393 for( i=0; i<table->row_count; i++ )
394 msi_free( table->data[i] );
395 msi_free( table->data );
396 msi_free( table->data_persistent );
397 msi_free_colinfo( table->colinfo, table->col_count );
398 msi_free( table->colinfo );
399 msi_free( table );
400 }
401
402 static UINT msi_table_get_row_size( MSIDATABASE *db,const MSICOLUMNINFO *cols,
403 UINT count )
404 {
405 const MSICOLUMNINFO *last_col = &cols[count-1];
406 if (!count)
407 return 0;
408 return last_col->offset + bytes_per_column( db, last_col );
409 }
410
411 /* add this table to the list of cached tables in the database */
412 static UINT read_table_from_storage( MSIDATABASE *db, MSITABLE *t, IStorage *stg )
413 {
414 BYTE *rawdata = NULL;
415 UINT rawsize = 0, i, j, row_size = 0;
416
417 TRACE("%s\n",debugstr_w(t->name));
418
419 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
420
421 /* if we can't read the table, just assume that it's empty */
422 read_stream_data( stg, t->name, TRUE, &rawdata, &rawsize );
423 if( !rawdata )
424 return ERROR_SUCCESS;
425
426 TRACE("Read %d bytes\n", rawsize );
427
428 if( rawsize % row_size )
429 {
430 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
431 goto err;
432 }
433
434 t->row_count = rawsize / row_size;
435 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
436 if( !t->data )
437 goto err;
438 t->data_persistent = msi_alloc_zero( t->row_count * sizeof(BOOL));
439 if ( !t->data_persistent )
440 goto err;
441
442 /* transpose all the data */
443 TRACE("Transposing data from %d rows\n", t->row_count );
444 for( i=0; i<t->row_count; i++ )
445 {
446 t->data[i] = msi_alloc( row_size );
447 if( !t->data[i] )
448 goto err;
449 t->data_persistent[i] = TRUE;
450
451 for( j=0; j<t->col_count; j++ )
452 {
453 UINT ofs = t->colinfo[j].offset;
454 UINT n = bytes_per_column( db, &t->colinfo[j] );
455 UINT k;
456
457 if ( n != 2 && n != 3 && n != 4 )
458 {
459 ERR("oops - unknown column width %d\n", n);
460 goto err;
461 }
462
463 for ( k = 0; k < n; k++ )
464 t->data[i][ofs + k] = rawdata[ofs*t->row_count + i * n + k];
465 }
466 }
467
468 msi_free( rawdata );
469 return ERROR_SUCCESS;
470 err:
471 msi_free( rawdata );
472 return ERROR_FUNCTION_FAILED;
473 }
474
475 void free_cached_tables( MSIDATABASE *db )
476 {
477 while( !list_empty( &db->tables ) )
478 {
479 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
480
481 list_remove( &t->entry );
482 free_table( t );
483 }
484 }
485
486 static MSITABLE *find_cached_table( MSIDATABASE *db, LPCWSTR name )
487 {
488 MSITABLE *t;
489
490 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
491 if( !lstrcmpW( name, t->name ) )
492 return t;
493
494 return NULL;
495 }
496
497 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
498 {
499 UINT r, column_count = 0;
500 MSICOLUMNINFO *columns;
501
502 /* get the number of columns in this table */
503 column_count = 0;
504 r = get_tablecolumns( db, name, NULL, &column_count );
505 if( r != ERROR_SUCCESS )
506 return r;
507
508 *pcount = column_count;
509
510 /* if there's no columns, there's no table */
511 if( column_count == 0 )
512 return ERROR_INVALID_PARAMETER;
513
514 TRACE("Table %s found\n", debugstr_w(name) );
515
516 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO) );
517 if( !columns )
518 return ERROR_FUNCTION_FAILED;
519
520 r = get_tablecolumns( db, name, columns, &column_count );
521 if( r != ERROR_SUCCESS )
522 {
523 msi_free( columns );
524 return ERROR_FUNCTION_FAILED;
525 }
526
527 *pcols = columns;
528
529 return r;
530 }
531
532 UINT msi_create_table( MSIDATABASE *db, LPCWSTR name, column_info *col_info,
533 MSICONDITION persistent, MSITABLE **table_ret)
534 {
535 UINT r, nField;
536 MSIVIEW *tv = NULL;
537 MSIRECORD *rec = NULL;
538 column_info *col;
539 MSITABLE *table;
540 UINT i;
541
542 /* only add tables that don't exist already */
543 if( TABLE_Exists(db, name ) )
544 {
545 WARN("table %s exists\n", debugstr_w(name));
546 return ERROR_BAD_QUERY_SYNTAX;
547 }
548
549 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
550 if( !table )
551 return ERROR_FUNCTION_FAILED;
552
553 table->ref_count = 1;
554 table->row_count = 0;
555 table->data = NULL;
556 table->data_persistent = NULL;
557 table->colinfo = NULL;
558 table->col_count = 0;
559 table->persistent = persistent;
560 lstrcpyW( table->name, name );
561
562 for( col = col_info; col; col = col->next )
563 table->col_count++;
564
565 table->colinfo = msi_alloc( table->col_count * sizeof(MSICOLUMNINFO) );
566 if (!table->colinfo)
567 {
568 free_table( table );
569 return ERROR_FUNCTION_FAILED;
570 }
571
572 for( i = 0, col = col_info; col; i++, col = col->next )
573 {
574 table->colinfo[ i ].tablename = strdupW( col->table );
575 table->colinfo[ i ].number = i + 1;
576 table->colinfo[ i ].colname = strdupW( col->column );
577 table->colinfo[ i ].type = col->type;
578 table->colinfo[ i ].offset = 0;
579 table->colinfo[ i ].ref_count = 0;
580 table->colinfo[ i ].hash_table = NULL;
581 table->colinfo[ i ].temporary = col->temporary;
582 }
583 table_calc_column_offsets( db, table->colinfo, table->col_count);
584
585 r = TABLE_CreateView( db, szTables, &tv );
586 TRACE("CreateView returned %x\n", r);
587 if( r )
588 {
589 free_table( table );
590 return r;
591 }
592
593 r = tv->ops->execute( tv, 0 );
594 TRACE("tv execute returned %x\n", r);
595 if( r )
596 goto err;
597
598 rec = MSI_CreateRecord( 1 );
599 if( !rec )
600 goto err;
601
602 r = MSI_RecordSetStringW( rec, 1, name );
603 if( r )
604 goto err;
605
606 r = tv->ops->insert_row( tv, rec, -1, persistent == MSICONDITION_FALSE );
607 TRACE("insert_row returned %x\n", r);
608 if( r )
609 goto err;
610
611 tv->ops->delete( tv );
612 tv = NULL;
613
614 msiobj_release( &rec->hdr );
615 rec = NULL;
616
617 if( persistent != MSICONDITION_FALSE )
618 {
619 /* add each column to the _Columns table */
620 r = TABLE_CreateView( db, szColumns, &tv );
621 if( r )
622 return r;
623
624 r = tv->ops->execute( tv, 0 );
625 TRACE("tv execute returned %x\n", r);
626 if( r )
627 goto err;
628
629 rec = MSI_CreateRecord( 4 );
630 if( !rec )
631 goto err;
632
633 r = MSI_RecordSetStringW( rec, 1, name );
634 if( r )
635 goto err;
636
637 /*
638 * need to set the table, column number, col name and type
639 * for each column we enter in the table
640 */
641 nField = 1;
642 for( col = col_info; col; col = col->next )
643 {
644 r = MSI_RecordSetInteger( rec, 2, nField );
645 if( r )
646 goto err;
647
648 r = MSI_RecordSetStringW( rec, 3, col->column );
649 if( r )
650 goto err;
651
652 r = MSI_RecordSetInteger( rec, 4, col->type );
653 if( r )
654 goto err;
655
656 r = tv->ops->insert_row( tv, rec, -1, FALSE );
657 if( r )
658 goto err;
659
660 nField++;
661 }
662 if( !col )
663 r = ERROR_SUCCESS;
664 }
665
666 err:
667 if (rec)
668 msiobj_release( &rec->hdr );
669 /* FIXME: remove values from the string table on error */
670 if( tv )
671 tv->ops->delete( tv );
672
673 if (r == ERROR_SUCCESS)
674 {
675 list_add_head( &db->tables, &table->entry );
676 *table_ret = table;
677 }
678 else
679 free_table( table );
680
681 return r;
682 }
683
684 static UINT get_table( MSIDATABASE *db, LPCWSTR name, MSITABLE **table_ret )
685 {
686 MSITABLE *table;
687 UINT r;
688
689 /* first, see if the table is cached */
690 table = find_cached_table( db, name );
691 if( table )
692 {
693 *table_ret = table;
694 return ERROR_SUCCESS;
695 }
696
697 /* nonexistent tables should be interpreted as empty tables */
698 table = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
699 if( !table )
700 return ERROR_FUNCTION_FAILED;
701
702 table->row_count = 0;
703 table->data = NULL;
704 table->data_persistent = NULL;
705 table->colinfo = NULL;
706 table->col_count = 0;
707 table->persistent = MSICONDITION_TRUE;
708 lstrcpyW( table->name, name );
709
710 if ( !lstrcmpW(name, szTables) || !lstrcmpW(name, szColumns) )
711 table->persistent = MSICONDITION_NONE;
712
713 r = table_get_column_info( db, name, &table->colinfo, &table->col_count);
714 if (r != ERROR_SUCCESS)
715 {
716 free_table ( table );
717 return r;
718 }
719
720 r = read_table_from_storage( db, table, db->storage );
721 if( r != ERROR_SUCCESS )
722 {
723 free_table( table );
724 return r;
725 }
726
727 list_add_head( &db->tables, &table->entry );
728 *table_ret = table;
729 return ERROR_SUCCESS;
730 }
731
732 static UINT save_table( MSIDATABASE *db, const MSITABLE *t )
733 {
734 BYTE *rawdata = NULL, *p;
735 UINT rawsize, r, i, j, row_size;
736
737 /* Nothing to do for non-persistent tables */
738 if( t->persistent == MSICONDITION_FALSE )
739 return ERROR_SUCCESS;
740
741 TRACE("Saving %s\n", debugstr_w( t->name ) );
742
743 row_size = msi_table_get_row_size( db, t->colinfo, t->col_count );
744
745 rawsize = t->row_count * row_size;
746 rawdata = msi_alloc_zero( rawsize );
747 if( !rawdata )
748 {
749 r = ERROR_NOT_ENOUGH_MEMORY;
750 goto err;
751 }
752
753 rawsize = 0;
754 p = rawdata;
755 for( i=0; i<t->col_count; i++ )
756 {
757 for( j=0; j<t->row_count; j++ )
758 {
759 UINT offset = t->colinfo[i].offset;
760
761 if (!t->data_persistent[j]) continue;
762 if (i == 0)
763 rawsize += row_size;
764
765 *p++ = t->data[j][offset];
766 *p++ = t->data[j][offset + 1];
767 if( 4 == bytes_per_column( db, &t->colinfo[i] ) )
768 {
769 *p++ = t->data[j][offset + 2];
770 *p++ = t->data[j][offset + 3];
771 }
772 }
773 }
774
775 TRACE("writing %d bytes\n", rawsize);
776 r = write_stream_data( db->storage, t->name, rawdata, rawsize, TRUE );
777
778 err:
779 msi_free( rawdata );
780
781 return r;
782 }
783
784 static void table_calc_column_offsets( MSIDATABASE *db, MSICOLUMNINFO *colinfo,
785 DWORD count )
786 {
787 DWORD i;
788
789 for( i=0; colinfo && (i<count); i++ )
790 {
791 assert( (i+1) == colinfo[ i ].number );
792 if (i)
793 colinfo[i].offset = colinfo[ i - 1 ].offset
794 + bytes_per_column( db, &colinfo[ i - 1 ] );
795 else
796 colinfo[i].offset = 0;
797 TRACE("column %d is [%s] with type %08x ofs %d\n",
798 colinfo[i].number, debugstr_w(colinfo[i].colname),
799 colinfo[i].type, colinfo[i].offset);
800 }
801 }
802
803 static UINT get_defaulttablecolumns( MSIDATABASE *db, LPCWSTR name,
804 MSICOLUMNINFO *colinfo, UINT *sz)
805 {
806 const MSICOLUMNINFO *p;
807 DWORD i, n;
808
809 TRACE("%s\n", debugstr_w(name));
810
811 if (!lstrcmpW( name, szTables ))
812 {
813 p = _Tables_cols;
814 n = 1;
815 }
816 else if (!lstrcmpW( name, szColumns ))
817 {
818 p = _Columns_cols;
819 n = 4;
820 }
821 else
822 return ERROR_FUNCTION_FAILED;
823
824 /* duplicate the string data so we can free it in msi_free_colinfo */
825 for (i=0; i<n; i++)
826 {
827 if (colinfo && (i < *sz) )
828 {
829 colinfo[i] = p[i];
830 colinfo[i].tablename = strdupW( p[i].tablename );
831 colinfo[i].colname = strdupW( p[i].colname );
832 }
833 if( colinfo && (i >= *sz) )
834 break;
835 }
836 table_calc_column_offsets( db, colinfo, n );
837 *sz = n;
838 return ERROR_SUCCESS;
839 }
840
841 static void msi_free_colinfo( MSICOLUMNINFO *colinfo, UINT count )
842 {
843 UINT i;
844
845 for( i=0; i<count; i++ )
846 {
847 msi_free( colinfo[i].tablename );
848 msi_free( colinfo[i].colname );
849 msi_free( colinfo[i].hash_table );
850 }
851 }
852
853 static LPWSTR msi_makestring( const MSIDATABASE *db, UINT stringid)
854 {
855 return strdupW(msi_string_lookup_id( db->strings, stringid ));
856 }
857
858 static UINT read_table_int(BYTE *const *data, UINT row, UINT col, UINT bytes)
859 {
860 UINT ret = 0, i;
861
862 for (i = 0; i < bytes; i++)
863 ret += (data[row][col + i] << i * 8);
864
865 return ret;
866 }
867
868 static UINT get_tablecolumns( MSIDATABASE *db,
869 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
870 {
871 UINT r, i, n=0, table_id, count, maxcount = *sz;
872 MSITABLE *table = NULL;
873
874 TRACE("%s\n", debugstr_w(szTableName));
875
876 /* first check if there is a default table with that name */
877 r = get_defaulttablecolumns( db, szTableName, colinfo, sz );
878 if( ( r == ERROR_SUCCESS ) && *sz )
879 return r;
880
881 r = get_table( db, szColumns, &table );
882 if( r != ERROR_SUCCESS )
883 {
884 ERR("couldn't load _Columns table\n");
885 return ERROR_FUNCTION_FAILED;
886 }
887
888 /* convert table and column names to IDs from the string table */
889 r = msi_string2idW( db->strings, szTableName, &table_id );
890 if( r != ERROR_SUCCESS )
891 {
892 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
893 return r;
894 }
895
896 TRACE("Table id is %d, row count is %d\n", table_id, table->row_count);
897
898 /* Note: _Columns table doesn't have non-persistent data */
899
900 /* if maxcount is non-zero, assume it's exactly right for this table */
901 memset( colinfo, 0, maxcount*sizeof(*colinfo) );
902 count = table->row_count;
903 for( i=0; i<count; i++ )
904 {
905 if( read_table_int(table->data, i, 0, db->bytes_per_strref) != table_id )
906 continue;
907 if( colinfo )
908 {
909 UINT id = read_table_int(table->data, i, table->colinfo[2].offset, db->bytes_per_strref);
910 UINT col = read_table_int(table->data, i, table->colinfo[1].offset, sizeof(USHORT)) - (1<<15);
911
912 /* check the column number is in range */
913 if (col<1 || col>maxcount)
914 {
915 ERR("column %d out of range\n", col);
916 continue;
917 }
918
919 /* check if this column was already set */
920 if (colinfo[ col - 1 ].number)
921 {
922 ERR("duplicate column %d\n", col);
923 continue;
924 }
925
926 colinfo[ col - 1 ].tablename = msi_makestring( db, table_id );
927 colinfo[ col - 1 ].number = col;
928 colinfo[ col - 1 ].colname = msi_makestring( db, id );
929 colinfo[ col - 1 ].type = read_table_int(table->data, i,
930 table->colinfo[3].offset,
931 sizeof(USHORT)) - (1<<15);
932 colinfo[ col - 1 ].offset = 0;
933 colinfo[ col - 1 ].ref_count = 0;
934 colinfo[ col - 1 ].hash_table = NULL;
935 }
936 n++;
937 }
938
939 TRACE("%s has %d columns\n", debugstr_w(szTableName), n);
940
941 if (colinfo && n != maxcount)
942 {
943 ERR("missing column in table %s\n", debugstr_w(szTableName));
944 msi_free_colinfo(colinfo, maxcount );
945 return ERROR_FUNCTION_FAILED;
946 }
947
948 table_calc_column_offsets( db, colinfo, n );
949 *sz = n;
950
951 return ERROR_SUCCESS;
952 }
953
954 static void msi_update_table_columns( MSIDATABASE *db, LPCWSTR name )
955 {
956 MSITABLE *table;
957 LPWSTR tablename;
958 UINT size, offset, old_count;
959 UINT n;
960
961 /* We may free name in msi_free_colinfo. */
962 tablename = strdupW( name );
963
964 table = find_cached_table( db, tablename );
965 old_count = table->col_count;
966 msi_free_colinfo( table->colinfo, table->col_count );
967 msi_free( table->colinfo );
968 table->colinfo = NULL;
969
970 table_get_column_info( db, tablename, &table->colinfo, &table->col_count );
971 if (!table->col_count)
972 goto done;
973
974 size = msi_table_get_row_size( db, table->colinfo, table->col_count );
975 offset = table->colinfo[table->col_count - 1].offset;
976
977 for ( n = 0; n < table->row_count; n++ )
978 {
979 table->data[n] = msi_realloc( table->data[n], size );
980 if (old_count < table->col_count)
981 memset( &table->data[n][offset], 0, size - offset );
982 }
983
984 done:
985 msi_free(tablename);
986 }
987
988 /* try to find the table name in the _Tables table */
989 BOOL TABLE_Exists( MSIDATABASE *db, LPCWSTR name )
990 {
991 UINT r, table_id = 0, i, count;
992 MSITABLE *table = NULL;
993
994 static const WCHAR szStreams[] = {'_','S','t','r','e','a','m','s',0};
995 static const WCHAR szStorages[] = {'_','S','t','o','r','a','g','e','s',0};
996
997 if( !lstrcmpW( name, szTables ) || !lstrcmpW( name, szColumns ) ||
998 !lstrcmpW( name, szStreams ) || !lstrcmpW( name, szStorages ) )
999 return TRUE;
1000
1001 r = msi_string2idW( db->strings, name, &table_id );
1002 if( r != ERROR_SUCCESS )
1003 {
1004 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1005 return FALSE;
1006 }
1007
1008 r = get_table( db, szTables, &table );
1009 if( r != ERROR_SUCCESS )
1010 {
1011 ERR("table %s not available\n", debugstr_w(szTables));
1012 return FALSE;
1013 }
1014
1015 count = table->row_count;
1016 for( i=0; i<count; i++ )
1017 if( table->data[ i ][ 0 ] == table_id )
1018 return TRUE;
1019
1020 return FALSE;
1021 }
1022
1023 /* below is the query interface to a table */
1024
1025 typedef struct tagMSITABLEVIEW
1026 {
1027 MSIVIEW view;
1028 MSIDATABASE *db;
1029 MSITABLE *table;
1030 MSICOLUMNINFO *columns;
1031 MSIORDERINFO *order;
1032 UINT num_cols;
1033 UINT row_size;
1034 WCHAR name[1];
1035 } MSITABLEVIEW;
1036
1037 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1038 {
1039 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1040 UINT offset, n;
1041
1042 if( !tv->table )
1043 return ERROR_INVALID_PARAMETER;
1044
1045 if( (col==0) || (col>tv->num_cols) )
1046 return ERROR_INVALID_PARAMETER;
1047
1048 /* how many rows are there ? */
1049 if( row >= tv->table->row_count )
1050 return ERROR_NO_MORE_ITEMS;
1051
1052 if( tv->columns[col-1].offset >= tv->row_size )
1053 {
1054 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1055 ERR("%p %p\n", tv, tv->columns );
1056 return ERROR_FUNCTION_FAILED;
1057 }
1058
1059 if (tv->order)
1060 row = tv->order->reorder[row];
1061
1062 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1063 if (n != 2 && n != 3 && n != 4)
1064 {
1065 ERR("oops! what is %d bytes per column?\n", n );
1066 return ERROR_FUNCTION_FAILED;
1067 }
1068
1069 offset = tv->columns[col-1].offset;
1070 *val = read_table_int(tv->table->data, row, offset, n);
1071
1072 /* TRACE("Data [%d][%d] = %d\n", row, col, *val ); */
1073
1074 return ERROR_SUCCESS;
1075 }
1076
1077 static UINT msi_stream_name( const MSITABLEVIEW *tv, UINT row, LPWSTR *pstname )
1078 {
1079 LPWSTR p, stname = NULL;
1080 UINT i, r, type, ival;
1081 DWORD len;
1082 LPCWSTR sval;
1083 MSIVIEW *view = (MSIVIEW *) tv;
1084
1085 TRACE("%p %d\n", tv, row);
1086
1087 len = lstrlenW( tv->name ) + 1;
1088 stname = msi_alloc( len*sizeof(WCHAR) );
1089 if ( !stname )
1090 {
1091 r = ERROR_OUTOFMEMORY;
1092 goto err;
1093 }
1094
1095 lstrcpyW( stname, tv->name );
1096
1097 for ( i = 0; i < tv->num_cols; i++ )
1098 {
1099 type = tv->columns[i].type;
1100 if ( type & MSITYPE_KEY )
1101 {
1102 r = TABLE_fetch_int( view, row, i+1, &ival );
1103 if ( r != ERROR_SUCCESS )
1104 goto err;
1105
1106 if ( tv->columns[i].type & MSITYPE_STRING )
1107 {
1108 sval = msi_string_lookup_id( tv->db->strings, ival );
1109 if ( !sval )
1110 {
1111 r = ERROR_INVALID_PARAMETER;
1112 goto err;
1113 }
1114 }
1115 else
1116 {
1117 static const WCHAR fmt[] = { '%','d',0 };
1118 WCHAR number[0x20];
1119 UINT n = bytes_per_column( tv->db, &tv->columns[i] );
1120
1121 switch( n )
1122 {
1123 case 2:
1124 sprintfW( number, fmt, ival-0x8000 );
1125 break;
1126 case 4:
1127 sprintfW( number, fmt, ival^0x80000000 );
1128 break;
1129 default:
1130 ERR( "oops - unknown column width %d\n", n );
1131 r = ERROR_FUNCTION_FAILED;
1132 goto err;
1133 }
1134 sval = number;
1135 }
1136
1137 len += lstrlenW( szDot ) + lstrlenW( sval );
1138 p = msi_realloc ( stname, len*sizeof(WCHAR) );
1139 if ( !p )
1140 {
1141 r = ERROR_OUTOFMEMORY;
1142 goto err;
1143 }
1144 stname = p;
1145
1146 lstrcatW( stname, szDot );
1147 lstrcatW( stname, sval );
1148 }
1149 else
1150 continue;
1151 }
1152
1153 *pstname = stname;
1154 return ERROR_SUCCESS;
1155
1156 err:
1157 msi_free( stname );
1158 *pstname = NULL;
1159 return r;
1160 }
1161
1162 /*
1163 * We need a special case for streams, as we need to reference column with
1164 * the name of the stream in the same table, and the table name
1165 * which may not be available at higher levels of the query
1166 */
1167 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1168 {
1169 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1170 UINT r;
1171 LPWSTR encname, full_name = NULL;
1172
1173 if( !view->ops->fetch_int )
1174 return ERROR_INVALID_PARAMETER;
1175
1176 r = msi_stream_name( tv, row, &full_name );
1177 if ( r != ERROR_SUCCESS )
1178 {
1179 ERR("fetching stream, error = %d\n", r);
1180 return r;
1181 }
1182
1183 encname = encode_streamname( FALSE, full_name );
1184 r = db_get_raw_stream( tv->db, encname, stm );
1185 if( r )
1186 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1187
1188 msi_free( full_name );
1189 msi_free( encname );
1190 return r;
1191 }
1192
1193 static UINT TABLE_set_int( MSITABLEVIEW *tv, UINT row, UINT col, UINT val )
1194 {
1195 UINT offset, n, i;
1196
1197 if( !tv->table )
1198 return ERROR_INVALID_PARAMETER;
1199
1200 if( (col==0) || (col>tv->num_cols) )
1201 return ERROR_INVALID_PARAMETER;
1202
1203 if( row >= tv->table->row_count )
1204 return ERROR_INVALID_PARAMETER;
1205
1206 if( tv->columns[col-1].offset >= tv->row_size )
1207 {
1208 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1209 ERR("%p %p\n", tv, tv->columns );
1210 return ERROR_FUNCTION_FAILED;
1211 }
1212
1213 msi_free( tv->columns[col-1].hash_table );
1214 tv->columns[col-1].hash_table = NULL;
1215
1216 n = bytes_per_column( tv->db, &tv->columns[col-1] );
1217 if ( n != 2 && n != 3 && n != 4 )
1218 {
1219 ERR("oops! what is %d bytes per column?\n", n );
1220 return ERROR_FUNCTION_FAILED;
1221 }
1222
1223 offset = tv->columns[col-1].offset;
1224 for ( i = 0; i < n; i++ )
1225 tv->table->data[row][offset + i] = (val >> i * 8) & 0xff;
1226
1227 return ERROR_SUCCESS;
1228 }
1229
1230 static UINT TABLE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
1231 {
1232 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1233
1234 if (!tv->table)
1235 return ERROR_INVALID_PARAMETER;
1236
1237 if (tv->order)
1238 row = tv->order->reorder[row];
1239
1240 return msi_view_get_row(tv->db, view, row, rec);
1241 }
1242
1243 static UINT msi_addstreamW( MSIDATABASE *db, LPCWSTR name, IStream *data )
1244 {
1245 UINT r;
1246 MSIQUERY *query = NULL;
1247 MSIRECORD *rec = NULL;
1248
1249 static const WCHAR insert[] = {
1250 'I','N','S','E','R','T',' ','I','N','T','O',' ',
1251 '`','_','S','t','r','e','a','m','s','`',' ',
1252 '(','`','N','a','m','e','`',',',
1253 '`','D','a','t','a','`',')',' ',
1254 'V','A','L','U','E','S',' ','(','?',',','?',')',0};
1255
1256 TRACE("%p %s %p\n", db, debugstr_w(name), data);
1257
1258 rec = MSI_CreateRecord( 2 );
1259 if ( !rec )
1260 return ERROR_OUTOFMEMORY;
1261
1262 r = MSI_RecordSetStringW( rec, 1, name );
1263 if ( r != ERROR_SUCCESS )
1264 goto err;
1265
1266 r = MSI_RecordSetIStream( rec, 2, data );
1267 if ( r != ERROR_SUCCESS )
1268 goto err;
1269
1270 r = MSI_DatabaseOpenViewW( db, insert, &query );
1271 if ( r != ERROR_SUCCESS )
1272 goto err;
1273
1274 r = MSI_ViewExecute( query, rec );
1275
1276 err:
1277 msiobj_release( &query->hdr );
1278 msiobj_release( &rec->hdr );
1279
1280 return r;
1281 }
1282
1283 static UINT get_table_value_from_record( MSITABLEVIEW *tv, MSIRECORD *rec, UINT iField, UINT *pvalue )
1284 {
1285 MSICOLUMNINFO columninfo;
1286 UINT r;
1287
1288 if ( (iField <= 0) ||
1289 (iField > tv->num_cols) ||
1290 MSI_RecordIsNull( rec, iField ) )
1291 return ERROR_FUNCTION_FAILED;
1292
1293 columninfo = tv->columns[ iField - 1 ];
1294
1295 if ( MSITYPE_IS_BINARY(columninfo.type) )
1296 {
1297 *pvalue = 1; /* refers to the first key column */
1298 }
1299 else if ( columninfo.type & MSITYPE_STRING )
1300 {
1301 LPCWSTR sval = MSI_RecordGetString( rec, iField );
1302
1303 r = msi_string2idW(tv->db->strings, sval, pvalue);
1304 if (r != ERROR_SUCCESS)
1305 return ERROR_NOT_FOUND;
1306 }
1307 else if ( 2 == bytes_per_column( tv->db, &columninfo ) )
1308 {
1309 *pvalue = 0x8000 + MSI_RecordGetInteger( rec, iField );
1310 if ( *pvalue & 0xffff0000 )
1311 {
1312 ERR("field %u value %d out of range\n", iField, *pvalue - 0x8000);
1313 return ERROR_FUNCTION_FAILED;
1314 }
1315 }
1316 else
1317 {
1318 INT ival = MSI_RecordGetInteger( rec, iField );
1319 *pvalue = ival ^ 0x80000000;
1320 }
1321
1322 return ERROR_SUCCESS;
1323 }
1324
1325 static UINT TABLE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
1326 {
1327 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1328 UINT i, val, r = ERROR_SUCCESS;
1329
1330 if ( !tv->table )
1331 return ERROR_INVALID_PARAMETER;
1332
1333 /* test if any of the mask bits are invalid */
1334 if ( mask >= (1<<tv->num_cols) )
1335 return ERROR_INVALID_PARAMETER;
1336
1337 for ( i = 0; i < tv->num_cols; i++ )
1338 {
1339 BOOL persistent;
1340
1341 /* only update the fields specified in the mask */
1342 if ( !(mask&(1<<i)) )
1343 continue;
1344
1345 persistent = (tv->table->persistent != MSICONDITION_FALSE) &&
1346 (tv->table->data_persistent[row]);
1347 /* FIXME: should we allow updating keys? */
1348
1349 val = 0;
1350 if ( !MSI_RecordIsNull( rec, i + 1 ) )
1351 {
1352 r = get_table_value_from_record (tv, rec, i + 1, &val);
1353 if ( MSITYPE_IS_BINARY(tv->columns[ i ].type) )
1354 {
1355 IStream *stm;
1356 LPWSTR stname;
1357
1358 if ( r != ERROR_SUCCESS )
1359 return ERROR_FUNCTION_FAILED;
1360
1361 r = MSI_RecordGetIStream( rec, i + 1, &stm );
1362 if ( r != ERROR_SUCCESS )
1363 return r;
1364
1365 r = msi_stream_name( tv, row, &stname );
1366 if ( r != ERROR_SUCCESS )
1367 {
1368 IStream_Release( stm );
1369 return r;
1370 }
1371
1372 r = msi_addstreamW( tv->db, stname, stm );
1373 IStream_Release( stm );
1374 msi_free ( stname );
1375
1376 if ( r != ERROR_SUCCESS )
1377 return r;
1378 }
1379 else if ( tv->columns[i].type & MSITYPE_STRING )
1380 {
1381 UINT x;
1382
1383 if ( r != ERROR_SUCCESS )
1384 {
1385 LPCWSTR sval = MSI_RecordGetString( rec, i + 1 );
1386 val = msi_addstringW( tv->db->strings, sval, -1, 1,
1387 persistent ? StringPersistent : StringNonPersistent );
1388
1389 }
1390 else
1391 {
1392 TABLE_fetch_int(&tv->view, row, i + 1, &x);
1393 if (val == x)
1394 continue;
1395 }
1396 }
1397 else
1398 {
1399 if ( r != ERROR_SUCCESS )
1400 return ERROR_FUNCTION_FAILED;
1401 }
1402 }
1403
1404 r = TABLE_set_int( tv, row, i+1, val );
1405 if ( r != ERROR_SUCCESS )
1406 break;
1407 }
1408 return r;
1409 }
1410
1411 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num, BOOL temporary )
1412 {
1413 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1414 BYTE **p, *row;
1415 BOOL *b;
1416 UINT sz;
1417 BYTE ***data_ptr;
1418 BOOL **data_persist_ptr;
1419 UINT *row_count;
1420
1421 TRACE("%p %s\n", view, temporary ? "TRUE" : "FALSE");
1422
1423 if( !tv->table )
1424 return ERROR_INVALID_PARAMETER;
1425
1426 row = msi_alloc_zero( tv->row_size );
1427 if( !row )
1428 return ERROR_NOT_ENOUGH_MEMORY;
1429
1430 row_count = &tv->table->row_count;
1431 data_ptr = &tv->table->data;
1432 data_persist_ptr = &tv->table->data_persistent;
1433 if (*num == -1)
1434 *num = tv->table->row_count;
1435
1436 sz = (*row_count + 1) * sizeof (BYTE*);
1437 if( *data_ptr )
1438 p = msi_realloc( *data_ptr, sz );
1439 else
1440 p = msi_alloc( sz );
1441 if( !p )
1442 {
1443 msi_free( row );
1444 return ERROR_NOT_ENOUGH_MEMORY;
1445 }
1446
1447 sz = (*row_count + 1) * sizeof (BOOL);
1448 if( *data_persist_ptr )
1449 b = msi_realloc( *data_persist_ptr, sz );
1450 else
1451 b = msi_alloc( sz );
1452 if( !b )
1453 {
1454 msi_free( row );
1455 msi_free( p );
1456 return ERROR_NOT_ENOUGH_MEMORY;
1457 }
1458
1459 *data_ptr = p;
1460 (*data_ptr)[*row_count] = row;
1461
1462 *data_persist_ptr = b;
1463 (*data_persist_ptr)[*row_count] = !temporary;
1464
1465 (*row_count)++;
1466
1467 return ERROR_SUCCESS;
1468 }
1469
1470 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1471 {
1472 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1473
1474 TRACE("%p %p\n", tv, record);
1475
1476 TRACE("There are %d columns\n", tv->num_cols );
1477
1478 return ERROR_SUCCESS;
1479 }
1480
1481 static UINT TABLE_close( struct tagMSIVIEW *view )
1482 {
1483 TRACE("%p\n", view );
1484
1485 return ERROR_SUCCESS;
1486 }
1487
1488 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1489 {
1490 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1491
1492 TRACE("%p %p %p\n", view, rows, cols );
1493
1494 if( cols )
1495 *cols = tv->num_cols;
1496 if( rows )
1497 {
1498 if( !tv->table )
1499 return ERROR_INVALID_PARAMETER;
1500 *rows = tv->table->row_count;
1501 }
1502
1503 return ERROR_SUCCESS;
1504 }
1505
1506 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1507 UINT n, LPWSTR *name, UINT *type, BOOL *temporary,
1508 LPWSTR *table_name )
1509 {
1510 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1511
1512 TRACE("%p %d %p %p\n", tv, n, name, type );
1513
1514 if( ( n == 0 ) || ( n > tv->num_cols ) )
1515 return ERROR_INVALID_PARAMETER;
1516
1517 if( name )
1518 {
1519 *name = strdupW( tv->columns[n-1].colname );
1520 if( !*name )
1521 return ERROR_FUNCTION_FAILED;
1522 }
1523
1524 if( table_name )
1525 {
1526 *table_name = strdupW( tv->columns[n-1].tablename );
1527 if( !*table_name )
1528 return ERROR_FUNCTION_FAILED;
1529 }
1530
1531 if( type )
1532 *type = tv->columns[n-1].type;
1533
1534 if( temporary )
1535 *temporary = tv->columns[n-1].temporary;
1536
1537 return ERROR_SUCCESS;
1538 }
1539
1540 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row );
1541
1542 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1543 {
1544 UINT r, row, i;
1545
1546 /* check there's no null values where they're not allowed */
1547 for( i = 0; i < tv->num_cols; i++ )
1548 {
1549 if ( tv->columns[i].type & MSITYPE_NULLABLE )
1550 continue;
1551
1552 if ( MSITYPE_IS_BINARY(tv->columns[i].type) )
1553 TRACE("skipping binary column\n");
1554 else if ( tv->columns[i].type & MSITYPE_STRING )
1555 {
1556 LPCWSTR str;
1557
1558 str = MSI_RecordGetString( rec, i+1 );
1559 if (str == NULL || str[0] == 0)
1560 return ERROR_INVALID_DATA;
1561 }
1562 else
1563 {
1564 UINT n;
1565
1566 n = MSI_RecordGetInteger( rec, i+1 );
1567 if (n == MSI_NULL_INTEGER)
1568 return ERROR_INVALID_DATA;
1569 }
1570 }
1571
1572 /* check there's no duplicate keys */
1573 r = msi_table_find_row( tv, rec, &row );
1574 if (r == ERROR_SUCCESS)
1575 return ERROR_FUNCTION_FAILED;
1576
1577 return ERROR_SUCCESS;
1578 }
1579
1580 static UINT find_insert_index( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *pidx )
1581 {
1582 UINT r, idx, j, ivalue, x;
1583
1584 TRACE("%p %p %p\n", tv, rec, pidx);
1585
1586 for (idx = 0; idx < tv->table->row_count; idx++)
1587 {
1588 for (j = 0; j < tv->num_cols; j++ )
1589 {
1590 r = get_table_value_from_record (tv, rec, j+1, &ivalue);
1591 if (r != ERROR_SUCCESS)
1592 break;
1593
1594 r = TABLE_fetch_int(&tv->view, idx, j + 1, &x);
1595 if (r != ERROR_SUCCESS)
1596 return r;
1597
1598 if (ivalue > x)
1599 break;
1600 else if (ivalue == x)
1601 continue;
1602 else {
1603 TRACE("Found %d.\n", idx);
1604 *pidx = idx;
1605 return ERROR_SUCCESS;
1606 }
1607 }
1608 }
1609
1610 TRACE("Found %d.\n", idx);
1611 *pidx = idx;
1612 return ERROR_SUCCESS;
1613 }
1614
1615 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row, BOOL temporary )
1616 {
1617 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1618 UINT i, r;
1619
1620 TRACE("%p %p %s\n", tv, rec, temporary ? "TRUE" : "FALSE" );
1621
1622 /* check that the key is unique - can we find a matching row? */
1623 r = table_validate_new( tv, rec );
1624 if( r != ERROR_SUCCESS )
1625 return ERROR_FUNCTION_FAILED;
1626
1627 if (row == -1)
1628 {
1629 r = find_insert_index(tv, rec, &row);
1630 if( r != ERROR_SUCCESS )
1631 return ERROR_FUNCTION_FAILED;
1632 }
1633
1634 r = table_create_new_row( view, &row, temporary );
1635 TRACE("insert_row returned %08x\n", r);
1636 if( r != ERROR_SUCCESS )
1637 return r;
1638
1639 /* shift the rows to make room for the new row */
1640 for (i = tv->table->row_count - 1; i > row; i--)
1641 {
1642 memmove(&(tv->table->data[i][0]),
1643 &(tv->table->data[i - 1][0]), tv->row_size);
1644 tv->table->data_persistent[i] = tv->table->data_persistent[i - 1];
1645 }
1646
1647 /* Re-set the persistence flag */
1648 tv->table->data_persistent[row] = !temporary;
1649 return TABLE_set_row( view, row, rec, (1<<tv->num_cols) - 1 );
1650 }
1651
1652 static UINT TABLE_delete_row( struct tagMSIVIEW *view, UINT row )
1653 {
1654 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1655 UINT r, num_rows, num_cols, i;
1656
1657 TRACE("%p %d\n", tv, row);
1658
1659 if ( !tv->table )
1660 return ERROR_INVALID_PARAMETER;
1661
1662 r = TABLE_get_dimensions( view, &num_rows, &num_cols );
1663 if ( r != ERROR_SUCCESS )
1664 return r;
1665
1666 if ( row >= num_rows )
1667 return ERROR_FUNCTION_FAILED;
1668
1669 num_rows = tv->table->row_count;
1670 tv->table->row_count--;
1671
1672 /* reset the hash tables */
1673 for (i = 0; i < tv->num_cols; i++)
1674 {
1675 msi_free( tv->columns[i].hash_table );
1676 tv->columns[i].hash_table = NULL;
1677 }
1678
1679 for (i = row + 1; i < num_rows; i++)
1680 {
1681 memcpy(tv->table->data[i - 1], tv->table->data[i], tv->row_size);
1682 tv->table->data_persistent[i - 1] = tv->table->data_persistent[i];
1683 }
1684
1685 msi_free(tv->table->data[num_rows - 1]);
1686
1687 return ERROR_SUCCESS;
1688 }
1689
1690 static UINT msi_table_update(struct tagMSIVIEW *view, MSIRECORD *rec, UINT row)
1691 {
1692 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1693 UINT r, new_row;
1694
1695 /* FIXME: MsiViewFetch should set rec index 0 to some ID that
1696 * sets the fetched record apart from other records
1697 */
1698
1699 if (!tv->table)
1700 return ERROR_INVALID_PARAMETER;
1701
1702 r = msi_table_find_row(tv, rec, &new_row);
1703 if (r != ERROR_SUCCESS)
1704 {
1705 ERR("can't find row to modify\n");
1706 return ERROR_FUNCTION_FAILED;
1707 }
1708
1709 /* the row cannot be changed */
1710 if (row != new_row + 1)
1711 return ERROR_FUNCTION_FAILED;
1712
1713 return TABLE_set_row(view, new_row, rec, (1 << tv->num_cols) - 1);
1714 }
1715
1716 static UINT msi_table_assign(struct tagMSIVIEW *view, MSIRECORD *rec)
1717 {
1718 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1719 UINT r, row;
1720
1721 if (!tv->table)
1722 return ERROR_INVALID_PARAMETER;
1723
1724 r = msi_table_find_row(tv, rec, &row);
1725 if (r == ERROR_SUCCESS)
1726 return TABLE_set_row(view, row, rec, (1 << tv->num_cols) - 1);
1727 else
1728 return TABLE_insert_row( view, rec, -1, FALSE );
1729 }
1730
1731 static UINT modify_delete_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1732 {
1733 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
1734 UINT row, r;
1735
1736 r = msi_table_find_row(tv, rec, &row);
1737 if (r != ERROR_SUCCESS)
1738 return r;
1739
1740 return TABLE_delete_row(view, row);
1741 }
1742
1743 static UINT msi_refresh_record( struct tagMSIVIEW *view, MSIRECORD *rec, UINT row )
1744 {
1745 MSIRECORD *curr;
1746 UINT r, i, count;
1747
1748 r = TABLE_get_row(view, row - 1, &curr);
1749 if (r != ERROR_SUCCESS)
1750 return r;
1751
1752 /* Close the original record */
1753 MSI_CloseRecord(&rec->hdr);
1754
1755 count = MSI_RecordGetFieldCount(rec);
1756 for (i = 0; i < count; i++)
1757 MSI_RecordCopyField(curr, i + 1, rec, i + 1);
1758
1759 msiobj_release(&curr->hdr);
1760 return ERROR_SUCCESS;
1761 }
1762
1763 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1764 MSIRECORD *rec, UINT row)
1765 {
1766 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1767 UINT r;
1768
1769 TRACE("%p %d %p\n", view, eModifyMode, rec );
1770
1771 switch (eModifyMode)
1772 {
1773 case MSIMODIFY_DELETE:
1774 r = modify_delete_row( view, rec );
1775 break;
1776 case MSIMODIFY_VALIDATE_NEW:
1777 r = table_validate_new( tv, rec );
1778 break;
1779
1780 case MSIMODIFY_INSERT:
1781 r = table_validate_new( tv, rec );
1782 if (r != ERROR_SUCCESS)
1783 break;
1784 r = TABLE_insert_row( view, rec, -1, FALSE );
1785 break;
1786
1787 case MSIMODIFY_INSERT_TEMPORARY:
1788 r = table_validate_new( tv, rec );
1789 if (r != ERROR_SUCCESS)
1790 break;
1791 r = TABLE_insert_row( view, rec, -1, TRUE );
1792 break;
1793
1794 case MSIMODIFY_REFRESH:
1795 r = msi_refresh_record( view, rec, row );
1796 break;
1797
1798 case MSIMODIFY_UPDATE:
1799 r = msi_table_update( view, rec, row );
1800 break;
1801
1802 case MSIMODIFY_ASSIGN:
1803 r = msi_table_assign( view, rec );
1804 break;
1805
1806 case MSIMODIFY_REPLACE:
1807 case MSIMODIFY_MERGE:
1808 case MSIMODIFY_VALIDATE:
1809 case MSIMODIFY_VALIDATE_FIELD:
1810 case MSIMODIFY_VALIDATE_DELETE:
1811 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1812 r = ERROR_CALL_NOT_IMPLEMENTED;
1813 break;
1814
1815 default:
1816 r = ERROR_INVALID_DATA;
1817 }
1818
1819 return r;
1820 }
1821
1822 static UINT TABLE_delete( struct tagMSIVIEW *view )
1823 {
1824 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1825
1826 TRACE("%p\n", view );
1827
1828 tv->table = NULL;
1829 tv->columns = NULL;
1830
1831 if (tv->order)
1832 {
1833 msi_free( tv->order->reorder );
1834 msi_free( tv->order );
1835 tv->order = NULL;
1836 }
1837
1838 msi_free( tv );
1839
1840 return ERROR_SUCCESS;
1841 }
1842
1843 static UINT TABLE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
1844 UINT val, UINT *row, MSIITERHANDLE *handle )
1845 {
1846 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1847 const MSICOLUMNHASHENTRY *entry;
1848
1849 TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
1850
1851 if( !tv->table )
1852 return ERROR_INVALID_PARAMETER;
1853
1854 if( (col==0) || (col > tv->num_cols) )
1855 return ERROR_INVALID_PARAMETER;
1856
1857 if( !tv->columns[col-1].hash_table )
1858 {
1859 UINT i;
1860 UINT num_rows = tv->table->row_count;
1861 MSICOLUMNHASHENTRY **hash_table;
1862 MSICOLUMNHASHENTRY *new_entry;
1863
1864 if( tv->columns[col-1].offset >= tv->row_size )
1865 {
1866 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1867 ERR("%p %p\n", tv, tv->columns );
1868 return ERROR_FUNCTION_FAILED;
1869 }
1870
1871 /* allocate contiguous memory for the table and its entries so we
1872 * don't have to do an expensive cleanup */
1873 hash_table = msi_alloc(MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*) +
1874 num_rows * sizeof(MSICOLUMNHASHENTRY));
1875 if (!hash_table)
1876 return ERROR_OUTOFMEMORY;
1877
1878 memset(hash_table, 0, MSITABLE_HASH_TABLE_SIZE * sizeof(MSICOLUMNHASHENTRY*));
1879 tv->columns[col-1].hash_table = hash_table;
1880
1881 new_entry = (MSICOLUMNHASHENTRY *)(hash_table + MSITABLE_HASH_TABLE_SIZE);
1882
1883 for (i = 0; i < num_rows; i++, new_entry++)
1884 {
1885 UINT row_value;
1886
1887 if (view->ops->fetch_int( view, i, col, &row_value ) != ERROR_SUCCESS)
1888 continue;
1889
1890 new_entry->next = NULL;
1891 new_entry->value = row_value;
1892 new_entry->row = i;
1893 if (hash_table[row_value % MSITABLE_HASH_TABLE_SIZE])
1894 {
1895 MSICOLUMNHASHENTRY *prev_entry = hash_table[row_value % MSITABLE_HASH_TABLE_SIZE];
1896 while (prev_entry->next)
1897 prev_entry = prev_entry->next;
1898 prev_entry->next = new_entry;
1899 }
1900 else
1901 hash_table[row_value % MSITABLE_HASH_TABLE_SIZE] = new_entry;
1902 }
1903 }
1904
1905 if( !*handle )
1906 entry = tv->columns[col-1].hash_table[val % MSITABLE_HASH_TABLE_SIZE];
1907 else
1908 entry = (*handle)->next;
1909
1910 while (entry && entry->value != val)
1911 entry = entry->next;
1912
1913 *handle = entry;
1914 if (!entry)
1915 return ERROR_NO_MORE_ITEMS;
1916
1917 *row = entry->row;
1918
1919 return ERROR_SUCCESS;
1920 }
1921
1922 static UINT TABLE_add_ref(struct tagMSIVIEW *view)
1923 {
1924 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1925 UINT i;
1926
1927 TRACE("%p %d\n", view, tv->table->ref_count);
1928
1929 for (i = 0; i < tv->table->col_count; i++)
1930 {
1931 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1932 InterlockedIncrement(&tv->table->colinfo[i].ref_count);
1933 }
1934
1935 return InterlockedIncrement(&tv->table->ref_count);
1936 }
1937
1938 static UINT TABLE_remove_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number)
1939 {
1940 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1941 MSIRECORD *rec = NULL;
1942 MSIVIEW *columns = NULL;
1943 UINT row, r;
1944
1945 rec = MSI_CreateRecord(2);
1946 if (!rec)
1947 return ERROR_OUTOFMEMORY;
1948
1949 MSI_RecordSetStringW(rec, 1, table);
1950 MSI_RecordSetInteger(rec, 2, number);
1951
1952 r = TABLE_CreateView(tv->db, szColumns, &columns);
1953 if (r != ERROR_SUCCESS)
1954 return r;
1955
1956 r = msi_table_find_row((MSITABLEVIEW *)columns, rec, &row);
1957 if (r != ERROR_SUCCESS)
1958 goto done;
1959
1960 r = TABLE_delete_row(columns, row);
1961 if (r != ERROR_SUCCESS)
1962 goto done;
1963
1964 msi_update_table_columns(tv->db, table);
1965
1966 done:
1967 msiobj_release(&rec->hdr);
1968 columns->ops->delete(columns);
1969 return r;
1970 }
1971
1972 static UINT TABLE_release(struct tagMSIVIEW *view)
1973 {
1974 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1975 INT ref = tv->table->ref_count;
1976 UINT i, r;
1977
1978 TRACE("%p %d\n", view, ref);
1979
1980 for (i = 0; i < tv->table->col_count; i++)
1981 {
1982 if (tv->table->colinfo[i].type & MSITYPE_TEMPORARY)
1983 {
1984 ref = InterlockedDecrement(&tv->table->colinfo[i].ref_count);
1985 if (ref == 0)
1986 {
1987 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
1988 tv->table->colinfo[i].number);
1989 if (r != ERROR_SUCCESS)
1990 break;
1991 }
1992 }
1993 }
1994
1995 ref = InterlockedDecrement(&tv->table->ref_count);
1996 if (ref == 0)
1997 {
1998 if (!tv->table->row_count)
1999 {
2000 list_remove(&tv->table->entry);
2001 free_table(tv->table);
2002 TABLE_delete(view);
2003 }
2004 }
2005
2006 return ref;
2007 }
2008
2009 static UINT TABLE_add_column(struct tagMSIVIEW *view, LPCWSTR table, UINT number,
2010 LPCWSTR column, UINT type, BOOL hold)
2011 {
2012 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2013 MSITABLE *msitable;
2014 MSIRECORD *rec;
2015 UINT r, i;
2016
2017 rec = MSI_CreateRecord(4);
2018 if (!rec)
2019 return ERROR_OUTOFMEMORY;
2020
2021 MSI_RecordSetStringW(rec, 1, table);
2022 MSI_RecordSetInteger(rec, 2, number);
2023 MSI_RecordSetStringW(rec, 3, column);
2024 MSI_RecordSetInteger(rec, 4, type);
2025
2026 r = TABLE_insert_row(&tv->view, rec, -1, FALSE);
2027 if (r != ERROR_SUCCESS)
2028 goto done;
2029
2030 msi_update_table_columns(tv->db, table);
2031
2032 if (!hold)
2033 goto done;
2034
2035 msitable = find_cached_table(tv->db, table);
2036 for (i = 0; i < msitable->col_count; i++)
2037 {
2038 if (!lstrcmpW(msitable->colinfo[i].colname, column))
2039 {
2040 InterlockedIncrement(&msitable->colinfo[i].ref_count);
2041 break;
2042 }
2043 }
2044
2045 done:
2046 msiobj_release(&rec->hdr);
2047 return r;
2048 }
2049
2050 static UINT order_add_column(struct tagMSIVIEW *view, MSIORDERINFO *order, LPCWSTR name)
2051 {
2052 UINT n, r, count;
2053 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2054
2055 r = TABLE_get_dimensions(view, NULL, &count);
2056 if (r != ERROR_SUCCESS)
2057 return r;
2058
2059 if (order->num_cols >= count)
2060 return ERROR_FUNCTION_FAILED;
2061
2062 r = VIEW_find_column(view, name, tv->name, &n);
2063 if (r != ERROR_SUCCESS)
2064 return r;
2065
2066 order->cols[order->num_cols] = n;
2067 TRACE("Ordering by column %s (%d)\n", debugstr_w(name), n);
2068
2069 order->num_cols++;
2070
2071 return ERROR_SUCCESS;
2072 }
2073
2074 static UINT order_compare(struct tagMSIVIEW *view, MSIORDERINFO *order,
2075 UINT a, UINT b, UINT *swap)
2076 {
2077 UINT r, i, a_val = 0, b_val = 0;
2078
2079 *swap = 0;
2080 for (i = 0; i < order->num_cols; i++)
2081 {
2082 r = TABLE_fetch_int(view, a, order->cols[i], &a_val);
2083 if (r != ERROR_SUCCESS)
2084 return r;
2085
2086 r = TABLE_fetch_int(view, b, order->cols[i], &b_val);
2087 if (r != ERROR_SUCCESS)
2088 return r;
2089
2090 if (a_val != b_val)
2091 {
2092 if (a_val > b_val)
2093 *swap = 1;
2094 break;
2095 }
2096 }
2097
2098 return ERROR_SUCCESS;
2099 }
2100
2101 static UINT order_mergesort(struct tagMSIVIEW *view, MSIORDERINFO *order,
2102 UINT left, UINT right)
2103 {
2104 UINT r, i, j, temp;
2105 UINT swap = 0, center = (left + right) / 2;
2106 UINT *array = order->reorder;
2107
2108 if (left == right)
2109 return ERROR_SUCCESS;
2110
2111 /* sort the left half */
2112 r = order_mergesort(view, order, left, center);
2113 if (r != ERROR_SUCCESS)
2114 return r;
2115
2116 /* sort the right half */
2117 r = order_mergesort(view, order, center + 1, right);
2118 if (r != ERROR_SUCCESS)
2119 return r;
2120
2121 for (i = left, j = center + 1; (i <= center) && (j <= right); i++)
2122 {
2123 r = order_compare(view, order, array[i], array[j], &swap);
2124 if (r != ERROR_SUCCESS)
2125 return r;
2126
2127 if (swap)
2128 {
2129 temp = array[j];
2130 memmove(&array[i + 1], &array[i], (j - i) * sizeof(UINT));
2131 array[i] = temp;
2132 j++;
2133 center++;
2134 }
2135 }
2136
2137 return ERROR_SUCCESS;
2138 }
2139
2140 static UINT order_verify(struct tagMSIVIEW *view, MSIORDERINFO *order, UINT num_rows)
2141 {
2142 UINT i, swap, r;
2143
2144 for (i = 1; i < num_rows; i++)
2145 {
2146 r = order_compare(view, order, order->reorder[i - 1],
2147 order->reorder[i], &swap);
2148 if (r != ERROR_SUCCESS)
2149 return r;
2150
2151 if (!swap)
2152 continue;
2153
2154 ERR("Bad order! %d\n", i);
2155 return ERROR_FUNCTION_FAILED;
2156 }
2157
2158 return ERROR_SUCCESS;
2159 }
2160
2161 static UINT TABLE_sort(struct tagMSIVIEW *view, column_info *columns)
2162 {
2163 MSITABLEVIEW *tv = (MSITABLEVIEW *)view;
2164 MSIORDERINFO *order;
2165 column_info *ptr;
2166 UINT r, i;
2167 UINT rows, cols;
2168
2169 TRACE("sorting table %s\n", debugstr_w(tv->name));
2170
2171 r = TABLE_get_dimensions(view, &rows, &cols);
2172 if (r != ERROR_SUCCESS)
2173 return r;
2174
2175 if (rows == 0)
2176 return ERROR_SUCCESS;
2177
2178 order = msi_alloc_zero(sizeof(MSIORDERINFO) + sizeof(UINT) * cols);
2179 if (!order)
2180 return ERROR_OUTOFMEMORY;
2181
2182 for (ptr = columns; ptr; ptr = ptr->next)
2183 order_add_column(view, order, ptr->column);
2184
2185 order->reorder = msi_alloc(rows * sizeof(UINT));
2186 if (!order->reorder)
2187 return ERROR_OUTOFMEMORY;
2188
2189 for (i = 0; i < rows; i++)
2190 order->reorder[i] = i;
2191
2192 r = order_mergesort(view, order, 0, rows - 1);
2193 if (r != ERROR_SUCCESS)
2194 return r;
2195
2196 r = order_verify(view, order, rows);
2197 if (r != ERROR_SUCCESS)
2198 return r;
2199
2200 tv->order = order;
2201
2202 return ERROR_SUCCESS;
2203 }
2204
2205 static UINT TABLE_drop(struct tagMSIVIEW *view)
2206 {
2207 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
2208 MSIVIEW *tables = NULL;
2209 MSIRECORD *rec = NULL;
2210 UINT r, row;
2211 INT i;
2212
2213 TRACE("dropping table %s\n", debugstr_w(tv->name));
2214
2215 for (i = tv->table->col_count - 1; i >= 0; i--)
2216 {
2217 r = TABLE_remove_column(view, tv->table->colinfo[i].tablename,
2218 tv->table->colinfo[i].number);
2219 if (r != ERROR_SUCCESS)
2220 return r;
2221 }
2222
2223 rec = MSI_CreateRecord(1);
2224 if (!rec)
2225 return ERROR_OUTOFMEMORY;
2226
2227 MSI_RecordSetStringW(rec, 1, tv->name);
2228
2229 r = TABLE_CreateView(tv->db, szTables, &tables);
2230 if (r != ERROR_SUCCESS)
2231 return r;
2232
2233 r = msi_table_find_row((MSITABLEVIEW *)tables, rec, &row);
2234 if (r != ERROR_SUCCESS)
2235 goto done;
2236
2237 r = TABLE_delete_row(tables, row);
2238 if (r != ERROR_SUCCESS)
2239 goto done;
2240
2241 list_remove(&tv->table->entry);
2242 free_table(tv->table);
2243
2244 done:
2245 msiobj_release(&rec->hdr);
2246 tables->ops->delete(tables);
2247
2248 return r;
2249 }
2250
2251 static const MSIVIEWOPS table_ops =
2252 {
2253 TABLE_fetch_int,
2254 TABLE_fetch_stream,
2255 TABLE_get_row,
2256 TABLE_set_row,
2257 TABLE_insert_row,
2258 TABLE_delete_row,
2259 TABLE_execute,
2260 TABLE_close,
2261 TABLE_get_dimensions,
2262 TABLE_get_column_info,
2263 TABLE_modify,
2264 TABLE_delete,
2265 TABLE_find_matching_rows,
2266 TABLE_add_ref,
2267 TABLE_release,
2268 TABLE_add_column,
2269 TABLE_remove_column,
2270 TABLE_sort,
2271 TABLE_drop,
2272 };
2273
2274 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
2275 {
2276 MSITABLEVIEW *tv ;
2277 UINT r, sz;
2278
2279 static const WCHAR Streams[] = {'_','S','t','r','e','a','m','s',0};
2280 static const WCHAR Storages[] = {'_','S','t','o','r','a','g','e','s',0};
2281
2282 TRACE("%p %s %p\n", db, debugstr_w(name), view );
2283
2284 if ( !lstrcmpW( name, Streams ) )
2285 return STREAMS_CreateView( db, view );
2286 else if ( !lstrcmpW( name, Storages ) )
2287 return STORAGES_CreateView( db, view );
2288
2289 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
2290 tv = msi_alloc_zero( sz );
2291 if( !tv )
2292 return ERROR_FUNCTION_FAILED;
2293
2294 r = get_table( db, name, &tv->table );
2295 if( r != ERROR_SUCCESS )
2296 {
2297 msi_free( tv );
2298 WARN("table not found\n");
2299 return r;
2300 }
2301
2302 TRACE("table %p found with %d columns\n", tv->table, tv->table->col_count);
2303
2304 /* fill the structure */
2305 tv->view.ops = &table_ops;
2306 tv->db = db;
2307 tv->columns = tv->table->colinfo;
2308 tv->num_cols = tv->table->col_count;
2309 tv->row_size = msi_table_get_row_size( db, tv->table->colinfo, tv->table->col_count );
2310
2311 TRACE("%s one row is %d bytes\n", debugstr_w(name), tv->row_size );
2312
2313 *view = (MSIVIEW*) tv;
2314 lstrcpyW( tv->name, name );
2315
2316 return ERROR_SUCCESS;
2317 }
2318
2319 UINT MSI_CommitTables( MSIDATABASE *db )
2320 {
2321 UINT r;
2322 MSITABLE *table = NULL;
2323
2324 TRACE("%p\n",db);
2325
2326 r = msi_save_string_table( db->strings, db->storage );
2327 if( r != ERROR_SUCCESS )
2328 {
2329 WARN("failed to save string table r=%08x\n",r);
2330 return r;
2331 }
2332
2333 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
2334 {
2335 r = save_table( db, table );
2336 if( r != ERROR_SUCCESS )
2337 {
2338 WARN("failed to save table %s (r=%08x)\n",
2339 debugstr_w(table->name), r);
2340 return r;
2341 }
2342 }
2343
2344 /* force everything to reload next time */
2345 free_cached_tables( db );
2346
2347 return ERROR_SUCCESS;
2348 }
2349
2350 MSICONDITION MSI_DatabaseIsTablePersistent( MSIDATABASE *db, LPCWSTR table )
2351 {
2352 MSITABLE *t;
2353 UINT r;
2354
2355 TRACE("%p %s\n", db, debugstr_w(table));
2356
2357 if (!table)
2358 return MSICONDITION_ERROR;
2359
2360 r = get_table( db, table, &t );
2361 if (r != ERROR_SUCCESS)
2362 return MSICONDITION_NONE;
2363
2364 return t->persistent;
2365 }
2366
2367 static UINT read_raw_int(const BYTE *data, UINT col, UINT bytes)
2368 {
2369 UINT ret = 0, i;
2370
2371 for (i = 0; i < bytes; i++)
2372 ret += (data[col + i] << i * 8);
2373
2374 return ret;
2375 }
2376
2377 static UINT msi_record_encoded_stream_name( const MSITABLEVIEW *tv, MSIRECORD *rec, LPWSTR *pstname )
2378 {
2379 LPWSTR stname = NULL, sval, p;
2380 DWORD len;
2381 UINT i, r;
2382
2383 TRACE("%p %p\n", tv, rec);
2384
2385 len = lstrlenW( tv->name ) + 1;
2386 stname = msi_alloc( len*sizeof(WCHAR) );
2387 if ( !stname )
2388 {
2389 r = ERROR_OUTOFMEMORY;
2390 goto err;
2391 }
2392
2393 lstrcpyW( stname, tv->name );
2394
2395 for ( i = 0; i < tv->num_cols; i++ )
2396 {
2397 if ( tv->columns[i].type & MSITYPE_KEY )
2398 {
2399 sval = msi_dup_record_field( rec, i + 1 );
2400 if ( !sval )
2401 {
2402 r = ERROR_OUTOFMEMORY;
2403 goto err;
2404 }
2405
2406 len += lstrlenW( szDot ) + lstrlenW ( sval );
2407 p = msi_realloc ( stname, len*sizeof(WCHAR) );
2408 if ( !p )
2409 {
2410 r = ERROR_OUTOFMEMORY;
2411 goto err;
2412 }
2413 stname = p;
2414
2415 lstrcatW( stname, szDot );
2416 lstrcatW( stname, sval );
2417
2418 msi_free( sval );
2419 }
2420 else
2421 continue;
2422 }
2423
2424 *pstname = encode_streamname( FALSE, stname );
2425 msi_free( stname );
2426
2427 return ERROR_SUCCESS;
2428
2429 err:
2430 msi_free ( stname );
2431 *pstname = NULL;
2432 return r;
2433 }
2434
2435 static MSIRECORD *msi_get_transform_record( const MSITABLEVIEW *tv, const string_table *st,
2436 IStorage *stg,
2437 const BYTE *rawdata, UINT bytes_per_strref )
2438 {
2439 UINT i, val, ofs = 0;
2440 USHORT mask;
2441 MSICOLUMNINFO *columns = tv->columns;
2442 MSIRECORD *rec;
2443
2444 mask = rawdata[0] | (rawdata[1] << 8);
2445 rawdata += 2;
2446
2447 rec = MSI_CreateRecord( tv->num_cols );
2448 if( !rec )
2449 return rec;
2450
2451 TRACE("row ->\n");
2452 for( i=0; i<tv->num_cols; i++ )
2453 {
2454 if ( (mask&1) && (i>=(mask>>8)) )
2455 break;
2456 /* all keys must be present */
2457 if ( (~mask&1) && (~columns[i].type & MSITYPE_KEY) && ((1<<i) & ~mask) )
2458 continue;
2459
2460 if( MSITYPE_IS_BINARY(tv->columns[i].type) )
2461 {
2462 LPWSTR encname;
2463 IStream *stm = NULL;
2464 UINT r;
2465
2466 ofs += bytes_per_column( tv->db, &columns[i] );
2467
2468 r = msi_record_encoded_stream_name( tv, rec, &encname );
2469 if ( r != ERROR_SUCCESS )
2470 return NULL;
2471
2472 r = IStorage_OpenStream( stg, encname, NULL,
2473 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm );
2474 msi_free( encname );
2475 if ( r != ERROR_SUCCESS )
2476 return NULL;
2477
2478 MSI_RecordSetStream( rec, i+1, stm );
2479 TRACE(" field %d [%s]\n", i+1, debugstr_w(encname));
2480 }
2481 else if( columns[i].type & MSITYPE_STRING )
2482 {
2483 LPCWSTR sval;
2484
2485 val = read_raw_int(rawdata, ofs, bytes_per_strref);
2486 sval = msi_string_lookup_id( st, val );
2487 MSI_RecordSetStringW( rec, i+1, sval );
2488 TRACE(" field %d [%s]\n", i+1, debugstr_w(sval));
2489 ofs += bytes_per_strref;
2490 }
2491 else
2492 {
2493 UINT n = bytes_per_column( tv->db, &columns[i] );
2494 switch( n )
2495 {
2496 case 2:
2497 val = read_raw_int(rawdata, ofs, n);
2498 if (val)
2499 MSI_RecordSetInteger( rec, i+1, val-0x8000 );
2500 TRACE(" field %d [0x%04x]\n", i+1, val );
2501 break;
2502 case 4:
2503 val = read_raw_int(rawdata, ofs, n);
2504 if (val)
2505 MSI_RecordSetInteger( rec, i+1, val^0x80000000 );
2506 TRACE(" field %d [0x%08x]\n", i+1, val );
2507 break;
2508 default:
2509 ERR("oops - unknown column width %d\n", n);
2510 break;
2511 }
2512 ofs += n;
2513 }
2514 }
2515 return rec;
2516 }
2517
2518 static void dump_record( MSIRECORD *rec )
2519 {
2520 UINT i, n;
2521
2522 n = MSI_RecordGetFieldCount( rec );
2523 for( i=1; i<=n; i++ )
2524 {
2525 LPCWSTR sval = MSI_RecordGetString( rec, i );
2526
2527 if( MSI_RecordIsNull( rec, i ) )
2528 TRACE("row -> []\n");
2529 else if( (sval = MSI_RecordGetString( rec, i )) )
2530 TRACE("row -> [%s]\n", debugstr_w(sval));
2531 else
2532 TRACE("row -> [0x%08x]\n", MSI_RecordGetInteger( rec, i ) );
2533 }
2534 }
2535
2536 static void dump_table( const string_table *st, const USHORT *rawdata, UINT rawsize )
2537 {
2538 LPCWSTR sval;
2539 UINT i;
2540
2541 for( i=0; i<(rawsize/2); i++ )
2542 {
2543 sval = msi_string_lookup_id( st, rawdata[i] );
2544 MESSAGE(" %04x %s\n", rawdata[i], debugstr_w(sval) );
2545 }
2546 }
2547
2548 static UINT* msi_record_to_row( const MSITABLEVIEW *tv, MSIRECORD *rec )
2549 {
2550 LPCWSTR str;
2551 UINT i, r, *data;
2552
2553 data = msi_alloc( tv->num_cols *sizeof (UINT) );
2554 for( i=0; i<tv->num_cols; i++ )
2555 {
2556 data[i] = 0;
2557
2558 if ( ~tv->columns[i].type & MSITYPE_KEY )
2559 continue;
2560
2561 /* turn the transform column value into a row value */
2562 if ( ( tv->columns[i].type & MSITYPE_STRING ) &&
2563 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2564 {
2565 str = MSI_RecordGetString( rec, i+1 );
2566 r = msi_string2idW( tv->db->strings, str, &data[i] );
2567
2568 /* if there's no matching string in the string table,
2569 these keys can't match any record, so fail now. */
2570 if( ERROR_SUCCESS != r )
2571 {
2572 msi_free( data );
2573 return NULL;
2574 }
2575 }
2576 else
2577 {
2578 data[i] = MSI_RecordGetInteger( rec, i+1 );
2579
2580 if (data[i] == MSI_NULL_INTEGER)
2581 data[i] = 0;
2582 else if ((tv->columns[i].type&0xff) == 2)
2583 data[i] += 0x8000;
2584 else
2585 data[i] += 0x80000000;
2586 }
2587 }
2588 return data;
2589 }
2590
2591 static UINT msi_row_matches( MSITABLEVIEW *tv, UINT row, const UINT *data )
2592 {
2593 UINT i, r, x, ret = ERROR_FUNCTION_FAILED;
2594
2595 for( i=0; i<tv->num_cols; i++ )
2596 {
2597 if ( ~tv->columns[i].type & MSITYPE_KEY )
2598 continue;
2599
2600 /* turn the transform column value into a row value */
2601 r = TABLE_fetch_int( &tv->view, row, i+1, &x );
2602 if ( r != ERROR_SUCCESS )
2603 {
2604 ERR("TABLE_fetch_int shouldn't fail here\n");
2605 break;
2606 }
2607
2608 /* if this key matches, move to the next column */
2609 if ( x != data[i] )
2610 {
2611 ret = ERROR_FUNCTION_FAILED;
2612 break;
2613 }
2614
2615 ret = ERROR_SUCCESS;
2616 }
2617
2618 return ret;
2619 }
2620
2621 static UINT msi_table_find_row( MSITABLEVIEW *tv, MSIRECORD *rec, UINT *row )
2622 {
2623 UINT i, r = ERROR_FUNCTION_FAILED, *data;
2624
2625 data = msi_record_to_row( tv, rec );
2626 if( !data )
2627 return r;
2628 for( i = 0; i < tv->table->row_count; i++ )
2629 {
2630 r = msi_row_matches( tv, i, data );
2631 if( r == ERROR_SUCCESS )
2632 {
2633 *row = i;
2634 break;
2635 }
2636 }
2637 msi_free( data );
2638 return r;
2639 }
2640
2641 typedef struct
2642 {
2643 struct list entry;
2644 LPWSTR name;
2645 } TRANSFORMDATA;
2646
2647 static UINT msi_table_load_transform( MSIDATABASE *db, IStorage *stg,
2648 string_table *st, TRANSFORMDATA *transform,
2649 UINT bytes_per_strref )
2650 {
2651 UINT rawsize = 0;
2652 BYTE *rawdata = NULL;
2653 MSITABLEVIEW *tv = NULL;
2654 UINT r, n, sz, i, mask;
2655 MSIRECORD *rec = NULL;
2656 UINT colcol = 0;
2657 WCHAR coltable[32];
2658 LPWSTR name;
2659
2660 if (!transform)
2661 return ERROR_SUCCESS;
2662
2663 name = transform->name;
2664
2665 coltable[0] = 0;
2666 TRACE("%p %p %p %s\n", db, stg, st, debugstr_w(name) );
2667
2668 /* read the transform data */
2669 read_stream_data( stg, name, TRUE, &rawdata, &rawsize );
2670 if ( !rawdata )
2671 {
2672 TRACE("table %s empty\n", debugstr_w(name) );
2673 return ERROR_INVALID_TABLE;
2674 }
2675
2676 /* create a table view */
2677 r = TABLE_CreateView( db, name, (MSIVIEW**) &tv );
2678 if( r != ERROR_SUCCESS )
2679 goto err;
2680
2681 r = tv->view.ops->execute( &tv->view, NULL );
2682 if( r != ERROR_SUCCESS )
2683 goto err;
2684
2685 TRACE("name = %s columns = %u row_size = %u raw size = %u\n",
2686 debugstr_w(name), tv->num_cols, tv->row_size, rawsize );
2687
2688 /* interpret the data */
2689 r = ERROR_SUCCESS;
2690 for( n=0; n < rawsize; )
2691 {
2692 mask = rawdata[n] | (rawdata[n+1] << 8);
2693
2694 if (mask&1)
2695 {
2696 /*
2697 * if the low bit is set, columns are continuous and
2698 * the number of columns is specified in the high byte
2699 */
2700 sz = 2;
2701 for( i=0; i<tv->num_cols; i++ )
2702 {
2703 if( (tv->columns[i].type & MSITYPE_STRING) &&
2704 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2705 sz += bytes_per_strref;
2706 else
2707 sz += bytes_per_column( tv->db, &tv->columns[i] );
2708 }
2709 }
2710 else
2711 {
2712 /*
2713 * If the low bit is not set, mask is a bitmask.
2714 * Excepting for key fields, which are always present,
2715 * each bit indicates that a field is present in the transform record.
2716 *
2717 * mask == 0 is a special case ... only the keys will be present
2718 * and it means that this row should be deleted.
2719 */
2720 sz = 2;
2721 for( i=0; i<tv->num_cols; i++ )
2722 {
2723 if( (tv->columns[i].type & MSITYPE_KEY) || ((1<<i)&mask))
2724 {
2725 if( (tv->columns[i].type & MSITYPE_STRING) &&
2726 ! MSITYPE_IS_BINARY(tv->columns[i].type) )
2727 sz += bytes_per_strref;
2728 else
2729 sz += bytes_per_column( tv->db, &tv->columns[i] );
2730 }
2731 }
2732 }
2733
2734 /* check we didn't run of the end of the table */
2735 if ( (n+sz) > rawsize )
2736 {
2737 ERR("borked.\n");
2738 dump_table( st, (USHORT *)rawdata, rawsize );
2739 break;
2740 }
2741
2742 rec = msi_get_transform_record( tv, st, stg, &rawdata[n], bytes_per_strref );
2743 if (rec)
2744 {
2745 if ( mask & 1 )
2746 {
2747 WCHAR table[32];
2748 DWORD sz = 32;
2749 UINT number = MSI_NULL_INTEGER;
2750
2751 TRACE("inserting record\n");
2752
2753 if (!lstrcmpW(name, szColumns))
2754 {
2755 MSI_RecordGetStringW( rec, 1, table, &sz );
2756 number = MSI_RecordGetInteger( rec, 2 );
2757
2758 /*
2759 * Native msi seems writes nul into the Number (2nd) column of
2760 * the _Columns table, only when the columns are from a new table
2761 */
2762 if ( number == MSI_NULL_INTEGER )
2763 {
2764 /* reset the column number on a new table */
2765 if ( lstrcmpW(coltable, table) )
2766 {
2767 colcol = 0;
2768 lstrcpyW( coltable, table );
2769 }
2770
2771 /* fix nul column numbers */
2772 MSI_RecordSetInteger( rec, 2, ++colcol );
2773 }
2774 }
2775
2776 r = TABLE_insert_row( &tv->view, rec, -1, FALSE );
2777 if (r != ERROR_SUCCESS)
2778 WARN("insert row failed\n");
2779
2780 if ( number != MSI_NULL_INTEGER && !lstrcmpW(name, szColumns) )
2781 msi_update_table_columns( db, table );
2782 }
2783 else
2784 {
2785 UINT row = 0;
2786
2787 r = msi_table_find_row( tv, rec, &row );
2788 if (r != ERROR_SUCCESS)
2789 WARN("no matching row to transform\n");
2790 else if ( mask )
2791 {
2792 TRACE("modifying row [%d]:\n", row);
2793 TABLE_set_row( &tv->view, row, rec, mask );
2794 }
2795 else
2796 {
2797 TRACE("deleting row [%d]:\n", row);
2798 TABLE_delete_row( &tv->view, row );
2799 }
2800 }
2801 if( TRACE_ON(msidb) ) dump_record( rec );
2802 msiobj_release( &rec->hdr );
2803 }
2804
2805 n += sz;
2806 }
2807
2808 err:
2809 /* no need to free the table, it's associated with the database */
2810 msi_free( rawdata );
2811 if( tv )
2812 tv->view.ops->delete( &tv->view );
2813
2814 return ERROR_SUCCESS;
2815 }
2816
2817 /*
2818 * msi_table_apply_transform
2819 *
2820 * Enumerate the table transforms in a transform storage and apply each one.
2821 */
2822 UINT msi_table_apply_transform( MSIDATABASE *db, IStorage *stg )
2823 {
2824 struct list transforms;
2825 IEnumSTATSTG *stgenum = NULL;
2826 TRANSFORMDATA *transform;
2827 TRANSFORMDATA *tables = NULL, *columns = NULL;
2828 HRESULT r;
2829 STATSTG stat;
2830 string_table *strings;
2831 UINT ret = ERROR_FUNCTION_FAILED;
2832 UINT bytes_per_strref;
2833
2834 TRACE("%p %p\n", db, stg );
2835
2836 strings = msi_load_string_table( stg, &bytes_per_strref );
2837 if( !strings )
2838 goto end;
2839
2840 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
2841 if( FAILED( r ) )
2842 goto end;
2843
2844 list_init(&transforms);
2845
2846 while ( TRUE )
2847 {
2848 MSITABLEVIEW *tv = NULL;
2849 WCHAR name[0x40];
2850 ULONG count = 0;
2851
2852 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
2853 if ( FAILED( r ) || !count )
2854 break;
2855
2856 decode_streamname( stat.pwcsName, name );
2857 CoTaskMemFree( stat.pwcsName );
2858 if ( name[0] != 0x4840 )
2859 continue;
2860
2861 if ( !lstrcmpW( name+1, szStringPool ) ||
2862 !lstrcmpW( name+1, szStringData ) )
2863 continue;
2864
2865 transform = msi_alloc_zero( sizeof(TRANSFORMDATA) );
2866 if ( !transform )
2867 break;
2868
2869 list_add_tail( &transforms, &transform->entry );
2870
2871 transform->name = strdupW( name + 1 );
2872
2873 if ( !lstrcmpW( transform->name, szTables ) )
2874 tables = transform;
2875 else if (!lstrcmpW( transform->name, szColumns ) )
2876 columns = transform;
2877
2878 TRACE("transform contains stream %s\n", debugstr_w(name));
2879
2880 /* load the table */
2881 r = TABLE_CreateView( db, transform->name, (MSIVIEW**) &tv );
2882 if( r != ERROR_SUCCESS )
2883 continue;
2884
2885 r = tv->view.ops->execute( &tv->view, NULL );
2886 if( r != ERROR_SUCCESS )
2887 {
2888 tv->view.ops->delete( &tv->view );
2889 continue;
2890 }
2891
2892 tv->view.ops->delete( &tv->view );
2893 }
2894
2895 /*
2896 * Apply _Tables and _Columns transforms first so that
2897 * the table metadata is correct, and empty tables exist.
2898 */
2899 ret = msi_table_load_transform( db, stg, strings, tables, bytes_per_strref );
2900 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2901 goto end;
2902
2903 ret = msi_table_load_transform( db, stg, strings, columns, bytes_per_strref );
2904 if (ret != ERROR_SUCCESS && ret != ERROR_INVALID_TABLE)
2905 goto end;
2906
2907 ret = ERROR_SUCCESS;
2908
2909 while ( !list_empty( &transforms ) )
2910 {
2911 transform = LIST_ENTRY( list_head( &transforms ), TRANSFORMDATA, entry );
2912
2913 if ( lstrcmpW( transform->name, szColumns ) &&
2914 lstrcmpW( transform->name, szTables ) &&
2915 ret == ERROR_SUCCESS )
2916 {
2917 ret = msi_table_load_transform( db, stg, strings, transform, bytes_per_strref );
2918 }
2919
2920 list_remove( &transform->entry );
2921 msi_free( transform->name );
2922 msi_free( transform );
2923 }
2924
2925 if ( ret == ERROR_SUCCESS )
2926 append_storage_to_db( db, stg );
2927
2928 end:
2929 if ( stgenum )
2930 IEnumSTATSTG_Release( stgenum );
2931 if ( strings )
2932 msi_destroy_stringtable( strings );
2933
2934 return ret;
2935 }