Hobrasoft httpd server
Embedded HTTP server for Qt and C++
httpgzipcompression.cpp
Go to the documentation of this file.
1 
5 #include "httpgzipcompression.h"
6 #include <QDataStream>
7 #include <QDebug>
8 
9 
10 QByteArray HttpGZipCompression::compressData(const QByteArray &data) {
11  return header() + compress(data) + footer(data);
12 }
13 
14 
15 QByteArray HttpGZipCompression::header() {
16  return QByteArray::fromHex("1f8b080000000000000b");
17 }
18 
19 
20 QByteArray HttpGZipCompression::compress(const QByteArray &sourceData) {
21  QByteArray compressedData = qCompress(sourceData);
22  compressedData.remove(0, 6);
23  compressedData.chop(4);
24  return compressedData;
25 }
26 
27 
28 QByteArray HttpGZipCompression::footer(const QByteArray &sourceData) {
29  QByteArray footer;
30  QDataStream footerStream(&footer, QIODevice::WriteOnly);
31  footerStream.setByteOrder(QDataStream::LittleEndian);
32  footerStream << crc32(sourceData) << quint32(sourceData.size());
33  return footer;
34 }
35 
36 
37 quint32 HttpGZipCompression::updateCRC32(unsigned char ch, quint32 crc) {
38  return (crc_32_tab[((crc) ^ ((quint32)ch)) & 0xff] ^ ((crc) >> 8));
39 }
40 
41 
42 quint32 HttpGZipCompression::crc32(const QByteArray& data) {
43  quint32 crc = 0xFFFFFFFFL;
44  for (int i=0; i<data.size(); i++) {
45  crc = updateCRC32(data[i], crc);
46  }
47  return ~crc;
48 }
49