Clean up the resource reading code

Instead of doing the conversion from the big-endian data by hand, let's
use the convenience functions from qendian.h.

Change-Id: If3966ca94428afabb1f5c922967fb9970f976622
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Simon Hausmann 2016-11-21 15:44:36 +01:00
parent 60054b5940
commit dc4c647137

View File

@ -49,6 +49,7 @@
#include "qdatetime.h" #include "qdatetime.h"
#include "qbytearray.h" #include "qbytearray.h"
#include "qstringlist.h" #include "qstringlist.h"
#include "qendian.h"
#include <qshareddata.h> #include <qshareddata.h>
#include <qplatformdefs.h> #include <qplatformdefs.h>
#include "private/qabstractfileengine_p.h" #include "private/qabstractfileengine_p.h"
@ -606,11 +607,9 @@ inline uint QResourceRoot::hash(int node) const
if(!node) //root if(!node) //root
return 0; return 0;
const int offset = findOffset(node); const int offset = findOffset(node);
int name_offset = (tree[offset+0] << 24) + (tree[offset+1] << 16) + qint32 name_offset = qFromBigEndian<qint32>(tree + offset);
(tree[offset+2] << 8) + (tree[offset+3] << 0);
name_offset += 2; //jump past name length name_offset += 2; //jump past name length
return (names[name_offset+0] << 24) + (names[name_offset+1] << 16) + return qFromBigEndian<quint32>(names + name_offset);
(names[name_offset+2] << 8) + (names[name_offset+3] << 0);
} }
inline QString QResourceRoot::name(int node) const inline QString QResourceRoot::name(int node) const
{ {
@ -619,10 +618,8 @@ inline QString QResourceRoot::name(int node) const
const int offset = findOffset(node); const int offset = findOffset(node);
QString ret; QString ret;
int name_offset = (tree[offset+0] << 24) + (tree[offset+1] << 16) + qint32 name_offset = qFromBigEndian<qint32>(tree + offset);
(tree[offset+2] << 8) + (tree[offset+3] << 0); const qint16 name_length = qFromBigEndian<qint16>(names + name_offset);
const short name_length = (names[name_offset+0] << 8) +
(names[name_offset+1] << 0);
name_offset += 2; name_offset += 2;
name_offset += 4; //jump past hash name_offset += 4; //jump past hash
@ -662,10 +659,8 @@ int QResourceRoot::findNode(const QString &_path, const QLocale &locale) const
return 0; return 0;
//the root node is always first //the root node is always first
int child_count = (tree[6] << 24) + (tree[7] << 16) + qint32 child_count = qFromBigEndian<qint32>(tree + 6);
(tree[8] << 8) + (tree[9] << 0); qint32 child = qFromBigEndian<qint32>(tree + 10);
int child = (tree[10] << 24) + (tree[11] << 16) +
(tree[12] << 8) + (tree[13] << 0);
//now iterate up the tree //now iterate up the tree
int node = -1; int node = -1;
@ -711,18 +706,15 @@ int QResourceRoot::findNode(const QString &_path, const QLocale &locale) const
#endif #endif
offset += 4; //jump past name offset += 4; //jump past name
const short flags = (tree[offset+0] << 8) + const qint16 flags = qFromBigEndian<qint16>(tree + offset);
(tree[offset+1] << 0);
offset += 2; offset += 2;
if(!splitter.hasNext()) { if(!splitter.hasNext()) {
if(!(flags & Directory)) { if(!(flags & Directory)) {
const short country = (tree[offset+0] << 8) + const qint16 country = qFromBigEndian<qint16>(tree + offset);
(tree[offset+1] << 0);
offset += 2; offset += 2;
const short language = (tree[offset+0] << 8) + const qint16 language = qFromBigEndian<qint16>(tree + offset);
(tree[offset+1] << 0);
offset += 2; offset += 2;
#ifdef DEBUG_RESOURCE_MATCH #ifdef DEBUG_RESOURCE_MATCH
qDebug() << " " << "LOCALE" << country << language; qDebug() << " " << "LOCALE" << country << language;
@ -749,11 +741,9 @@ int QResourceRoot::findNode(const QString &_path, const QLocale &locale) const
if(!(flags & Directory)) if(!(flags & Directory))
return -1; return -1;
child_count = (tree[offset+0] << 24) + (tree[offset+1] << 16) + child_count = qFromBigEndian<qint32>(tree + offset);
(tree[offset+2] << 8) + (tree[offset+3] << 0);
offset += 4; offset += 4;
child = (tree[offset+0] << 24) + (tree[offset+1] << 16) + child = qFromBigEndian<qint32>(tree + offset);
(tree[offset+2] << 8) + (tree[offset+3] << 0);
break; break;
} }
} }
@ -771,7 +761,7 @@ short QResourceRoot::flags(int node) const
if(node == -1) if(node == -1)
return 0; return 0;
const int offset = findOffset(node) + 4; //jump past name const int offset = findOffset(node) + 4; //jump past name
return (tree[offset+0] << 8) + (tree[offset+1] << 0); return qFromBigEndian<qint16>(tree + offset);
} }
const uchar *QResourceRoot::data(int node, qint64 *size) const const uchar *QResourceRoot::data(int node, qint64 *size) const
{ {
@ -781,16 +771,14 @@ const uchar *QResourceRoot::data(int node, qint64 *size) const
} }
int offset = findOffset(node) + 4; //jump past name int offset = findOffset(node) + 4; //jump past name
const short flags = (tree[offset+0] << 8) + (tree[offset+1] << 0); const qint16 flags = qFromBigEndian<qint16>(tree + offset);
offset += 2; offset += 2;
offset += 4; //jump past locale offset += 4; //jump past locale
if(!(flags & Directory)) { if(!(flags & Directory)) {
const int data_offset = (tree[offset+0] << 24) + (tree[offset+1] << 16) + const qint32 data_offset = qFromBigEndian<qint32>(tree + offset);
(tree[offset+2] << 8) + (tree[offset+3] << 0); const quint32 data_length = qFromBigEndian<quint32>(payloads + data_offset);
const uint data_length = (payloads[data_offset+0] << 24) + (payloads[data_offset+1] << 16) +
(payloads[data_offset+2] << 8) + (payloads[data_offset+3] << 0);
const uchar *ret = payloads+data_offset+4; const uchar *ret = payloads+data_offset+4;
*size = data_length; *size = data_length;
return ret; return ret;
@ -806,10 +794,7 @@ QDateTime QResourceRoot::lastModified(int node) const
const int offset = findOffset(node) + 14; const int offset = findOffset(node) + 14;
const quint64 timeStamp = (quint64(tree[offset+0]) << 56) + (quint64(tree[offset+1]) << 48) + const quint64 timeStamp = qFromBigEndian<quint64>(tree + offset);
(quint64(tree[offset+2]) << 40) + (quint64(tree[offset+3]) << 32) +
(quint64(tree[offset+4]) << 24) + (quint64(tree[offset+5]) << 16) +
(quint64(tree[offset+6]) << 8) + (quint64(tree[offset+7]));
if (timeStamp == 0) if (timeStamp == 0)
return QDateTime(); return QDateTime();
@ -822,16 +807,14 @@ QStringList QResourceRoot::children(int node) const
return QStringList(); return QStringList();
int offset = findOffset(node) + 4; //jump past name int offset = findOffset(node) + 4; //jump past name
const short flags = (tree[offset+0] << 8) + (tree[offset+1] << 0); const qint16 flags = qFromBigEndian<qint16>(tree + offset);
offset += 2; offset += 2;
QStringList ret; QStringList ret;
if(flags & Directory) { if(flags & Directory) {
const int child_count = (tree[offset+0] << 24) + (tree[offset+1] << 16) + const qint32 child_count = qFromBigEndian<qint32>(tree + offset);
(tree[offset+2] << 8) + (tree[offset+3] << 0);
offset += 4; offset += 4;
const int child_off = (tree[offset+0] << 24) + (tree[offset+1] << 16) + const qint32 child_off = qFromBigEndian<qint32>(tree + offset);
(tree[offset+2] << 8) + (tree[offset+3] << 0);
ret.reserve(child_count); ret.reserve(child_count);
for(int i = child_off; i < child_off+child_count; ++i) for(int i = child_off; i < child_off+child_count; ++i)
ret << name(i); ret << name(i);
@ -935,20 +918,16 @@ public:
} }
offset += 4; offset += 4;
const int version = (b[offset+0] << 24) + (b[offset+1] << 16) + const int version = qFromBigEndian<qint32>(b + offset);
(b[offset+2] << 8) + (b[offset+3] << 0);
offset += 4; offset += 4;
const int tree_offset = (b[offset+0] << 24) + (b[offset+1] << 16) + const int tree_offset = qFromBigEndian<qint32>(b + offset);
(b[offset+2] << 8) + (b[offset+3] << 0);
offset += 4; offset += 4;
const int data_offset = (b[offset+0] << 24) + (b[offset+1] << 16) + const int data_offset = qFromBigEndian<qint32>(b + offset);
(b[offset+2] << 8) + (b[offset+3] << 0);
offset += 4; offset += 4;
const int name_offset = (b[offset+0] << 24) + (b[offset+1] << 16) + const int name_offset = qFromBigEndian<qint32>(b + offset);
(b[offset+2] << 8) + (b[offset+3] << 0);
offset += 4; offset += 4;
// Some sanity checking for sizes. This is _not_ a security measure. // Some sanity checking for sizes. This is _not_ a security measure.