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