6 ex__zlib_8c
gullradriel edited this page 2026-10-09 00:55:18 +02:00

examples/ex_zlib.c

Functions

Name
int main(void )

Functions Documentation

function main

int main(
    void 
)

Source code

/*
 * 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_common.h"
#include "nilorea/n_log.h"
#include "nilorea/n_str.h"
#include "nilorea/n_zlib.h"

int main(void) {
    set_log_level(LOG_DEBUG);

    /* 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);
}