]>
Commit | Line | Data |
---|---|---|
b3378a02 | 1 | /* Cycript - Optimizing JavaScript Compiler/Runtime |
8d7447c1 | 2 | * Copyright (C) 2009-2012 Jay Freeman (saurik) |
b4aa79af JF |
3 | */ |
4 | ||
b3378a02 | 5 | /* GNU Lesser General Public License, Version 3 {{{ */ |
b4aa79af | 6 | /* |
b3378a02 JF |
7 | * Cycript is free software: you can redistribute it and/or modify it under |
8 | * the terms of the GNU Lesser General Public License as published by the | |
9 | * Free Software Foundation, either version 3 of the License, or (at your | |
10 | * option) any later version. | |
b4aa79af | 11 | * |
b3378a02 JF |
12 | * Cycript is distributed in the hope that it will be useful, but WITHOUT |
13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public | |
15 | * License for more details. | |
b4aa79af | 16 | * |
b3378a02 JF |
17 | * You should have received a copy of the GNU Lesser General Public License |
18 | * along with Cycript. If not, see <http://www.gnu.org/licenses/>. | |
19 | **/ | |
b4aa79af JF |
20 | /* }}} */ |
21 | ||
9185d5ef | 22 | #include <apr_strings.h> |
ea2d184c | 23 | #include "sig/parse.hpp" |
37954781 JF |
24 | #include "Error.hpp" |
25 | ||
26 | #include <cstdio> | |
27 | #include <cstdlib> | |
28 | #include <cstring> | |
ea2d184c JF |
29 | |
30 | namespace sig { | |
31 | ||
f33b048a JF |
32 | void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos, Callback callback); |
33 | struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named, Callback callback); | |
b21525c7 JF |
34 | |
35 | ||
ea2d184c JF |
36 | /* XXX: I really screwed up this time */ |
37 | void *prealloc_(apr_pool_t *pool, void *odata, size_t osize, size_t nsize) { | |
38 | void *ndata = apr_palloc(pool, nsize); | |
39 | memcpy(ndata, odata, osize); | |
40 | return ndata; | |
41 | } | |
42 | ||
f33b048a | 43 | void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos, Callback callback) { |
ea2d184c JF |
44 | _assert(*name != NULL); |
45 | ||
f33b048a JF |
46 | // XXX: this is just a stupid check :( |
47 | bool named(**name == '"'); | |
ea2d184c JF |
48 | |
49 | signature->elements = NULL; | |
50 | signature->count = 0; | |
51 | ||
52 | for (;;) { | |
53 | if (**name == eos) { | |
54 | ++*name; | |
55 | return; | |
56 | } | |
57 | ||
58 | signature->elements = (struct Element *) prealloc_(pool, signature->elements, signature->count * sizeof(struct Element), (signature->count + 1) * sizeof(struct Element)); | |
59 | _assert(signature->elements != NULL); | |
60 | ||
61 | struct Element *element = &signature->elements[signature->count++]; | |
62 | ||
63 | if (**name != '"') | |
64 | element->name = NULL; | |
65 | else { | |
a815a4d6 | 66 | const char *quote = strchr(++*name, '"'); |
ea2d184c JF |
67 | element->name = apr_pstrmemdup(pool, *name, quote - *name); |
68 | *name = quote + 1; | |
69 | } | |
70 | ||
f33b048a | 71 | element->type = Parse_(pool, name, eos, named, callback); |
ea2d184c JF |
72 | |
73 | if (**name < '0' || **name > '9') | |
74 | element->offset = _not(size_t); | |
75 | else { | |
76 | element->offset = 0; | |
77 | ||
78 | do | |
79 | element->offset = element->offset * 10 + (*(*name)++ - '0'); | |
80 | while (**name >= '0' && **name <= '9'); | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
f33b048a | 85 | struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named, Callback callback) { |
ea2d184c JF |
86 | char next = *(*name)++; |
87 | if (next == '?') | |
88 | return NULL; | |
89 | ||
90 | struct Type *type = (struct Type *) apr_palloc(pool, sizeof(struct Type)); | |
91 | _assert(type != NULL); | |
92 | memset(type, 0, sizeof(struct Type)); | |
93 | ||
94 | parse: | |
95 | switch (next) { | |
96 | case '#': type->primitive = typename_P; break; | |
97 | ||
98 | case '(': | |
d63c39cc JF |
99 | if (type->data.signature.count < 2) |
100 | type->primitive = struct_P; | |
101 | else | |
102 | type->primitive = union_P; | |
ea2d184c JF |
103 | next = ')'; |
104 | goto aggregate; | |
105 | ||
106 | case '*': type->primitive = string_P; break; | |
107 | case ':': type->primitive = selector_P; break; | |
108 | ||
ecf94af8 JF |
109 | case '@': { |
110 | char next(**name); | |
111 | ||
112 | if (next == '?') { | |
113 | type->primitive = block_P; | |
114 | ++*name; | |
115 | } else { | |
116 | type->primitive = object_P; | |
117 | ||
118 | if (next == '"') { | |
119 | const char *quote = strchr(*name + 1, '"'); | |
506aad76 JF |
120 | if (quote == NULL) { |
121 | printf("unterminated specific id type {%s}\n", *name - 10); | |
122 | _assert(false); | |
123 | } else if (!named || quote[1] == eos || quote[1] == '"') { | |
ecf94af8 JF |
124 | type->name = apr_pstrmemdup(pool, *name + 1, quote - *name - 1); |
125 | *name = quote + 1; | |
126 | } | |
ea2d184c JF |
127 | } |
128 | } | |
129 | ||
ecf94af8 | 130 | } break; |
ea2d184c JF |
131 | |
132 | case 'B': type->primitive = boolean_P; break; | |
133 | case 'C': type->primitive = uchar_P; break; | |
134 | case 'I': type->primitive = uint_P; break; | |
135 | case 'L': type->primitive = ulong_P; break; | |
136 | case 'Q': type->primitive = ulonglong_P; break; | |
137 | case 'S': type->primitive = ushort_P; break; | |
138 | ||
139 | case '[': | |
140 | type->primitive = array_P; | |
141 | type->data.data.size = strtoul(*name, (char **) name, 10); | |
f33b048a | 142 | type->data.data.type = Parse_(pool, name, eos, false, callback); |
ea2d184c JF |
143 | if (**name != ']') { |
144 | printf("']' != \"%s\"\n", *name); | |
145 | _assert(false); | |
146 | } | |
147 | ++*name; | |
148 | break; | |
149 | ||
150 | case '^': | |
151 | type->primitive = pointer_P; | |
61933e16 | 152 | if (**name == '"') { |
ea2d184c JF |
153 | type->data.data.type = NULL; |
154 | } else { | |
f33b048a | 155 | type->data.data.type = Parse_(pool, name, eos, named, callback); |
3a1b79a7 JF |
156 | sig::Type *&target(type->data.data.type); |
157 | if (target != NULL && target->primitive == void_P) | |
158 | target = NULL; | |
ea2d184c JF |
159 | } |
160 | break; | |
161 | ||
162 | case 'b': | |
163 | type->primitive = bit_P; | |
164 | type->data.data.size = strtoul(*name, (char **) name, 10); | |
165 | break; | |
166 | ||
167 | case 'c': type->primitive = char_P; break; | |
168 | case 'd': type->primitive = double_P; break; | |
169 | case 'f': type->primitive = float_P; break; | |
170 | case 'i': type->primitive = int_P; break; | |
171 | case 'l': type->primitive = long_P; break; | |
172 | case 'q': type->primitive = longlong_P; break; | |
173 | case 's': type->primitive = short_P; break; | |
174 | case 'v': type->primitive = void_P; break; | |
175 | ||
176 | case '{': | |
177 | type->primitive = struct_P; | |
178 | next = '}'; | |
179 | goto aggregate; | |
180 | ||
181 | aggregate: { | |
182 | char end = next; | |
183 | const char *begin = *name; | |
184 | do next = *(*name)++; | |
185 | while ( | |
186 | next != '=' && | |
187 | next != '}' | |
188 | ); | |
189 | size_t length = *name - begin - 1; | |
190 | if (strncmp(begin, "?", length) != 0) | |
191 | type->name = (char *) apr_pstrmemdup(pool, begin, length); | |
192 | else | |
193 | type->name = NULL; | |
194 | ||
f33b048a JF |
195 | // XXX: this types thing is a throwback to JocStrap |
196 | ||
9814ec39 | 197 | if (next == '=') |
f33b048a | 198 | Parse_(pool, &type->data.signature, name, end, callback); |
ea2d184c JF |
199 | } break; |
200 | ||
201 | case 'N': type->flags |= JOC_TYPE_INOUT; goto next; | |
202 | case 'n': type->flags |= JOC_TYPE_IN; goto next; | |
203 | case 'O': type->flags |= JOC_TYPE_BYCOPY; goto next; | |
204 | case 'o': type->flags |= JOC_TYPE_OUT; goto next; | |
205 | case 'R': type->flags |= JOC_TYPE_BYREF; goto next; | |
206 | case 'r': type->flags |= JOC_TYPE_CONST; goto next; | |
207 | case 'V': type->flags |= JOC_TYPE_ONEWAY; goto next; | |
208 | ||
209 | next: | |
210 | next = *(*name)++; | |
211 | goto parse; | |
212 | break; | |
213 | ||
214 | default: | |
215 | printf("invalid type character: '%c' {%s}\n", next, *name - 10); | |
216 | _assert(false); | |
217 | } | |
218 | ||
9814ec39 JF |
219 | if (callback != NULL) |
220 | (*callback)(pool, type); | |
221 | ||
ea2d184c JF |
222 | return type; |
223 | } | |
224 | ||
f33b048a | 225 | void Parse(apr_pool_t *pool, struct Signature *signature, const char *name, Callback callback) { |
ea2d184c | 226 | const char *temp = name; |
f33b048a | 227 | Parse_(pool, signature, &temp, '\0', callback); |
ea2d184c JF |
228 | _assert(temp[-1] == '\0'); |
229 | } | |
230 | ||
b21525c7 | 231 | const char *Unparse(apr_pool_t *pool, struct Signature *signature) { |
ea2d184c JF |
232 | const char *value = ""; |
233 | size_t offset; | |
234 | ||
235 | for (offset = 0; offset != signature->count; ++offset) { | |
b21525c7 | 236 | const char *type = Unparse(pool, signature->elements[offset].type); |
ea2d184c JF |
237 | value = apr_pstrcat(pool, value, type, NULL); |
238 | } | |
239 | ||
240 | return value; | |
241 | } | |
242 | ||
51714815 JF |
243 | const char *Unparse_(apr_pool_t *pool, struct Type *type) { |
244 | switch (type->primitive) { | |
ea2d184c | 245 | case typename_P: return "#"; |
b21525c7 | 246 | case union_P: return apr_psprintf(pool, "(%s)", Unparse(pool, &type->data.signature)); |
ea2d184c JF |
247 | case string_P: return "*"; |
248 | case selector_P: return ":"; | |
ecf94af8 | 249 | case block_P: return "@?"; |
ea2d184c JF |
250 | case object_P: return type->name == NULL ? "@" : apr_psprintf(pool, "@\"%s\"", type->name); |
251 | case boolean_P: return "B"; | |
252 | case uchar_P: return "C"; | |
253 | case uint_P: return "I"; | |
254 | case ulong_P: return "L"; | |
255 | case ulonglong_P: return "Q"; | |
256 | case ushort_P: return "S"; | |
257 | ||
258 | case array_P: { | |
b21525c7 | 259 | const char *value = Unparse(pool, type->data.data.type); |
0fe90592 | 260 | return apr_psprintf(pool, "[%"APR_SIZE_T_FMT"%s]", type->data.data.size, value); |
ea2d184c JF |
261 | } break; |
262 | ||
cbaa5f0f | 263 | case pointer_P: return apr_psprintf(pool, "^%s", type->data.data.type == NULL ? "v" : Unparse(pool, type->data.data.type)); |
0fe90592 | 264 | case bit_P: return apr_psprintf(pool, "b%"APR_SIZE_T_FMT"", type->data.data.size); |
ea2d184c JF |
265 | case char_P: return "c"; |
266 | case double_P: return "d"; | |
267 | case float_P: return "f"; | |
268 | case int_P: return "i"; | |
269 | case long_P: return "l"; | |
270 | case longlong_P: return "q"; | |
271 | case short_P: return "s"; | |
272 | case void_P: return "v"; | |
b21525c7 | 273 | case struct_P: return apr_psprintf(pool, "{%s=%s}", type->name == NULL ? "?" : type->name, Unparse(pool, &type->data.signature)); |
ea2d184c JF |
274 | } |
275 | ||
276 | _assert(false); | |
277 | return NULL; | |
278 | } | |
279 | ||
51714815 JF |
280 | const char *Unparse(apr_pool_t *pool, struct Type *type) { |
281 | if (type == NULL) | |
282 | return "?"; | |
283 | ||
284 | const char *base(Unparse_(pool, type)); | |
285 | if (type->flags == 0) | |
286 | return base; | |
287 | ||
288 | #define iovec_(base, size) \ | |
289 | (struct iovec) {const_cast<char *>(base), size} | |
290 | ||
291 | struct iovec parts[8]; | |
292 | memset(parts, 0, sizeof(parts)); | |
293 | ||
294 | if ((type->flags & JOC_TYPE_INOUT) != 0) | |
295 | parts[0] = iovec_("N", 1); | |
296 | if ((type->flags & JOC_TYPE_IN) != 0) | |
297 | parts[1] = iovec_("n", 1); | |
298 | if ((type->flags & JOC_TYPE_BYCOPY) != 0) | |
299 | parts[2] = iovec_("O", 1); | |
300 | if ((type->flags & JOC_TYPE_OUT) != 0) | |
301 | parts[3] = iovec_("o", 1); | |
302 | if ((type->flags & JOC_TYPE_BYREF) != 0) | |
303 | parts[4] = iovec_("R", 1); | |
304 | if ((type->flags & JOC_TYPE_CONST) != 0) | |
305 | parts[5] = iovec_("r", 1); | |
306 | if ((type->flags & JOC_TYPE_ONEWAY) != 0) | |
307 | parts[6] = iovec_("V", 1); | |
308 | ||
309 | parts[7] = iovec_(base, strlen(base)); | |
310 | return apr_pstrcatv(pool, parts, 8, NULL); | |
311 | } | |
312 | ||
ea2d184c | 313 | } |