Added addrinfo structures.
[wine/multimedia.git] / dlls / msi / table.c
blob5f488e28a1e8c8aa6ba0cffcc0137761b412fec5
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 list entry;
57 WCHAR name[1];
60 #define MAX_STREAM_NAME 0x1f
62 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
63 MSICOLUMNINFO **pcols, UINT *pcount );
64 static UINT get_tablecolumns( MSIDATABASE *db,
65 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
67 static inline UINT bytes_per_column( MSICOLUMNINFO *col )
69 if( col->type & MSITYPE_STRING )
70 return 2;
71 if( (col->type & 0xff) > 4 )
72 ERR("Invalid column size!\n");
73 return col->type & 0xff;
76 static int utf2mime(int x)
78 if( (x>='0') && (x<='9') )
79 return x-'0';
80 if( (x>='A') && (x<='Z') )
81 return x-'A'+10;
82 if( (x>='a') && (x<='z') )
83 return x-'a'+10+26;
84 if( x=='.' )
85 return 10+26+26;
86 if( x=='_' )
87 return 10+26+26+1;
88 return -1;
91 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
93 DWORD count = MAX_STREAM_NAME;
94 DWORD ch, next;
95 LPWSTR out, p;
97 if( !bTable )
98 count = lstrlenW( in )+2;
99 out = msi_alloc( count*sizeof(WCHAR) );
100 p = out;
102 if( bTable )
104 *p++ = 0x4840;
105 count --;
107 while( count -- )
109 ch = *in++;
110 if( !ch )
112 *p = ch;
113 return out;
115 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
117 ch = utf2mime(ch) + 0x4800;
118 next = *in;
119 if( next && (next<0x80) )
121 next = utf2mime(next);
122 if( next >= 0 )
124 next += 0x3ffffc0;
125 ch += (next<<6);
126 in++;
130 *p++ = ch;
132 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
133 msi_free( out );
134 return NULL;
137 static int mime2utf(int x)
139 if( x<10 )
140 return x + '0';
141 if( x<(10+26))
142 return x - 10 + 'A';
143 if( x<(10+26+26))
144 return x - 10 - 26 + 'a';
145 if( x == (10+26+26) )
146 return '.';
147 return '_';
150 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
152 WCHAR ch;
153 DWORD count = 0;
155 while ( (ch = *in++) )
157 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
159 if( ch >= 0x4800 )
160 ch = mime2utf(ch-0x4800);
161 else
163 ch -= 0x3800;
164 *out++ = mime2utf(ch&0x3f);
165 count++;
166 ch = mime2utf((ch>>6)&0x3f);
169 *out++ = ch;
170 count++;
172 *out = 0;
173 return count;
176 void enum_stream_names( IStorage *stg )
178 IEnumSTATSTG *stgenum = NULL;
179 HRESULT r;
180 STATSTG stat;
181 ULONG n, count;
182 WCHAR name[0x40];
184 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
185 if( FAILED( r ) )
186 return;
188 n = 0;
189 while( 1 )
191 count = 0;
192 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
193 if( FAILED( r ) || !count )
194 break;
195 decode_streamname( stat.pwcsName, name );
196 TRACE("stream %2ld -> %s %s\n", n,
197 debugstr_w(stat.pwcsName), debugstr_w(name) );
198 n++;
201 IEnumSTATSTG_Release( stgenum );
204 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
205 USHORT **pdata, UINT *psz )
207 HRESULT r;
208 UINT ret = ERROR_FUNCTION_FAILED;
209 VOID *data;
210 ULONG sz, count;
211 IStream *stm = NULL;
212 STATSTG stat;
213 LPWSTR encname;
215 encname = encode_streamname(TRUE, stname);
217 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
219 r = IStorage_OpenStream(stg, encname, NULL,
220 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
221 msi_free( encname );
222 if( FAILED( r ) )
224 WARN("open stream failed r = %08lx - empty table?\n",r);
225 return ret;
228 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
229 if( FAILED( r ) )
231 WARN("open stream failed r = %08lx!\n",r);
232 goto end;
235 if( stat.cbSize.QuadPart >> 32 )
237 WARN("Too big!\n");
238 goto end;
241 sz = stat.cbSize.QuadPart;
242 data = msi_alloc( sz );
243 if( !data )
245 WARN("couldn't allocate memory r=%08lx!\n",r);
246 ret = ERROR_NOT_ENOUGH_MEMORY;
247 goto end;
250 r = IStream_Read(stm, data, sz, &count );
251 if( FAILED( r ) || ( count != sz ) )
253 msi_free( data );
254 WARN("read stream failed r = %08lx!\n",r);
255 goto end;
258 *pdata = data;
259 *psz = sz;
260 ret = ERROR_SUCCESS;
262 end:
263 IStream_Release( stm );
265 return ret;
268 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
270 LPWSTR encname;
271 HRESULT r;
273 encname = encode_streamname(FALSE, stname);
275 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
277 r = IStorage_OpenStream(db->storage, encname, NULL,
278 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
279 msi_free( encname );
280 if( FAILED( r ) )
282 WARN("open stream failed r = %08lx - empty table?\n",r);
283 return ERROR_FUNCTION_FAILED;
286 return ERROR_SUCCESS;
289 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
290 USHORT **pdata, UINT *psz )
292 HRESULT r;
293 UINT ret = ERROR_FUNCTION_FAILED;
294 VOID *data;
295 ULONG sz, count;
296 IStream *stm = NULL;
297 STATSTG stat;
299 r = db_get_raw_stream( db, stname, &stm );
300 if( r != ERROR_SUCCESS)
301 return ret;
302 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
303 if( FAILED( r ) )
305 WARN("open stream failed r = %08lx!\n",r);
306 goto end;
309 if( stat.cbSize.QuadPart >> 32 )
311 WARN("Too big!\n");
312 goto end;
315 sz = stat.cbSize.QuadPart;
316 data = msi_alloc( sz );
317 if( !data )
319 WARN("couldn't allocate memory r=%08lx!\n",r);
320 ret = ERROR_NOT_ENOUGH_MEMORY;
321 goto end;
324 r = IStream_Read(stm, data, sz, &count );
325 if( FAILED( r ) || ( count != sz ) )
327 msi_free( data );
328 WARN("read stream failed r = %08lx!\n",r);
329 goto end;
332 *pdata = data;
333 *psz = sz;
334 ret = ERROR_SUCCESS;
336 end:
337 IStream_Release( stm );
339 return ret;
342 static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
343 LPVOID data, UINT sz )
345 HRESULT r;
346 UINT ret = ERROR_FUNCTION_FAILED;
347 ULONG count;
348 IStream *stm = NULL;
349 ULARGE_INTEGER size;
350 LARGE_INTEGER pos;
351 LPWSTR encname;
353 encname = encode_streamname(TRUE, stname );
354 r = IStorage_OpenStream( stg, encname, NULL,
355 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
356 if( FAILED(r) )
358 r = IStorage_CreateStream( stg, encname,
359 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
361 msi_free( encname );
362 if( FAILED( r ) )
364 WARN("open stream failed r = %08lx\n",r);
365 return ret;
368 size.QuadPart = sz;
369 r = IStream_SetSize( stm, size );
370 if( FAILED( r ) )
372 WARN("Failed to SetSize\n");
373 goto end;
376 pos.QuadPart = 0;
377 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
378 if( FAILED( r ) )
380 WARN("Failed to Seek\n");
381 goto end;
384 r = IStream_Write(stm, data, sz, &count );
385 if( FAILED( r ) || ( count != sz ) )
387 WARN("Failed to Write\n");
388 goto end;
391 ret = ERROR_SUCCESS;
393 end:
394 IStream_Release( stm );
396 return ret;
399 static void free_table( MSITABLE *table )
401 int i;
402 for( i=0; i<table->row_count; i++ )
403 msi_free( table->data[i] );
404 msi_free( table->data );
405 msi_free( table );
408 static UINT read_table_from_storage( MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
410 MSITABLE *t;
411 USHORT *rawdata = NULL;
412 UINT rawsize = 0, r, i, j, row_size = 0, num_cols = 0;
413 MSICOLUMNINFO *cols, *last_col;
415 TRACE("%s\n",debugstr_w(name));
417 /* nonexistent tables should be interpreted as empty tables */
418 t = msi_alloc( sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
419 if( !t )
420 return ERROR_NOT_ENOUGH_MEMORY;
422 r = table_get_column_info( db, name, &cols, &num_cols );
423 if( r != ERROR_SUCCESS )
425 msi_free( t );
426 return r;
428 last_col = &cols[num_cols-1];
429 row_size = last_col->offset + bytes_per_column( last_col );
431 t->row_count = 0;
432 t->data = NULL;
433 lstrcpyW( t->name, name );
434 t->ref_count = 1;
435 *ptable = t;
437 /* if we can't read the table, just assume that it's empty */
438 read_stream_data( db->storage, name, &rawdata, &rawsize );
439 if( !rawdata )
440 return ERROR_SUCCESS;
442 TRACE("Read %d bytes\n", rawsize );
444 if( rawsize % row_size )
446 WARN("Table size is invalid %d/%d\n", rawsize, row_size );
447 return ERROR_FUNCTION_FAILED;
450 t->row_count = rawsize / row_size;
451 t->data = msi_alloc_zero( t->row_count * sizeof (USHORT*) );
452 if( !t->data )
454 r = ERROR_NOT_ENOUGH_MEMORY;
455 goto err;
458 /* transpose all the data */
459 TRACE("Transposing data from %d columns\n", t->row_count );
460 for( i=0; i<t->row_count; i++ )
462 t->data[i] = msi_alloc( row_size );
463 if( !t->data[i] )
465 r = ERROR_NOT_ENOUGH_MEMORY;
466 goto err;
468 for( j=0; j<num_cols; j++ )
470 UINT ofs = cols[j].offset/2;
471 UINT n = bytes_per_column( &cols[j] );
473 switch( n )
475 case 2:
476 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
477 break;
478 case 4:
479 t->data[i][ofs] = rawdata[ofs*t->row_count + i*2 ];
480 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i*2 + 1];
481 break;
482 default:
483 ERR("oops - unknown column width %d\n", n);
484 r = ERROR_FUNCTION_FAILED;
485 goto err;
490 msi_free( cols );
491 msi_free( rawdata );
493 return ERROR_SUCCESS;
495 err:
496 msi_free( cols );
497 msi_free( rawdata );
498 free_table( t );
499 return r;
502 /* add this table to the list of cached tables in the database */
503 void add_table(MSIDATABASE *db, MSITABLE *table)
505 list_add_head( &db->tables, &table->entry );
508 /* remove from the list of cached tables */
509 void remove_table( MSIDATABASE *db, MSITABLE *table )
511 list_remove( &table->entry );
514 static void release_table( MSIDATABASE *db, MSITABLE *table )
516 if( !table->ref_count )
517 ERR("Trying to destroy table with refcount 0\n");
518 table->ref_count --;
519 if( !table->ref_count )
521 remove_table( db, table );
522 TRACE("Destroying table %s\n", debugstr_w(table->name));
523 free_table( table );
527 void free_cached_tables( MSIDATABASE *db )
529 while( !list_empty( &db->tables ) )
531 MSITABLE *t = LIST_ENTRY( list_head( &db->tables ), MSITABLE, entry );
533 list_remove( &t->entry );
534 if ( --t->ref_count )
535 ERR("table ref count not zero for %s\n", debugstr_w(t->name));
536 remove_table( db, t );
537 msi_free( t->data );
538 msi_free( t );
542 UINT find_cached_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
544 MSITABLE *t;
546 LIST_FOR_EACH_ENTRY( t, &db->tables, MSITABLE, entry )
548 if( !lstrcmpW( name, t->name ) )
550 *ptable = t;
551 return ERROR_SUCCESS;
555 return ERROR_FUNCTION_FAILED;
558 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
560 UINT r, column_count;
561 MSICOLUMNINFO *columns;
563 /* get the number of columns in this table */
564 column_count = 0;
565 r = get_tablecolumns( db, name, NULL, &column_count );
566 if( r != ERROR_SUCCESS )
567 return r;
569 /* if there's no columns, there's no table */
570 if( column_count == 0 )
571 return ERROR_INVALID_PARAMETER;
573 TRACE("Table %s found\n", debugstr_w(name) );
575 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO));
576 if( !columns )
577 return ERROR_FUNCTION_FAILED;
579 r = get_tablecolumns( db, name, columns, &column_count );
580 if( r != ERROR_SUCCESS )
582 msi_free( columns );
583 return ERROR_FUNCTION_FAILED;
586 *pcols = columns;
587 *pcount = column_count;
589 return r;
592 UINT get_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
594 UINT r;
596 *ptable = NULL;
598 /* first, see if the table is cached */
599 r = find_cached_table( db, name, ptable );
600 if( r == ERROR_SUCCESS )
602 (*ptable)->ref_count++;
603 return r;
606 r = read_table_from_storage( db, name, ptable );
607 if( r != ERROR_SUCCESS )
608 return r;
610 /* add the table to the list */
611 add_table( db, *ptable );
612 (*ptable)->ref_count++;
614 return ERROR_SUCCESS;
617 static UINT save_table( MSIDATABASE *db, MSITABLE *t )
619 USHORT *rawdata = NULL, *p;
620 UINT rawsize, r, i, j, row_size, num_cols = 0;
621 MSICOLUMNINFO *cols, *last_col;
623 TRACE("Saving %s\n", debugstr_w( t->name ) );
625 r = table_get_column_info( db, t->name, &cols, &num_cols );
626 if( r != ERROR_SUCCESS )
627 return r;
629 last_col = &cols[num_cols-1];
630 row_size = last_col->offset + bytes_per_column( last_col );
632 rawsize = t->row_count * row_size;
633 rawdata = msi_alloc_zero( rawsize );
634 if( !rawdata )
635 return ERROR_NOT_ENOUGH_MEMORY;
637 p = rawdata;
638 for( i=0; i<num_cols; i++ )
640 for( j=0; j<t->row_count; j++ )
642 UINT offset = cols[i].offset;
644 *p++ = t->data[j][offset/2];
645 if( 4 == bytes_per_column( &cols[i] ) )
646 *p++ = t->data[j][offset/2+1];
650 TRACE("writing %d bytes\n", rawsize);
651 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
653 msi_free( rawdata );
655 return r;
658 HRESULT init_string_table( IStorage *stg )
660 HRESULT r;
661 static const WCHAR szStringData[] = {
662 '_','S','t','r','i','n','g','D','a','t','a',0 };
663 static const WCHAR szStringPool[] = {
664 '_','S','t','r','i','n','g','P','o','o','l',0 };
665 USHORT zero[2] = { 0, 0 };
666 ULONG count = 0;
667 IStream *stm = NULL;
668 LPWSTR encname;
670 encname = encode_streamname(TRUE, szStringPool );
672 /* create the StringPool stream... add the zero string to it*/
673 r = IStorage_CreateStream( stg, encname,
674 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
675 msi_free( encname );
676 if( r )
678 TRACE("Failed\n");
679 return r;
682 r = IStream_Write(stm, zero, sizeof zero, &count );
683 IStream_Release( stm );
685 if( FAILED( r ) || ( count != sizeof zero ) )
687 TRACE("Failed\n");
688 return E_FAIL;
691 /* create the StringData stream... make it zero length */
692 encname = encode_streamname(TRUE, szStringData );
693 r = IStorage_CreateStream( stg, encname,
694 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
695 msi_free( encname );
696 if( r )
698 TRACE("Failed\n");
699 return E_FAIL;
701 IStream_Release( stm );
703 return r;
706 string_table *load_string_table( IStorage *stg )
708 string_table *st = NULL;
709 CHAR *data;
710 USHORT *pool;
711 UINT r, datasize = 0, poolsize = 0, codepage;
712 DWORD i, count, offset, len, n;
713 static const WCHAR szStringData[] = {
714 '_','S','t','r','i','n','g','D','a','t','a',0 };
715 static const WCHAR szStringPool[] = {
716 '_','S','t','r','i','n','g','P','o','o','l',0 };
718 r = read_stream_data( stg, szStringPool, &pool, &poolsize );
719 if( r != ERROR_SUCCESS)
720 goto end;
721 r = read_stream_data( stg, szStringData, (USHORT**)&data, &datasize );
722 if( r != ERROR_SUCCESS)
723 goto end;
725 count = poolsize/4;
726 if( poolsize > 4 )
727 codepage = pool[0] | ( pool[1] << 16 );
728 else
729 codepage = CP_ACP;
730 st = msi_init_stringtable( count, codepage );
732 offset = 0;
733 n = 1;
734 for( i=1; i<count; i++ )
736 len = pool[i*2];
739 * If a string is over 64k, the previous string entry is made null
740 * and its the high word of the length is inserted in the null string's
741 * reference count field.
743 if( pool[i*2-2] == 0 )
744 len += pool[i*2-1] * 0x10000;
746 if( (offset + len) > datasize )
748 ERR("string table corrupt?\n");
749 break;
752 /* don't add the high word of a string's length as a string */
753 if ( len || !pool[i*2+1] )
755 r = msi_addstring( st, n, data+offset, len, pool[i*2+1] );
756 if( r != n )
757 ERR("Failed to add string %ld\n", n );
758 n++;
761 offset += len;
764 if ( datasize != offset )
765 ERR("string table load failed! (%08x != %08lx)\n", datasize, offset );
767 TRACE("Loaded %ld strings\n", count);
769 end:
770 msi_free( pool );
771 msi_free( data );
773 return st;
776 static UINT save_string_table( MSIDATABASE *db )
778 UINT i, count, datasize, poolsize, sz, used, r, codepage;
779 UINT ret = ERROR_FUNCTION_FAILED;
780 static const WCHAR szStringData[] = {
781 '_','S','t','r','i','n','g','D','a','t','a',0 };
782 static const WCHAR szStringPool[] = {
783 '_','S','t','r','i','n','g','P','o','o','l',0 };
784 CHAR *data = NULL;
785 USHORT *pool = NULL;
787 TRACE("\n");
789 /* construct the new table in memory first */
790 datasize = msi_string_totalsize( db->strings, &count );
791 poolsize = count*2*sizeof(USHORT);
793 pool = msi_alloc( poolsize );
794 if( ! pool )
796 WARN("Failed to alloc pool %d bytes\n", poolsize );
797 goto err;
799 data = msi_alloc( datasize );
800 if( ! data )
802 WARN("Failed to alloc data %d bytes\n", poolsize );
803 goto err;
806 used = 0;
807 codepage = msi_string_get_codepage( db->strings );
808 pool[0]=codepage&0xffff;
809 pool[1]=(codepage>>16);
810 for( i=1; i<count; i++ )
812 sz = datasize - used;
813 r = msi_id2stringA( db->strings, i, data+used, &sz );
814 if( r != ERROR_SUCCESS )
816 ERR("failed to fetch string\n");
817 sz = 0;
819 if( sz && (sz < (datasize - used ) ) )
820 sz--;
821 TRACE("adding %u bytes %s\n", sz, debugstr_a(data+used) );
822 pool[ i*2 ] = sz;
823 pool[ i*2 + 1 ] = msi_id_refcount( db->strings, i );
824 used += sz;
825 if( used > datasize )
827 ERR("oops overran %d >= %d\n", used, datasize);
828 goto err;
832 if( used != datasize )
834 ERR("oops used %d != datasize %d\n", used, datasize);
835 goto err;
838 /* write the streams */
839 r = write_stream_data( db->storage, szStringData, data, datasize );
840 TRACE("Wrote StringData r=%08x\n", r);
841 if( r )
842 goto err;
843 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
844 TRACE("Wrote StringPool r=%08x\n", r);
845 if( r )
846 goto err;
848 ret = ERROR_SUCCESS;
850 err:
851 msi_free( data );
852 msi_free( pool );
854 return ret;
857 /* information for default tables */
858 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
859 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
860 static const WCHAR szName[] = { 'N','a','m','e',0 };
861 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
862 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
863 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
864 static const WCHAR szType[] = { 'T','y','p','e',0 };
866 struct standard_table {
867 LPCWSTR tablename;
868 LPCWSTR columnname;
869 UINT number;
870 UINT type;
871 } MSI_standard_tables[] =
873 { szTables, szName, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
874 { szColumns, szTable, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
875 { szColumns, szNumber, 2, MSITYPE_VALID | 2},
876 { szColumns, szName, 3, MSITYPE_VALID | MSITYPE_STRING | 32},
877 { szColumns, szType, 4, MSITYPE_VALID | 2},
880 #define STANDARD_TABLE_COUNT \
881 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
883 static UINT get_defaulttablecolumns( LPCWSTR szTable, MSICOLUMNINFO *colinfo, UINT *sz)
885 DWORD i, n=0;
887 for(i=0; i<STANDARD_TABLE_COUNT; i++)
889 if( lstrcmpW( szTable, MSI_standard_tables[i].tablename ) )
890 continue;
891 if(colinfo && (n < *sz) )
893 colinfo[n].tablename = strdupW(MSI_standard_tables[i].tablename);
894 colinfo[n].colname = strdupW(MSI_standard_tables[i].columnname);
895 colinfo[n].number = MSI_standard_tables[i].number;
896 colinfo[n].type = MSI_standard_tables[i].type;
897 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
898 debugstr_w(colinfo[n].colname)); */
899 if( n )
900 colinfo[n].offset = colinfo[n-1].offset
901 + bytes_per_column( &colinfo[n-1] );
902 else
903 colinfo[n].offset = 0;
905 n++;
906 if( colinfo && (n >= *sz) )
907 break;
909 *sz = n;
910 return ERROR_SUCCESS;
913 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
915 UINT sz=0, r;
916 LPWSTR str;
918 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
919 if( r != ERROR_SUCCESS )
920 return NULL;
921 str = msi_alloc( sz*sizeof (WCHAR));
922 if( !str )
923 return str;
924 r = msi_id2stringW( db->strings, stringid, str, &sz );
925 if( r == ERROR_SUCCESS )
926 return str;
927 msi_free( str );
928 return NULL;
931 static UINT get_tablecolumns( MSIDATABASE *db,
932 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
934 UINT r, i, n=0, table_id, count, maxcount = *sz;
935 MSITABLE *table = NULL;
936 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
938 /* first check if there is a default table with that name */
939 r = get_defaulttablecolumns( szTableName, colinfo, sz );
940 if( ( r == ERROR_SUCCESS ) && *sz )
941 return r;
943 r = get_table( db, szColumns, &table);
944 if( r != ERROR_SUCCESS )
946 WARN("table %s not available\n", debugstr_w(szColumns));
947 return r;
950 /* convert table and column names to IDs from the string table */
951 r = msi_string2idW( db->strings, szTableName, &table_id );
952 if( r != ERROR_SUCCESS )
954 release_table( db, table );
955 WARN("Couldn't find id for %s\n", debugstr_w(szTableName));
956 return r;
959 TRACE("Table id is %d\n", table_id);
961 count = table->row_count;
962 for( i=0; i<count; i++ )
964 if( table->data[ i ][ 0 ] != table_id )
965 continue;
966 if( colinfo )
968 UINT id = table->data[ i ] [ 2 ];
969 colinfo[n].tablename = MSI_makestring( db, table_id );
970 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
971 colinfo[n].colname = MSI_makestring( db, id );
972 colinfo[n].type = table->data[ i ] [ 3 ];
973 /* this assumes that columns are in order in the table */
974 if( n )
975 colinfo[n].offset = colinfo[n-1].offset
976 + bytes_per_column( &colinfo[n-1] );
977 else
978 colinfo[n].offset = 0;
979 TRACE("table %s column %d is [%s] (%d) with type %08x "
980 "offset %d at row %d\n", debugstr_w(szTableName),
981 colinfo[n].number, debugstr_w(colinfo[n].colname),
982 id, colinfo[n].type, colinfo[n].offset, i);
983 if( n != (colinfo[n].number-1) )
985 ERR("oops. data in the _Columns table isn't in the right "
986 "order for table %s\n", debugstr_w(szTableName));
987 return ERROR_FUNCTION_FAILED;
990 n++;
991 if( colinfo && ( n >= maxcount ) )
992 break;
994 *sz = n;
996 release_table( db, table );
998 return ERROR_SUCCESS;
1001 /* try to find the table name in the _Tables table */
1002 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
1004 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
1005 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
1006 UINT r, table_id = 0, i, count;
1007 MSITABLE *table = NULL;
1009 if( !lstrcmpW( name, szTables ) )
1010 return TRUE;
1011 if( !lstrcmpW( name, szColumns ) )
1012 return TRUE;
1014 r = msi_string2idW( db->strings, name, &table_id );
1015 if( r != ERROR_SUCCESS )
1017 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1018 return FALSE;
1021 r = get_table( db, szTables, &table);
1022 if( r != ERROR_SUCCESS )
1024 TRACE("table %s not available\n", debugstr_w(szTables));
1025 return FALSE;
1028 /* count = table->size/2; */
1029 count = table->row_count;
1030 for( i=0; i<count; i++ )
1031 if( table->data[ i ][ 0 ] == table_id )
1032 break;
1034 release_table( db, table );
1036 if (i!=count)
1037 return TRUE;
1039 TRACE("Searched %d tables, but %d was not found\n", count, table_id );
1041 return FALSE;
1044 /* below is the query interface to a table */
1046 typedef struct tagMSITABLEVIEW
1048 MSIVIEW view;
1049 MSIDATABASE *db;
1050 MSITABLE *table;
1051 MSICOLUMNINFO *columns;
1052 UINT num_cols;
1053 UINT row_size;
1054 WCHAR name[1];
1055 } MSITABLEVIEW;
1057 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1059 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1060 UINT offset, num_rows, n;
1062 if( !tv->table )
1063 return ERROR_INVALID_PARAMETER;
1065 if( (col==0) || (col>tv->num_cols) )
1066 return ERROR_INVALID_PARAMETER;
1068 /* how many rows are there ? */
1069 num_rows = tv->table->row_count;
1070 if( row >= num_rows )
1071 return ERROR_NO_MORE_ITEMS;
1073 if( tv->columns[col-1].offset >= tv->row_size )
1075 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1076 ERR("%p %p\n", tv, tv->columns );
1077 return ERROR_FUNCTION_FAILED;
1080 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1081 n = bytes_per_column( &tv->columns[col-1] );
1082 switch( n )
1084 case 4:
1085 offset = tv->columns[col-1].offset/2;
1086 *val = tv->table->data[row][offset] +
1087 (tv->table->data[row][offset + 1] << 16);
1088 break;
1089 case 2:
1090 offset = tv->columns[col-1].offset/2;
1091 *val = tv->table->data[row][offset];
1092 break;
1093 default:
1094 ERR("oops! what is %d bytes per column?\n", n );
1095 return ERROR_FUNCTION_FAILED;
1098 /* TRACE("Data [%d][%d] = %d \n", row, col, *val ); */
1100 return ERROR_SUCCESS;
1104 * We need a special case for streams, as we need to reference column with
1105 * the name of the stream in the same table, and the table name
1106 * which may not be available at higher levels of the query
1108 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1110 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1111 UINT ival = 0, refcol = 0, r;
1112 LPWSTR sval;
1113 LPWSTR full_name;
1114 DWORD len;
1115 static const WCHAR szDot[] = { '.', 0 };
1117 if( !view->ops->fetch_int )
1118 return ERROR_INVALID_PARAMETER;
1121 * The column marked with the type stream data seems to have a single number
1122 * which references the column containing the name of the stream data
1124 * Fetch the column to reference first.
1126 r = view->ops->fetch_int( view, row, col, &ival );
1127 if( r != ERROR_SUCCESS )
1128 return r;
1130 /* now get the column with the name of the stream */
1131 r = view->ops->fetch_int( view, row, ival, &refcol );
1132 if( r != ERROR_SUCCESS )
1133 return r;
1135 /* lookup the string value from the string table */
1136 sval = MSI_makestring( tv->db, refcol );
1137 if( !sval )
1138 return ERROR_INVALID_PARAMETER;
1140 len = lstrlenW( tv->name ) + 2 + lstrlenW( sval );
1141 full_name = msi_alloc( len*sizeof(WCHAR) );
1142 lstrcpyW( full_name, tv->name );
1143 lstrcatW( full_name, szDot );
1144 lstrcatW( full_name, sval );
1146 r = db_get_raw_stream( tv->db, full_name, stm );
1147 if( r )
1148 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1149 msi_free( full_name );
1150 msi_free( sval );
1152 return r;
1155 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1157 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1158 UINT offset, n;
1160 if( !tv->table )
1161 return ERROR_INVALID_PARAMETER;
1163 if( (col==0) || (col>tv->num_cols) )
1164 return ERROR_INVALID_PARAMETER;
1166 if( tv->columns[col-1].offset >= tv->row_size )
1168 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1169 ERR("%p %p\n", tv, tv->columns );
1170 return ERROR_FUNCTION_FAILED;
1173 n = bytes_per_column( &tv->columns[col-1] );
1174 switch( n )
1176 case 4:
1177 offset = tv->columns[col-1].offset/2;
1178 tv->table->data[row][offset] = val & 0xffff;
1179 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1180 break;
1181 case 2:
1182 offset = tv->columns[col-1].offset/2;
1183 tv->table->data[row][offset] = val;
1184 break;
1185 default:
1186 ERR("oops! what is %d bytes per column?\n", n );
1187 return ERROR_FUNCTION_FAILED;
1189 return ERROR_SUCCESS;
1192 static UINT table_create_new_row( struct tagMSIVIEW *view, UINT *num )
1194 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1195 USHORT **p, *row;
1196 UINT sz;
1198 TRACE("%p\n", view);
1200 if( !tv->table )
1201 return ERROR_INVALID_PARAMETER;
1203 row = msi_alloc_zero( tv->row_size );
1204 if( !row )
1205 return ERROR_NOT_ENOUGH_MEMORY;
1207 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1208 if( tv->table->data )
1209 p = msi_realloc( tv->table->data, sz );
1210 else
1211 p = msi_alloc( sz );
1212 if( !p )
1214 msi_free( row );
1215 return ERROR_NOT_ENOUGH_MEMORY;
1218 tv->table->data = p;
1219 tv->table->data[tv->table->row_count] = row;
1220 *num = tv->table->row_count;
1221 tv->table->row_count++;
1223 return ERROR_SUCCESS;
1226 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1228 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1229 UINT r;
1231 TRACE("%p %p\n", tv, record);
1233 if( tv->table )
1235 release_table( tv->db, tv->table );
1236 tv->table = NULL;
1239 r = get_table( tv->db, tv->name, &tv->table );
1240 if( r != ERROR_SUCCESS )
1241 return r;
1243 return ERROR_SUCCESS;
1246 static UINT TABLE_close( struct tagMSIVIEW *view )
1248 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1250 TRACE("%p\n", view );
1252 if( !tv->table )
1253 return ERROR_FUNCTION_FAILED;
1255 release_table( tv->db, tv->table );
1256 tv->table = NULL;
1258 return ERROR_SUCCESS;
1261 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1263 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1265 TRACE("%p %p %p\n", view, rows, cols );
1267 if( cols )
1268 *cols = tv->num_cols;
1269 if( rows )
1271 if( !tv->table )
1272 return ERROR_INVALID_PARAMETER;
1273 *rows = tv->table->row_count;
1276 return ERROR_SUCCESS;
1279 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1280 UINT n, LPWSTR *name, UINT *type )
1282 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1284 TRACE("%p %d %p %p\n", tv, n, name, type );
1286 if( ( n == 0 ) || ( n > tv->num_cols ) )
1287 return ERROR_INVALID_PARAMETER;
1289 if( name )
1291 *name = strdupW( tv->columns[n-1].colname );
1292 if( !*name )
1293 return ERROR_FUNCTION_FAILED;
1295 if( type )
1296 *type = tv->columns[n-1].type;
1298 return ERROR_SUCCESS;
1301 static UINT table_find_in_column( MSITABLEVIEW *tv, UINT col, UINT val, UINT *row )
1303 UINT i, r, x;
1305 for( i=0; i<tv->table->row_count; i++ )
1307 r = TABLE_fetch_int( (struct tagMSIVIEW*) tv, i, col, &x );
1308 if ( r != ERROR_SUCCESS )
1310 ERR("TABLE_fetch_int shouldn't fail here\n");
1311 break;
1313 if ( x == val )
1315 *row = i;
1316 return ERROR_SUCCESS;
1319 return ERROR_FUNCTION_FAILED;
1322 static UINT table_validate_new( MSITABLEVIEW *tv, MSIRECORD *rec )
1324 LPCWSTR str;
1325 UINT i, val, r, row;
1326 BOOL has_key = FALSE;
1328 /* FIXME: set the MsiViewGetError value */
1330 for( i = 0; i<tv->num_cols; i++ )
1332 /* check for duplicate keys */
1333 if( !( tv->columns[i].type & MSITYPE_KEY ) )
1334 continue;
1336 has_key = TRUE;
1338 TRACE("column %d (%s.%s)is a key\n", i,
1339 debugstr_w(tv->columns[i].tablename),
1340 debugstr_w(tv->columns[i].colname) );
1342 val = 0;
1343 if( tv->columns[i].type & MSITYPE_STRING )
1345 /* keys can't be null */
1346 str = MSI_RecordGetString( rec, i+1 );
1347 if( !str )
1348 return ERROR_INVALID_DATA;
1350 /* if the string doesn't exist in the string table yet, it's OK */
1351 r = msi_string2idW( tv->db->strings, str, &val );
1352 if( ERROR_SUCCESS != r )
1353 return ERROR_SUCCESS;
1355 else
1357 val = MSI_RecordGetInteger( rec, i+1 );
1358 val ^= 0x8000;
1361 /* if we find the same value in the table, it's a duplicate */
1362 row = 0;
1363 r = table_find_in_column( tv, i+1, val, &row );
1364 if( ERROR_SUCCESS != r )
1365 return ERROR_SUCCESS;
1367 TRACE("found in row %d\n", row );
1370 if (has_key)
1371 return ERROR_INVALID_DATA;
1373 return ERROR_SUCCESS;
1376 static UINT TABLE_insert_row( struct tagMSIVIEW *view, MSIRECORD *rec )
1378 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1379 UINT n, type, val, r, row, col_count = 0;
1381 r = TABLE_get_dimensions( view, NULL, &col_count );
1382 if( r )
1383 return r;
1385 row = -1;
1386 r = table_create_new_row( view, &row );
1387 TRACE("insert_row returned %08x\n", r);
1388 if( r )
1389 return r;
1391 for( n = 1; n <= col_count; n++ )
1393 r = TABLE_get_column_info( view, n, NULL, &type );
1394 if( r )
1395 break;
1397 if( type & MSITYPE_STRING )
1399 const WCHAR *str = MSI_RecordGetString( rec, n );
1400 val = msi_addstringW( tv->db->strings, 0, str, -1, 1 );
1402 else
1404 val = MSI_RecordGetInteger( rec, n );
1405 val ^= 0x8000;
1407 r = TABLE_set_int( view, row, n, val );
1408 if( r )
1409 break;
1412 return r;
1415 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
1416 MSIRECORD *rec)
1418 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1419 UINT r;
1421 TRACE("%p %d %p\n", view, eModifyMode, rec );
1423 if (!tv->table)
1425 r = TABLE_execute( view, NULL );
1426 if( r )
1427 return r;
1430 switch (eModifyMode)
1432 case MSIMODIFY_VALIDATE_NEW:
1433 r = table_validate_new( tv, rec );
1434 break;
1436 case MSIMODIFY_INSERT_TEMPORARY:
1437 r = table_validate_new( tv, rec );
1438 if (r != ERROR_SUCCESS)
1439 break;
1440 r = TABLE_insert_row( view, rec );
1441 break;
1443 case MSIMODIFY_REFRESH:
1444 case MSIMODIFY_INSERT:
1445 case MSIMODIFY_UPDATE:
1446 case MSIMODIFY_ASSIGN:
1447 case MSIMODIFY_REPLACE:
1448 case MSIMODIFY_MERGE:
1449 case MSIMODIFY_DELETE:
1450 case MSIMODIFY_VALIDATE:
1451 case MSIMODIFY_VALIDATE_FIELD:
1452 case MSIMODIFY_VALIDATE_DELETE:
1453 FIXME("%p %d %p - mode not implemented\n", view, eModifyMode, rec );
1454 r = ERROR_CALL_NOT_IMPLEMENTED;
1455 break;
1457 default:
1458 r = ERROR_INVALID_DATA;
1461 return r;
1464 static UINT TABLE_delete( struct tagMSIVIEW *view )
1466 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1468 TRACE("%p\n", view );
1470 if( tv->table )
1471 release_table( tv->db, tv->table );
1472 tv->table = NULL;
1474 if( tv->columns )
1476 UINT i;
1477 for( i=0; i<tv->num_cols; i++)
1479 msi_free( tv->columns[i].colname );
1480 msi_free( tv->columns[i].tablename );
1482 msi_free( tv->columns );
1484 tv->columns = NULL;
1486 msi_free( tv );
1488 return ERROR_SUCCESS;
1492 MSIVIEWOPS table_ops =
1494 TABLE_fetch_int,
1495 TABLE_fetch_stream,
1496 TABLE_set_int,
1497 TABLE_insert_row,
1498 TABLE_execute,
1499 TABLE_close,
1500 TABLE_get_dimensions,
1501 TABLE_get_column_info,
1502 TABLE_modify,
1503 TABLE_delete
1506 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1508 MSITABLEVIEW *tv ;
1509 UINT r, sz, column_count;
1510 MSICOLUMNINFO *columns, *last_col;
1512 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1514 /* get the number of columns in this table */
1515 column_count = 0;
1516 r = get_tablecolumns( db, name, NULL, &column_count );
1517 if( r != ERROR_SUCCESS )
1518 return r;
1520 /* if there's no columns, there's no table */
1521 if( column_count == 0 )
1522 return ERROR_INVALID_PARAMETER;
1524 TRACE("Table found\n");
1526 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1527 tv = msi_alloc_zero( sz );
1528 if( !tv )
1529 return ERROR_FUNCTION_FAILED;
1531 columns = msi_alloc( column_count*sizeof (MSICOLUMNINFO));
1532 if( !columns )
1534 msi_free( tv );
1535 return ERROR_FUNCTION_FAILED;
1538 r = get_tablecolumns( db, name, columns, &column_count );
1539 if( r != ERROR_SUCCESS )
1541 msi_free( columns );
1542 msi_free( tv );
1543 return ERROR_FUNCTION_FAILED;
1546 TRACE("Table has %d columns\n", column_count);
1548 last_col = &columns[column_count-1];
1550 /* fill the structure */
1551 tv->view.ops = &table_ops;
1552 tv->db = db;
1553 tv->columns = columns;
1554 tv->num_cols = column_count;
1555 tv->table = NULL;
1556 tv->row_size = last_col->offset + bytes_per_column( last_col );
1558 TRACE("one row is %d bytes\n", tv->row_size );
1560 *view = (MSIVIEW*) tv;
1561 lstrcpyW( tv->name, name );
1563 return ERROR_SUCCESS;
1566 UINT MSI_CommitTables( MSIDATABASE *db )
1568 UINT r;
1569 MSITABLE *table = NULL;
1571 TRACE("%p\n",db);
1573 r = save_string_table( db );
1574 if( r != ERROR_SUCCESS )
1576 WARN("failed to save string table r=%08x\n",r);
1577 return r;
1580 LIST_FOR_EACH_ENTRY( table, &db->tables, MSITABLE, entry )
1582 r = save_table( db, table );
1583 if( r != ERROR_SUCCESS )
1585 WARN("failed to save table %s (r=%08x)\n",
1586 debugstr_w(table->name), r);
1587 return r;
1591 /* force everything to reload next time */
1592 free_cached_tables( db );
1594 return ERROR_SUCCESS;