Fix _setmbcp behavior for unreal codepages.
[wine/multimedia.git] / dlls / msi / table.c
blob60fd269be4ce47aa753ec02363353002a462ff8e
1 /*
2 * Implementation of the Microsoft Installer (msi.dll)
4 * Copyright 2002-2004 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdarg.h>
23 #define COBJMACROS
24 #define NONAMELESSUNION
25 #define NONAMELESSSTRUCT
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "wine/debug.h"
31 #include "msi.h"
32 #include "msiquery.h"
33 #include "objbase.h"
34 #include "objidl.h"
35 #include "msipriv.h"
36 #include "winnls.h"
38 #include "query.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(msi);
42 typedef struct tagMSICOLUMNINFO
44 LPWSTR tablename;
45 UINT number;
46 LPWSTR colname;
47 UINT type;
48 UINT offset;
49 } MSICOLUMNINFO;
51 struct tagMSITABLE
53 USHORT **data;
54 UINT ref_count;
55 UINT row_count;
56 struct tagMSITABLE *next;
57 struct tagMSITABLE *prev;
58 WCHAR name[1];
61 #define MAX_STREAM_NAME 0x1f
63 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
64 MSICOLUMNINFO **pcols, UINT *pcount );
65 static UINT get_tablecolumns( MSIDATABASE *db,
66 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
68 static inline UINT bytes_per_column( MSICOLUMNINFO *col )
70 if( col->type & MSITYPE_STRING )
71 return 2;
72 if( (col->type & 0xff) > 4 )
73 ERR("Invalid column size!\n");
74 return col->type & 0xff;
77 static int utf2mime(int x)
79 if( (x>='0') && (x<='9') )
80 return x-'0';
81 if( (x>='A') && (x<='Z') )
82 return x-'A'+10;
83 if( (x>='a') && (x<='z') )
84 return x-'a'+10+26;
85 if( x=='.' )
86 return 10+26+26;
87 if( x=='_' )
88 return 10+26+26+1;
89 return -1;
92 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
94 DWORD count = MAX_STREAM_NAME;
95 DWORD ch, next;
96 LPWSTR out, p;
98 if( !bTable )
99 count = lstrlenW( in )+2;
100 out = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
101 p = out;
103 if( bTable )
105 *p++ = 0x4840;
106 count --;
108 while( count -- )
110 ch = *in++;
111 if( !ch )
113 *p = ch;
114 return out;
116 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
118 ch = utf2mime(ch) + 0x4800;
119 next = *in;
120 if( next && (next<0x80) )
122 next = utf2mime(next);
123 if( next >= 0 )
125 next += 0x3ffffc0;
126 ch += (next<<6);
127 in++;
131 *p++ = ch;
133 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
134 HeapFree( GetProcessHeap(), 0, out );
135 return NULL;
138 static int mime2utf(int x)
140 if( x<10 )
141 return x + '0';
142 if( x<(10+26))
143 return x - 10 + 'A';
144 if( x<(10+26+26))
145 return x - 10 - 26 + 'a';
146 if( x == (10+26+26) )
147 return '.';
148 return '_';
151 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
153 WCHAR ch;
154 DWORD count = 0;
156 while ( (ch = *in++) )
158 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
160 if( ch >= 0x4800 )
161 ch = mime2utf(ch-0x4800);
162 else
164 ch -= 0x3800;
165 *out++ = mime2utf(ch&0x3f);
166 count++;
167 ch = mime2utf((ch>>6)&0x3f);
170 *out++ = ch;
171 count++;
173 *out = 0;
174 return count;
177 void enum_stream_names( IStorage *stg )
179 IEnumSTATSTG *stgenum = NULL;
180 HRESULT r;
181 STATSTG stat;
182 ULONG n, count;
183 WCHAR name[0x40];
185 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
186 if( FAILED( r ) )
187 return;
189 n = 0;
190 while( 1 )
192 count = 0;
193 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
194 if( FAILED( r ) || !count )
195 break;
196 decode_streamname( stat.pwcsName, name );
197 TRACE("stream %2ld -> %s %s\n", n,
198 debugstr_w(stat.pwcsName), debugstr_w(name) );
199 n++;
202 IEnumSTATSTG_Release( stgenum );
205 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
206 USHORT **pdata, UINT *psz )
208 HRESULT r;
209 UINT ret = ERROR_FUNCTION_FAILED;
210 VOID *data;
211 ULONG sz, count;
212 IStream *stm = NULL;
213 STATSTG stat;
214 LPWSTR encname;
216 encname = encode_streamname(TRUE, stname);
218 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
220 r = IStorage_OpenStream(stg, encname, NULL,
221 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
222 HeapFree( GetProcessHeap(), 0, encname );
223 if( FAILED( r ) )
225 WARN("open stream failed r = %08lx - empty table?\n",r);
226 return ret;
229 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
230 if( FAILED( r ) )
232 WARN("open stream failed r = %08lx!\n",r);
233 goto end;
236 if( stat.cbSize.QuadPart >> 32 )
238 WARN("Too big!\n");
239 goto end;
242 sz = stat.cbSize.QuadPart;
243 data = HeapAlloc( GetProcessHeap(), 0, sz );
244 if( !data )
246 WARN("couldn't allocate memory r=%08lx!\n",r);
247 ret = ERROR_NOT_ENOUGH_MEMORY;
248 goto end;
251 r = IStream_Read(stm, data, sz, &count );
252 if( FAILED( r ) || ( count != sz ) )
254 HeapFree( GetProcessHeap(), 0, data );
255 WARN("read stream failed r = %08lx!\n",r);
256 goto end;
259 *pdata = data;
260 *psz = sz;
261 ret = ERROR_SUCCESS;
263 end:
264 IStream_Release( stm );
266 return ret;
269 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
271 LPWSTR encname;
272 HRESULT r;
274 encname = encode_streamname(FALSE, stname);
276 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
278 r = IStorage_OpenStream(db->storage, encname, NULL,
279 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
280 HeapFree( GetProcessHeap(), 0, encname );
281 if( FAILED( r ) )
283 WARN("open stream failed r = %08lx - empty table?\n",r);
284 return ERROR_FUNCTION_FAILED;
287 return ERROR_SUCCESS;
290 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
291 USHORT **pdata, UINT *psz )
293 HRESULT r;
294 UINT ret = ERROR_FUNCTION_FAILED;
295 VOID *data;
296 ULONG sz, count;
297 IStream *stm = NULL;
298 STATSTG stat;
300 r = db_get_raw_stream( db, stname, &stm );
301 if( r != ERROR_SUCCESS)
302 return ret;
303 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
304 if( FAILED( r ) )
306 WARN("open stream failed r = %08lx!\n",r);
307 goto end;
310 if( stat.cbSize.QuadPart >> 32 )
312 WARN("Too big!\n");
313 goto end;
316 sz = stat.cbSize.QuadPart;
317 data = HeapAlloc( GetProcessHeap(), 0, sz );
318 if( !data )
320 WARN("couldn't allocate memory r=%08lx!\n",r);
321 ret = ERROR_NOT_ENOUGH_MEMORY;
322 goto end;
325 r = IStream_Read(stm, data, sz, &count );
326 if( FAILED( r ) || ( count != sz ) )
328 HeapFree( GetProcessHeap(), 0, data );
329 WARN("read stream failed r = %08lx!\n",r);
330 goto end;
333 *pdata = data;
334 *psz = sz;
335 ret = ERROR_SUCCESS;
337 end:
338 IStream_Release( stm );
340 return ret;
343 static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
344 LPVOID data, UINT sz )
346 HRESULT r;
347 UINT ret = ERROR_FUNCTION_FAILED;
348 ULONG count;
349 IStream *stm = NULL;
350 ULARGE_INTEGER size;
351 LARGE_INTEGER pos;
352 LPWSTR encname;
354 encname = encode_streamname(TRUE, stname );
355 r = IStorage_OpenStream( stg, encname, NULL,
356 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
357 if( FAILED(r) )
359 r = IStorage_CreateStream( stg, encname,
360 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
362 HeapFree( GetProcessHeap(), 0, encname );
363 if( FAILED( r ) )
365 WARN("open stream failed r = %08lx\n",r);
366 return ret;
369 size.QuadPart = sz;
370 r = IStream_SetSize( stm, size );
371 if( FAILED( r ) )
373 WARN("Failed to SetSize\n");
374 goto end;
377 pos.QuadPart = 0;
378 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
379 if( FAILED( r ) )
381 WARN("Failed to Seek\n");
382 goto end;
385 r = IStream_Write(stm, data, sz, &count );
386 if( FAILED( r ) || ( count != sz ) )
388 WARN("Failed to Write\n");
389 goto end;
392 ret = ERROR_SUCCESS;
394 end:
395 IStream_Release( stm );
397 return ret;
400 static UINT read_table_from_storage( MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
402 MSITABLE *t;
403 USHORT *rawdata = NULL;
404 UINT rawsize = 0, r, i, j, row_size = 0, num_cols = 0;
405 MSICOLUMNINFO *cols, *last_col;
407 TRACE("%s\n",debugstr_w(name));
409 /* nonexistent tables should be interpreted as empty tables */
410 t = HeapAlloc( GetProcessHeap(), 0,
411 sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
412 if( !t )
413 return ERROR_NOT_ENOUGH_MEMORY;
415 r = table_get_column_info( db, name, &cols, &num_cols );
416 if( r != ERROR_SUCCESS )
418 HeapFree( GetProcessHeap(), 0, t );
419 return r;
421 last_col = &cols[num_cols-1];
422 row_size = last_col->offset + bytes_per_column( last_col );
424 t->row_count = 0;
425 t->data = NULL;
426 lstrcpyW( t->name, name );
427 t->ref_count = 1;
428 *ptable = t;
430 /* if we can't read the table, just assume that it's empty */
431 read_stream_data( db->storage, name, &rawdata, &rawsize );
432 if( !rawdata )
433 return ERROR_SUCCESS;
435 TRACE("Read %d bytes\n", rawsize );
437 if( rawsize % row_size )
439 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
440 return ERROR_FUNCTION_FAILED;
443 t->row_count = rawsize / row_size;
444 t->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
445 t->row_count * sizeof (USHORT*) );
446 if( !t->data )
447 return ERROR_NOT_ENOUGH_MEMORY; /* FIXME: memory leak */
449 /* transpose all the data */
450 TRACE("Transposing data from %d columns\n", t->row_count );
451 for( i=0; i<t->row_count; i++ )
453 t->data[i] = HeapAlloc( GetProcessHeap(), 0, row_size );
454 if( !t->data[i] )
455 return ERROR_NOT_ENOUGH_MEMORY; /* FIXME: memory leak */
456 for( j=0; j<num_cols; j++ )
458 UINT ofs = cols[j].offset/2;
459 UINT n = bytes_per_column( &cols[j] );
461 switch( n )
463 case 2:
464 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
465 break;
466 case 4:
467 t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
468 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
469 break;
470 default:
471 ERR("oops - unknown column width %d\n", n);
472 return ERROR_FUNCTION_FAILED;
477 HeapFree( GetProcessHeap(), 0, cols );
478 HeapFree( GetProcessHeap(), 0, rawdata );
480 return ERROR_SUCCESS;
483 /* add this table to the list of cached tables in the database */
484 void add_table(MSIDATABASE *db, MSITABLE *table)
486 table->next = db->first_table;
487 table->prev = NULL;
488 if( db->first_table )
489 db->first_table->prev = table;
490 else
491 db->last_table = table;
492 db->first_table = table;
495 /* remove from the list of cached tables */
496 void remove_table( MSIDATABASE *db, MSITABLE *table )
498 if( table->next )
499 table->next->prev = table->prev;
500 else
501 db->last_table = table->prev;
502 if( table->prev )
503 table->prev->next = table->next;
504 else
505 db->first_table = table->next;
506 table->next = NULL;
507 table->prev = NULL;
510 static void release_table( MSIDATABASE *db, MSITABLE *table )
512 if( !table->ref_count )
513 ERR("Trying to destroy table with refcount 0\n");
514 table->ref_count --;
515 if( !table->ref_count )
517 remove_table( db, table );
518 HeapFree( GetProcessHeap(), 0, table->data );
519 HeapFree( GetProcessHeap(), 0, table );
520 TRACE("Destroyed table %s\n", debugstr_w(table->name));
524 void free_cached_tables( MSIDATABASE *db )
526 while( db->first_table )
528 MSITABLE *t = db->first_table;
530 if ( --t->ref_count )
531 ERR("table ref count not zero for %s\n", debugstr_w(t->name));
532 remove_table( db, t );
533 HeapFree( GetProcessHeap(), 0, t->data );
534 HeapFree( GetProcessHeap(), 0, t );
538 UINT find_cached_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
540 MSITABLE *t;
542 for( t = db->first_table; t; t=t->next )
544 if( !lstrcmpW( name, t->name ) )
546 *ptable = t;
547 return ERROR_SUCCESS;
551 return ERROR_FUNCTION_FAILED;
554 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
556 UINT r, column_count;
557 MSICOLUMNINFO *columns;
559 /* get the number of columns in this table */
560 column_count = 0;
561 r = get_tablecolumns( db, name, NULL, &column_count );
562 if( r != ERROR_SUCCESS )
563 return r;
565 /* if there's no columns, there's no table */
566 if( column_count == 0 )
567 return ERROR_INVALID_PARAMETER;
569 TRACE("Table %s found\n", debugstr_w(name) );
571 columns = HeapAlloc( GetProcessHeap(), 0, column_count*sizeof (MSICOLUMNINFO));
572 if( !columns )
573 return ERROR_FUNCTION_FAILED;
575 r = get_tablecolumns( db, name, columns, &column_count );
576 if( r != ERROR_SUCCESS )
578 HeapFree( GetProcessHeap(), 0, columns );
579 return ERROR_FUNCTION_FAILED;
582 *pcols = columns;
583 *pcount = column_count;
585 return r;
588 UINT get_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
590 UINT r;
592 *ptable = NULL;
594 /* first, see if the table is cached */
595 r = find_cached_table( db, name, ptable );
596 if( r == ERROR_SUCCESS )
598 (*ptable)->ref_count++;
599 return r;
602 r = read_table_from_storage( db, name, ptable );
603 if( r != ERROR_SUCCESS )
604 return r;
606 /* add the table to the list */
607 add_table( db, *ptable );
608 (*ptable)->ref_count++;
610 return ERROR_SUCCESS;
613 static UINT save_table( MSIDATABASE *db, MSITABLE *t )
615 USHORT *rawdata = NULL, *p;
616 UINT rawsize, r, i, j, row_size, num_cols = 0;
617 MSICOLUMNINFO *cols, *last_col;
619 TRACE("Saving %s\n", debugstr_w( t->name ) );
621 r = table_get_column_info( db, t->name, &cols, &num_cols );
622 if( r != ERROR_SUCCESS )
623 return r;
625 last_col = &cols[num_cols-1];
626 row_size = last_col->offset + bytes_per_column( last_col );
628 rawsize = t->row_count * row_size;
629 rawdata = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, rawsize );
630 if( !rawdata )
631 return ERROR_NOT_ENOUGH_MEMORY;
633 p = rawdata;
634 for( i=0; i<num_cols; i++ )
636 for( j=0; j<t->row_count; j++ )
638 UINT offset = cols[i].offset;
640 *p++ = t->data[j][offset/2];
641 if( 4 == bytes_per_column( &cols[i] ) )
642 *p++ = t->data[j][offset/2+1];
646 TRACE("writing %d bytes\n", rawsize);
647 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
649 HeapFree( GetProcessHeap(), 0, rawdata );
651 return r;
654 HRESULT init_string_table( IStorage *stg )
656 HRESULT r;
657 static const WCHAR szStringData[] = {
658 '_','S','t','r','i','n','g','D','a','t','a',0 };
659 static const WCHAR szStringPool[] = {
660 '_','S','t','r','i','n','g','P','o','o','l',0 };
661 USHORT zero[2] = { 0, 0 };
662 ULONG count = 0;
663 IStream *stm = NULL;
664 LPWSTR encname;
666 encname = encode_streamname(TRUE, szStringPool );
668 /* create the StringPool stream... add the zero string to it*/
669 r = IStorage_CreateStream( stg, encname,
670 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
671 HeapFree( GetProcessHeap(), 0, encname );
672 if( r )
674 TRACE("Failed\n");
675 return r;
678 r = IStream_Write(stm, zero, sizeof zero, &count );
679 IStream_Release( stm );
681 if( FAILED( r ) || ( count != sizeof zero ) )
683 TRACE("Failed\n");
684 return E_FAIL;
687 /* create the StringData stream... make it zero length */
688 encname = encode_streamname(TRUE, szStringData );
689 r = IStorage_CreateStream( stg, encname,
690 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
691 HeapFree( GetProcessHeap(), 0, encname );
692 if( r )
694 TRACE("Failed\n");
695 return E_FAIL;
697 IStream_Release( stm );
699 return r;
702 UINT load_string_table( MSIDATABASE *db )
704 CHAR *data;
705 USHORT *pool;
706 UINT r, ret = ERROR_FUNCTION_FAILED, datasize = 0, poolsize = 0, codepage;
707 DWORD i, count, offset, len, n;
708 static const WCHAR szStringData[] = {
709 '_','S','t','r','i','n','g','D','a','t','a',0 };
710 static const WCHAR szStringPool[] = {
711 '_','S','t','r','i','n','g','P','o','o','l',0 };
713 if( db->strings )
715 msi_destroy_stringtable( db->strings );
716 db->strings = NULL;
719 r = read_stream_data( db->storage, szStringPool, &pool, &poolsize );
720 if( r != ERROR_SUCCESS)
721 goto end;
722 r = read_stream_data( db->storage, szStringData, (USHORT**)&data, &datasize );
723 if( r != ERROR_SUCCESS)
724 goto end;
726 count = poolsize/4;
727 if( poolsize > 4 )
728 codepage = pool[0] | ( pool[1] << 16 );
729 else
730 codepage = CP_ACP;
731 db->strings = msi_init_stringtable( count, codepage );
733 offset = 0;
734 for( i=1; i<count; i++ )
736 len = pool[i*2];
737 n = msi_addstring( db->strings, i, data+offset, len, pool[i*2+1] );
738 if( n != i )
739 ERR("Failed to add string %ld\n", i );
740 offset += len;
743 TRACE("Loaded %ld strings\n", count);
745 ret = ERROR_SUCCESS;
747 end:
748 HeapFree( GetProcessHeap(), 0, pool );
749 HeapFree( GetProcessHeap(), 0, data );
751 return ret;
754 static UINT save_string_table( MSIDATABASE *db )
756 UINT i, count, datasize, poolsize, sz, used, r, codepage;
757 UINT ret = ERROR_FUNCTION_FAILED;
758 static const WCHAR szStringData[] = {
759 '_','S','t','r','i','n','g','D','a','t','a',0 };
760 static const WCHAR szStringPool[] = {
761 '_','S','t','r','i','n','g','P','o','o','l',0 };
762 CHAR *data = NULL;
763 USHORT *pool = NULL;
765 TRACE("\n");
767 /* construct the new table in memory first */
768 datasize = msi_string_totalsize( db->strings, &count );
769 poolsize = count*2*sizeof(USHORT);
771 pool = HeapAlloc( GetProcessHeap(), 0, poolsize );
772 if( ! pool )
774 WARN("Failed to alloc pool %d bytes\n", poolsize );
775 goto err;
777 data = HeapAlloc( GetProcessHeap(), 0, datasize );
778 if( ! data )
780 WARN("Failed to alloc data %d bytes\n", poolsize );
781 goto err;
784 used = 0;
785 codepage = msi_string_get_codepage( db->strings );
786 pool[0]=codepage&0xffff;
787 pool[1]=(codepage>>16);
788 for( i=1; i<count; i++ )
790 sz = datasize - used;
791 r = msi_id2stringA( db->strings, i, data+used, &sz );
792 if( r != ERROR_SUCCESS )
794 ERR("failed to fetch string\n");
795 sz = 0;
797 if( sz && (sz < (datasize - used ) ) )
798 sz--;
799 TRACE("adding %u bytes %s\n", sz, data+used );
800 pool[ i*2 ] = sz;
801 pool[ i*2 + 1 ] = msi_id_refcount( db->strings, i );
802 used += sz;
803 if( used > datasize )
805 ERR("oops overran %d >= %d\n", used, datasize);
806 goto err;
810 if( used != datasize )
812 ERR("oops used %d != datasize %d\n", used, datasize);
813 goto err;
816 /* write the streams */
817 r = write_stream_data( db->storage, szStringData, data, datasize );
818 TRACE("Wrote StringData r=%08x\n", r);
819 if( r )
820 goto err;
821 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
822 TRACE("Wrote StringPool r=%08x\n", r);
823 if( r )
824 goto err;
826 ret = ERROR_SUCCESS;
828 err:
829 HeapFree( GetProcessHeap(), 0, data );
830 HeapFree( GetProcessHeap(), 0, pool );
832 return ret;
835 /* information for default tables */
836 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
837 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
838 static const WCHAR szName[] = { 'N','a','m','e',0 };
839 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
840 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
841 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
842 static const WCHAR szType[] = { 'T','y','p','e',0 };
844 struct standard_table {
845 LPCWSTR tablename;
846 LPCWSTR columnname;
847 UINT number;
848 UINT type;
849 } MSI_standard_tables[] =
851 { szTables, szName, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
852 { szColumns, szTable, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
853 { szColumns, szNumber, 2, MSITYPE_VALID | 2},
854 { szColumns, szName, 3, MSITYPE_VALID | MSITYPE_STRING | 32},
855 { szColumns, szType, 4, MSITYPE_VALID | 2},
858 #define STANDARD_TABLE_COUNT \
859 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
861 static UINT get_defaulttablecolumns( LPCWSTR szTable, MSICOLUMNINFO *colinfo, UINT *sz)
863 DWORD i, n=0;
865 for(i=0; i<STANDARD_TABLE_COUNT; i++)
867 if( lstrcmpW( szTable, MSI_standard_tables[i].tablename ) )
868 continue;
869 if(colinfo && (n < *sz) )
871 colinfo[n].tablename = strdupW(MSI_standard_tables[i].tablename);
872 colinfo[n].colname = strdupW(MSI_standard_tables[i].columnname);
873 colinfo[n].number = MSI_standard_tables[i].number;
874 colinfo[n].type = MSI_standard_tables[i].type;
875 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
876 debugstr_w(colinfo[n].colname)); */
877 if( n )
878 colinfo[n].offset = colinfo[n-1].offset
879 + bytes_per_column( &colinfo[n-1] );
880 else
881 colinfo[n].offset = 0;
883 n++;
884 if( colinfo && (n >= *sz) )
885 break;
887 *sz = n;
888 return ERROR_SUCCESS;
891 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
893 UINT sz=0, r;
894 LPWSTR str;
896 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
897 if( r != ERROR_SUCCESS )
898 return NULL;
899 str = HeapAlloc( GetProcessHeap(), 0, sz*sizeof (WCHAR));
900 if( !str )
901 return str;
902 r = msi_id2stringW( db->strings, stringid, str, &sz );
903 if( r == ERROR_SUCCESS )
904 return str;
905 HeapFree( GetProcessHeap(), 0, str );
906 return NULL;
909 static UINT get_tablecolumns( MSIDATABASE *db,
910 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
912 UINT r, i, n=0, table_id, count, maxcount = *sz;
913 MSITABLE *table = NULL;
914 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
916 /* first check if there is a default table with that name */
917 r = get_defaulttablecolumns( szTableName, colinfo, sz );
918 if( ( r == ERROR_SUCCESS ) && *sz )
919 return r;
921 r = get_table( db, szColumns, &table);
922 if( r != ERROR_SUCCESS )
924 WARN("table %s not available\n", debugstr_w(szColumns));
925 return r;
928 /* convert table and column names to IDs from the string table */
929 r = msi_string2idW( db->strings, szTableName, &table_id );
930 if( r != ERROR_SUCCESS )
932 release_table( db, table );
933 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
934 return r;
937 TRACE("Table id is %d\n", table_id);
939 count = table->row_count;
940 for( i=0; i<count; i++ )
942 if( table->data[ i ][ 0 ] != table_id )
943 continue;
944 if( colinfo )
946 UINT id = table->data[ i ] [ 2 ];
947 colinfo[n].tablename = MSI_makestring( db, table_id );
948 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
949 colinfo[n].colname = MSI_makestring( db, id );
950 colinfo[n].type = table->data[ i ] [ 3 ];
951 /* this assumes that columns are in order in the table */
952 if( n )
953 colinfo[n].offset = colinfo[n-1].offset
954 + bytes_per_column( &colinfo[n-1] );
955 else
956 colinfo[n].offset = 0;
957 TRACE("table %s column %d is [%s] (%d) with type %08x "
958 "offset %d at row %d\n", debugstr_w(szTableName),
959 colinfo[n].number, debugstr_w(colinfo[n].colname),
960 id, colinfo[n].type, colinfo[n].offset, i);
961 if( n != (colinfo[n].number-1) )
963 ERR("oops. data in the _Columns table isn't in the right "
964 "order for table %s\n", debugstr_w(szTableName));
965 return ERROR_FUNCTION_FAILED;
968 n++;
969 if( colinfo && ( n >= maxcount ) )
970 break;
972 *sz = n;
974 release_table( db, table );
976 return ERROR_SUCCESS;
979 /* try to find the table name in the _Tables table */
980 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
982 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
983 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
984 UINT r, table_id = 0, i, count;
985 MSITABLE *table = NULL;
987 if( !lstrcmpW( name, szTables ) )
988 return TRUE;
989 if( !lstrcmpW( name, szColumns ) )
990 return TRUE;
992 r = msi_string2idW( db->strings, name, &table_id );
993 if( r != ERROR_SUCCESS )
995 TRACE("Couldn't find id for %s\n", debugstr_w(name));
996 return FALSE;
999 r = get_table( db, szTables, &table);
1000 if( r != ERROR_SUCCESS )
1002 TRACE("table %s not available\n", debugstr_w(szTables));
1003 return FALSE;
1006 /* count = table->size/2; */
1007 count = table->row_count;
1008 for( i=0; i<count; i++ )
1009 if( table->data[ i ][ 0 ] == table_id )
1010 break;
1012 release_table( db, table );
1014 if (i!=count)
1015 return TRUE;
1017 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1019 return FALSE;
1022 /* below is the query interface to a table */
1024 typedef struct tagMSITABLEVIEW
1026 MSIVIEW view;
1027 MSIDATABASE *db;
1028 MSITABLE *table;
1029 MSICOLUMNINFO *columns;
1030 UINT num_cols;
1031 UINT row_size;
1032 WCHAR name[1];
1033 } MSITABLEVIEW;
1035 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1037 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1038 UINT offset, num_rows, n;
1040 if( !tv->table )
1041 return ERROR_INVALID_PARAMETER;
1043 if( (col==0) || (col>tv->num_cols) )
1044 return ERROR_INVALID_PARAMETER;
1046 /* how many rows are there ? */
1047 num_rows = tv->table->row_count;
1048 if( row >= num_rows )
1049 return ERROR_NO_MORE_ITEMS;
1051 if( tv->columns[col-1].offset >= tv->row_size )
1053 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1054 ERR("%p %p\n", tv, tv->columns );
1055 return ERROR_FUNCTION_FAILED;
1058 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1059 n = bytes_per_column( &tv->columns[col-1] );
1060 switch( n )
1062 case 4:
1063 offset = tv->columns[col-1].offset/2;
1064 *val = tv->table->data[row][offset] +
1065 (tv->table->data[row][offset + 1] << 16);
1066 break;
1067 case 2:
1068 offset = tv->columns[col-1].offset/2;
1069 *val = tv->table->data[row][offset];
1070 break;
1071 default:
1072 ERR("oops! what is %d bytes per column?\n", n );
1073 return ERROR_FUNCTION_FAILED;
1076 /* TRACE("Data [%d][%d] = %d \n", row, col, *val ); */
1078 return ERROR_SUCCESS;
1082 * We need a special case for streams, as we need to reference column with
1083 * the name of the stream in the same table, and the table name
1084 * which may not be available at higher levels of the query
1086 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1088 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1089 UINT ival = 0, refcol = 0, r;
1090 LPWSTR sval;
1091 LPWSTR full_name;
1092 DWORD len;
1093 static const WCHAR szDot[] = { '.', 0 };
1095 if( !view->ops->fetch_int )
1096 return ERROR_INVALID_PARAMETER;
1099 * The column marked with the type stream data seems to have a single number
1100 * which references the column containing the name of the stream data
1102 * Fetch the column to reference first.
1104 r = view->ops->fetch_int( view, row, col, &ival );
1105 if( r != ERROR_SUCCESS )
1106 return r;
1108 /* now get the column with the name of the stream */
1109 r = view->ops->fetch_int( view, row, ival, &refcol );
1110 if( r != ERROR_SUCCESS )
1111 return r;
1113 /* lookup the string value from the string table */
1114 sval = MSI_makestring( tv->db, refcol );
1115 if( !sval )
1116 return ERROR_INVALID_PARAMETER;
1118 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1119 full_name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1120 lstrcpyW( full_name, tv->name );
1121 lstrcatW( full_name, szDot );
1122 lstrcatW( full_name, sval );
1124 r = db_get_raw_stream( tv->db, full_name, stm );
1125 if( r )
1126 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1127 HeapFree( GetProcessHeap(), 0, full_name );
1128 HeapFree( GetProcessHeap(), 0, sval );
1130 return r;
1133 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1135 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1136 UINT offset, n;
1138 if( !tv->table )
1139 return ERROR_INVALID_PARAMETER;
1141 if( (col==0) || (col>tv->num_cols) )
1142 return ERROR_INVALID_PARAMETER;
1144 if( tv->columns[col-1].offset >= tv->row_size )
1146 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1147 ERR("%p %p\n", tv, tv->columns );
1148 return ERROR_FUNCTION_FAILED;
1151 n = bytes_per_column( &tv->columns[col-1] );
1152 switch( n )
1154 case 4:
1155 offset = tv->columns[col-1].offset/2;
1156 tv->table->data[row][offset] = val & 0xffff;
1157 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1158 break;
1159 case 2:
1160 offset = tv->columns[col-1].offset/2;
1161 tv->table->data[row][offset] = val;
1162 break;
1163 default:
1164 ERR("oops! what is %d bytes per column?\n", n );
1165 return ERROR_FUNCTION_FAILED;
1167 return ERROR_SUCCESS;
1170 static UINT TABLE_insert_row( struct tagMSIVIEW *view, UINT *num )
1172 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1173 USHORT **p, *row;
1174 UINT sz;
1176 TRACE("%p\n", view);
1178 if( !tv->table )
1179 return ERROR_INVALID_PARAMETER;
1181 row = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, tv->row_size );
1182 if( !row )
1183 return ERROR_NOT_ENOUGH_MEMORY;
1185 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1186 if( tv->table->data )
1187 p = HeapReAlloc( GetProcessHeap(), 0, tv->table->data, sz );
1188 else
1189 p = HeapAlloc( GetProcessHeap(), 0, sz );
1190 if( !p )
1192 HeapFree( GetProcessHeap(), 0, row );
1193 return ERROR_NOT_ENOUGH_MEMORY;
1196 tv->table->data = p;
1197 tv->table->data[tv->table->row_count] = row;
1198 *num = tv->table->row_count;
1199 tv->table->row_count++;
1201 return ERROR_SUCCESS;
1204 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1206 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1207 UINT r;
1209 TRACE("%p %p\n", tv, record);
1211 if( tv->table )
1213 release_table( tv->db, tv->table );
1214 tv->table = NULL;
1217 r = get_table( tv->db, tv->name, &tv->table );
1218 if( r != ERROR_SUCCESS )
1219 return r;
1221 return ERROR_SUCCESS;
1224 static UINT TABLE_close( struct tagMSIVIEW *view )
1226 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1228 TRACE("%p\n", view );
1230 if( !tv->table )
1231 return ERROR_FUNCTION_FAILED;
1233 release_table( tv->db, tv->table );
1234 tv->table = NULL;
1236 return ERROR_SUCCESS;
1239 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1241 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1243 TRACE("%p %p %p\n", view, rows, cols );
1245 if( cols )
1246 *cols = tv->num_cols;
1247 if( rows )
1249 if( !tv->table )
1250 return ERROR_INVALID_PARAMETER;
1251 *rows = tv->table->row_count;
1254 return ERROR_SUCCESS;
1257 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1258 UINT n, LPWSTR *name, UINT *type )
1260 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1262 TRACE("%p %d %p %p\n", tv, n, name, type );
1264 if( ( n == 0 ) || ( n > tv->num_cols ) )
1265 return ERROR_INVALID_PARAMETER;
1267 if( name )
1269 *name = strdupW( tv->columns[n-1].colname );
1270 if( !*name )
1271 return ERROR_FUNCTION_FAILED;
1273 if( type )
1274 *type = tv->columns[n-1].type;
1276 return ERROR_SUCCESS;
1279 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1280 MSIRECORD *rec)
1282 FIXME("%p %d %p\n", view, eModifyMode, rec );
1283 return ERROR_CALL_NOT_IMPLEMENTED;
1286 static UINT TABLE_delete( struct tagMSIVIEW *view )
1288 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1290 TRACE("%p\n", view );
1292 if( tv->table )
1293 release_table( tv->db, tv->table );
1294 tv->table = NULL;
1296 if( tv->columns )
1298 UINT i;
1299 for( i=0; i<tv->num_cols; i++)
1301 HeapFree( GetProcessHeap(), 0, tv->columns[i].colname );
1302 HeapFree( GetProcessHeap(), 0, tv->columns[i].tablename );
1304 HeapFree( GetProcessHeap(), 0, tv->columns );
1306 tv->columns = NULL;
1308 HeapFree( GetProcessHeap(), 0, tv );
1310 return ERROR_SUCCESS;
1314 MSIVIEWOPS table_ops =
1316 TABLE_fetch_int,
1317 TABLE_fetch_stream,
1318 TABLE_set_int,
1319 TABLE_insert_row,
1320 TABLE_execute,
1321 TABLE_close,
1322 TABLE_get_dimensions,
1323 TABLE_get_column_info,
1324 TABLE_modify,
1325 TABLE_delete
1328 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1330 MSITABLEVIEW *tv ;
1331 UINT r, sz, column_count;
1332 MSICOLUMNINFO *columns, *last_col;
1334 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1336 /* get the number of columns in this table */
1337 column_count = 0;
1338 r = get_tablecolumns( db, name, NULL, &column_count );
1339 if( r != ERROR_SUCCESS )
1340 return r;
1342 /* if there's no columns, there's no table */
1343 if( column_count == 0 )
1344 return ERROR_INVALID_PARAMETER;
1346 TRACE("Table found\n");
1348 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1349 tv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sz );
1350 if( !tv )
1351 return ERROR_FUNCTION_FAILED;
1353 columns = HeapAlloc( GetProcessHeap(), 0, column_count*sizeof (MSICOLUMNINFO));
1354 if( !columns )
1356 HeapFree( GetProcessHeap(), 0, tv );
1357 return ERROR_FUNCTION_FAILED;
1360 r = get_tablecolumns( db, name, columns, &column_count );
1361 if( r != ERROR_SUCCESS )
1363 HeapFree( GetProcessHeap(), 0, columns );
1364 HeapFree( GetProcessHeap(), 0, tv );
1365 return ERROR_FUNCTION_FAILED;
1368 TRACE("Table has %d columns\n", column_count);
1370 last_col = &columns[column_count-1];
1372 /* fill the structure */
1373 tv->view.ops = &table_ops;
1374 tv->db = db;
1375 tv->columns = columns;
1376 tv->num_cols = column_count;
1377 tv->table = NULL;
1378 tv->row_size = last_col->offset + bytes_per_column( last_col );
1380 TRACE("one row is %d bytes\n", tv->row_size );
1382 *view = (MSIVIEW*) tv;
1383 lstrcpyW( tv->name, name );
1385 return ERROR_SUCCESS;
1388 UINT MSI_CommitTables( MSIDATABASE *db )
1390 UINT r;
1391 MSITABLE *table = NULL;
1393 TRACE("%p\n",db);
1395 r = save_string_table( db );
1396 if( r != ERROR_SUCCESS )
1398 WARN("failed to save string table r=%08x\n",r);
1399 return r;
1402 for( table = db->first_table; table; table = table->next )
1404 r = save_table( db, table );
1405 if( r != ERROR_SUCCESS )
1407 WARN("failed to save table %s (r=%08x)\n",
1408 debugstr_w(table->name), r);
1409 return r;
1413 /* force everything to reload next time */
1414 free_cached_tables( db );
1416 return ERROR_SUCCESS;