File: gzip.cpp

package info (click to toggle)
acoustid-fingerprinter 0.6-4
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 504 kB
  • ctags: 285
  • sloc: cpp: 1,626; ansic: 204; makefile: 3
file content (45 lines) | stat: -rw-r--r-- 1,094 bytes parent folder | download | duplicates (3)
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
#include "crc.h"
#include "gzip.h"

inline QByteArray render32BitInt(unsigned long value)
{
	unsigned char data[4];
	data[0] = (value      ) & 255;
	data[1] = (value >>  8) & 255;
	data[2] = (value >> 16) & 255;
	data[3] = (value >> 24) & 255;
	return QByteArray((char *)data, 4);
}

inline unsigned long calculateCrc32(const QByteArray &data)
{
	crc_t crc;
	crc = crc_init();
	crc = crc_update(crc, (unsigned char *)data.data(), data.size());
	crc = crc_finalize(crc);
	return crc;
}

QByteArray gzipCompress(const QByteArray &data)
{
	const char header[10] = {
		0x1f, 0x8b,	// ID1 + ID2
		8,			// Compression Method
		0,			// Flags
		0, 0, 0, 0, // Modification Time
		2,			// Extra Flags
		255,		// Operating System
	};

	QByteArray compressedData = qCompress(data);
	compressedData.remove(0, 6); // Qt size + zlib header
	compressedData.remove(compressedData.size() - 4, 4); // zlib footer

	QByteArray result;
	result.append(header, 10);
	result.append(compressedData);
	result.append(render32BitInt(calculateCrc32(data)));
	result.append(render32BitInt(data.size()));
	return result;
}