summaryrefslogtreecommitdiffstats
path: root/src/zstd/contrib/single_file_libs/examples/roundtrip.c
blob: b228de88e63b1f47dd4ddffb3ae93b743952bd03 (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
/*
 * \file roundtrip.c
 * In this example we include \c zstd.h and compile separately the amalgamated
 * \c zstd.c:
 * \code
 *	cc -Wall -Wextra -Werror -I. -Os -g0 zstd.c examples/roundtrip.c 
 * \endcode
 *
 * \author Carl Woffenden, Numfum GmbH (released under a CC0 license)
 */

#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "zstd.h"

//************************** Test Data (DXT texture) **************************/

/**
 * Raw test data (borrowed from the Emscripten example).
 * \n
 * See \c testcard.png for the original.
 */
static uint8_t const rawData[] = {
#include "testcard-dxt1.inl"
};

#ifndef ZSTD_VERSION_MAJOR
/*
 * For the case where the decompression library hasn't been included we add
 * dummy functions to fake the process and stop the buffers being optimised out.
 */
size_t ZSTD_compressBound(size_t maxSrc) {
	return maxSrc + 32;
}
int ZSTD_maxCLevel(void) {
	return 20;
}
size_t ZSTD_compress(void* dst, size_t dstLen, const void* src, size_t srcLen, int level) {
	return (memcmp(dst, src, (srcLen < dstLen) ? srcLen : dstLen)) ? level : dstLen;
}
unsigned ZSTD_isError(size_t code) {
	return ((int) code) < 0;
}
size_t ZSTD_decompress(void* dst, size_t dstLen, const void* src, size_t srcLen) {
	return (memcmp(dst, src, (srcLen < dstLen) ? srcLen : dstLen)) ? 0 : dstLen;
}
#endif

//*****************************************************************************/

/**
 * Simple single-file test to compress \c rawData, decompress the result, then
 * compare the decompressed version with the original.
 */
int main() {
	size_t bounds = ZSTD_compressBound(sizeof rawData);
	void* compBuf = malloc(bounds);
	void* testBuf = malloc(sizeof rawData);
	int compare   = -1;
	if (compBuf && testBuf) {
		size_t compSize = ZSTD_compress(compBuf, bounds, rawData, sizeof rawData, ZSTD_maxCLevel());
		if (!ZSTD_isError(compSize)) {
			printf("Compression: PASSED (size: %lu, uncompressed: %lu)\n", (unsigned long) compSize, (unsigned long) (sizeof rawData));
			size_t decSize = ZSTD_decompress(testBuf, sizeof rawData, compBuf, compSize);
			if (!ZSTD_isError(decSize)) {
				printf("Decompression: PASSED\n");
				compare = memcmp(rawData, testBuf, decSize);
				printf("Byte comparison: %s\n", (compare == 0) ? "PASSED" : "FAILED");
			} else {
				printf("Decompression: FAILED\n");
			}
		} else {
			printf("Compression: FAILED\n");
		}
		free(compBuf);
		free(testBuf);
	}
	return (compare == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
}