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