Skip to content

Commit

Permalink
Add zstd as compression algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
kripton committed Jun 8, 2021
1 parent ffdb902 commit 920ebc7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/zstd
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ pico_sdk_init()
## Include the CMakeLists-files of (non-pico-sdk)-libraries we will be using
include(../lib/heatshrink/CMakeLists.txt)
include(../lib/zlib/interfaceLibForPicoSDK.cmake)
include(../lib/zstd/interfaceLibForPicoSDK.cmake)

## Add our own C/C++ files here
## Sorted alphabetically
add_executable(${CMAKE_PROJECT_NAME}
${TINYUSB_LIBNETWORKING_SOURCES}
algo-heatshrink.c
algo-zlib.c
algo-zstd.c
main.c
)

Expand Down Expand Up @@ -48,6 +50,7 @@ target_link_libraries(${CMAKE_PROJECT_NAME}
heatshrink
pico_stdlib
zlib
zstd
)

## enable usb output, disable uart output
Expand Down
22 changes: 22 additions & 0 deletions src/algo-zstd.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "algo-zstd.h"

#include "zstd.h"

static struct ZSTD_CCtx_s* cctx;
static struct ZSTD_DCtx_s* dctx;

void zstd_init()
{
cctx = ZSTD_createCCtx();
dctx = ZSTD_createDCtx();
}

void zstd_compress(uint8_t *input, size_t insize, uint8_t *output, size_t *outsize)
{
*outsize = ZSTD_compressCCtx(cctx, output, *outsize, input, insize, 1);
}

void zstd_uncompress(uint8_t *input, size_t insize, uint8_t *output, size_t *outsize)
{
*outsize = ZSTD_decompressDCtx(dctx, output, *outsize, input, insize);
}
10 changes: 10 additions & 0 deletions src/algo-zstd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __ALGO_ZSTD_H__
#define __ALGO_ZSTD_H__

#include "pico/stdlib.h"

void zstd_init();
void zstd_compress(uint8_t* input, size_t insize, uint8_t* output, size_t* outsize);
void zstd_uncompress(uint8_t* input, size_t insize, uint8_t* output, size_t* outsize);

#endif // __ALGO_ZSTD_H__
11 changes: 10 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "algo-heatshrink.h"
#include "algo-zlib.h"
#include "algo-zstd.h"


// TEST VECTORS
Expand Down Expand Up @@ -52,6 +53,7 @@ struct algo {
enum {
ALGO_HEATSHRINK,
ALGO_ZLIB,
ALGO_ZSTD,
// Add more HERE
ALGO_NUM_TOTAL
};
Expand Down Expand Up @@ -182,13 +184,20 @@ int main() {
algos[ALGO_HEATSHRINK].uncompress = heatshrink_uncompress;
algos[ALGO_HEATSHRINK].init();

// Algo 0: zlib
// Algo 1: zlib
sprintf(algos[ALGO_ZLIB].name, "ZLIB");
algos[ALGO_ZLIB].init = zlib_init;
algos[ALGO_ZLIB].compress = zlib_compress;
algos[ALGO_ZLIB].uncompress = zlib_uncompress;
algos[ALGO_ZLIB].init();

// Algo 2: zstd
sprintf(algos[ALGO_ZSTD].name, "ZSTD");
algos[ALGO_ZSTD].init = zstd_init;
algos[ALGO_ZSTD].compress = zstd_compress;
algos[ALGO_ZSTD].uncompress = zstd_uncompress;
algos[ALGO_ZSTD].init();

finish_time = time_us_32();
elapsed_time = finish_time - start_time;
printf("%luµs\n", elapsed_time);
Expand Down

0 comments on commit 920ebc7

Please sign in to comment.