mirror of
https://github.com/jaredtao/TaoQuick.git
synced 2025-01-31 21:22:58 +08:00
35 lines
959 B
C
35 lines
959 B
C
#pragma once
|
|
#include <QByteArray>
|
|
#include <QByteArrayList>
|
|
#include <QDataStream>
|
|
#include <QIODevice>
|
|
const int static headerLength = sizeof(quint32);
|
|
|
|
static QByteArray pack(const QByteArray &data)
|
|
{
|
|
QByteArray header(headerLength, 0);
|
|
QDataStream os(&header, QIODevice::WriteOnly);
|
|
os << static_cast<quint32>(data.length());
|
|
return header + data;
|
|
}
|
|
static QByteArrayList unpack(const QByteArray &data)
|
|
{
|
|
QByteArrayList list;
|
|
QDataStream inStream(data);
|
|
quint32 sum = data.size();
|
|
quint32 pos = 0;
|
|
while (pos + headerLength < sum) {
|
|
quint32 packageLen = 0;
|
|
packageLen = 0;
|
|
inStream >> packageLen;
|
|
if (packageLen + headerLength > sum - pos) {
|
|
break;
|
|
}
|
|
QByteArray subPackage = data.mid(pos + headerLength, packageLen);
|
|
inStream.skipRawData(packageLen);
|
|
list.append(subPackage);
|
|
pos += headerLength + packageLen;
|
|
}
|
|
return list;
|
|
}
|