summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@nokia.com>2012-01-07 17:57:20 +0100
committerJamey Hicks <jamey.hicks@nokia.com>2012-01-09 12:27:43 +0100
commit36820aaf81585241aea551c0d8d6d688f8abde1b (patch)
tree702613e87a64b19f100ae92760dd47e2cd6d6089
parent73790e615bf9d383dc6c8c9462928c74783e7071 (diff)
Make the json parser endian safe.
Change-Id: I7dfb3986103d17911d6fc2d40fc2bc1eb6ff8b85 Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com> Reviewed-by: Jamey Hicks <jamey.hicks@nokia.com>
-rw-r--r--src/qjsonparser.cpp23
1 files changed, 15 insertions, 8 deletions
diff --git a/src/qjsonparser.cpp b/src/qjsonparser.cpp
index bd60386..9d33ff0 100644
--- a/src/qjsonparser.cpp
+++ b/src/qjsonparser.cpp
@@ -164,7 +164,7 @@ QtJson::JsonDocument QJsonParser::parse()
// fill in Header data
Header *h = (Header *)data;
h->tag = QBJS_Tag;
- h->version = 1;
+ h->version = 1u;
current = sizeof(Header);
@@ -228,7 +228,14 @@ bool QJsonParser::parseObject()
if (offsets.size()) {
int tableSize = offsets.size()*sizeof(uint);
table = reserveSpace(tableSize);
+#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
memcpy(data + table, offsets.constData(), tableSize);
+#else
+ offset *o = (offset *)(data + table);
+ for (int i = 0; i < tableSize; ++i)
+ o[i] = offsets[i];
+
+#endif
}
Object *o = (Object *)(data + objectOffset);
@@ -480,7 +487,7 @@ bool QJsonParser::parseNumber(Value *val, int baseOffset)
bool ok;
union {
- char raw[sizeof(double)];
+ quint64 ui;
double d;
};
d = number.toDouble(&ok);
@@ -489,7 +496,7 @@ bool QJsonParser::parseNumber(Value *val, int baseOffset)
return false;
int pos = reserveSpace(sizeof(double));
- memcpy(data + pos, raw, sizeof(double));
+ *(quint64 *)(data + pos) = qToLittleEndian(ui);
val->val = pos - baseOffset;
val->latinOrIntValue = false;
@@ -672,7 +679,7 @@ bool QJsonParser::parseString(bool *latin1)
// no unicode string, we are done
if (*latin1) {
// write string length
- *(ushort *)(data + stringPos) = current - outStart - sizeof(ushort);
+ *(qle_ushort *)(data + stringPos) = current - outStart - sizeof(ushort);
int pos = reserveSpace((4 - current) & 3);
while (pos & 3)
data[pos++] = 0;
@@ -699,11 +706,11 @@ bool QJsonParser::parseString(bool *latin1)
}
if (ch > 0xffff) {
int pos = reserveSpace(4);
- *(ushort *)(data + pos) = QChar::highSurrogate(ch);
- *(ushort *)(data + pos + 2) = QChar::lowSurrogate(ch);
+ *(qle_ushort *)(data + pos) = QChar::highSurrogate(ch);
+ *(qle_ushort *)(data + pos + 2) = QChar::lowSurrogate(ch);
} else {
int pos = reserveSpace(2);
- *(ushort *)(data + pos) = (ushort)ch;
+ *(qle_ushort *)(data + pos) = (ushort)ch;
}
}
++json;
@@ -712,7 +719,7 @@ bool QJsonParser::parseString(bool *latin1)
return false;
// write string length
- *(int *)(data + stringPos) = (current - outStart - sizeof(int))/2;
+ *(qle_int *)(data + stringPos) = (current - outStart - sizeof(int))/2;
int pos = reserveSpace((4 - current) & 3);
while (pos & 3)
data[pos++] = 0;