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 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#include "nilorea/n_common.h"
29#include "nilorea/n_log.h"
30#include "nilorea/n_str.h"
31#include "nilorea/n_zlib.h"
32
33int main(void) {
35
36 /* test raw compress / uncompress with GetMaxCompressedLen, CompressData, UncompressData */
37 const char* src_text =
38 "Hello World! This is a test of the zlib compression wrappers in nilorea. "
39 "Repeated data compresses well: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
40 size_t src_len = strlen(src_text) + 1;
41
42 size_t max_dst = GetMaxCompressedLen(src_len);
43 n_log(LOG_INFO, "Source length: %zu, max compressed length: %zu", src_len, max_dst);
44
45 unsigned char* compressed = NULL;
46 Malloc(compressed, unsigned char, max_dst);
47 __n_assert(compressed, return 1);
48
49 size_t compressed_len = CompressData((unsigned char*)src_text, src_len, compressed, max_dst);
50 if (compressed_len == 0) {
51 n_log(LOG_ERR, "CompressData failed");
52 Free(compressed);
53 return 1;
54 }
55 n_log(LOG_INFO, "Compressed %zu bytes to %zu bytes", src_len, compressed_len);
56
57 unsigned char* decompressed = NULL;
58 Malloc(decompressed, unsigned char, src_len);
59 __n_assert(decompressed, Free(compressed); return 1);
60
61 size_t decompressed_len = UncompressData(compressed, compressed_len, decompressed, src_len);
62 if (decompressed_len == 0) {
63 n_log(LOG_ERR, "UncompressData failed");
64 } else {
65 n_log(LOG_INFO, "Decompressed %zu bytes back to %zu bytes", compressed_len, decompressed_len);
66 if (strcmp((char*)decompressed, src_text) == 0) {
67 n_log(LOG_NOTICE, "Raw compress/decompress: data matches");
68 } else {
69 n_log(LOG_ERR, "Raw compress/decompress: data mismatch");
70 }
71 }
72 Free(compressed);
73 Free(decompressed);
74
75 /* test N_STR wrappers: zip_nstr, unzip_nstr */
76 N_STR* original = char_to_nstr(
77 "Nilorea Library zlib N_STR test. "
78 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB "
79 "The quick brown fox jumps over the lazy dog.");
80 n_log(LOG_INFO, "Original N_STR: %zu written, %zu length", original->written, original->length);
81
82 N_STR* zipped = zip_nstr(original);
83 if (!zipped) {
84 n_log(LOG_ERR, "zip_nstr failed");
85 free_nstr(&original);
86 return 1;
87 }
88 n_log(LOG_INFO, "Zipped N_STR: %zu written, %zu length", zipped->written, zipped->length);
89
90 N_STR* unzipped = unzip_nstr(zipped);
91 if (!unzipped) {
92 n_log(LOG_ERR, "unzip_nstr failed");
93 } else {
94 n_log(LOG_INFO, "Unzipped N_STR: %zu written, %zu length", unzipped->written, unzipped->length);
95 if (strcmp(original->data, unzipped->data) == 0) {
96 n_log(LOG_NOTICE, "N_STR zip/unzip: data matches");
97 } else {
98 n_log(LOG_ERR, "N_STR zip/unzip: data mismatch");
99 }
100 free_nstr(&unzipped);
101 }
102 free_nstr(&zipped);
103 free_nstr(&original);
104
105 n_log(LOG_INFO, "zlib example done");
106 exit(0);
107}
int main(void)
#define Malloc(__ptr, __struct, __size)
Malloc Handler to get errors and set to 0.
Definition n_common.h:204
#define __n_assert(__ptr, __ret)
macro to assert things
Definition n_common.h:279
#define Free(__ptr)
Free Handler to get errors.
Definition n_common.h:263
#define n_log(__LEVEL__,...)
Logging function wrapper to get line and func.
Definition n_log.h:89
#define LOG_DEBUG
debug-level messages
Definition n_log.h:84
#define LOG_ERR
error conditions
Definition n_log.h:76
void set_log_level(const int log_level)
Set the global log level value ( static int LOG_LEVEL )
Definition n_log.c:121
#define LOG_NOTICE
normal but significant condition
Definition n_log.h:80
#define LOG_INFO
informational
Definition n_log.h:82
size_t written
number of meaningful bytes in data, excluding the null terminator; the size including the null termin...
Definition n_str.h:68
char * data
the string
Definition n_str.h:63
size_t length
total allocation (in bytes) of the data buffer, padding included
Definition n_str.h:65
#define free_nstr(__ptr)
free a N_STR structure and set the pointer to NULL
Definition n_str.h:203
N_STR * char_to_nstr(const char *src)
Convert a char into a N_STR, short version.
Definition n_str.c:255
A box including a string and his lenght.
Definition n_str.h:61
size_t GetMaxCompressedLen(size_t nLenSrc)
Return the maximum compressed size.
Definition n_zlib.c:42
size_t CompressData(unsigned char *abSrc, size_t nLenSrc, unsigned char *abDst, size_t nLenDst)
Compress a string to another.
Definition n_zlib.c:55
size_t UncompressData(unsigned char *abSrc, size_t nLenSrc, unsigned char *abDst, size_t nLenDst)
Uncompress a string to another.
Definition n_zlib.c:112
N_STR * unzip_nstr(N_STR *src)
return an uncompressed version of src
Definition n_zlib.c:217
N_STR * zip_nstr(N_STR *src)
return a compressed version of src
Definition n_zlib.c:166
Common headers and low-level functions & define.
Generic log system.
N_STR and string function declaration.
ZLIB compression handler.