Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Endian.cpp « core « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 177c3a7fbfc230f77c69a1ee7e4db445cd341de9 (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
/*
 *  Copyright (C) 2010 Felix Geyer <debfx@fobos.de>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 or (at your option)
 *  version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "Endian.h"

#include <QtCore/QtEndian>
#include <QtCore/QIODevice>

namespace Endian
{

qint16 bytesToInt16(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
    Q_ASSERT(ba.size() == 2);

    if (byteOrder == QSysInfo::LittleEndian) {
        return qFromLittleEndian<qint16>(reinterpret_cast<const uchar*>(ba.constData()));
    }
    else {
        return qFromBigEndian<qint16>(reinterpret_cast<const uchar*>(ba.constData()));
    }
}

qint32 bytesToInt32(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
    Q_ASSERT(ba.size() == 4);

    if (byteOrder == QSysInfo::LittleEndian) {
        return qFromLittleEndian<qint32>(reinterpret_cast<const uchar*>(ba.constData()));
    }
    else {
        return qFromBigEndian<qint32>(reinterpret_cast<const uchar*>(ba.constData()));
    }
}

qint64 bytesToInt64(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
    Q_ASSERT(ba.size() == 8);

    if (byteOrder == QSysInfo::LittleEndian) {
        return qFromLittleEndian<qint64>(reinterpret_cast<const uchar*>(ba.constData()));
    }
    else {
        return qFromBigEndian<qint64>(reinterpret_cast<const uchar*>(ba.constData()));
    }
}

quint16 bytesToUInt16(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
    return bytesToInt16(ba, byteOrder);
}

quint32 bytesToUInt32(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
    return bytesToInt32(ba, byteOrder);
}

quint64 bytesToUInt64(const QByteArray& ba, QSysInfo::Endian byteOrder)
{
    return bytesToInt64(ba, byteOrder);
}

qint16 readInt16(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
    QByteArray ba = device->read(2);

    if (ba.size() != 2) {
        *ok = false;
        return 0;
    }
    else {
        *ok = true;
        return bytesToUInt16(ba, byteOrder);
    }
}

qint32 readInt32(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
    QByteArray ba = device->read(4);

    if (ba.size() != 4) {
        *ok = false;
        return 0;
    }
    else {
        *ok = true;
        return bytesToUInt32(ba, byteOrder);
    }
}

qint64 readInt64(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
    QByteArray ba = device->read(8);

    if (ba.size() != 8) {
        *ok = false;
        return 0;
    }
    else {
        *ok = true;
        return bytesToUInt64(ba, byteOrder);
    }
}

quint16 readUInt16(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
    return readInt16(device, byteOrder, ok);
}

quint32 readUInt32(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
    return readInt32(device, byteOrder, ok);
}

quint64 readUInt64(QIODevice* device, QSysInfo::Endian byteOrder, bool* ok)
{
    return readInt64(device, byteOrder, ok);
}

QByteArray int16ToBytes(qint16 num, QSysInfo::Endian byteOrder)
{
    QByteArray ba;
    ba.resize(2);

    if (byteOrder == QSysInfo::LittleEndian) {
        qToLittleEndian<qint16>(num, reinterpret_cast<uchar*>(ba.data()));
    }
    else {
        qToBigEndian<qint64>(num, reinterpret_cast<uchar*>(ba.data()));
    }

    return ba;
}

QByteArray int32ToBytes(qint32 num, QSysInfo::Endian byteOrder)
{
    QByteArray ba;
    ba.resize(4);

    if (byteOrder == QSysInfo::LittleEndian) {
        qToLittleEndian<qint32>(num, reinterpret_cast<uchar*>(ba.data()));
    }
    else {
        qToBigEndian<qint32>(num, reinterpret_cast<uchar*>(ba.data()));
    }

    return ba;
}

QByteArray int64ToBytes(qint64 num, QSysInfo::Endian byteOrder)
{
    QByteArray ba;
    ba.resize(8);

    if (byteOrder == QSysInfo::LittleEndian) {
        qToLittleEndian<qint64>(num, reinterpret_cast<uchar*>(ba.data()));
    }
    else {
        qToBigEndian<qint64>(num, reinterpret_cast<uchar*>(ba.data()));
    }

    return ba;
}

bool writeInt16(qint16 num, QIODevice* device, QSysInfo::Endian byteOrder)
{
    QByteArray ba = int16ToBytes(num, byteOrder);
    int bytesWritten = device->write(ba);
    return (bytesWritten == ba.size());
}

bool writeInt32(qint32 num, QIODevice* device, QSysInfo::Endian byteOrder)
{
    QByteArray ba = int32ToBytes(num, byteOrder);
    int bytesWritten = device->write(ba);
    return (bytesWritten == ba.size());
}

bool writeInt64(qint64 num, QIODevice* device, QSysInfo::Endian byteOrder)
{
    QByteArray ba = int64ToBytes(num, byteOrder);
    int bytesWritten = device->write(ba);
    return (bytesWritten == ba.size());
}

} // namespace Endian