/* * Copyright (C) 2010 Felix Geyer * * 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 . */ #include "Endian.h" #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 3)) #pragma GCC push_options #pragma GCC diagnostic ignored "-Wold-style-cast" #endif #include #if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 3)) #pragma GCC pop_options #endif #include namespace Endian { qint16 bytesToInt16(const QByteArray& ba, QSysInfo::Endian byteOrder) { Q_ASSERT(ba.size() == 2); if (byteOrder == QSysInfo::LittleEndian) { return qFromLittleEndian(reinterpret_cast(ba.constData())); } else { return qFromBigEndian(reinterpret_cast(ba.constData())); } } qint32 bytesToInt32(const QByteArray& ba, QSysInfo::Endian byteOrder) { Q_ASSERT(ba.size() == 4); if (byteOrder == QSysInfo::LittleEndian) { return qFromLittleEndian(reinterpret_cast(ba.constData())); } else { return qFromBigEndian(reinterpret_cast(ba.constData())); } } qint64 bytesToInt64(const QByteArray& ba, QSysInfo::Endian byteOrder) { Q_ASSERT(ba.size() == 8); if (byteOrder == QSysInfo::LittleEndian) { return qFromLittleEndian(reinterpret_cast(ba.constData())); } else { return qFromBigEndian(reinterpret_cast(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(num, reinterpret_cast(ba.data())); } else { qToBigEndian(num, reinterpret_cast(ba.data())); } return ba; } QByteArray int32ToBytes(qint32 num, QSysInfo::Endian byteOrder) { QByteArray ba; ba.resize(4); if (byteOrder == QSysInfo::LittleEndian) { qToLittleEndian(num, reinterpret_cast(ba.data())); } else { qToBigEndian(num, reinterpret_cast(ba.data())); } return ba; } QByteArray int64ToBytes(qint64 num, QSysInfo::Endian byteOrder) { QByteArray ba; ba.resize(8); if (byteOrder == QSysInfo::LittleEndian) { qToLittleEndian(num, reinterpret_cast(ba.data())); } else { qToBigEndian(num, reinterpret_cast(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