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