LibTMJ 1.6.0
A library for loading JSON Tiled maps
Loading...
Searching...
No Matches
util.c
Go to the documentation of this file.
1#include <stddef.h>
2#include <stdint.h>
3#include <stdlib.h>
4#include <string.h>
5
6#include "decode.h"
7#include "log.h"
8
9// Library version definition
10const unsigned int TMJ_VERSION_MAJOR = LIBTMJ_VERSION_MAJOR;
11const unsigned int TMJ_VERSION_MINOR = LIBTMJ_VERSION_MINOR;
12const unsigned int TMJ_VERSION_PATCH = LIBTMJ_VERSION_PATCH;
13const char* const TMJ_VERSION = LIBTMJ_VERSION;
14
15uint32_t* tmj_decode_layer(const char* data, const char* encoding, const char* compression, size_t* size) {
16 if (strcmp(encoding, "base64") != 0) {
17 logmsg(TMJ_LOG_ERR, "Layer data in csv format; decode it yourself");
18
19 return NULL;
20 }
21
22 size_t dsize = 0;
23 uint8_t* dat = tmj_b64_decode(data, &dsize);
24
25 if (dat == NULL) {
26 logmsg(TMJ_LOG_ERR, "Unable to base64 decode layer data");
27
28 return NULL;
29 }
30
31 size_t dsize2 = 0;
32 uint8_t* dat2 = NULL;
33
34 if (strlen(compression) == 0) {
35 dat2 = malloc(dsize);
36
37 if (dat2 == NULL) {
38 logmsg(TMJ_LOG_ERR, "Could not allocate buffer for decoded layer data, the system is out of memory");
39
40 goto fail;
41 }
42
43 memcpy(dat2, dat, dsize);
44 dsize2 = dsize;
45 }
46
47 if (strcmp(compression, "zlib") == 0 || strcmp(compression, "gzip") == 0) {
48#ifndef LIBTMJ_ZLIB
49 logmsg(TMJ_LOG_ERR, "Layer data encoded with %s, but libtmj was not compiled with %s support", compression, compression);
50
51 goto fail;
52#endif
53// This ifdef is only here to shut up the compiler warnings
54#ifdef LIBTMJ_ZLIB
55 dat2 = tmj_zlib_decompress(dat, dsize, &dsize2);
56#endif
57 }
58
59 if (strcmp(compression, "zstd") == 0) {
60#ifndef LIBTMJ_ZSTD
61 logmsg(TMJ_LOG_ERR, "Layer data encoded with zstd, but libtmj was not compiled with zstd support");
62
63 goto fail;
64#endif
65#ifdef LIBTMJ_ZSTD
66 dat2 = tmj_zstd_decompress(dat, dsize, &dsize2);
67#endif
68 }
69
70 if (dat2 == NULL) {
71 logmsg(TMJ_LOG_ERR, "Unable to decompress %s encoded layer data", compression);
72
73 goto fail;
74 }
75
76 free(dat);
77
78 *size = dsize2 / 4;
79
80 return (uint32_t*)dat2;
81
82fail:
83 free(dat);
84 return NULL;
85}
uint8_t * tmj_b64_decode(const char *data, size_t *decoded_size)
Decodes a base64 string.
Definition decode.c:421
uint8_t * tmj_zstd_decompress(const uint8_t *data, size_t data_size, size_t *decompressed_size)
Decompresses a zstd-compressed buffer of bytes.
Definition decode.c:10
uint8_t * tmj_zlib_decompress(const uint8_t *data, size_t data_size, size_t *decompressed_size)
Decompresses a zlib/gzip-compressed buffer of bytes.
Definition decode.c:62
void logmsg(tmj_log_priority priority, char *msg,...)
Processes log messages and passes them to the active logging callback, if there is one.
Definition log.c:23
uint32_t * tmj_decode_layer(const char *data, const char *encoding, const char *compression, size_t *size)
Decodes layer data from a Tiled map layer.
Definition util.c:15
const char *const TMJ_VERSION
The full library version.
Definition util.c:13
const unsigned int TMJ_VERSION_MINOR
The library minor version.
Definition util.c:11
const unsigned int TMJ_VERSION_PATCH
The library patch version.
Definition util.c:12
const unsigned int TMJ_VERSION_MAJOR
‍**
Definition util.c:10
@ TMJ_LOG_ERR
Definition tmj.h:502