]> git.saurik.com Git - cycript.git/blob - sig/parse.cpp
Ignore Struct.hpp.
[cycript.git] / sig / parse.cpp
1 #ifndef _GNU_SOURCE
2 #define _GNU_SOURCE
3 #endif
4
5 #include "minimal/stdlib.h"
6
7 #include <apr-1/apr_strings.h>
8
9 #include <string.h>
10
11 #include "sig/parse.hpp"
12
13 namespace sig {
14
15 void (*sig_aggregate)(apr_pool_t *pool, enum Primitive primitive, const char *name, struct Signature *signature, const char *types) = NULL;
16
17 void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos);
18 struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named);
19
20
21 /* XXX: I really screwed up this time */
22 void *prealloc_(apr_pool_t *pool, void *odata, size_t osize, size_t nsize) {
23 void *ndata = apr_palloc(pool, nsize);
24 memcpy(ndata, odata, osize);
25 return ndata;
26 }
27
28 void Parse_(apr_pool_t *pool, struct Signature *signature, const char **name, char eos) {
29 _assert(*name != NULL);
30
31 bool named = **name == '"';
32
33 signature->elements = NULL;
34 signature->count = 0;
35
36 for (;;) {
37 if (**name == eos) {
38 ++*name;
39 return;
40 }
41
42 signature->elements = (struct Element *) prealloc_(pool, signature->elements, signature->count * sizeof(struct Element), (signature->count + 1) * sizeof(struct Element));
43 _assert(signature->elements != NULL);
44
45 struct Element *element = &signature->elements[signature->count++];
46
47 if (**name != '"')
48 element->name = NULL;
49 else {
50 char *quote = strchr(++*name, '"');
51 element->name = apr_pstrmemdup(pool, *name, quote - *name);
52 *name = quote + 1;
53 }
54
55 element->type = Parse_(pool, name, eos, named);
56
57 if (**name < '0' || **name > '9')
58 element->offset = _not(size_t);
59 else {
60 element->offset = 0;
61
62 do
63 element->offset = element->offset * 10 + (*(*name)++ - '0');
64 while (**name >= '0' && **name <= '9');
65 }
66 }
67 }
68
69 struct Type *Parse_(apr_pool_t *pool, const char **name, char eos, bool named) {
70 char next = *(*name)++;
71 if (next == '?')
72 return NULL;
73
74 struct Type *type = (struct Type *) apr_palloc(pool, sizeof(struct Type));
75 _assert(type != NULL);
76 memset(type, 0, sizeof(struct Type));
77
78 parse:
79 switch (next) {
80 case '#': type->primitive = typename_P; break;
81
82 case '(':
83 type->primitive = union_P;
84 next = ')';
85 goto aggregate;
86
87 case '*': type->primitive = string_P; break;
88 case ':': type->primitive = selector_P; break;
89
90 case '@':
91 if (**name == '"') {
92 char *quote = strchr(*name + 1, '"');
93 if (!named || quote[1] == eos || quote[1] == '"') {
94 type->name = apr_pstrmemdup(pool, *name + 1, quote - *name - 1);
95 *name = quote + 1;
96 }
97 }
98
99 type->primitive = object_P;
100 break;
101
102 case 'B': type->primitive = boolean_P; break;
103 case 'C': type->primitive = uchar_P; break;
104 case 'I': type->primitive = uint_P; break;
105 case 'L': type->primitive = ulong_P; break;
106 case 'Q': type->primitive = ulonglong_P; break;
107 case 'S': type->primitive = ushort_P; break;
108
109 case '[':
110 type->primitive = array_P;
111 type->data.data.size = strtoul(*name, (char **) name, 10);
112 type->data.data.type = Parse_(pool, name, eos, false);
113 if (**name != ']') {
114 printf("']' != \"%s\"\n", *name);
115 _assert(false);
116 }
117 ++*name;
118 break;
119
120 case '^':
121 type->primitive = pointer_P;
122 if (**name == 'v') {
123 type->data.data.type = NULL;
124 ++*name;
125 } else if (**name == '"') {
126 type->data.data.type = NULL;
127 } else {
128 type->data.data.type = Parse_(pool, name, eos, named);
129 }
130 break;
131
132 case 'b':
133 type->primitive = bit_P;
134 type->data.data.size = strtoul(*name, (char **) name, 10);
135 break;
136
137 case 'c': type->primitive = char_P; break;
138 case 'd': type->primitive = double_P; break;
139 case 'f': type->primitive = float_P; break;
140 case 'i': type->primitive = int_P; break;
141 case 'l': type->primitive = long_P; break;
142 case 'q': type->primitive = longlong_P; break;
143 case 's': type->primitive = short_P; break;
144 case 'v': type->primitive = void_P; break;
145
146 case '{':
147 type->primitive = struct_P;
148 next = '}';
149 goto aggregate;
150
151 aggregate: {
152 char end = next;
153 const char *begin = *name;
154 do next = *(*name)++;
155 while (
156 next != '=' &&
157 next != '}'
158 );
159 size_t length = *name - begin - 1;
160 if (strncmp(begin, "?", length) != 0)
161 type->name = (char *) apr_pstrmemdup(pool, begin, length);
162 else
163 type->name = NULL;
164
165 char *types;
166 if (next != '=')
167 types = NULL;
168 else {
169 const char *temp = *name;
170 Parse_(pool, &type->data.signature, name, end);
171 types = (char *) apr_pstrmemdup(pool, temp, *name - temp - 1);
172 }
173
174 if (type->name != NULL && sig_aggregate != NULL) {
175 char *angle = strchr(type->name, '<');
176 if (angle == NULL)
177 (*sig_aggregate)(pool, type->primitive, type->name, &type->data.signature, types);
178 else {
179 angle = (char *) apr_pstrmemdup(pool, type->name, angle - type->name);
180 (*sig_aggregate)(pool, type->primitive, angle, &type->data.signature, types);
181 }
182 }
183 } break;
184
185 case 'N': type->flags |= JOC_TYPE_INOUT; goto next;
186 case 'n': type->flags |= JOC_TYPE_IN; goto next;
187 case 'O': type->flags |= JOC_TYPE_BYCOPY; goto next;
188 case 'o': type->flags |= JOC_TYPE_OUT; goto next;
189 case 'R': type->flags |= JOC_TYPE_BYREF; goto next;
190 case 'r': type->flags |= JOC_TYPE_CONST; goto next;
191 case 'V': type->flags |= JOC_TYPE_ONEWAY; goto next;
192
193 next:
194 next = *(*name)++;
195 goto parse;
196 break;
197
198 default:
199 printf("invalid type character: '%c' {%s}\n", next, *name - 10);
200 _assert(false);
201 }
202
203 return type;
204 }
205
206 void Parse(apr_pool_t *pool, struct Signature *signature, const char *name) {
207 const char *temp = name;
208 Parse_(pool, signature, &temp, '\0');
209 _assert(temp[-1] == '\0');
210 }
211
212 const char *Unparse(apr_pool_t *pool, struct Signature *signature) {
213 const char *value = "";
214 size_t offset;
215
216 for (offset = 0; offset != signature->count; ++offset) {
217 const char *type = Unparse(pool, signature->elements[offset].type);
218 value = apr_pstrcat(pool, value, type, NULL);
219 }
220
221 return value;
222 }
223
224 const char *Unparse(apr_pool_t *pool, struct Type *type) {
225 if (type == NULL)
226 return "?";
227 else switch (type->primitive) {
228 case typename_P: return "#";
229 case union_P: return apr_psprintf(pool, "(%s)", Unparse(pool, &type->data.signature));
230 case string_P: return "*";
231 case selector_P: return ":";
232 case object_P: return type->name == NULL ? "@" : apr_psprintf(pool, "@\"%s\"", type->name);
233 case boolean_P: return "B";
234 case uchar_P: return "C";
235 case uint_P: return "I";
236 case ulong_P: return "L";
237 case ulonglong_P: return "Q";
238 case ushort_P: return "S";
239
240 case array_P: {
241 const char *value = Unparse(pool, type->data.data.type);
242 return apr_psprintf(pool, "[%lu%s]", type->data.data.size, value);
243 } break;
244
245 case pointer_P: return apr_psprintf(pool, "^%s", type->data.data.type == NULL ? "" : Unparse(pool, type->data.data.type));
246 case bit_P: return apr_psprintf(pool, "b%zu", type->data.data.size);
247 case char_P: return "c";
248 case double_P: return "d";
249 case float_P: return "f";
250 case int_P: return "i";
251 case long_P: return "l";
252 case longlong_P: return "q";
253 case short_P: return "s";
254 case void_P: return "v";
255 case struct_P: return apr_psprintf(pool, "{%s=%s}", type->name == NULL ? "?" : type->name, Unparse(pool, &type->data.signature));
256 }
257
258 _assert(false);
259 return NULL;
260 }
261
262 }