LibTMJ  1.0.0
A library for loading JSON Tiled maps
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
10 const unsigned int TMJ_VERSION_MAJOR = LIBTMJ_VERSION_MAJOR;
11 const unsigned int TMJ_VERSION_MINOR = LIBTMJ_VERSION_MINOR;
12 const unsigned int TMJ_VERSION_PATCH = LIBTMJ_VERSION_PATCH;
13 const char* const TMJ_VERSION = LIBTMJ_VERSION;
14 
15 uint32_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 (strcmp(compression, "zlib") == 0 || strcmp(compression, "gzip") == 0) {
35 #ifndef LIBTMJ_ZLIB
36  logmsg(TMJ_LOG_ERR, "Layer data encoded with %s, but libtmj was not compiled with %s support", compression, compression);
37 
38  return NULL;
39 #endif
40 // This ifdef is only here to shut up the compiler warnings
41 #ifdef LIBTMJ_ZLIB
42  dat2 = tmj_zlib_decompress(dat, dsize, &dsize2);
43 #endif
44  }
45 
46  if (strcmp(compression, "zstd") == 0) {
47 #ifndef LIBTMJ_ZSTD
48  logmsg(TMJ_LOG_ERR, "Layer data encoded with zstd, but libtmj was not compiled with zstd support");
49 
50  return NULL;
51 #endif
52 #ifdef LIBTMJ_ZSTD
53  dat2 = tmj_zstd_decompress(dat, dsize, &dsize2);
54 #endif
55  }
56 
57  if (dat2 == NULL) {
58  logmsg(TMJ_LOG_ERR, "Unable to decompress %s encoded layer data", compression);
59 
60  free(dat);
61 
62  return NULL;
63  }
64 
65  free(dat);
66 
67  *size = dsize2 / 4;
68 
69  return (uint32_t*)dat2;
70 }
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
uint8_t * tmj_b64_decode(const char *data, size_t *decoded_size)
Decodes a base64 string.
Definition: decode.c:287
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
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:500