]> git.saurik.com Git - cycript.git/blame - sig/parse.cpp
Got true JS booleans working in GNUstep.
[cycript.git] / sig / parse.cpp
CommitLineData
b4aa79af
JF
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
9185d5ef 40#include <apr_strings.h>
ea2d184c 41#include "sig/parse.hpp"
37954781
JF
42#include "Error.hpp"
43
44#include <cstdio>
45#include <cstdlib>
46#include <cstring>
ea2d184c
JF
47
48namespace sig {
49
f33b048a
JF
50void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos, Callback callback);
51struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named, Callback callback);
b21525c7
JF
52
53
ea2d184c
JF
54/* XXX: I really screwed up this time */
55void *prealloc_(apr_pool_t *pool, void *odata, size_t osize, size_t nsize) {
56 void *ndata = apr_palloc(pool, nsize);
57 memcpy(ndata, odata, osize);
58 return ndata;
59}
60
f33b048a 61void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos, Callback callback) {
ea2d184c
JF
62 _assert(*name != NULL);
63
f33b048a
JF
64 // XXX: this is just a stupid check :(
65 bool named(**name == '"');
ea2d184c
JF
66
67 signature->elements = NULL;
68 signature->count = 0;
69
70 for (;;) {
71 if (**name == eos) {
72 ++*name;
73 return;
74 }
75
76 signature->elements = (struct Element *) prealloc_(pool, signature->elements, signature->count * sizeof(struct Element), (signature->count + 1) * sizeof(struct Element));
77 _assert(signature->elements != NULL);
78
79 struct Element *element = &signature->elements[signature->count++];
80
81 if (**name != '"')
82 element->name = NULL;
83 else {
84 char *quote = strchr(++*name, '"');
85 element->name = apr_pstrmemdup(pool, *name, quote - *name);
86 *name = quote + 1;
87 }
88
f33b048a 89 element->type = Parse_(pool, name, eos, named, callback);
ea2d184c
JF
90
91 if (**name < '0' || **name > '9')
92 element->offset = _not(size_t);
93 else {
94 element->offset = 0;
95
96 do
97 element->offset = element->offset * 10 + (*(*name)++ - '0');
98 while (**name >= '0' && **name <= '9');
99 }
100 }
101}
102
f33b048a 103struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named, Callback callback) {
ea2d184c
JF
104 char next = *(*name)++;
105 if (next == '?')
106 return NULL;
107
108 struct Type *type = (struct Type *) apr_palloc(pool, sizeof(struct Type));
109 _assert(type != NULL);
110 memset(type, 0, sizeof(struct Type));
111
112 parse:
113 switch (next) {
114 case '#': type->primitive = typename_P; break;
115
116 case '(':
d63c39cc
JF
117 if (type->data.signature.count < 2)
118 type->primitive = struct_P;
119 else
120 type->primitive = union_P;
ea2d184c
JF
121 next = ')';
122 goto aggregate;
123
124 case '*': type->primitive = string_P; break;
125 case ':': type->primitive = selector_P; break;
126
127 case '@':
128 if (**name == '"') {
129 char *quote = strchr(*name + 1, '"');
130 if (!named || quote[1] == eos || quote[1] == '"') {
131 type->name = apr_pstrmemdup(pool, *name + 1, quote - *name - 1);
132 *name = quote + 1;
133 }
134 }
135
136 type->primitive = object_P;
137 break;
138
139 case 'B': type->primitive = boolean_P; break;
140 case 'C': type->primitive = uchar_P; break;
141 case 'I': type->primitive = uint_P; break;
142 case 'L': type->primitive = ulong_P; break;
143 case 'Q': type->primitive = ulonglong_P; break;
144 case 'S': type->primitive = ushort_P; break;
145
146 case '[':
147 type->primitive = array_P;
148 type->data.data.size = strtoul(*name, (char **) name, 10);
f33b048a 149 type->data.data.type = Parse_(pool, name, eos, false, callback);
ea2d184c
JF
150 if (**name != ']') {
151 printf("']' != \"%s\"\n", *name);
152 _assert(false);
153 }
154 ++*name;
155 break;
156
157 case '^':
158 type->primitive = pointer_P;
61933e16 159 if (**name == '"') {
ea2d184c
JF
160 type->data.data.type = NULL;
161 } else {
f33b048a 162 type->data.data.type = Parse_(pool, name, eos, named, callback);
3a1b79a7
JF
163 sig::Type *&target(type->data.data.type);
164 if (target != NULL && target->primitive == void_P)
165 target = NULL;
ea2d184c
JF
166 }
167 break;
168
169 case 'b':
170 type->primitive = bit_P;
171 type->data.data.size = strtoul(*name, (char **) name, 10);
172 break;
173
174 case 'c': type->primitive = char_P; break;
175 case 'd': type->primitive = double_P; break;
176 case 'f': type->primitive = float_P; break;
177 case 'i': type->primitive = int_P; break;
178 case 'l': type->primitive = long_P; break;
179 case 'q': type->primitive = longlong_P; break;
180 case 's': type->primitive = short_P; break;
181 case 'v': type->primitive = void_P; break;
182
183 case '{':
184 type->primitive = struct_P;
185 next = '}';
186 goto aggregate;
187
188 aggregate: {
189 char end = next;
190 const char *begin = *name;
191 do next = *(*name)++;
192 while (
193 next != '=' &&
194 next != '}'
195 );
196 size_t length = *name - begin - 1;
197 if (strncmp(begin, "?", length) != 0)
198 type->name = (char *) apr_pstrmemdup(pool, begin, length);
199 else
200 type->name = NULL;
201
f33b048a
JF
202 // XXX: this types thing is a throwback to JocStrap
203
ea2d184c 204 char *types;
f33b048a 205 if (next != '=') {
ea2d184c 206 types = NULL;
f33b048a
JF
207 } else {
208 const char *temp(*name);
209 Parse_(pool, &type->data.signature, name, end, callback);
ea2d184c
JF
210 types = (char *) apr_pstrmemdup(pool, temp, *name - temp - 1);
211 }
212
f33b048a
JF
213 if (callback != NULL)
214 (*callback)(pool, type->name, types, type);
ea2d184c
JF
215 } break;
216
217 case 'N': type->flags |= JOC_TYPE_INOUT; goto next;
218 case 'n': type->flags |= JOC_TYPE_IN; goto next;
219 case 'O': type->flags |= JOC_TYPE_BYCOPY; goto next;
220 case 'o': type->flags |= JOC_TYPE_OUT; goto next;
221 case 'R': type->flags |= JOC_TYPE_BYREF; goto next;
222 case 'r': type->flags |= JOC_TYPE_CONST; goto next;
223 case 'V': type->flags |= JOC_TYPE_ONEWAY; goto next;
224
225 next:
226 next = *(*name)++;
227 goto parse;
228 break;
229
230 default:
231 printf("invalid type character: '%c' {%s}\n", next, *name - 10);
232 _assert(false);
233 }
234
235 return type;
236}
237
f33b048a 238void Parse(apr_pool_t *pool, struct Signature *signature, const char *name, Callback callback) {
ea2d184c 239 const char *temp = name;
f33b048a 240 Parse_(pool, signature, &temp, '\0', callback);
ea2d184c
JF
241 _assert(temp[-1] == '\0');
242}
243
b21525c7 244const char *Unparse(apr_pool_t *pool, struct Signature *signature) {
ea2d184c
JF
245 const char *value = "";
246 size_t offset;
247
248 for (offset = 0; offset != signature->count; ++offset) {
b21525c7 249 const char *type = Unparse(pool, signature->elements[offset].type);
ea2d184c
JF
250 value = apr_pstrcat(pool, value, type, NULL);
251 }
252
253 return value;
254}
255
b21525c7 256const char *Unparse(apr_pool_t *pool, struct Type *type) {
ea2d184c
JF
257 if (type == NULL)
258 return "?";
259 else switch (type->primitive) {
260 case typename_P: return "#";
b21525c7 261 case union_P: return apr_psprintf(pool, "(%s)", Unparse(pool, &type->data.signature));
ea2d184c
JF
262 case string_P: return "*";
263 case selector_P: return ":";
264 case object_P: return type->name == NULL ? "@" : apr_psprintf(pool, "@\"%s\"", type->name);
265 case boolean_P: return "B";
266 case uchar_P: return "C";
267 case uint_P: return "I";
268 case ulong_P: return "L";
269 case ulonglong_P: return "Q";
270 case ushort_P: return "S";
271
272 case array_P: {
b21525c7 273 const char *value = Unparse(pool, type->data.data.type);
272a0dd4 274 return apr_psprintf(pool, "[%zu%s]", type->data.data.size, value);
ea2d184c
JF
275 } break;
276
cbaa5f0f 277 case pointer_P: return apr_psprintf(pool, "^%s", type->data.data.type == NULL ? "v" : Unparse(pool, type->data.data.type));
ea2d184c
JF
278 case bit_P: return apr_psprintf(pool, "b%zu", type->data.data.size);
279 case char_P: return "c";
280 case double_P: return "d";
281 case float_P: return "f";
282 case int_P: return "i";
283 case long_P: return "l";
284 case longlong_P: return "q";
285 case short_P: return "s";
286 case void_P: return "v";
b21525c7 287 case struct_P: return apr_psprintf(pool, "{%s=%s}", type->name == NULL ? "?" : type->name, Unparse(pool, &type->data.signature));
ea2d184c
JF
288 }
289
290 _assert(false);
291 return NULL;
292}
293
294}