C 中使用 esp idf 和 miniz 进行字符串压缩

问题描述 投票:0回答:1

我将不胜感激任何帮助。我想在 esp32 上使用 defalte 算法(解压缩)压缩字符串。

我有这个 main.c,当我尝试“idf build”时,它正确包含“miniz.h”,但找不到头文件中的函数。我在 esp-idf 文档中找不到任何内容。并且不知道缺少什么。我对 esp-idf 框架还很陌生,所以我可能会遗漏一些东西。

#include <stdio.h>
#include <string.h>  // for strlen
#include <assert.h>
#include "miniz.h"

void app_main() {

    // original string len = 36
    char a[50] = "Hello Hello Hello Hello Hello Hello!";
    // placeholder for the compressed (deflated) version of "a" 
    char b[50];
    // placeholder for the UNcompressed (inflated) version of "b"
    char c[50];

    printf("Uncompressed size is: %u\n", strlen(a));
    printf("Uncompressed string is: %s\n", a);

    printf("\n----------\n\n");
    // STEP 1.
    // deflate a into b. (that is, compress a into b)
    // zlib struct
    z_stream defstream;
    defstream.zalloc = Z_NULL;
    defstream.zfree = Z_NULL;
    defstream.opaque = Z_NULL;
    // setup "a" as the input and "b" as the compressed output
    defstream.avail_in = (uInt)strlen(a)+1; // size of input, string + terminator
    defstream.next_in = (Bytef *)a; // input char array
    defstream.avail_out = (uInt)sizeof(b); // size of output
    defstream.next_out = (Bytef *)b; // output char array
    // the actual compression work.
    deflateInit(&defstream, Z_BEST_COMPRESSION);
    deflate(&defstream, Z_FINISH);
    deflateEnd(&defstream);
    // This is one way of getting the size of the output
    printf("Compressed size is: %lu\n", strlen(b));
    printf("Compressed string is: %s\n", b);
    printf("\n----------\n\n");
    // STEP 2.
    // inflate b into c
    // zlib struct
    z_stream infstream;
    infstream.zalloc = Z_NULL;
    infstream.zfree = Z_NULL;
    infstream.opaque = Z_NULL;
    // setup "b" as the input and "c" as the compressed output
    infstream.avail_in = (uInt)((char*)defstream.next_out - b); // size of input
    infstream.next_in = (Bytef *)b; // input char array
    infstream.avail_out = (uInt)sizeof(c); // size of output
    infstream.next_out = (Bytef *)c; // output char array
    // the actual DE-compression work.
    inflateInit(&infstream);
    inflate(&infstream, MZ_NO_FLUSH);
    inflateEnd(&infstream);
    printf("Uncompressed size is: %zu\n", strlen(c));
    printf("Uncompressed string is: %s\n", c);
    // make sure uncompressed is exactly equal to original.
    assert(strcmp(a,c)==0);
}
c compression esp32 esp-idf
1个回答
0
投票

据我所知,您正在尝试使用 zlib 的 API,而不是 miniz。这是 miniz 的 API:https://github.com/espressif/esp-idf/blob/master/components/esp_rom/include/miniz.h

该实现似乎在芯片的 ROM 中。

© www.soinside.com 2019 - 2024. All rights reserved.