FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
TestGdiRop3.c
1
2#include <winpr/crt.h>
3#include <winpr/winpr.h>
4#include <winpr/collections.h>
5
97static char* gdi_convert_postfix_to_infix(const char* postfix)
98{
99 BOOL unary = 0;
100 size_t al = 0;
101 size_t bl = 0;
102 wStack* stack = Stack_New(FALSE);
103 size_t length = strlen(postfix);
104
105 for (size_t i = 0; i < length; i++)
106 {
107 BOOL success = FALSE;
108 if ((postfix[i] == 'P') || (postfix[i] == 'D') || (postfix[i] == 'S'))
109 {
110 /* token is an operand, push on the stack */
111 char* a = malloc(2);
112 if (!a)
113 goto end;
114 a[0] = postfix[i];
115 a[1] = '\0';
116 // printf("Operand: %s\n", a);
117 Stack_Push(stack, a);
118 }
119 else
120 {
121 char* a = NULL;
122 char* b = NULL;
123
124 /* token is an operator */
125 unary = FALSE;
126 char* c = malloc(2);
127 if (!c)
128 goto fail;
129 c[0] = postfix[i];
130 c[1] = '\0';
131
132 if (c[0] == 'a')
133 {
134 c[0] = '&';
135 }
136 else if (c[0] == 'o')
137 {
138 c[0] = '|';
139 }
140 else if (c[0] == 'n')
141 {
142 c[0] = '~';
143 unary = TRUE;
144 }
145 else if (c[0] == 'x')
146 {
147 c[0] = '^';
148 }
149 else
150 {
151 printf("invalid operator: %c\n", c[0]);
152 }
153
154 // printf("Operator: %s\n", c);
155 a = (char*)Stack_Pop(stack);
156 if (!a)
157 goto fail;
158
159 if (!unary)
160 b = (char*)Stack_Pop(stack);
161
162 al = strlen(a);
163
164 if (b)
165 bl = strlen(b);
166
167 size_t cl = 1;
168 size_t dl = al + bl + cl + 3;
169 char* d = malloc(dl + 1);
170 if (!d)
171 goto fail;
172
173 (void)sprintf_s(d, dl, "(%s%s%s)", b ? b : "", c, a);
174 Stack_Push(stack, d);
175
176 success = TRUE;
177 fail:
178 free(a);
179 free(b);
180 free(c);
181 if (!success)
182 goto end;
183 }
184 }
185
186 char* d = (char*)Stack_Pop(stack);
187 Stack_Free(stack);
188 return d;
189
190end:
191 Stack_Free(stack);
192 return NULL;
193}
194
195static const char* test_ROP3[] = { "DSPDxax", "PSDPxax", "SPna", "DSna", "DPa",
196 "PDxn", "DSxn", "PSDnox", "PDSona", "DSPDxox",
197 "DPSDonox", "SPDSxax", "DPon", "DPna", "Pn",
198 "PDna", "DPan", "DSan", "DSxn", "DPa",
199 "D", "DPno", "SDno", "PDno", "DPo" };
200
201int TestGdiRop3(int argc, char* argv[])
202{
203 WINPR_UNUSED(argc);
204 WINPR_UNUSED(argv);
205
206 for (size_t index = 0; index < sizeof(test_ROP3) / sizeof(test_ROP3[0]); index++)
207 {
208 const char* postfix = test_ROP3[index];
209 char* infix = gdi_convert_postfix_to_infix(postfix);
210
211 if (!infix)
212 return -1;
213
214 printf("%s\t\t%s\n", postfix, infix);
215 free(infix);
216 }
217
218 return 0;
219}