| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtScript module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | // for strtoll
|
|---|
| 43 | #include <qplatformdefs.h>
|
|---|
| 44 |
|
|---|
| 45 | #ifndef QT_NO_SCRIPT
|
|---|
| 46 |
|
|---|
| 47 | #include "qscriptecmaglobal_p.h"
|
|---|
| 48 | #include "qscriptengine_p.h"
|
|---|
| 49 | #include "qscriptvalueimpl_p.h"
|
|---|
| 50 | #include "qscriptcontext_p.h"
|
|---|
| 51 | #include "qscriptmember_p.h"
|
|---|
| 52 | #include "qscriptobject_p.h"
|
|---|
| 53 |
|
|---|
| 54 | #include <QtCore/QVarLengthArray>
|
|---|
| 55 | #include <QtCore/qnumeric.h>
|
|---|
| 56 |
|
|---|
| 57 | QT_BEGIN_NAMESPACE
|
|---|
| 58 |
|
|---|
| 59 | extern double qstrtod(const char *s00, char const **se, bool *ok);
|
|---|
| 60 |
|
|---|
| 61 | namespace QScript {
|
|---|
| 62 |
|
|---|
| 63 | extern qsreal integerFromString(const QString &str, int radix);
|
|---|
| 64 |
|
|---|
| 65 | static inline char toHex(char c)
|
|---|
| 66 | {
|
|---|
| 67 | static const char hexnumbers[] = "0123456789ABCDEF";
|
|---|
| 68 | return hexnumbers[c & 0xf];
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | static int fromHex(char c)
|
|---|
| 72 | {
|
|---|
| 73 | if ((c >= '0') && (c <= '9'))
|
|---|
| 74 | return c - '0';
|
|---|
| 75 | if ((c >= 'A') && (c <= 'F'))
|
|---|
| 76 | return c - 'A' + 10;
|
|---|
| 77 | if ((c >= 'a') && (c <= 'f'))
|
|---|
| 78 | return c - 'a' + 10;
|
|---|
| 79 | return -1;
|
|---|
| 80 | }
|
|---|
| 81 |
|
|---|
| 82 | static QByteArray escape(const QString &input)
|
|---|
| 83 | {
|
|---|
| 84 | QVarLengthArray<char> output;
|
|---|
| 85 | output.reserve(input.size() * 3);
|
|---|
| 86 | const int length = input.length();
|
|---|
| 87 | for (int i = 0; i < length; ++i) {
|
|---|
| 88 | ushort uc = input.at(i).unicode();
|
|---|
| 89 | if (uc < 0x100) {
|
|---|
| 90 | if ( (uc > 0x60 && uc < 0x7B)
|
|---|
| 91 | || (uc > 0x3F && uc < 0x5B)
|
|---|
| 92 | || (uc > 0x2C && uc < 0x3A)
|
|---|
| 93 | || (uc == 0x2A)
|
|---|
| 94 | || (uc == 0x2B)
|
|---|
| 95 | || (uc == 0x5F)) {
|
|---|
| 96 | output.append(char(uc));
|
|---|
| 97 | } else {
|
|---|
| 98 | output.append('%');
|
|---|
| 99 | output.append(toHex(uc >> 4));
|
|---|
| 100 | output.append(toHex(uc));
|
|---|
| 101 | }
|
|---|
| 102 | } else {
|
|---|
| 103 | output.append('%');
|
|---|
| 104 | output.append('u');
|
|---|
| 105 | output.append(toHex(uc >> 12));
|
|---|
| 106 | output.append(toHex(uc >> 8));
|
|---|
| 107 | output.append(toHex(uc >> 4));
|
|---|
| 108 | output.append(toHex(uc));
|
|---|
| 109 | }
|
|---|
| 110 | }
|
|---|
| 111 | return QByteArray(output.constData(), output.size());
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | static QString unescape(const QByteArray &input)
|
|---|
| 115 | {
|
|---|
| 116 | QString result;
|
|---|
| 117 | int i = 0;
|
|---|
| 118 | const int length = input.length();
|
|---|
| 119 | while (i < length) {
|
|---|
| 120 | char c = input.at(i++);
|
|---|
| 121 | if ((c == '%') && (i + 1 < length)) {
|
|---|
| 122 | char a = input.at(i);
|
|---|
| 123 | if ((a == 'u') && (i + 4 < length)) {
|
|---|
| 124 | int d3 = fromHex(input.at(i+1));
|
|---|
| 125 | int d2 = fromHex(input.at(i+2));
|
|---|
| 126 | int d1 = fromHex(input.at(i+3));
|
|---|
| 127 | int d0 = fromHex(input.at(i+4));
|
|---|
| 128 | if ((d3 != -1) && (d2 != -1) && (d1 != -1) && (d0 != -1)) {
|
|---|
| 129 | ushort uc = ushort((d3 << 12) | (d2 << 8) | (d1 << 4) | d0);
|
|---|
| 130 | result.append(QChar(uc));
|
|---|
| 131 | i += 5;
|
|---|
| 132 | } else {
|
|---|
| 133 | result.append(QLatin1Char(c));
|
|---|
| 134 | }
|
|---|
| 135 | } else {
|
|---|
| 136 | int d1 = fromHex(a);
|
|---|
| 137 | int d0 = fromHex(input.at(i+1));
|
|---|
| 138 | if ((d1 != -1) && (d0 != -1)) {
|
|---|
| 139 | c = (d1 << 4) | d0;
|
|---|
| 140 | i += 2;
|
|---|
| 141 | }
|
|---|
| 142 | result.append(QLatin1Char(c));
|
|---|
| 143 | }
|
|---|
| 144 | } else {
|
|---|
| 145 | result.append(QLatin1Char(c));
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 | return result;
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | static const char uriReserved[] = ";/?:@&=+$,";
|
|---|
| 152 | static const char uriUnescaped[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";
|
|---|
| 153 |
|
|---|
| 154 | static QString encode(const QString &input, const QString &unescapedSet, bool *ok)
|
|---|
| 155 | {
|
|---|
| 156 | QString output;
|
|---|
| 157 | const int length = input.length();
|
|---|
| 158 | int i = 0;
|
|---|
| 159 | while (i < length) {
|
|---|
| 160 | const QChar c = input.at(i);
|
|---|
| 161 | if (!unescapedSet.contains(c)) {
|
|---|
| 162 | ushort uc = c.unicode();
|
|---|
| 163 | if ((uc >= 0xDC00) && (uc <= 0xDFFF)) {
|
|---|
| 164 | // URIError
|
|---|
| 165 | break;
|
|---|
| 166 | }
|
|---|
| 167 | if (!((uc < 0xD800) || (uc > 0xDBFF))) {
|
|---|
| 168 | ++i;
|
|---|
| 169 | if (i == length) {
|
|---|
| 170 | // URIError
|
|---|
| 171 | break;
|
|---|
| 172 | }
|
|---|
| 173 | const ushort uc2 = input.at(i).unicode();
|
|---|
| 174 | if ((uc < 0xDC00) || (uc > 0xDFFF)) {
|
|---|
| 175 | // URIError
|
|---|
| 176 | break;
|
|---|
| 177 | }
|
|---|
| 178 | uc = ((uc - 0xD800) * 0x400) + (uc2 - 0xDC00) + 0x10000;
|
|---|
| 179 | }
|
|---|
| 180 | QString tmp(1, QChar(uc));
|
|---|
| 181 | QByteArray octets = tmp.toUtf8();
|
|---|
| 182 | for (int j = 0; j < octets.length(); ++j) {
|
|---|
| 183 | output.append(QLatin1Char('%'));
|
|---|
| 184 | output.append(QLatin1Char(toHex(octets.at(j) >> 4)));
|
|---|
| 185 | output.append(QLatin1Char(toHex(octets.at(j))));
|
|---|
| 186 | }
|
|---|
| 187 | } else {
|
|---|
| 188 | output.append(c);
|
|---|
| 189 | }
|
|---|
| 190 | ++i;
|
|---|
| 191 | }
|
|---|
| 192 | *ok = (i == length);
|
|---|
| 193 | return output;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | static QString decode(const QString &input, const QString &reservedSet, bool *ok)
|
|---|
| 197 | {
|
|---|
|
|---|