File: simple.c

package info (click to toggle)
mame 0.280%2Bdfsg.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 911,560 kB
  • sloc: cpp: 5,252,752; xml: 2,221,427; ansic: 750,970; sh: 34,449; lisp: 19,643; python: 16,304; makefile: 13,251; java: 8,492; yacc: 8,152; javascript: 7,069; cs: 6,013; asm: 4,786; ada: 1,681; pascal: 1,191; lex: 1,174; perl: 585; ruby: 373
file content (75 lines) | stat: -rw-r--r-- 2,309 bytes parent folder | download | duplicates (16)
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
/**
 * \file simple.c
 * Simple standalone example of using the single-file \c zstddeclib.
 * 
 * \note In this simple example we include the amalgamated source and compile
 * just this single file, but we could equally (and more conventionally)
 * include \c zstd.h and compile both this file and \c zstddeclib.c (the
 * resulting binaries differ slightly in size but perform the same).
 * 
 * \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 "../zstddeclib.c"

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

/**
 * Raw 256x256 DXT1 data (used to compare the result).
 * \n
 * See \c testcard.png for the original.
 */
static uint8_t const rawDxt1[] = {
#include "testcard-dxt1.inl"
};

/**
 * Zstd compressed version of \c #rawDxt1.
 * \n
 * See \c testcard.png for the original.
 */
static uint8_t const srcZstd[] = {
#include "testcard-zstd.inl"
};

/**
 * Destination for decoding \c #srcZstd.
 */
static uint8_t dstDxt1[sizeof rawDxt1] = {};

#ifndef ZSTD_VERSION_MAJOR
/**
 * For the case where the decompression library hasn't been included we add a
 * dummy function to fake the process and stop the buffers being optimised out.
 */
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 decompress \c #srcZstd into \c # dstDxt1 then
 * compare the resulting bytes with \c #rawDxt1.
 * \n
 * As a (naive) comparison, removing Zstd and building with "-Os -g0 simple.c"
 * results in a 44kB binary (macOS 10.14, Clang 10); re-adding Zstd increases
 * the binary by 56kB (after calling \c strip).
 */
int main() {
	size_t size = ZSTD_decompress(dstDxt1, sizeof dstDxt1, srcZstd, sizeof srcZstd);
	int compare = memcmp(rawDxt1, dstDxt1, sizeof dstDxt1);
	printf("Decompressed size: %s\n", (size == sizeof dstDxt1) ? "PASSED" : "FAILED");
	printf("Byte comparison: %s\n", (compare == 0) ? "PASSED" : "FAILED");
	if (size == sizeof dstDxt1 && compare == 0) {
		return EXIT_SUCCESS;
	}
	return EXIT_FAILURE;
}