forked from NeuroJSON/zmat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testzmat.c
111 lines (84 loc) · 3.19 KB
/
testzmat.c
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/***************************************************************************//**
** \mainpage ZMat - A portable C-library and MATLAB/Octave toolbox for inline data compression
**
** \author Qianqian Fang <q.fang at neu.edu>
** \copyright Qianqian Fang, 2019,2020,2022
**
** Demo on how to use zmatlib for simple compression and decompression in C
**
** \section slicense License
** GPL v3, see LICENSE.txt for details
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* to compile, one should add -I/path/to/zmatlib.h to the compilation command and
* -L/path/to/libzmat.so(or .a) -lzmat -lz -lpthread to the linking command
*/
#include "zmatlib.h"
int main(void) {
char* test[] = {"__o000o__(o)(o)__o000o__ =^_^= __o000o__(o)(o)__o000o__"};
int ret = 0, status = 0;
size_t compressedlen, encodedlen, decodelen, decompressedlen;
/*output buffers will be allocated inside zmat functions, host is responsible to free after use*/
unsigned char* compressed = NULL, *encoded = NULL, *decoded = NULL, *decompressed = NULL;
/*=====================================*/
/* compressing and encoding the string */
/*=====================================*/
/*first, perform zlib compression use the highest compression (-9); one can use zmat_encode as well*/
ret = zmat_run(strlen(test[0]), (unsigned char*)test[0], &compressedlen, &compressed, zmZlib, &status, -9);
if (ret == 0) {
/*next, encode the compressed data using base64*/
ret = zmat_encode(compressedlen, compressed, &encodedlen, &encoded, zmBase64, &status);
if (ret == 0) {
printf("{\n\t\"original\":\"%s\",\n", test[0]);
printf("\t\"encoded\":\"%s\",\n", encoded);
}
}
/* error handling */
if (ret) {
printf("encoding failed, error code: %d: encoder error code %d\n", ret, status);
if (compressed) {
free(compressed);
}
return ret;
}
/*==========================================================*/
/* decode and then decompress to restore the orginal string */
/*==========================================================*/
/*first, perform base64 decoding*/
ret = zmat_decode(encodedlen, encoded, &decodelen, &decoded, zmBase64, &status);
if (ret == 0) {
/*next, decompress using zlib (deflate) */
ret = zmat_decode(decodelen, decoded, &decompressedlen, &decompressed, zmZlib, &status);
if (ret == 0) {
printf("\t\"decompressed\":\"%s\",\n", decompressed);
}
}
printf("}\n");
/* error handling */
if (ret) {
printf("decoding failed, error code: %d: decoder error code %d\n", ret, status);
if (decoded) {
free(decoded);
}
return ret;
}
/*==================================*/
/* host must free buffers after use */
/*==================================*/
if (compressed) {
free(compressed);
}
if (encoded) {
free(encoded);
}
if (decoded) {
free(decoded);
}
if (decompressed) {
free(decompressed);
}
return 0;
}