File indexing completed on 2024-04-28 04:40:02

0001 #include "leinputstream.h"
0002 #include "leoutputstream.h"
0003 #include "introspection.h"
0004 #include "pole.h"
0005 #include <QBuffer>
0006 #include <QCoreApplication>
0007 #include <QDebug>
0008 #include <QFile>
0009 #include <QVariant>
0010 #include <cstdio>
0011 
0012 const Introspectable* parse(const QString& key, LEInputStream& in);
0013 void serialize(const Introspectable* i, const QString& key, LEOutputStream& out);
0014 
0015 using namespace std;
0016 
0017 void
0018 write(const QString& name, const QByteArray& data) {
0019     QFile out(name);
0020     out.open(QIODevice::WriteOnly);
0021     out.write(data);
0022     out.close();
0023 }
0024 
0025 bool
0026 parse(const QString& file) {
0027     try {
0028         POLE::Storage storage(file.toLocal8Bit());
0029         if (!storage.open()) {
0030             qDebug() << "Cannot open file " << file << ".";
0031             return false;
0032         }
0033 
0034         string prefix;
0035         if (storage.isDirectory("PP97_DUALSTORAGE")) {
0036             prefix = "PP97_DUALSTORAGE/";
0037         } else {
0038             prefix = "/";
0039         }
0040 
0041         list<string> entries = storage.entries(prefix);
0042         for (list<string>::const_iterator i=entries.begin(); i!=entries.end(); ++i) {
0043             // if encrypted, do not report failure.
0044             if (*i == "EncryptedSummary") return true;
0045         }
0046         for (list<string>::const_iterator i=entries.begin(); i!=entries.end(); ++i) {
0047             if (!storage.isDirectory(prefix+*i)) {
0048                 POLE::Stream stream(&storage, prefix+*i);
0049                 QString streamname(QString::fromStdString(*i));
0050                 QByteArray array;
0051                 array.resize(stream.size());
0052                 unsigned long read = stream.read((unsigned char*)array.data(), stream.size());
0053                 if (read != stream.size()) {
0054                     qDebug() << "Error reading stream " << streamname;
0055                     return false;
0056                 }
0057 //                write("/tmp/"+streamname+".in", array);
0058                 QBuffer buffer;
0059                 buffer.setData(array);
0060                 buffer.open(QIODevice::ReadOnly);
0061                 LEInputStream listream(&buffer);
0062                 qDebug() << "Parsing stream '" << streamname << "' of size "
0063                         << array.size();
0064                 const Introspectable* i = parse(streamname, listream);
0065                 if (listream.getPosition() != (qint64)stream.size()) {
0066                     qDebug() << stream.size() - listream.getPosition()
0067                         << "trailing bytes in stream " << streamname
0068                         << ", so probably an error at position "
0069                         << listream.getMaxPosition();
0070                     return false;
0071                 }
0072                 buffer.close();
0073 
0074                 buffer.buffer().clear();
0075                 buffer.open(QIODevice::WriteOnly);
0076                 LEOutputStream lostream(&buffer);
0077                 serialize(i, streamname, lostream);
0078 //                write("/tmp/"+streamname+".out", buffer.data());
0079                 if (array != buffer.data()) {
0080                     qDebug() << "Serialized data different from original in "
0081                         << streamname;
0082                     return false;
0083                 }
0084 
0085 
0086                 delete i;
0087             }
0088         }
0089     } catch (IOException& e) {
0090         qDebug() << "Error: " << e.msg;
0091         return false;
0092     }
0093     return true;
0094 }
0095 
0096 int
0097 main(int argc, char** argv) {
0098     QCoreApplication app(argc, argv);
0099     if (argc < 2) return -1;
0100 
0101     for (int i=1; i<argc; ++i) {
0102         QString file(argv[i]);
0103         qDebug() << "Parsing of " << file;
0104         if (parse(file)) {
0105             qDebug() << "Parsing of " << file << " succeedded.";
0106         } else {
0107             qDebug() << "Parsing of " << file << " failed.";
0108         }
0109     }
0110 
0111     return 0;
0112 }