summaryrefslogtreecommitdiffstats
path: root/src/libs/installer/binaryformat.h
blob: e2e1f6a0f06486e964380df97c08cb784d4c8a07 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/**************************************************************************
**
** This file is part of Installer Framework
**
** Copyright (c) 2010-2012 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
**
** GNU Lesser General Public License Usage
**
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this file.
** Please review the following information to ensure the GNU Lesser General
** Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
** If you have questions regarding the use of this file, please contact
** Nokia at qt-info@nokia.com.
**
**************************************************************************/

#ifndef BINARYFORMAT_H
#define BINARYFORMAT_H

#include "binaryformatenginehandler.h"
#include "range.h"
#include "qinstallerglobal.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QFile>
#include <QtCore/QHash>
#include <QtCore/QStack>
#include <QtCore/QVector>
#include <QtCore/QSharedPointer>

namespace QInstaller {
    static const qint64 MagicInstallerMarker = 0x12023233UL;
    static const qint64 MagicUninstallerMarker = 0x12023234UL;

    static const qint64 MagicUpdaterMarker =  0x12023235UL;
    static const qint64 MagicPackageManagerMarker = 0x12023236UL;

    // this cookie is put at the end of the file to determine whether we have data
    static const quint64 MagicCookie = 0xc2630a1c99d668f8LL;
    static const quint64 MagicCookieDat = 0xc2630a1c99d668f9LL;

    qint64 INSTALLER_EXPORT findMagicCookie(QFile *file, quint64 magicCookie = MagicCookie);
    void INSTALLER_EXPORT appendFileData(QIODevice *out, QIODevice *in);
    void INSTALLER_EXPORT appendInt64(QIODevice *out, qint64 n);
    void INSTALLER_EXPORT appendInt64Range(QIODevice *out, const Range<qint64> &r);
    void INSTALLER_EXPORT appendData(QIODevice *out, QIODevice *in, qint64 size);
    void INSTALLER_EXPORT appendByteArray(QIODevice *out, const QByteArray &ba);
    void INSTALLER_EXPORT appendString(QIODevice *out, const QString &str);
    void INSTALLER_EXPORT appendStringList(QIODevice *out, const QStringList &list);
    void INSTALLER_EXPORT appendDictionary(QIODevice *out, const QHash<QString,QString> &dict);
    qint64 INSTALLER_EXPORT appendCompressedData(QIODevice *out, QIODevice *in, qint64 size);

    void INSTALLER_EXPORT retrieveFileData(QIODevice *out, QIODevice *in);
    qint64 INSTALLER_EXPORT retrieveInt64(QIODevice *in);
    Range<qint64> INSTALLER_EXPORT retrieveInt64Range(QIODevice *in);
    QByteArray INSTALLER_EXPORT retrieveByteArray(QIODevice *in);
    QString INSTALLER_EXPORT retrieveString(QIODevice *in);
    QStringList INSTALLER_EXPORT retrieveStringList(QIODevice *in);
    QHash<QString,QString> INSTALLER_EXPORT retrieveDictionary(QIODevice *in);
    QByteArray INSTALLER_EXPORT retrieveData(QIODevice *in, qint64 size);
    QByteArray INSTALLER_EXPORT retrieveCompressedData(QIODevice *in, qint64 size);
}

namespace QInstallerCreator {
class Component;

class INSTALLER_EXPORT Archive : public QIODevice
{
    Q_OBJECT
public:
    explicit Archive(const QString &path);
    Archive(const QByteArray &name, const QByteArray &data);
    Archive(const QByteArray &name, const QSharedPointer<QFile> &device, const Range<qint64> &segment);
    ~Archive();

    bool open(OpenMode mode);
    void close();

    bool seek(qint64 pos);
    qint64 size() const;

    bool createZippedFile();
    bool isZippedDirectory() const;
    bool copy(const QString &name);

    QByteArray name() const;
    void setName(const QByteArray &name);

protected:
    qint64 readData(char *data, qint64 maxSize);
    qint64 writeData(const char *data, qint64 maxSize);

    Range< qint64 > binarySegment() const;

private:
    //used when when reading from the installer
    QSharedPointer<QFile> m_device;
    const Range<qint64> m_segment;

    //used when creating the installer, archive input file
    QFile m_inputFile;
    const bool m_isTempFile;
    const QString m_path;
    QByteArray m_name;
};

class INSTALLER_EXPORT Component
{
    Q_DECLARE_TR_FUNCTIONS(Component)

public:
    virtual ~Component();

    static Component readFromIndexEntry(const QSharedPointer<QFile> &dev, qint64 offset);
    void writeIndexEntry(QIODevice *dev, qint64 offset) const;

    void writeData(QIODevice *dev, qint64 positionOffset) const;
    void readData(const QSharedPointer<QFile> &dev, qint64 offset);

    QByteArray name() const;
    void setName(const QByteArray &ba);

    QString dataDirectory() const;
    void setDataDirectory(const QString &path);

    Range<qint64> binarySegment() const;
    void setBinarySegment(const Range<qint64> &r);

    void appendArchive(const QSharedPointer<Archive> &archive);
    QSharedPointer<Archive> archiveByName(const QByteArray &name) const;
    QVector< QSharedPointer<Archive> > archives() const;

    bool operator<(const Component &other) const;
    bool operator==(const Component &other) const;

private:
    QByteArray m_name;
    QVector<QSharedPointer<Archive> > m_archives;
    mutable Range<qint64> m_binarySegment;
    QString m_dataDirectory;
};


class INSTALLER_EXPORT ComponentIndex
{
public:
    ComponentIndex();
    static ComponentIndex read(const QSharedPointer<QFile> &dev, qint64 offset);
    void writeIndex(QIODevice *dev, qint64 offset) const;
    void writeComponentData(QIODevice *dev, qint64 offset) const;
    Component componentByName(const QByteArray &name) const;
    void insertComponent(const Component &name);
    void removeComponent(const QByteArray &name);
    QVector<Component> components() const;
    int componentCount() const;

private:
    QHash<QByteArray, Component> m_components;
};
}

namespace QInstaller {

struct BinaryLayout
{
    QVector<Range<qint64> > metadataResourceSegments;
    qint64 operationsStart;
    qint64 operationsEnd;
    qint64 resourceCount;
    qint64 dataBlockSize;
    qint64 magicMarker;
    quint64 magicCookie;
    qint64 indexSize;
    qint64 endOfData;
};

class BinaryContentPrivate : public QSharedData
{
public:
    BinaryContentPrivate(const QString &path);
    BinaryContentPrivate(const BinaryContentPrivate &other);
    ~BinaryContentPrivate();

    qint64 m_magicMarker;
    qint64 m_dataBlockStart;

    QSharedPointer<QFile> m_appBinary;
    QSharedPointer<QFile> m_binaryDataFile;

    QList<Operation *> m_performedOperations;
    QList<QPair<QString, QString> > m_performedOperationsData;

    QVector<const uchar *> m_resourceMappings;
    QVector<Range<qint64> > m_metadataResourceSegments;

    QInstallerCreator::ComponentIndex m_componentIndex;
    QInstallerCreator::BinaryFormatEngineHandler m_binaryFormatEngineHandler;
};

class INSTALLER_EXPORT BinaryContent
{
    explicit BinaryContent(const QString &path);

public:
    virtual ~BinaryContent();

    static BinaryContent readAndRegisterFromApplicationFile();
    static BinaryContent readAndRegisterFromBinary(const QString &path);

    static BinaryContent readFromApplicationFile();
    static BinaryContent readFromBinary(const QString &path);

    static BinaryLayout readBinaryLayout(QIODevice *const file, qint64 cookiePos);

    int registerPerformedOperations();
    OperationList performedOperations() const;

    qint64 magicMarker() const;
    int registerEmbeddedQResources();
    QInstallerCreator::ComponentIndex componentIndex() const;

private:
    static void readBinaryData(BinaryContent &content, const QSharedPointer<QFile> &file,
        const BinaryLayout &layout);

private:
    QSharedDataPointer<BinaryContentPrivate> d;
};

}

#endif // BINARYFORMAT_H