msi: Initialize the size parameter.
[wine] / dlls / msi / where.c
1 /*
2  * Implementation of the Microsoft Installer (msi.dll)
3  *
4  * Copyright 2002 Mike McCormack for CodeWeavers
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include <stdarg.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winerror.h"
26 #include "wine/debug.h"
27 #include "msi.h"
28 #include "msiquery.h"
29 #include "objbase.h"
30 #include "objidl.h"
31 #include "msipriv.h"
32 #include "winnls.h"
33
34 #include "query.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(msidb);
37
38 #define MSI_HASH_TABLE_SIZE 37
39
40 typedef struct tagMSIHASHENTRY
41 {
42     struct tagMSIHASHENTRY *next;
43     UINT value;
44     UINT row;
45 } MSIHASHENTRY;
46
47 /* below is the query interface to a table */
48
49 typedef struct tagMSIWHEREVIEW
50 {
51     MSIVIEW        view;
52     MSIDATABASE   *db;
53     MSIVIEW       *table;
54     UINT           row_count;
55     MSIHASHENTRY **reorder;
56     struct expr   *cond;
57     UINT           rec_index;
58 } MSIWHEREVIEW;
59
60 static void free_hash_table(MSIHASHENTRY **table)
61 {
62     MSIHASHENTRY *new, *old;
63     int i;
64
65     if (!table)
66         return;
67
68     for (i = 0; i < MSI_HASH_TABLE_SIZE; i++)
69     {
70         new = table[i];
71
72         while (new)
73         {
74             old = new;
75             new = old->next;
76             msi_free(old);
77         }
78
79         table[i] = NULL;
80     }
81
82     msi_free(table);
83 }
84
85 static UINT find_entry_in_hash(MSIHASHENTRY **table, UINT row, UINT *val)
86 {
87     MSIHASHENTRY *entry;
88
89     if (!(entry = table[row % MSI_HASH_TABLE_SIZE]))
90     {
91         WARN("Row not found in hash table!\n");
92         return ERROR_FUNCTION_FAILED;
93     }
94
95     while (entry && entry->row != row)
96         entry = entry->next;
97
98     if (entry) *val = entry->value;
99     return ERROR_SUCCESS;
100 }
101
102 static UINT add_entry_to_hash(MSIHASHENTRY **table, UINT row, UINT val)
103 {
104     MSIHASHENTRY *new = msi_alloc(sizeof(MSIHASHENTRY));
105     MSIHASHENTRY *prev;
106
107     if (!new)
108         return ERROR_OUTOFMEMORY;
109
110     new->next = NULL;
111     new->value = val;
112     new->row = row;
113
114     prev = table[row % MSI_HASH_TABLE_SIZE];
115     if (prev)
116         new->next = prev;
117
118     table[row % MSI_HASH_TABLE_SIZE] = new;
119
120     return ERROR_SUCCESS;
121 }
122
123 static UINT WHERE_fetch_int( struct tagMSIVIEW *view, UINT row, UINT col, UINT *val )
124 {
125     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
126     UINT r;
127
128     TRACE("%p %d %d %p\n", wv, row, col, val );
129
130     if( !wv->table )
131         return ERROR_FUNCTION_FAILED;
132
133     if( row > wv->row_count )
134         return ERROR_NO_MORE_ITEMS;
135
136     r = find_entry_in_hash(wv->reorder, row, &row);
137     if (r != ERROR_SUCCESS)
138         return r;
139
140     return wv->table->ops->fetch_int( wv->table, row, col, val );
141 }
142
143 static UINT WHERE_fetch_stream( struct tagMSIVIEW *view, UINT row, UINT col, IStream **stm )
144 {
145     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
146     UINT r;
147
148     TRACE("%p %d %d %p\n", wv, row, col, stm );
149
150     if( !wv->table )
151         return ERROR_FUNCTION_FAILED;
152
153     if( row > wv->row_count )
154         return ERROR_NO_MORE_ITEMS;
155
156     r = find_entry_in_hash(wv->reorder, row, &row);
157     if (r != ERROR_SUCCESS)
158         return r;
159
160     return wv->table->ops->fetch_stream( wv->table, row, col, stm );
161 }
162
163 static UINT WHERE_get_row( struct tagMSIVIEW *view, UINT row, MSIRECORD **rec )
164 {
165     MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
166     UINT r;
167
168     TRACE("%p %d %p\n", wv, row, rec );
169
170     if (!wv->table)
171         return ERROR_FUNCTION_FAILED;
172
173     if (row > wv->row_count)
174         return ERROR_NO_MORE_ITEMS;
175
176     r = find_entry_in_hash(wv->reorder, row, &row);
177     if (r != ERROR_SUCCESS)
178         return r;
179
180     return wv->table->ops->get_row(view, row, rec);
181 }
182
183 static UINT WHERE_set_row( struct tagMSIVIEW *view, UINT row, MSIRECORD *rec, UINT mask )
184 {
185     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
186     UINT r;
187
188     TRACE("%p %d %p %08x\n", wv, row, rec, mask );
189
190     if( !wv->table )
191          return ERROR_FUNCTION_FAILED;
192
193     if( row > wv->row_count )
194         return ERROR_NO_MORE_ITEMS;
195
196     r = find_entry_in_hash(wv->reorder, row, &row);
197     if (r != ERROR_SUCCESS)
198         return r;
199
200     return wv->table->ops->set_row( wv->table, row, rec, mask );
201 }
202
203 static UINT WHERE_delete_row(struct tagMSIVIEW *view, UINT row)
204 {
205     MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
206     UINT r;
207
208     TRACE("(%p %d)\n", view, row);
209
210     if ( !wv->table )
211         return ERROR_FUNCTION_FAILED;
212
213     if ( row > wv->row_count )
214         return ERROR_NO_MORE_ITEMS;
215
216     r = find_entry_in_hash( wv->reorder, row, &row );
217     if ( r != ERROR_SUCCESS )
218         return r;
219
220     return wv->table->ops->delete_row( wv->table, row );
221 }
222
223 static INT INT_evaluate_binary( INT lval, UINT op, INT rval )
224 {
225     switch( op )
226     {
227     case OP_EQ:
228         return ( lval == rval );
229     case OP_AND:
230         return ( lval && rval );
231     case OP_OR:
232         return ( lval || rval );
233     case OP_GT:
234         return ( lval > rval );
235     case OP_LT:
236         return ( lval < rval );
237     case OP_LE:
238         return ( lval <= rval );
239     case OP_GE:
240         return ( lval >= rval );
241     case OP_NE:
242         return ( lval != rval );
243     default:
244         ERR("Unknown operator %d\n", op );
245     }
246     return 0;
247 }
248
249 static INT INT_evaluate_unary( INT lval, UINT op )
250 {
251     switch( op )
252     {
253     case OP_ISNULL:
254         return ( !lval );
255     case OP_NOTNULL:
256         return ( lval );
257     default:
258         ERR("Unknown operator %d\n", op );
259     }
260     return 0;
261 }
262
263 static const WCHAR *STRING_evaluate( MSIWHEREVIEW *wv, UINT row,
264                                      const struct expr *expr,
265                                      const MSIRECORD *record )
266 {
267     UINT val = 0, r;
268
269     switch( expr->type )
270     {
271     case EXPR_COL_NUMBER_STRING:
272         r = wv->table->ops->fetch_int( wv->table, row, expr->u.col_number, &val );
273         if( r != ERROR_SUCCESS )
274             return NULL;
275         return msi_string_lookup_id( wv->db->strings, val );
276
277     case EXPR_SVAL:
278         return expr->u.sval;
279
280     case EXPR_WILDCARD:
281         return MSI_RecordGetString( record, ++wv->rec_index );
282
283     default:
284         ERR("Invalid expression type\n");
285         break;
286     }
287     return NULL;
288 }
289
290 static UINT STRCMP_Evaluate( MSIWHEREVIEW *wv, UINT row, const struct expr *cond,
291                              INT *val, const MSIRECORD *record )
292 {
293     int sr;
294     const WCHAR *l_str, *r_str;
295
296     l_str = STRING_evaluate( wv, row, cond->u.expr.left, record );
297     r_str = STRING_evaluate( wv, row, cond->u.expr.right, record );
298     if( l_str == r_str ||
299         ((!l_str || !*l_str) && (!r_str || !*r_str)) )
300         sr = 0;
301     else if( l_str && ! r_str )
302         sr = 1;
303     else if( r_str && ! l_str )
304         sr = -1;
305     else
306         sr = lstrcmpW( l_str, r_str );
307
308     *val = ( cond->u.expr.op == OP_EQ && ( sr == 0 ) ) ||
309            ( cond->u.expr.op == OP_NE && ( sr != 0 ) ) ||
310            ( cond->u.expr.op == OP_LT && ( sr < 0 ) ) ||
311            ( cond->u.expr.op == OP_GT && ( sr > 0 ) );
312
313     return ERROR_SUCCESS;
314 }
315
316 static UINT WHERE_evaluate( MSIWHEREVIEW *wv, UINT row,
317                             struct expr *cond, INT *val, MSIRECORD *record )
318 {
319     UINT r, tval;
320     INT lval, rval;
321
322     if( !cond )
323         return ERROR_SUCCESS;
324
325     switch( cond->type )
326     {
327     case EXPR_COL_NUMBER:
328         r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
329         *val = tval - 0x8000;
330         return ERROR_SUCCESS;
331
332     case EXPR_COL_NUMBER32:
333         r = wv->table->ops->fetch_int( wv->table, row, cond->u.col_number, &tval );
334         *val = tval - 0x80000000;
335         return r;
336
337     case EXPR_UVAL:
338         *val = cond->u.uval;
339         return ERROR_SUCCESS;
340
341     case EXPR_COMPLEX:
342         r = WHERE_evaluate( wv, row, cond->u.expr.left, &lval, record );
343         if( r != ERROR_SUCCESS )
344             return r;
345         r = WHERE_evaluate( wv, row, cond->u.expr.right, &rval, record );
346         if( r != ERROR_SUCCESS )
347             return r;
348         *val = INT_evaluate_binary( lval, cond->u.expr.op, rval );
349         return ERROR_SUCCESS;
350
351     case EXPR_UNARY:
352         r = wv->table->ops->fetch_int( wv->table, row, cond->u.expr.left->u.col_number, &tval );
353         if( r != ERROR_SUCCESS )
354             return r;
355         *val = INT_evaluate_unary( tval, cond->u.expr.op );
356         return ERROR_SUCCESS;
357
358     case EXPR_STRCMP:
359         return STRCMP_Evaluate( wv, row, cond, val, record );
360
361     case EXPR_WILDCARD:
362         *val = MSI_RecordGetInteger( record, ++wv->rec_index );
363         return ERROR_SUCCESS;
364
365     default:
366         ERR("Invalid expression type\n");
367         break;
368     }
369
370     return ERROR_SUCCESS;
371 }
372
373 static UINT WHERE_execute( struct tagMSIVIEW *view, MSIRECORD *record )
374 {
375     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
376     UINT count = 0, r, i;
377     INT val;
378     MSIVIEW *table = wv->table;
379
380     TRACE("%p %p\n", wv, record);
381
382     if( !table )
383          return ERROR_FUNCTION_FAILED;
384
385     r = table->ops->execute( table, record );
386     if( r != ERROR_SUCCESS )
387         return r;
388
389     r = table->ops->get_dimensions( table, &count, NULL );
390     if( r != ERROR_SUCCESS )
391         return r;
392
393     free_hash_table(wv->reorder);
394     wv->reorder = msi_alloc_zero(MSI_HASH_TABLE_SIZE * sizeof(MSIHASHENTRY *));
395     if( !wv->reorder )
396         return ERROR_OUTOFMEMORY;
397
398     wv->row_count = 0;
399     if (wv->cond->type == EXPR_STRCMP)
400     {
401         MSIITERHANDLE handle = NULL;
402         UINT row, value, col;
403         struct expr *col_cond = wv->cond->u.expr.left;
404         struct expr *val_cond = wv->cond->u.expr.right;
405
406         /* swap conditionals */
407         if (col_cond->type != EXPR_COL_NUMBER_STRING)
408         {
409             val_cond = wv->cond->u.expr.left;
410             col_cond = wv->cond->u.expr.right;
411         }
412
413         if ((col_cond->type == EXPR_COL_NUMBER_STRING) && (val_cond->type == EXPR_SVAL))
414         {
415             col = col_cond->u.col_number;
416             /* special case for "" - translate it into nil */
417             if (!val_cond->u.sval[0])
418                 value = 0;
419             else
420             {
421                 r = msi_string2idW(wv->db->strings, val_cond->u.sval, &value);
422                 if (r != ERROR_SUCCESS)
423                 {
424                     TRACE("no id for %s, assuming it doesn't exist in the table\n", debugstr_w(wv->cond->u.expr.right->u.sval));
425                     return ERROR_SUCCESS;
426                 }
427             }
428
429             do
430             {
431                 r = table->ops->find_matching_rows(table, col, value, &row, &handle);
432                 if (r == ERROR_SUCCESS)
433                     add_entry_to_hash(wv->reorder, wv->row_count++, row);
434             } while (r == ERROR_SUCCESS);
435
436             if (r == ERROR_NO_MORE_ITEMS)
437                 return ERROR_SUCCESS;
438             else
439                 return r;
440         }
441         /* else fallback to slow case */
442     }
443
444     for( i=0; i<count; i++ )
445     {
446         val = 0;
447         wv->rec_index = 0;
448         r = WHERE_evaluate( wv, i, wv->cond, &val, record );
449         if( r != ERROR_SUCCESS )
450             return r;
451         if( val )
452             add_entry_to_hash( wv->reorder, wv->row_count++, i );
453     }
454
455     return ERROR_SUCCESS;
456 }
457
458 static UINT WHERE_close( struct tagMSIVIEW *view )
459 {
460     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
461
462     TRACE("%p\n", wv );
463
464     if( !wv->table )
465         return ERROR_FUNCTION_FAILED;
466
467     return wv->table->ops->close( wv->table );
468 }
469
470 static UINT WHERE_get_dimensions( struct tagMSIVIEW *view, UINT *rows, UINT *cols )
471 {
472     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
473
474     TRACE("%p %p %p\n", wv, rows, cols );
475
476     if( !wv->table )
477          return ERROR_FUNCTION_FAILED;
478
479     if( rows )
480     {
481         if( !wv->reorder )
482             return ERROR_FUNCTION_FAILED;
483         *rows = wv->row_count;
484     }
485
486     return wv->table->ops->get_dimensions( wv->table, NULL, cols );
487 }
488
489 static UINT WHERE_get_column_info( struct tagMSIVIEW *view,
490                 UINT n, LPWSTR *name, UINT *type )
491 {
492     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
493
494     TRACE("%p %d %p %p\n", wv, n, name, type );
495
496     if( !wv->table )
497          return ERROR_FUNCTION_FAILED;
498
499     return wv->table->ops->get_column_info( wv->table, n, name, type );
500 }
501
502 static UINT WHERE_modify( struct tagMSIVIEW *view, MSIMODIFY eModifyMode,
503                           MSIRECORD *rec, UINT row )
504 {
505     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
506     UINT r;
507
508     TRACE("%p %d %p\n", wv, eModifyMode, rec);
509
510     r = WHERE_execute(view, NULL);
511     if (r != ERROR_SUCCESS)
512         return r;
513
514     find_entry_in_hash(wv->reorder, row - 1, &row);
515     return wv->table->ops->modify( wv->table, eModifyMode, rec, row );
516 }
517
518 static UINT WHERE_delete( struct tagMSIVIEW *view )
519 {
520     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
521
522     TRACE("%p\n", wv );
523
524     if( wv->table )
525         wv->table->ops->delete( wv->table );
526     wv->table = 0;
527
528     free_hash_table(wv->reorder);
529     wv->reorder = NULL;
530     wv->row_count = 0;
531
532     msiobj_release( &wv->db->hdr );
533     msi_free( wv );
534
535     return ERROR_SUCCESS;
536 }
537
538 static UINT WHERE_find_matching_rows( struct tagMSIVIEW *view, UINT col,
539     UINT val, UINT *row, MSIITERHANDLE *handle )
540 {
541     MSIWHEREVIEW *wv = (MSIWHEREVIEW*)view;
542     UINT r;
543
544     TRACE("%p, %d, %u, %p\n", view, col, val, *handle);
545
546     if( !wv->table )
547          return ERROR_FUNCTION_FAILED;
548
549     r = wv->table->ops->find_matching_rows( wv->table, col, val, row, handle );
550     if (r != ERROR_SUCCESS)
551         return r;
552
553     if( *row > wv->row_count )
554         return ERROR_NO_MORE_ITEMS;
555
556     return find_entry_in_hash(wv->reorder, *row, row);
557 }
558
559 static UINT WHERE_sort(struct tagMSIVIEW *view, column_info *columns)
560 {
561     MSIWHEREVIEW *wv = (MSIWHEREVIEW *)view;
562
563     TRACE("%p %p\n", view, columns);
564
565     return wv->table->ops->sort(wv->table, columns);
566 }
567
568 static const MSIVIEWOPS where_ops =
569 {
570     WHERE_fetch_int,
571     WHERE_fetch_stream,
572     WHERE_get_row,
573     WHERE_set_row,
574     NULL,
575     WHERE_delete_row,
576     WHERE_execute,
577     WHERE_close,
578     WHERE_get_dimensions,
579     WHERE_get_column_info,
580     WHERE_modify,
581     WHERE_delete,
582     WHERE_find_matching_rows,
583     NULL,
584     NULL,
585     NULL,
586     NULL,
587     WHERE_sort,
588 };
589
590 static UINT WHERE_VerifyCondition( MSIDATABASE *db, MSIVIEW *table, struct expr *cond,
591                                    UINT *valid )
592 {
593     UINT r, val = 0;
594
595     switch( cond->type )
596     {
597     case EXPR_COLUMN:
598         r = VIEW_find_column( table, cond->u.column, &val );
599         if( r == ERROR_SUCCESS )
600         {
601             UINT type = 0;
602             r = table->ops->get_column_info( table, val, NULL, &type );
603             if( r == ERROR_SUCCESS )
604             {
605                 if (type&MSITYPE_STRING)
606                     cond->type = EXPR_COL_NUMBER_STRING;
607                 else if ((type&0xff) == 4)
608                     cond->type = EXPR_COL_NUMBER32;
609                 else
610                     cond->type = EXPR_COL_NUMBER;
611                 cond->u.col_number = val;
612                 *valid = 1;
613             }
614             else
615                 *valid = 0;
616         }
617         else
618         {
619             *valid = 0;
620             WARN("Couldn't find column %s\n", debugstr_w( cond->u.column ) );
621         }
622         break;
623     case EXPR_COMPLEX:
624         r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
625         if( r != ERROR_SUCCESS )
626             return r;
627         if( !*valid )
628             return ERROR_SUCCESS;
629         r = WHERE_VerifyCondition( db, table, cond->u.expr.right, valid );
630         if( r != ERROR_SUCCESS )
631             return r;
632
633         /* check the type of the comparison */
634         if( ( cond->u.expr.left->type == EXPR_SVAL ) ||
635             ( cond->u.expr.left->type == EXPR_COL_NUMBER_STRING ) ||
636             ( cond->u.expr.right->type == EXPR_SVAL ) ||
637             ( cond->u.expr.right->type == EXPR_COL_NUMBER_STRING ) )
638         {
639             switch( cond->u.expr.op )
640             {
641             case OP_EQ:
642             case OP_GT:
643             case OP_LT:
644             case OP_NE:
645                 break;
646             default:
647                 *valid = FALSE;
648                 return ERROR_INVALID_PARAMETER;
649             }
650
651             /* FIXME: check we're comparing a string to a column */
652
653             cond->type = EXPR_STRCMP;
654         }
655
656         break;
657     case EXPR_UNARY:
658         if ( cond->u.expr.left->type != EXPR_COLUMN )
659         {
660             *valid = FALSE;
661             return ERROR_INVALID_PARAMETER;
662         }
663         r = WHERE_VerifyCondition( db, table, cond->u.expr.left, valid );
664         if( r != ERROR_SUCCESS )
665             return r;
666         break;
667     case EXPR_IVAL:
668         *valid = 1;
669         cond->type = EXPR_UVAL;
670         cond->u.uval = cond->u.ival;
671         break;
672     case EXPR_WILDCARD:
673         *valid = 1;
674         break;
675     case EXPR_SVAL:
676         *valid = 1;
677         break;
678     default:
679         ERR("Invalid expression type\n");
680         *valid = 0;
681         break;
682     }
683
684     return ERROR_SUCCESS;
685 }
686
687 UINT WHERE_CreateView( MSIDATABASE *db, MSIVIEW **view, MSIVIEW *table,
688                        struct expr *cond )
689 {
690     MSIWHEREVIEW *wv = NULL;
691     UINT count = 0, r, valid = 0;
692
693     TRACE("%p\n", table );
694
695     r = table->ops->get_dimensions( table, NULL, &count );
696     if( r != ERROR_SUCCESS )
697     {
698         ERR("can't get table dimensions\n");
699         return r;
700     }
701
702     if( cond )
703     {
704         r = WHERE_VerifyCondition( db, table, cond, &valid );
705         if( r != ERROR_SUCCESS )
706             return r;
707         if( !valid )
708             return ERROR_FUNCTION_FAILED;
709     }
710
711     wv = msi_alloc_zero( sizeof *wv );
712     if( !wv )
713         return ERROR_FUNCTION_FAILED;
714     
715     /* fill the structure */
716     wv->view.ops = &where_ops;
717     msiobj_addref( &db->hdr );
718     wv->db = db;
719     wv->table = table;
720     wv->row_count = 0;
721     wv->reorder = NULL;
722     wv->cond = cond;
723     wv->rec_index = 0;
724     *view = (MSIVIEW*) wv;
725
726     return ERROR_SUCCESS;
727 }