Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_zlib.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
27#include "nilorea/n_common.h"
28#include "nilorea/n_log.h"
29#include "nilorea/n_str.h"
30#include "nilorea/n_zlib.h"
31
32int main(void) {
34
35 /* test raw compress / uncompress with GetMaxCompressedLen, CompressData, UncompressData */
36 const char* src_text =
37 "Hello World! This is a test of the zlib compression wrappers in nilorea. "
38 "Repeated data compresses well: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
39 size_t src_len = strlen(src_text) + 1;
40
41 size_t max_dst = GetMaxCompressedLen(src_len);
42 n_log(LOG_INFO, "Source length: %zu, max compressed length: %zu", src_len, max_dst);
43
44 unsigned char* compressed = NULL;
45 Malloc(compressed, unsigned char, max_dst);
46 __n_assert(compressed, return 1);
47
48 size_t compressed_len = CompressData((unsigned char*)src_text, src_len, compressed, max_dst);
49 if (compressed_len == 0) {
50 n_log(LOG_ERR, "CompressData failed");
51 Free(compressed);
52 return 1;
53 }
54 n_log(LOG_INFO, "Compressed %zu bytes to %zu bytes", src_len, compressed_len);
55
56 unsigned char* decompressed = NULL;
57 Malloc(decompressed, unsigned char, src_len);
58 __n_assert(decompressed, Free(compressed); return 1);
59
60 size_t decompressed_len = UncompressData(compressed, compressed_len, decompressed, src_len);
61 if (decompressed_len == 0) {
62 n_log(LOG_ERR, "UncompressData failed");
63 } else {
64 n_log(LOG_INFO, "Decompressed %zu bytes back to %zu bytes", compressed_len, decompressed_len);
65 if (strcmp((char*)decompressed, src_text) == 0) {
66 n_log(LOG_NOTICE, "Raw compress/decompress: data matches");
67 } else {
68 n_log(LOG_ERR, "Raw compress/decompress: data mismatch");
69 }
70 }
71 Free(compressed);
72 Free(decompressed);
73
74 /* test N_STR wrappers: zip_nstr, unzip_nstr */
75 N_STR* original = char_to_nstr(
76 "Nilorea Library zlib N_STR test. "
77 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB "
78 "The quick brown fox jumps over the lazy dog.");
79 n_log(LOG_INFO, "Original N_STR: %zu written, %zu length", original->written, original->length);
80
81 N_STR* zipped = zip_nstr(original);
82 if (!zipped) {
83 n_log(LOG_ERR, "zip_nstr failed");
84 free_nstr(&original);
85 return 1;
86 }
87 n_log(LOG_INFO, "Zipped N_STR: %zu written, %zu length", zipped->written, zipped->length);
88
89 N_STR* unzipped = unzip_nstr(zipped);
90 if (!unzipped) {
91 n_log(LOG_ERR, "unzip_nstr failed");
92 } else {
93 n_log(LOG_INFO, "Unzipped N_STR: %zu written, %zu length", unzipped->written, unzipped->length);
94 if (strcmp(original->data, unzipped->data) == 0) {
95 n_log(LOG_NOTICE, "N_STR zip/unzip: data matches");
96 } else {
97 n_log(LOG_ERR, "N_STR zip/unzip: data mismatch");
98 }
99 free_nstr(&unzipped);
100 }
101 free_nstr(&zipped);
102 free_nstr(&original);
103
104 n_log(LOG_INFO, "zlib example done");
105 exit(0);
106}
int main(void)
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:203
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:278
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:262
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:88
#define LOG_DEBUG
debug-level messages
Definition n_log.h:83
#define LOG_ERR
error conditions
Definition n_log.h:75
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:120
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:79
#define LOG_INFO
informational
Definition n_log.h:81
size_t written
size of the written data inside the string
Definition n_str.h:66
char * data
the string
Definition n_str.h:62
size_t length
length of string (in case we wanna keep information after the 0 end of string value)
Definition n_str.h:64
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:201
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:254
A box including a string and his lenght.
Definition n_str.h:60
size_t GetMaxCompressedLen(size_t nLenSrc)
Return the maximum compressed size.
Definition n_zlib.c:41
size_t CompressData(unsigned char *abSrc, size_t nLenSrc, unsigned char *abDst, size_t nLenDst)
Compress a string to another.
Definition n_zlib.c:54
size_t UncompressData(unsigned char *abSrc, size_t nLenSrc, unsigned char *abDst, size_t nLenDst)
Uncompress a string to another.
Definition n_zlib.c:111
N_STR * unzip_nstr(N_STR *src)
return an uncompressed version of src
Definition n_zlib.c:216
N_STR * zip_nstr(N_STR *src)
return a compressed version of src
Definition n_zlib.c:165
Common headers and low-level functions & define.
Generic log system.
N_STR and string function declaration.
ZLIB compression handler.