Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
ex_zlib.c

Nilorea Library zlib compression api test.

Nilorea Library zlib compression api test

Author
Castagnier Mickael
Version
1.0
Date
27/06/2017
/*
* Nilorea Library
* Copyright (C) 2005-2026 Castagnier Mickael
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "nilorea/n_log.h"
#include "nilorea/n_str.h"
#include "nilorea/n_zlib.h"
int main(void) {
/* test raw compress / uncompress with GetMaxCompressedLen, CompressData, UncompressData */
const char* src_text =
"Hello World! This is a test of the zlib compression wrappers in nilorea. "
"Repeated data compresses well: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
size_t src_len = strlen(src_text) + 1;
size_t max_dst = GetMaxCompressedLen(src_len);
n_log(LOG_INFO, "Source length: %zu, max compressed length: %zu", src_len, max_dst);
unsigned char* compressed = NULL;
Malloc(compressed, unsigned char, max_dst);
__n_assert(compressed, return 1);
size_t compressed_len = CompressData((unsigned char*)src_text, src_len, compressed, max_dst);
if (compressed_len == 0) {
n_log(LOG_ERR, "CompressData failed");
Free(compressed);
return 1;
}
n_log(LOG_INFO, "Compressed %zu bytes to %zu bytes", src_len, compressed_len);
unsigned char* decompressed = NULL;
Malloc(decompressed, unsigned char, src_len);
__n_assert(decompressed, Free(compressed); return 1);
size_t decompressed_len = UncompressData(compressed, compressed_len, decompressed, src_len);
if (decompressed_len == 0) {
n_log(LOG_ERR, "UncompressData failed");
} else {
n_log(LOG_INFO, "Decompressed %zu bytes back to %zu bytes", compressed_len, decompressed_len);
if (strcmp((char*)decompressed, src_text) == 0) {
n_log(LOG_NOTICE, "Raw compress/decompress: data matches");
} else {
n_log(LOG_ERR, "Raw compress/decompress: data mismatch");
}
}
Free(compressed);
Free(decompressed);
/* test N_STR wrappers: zip_nstr, unzip_nstr */
N_STR* original = char_to_nstr(
"Nilorea Library zlib N_STR test. "
"BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB "
"The quick brown fox jumps over the lazy dog.");
n_log(LOG_INFO, "Original N_STR: %zu written, %zu length", original->written, original->length);
N_STR* zipped = zip_nstr(original);
if (!zipped) {
n_log(LOG_ERR, "zip_nstr failed");
free_nstr(&original);
return 1;
}
n_log(LOG_INFO, "Zipped N_STR: %zu written, %zu length", zipped->written, zipped->length);
N_STR* unzipped = unzip_nstr(zipped);
if (!unzipped) {
n_log(LOG_ERR, "unzip_nstr failed");
} else {
n_log(LOG_INFO, "Unzipped N_STR: %zu written, %zu length", unzipped->written, unzipped->length);
if (strcmp(original->data, unzipped->data) == 0) {
n_log(LOG_NOTICE, "N_STR zip/unzip: data matches");
} else {
n_log(LOG_ERR, "N_STR zip/unzip: data mismatch");
}
free_nstr(&unzipped);
}
free_nstr(&zipped);
free_nstr(&original);
n_log(LOG_INFO, "zlib example done");
exit(0);
}
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.