]> git.saurik.com Git - cycript.git/blob - sig/parse.cpp
5dcfd4faa194097c03aeebf8c6507c58ab77a26f
[cycript.git] / sig / parse.cpp
1 /* Cycript - Remove Execution Server and Disassembler
2 * Copyright (C) 2009 Jay Freeman (saurik)
3 */
4
5 /* Modified BSD License {{{ */
6 /*
7 * Redistribution and use in source and binary
8 * forms, with or without modification, are permitted
9 * provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the
12 * above copyright notice, this list of conditions
13 * and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the
15 * above copyright notice, this list of conditions
16 * and the following disclaimer in the documentation
17 * and/or other materials provided with the
18 * distribution.
19 * 3. The name of the author may not be used to endorse
20 * or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS''
24 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
25 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
34 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
36 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 */
38 /* }}} */
39
40 #ifndef _GNU_SOURCE
41 #define _GNU_SOURCE
42 #endif
43
44 #include "minimal/stdlib.h"
45
46 #include <apr-1/apr_strings.h>
47
48 #include <string.h>
49
50 #include "sig/parse.hpp"
51
52 namespace sig {
53
54 void (*sig_aggregate)(apr_pool_t *pool, enum Primitive primitive, const char *name, struct Signature *signature, const char *types) = NULL;
55
56 void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos);
57 struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named);
58
59
60 /* XXX: I really screwed up this time */
61 void *prealloc_(apr_pool_t *pool, void *odata, size_t osize, size_t nsize) {
62 void *ndata = apr_palloc(pool, nsize);
63 memcpy(ndata, odata, osize);
64 return ndata;
65 }
66
67 void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos) {
68 _assert(*name != NULL);
69
70 bool named = **name == '"';
71
72 signature->elements = NULL;
73 signature->count = 0;
74
75 for (;;) {
76 if (**name == eos) {
77 ++*name;
78 return;
79 }
80
81 signature->elements = (struct Element *) prealloc_(pool, signature->elements, signature->count * sizeof(struct Element), (signature->count + 1) * sizeof(struct Element));
82 _assert(signature->elements != NULL);
83
84 struct Element *element = &signature->elements[signature->count++];
85
86 if (**name != '"')
87 element->name = NULL;
88 else {
89 char *quote = strchr(++*name, '"');
90 element->name = apr_pstrmemdup(pool, *name, quote - *name);
91 *name = quote + 1;
92 }
93
94 element->type = Parse_(pool, name, eos, named);
95
96 if (**name < '0' || **name > '9')
97 element->offset = _not(size_t);
98 else {
99 element->offset = 0;
100
101 do
102 element->offset = element->offset * 10 + (*(*name)++ - '0');
103 while (**name >= '0' && **name <= '9');
104 }
105 }
106 }
107
108 struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named) {
109 char next = *(*name)++;
110 if (next == '?')
111 return NULL;
112
113 struct Type *type = (struct Type *) apr_palloc(pool, sizeof(struct Type));
114 _assert(type != NULL);
115 memset(type, 0, sizeof(struct Type));
116
117 parse:
118 switch (next) {
119 case '#': type->primitive = typename_P; break;
120
121 case '(':
122 type->primitive = union_P;
123 next = ')';
124 goto aggregate;
125
126 case '*': type->primitive = string_P; break;
127 case ':': type->primitive = selector_P; break;
128
129 case '@':
130 if (**name == '"') {
131 char *quote = strchr(*name + 1, '"');
132 if (!named || quote[1] == eos || quote[1] == '"') {
133 type->name = apr_pstrmemdup(pool, *name + 1, quote - *name - 1);
134 *name = quote + 1;
135 }
136 }
137
138 type->primitive = object_P;
139 break;
140
141 case 'B': type->primitive = boolean_P; break;
142 case 'C': type->primitive = uchar_P; break;
143 case 'I': type->primitive = uint_P; break;
144 case 'L': type->primitive = ulong_P; break;
145 case 'Q': type->primitive = ulonglong_P; break;
146 case 'S': type->primitive = ushort_P; break;
147
148 case '[':
149 type->primitive = array_P;
150 type->data.data.size = strtoul(*name, (char **) name, 10);
151 type->data.data.type = Parse_(pool, name, eos, false);
152 if (**name != ']') {
153 printf("']' != \"%s\"\n", *name);
154 _assert(false);
155 }
156 ++*name;
157 break;
158
159 case '^':
160 type->primitive = pointer_P;
161 if (**name == 'v') {
162 type->data.data.type = NULL;
163 ++*name;
164 } else if (**name == '"') {
165 type->data.data.type = NULL;
166 } else {
167 type->data.data.type = Parse_(pool, name, eos, named);
168 }
169 break;
170
171 case 'b':
172 type->primitive = bit_P;
173 type->data.data.size = strtoul(*name, (char **) name, 10);
174 break;
175
176 case 'c': type->primitive = char_P; break;
177 case 'd': type->primitive = double_P; break;
178 case 'f': type->primitive = float_P; break;
179 case 'i': type->primitive = int_P; break;
180 case 'l': type->primitive = long_P; break;
181 case 'q': type->primitive = longlong_P; break;
182 case 's': type->primitive = short_P; break;
183 case 'v': type->primitive = void_P; break;
184
185 case '{':
186 type->primitive = struct_P;
187 next = '}';
188 goto aggregate;
189
190 aggregate: {
191 char end = next;
192 const char *begin = *name;
193 do next = *(*name)++;
194 while (
195 next != '=' &&
196 next != '}'
197 );
198 size_t length = *name - begin - 1;
199 if (strncmp(begin, "?", length) != 0)
200 type->name = (char *) apr_pstrmemdup(pool, begin, length);
201 else
202 type->name = NULL;
203
204 char *types;
205 if (next != '=')
206 types = NULL;
207 else {
208 const char *temp = *name;
209 Parse_(pool, &type->data.signature, name, end);
210 types = (char *) apr_pstrmemdup(pool, temp, *name - temp - 1);
211 }
212
213 if (type->name != NULL && sig_aggregate != NULL) {
214 char *angle = strchr(type->name, '<');
215 if (angle == NULL)
216 (*sig_aggregate)(pool, type->primitive, type->name, &type->data.signature, types);
217 else {
218 angle = (char *) apr_pstrmemdup(pool, type->name, angle - type->name);
219 (*sig_aggregate)(pool, type->primitive, angle, &type->data.signature, types);
220 }
221 }
222 } break;
223
224 case 'N': type->flags |= JOC_TYPE_INOUT; goto next;
225 case 'n': type->flags |= JOC_TYPE_IN; goto next;
226 case 'O': type->flags |= JOC_TYPE_BYCOPY; goto next;
227 case 'o': type->flags |= JOC_TYPE_OUT; goto next;
228 case 'R': type->flags |= JOC_TYPE_BYREF; goto next;
229 case 'r': type->flags |= JOC_TYPE_CONST; goto next;
230 case 'V': type->flags |= JOC_TYPE_ONEWAY; goto next;
231
232 next:
233 next = *(*name)++;
234 goto parse;
235 break;
236
237 default:
238 printf("invalid type character: '%c' {%s}\n", next, *name - 10);
239 _assert(false);
240 }
241
242 return type;
243 }
244
245 void Parse(apr_pool_t *pool, struct Signature *signature, const char *name) {
246 const char *temp = name;
247 Parse_(pool, signature, &temp, '\0');
248 _assert(temp[-1] == '\0');
249 }
250
251 const char *Unparse(apr_pool_t *pool, struct Signature *signature) {
252 const char *value = "";
253 size_t offset;
254
255 for (offset = 0; offset != signature->count; ++offset) {
256 const char *type = Unparse(pool, signature->elements[offset].type);
257 value = apr_pstrcat(pool, value, type, NULL);
258 }
259
260 return value;
261 }
262
263 const char *Unparse(apr_pool_t *pool, struct Type *type) {
264 if (type == NULL)
265 return "?";
266 else switch (type->primitive) {
267 case typename_P: return "#";
268 case union_P: return apr_psprintf(pool, "(%s)", Unparse(pool, &type->data.signature));
269 case string_P: return "*";
270 case selector_P: return ":";
271 case object_P: return type->name == NULL ? "@" : apr_psprintf(pool, "@\"%s\"", type->name);
272 case boolean_P: return "B";
273 case uchar_P: return "C";
274 case uint_P: return "I";
275 case ulong_P: return "L";
276 case ulonglong_P: return "Q";
277 case ushort_P: return "S";
278
279 case array_P: {
280 const char *value = Unparse(pool, type->data.data.type);
281 return apr_psprintf(pool, "[%lu%s]", type->data.data.size, value);
282 } break;
283
284 case pointer_P: return apr_psprintf(pool, "^%s", type->data.data.type == NULL ? "" : Unparse(pool, type->data.data.type));
285 case bit_P: return apr_psprintf(pool, "b%zu", type->data.data.size);
286 case char_P: return "c";
287 case double_P: return "d";
288 case float_P: return "f";
289 case int_P: return "i";
290 case long_P: return "l";
291 case longlong_P: return "q";
292 case short_P: return "s";
293 case void_P: return "v";
294 case struct_P: return apr_psprintf(pool, "{%s=%s}", type->name == NULL ? "?" : type->name, Unparse(pool, &type->data.signature));
295 }
296
297 _assert(false);
298 return NULL;
299 }
300
301 }