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
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#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: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.