Moved the WindowFromPoint functionality to the server so that we can
[wine/wine-kai.git] / dlls / msi / table.c
blob0e01a116b7718fd19e74db7530554e8384aa1356
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 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
24 #include <stdarg.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winerror.h"
29 #include "wine/debug.h"
30 #include "msi.h"
31 #include "msiquery.h"
32 #include "objbase.h"
33 #include "objidl.h"
34 #include "msipriv.h"
35 #include "winnls.h"
37 #include "wine/unicode.h"
39 #include "query.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(msi);
43 typedef struct tagMSICOLUMNINFO
45 LPWSTR tablename;
46 UINT number;
47 LPWSTR colname;
48 UINT type;
49 UINT offset;
50 } MSICOLUMNINFO;
52 struct tagMSITABLE
54 USHORT **data;
55 UINT ref_count;
56 UINT row_count;
57 struct tagMSITABLE *next;
58 struct tagMSITABLE *prev;
59 WCHAR name[1];
62 #define MAX_STREAM_NAME 0x1f
64 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name,
65 MSICOLUMNINFO **pcols, UINT *pcount );
66 static UINT get_tablecolumns( MSIDATABASE *db,
67 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz);
69 static inline UINT bytes_per_column( MSICOLUMNINFO *col )
71 if( col->type & MSITYPE_STRING )
72 return 2;
73 if( (col->type & 0xff) > 4 )
74 ERR("Invalid column size!\n");
75 return col->type & 0xff;
78 static int utf2mime(int x)
80 if( (x>='0') && (x<='9') )
81 return x-'0';
82 if( (x>='A') && (x<='Z') )
83 return x-'A'+10;
84 if( (x>='a') && (x<='z') )
85 return x-'a'+10+26;
86 if( x=='.' )
87 return 10+26+26;
88 if( x=='_' )
89 return 10+26+26+1;
90 return -1;
93 static LPWSTR encode_streamname(BOOL bTable, LPCWSTR in)
95 DWORD count = MAX_STREAM_NAME;
96 DWORD ch, next;
97 LPWSTR out, p;
99 if( !bTable )
100 count = strlenW( in )+2;
101 out = HeapAlloc( GetProcessHeap(), 0, count*sizeof(WCHAR) );
102 p = out;
104 if( bTable )
106 *p++ = 0x4840;
107 count --;
109 while( count -- )
111 ch = *in++;
112 if( !ch )
114 *p = ch;
115 return out;
117 if( ( ch < 0x80 ) && ( utf2mime(ch) >= 0 ) )
119 ch = utf2mime(ch) + 0x4800;
120 next = *in;
121 if( next && (next<0x80) )
123 next = utf2mime(next);
124 if( next >= 0 )
126 next += 0x3ffffc0;
127 ch += (next<<6);
128 in++;
132 *p++ = ch;
134 ERR("Failed to encode stream name (%s)\n",debugstr_w(in));
135 HeapFree( GetProcessHeap(), 0, out );
136 return NULL;
139 static int mime2utf(int x)
141 if( x<10 )
142 return x + '0';
143 if( x<(10+26))
144 return x - 10 + 'A';
145 if( x<(10+26+26))
146 return x - 10 - 26 + 'a';
147 if( x == (10+26+26) )
148 return '.';
149 return '_';
152 static BOOL decode_streamname(LPWSTR in, LPWSTR out)
154 WCHAR ch;
155 DWORD count = 0;
157 while ( (ch = *in++) )
159 if( (ch >= 0x3800 ) && (ch < 0x4840 ) )
161 if( ch >= 0x4800 )
162 ch = mime2utf(ch-0x4800);
163 else
165 ch -= 0x3800;
166 *out++ = mime2utf(ch&0x3f);
167 count++;
168 ch = mime2utf((ch>>6)&0x3f);
171 *out++ = ch;
172 count++;
174 *out = 0;
175 return count;
178 void enum_stream_names( IStorage *stg )
180 IEnumSTATSTG *stgenum = NULL;
181 HRESULT r;
182 STATSTG stat;
183 ULONG n, count;
184 WCHAR name[0x40];
186 r = IStorage_EnumElements( stg, 0, NULL, 0, &stgenum );
187 if( FAILED( r ) )
188 return;
190 n = 0;
191 while( 1 )
193 count = 0;
194 r = IEnumSTATSTG_Next( stgenum, 1, &stat, &count );
195 if( FAILED( r ) || !count )
196 break;
197 decode_streamname( stat.pwcsName, name );
198 ERR("stream %2ld -> %s %s\n", n,
199 debugstr_w(stat.pwcsName), debugstr_w(name) );
200 n++;
203 IEnumSTATSTG_Release( stgenum );
206 static UINT read_stream_data( IStorage *stg, LPCWSTR stname,
207 USHORT **pdata, UINT *psz )
209 HRESULT r;
210 UINT ret = ERROR_FUNCTION_FAILED;
211 VOID *data;
212 ULONG sz, count;
213 IStream *stm = NULL;
214 STATSTG stat;
215 LPWSTR encname;
217 encname = encode_streamname(TRUE, stname);
219 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
221 r = IStorage_OpenStream(stg, encname, NULL,
222 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &stm);
223 HeapFree( GetProcessHeap(), 0, encname );
224 if( FAILED( r ) )
226 WARN("open stream failed r = %08lx - empty table?\n",r);
227 return ret;
230 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
231 if( FAILED( r ) )
233 ERR("open stream failed r = %08lx!\n",r);
234 goto end;
237 if( stat.cbSize.QuadPart >> 32 )
239 ERR("Too big!\n");
240 goto end;
243 sz = stat.cbSize.QuadPart;
244 data = HeapAlloc( GetProcessHeap(), 0, sz );
245 if( !data )
247 ERR("couldn't allocate memory r=%08lx!\n",r);
248 ret = ERROR_NOT_ENOUGH_MEMORY;
249 goto end;
252 r = IStream_Read(stm, data, sz, &count );
253 if( FAILED( r ) || ( count != sz ) )
255 HeapFree( GetProcessHeap(), 0, data );
256 ERR("read stream failed r = %08lx!\n",r);
257 goto end;
260 *pdata = data;
261 *psz = sz;
262 ret = ERROR_SUCCESS;
264 end:
265 IStream_Release( stm );
267 return ret;
270 UINT db_get_raw_stream( MSIDATABASE *db, LPCWSTR stname, IStream **stm )
272 LPWSTR encname;
273 HRESULT r;
275 encname = encode_streamname(FALSE, stname);
277 TRACE("%s -> %s\n",debugstr_w(stname),debugstr_w(encname));
279 r = IStorage_OpenStream(db->storage, encname, NULL,
280 STGM_READ | STGM_SHARE_EXCLUSIVE, 0, stm);
281 HeapFree( GetProcessHeap(), 0, encname );
282 if( FAILED( r ) )
284 WARN("open stream failed r = %08lx - empty table?\n",r);
285 return ERROR_FUNCTION_FAILED;
288 return ERROR_SUCCESS;
291 UINT read_raw_stream_data( MSIDATABASE *db, LPCWSTR stname,
292 USHORT **pdata, UINT *psz )
294 HRESULT r;
295 UINT ret = ERROR_FUNCTION_FAILED;
296 VOID *data;
297 ULONG sz, count;
298 IStream *stm = NULL;
299 STATSTG stat;
301 r = db_get_raw_stream( db, stname, &stm );
302 if( r != ERROR_SUCCESS)
303 return ret;
304 r = IStream_Stat(stm, &stat, STATFLAG_NONAME );
305 if( FAILED( r ) )
307 ERR("open stream failed r = %08lx!\n",r);
308 goto end;
311 if( stat.cbSize.QuadPart >> 32 )
313 ERR("Too big!\n");
314 goto end;
317 sz = stat.cbSize.QuadPart;
318 data = HeapAlloc( GetProcessHeap(), 0, sz );
319 if( !data )
321 ERR("couldn't allocate memory r=%08lx!\n",r);
322 ret = ERROR_NOT_ENOUGH_MEMORY;
323 goto end;
326 r = IStream_Read(stm, data, sz, &count );
327 if( FAILED( r ) || ( count != sz ) )
329 HeapFree( GetProcessHeap(), 0, data );
330 ERR("read stream failed r = %08lx!\n",r);
331 goto end;
334 *pdata = data;
335 *psz = sz;
336 ret = ERROR_SUCCESS;
338 end:
339 IStream_Release( stm );
341 return ret;
344 static UINT write_stream_data( IStorage *stg, LPCWSTR stname,
345 LPVOID data, UINT sz )
347 HRESULT r;
348 UINT ret = ERROR_FUNCTION_FAILED;
349 ULONG count;
350 IStream *stm = NULL;
351 ULARGE_INTEGER size;
352 LARGE_INTEGER pos;
353 LPWSTR encname;
355 encname = encode_streamname(TRUE, stname );
356 r = IStorage_OpenStream( stg, encname, NULL,
357 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, &stm);
358 HeapFree( GetProcessHeap(), 0, encname );
359 if( FAILED(r) )
361 r = IStorage_CreateStream( stg, encname,
362 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
364 if( FAILED( r ) )
366 ERR("open stream failed r = %08lx\n",r);
367 return ret;
370 size.QuadPart = sz;
371 r = IStream_SetSize( stm, size );
372 if( FAILED( r ) )
374 ERR("Failed to SetSize\n");
375 goto end;
378 pos.QuadPart = 0;
379 r = IStream_Seek( stm, pos, STREAM_SEEK_SET, NULL );
380 if( FAILED( r ) )
382 ERR("Failed to Seek\n");
383 goto end;
386 r = IStream_Write(stm, data, sz, &count );
387 if( FAILED( r ) || ( count != sz ) )
389 ERR("Failed to Write\n");
390 goto end;
393 ret = ERROR_SUCCESS;
395 end:
396 IStream_Release( stm );
398 return ret;
401 UINT read_table_from_storage( MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
403 MSITABLE *t;
404 USHORT *rawdata = NULL;
405 UINT rawsize = 0, r, i, j, row_size = 0, num_cols = 0;
406 MSICOLUMNINFO *cols, *last_col;
408 TRACE("%s\n",debugstr_w(name));
410 /* non-existing tables should be interpretted as empty tables */
411 t = HeapAlloc( GetProcessHeap(), 0,
412 sizeof (MSITABLE) + lstrlenW(name)*sizeof (WCHAR) );
413 if( !t )
414 return ERROR_NOT_ENOUGH_MEMORY;
416 r = table_get_column_info( db, name, &cols, &num_cols );
417 if( r != ERROR_SUCCESS )
419 HeapFree( GetProcessHeap(), 0, t );
420 return r;
422 last_col = &cols[num_cols-1];
423 row_size = last_col->offset + bytes_per_column( last_col );
425 t->row_count = 0;
426 t->data = NULL;
427 lstrcpyW( t->name, name );
428 t->ref_count = 1;
429 *ptable = t;
431 /* if we can't read the table, just assume that it's empty */
432 read_stream_data( db->storage, name, &rawdata, &rawsize );
433 if( !rawdata )
434 return ERROR_SUCCESS;
436 TRACE("Read %d bytes\n", rawsize );
438 if( rawsize % row_size )
440 ERR("Table size is invalid %d/%d\n", rawsize, row_size );
441 return ERROR_FUNCTION_FAILED;
444 t->row_count = rawsize / row_size;
445 t->data = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY,
446 t->row_count * sizeof (USHORT*) );
447 if( !t->data )
448 return ERROR_NOT_ENOUGH_MEMORY; /* FIXME: memory leak */
450 /* transpose all the data */
451 TRACE("Transposing data from %d columns\n", t->row_count );
452 for( i=0; i<t->row_count; i++ )
454 t->data[i] = HeapAlloc( GetProcessHeap(), 0, row_size );
455 if( !t->data[i] )
456 return ERROR_NOT_ENOUGH_MEMORY; /* FIXME: memory leak */
457 for( j=0; j<num_cols; j++ )
459 UINT ofs = cols[j].offset/2;
460 UINT n = bytes_per_column( &cols[j] );
462 switch( n )
464 case 2:
465 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
466 break;
467 case 4:
468 t->data[i][ofs] = rawdata[ofs*t->row_count + i ];
469 t->data[i][ofs+1] = rawdata[ofs*t->row_count + i + 1];
470 break;
471 default:
472 ERR("oops - unknown column width %d\n", n);
473 return ERROR_FUNCTION_FAILED;
478 HeapFree( GetProcessHeap(), 0, cols );
479 HeapFree( GetProcessHeap(), 0, rawdata );
481 return ERROR_SUCCESS;
484 /* add this table to the list of cached tables in the database */
485 void add_table(MSIDATABASE *db, MSITABLE *table)
487 table->next = db->first_table;
488 table->prev = NULL;
489 if( db->first_table )
490 db->first_table->prev = table;
491 else
492 db->last_table = table;
493 db->first_table = table;
496 /* remove from the list of cached tables */
497 void remove_table( MSIDATABASE *db, MSITABLE *table )
499 if( table->next )
500 table->next->prev = table->prev;
501 else
502 db->last_table = table->prev;
503 if( table->prev )
504 table->prev->next = table->next;
505 else
506 db->first_table = table->next;
507 table->next = NULL;
508 table->prev = NULL;
511 void release_table( MSIDATABASE *db, MSITABLE *table )
513 if( !table->ref_count )
514 ERR("Trying to destroy table with refcount 0\n");
515 table->ref_count --;
516 if( !table->ref_count )
518 remove_table( db, table );
519 HeapFree( GetProcessHeap(), 0, table->data );
520 HeapFree( GetProcessHeap(), 0, table );
521 TRACE("Destroyed table %s\n", debugstr_w(table->name));
525 void free_cached_tables( MSIDATABASE *db )
527 while( db->first_table )
529 MSITABLE *t = db->first_table;
531 if ( --t->ref_count )
532 ERR("table ref count not zero for %s\n", debugstr_w(t->name));
533 remove_table( db, t );
534 HeapFree( GetProcessHeap(), 0, t->data );
535 HeapFree( GetProcessHeap(), 0, t );
539 UINT find_cached_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
541 MSITABLE *t;
543 for( t = db->first_table; t; t=t->next )
545 if( !lstrcmpW( name, t->name ) )
547 *ptable = t;
548 return ERROR_SUCCESS;
552 return ERROR_FUNCTION_FAILED;
555 static UINT table_get_column_info( MSIDATABASE *db, LPCWSTR name, MSICOLUMNINFO **pcols, UINT *pcount )
557 UINT r, column_count;
558 MSICOLUMNINFO *columns;
560 /* get the number of columns in this table */
561 column_count = 0;
562 r = get_tablecolumns( db, name, NULL, &column_count );
563 if( r != ERROR_SUCCESS )
564 return r;
566 /* if there's no columns, there's no table */
567 if( column_count == 0 )
568 return ERROR_INVALID_PARAMETER;
570 TRACE("Table %s found\n", debugstr_w(name) );
572 columns = HeapAlloc( GetProcessHeap(), 0, column_count*sizeof (MSICOLUMNINFO));
573 if( !columns )
574 return ERROR_FUNCTION_FAILED;
576 r = get_tablecolumns( db, name, columns, &column_count );
577 if( r != ERROR_SUCCESS )
579 HeapFree( GetProcessHeap(), 0, columns );
580 return ERROR_FUNCTION_FAILED;
583 *pcols = columns;
584 *pcount = column_count;
586 return r;
589 UINT get_table(MSIDATABASE *db, LPCWSTR name, MSITABLE **ptable)
591 UINT r;
593 *ptable = NULL;
595 /* first, see if the table is cached */
596 r = find_cached_table( db, name, ptable );
597 if( r == ERROR_SUCCESS )
599 (*ptable)->ref_count++;
600 return r;
603 r = read_table_from_storage( db, name, ptable );
604 if( r != ERROR_SUCCESS )
605 return r;
607 /* add the table to the list */
608 add_table( db, *ptable );
609 (*ptable)->ref_count++;
611 return ERROR_SUCCESS;
614 UINT save_table( MSIDATABASE *db, MSITABLE *t )
616 USHORT *rawdata = NULL, *p;
617 UINT rawsize, r, i, j, row_size, num_cols = 0;
618 MSICOLUMNINFO *cols, *last_col;
620 TRACE("Saving %s\n", debugstr_w( t->name ) );
622 r = table_get_column_info( db, t->name, &cols, &num_cols );
623 if( r != ERROR_SUCCESS )
624 return r;
626 last_col = &cols[num_cols-1];
627 row_size = last_col->offset + bytes_per_column( last_col );
629 rawsize = t->row_count * row_size;
630 rawdata = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, rawsize );
631 if( !rawdata )
632 return ERROR_NOT_ENOUGH_MEMORY;
634 p = rawdata;
635 for( i=0; i<num_cols; i++ )
637 for( j=0; j<t->row_count; j++ )
639 UINT offset = cols[i].offset;
641 *p++ = t->data[j][offset/2];
642 if( 4 == bytes_per_column( &cols[i] ) )
643 *p++ = t->data[j][offset/2+1];
647 TRACE("writing %d bytes\n", rawsize);
648 r = write_stream_data( db->storage, t->name, rawdata, rawsize );
650 HeapFree( GetProcessHeap(), 0, rawdata );
652 return r;
655 HRESULT init_string_table( IStorage *stg )
657 HRESULT r;
658 static const WCHAR szStringData[] = {
659 '_','S','t','r','i','n','g','D','a','t','a',0 };
660 static const WCHAR szStringPool[] = {
661 '_','S','t','r','i','n','g','P','o','o','l',0 };
662 USHORT zero[2] = { 0, 0 };
663 ULONG count = 0;
664 IStream *stm = NULL;
665 LPWSTR encname;
667 encname = encode_streamname(TRUE, szStringPool );
669 /* create the StringPool stream... add the zero string to it*/
670 r = IStorage_CreateStream( stg, encname,
671 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
672 HeapFree( GetProcessHeap(), 0, encname );
673 if( r )
675 TRACE("Failed\n");
676 return r;
679 r = IStream_Write(stm, zero, sizeof zero, &count );
680 IStream_Release( stm );
682 if( FAILED( r ) || ( count != sizeof zero ) )
684 TRACE("Failed\n");
685 return E_FAIL;
688 /* create the StringData stream... make it zero length */
689 encname = encode_streamname(TRUE, szStringData );
690 r = IStorage_CreateStream( stg, encname,
691 STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &stm);
692 HeapFree( GetProcessHeap(), 0, encname );
693 if( r )
695 TRACE("Failed\n");
696 return E_FAIL;
698 IStream_Release( stm );
700 return r;
703 UINT load_string_table( MSIDATABASE *db )
705 CHAR *data;
706 USHORT *pool;
707 UINT r, ret = ERROR_FUNCTION_FAILED, datasize = 0, poolsize = 0, codepage;
708 DWORD i, count, offset, len, n;
709 static const WCHAR szStringData[] = {
710 '_','S','t','r','i','n','g','D','a','t','a',0 };
711 static const WCHAR szStringPool[] = {
712 '_','S','t','r','i','n','g','P','o','o','l',0 };
714 if( db->strings )
716 msi_destroy_stringtable( db->strings );
717 db->strings = NULL;
720 r = read_stream_data( db->storage, szStringPool, &pool, &poolsize );
721 if( r != ERROR_SUCCESS)
722 goto end;
723 r = read_stream_data( db->storage, szStringData, (USHORT**)&data, &datasize );
724 if( r != ERROR_SUCCESS)
725 goto end;
727 count = poolsize/4;
728 if( poolsize > 4 )
729 codepage = pool[0] | ( pool[1] << 16 );
730 else
731 codepage = CP_ACP;
732 db->strings = msi_init_stringtable( count, codepage );
734 offset = 0;
735 for( i=1; i<count; i++ )
737 len = pool[i*2];
738 n = msi_addstring( db->strings, i, data+offset, len, pool[i*2+1] );
739 if( n != i )
740 ERR("Failed to add string %ld\n", i );
741 offset += len;
744 TRACE("Loaded %ld strings\n", count);
746 ret = ERROR_SUCCESS;
748 end:
749 if( pool )
750 HeapFree( GetProcessHeap(), 0, pool );
751 if( data )
752 HeapFree( GetProcessHeap(), 0, data );
754 return ret;
757 UINT save_string_table( MSIDATABASE *db )
759 UINT i, count, datasize, poolsize, sz, used, r, codepage;
760 UINT ret = ERROR_FUNCTION_FAILED;
761 static const WCHAR szStringData[] = {
762 '_','S','t','r','i','n','g','D','a','t','a',0 };
763 static const WCHAR szStringPool[] = {
764 '_','S','t','r','i','n','g','P','o','o','l',0 };
765 CHAR *data = NULL;
766 USHORT *pool = NULL;
768 TRACE("\n");
770 /* construct the new table in memory first */
771 datasize = msi_string_totalsize( db->strings, &count );
772 poolsize = count*2*sizeof(USHORT);
774 pool = HeapAlloc( GetProcessHeap(), 0, poolsize );
775 if( ! pool )
777 ERR("Failed to alloc pool %d bytes\n", poolsize );
778 goto err;
780 data = HeapAlloc( GetProcessHeap(), 0, datasize );
781 if( ! data )
783 ERR("Failed to alloc data %d bytes\n", poolsize );
784 goto err;
787 used = 0;
788 codepage = msi_string_get_codepage( db->strings );
789 pool[0]=codepage&0xffff;
790 pool[1]=(codepage>>16);
791 for( i=1; i<count; i++ )
793 sz = datasize - used;
794 r = msi_id2stringA( db->strings, i, data+used, &sz );
795 if( r != ERROR_SUCCESS )
797 ERR("failed to fetch string\n");
798 sz = 0;
800 if( sz && (sz < (datasize - used ) ) )
801 sz--;
802 TRACE("adding %u bytes %s\n", sz, data+used );
803 pool[ i*2 ] = sz;
804 pool[ i*2 + 1 ] = msi_id_refcount( db->strings, i );
805 used += sz;
806 if( used > datasize )
808 ERR("oops overran %d >= %d\n", used, datasize);
809 goto err;
813 if( used != datasize )
815 ERR("oops used %d != datasize %d\n", used, datasize);
816 goto err;
819 /* write the streams */
820 r = write_stream_data( db->storage, szStringData, data, datasize );
821 TRACE("Wrote StringData r=%08x\n", r);
822 if( r )
823 goto err;
824 r = write_stream_data( db->storage, szStringPool, pool, poolsize );
825 TRACE("Wrote StringPool r=%08x\n", r);
826 if( r )
827 goto err;
829 ret = ERROR_SUCCESS;
831 err:
832 if( data )
833 HeapFree( GetProcessHeap(), 0, data );
834 if( pool )
835 HeapFree( GetProcessHeap(), 0, pool );
837 return ret;
840 static LPWSTR strdupW( LPCWSTR str )
842 UINT len = lstrlenW( str ) + 1;
843 LPWSTR ret = HeapAlloc( GetProcessHeap(), 0, len*sizeof (WCHAR) );
844 if( ret )
845 lstrcpyW( ret, str );
846 return ret;
849 /* information for default tables */
850 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
851 static const WCHAR szTable[] = { 'T','a','b','l','e',0 };
852 static const WCHAR szName[] = { 'N','a','m','e',0 };
853 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
854 static const WCHAR szColumn[] = { 'C','o','l','u','m','n',0 };
855 static const WCHAR szNumber[] = { 'N','u','m','b','e','r',0 };
856 static const WCHAR szType[] = { 'T','y','p','e',0 };
858 struct standard_table {
859 LPCWSTR tablename;
860 LPCWSTR columnname;
861 UINT number;
862 UINT type;
863 } MSI_standard_tables[] =
865 { szTables, szName, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
866 { szColumns, szTable, 1, MSITYPE_VALID | MSITYPE_STRING | 32},
867 { szColumns, szNumber, 2, MSITYPE_VALID | 2},
868 { szColumns, szName, 3, MSITYPE_VALID | MSITYPE_STRING | 32},
869 { szColumns, szType, 4, MSITYPE_VALID | 2},
872 #define STANDARD_TABLE_COUNT \
873 (sizeof(MSI_standard_tables)/sizeof(struct standard_table))
875 UINT get_defaulttablecolumns( LPCWSTR szTable, MSICOLUMNINFO *colinfo, UINT *sz)
877 DWORD i, n=0;
879 for(i=0; i<STANDARD_TABLE_COUNT; i++)
881 if( lstrcmpW( szTable, MSI_standard_tables[i].tablename ) )
882 continue;
883 if(colinfo && (n < *sz) )
885 colinfo[n].tablename = strdupW(MSI_standard_tables[i].tablename);
886 colinfo[n].colname = strdupW(MSI_standard_tables[i].columnname);
887 colinfo[n].number = MSI_standard_tables[i].number;
888 colinfo[n].type = MSI_standard_tables[i].type;
889 /* ERR("Table %s has column %s\n",debugstr_w(colinfo[n].tablename),
890 debugstr_w(colinfo[n].colname)); */
891 if( n )
892 colinfo[n].offset = colinfo[n-1].offset
893 + bytes_per_column( &colinfo[n-1] );
894 else
895 colinfo[n].offset = 0;
897 n++;
898 if( colinfo && (n >= *sz) )
899 break;
901 *sz = n;
902 return ERROR_SUCCESS;
905 LPWSTR MSI_makestring( MSIDATABASE *db, UINT stringid)
907 UINT sz=0, r;
908 LPWSTR str;
910 r = msi_id2stringW( db->strings, stringid, NULL, &sz );
911 if( r != ERROR_SUCCESS )
912 return NULL;
913 str = HeapAlloc( GetProcessHeap(), 0, sz*sizeof (WCHAR));
914 if( !str )
915 return str;
916 r = msi_id2stringW( db->strings, stringid, str, &sz );
917 if( r == ERROR_SUCCESS )
918 return str;
919 HeapFree( GetProcessHeap(), 0, str );
920 return NULL;
923 static UINT get_tablecolumns( MSIDATABASE *db,
924 LPCWSTR szTableName, MSICOLUMNINFO *colinfo, UINT *sz)
926 UINT r, i, n=0, table_id, count, maxcount = *sz;
927 MSITABLE *table = NULL;
928 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
930 /* first check if there is a default table with that name */
931 r = get_defaulttablecolumns( szTableName, colinfo, sz );
932 if( ( r == ERROR_SUCCESS ) && *sz )
933 return r;
935 r = get_table( db, szColumns, &table);
936 if( r != ERROR_SUCCESS )
938 ERR("table %s not available\n", debugstr_w(szColumns));
939 return r;
942 /* convert table and column names to IDs from the string table */
943 r = msi_string2idW( db->strings, szTableName, &table_id );
944 if( r != ERROR_SUCCESS )
946 release_table( db, table );
947 ERR("Couldn't find id for %s\n", debugstr_w(szTableName));
948 return r;
951 TRACE("Table id is %d\n", table_id);
953 count = table->row_count;
954 for( i=0; i<count; i++ )
956 if( table->data[ i ][ 0 ] != table_id )
957 continue;
958 if( colinfo )
960 UINT id = table->data[ i ] [ 2 ];
961 colinfo[n].tablename = MSI_makestring( db, table_id );
962 colinfo[n].number = table->data[ i ][ 1 ] - (1<<15);
963 colinfo[n].colname = MSI_makestring( db, id );
964 colinfo[n].type = table->data[ i ] [ 3 ];
965 /* this assumes that columns are in order in the table */
966 if( n )
967 colinfo[n].offset = colinfo[n-1].offset
968 + bytes_per_column( &colinfo[n-1] );
969 else
970 colinfo[n].offset = 0;
971 TRACE("table %s column %d is [%s] (%d) with type %08x "
972 "offset %d at row %d\n", debugstr_w(szTableName),
973 colinfo[n].number, debugstr_w(colinfo[n].colname),
974 id, colinfo[n].type, colinfo[n].offset, i);
975 if( n != (colinfo[n].number-1) )
977 ERR("oops. data in the _Columns table isn't in the right "
978 "order for table %s\n", debugstr_w(szTableName));
979 return ERROR_FUNCTION_FAILED;
982 n++;
983 if( colinfo && ( n >= maxcount ) )
984 break;
986 *sz = n;
988 release_table( db, table );
990 return ERROR_SUCCESS;
993 /* try to find the table name in the _Tables table */
994 BOOL TABLE_Exists( MSIDATABASE *db, LPWSTR name )
996 static const WCHAR szTables[] = { '_','T','a','b','l','e','s',0 };
997 static const WCHAR szColumns[] = { '_','C','o','l','u','m','n','s',0 };
998 UINT r, table_id = 0, i, count;
999 MSITABLE *table = NULL;
1001 if( !lstrcmpW( name, szTables ) )
1002 return TRUE;
1003 if( !lstrcmpW( name, szColumns ) )
1004 return TRUE;
1006 r = msi_string2idW( db->strings, name, &table_id );
1007 if( r != ERROR_SUCCESS )
1009 TRACE("Couldn't find id for %s\n", debugstr_w(name));
1010 return FALSE;
1013 r = get_table( db, szTables, &table);
1014 if( r != ERROR_SUCCESS )
1016 ERR("table %s not available\n", debugstr_w(szTables));
1017 return FALSE;
1020 /* count = table->size/2; */
1021 count = table->row_count;
1022 for( i=0; i<count; i++ )
1023 if( table->data[ i ][ 0 ] == table_id )
1024 break;
1026 release_table( db, table );
1028 if (i!=count)
1029 return TRUE;
1031 ERR("Searched %d tables, but %d was not found\n", count, table_id );
1033 return FALSE;
1036 /* below is the query interface to a table */
1038 typedef struct tagMSITABLEVIEW
1040 MSIVIEW view;
1041 MSIDATABASE *db;
1042 MSITABLE *table;
1043 MSICOLUMNINFO *columns;
1044 UINT num_cols;
1045 UINT row_size;
1046 WCHAR name[1];
1047 } MSITABLEVIEW;
1049 static UINT TABLE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
1051 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1052 UINT offset, num_rows, n;
1054 if( !tv->table )
1055 return ERROR_INVALID_PARAMETER;
1057 if( (col==0) || (col>tv->num_cols) )
1058 return ERROR_INVALID_PARAMETER;
1060 /* how many rows are there ? */
1061 num_rows = tv->table->row_count;
1062 if( row >= num_rows )
1063 return ERROR_NO_MORE_ITEMS;
1065 if( tv->columns[col-1].offset >= tv->row_size )
1067 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1068 ERR("%p %p\n", tv, tv->columns );
1069 return ERROR_FUNCTION_FAILED;
1072 offset = row + (tv->columns[col-1].offset/2) * num_rows;
1073 n = bytes_per_column( &tv->columns[col-1] );
1074 switch( n )
1076 case 4:
1077 offset = tv->columns[col-1].offset/2;
1078 *val = tv->table->data[row][offset] +
1079 (tv->table->data[row][offset + 1] << 16);
1080 break;
1081 case 2:
1082 offset = tv->columns[col-1].offset/2;
1083 *val = tv->table->data[row][offset];
1084 break;
1085 default:
1086 ERR("oops! what is %d bytes per column?\n", n );
1087 return ERROR_FUNCTION_FAILED;
1090 TRACE("Data [%d][%d] = %d \n", row, col, *val );
1092 return ERROR_SUCCESS;
1096 * We need a special case for streams, as we need to reference column with
1097 * the name of the stream in the same table, and the table name
1098 * which may not be available at higher levels of the query
1100 static UINT TABLE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
1102 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1103 UINT ival = 0, refcol = 0, r;
1104 LPWSTR sval;
1105 LPWSTR full_name;
1106 DWORD len;
1107 static const WCHAR szDot[] = { '.', 0 };
1109 if( !view->ops->fetch_int )
1110 return ERROR_INVALID_PARAMETER;
1113 * The column marked with the type stream data seems to have a single number
1114 * which references the column containing the name of the stream data
1116 * Fetch the column to reference first.
1118 r = view->ops->fetch_int( view, row, col, &ival );
1119 if( r != ERROR_SUCCESS )
1120 return r;
1122 /* now get the column with the name of the stream */
1123 r = view->ops->fetch_int( view, row, ival, &refcol );
1124 if( r != ERROR_SUCCESS )
1125 return r;
1127 /* lookup the string value from the string table */
1128 sval = MSI_makestring( tv->db, refcol );
1129 if( !sval )
1130 return ERROR_INVALID_PARAMETER;
1132 len = strlenW( tv->name ) + 2 + strlenW( sval );
1133 full_name = HeapAlloc( GetProcessHeap(), 0, len*sizeof(WCHAR) );
1134 strcpyW( full_name, tv->name );
1135 strcatW( full_name, szDot );
1136 strcatW( full_name, sval );
1138 r = db_get_raw_stream( tv->db, full_name, stm );
1139 if( r )
1140 ERR("fetching stream %s, error = %d\n",debugstr_w(full_name), r);
1141 HeapFree( GetProcessHeap(), 0, full_name );
1142 HeapFree( GetProcessHeap(), 0, sval );
1144 return r;
1147 static UINT TABLE_set_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT val )
1149 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1150 UINT offset, n;
1152 if( !tv->table )
1153 return ERROR_INVALID_PARAMETER;
1155 if( (col==0) || (col>tv->num_cols) )
1156 return ERROR_INVALID_PARAMETER;
1158 if( tv->columns[col-1].offset >= tv->row_size )
1160 ERR("Stuffed up %d >= %d\n", tv->columns[col-1].offset, tv->row_size );
1161 ERR("%p %p\n", tv, tv->columns );
1162 return ERROR_FUNCTION_FAILED;
1165 n = bytes_per_column( &tv->columns[col-1] );
1166 switch( n )
1168 case 4:
1169 offset = tv->columns[col-1].offset/2;
1170 tv->table->data[row][offset] = val & 0xffff;
1171 tv->table->data[row][offset + 1] = (val>>16)&0xffff;
1172 break;
1173 case 2:
1174 offset = tv->columns[col-1].offset/2;
1175 tv->table->data[row][offset] = val;
1176 break;
1177 default:
1178 ERR("oops! what is %d bytes per column?\n", n );
1179 return ERROR_FUNCTION_FAILED;
1181 return ERROR_SUCCESS;
1184 UINT TABLE_insert_row( struct tagMSIVIEW *view, UINT *num )
1186 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1187 USHORT **p, *row;
1188 UINT sz;
1190 TRACE("%p\n", view);
1192 if( !tv->table )
1193 return ERROR_INVALID_PARAMETER;
1195 row = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, tv->row_size );
1196 if( !row )
1197 return ERROR_NOT_ENOUGH_MEMORY;
1199 sz = (tv->table->row_count + 1) * sizeof (UINT*);
1200 if( tv->table->data )
1201 p = HeapReAlloc( GetProcessHeap(), 0, tv->table->data, sz );
1202 else
1203 p = HeapAlloc( GetProcessHeap(), 0, sz );
1204 if( !p )
1205 return ERROR_NOT_ENOUGH_MEMORY;
1207 tv->table->data = p;
1208 tv->table->data[tv->table->row_count] = row;
1209 *num = tv->table->row_count;
1210 tv->table->row_count++;
1212 return ERROR_SUCCESS;
1215 static UINT TABLE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
1217 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1218 UINT r;
1220 TRACE("%p %p\n", tv, record);
1222 if( tv->table )
1223 return ERROR_FUNCTION_FAILED;
1225 r = get_table( tv->db, tv->name, &tv->table );
1226 if( r != ERROR_SUCCESS )
1227 return r;
1229 return ERROR_SUCCESS;
1232 static UINT TABLE_close( struct tagMSIVIEW *view )
1234 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1236 TRACE("%p\n", view );
1238 if( !tv->table )
1239 return ERROR_FUNCTION_FAILED;
1241 release_table( tv->db, tv->table );
1242 tv->table = NULL;
1244 return ERROR_SUCCESS;
1247 static UINT TABLE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols)
1249 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1251 TRACE("%p %p %p\n", view, rows, cols );
1253 if( cols )
1254 *cols = tv->num_cols;
1255 if( rows )
1257 if( !tv->table )
1258 return ERROR_INVALID_PARAMETER;
1259 *rows = tv->table->row_count;
1262 return ERROR_SUCCESS;
1265 static UINT TABLE_get_column_info( struct tagMSIVIEW *view,
1266 UINT n, LPWSTR *name, UINT *type )
1268 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1270 TRACE("%p %d %p %p\n", tv, n, name, type );
1272 if( ( n == 0 ) || ( n > tv->num_cols ) )
1273 return ERROR_INVALID_PARAMETER;
1275 if( name )
1277 *name = strdupW( tv->columns[n-1].colname );
1278 if( !*name )
1279 return ERROR_FUNCTION_FAILED;
1281 if( type )
1282 *type = tv->columns[n-1].type;
1284 return ERROR_SUCCESS;
1287 static UINT TABLE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode, MSIHANDLE hrec)
1289 FIXME("%p %d %ld\n", view, eModifyMode, hrec );
1290 return ERROR_CALL_NOT_IMPLEMENTED;
1293 static UINT TABLE_delete( struct tagMSIVIEW *view )
1295 MSITABLEVIEW *tv = (MSITABLEVIEW*)view;
1297 TRACE("%p\n", view );
1299 if( tv->table )
1300 release_table( tv->db, tv->table );
1301 tv->table = NULL;
1303 if( tv->columns )
1305 UINT i;
1306 for( i=0; i<tv->num_cols; i++)
1308 HeapFree( GetProcessHeap(), 0, tv->columns[i].colname );
1309 HeapFree( GetProcessHeap(), 0, tv->columns[i].tablename );
1311 HeapFree( GetProcessHeap(), 0, tv->columns );
1313 tv->columns = NULL;
1315 HeapFree( GetProcessHeap(), 0, tv );
1317 return ERROR_SUCCESS;
1321 MSIVIEWOPS table_ops =
1323 TABLE_fetch_int,
1324 TABLE_fetch_stream,
1325 TABLE_set_int,
1326 TABLE_insert_row,
1327 TABLE_execute,
1328 TABLE_close,
1329 TABLE_get_dimensions,
1330 TABLE_get_column_info,
1331 TABLE_modify,
1332 TABLE_delete
1335 UINT TABLE_CreateView( MSIDATABASE *db, LPCWSTR name, MSIVIEW **view )
1337 MSITABLEVIEW *tv ;
1338 UINT r, sz, column_count;
1339 MSICOLUMNINFO *columns, *last_col;
1341 TRACE("%p %s %p\n", db, debugstr_w(name), view );
1343 /* get the number of columns in this table */
1344 column_count = 0;
1345 r = get_tablecolumns( db, name, NULL, &column_count );
1346 if( r != ERROR_SUCCESS )
1347 return r;
1349 /* if there's no columns, there's no table */
1350 if( column_count == 0 )
1351 return ERROR_INVALID_PARAMETER;
1353 TRACE("Table found\n");
1355 sz = sizeof *tv + lstrlenW(name)*sizeof name[0] ;
1356 tv = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sz );
1357 if( !tv )
1358 return ERROR_FUNCTION_FAILED;
1360 columns = HeapAlloc( GetProcessHeap(), 0, column_count*sizeof (MSICOLUMNINFO));
1361 if( !columns )
1363 HeapFree( GetProcessHeap(), 0, tv );
1364 return ERROR_FUNCTION_FAILED;
1367 r = get_tablecolumns( db, name, columns, &column_count );
1368 if( r != ERROR_SUCCESS )
1370 HeapFree( GetProcessHeap(), 0, columns );
1371 HeapFree( GetProcessHeap(), 0, tv );
1372 return ERROR_FUNCTION_FAILED;
1375 TRACE("Table has %d columns\n", column_count);
1377 last_col = &columns[column_count-1];
1379 /* fill the structure */
1380 tv->view.ops = &table_ops;
1381 tv->db = db;
1382 tv->columns = columns;
1383 tv->num_cols = column_count;
1384 tv->table = NULL;
1385 tv->row_size = last_col->offset + bytes_per_column( last_col );
1387 TRACE("one row is %d bytes\n", tv->row_size );
1389 *view = (MSIVIEW*) tv;
1390 lstrcpyW( tv->name, name );
1392 return ERROR_SUCCESS;
1395 UINT MSI_CommitTables( MSIDATABASE *db )
1397 UINT r;
1398 MSITABLE *table = NULL;
1400 TRACE("%p\n",db);
1402 r = save_string_table( db );
1403 if( r != ERROR_SUCCESS )
1405 ERR("failed to save string table r=%08x\n",r);
1406 return r;
1409 for( table = db->first_table; table; table = table->next )
1411 r = save_table( db, table );
1412 if( r != ERROR_SUCCESS )
1414 ERR("failed to save table %s (r=%08x)\n",
1415 debugstr_w(table->name), r);
1416 return r;
1420 /* force everything to reload next time */
1421 free_cached_tables( db );
1423 return ERROR_SUCCESS;