LibTMJ 1.4.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 memcpy(dat2, dat, dsize);
37 dsize2 = dsize;
38 }
39
40 if (strcmp(compression, "zlib") == 0 || strcmp(compression, "gzip") == 0) {
41#ifndef LIBTMJ_ZLIB
42 logmsg(TMJ_LOG_ERR, "Layer data encoded with %s, but libtmj was not compiled with %s support", compression, compression);
43
44 return NULL;
45#endif
46// This ifdef is only here to shut up the compiler warnings
47#ifdef LIBTMJ_ZLIB
48 dat2 = tmj_zlib_decompress(dat, dsize, &dsize2);
49#endif
50 }
51
52 if (strcmp(compression, "zstd") == 0) {
53#ifndef LIBTMJ_ZSTD
54 logmsg(TMJ_LOG_ERR, "Layer data encoded with zstd, but libtmj was not compiled with zstd support");
55
56 return NULL;
57#endif
58#ifdef LIBTMJ_ZSTD
59 dat2 = tmj_zstd_decompress(dat, dsize, &dsize2);
60#endif
61 }
62
63 if (dat2 == NULL) {
64 logmsg(TMJ_LOG_ERR, "Unable to decompress %s encoded layer data", compression);
65
66 free(dat);
67
68 return NULL;
69 }
70
71 free(dat);
72
73 *size = dsize2 / 4;
74
75 return (uint32_t*)dat2;
76}
uint8_t * tmj_b64_decode(const char *data, size_t *decoded_size)
Decodes a base64 string.
Definition decode.c:410
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