]> git.saurik.com Git - cycript.git/blame - Library.cpp
Fix a size initialization bug.
[cycript.git] / Library.cpp
CommitLineData
d15b59f5 1/* Cycript - Inlining/Optimizing JavaScript Compiler
37954781
JF
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
37954781
JF
40#include <dlfcn.h>
41#include <iconv.h>
42
43#include "cycript.hpp"
44
37954781
JF
45#include "Pooling.hpp"
46
47#include <sys/mman.h>
48
49#include <iostream>
50#include <ext/stdio_filebuf.h>
51#include <set>
52#include <map>
53#include <iomanip>
54#include <sstream>
55#include <cmath>
56
57#include "Parser.hpp"
58#include "Cycript.tab.hh"
59
60#include "Error.hpp"
37954781 61#include "String.hpp"
2f51d6ab 62#include "Execute.hpp"
37954781 63
59cd74c1
JF
64/* C Strings {{{ */
65template <typename Type_>
66_finline size_t iconv_(size_t (*iconv)(iconv_t, Type_, size_t *, char **, size_t *), iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) {
67 return iconv(cd, const_cast<Type_>(inbuf), inbytesleft, outbuf, outbytesleft);
68}
69
70#ifdef __GLIBC__
71#define UCS_2_INTERNAL "UCS-2"
72#else
73#define UCS_2_INTERNAL "UCS-2-INTERNAL"
74#endif
75
76CYUTF8String CYPoolUTF8String(apr_pool_t *pool, CYUTF16String utf16) {
77 _assert(pool != NULL);
78
79 const char *in(reinterpret_cast<const char *>(utf16.data));
80
81 iconv_t conversion(_syscall(iconv_open("UTF-8", UCS_2_INTERNAL)));
82
83 // XXX: this is wrong
84 size_t size(utf16.size * 5);
85 char *out(new(pool) char[size]);
86 CYUTF8String utf8(out, size);
87
88 size = utf16.size * 2;
89 _syscall(iconv_(&iconv, conversion, const_cast<char **>(&in), &size, &out, &utf8.size));
90
91 *out = '\0';
92 utf8.size = out - utf8.data;
93
94 _syscall(iconv_close(conversion));
95
96 return utf8;
97}
98
99CYUTF16String CYPoolUTF16String(apr_pool_t *pool, CYUTF8String utf8) {
100 _assert(pool != NULL);
101
102 const char *in(utf8.data);
103
104 iconv_t conversion(_syscall(iconv_open(UCS_2_INTERNAL, "UTF-8")));
105
106 // XXX: this is wrong
107 size_t size(utf8.size * 5);
108 uint16_t *temp(new (pool) uint16_t[size]);
109 CYUTF16String utf16(temp, size * 2);
110 char *out(reinterpret_cast<char *>(temp));
111
112 size = utf8.size;
113 _syscall(iconv_(&iconv, conversion, const_cast<char **>(&in), &size, &out, &utf16.size));
114
115 utf16.size = reinterpret_cast<uint16_t *>(out) - utf16.data;
116 temp[utf16.size] = 0;
117
118 _syscall(iconv_close(conversion));
119
120 return utf16;
121}
122/* }}} */
37954781
JF
123/* Index Offsets {{{ */
124size_t CYGetIndex(const CYUTF8String &value) {
125 if (value.data[0] != '0') {
ba4fa42f
JF
126 size_t index(0);
127 for (size_t i(0); i != value.size; ++i) {
128 if (!DigitRange_[value.data[i]])
129 return _not(size_t);
130 index *= 10;
131 index += value.data[i] - '0';
132 }
133 return index;
134 } else if (value.size == 1)
37954781 135 return 0;
ba4fa42f
JF
136 else
137 return _not(size_t);
37954781
JF
138}
139
ba4fa42f 140// XXX: this isn't actually right
37954781
JF
141bool CYGetOffset(const char *value, ssize_t &index) {
142 if (value[0] != '0') {
143 char *end;
144 index = strtol(value, &end, 10);
145 if (value + strlen(value) == end)
146 return true;
147 } else if (value[1] == '\0') {
148 index = 0;
149 return true;
150 }
151
152 return false;
153}
154/* }}} */
37954781
JF
155/* JavaScript *ify {{{ */
156void CYStringify(std::ostringstream &str, const char *data, size_t size) {
157 unsigned quot(0), apos(0);
158 for (const char *value(data), *end(data + size); value != end; ++value)
159 if (*value == '"')
160 ++quot;
161 else if (*value == '\'')
162 ++apos;
163
164 bool single(quot > apos);
165
166 str << (single ? '\'' : '"');
167
168 for (const char *value(data), *end(data + size); value != end; ++value)
169 switch (*value) {
170 case '\\': str << "\\\\"; break;
171 case '\b': str << "\\b"; break;
172 case '\f': str << "\\f"; break;
173 case '\n': str << "\\n"; break;
174 case '\r': str << "\\r"; break;
175 case '\t': str << "\\t"; break;
176 case '\v': str << "\\v"; break;
177
178 case '"':
179 if (!single)
180 str << "\\\"";
181 else goto simple;
182 break;
183
184 case '\'':
185 if (single)
186 str << "\\'";
187 else goto simple;
188 break;
189
190 default:
b64ab4da 191 // this test is designed to be "awesome", generating neither warnings nor incorrect results
37954781 192 if (*value < 0x20 || *value >= 0x7f)
51a72a9d 193 str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value));
37954781
JF
194 else simple:
195 str << *value;
196 }
197
198 str << (single ? '\'' : '"');
199}
200
201void CYNumerify(std::ostringstream &str, double value) {
202 char string[32];
203 // XXX: I want this to print 1e3 rather than 1000
204 sprintf(string, "%.17g", value);
205 str << string;
206}
207
208bool CYIsKey(CYUTF8String value) {
209 const char *data(value.data);
210 size_t size(value.size);
211
212 if (size == 0)
213 return false;
214
215 if (DigitRange_[data[0]]) {
216 size_t index(CYGetIndex(value));
217 if (index == _not(size_t))
218 return false;
219 } else {
220 if (!WordStartRange_[data[0]])
221 return false;
222 for (size_t i(1); i != size; ++i)
223 if (!WordEndRange_[data[i]])
224 return false;
225 }
226
227 return true;
228}
229/* }}} */
230
37954781
JF
231double CYCastDouble(const char *value, size_t size) {
232 char *end;
233 double number(strtod(value, &end));
234 if (end != value + size)
235 return NAN;
236 return number;
237}
238
239double CYCastDouble(const char *value) {
240 return CYCastDouble(value, strlen(value));
241}
242
4dc2cd21 243extern "C" void CydgetPoolParse(apr_pool_t *pool, const uint16_t **data, size_t *size) {
7e5391fd 244 CYDriver driver;
59cd74c1
JF
245 cy::parser parser(driver);
246
247 CYUTF8String utf8(CYPoolUTF8String(pool, CYUTF16String(*data, *size)));
248
249 driver.data_ = utf8.data;
250 driver.size_ = utf8.size;
251
252 if (parser.parse() != 0 || !driver.errors_.empty())
253 return;
254
029bc65b
JF
255 CYOptions options;
256 CYContext context(driver.pool_, options);
59cd74c1
JF
257 driver.program_->Replace(context);
258 std::ostringstream str;
029bc65b 259 CYOutput out(str, options);
59cd74c1
JF
260 out << *driver.program_;
261 std::string code(str.str());
262
263 CYUTF16String utf16(CYPoolUTF16String(pool, CYUTF8String(code.c_str(), code.size())));
264
265 *data = utf16.data;
266 *size = utf16.size;
267}
268
37954781
JF
269static apr_pool_t *Pool_;
270
271static bool initialized_;
272
9cad30fa 273void CYInitializeStatic() {
37954781
JF
274 if (!initialized_)
275 initialized_ = true;
276 else return;
277
278 _aprcall(apr_initialize());
279 _aprcall(apr_pool_create(&Pool_, NULL));
37954781
JF
280}
281
282apr_pool_t *CYGetGlobalPool() {
9cad30fa 283 CYInitializeStatic();
37954781
JF
284 return Pool_;
285}
286
37954781
JF
287void CYThrow(const char *format, ...) {
288 va_list args;
b64ab4da 289 va_start(args, format);
37954781
JF
290 throw CYPoolError(format, args);
291 // XXX: does this matter? :(
b64ab4da 292 va_end(args);
37954781
JF
293}
294
295const char *CYPoolError::PoolCString(apr_pool_t *pool) const {
296 return apr_pstrdup(pool, message_);
297}
298
299CYPoolError::CYPoolError(const char *format, ...) {
300 va_list args;
b64ab4da 301 va_start(args, format);
37954781 302 message_ = apr_pvsprintf(pool_, format, args);
b64ab4da 303 va_end(args);
37954781
JF
304}
305
306CYPoolError::CYPoolError(const char *format, va_list args) {
307 message_ = apr_pvsprintf(pool_, format, args);
308}