[MSI]
[reactos.git] / reactos / dll / win32 / msi / cond.y
1 %{
2
3 /*
4 * Implementation of the Microsoft Installer (msi.dll)
5 *
6 * Copyright 2003 Mike McCormack for CodeWeavers
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #define COBJMACROS
24
25 #include "config.h"
26
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "msi.h"
35 #include "msiquery.h"
36 #include "objbase.h"
37 #include "oleauto.h"
38
39 #include "msipriv.h"
40 #include "msiserver.h"
41 #include "wine/debug.h"
42 #include "wine/unicode.h"
43 #include "wine/list.h"
44
45 WINE_DEFAULT_DEBUG_CHANNEL(msi);
46
47 typedef struct tag_yyinput
48 {
49 MSIPACKAGE *package;
50 LPCWSTR str;
51 INT n;
52 MSICONDITION result;
53 struct list mem;
54 } COND_input;
55
56 struct cond_str {
57 LPCWSTR data;
58 INT len;
59 };
60
61 static LPWSTR COND_GetString( COND_input *info, const struct cond_str *str );
62 static LPWSTR COND_GetLiteral( COND_input *info, const struct cond_str *str );
63 static int cond_lex( void *COND_lval, COND_input *info);
64 static int cond_error( COND_input *info, const char *str);
65
66 static void *cond_alloc( COND_input *cond, unsigned int sz );
67 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz );
68 static void cond_free( void *ptr );
69
70 static INT compare_int( INT a, INT operator, INT b );
71 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert );
72
73 static INT compare_and_free_strings( LPWSTR a, INT op, LPWSTR b, BOOL convert )
74 {
75 INT r;
76
77 r = compare_string( a, op, b, convert );
78 cond_free( a );
79 cond_free( b );
80 return r;
81 }
82
83 static BOOL num_from_prop( LPCWSTR p, INT *val )
84 {
85 INT ret = 0, sign = 1;
86
87 if (!p)
88 return FALSE;
89 if (*p == '-')
90 {
91 sign = -1;
92 p++;
93 }
94 if (!*p)
95 return FALSE;
96 while (*p)
97 {
98 if( *p < '0' || *p > '9' )
99 return FALSE;
100 ret = ret*10 + (*p - '0');
101 p++;
102 }
103 *val = ret*sign;
104 return TRUE;
105 }
106
107 %}
108
109 %lex-param { COND_input *info }
110 %parse-param { COND_input *info }
111 %pure-parser
112
113 %union
114 {
115 struct cond_str str;
116 LPWSTR string;
117 INT value;
118 }
119
120 %token COND_SPACE COND_EOF
121 %token COND_OR COND_AND COND_NOT COND_XOR COND_IMP COND_EQV
122 %token COND_LT COND_GT COND_EQ COND_NE COND_GE COND_LE
123 %token COND_ILT COND_IGT COND_IEQ COND_INE COND_IGE COND_ILE
124 %token COND_LPAR COND_RPAR COND_TILDA COND_SS COND_ISS
125 %token COND_ILHS COND_IRHS COND_LHS COND_RHS
126 %token COND_PERCENT COND_DOLLARS COND_QUESTION COND_AMPER COND_EXCLAM
127 %token <str> COND_IDENT <str> COND_NUMBER <str> COND_LITER
128
129 %nonassoc COND_ERROR COND_EOF
130
131 %type <value> expression boolean_term boolean_factor
132 %type <value> value_i integer operator
133 %type <string> identifier symbol_s value_s literal
134
135 %%
136
137 condition:
138 expression
139 {
140 COND_input* cond = (COND_input*) info;
141 cond->result = $1;
142 }
143 | /* empty */
144 {
145 COND_input* cond = (COND_input*) info;
146 cond->result = MSICONDITION_NONE;
147 }
148 ;
149
150 expression:
151 boolean_term
152 {
153 $$ = $1;
154 }
155 | expression COND_OR boolean_term
156 {
157 $$ = $1 || $3;
158 }
159 | expression COND_IMP boolean_term
160 {
161 $$ = !$1 || $3;
162 }
163 | expression COND_XOR boolean_term
164 {
165 $$ = ( $1 || $3 ) && !( $1 && $3 );
166 }
167 | expression COND_EQV boolean_term
168 {
169 $$ = ( $1 && $3 ) || ( !$1 && !$3 );
170 }
171 ;
172
173 boolean_term:
174 boolean_factor
175 {
176 $$ = $1;
177 }
178 | boolean_term COND_AND boolean_factor
179 {
180 $$ = $1 && $3;
181 }
182 ;
183
184 boolean_factor:
185 COND_NOT boolean_factor
186 {
187 $$ = $2 ? 0 : 1;
188 }
189 | value_i
190 {
191 $$ = $1 ? 1 : 0;
192 }
193 | value_s
194 {
195 $$ = ($1 && $1[0]) ? 1 : 0;
196 cond_free( $1 );
197 }
198 | value_i operator value_i
199 {
200 $$ = compare_int( $1, $2, $3 );
201 }
202 | symbol_s operator value_i
203 {
204 int num;
205 if (num_from_prop( $1, &num ))
206 $$ = compare_int( num, $2, $3 );
207 else
208 $$ = ($2 == COND_NE || $2 == COND_INE );
209 cond_free( $1 );
210 }
211 | value_i operator symbol_s
212 {
213 int num;
214 if (num_from_prop( $3, &num ))
215 $$ = compare_int( $1, $2, num );
216 else
217 $$ = ($2 == COND_NE || $2 == COND_INE );
218 cond_free( $3 );
219 }
220 | symbol_s operator symbol_s
221 {
222 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
223 }
224 | symbol_s operator literal
225 {
226 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
227 }
228 | literal operator symbol_s
229 {
230 $$ = compare_and_free_strings( $1, $2, $3, TRUE );
231 }
232 | literal operator literal
233 {
234 $$ = compare_and_free_strings( $1, $2, $3, FALSE );
235 }
236 | literal operator value_i
237 {
238 $$ = 0;
239 cond_free( $1 );
240 }
241 | value_i operator literal
242 {
243 $$ = 0;
244 cond_free( $3 );
245 }
246 | COND_LPAR expression COND_RPAR
247 {
248 $$ = $2;
249 }
250 ;
251
252 operator:
253 /* common functions */
254 COND_EQ { $$ = COND_EQ; }
255 | COND_NE { $$ = COND_NE; }
256 | COND_LT { $$ = COND_LT; }
257 | COND_GT { $$ = COND_GT; }
258 | COND_LE { $$ = COND_LE; }
259 | COND_GE { $$ = COND_GE; }
260 | COND_SS { $$ = COND_SS; }
261 | COND_IEQ { $$ = COND_IEQ; }
262 | COND_INE { $$ = COND_INE; }
263 | COND_ILT { $$ = COND_ILT; }
264 | COND_IGT { $$ = COND_IGT; }
265 | COND_ILE { $$ = COND_ILE; }
266 | COND_IGE { $$ = COND_IGE; }
267 | COND_ISS { $$ = COND_ISS; }
268 | COND_LHS { $$ = COND_LHS; }
269 | COND_RHS { $$ = COND_RHS; }
270 | COND_ILHS { $$ = COND_ILHS; }
271 | COND_IRHS { $$ = COND_IRHS; }
272 ;
273
274 value_s:
275 symbol_s
276 {
277 $$ = $1;
278 }
279 | literal
280 {
281 $$ = $1;
282 }
283 ;
284
285 literal:
286 COND_LITER
287 {
288 COND_input* cond = (COND_input*) info;
289 $$ = COND_GetLiteral( cond, &$1 );
290 if( !$$ )
291 YYABORT;
292 }
293 ;
294
295 value_i:
296 integer
297 {
298 $$ = $1;
299 }
300 | COND_DOLLARS identifier
301 {
302 COND_input* cond = (COND_input*) info;
303 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
304
305 MSI_GetComponentStateW(cond->package, $2, &install, &action );
306 $$ = action;
307 cond_free( $2 );
308 }
309 | COND_QUESTION identifier
310 {
311 COND_input* cond = (COND_input*) info;
312 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
313
314 MSI_GetComponentStateW(cond->package, $2, &install, &action );
315 $$ = install;
316 cond_free( $2 );
317 }
318 | COND_AMPER identifier
319 {
320 COND_input* cond = (COND_input*) info;
321 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
322
323 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
324 if (action == INSTALLSTATE_UNKNOWN)
325 $$ = MSICONDITION_FALSE;
326 else
327 $$ = action;
328
329 cond_free( $2 );
330 }
331 | COND_EXCLAM identifier
332 {
333 COND_input* cond = (COND_input*) info;
334 INSTALLSTATE install = INSTALLSTATE_UNKNOWN, action = INSTALLSTATE_UNKNOWN;
335
336 MSI_GetFeatureStateW(cond->package, $2, &install, &action );
337 $$ = install;
338 cond_free( $2 );
339 }
340 ;
341
342 symbol_s:
343 identifier
344 {
345 COND_input* cond = (COND_input*) info;
346 UINT len;
347
348 $$ = msi_dup_property( cond->package->db, $1 );
349 if ($$)
350 {
351 len = (lstrlenW($$) + 1) * sizeof (WCHAR);
352 $$ = cond_track_mem( cond, $$, len );
353 }
354 cond_free( $1 );
355 }
356 | COND_PERCENT identifier
357 {
358 COND_input* cond = (COND_input*) info;
359 UINT len = GetEnvironmentVariableW( $2, NULL, 0 );
360 $$ = NULL;
361 if (len++)
362 {
363 $$ = cond_alloc( cond, len*sizeof (WCHAR) );
364 if( !$$ )
365 YYABORT;
366 GetEnvironmentVariableW( $2, $$, len );
367 }
368 cond_free( $2 );
369 }
370 ;
371
372 identifier:
373 COND_IDENT
374 {
375 COND_input* cond = (COND_input*) info;
376 $$ = COND_GetString( cond, &$1 );
377 if( !$$ )
378 YYABORT;
379 }
380 ;
381
382 integer:
383 COND_NUMBER
384 {
385 COND_input* cond = (COND_input*) info;
386 LPWSTR szNum = COND_GetString( cond, &$1 );
387 if( !szNum )
388 YYABORT;
389 $$ = atoiW( szNum );
390 cond_free( szNum );
391 }
392 ;
393
394 %%
395
396
397 static int COND_IsAlpha( WCHAR x )
398 {
399 return( ( ( x >= 'A' ) && ( x <= 'Z' ) ) ||
400 ( ( x >= 'a' ) && ( x <= 'z' ) ) ||
401 ( ( x == '_' ) ) );
402 }
403
404 static int COND_IsNumber( WCHAR x )
405 {
406 return( (( x >= '0' ) && ( x <= '9' )) || (x =='-') || (x =='.') );
407 }
408
409 static WCHAR *strstriW( const WCHAR *str, const WCHAR *sub )
410 {
411 LPWSTR strlower, sublower, r;
412 strlower = CharLowerW( strdupW( str ) );
413 sublower = CharLowerW( strdupW( sub ) );
414 r = strstrW( strlower, sublower );
415 if (r)
416 r = (LPWSTR)str + (r - strlower);
417 msi_free( strlower );
418 msi_free( sublower );
419 return r;
420 }
421
422 static BOOL str_is_number( LPCWSTR str )
423 {
424 int i;
425
426 if (!*str)
427 return FALSE;
428
429 for (i = 0; i < lstrlenW( str ); i++)
430 if (!isdigitW(str[i]))
431 return FALSE;
432
433 return TRUE;
434 }
435
436 static INT compare_substring( LPCWSTR a, INT operator, LPCWSTR b )
437 {
438 int lhs, rhs;
439
440 /* substring operators return 0 if LHS is missing */
441 if (!a || !*a)
442 return 0;
443
444 /* substring operators return 1 if RHS is missing */
445 if (!b || !*b)
446 return 1;
447
448 /* if both strings contain only numbers, use integer comparison */
449 lhs = atoiW(a);
450 rhs = atoiW(b);
451 if (str_is_number(a) && str_is_number(b))
452 return compare_int( lhs, operator, rhs );
453
454 switch (operator)
455 {
456 case COND_SS:
457 return strstrW( a, b ) != 0;
458 case COND_ISS:
459 return strstriW( a, b ) != 0;
460 case COND_LHS:
461 {
462 int l = strlenW( a );
463 int r = strlenW( b );
464 if (r > l) return 0;
465 return !strncmpW( a, b, r );
466 }
467 case COND_RHS:
468 {
469 int l = strlenW( a );
470 int r = strlenW( b );
471 if (r > l) return 0;
472 return !strncmpW( a + (l - r), b, r );
473 }
474 case COND_ILHS:
475 {
476 int l = strlenW( a );
477 int r = strlenW( b );
478 if (r > l) return 0;
479 return !strncmpiW( a, b, r );
480 }
481 case COND_IRHS:
482 {
483 int l = strlenW( a );
484 int r = strlenW( b );
485 if (r > l) return 0;
486 return !strncmpiW( a + (l - r), b, r );
487 }
488 default:
489 ERR("invalid substring operator\n");
490 return 0;
491 }
492 return 0;
493 }
494
495 static INT compare_string( LPCWSTR a, INT operator, LPCWSTR b, BOOL convert )
496 {
497 if (operator >= COND_SS && operator <= COND_RHS)
498 return compare_substring( a, operator, b );
499
500 /* null and empty string are equivalent */
501 if (!a) a = szEmpty;
502 if (!b) b = szEmpty;
503
504 if (convert && str_is_number(a) && str_is_number(b))
505 return compare_int( atoiW(a), operator, atoiW(b) );
506
507 /* a or b may be NULL */
508 switch (operator)
509 {
510 case COND_LT:
511 return strcmpW( a, b ) < 0;
512 case COND_GT:
513 return strcmpW( a, b ) > 0;
514 case COND_EQ:
515 return strcmpW( a, b ) == 0;
516 case COND_NE:
517 return strcmpW( a, b ) != 0;
518 case COND_GE:
519 return strcmpW( a, b ) >= 0;
520 case COND_LE:
521 return strcmpW( a, b ) <= 0;
522 case COND_ILT:
523 return strcmpiW( a, b ) < 0;
524 case COND_IGT:
525 return strcmpiW( a, b ) > 0;
526 case COND_IEQ:
527 return strcmpiW( a, b ) == 0;
528 case COND_INE:
529 return strcmpiW( a, b ) != 0;
530 case COND_IGE:
531 return strcmpiW( a, b ) >= 0;
532 case COND_ILE:
533 return strcmpiW( a, b ) <= 0;
534 default:
535 ERR("invalid string operator\n");
536 return 0;
537 }
538 return 0;
539 }
540
541
542 static INT compare_int( INT a, INT operator, INT b )
543 {
544 switch (operator)
545 {
546 case COND_LT:
547 case COND_ILT:
548 return a < b;
549 case COND_GT:
550 case COND_IGT:
551 return a > b;
552 case COND_EQ:
553 case COND_IEQ:
554 return a == b;
555 case COND_NE:
556 case COND_INE:
557 return a != b;
558 case COND_GE:
559 case COND_IGE:
560 return a >= b;
561 case COND_LE:
562 case COND_ILE:
563 return a <= b;
564 case COND_SS:
565 case COND_ISS:
566 return ( a & b ) ? 1 : 0;
567 case COND_RHS:
568 return ( ( a & 0xffff ) == b ) ? 1 : 0;
569 case COND_LHS:
570 return ( ( (a>>16) & 0xffff ) == b ) ? 1 : 0;
571 default:
572 ERR("invalid integer operator\n");
573 return 0;
574 }
575 return 0;
576 }
577
578
579 static int COND_IsIdent( WCHAR x )
580 {
581 return( COND_IsAlpha( x ) || COND_IsNumber( x ) || ( x == '_' )
582 || ( x == '#' ) || (x == '.') );
583 }
584
585 static int COND_GetOperator( COND_input *cond )
586 {
587 static const struct {
588 const WCHAR str[4];
589 int id;
590 } table[] = {
591 { {'~','<','=',0}, COND_ILE },
592 { {'~','>','<',0}, COND_ISS },
593 { {'~','>','>',0}, COND_IRHS },
594 { {'~','<','>',0}, COND_INE },
595 { {'~','>','=',0}, COND_IGE },
596 { {'~','<','<',0}, COND_ILHS },
597 { {'~','=',0}, COND_IEQ },
598 { {'~','<',0}, COND_ILT },
599 { {'~','>',0}, COND_IGT },
600 { {'>','=',0}, COND_GE },
601 { {'>','<',0}, COND_SS },
602 { {'<','<',0}, COND_LHS },
603 { {'<','>',0}, COND_NE },
604 { {'<','=',0}, COND_LE },
605 { {'>','>',0}, COND_RHS },
606 { {'>',0}, COND_GT },
607 { {'<',0}, COND_LT },
608 { {0}, 0 }
609 };
610 LPCWSTR p = &cond->str[cond->n];
611 int i = 0, len;
612
613 while ( 1 )
614 {
615 len = lstrlenW( table[i].str );
616 if ( !len || 0 == strncmpW( table[i].str, p, len ) )
617 break;
618 i++;
619 }
620 cond->n += len;
621 return table[i].id;
622 }
623
624 static int COND_GetOne( struct cond_str *str, COND_input *cond )
625 {
626 int rc, len = 1;
627 WCHAR ch;
628
629 str->data = &cond->str[cond->n];
630
631 ch = str->data[0];
632
633 switch( ch )
634 {
635 case 0: return 0;
636 case '(': rc = COND_LPAR; break;
637 case ')': rc = COND_RPAR; break;
638 case '&': rc = COND_AMPER; break;
639 case '!': rc = COND_EXCLAM; break;
640 case '$': rc = COND_DOLLARS; break;
641 case '?': rc = COND_QUESTION; break;
642 case '%': rc = COND_PERCENT; break;
643 case ' ': rc = COND_SPACE; break;
644 case '=': rc = COND_EQ; break;
645
646 case '~':
647 case '<':
648 case '>':
649 rc = COND_GetOperator( cond );
650 if (!rc)
651 rc = COND_ERROR;
652 return rc;
653 default:
654 rc = 0;
655 }
656
657 if ( rc )
658 {
659 cond->n += len;
660 return rc;
661 }
662
663 if (ch == '"' )
664 {
665 LPCWSTR p = strchrW( str->data + 1, '"' );
666 if (!p) return COND_ERROR;
667 len = p - str->data + 1;
668 rc = COND_LITER;
669 }
670 else if( COND_IsAlpha( ch ) )
671 {
672 static const WCHAR szNot[] = {'N','O','T',0};
673 static const WCHAR szAnd[] = {'A','N','D',0};
674 static const WCHAR szXor[] = {'X','O','R',0};
675 static const WCHAR szEqv[] = {'E','Q','V',0};
676 static const WCHAR szImp[] = {'I','M','P',0};
677 static const WCHAR szOr[] = {'O','R',0};
678
679 while( COND_IsIdent( str->data[len] ) )
680 len++;
681 rc = COND_IDENT;
682
683 if ( len == 3 )
684 {
685 if ( !strncmpiW( str->data, szNot, len ) )
686 rc = COND_NOT;
687 else if( !strncmpiW( str->data, szAnd, len ) )
688 rc = COND_AND;
689 else if( !strncmpiW( str->data, szXor, len ) )
690 rc = COND_XOR;
691 else if( !strncmpiW( str->data, szEqv, len ) )
692 rc = COND_EQV;
693 else if( !strncmpiW( str->data, szImp, len ) )
694 rc = COND_IMP;
695 }
696 else if( (len == 2) && !strncmpiW( str->data, szOr, len ) )
697 rc = COND_OR;
698 }
699 else if( COND_IsNumber( ch ) )
700 {
701 while( COND_IsNumber( str->data[len] ) )
702 len++;
703 rc = COND_NUMBER;
704 }
705 else
706 {
707 ERR("Got unknown character %c(%x)\n",ch,ch);
708 return COND_ERROR;
709 }
710
711 cond->n += len;
712 str->len = len;
713
714 return rc;
715 }
716
717 static int cond_lex( void *COND_lval, COND_input *cond )
718 {
719 int rc;
720 struct cond_str *str = COND_lval;
721
722 do {
723 rc = COND_GetOne( str, cond );
724 } while (rc == COND_SPACE);
725
726 return rc;
727 }
728
729 static LPWSTR COND_GetString( COND_input *cond, const struct cond_str *str )
730 {
731 LPWSTR ret;
732
733 ret = cond_alloc( cond, (str->len+1) * sizeof (WCHAR) );
734 if( ret )
735 {
736 memcpy( ret, str->data, str->len * sizeof(WCHAR));
737 ret[str->len]=0;
738 }
739 TRACE("Got identifier %s\n",debugstr_w(ret));
740 return ret;
741 }
742
743 static LPWSTR COND_GetLiteral( COND_input *cond, const struct cond_str *str )
744 {
745 LPWSTR ret;
746
747 ret = cond_alloc( cond, (str->len-1) * sizeof (WCHAR) );
748 if( ret )
749 {
750 memcpy( ret, str->data+1, (str->len-2) * sizeof(WCHAR) );
751 ret[str->len - 2]=0;
752 }
753 TRACE("Got literal %s\n",debugstr_w(ret));
754 return ret;
755 }
756
757 static void *cond_alloc( COND_input *cond, unsigned int sz )
758 {
759 struct list *mem;
760
761 mem = msi_alloc( sizeof (struct list) + sz );
762 if( !mem )
763 return NULL;
764
765 list_add_head( &(cond->mem), mem );
766 return mem + 1;
767 }
768
769 static void *cond_track_mem( COND_input *cond, void *ptr, unsigned int sz )
770 {
771 void *new_ptr;
772
773 if( !ptr )
774 return ptr;
775
776 new_ptr = cond_alloc( cond, sz );
777 if( !new_ptr )
778 {
779 msi_free( ptr );
780 return NULL;
781 }
782
783 memcpy( new_ptr, ptr, sz );
784 msi_free( ptr );
785 return new_ptr;
786 }
787
788 static void cond_free( void *ptr )
789 {
790 struct list *mem = (struct list *)ptr - 1;
791
792 if( ptr )
793 {
794 list_remove( mem );
795 msi_free( mem );
796 }
797 }
798
799 static int cond_error( COND_input *info, const char *str )
800 {
801 TRACE("%s\n", str );
802 return 0;
803 }
804
805 MSICONDITION MSI_EvaluateConditionW( MSIPACKAGE *package, LPCWSTR szCondition )
806 {
807 COND_input cond;
808 MSICONDITION r;
809 struct list *mem, *safety;
810
811 TRACE("%s\n", debugstr_w( szCondition ) );
812
813 if (szCondition == NULL) return MSICONDITION_NONE;
814
815 cond.package = package;
816 cond.str = szCondition;
817 cond.n = 0;
818 cond.result = MSICONDITION_ERROR;
819
820 list_init( &cond.mem );
821
822 if ( !cond_parse( &cond ) )
823 r = cond.result;
824 else
825 r = MSICONDITION_ERROR;
826
827 LIST_FOR_EACH_SAFE( mem, safety, &cond.mem )
828 {
829 /* The tracked memory lives directly after the list struct */
830 void *ptr = mem + 1;
831 if ( r != MSICONDITION_ERROR )
832 WARN( "condition parser failed to free up some memory: %p\n", ptr );
833 cond_free( ptr );
834 }
835
836 TRACE("%i <- %s\n", r, debugstr_w(szCondition));
837 return r;
838 }
839
840 MSICONDITION WINAPI MsiEvaluateConditionW( MSIHANDLE hInstall, LPCWSTR szCondition )
841 {
842 MSIPACKAGE *package;
843 UINT ret;
844
845 package = msihandle2msiinfo( hInstall, MSIHANDLETYPE_PACKAGE);
846 if( !package )
847 {
848 HRESULT hr;
849 BSTR condition;
850 IWineMsiRemotePackage *remote_package;
851
852 remote_package = (IWineMsiRemotePackage *)msi_get_remote( hInstall );
853 if (!remote_package)
854 return MSICONDITION_ERROR;
855
856 condition = SysAllocString( szCondition );
857 if (!condition)
858 {
859 IWineMsiRemotePackage_Release( remote_package );
860 return ERROR_OUTOFMEMORY;
861 }
862
863 hr = IWineMsiRemotePackage_EvaluateCondition( remote_package, condition );
864
865 SysFreeString( condition );
866 IWineMsiRemotePackage_Release( remote_package );
867
868 if (FAILED(hr))
869 {
870 if (HRESULT_FACILITY(hr) == FACILITY_WIN32)
871 return HRESULT_CODE(hr);
872
873 return ERROR_FUNCTION_FAILED;
874 }
875
876 return ERROR_SUCCESS;
877 }
878
879 ret = MSI_EvaluateConditionW( package, szCondition );
880 msiobj_release( &package->hdr );
881 return ret;
882 }
883
884 MSICONDITION WINAPI MsiEvaluateConditionA( MSIHANDLE hInstall, LPCSTR szCondition )
885 {
886 LPWSTR szwCond = NULL;
887 MSICONDITION r;
888
889 szwCond = strdupAtoW( szCondition );
890 if( szCondition && !szwCond )
891 return MSICONDITION_ERROR;
892
893 r = MsiEvaluateConditionW( hInstall, szwCond );
894 msi_free( szwCond );
895 return r;
896 }