zstd Interfaces
Interface Description
Optimized zstd interfaces.
Interface Format
Compression interface:
- size_t ZSTD_compress2(ZSTD_CCtx* cctx, void* dst, size_t dstCapacity, const void* src, size_t srcSize);
Decompression interface:
- size_t ZSTD_decompress(void* dst, size_t dstCapacity, const void* src, size_t compressedSize);
Parameters
Parameter |
Type |
Description |
Input/Output |
|---|---|---|---|
cctx |
Structure pointer |
Initialization parameter information of the content to be compressed. (The ZSTD_createCCTX() function of the open source zstd is used for initialization. The ZSTD_CCtx_setParameter() function is used to assign values to parameters. For details, see the zstd.h header file.) |
Input |
dst |
Pointer array |
Destination array that stores the compressed content. |
Input/Output |
dstCapacity |
Integer |
Number of bytes of the compressed content. |
Input |
src |
Pointer array |
Source array that stores the content to be compressed. |
Input |
srcSize |
Integer |
Number of bytes of the content to be compressed. |
Input |
ret |
Integer |
If the operation succeeds, the number of bytes after compression is returned. Otherwise, an error code is returned. |
Output |
Parameter |
Type |
Description |
Input/Output |
|---|---|---|---|
dst |
Pointer array |
Destination array that stores the decompressed content. |
Input/Output |
dstCapacity |
Integer |
Upper limit of the number of bytes of the decompressed content. |
Input |
src |
Pointer array |
Source array that stores the content to be decompressed. |
Input |
compressedSize |
Integer |
Number of bytes of the content to be decompressed. |
Input |
ret |
Integer |
If the operation succeeds, the number of bytes of the source content after decompression is returned. Otherwise, an error code is returned. |
Output |
Dependency
zstd.h
Before using the interfaces, compile and deploy the complete libzstd.so dynamic library. For details, see Compiling and Installing the KSAL zstd Algorithm Package.
Example
- Write zstd test code.
- Create a test.c file.
vi test.c
- Press i to enter the insert mode and add the following test code to the file:
#include <stdint.h> #include <stdio.h> #include <assert.h> #include <string.h> #include "/usr/ksal_zstd/zstd.h" #define BLOCK_SIZE (128 * 1024) #define DST_SIZE (1024 * 1024) uint8_t inn[BLOCK_SIZE]={0}; uint8_t outt[DST_SIZE]; uint8_t doutt[DST_SIZE]; int main(int argc, char **argv) { ZSTD_CCtx *cctx = ZSTD_createCCtx(); ZSTD_CCtx_setParameter(cctx, ZSTD_c_compressionLevel, 5); size_t compressed = ZSTD_compress2(cctx, (void *)outt, DST_SIZE, (void *)inn, BLOCK_SIZE); size_t decompressed = ZSTD_decompress(doutt, DST_SIZE, outt, compressed); assert(decompressed == BLOCK_SIZE); assert(memcmp(doutt, inn, BLOCK_SIZE) == 0); ZSTD_freeCCtx(cctx); printf("compression and decompression succ!!\n"); return 0; } - Press Esc to exit the insert mode. Type :wq! and press Enter to save and exit the file.
- Create a test.c file.
- Compile the test.c file into an executable file test.
gcc test.c -L/usr/ksal_zstd -l:libzstd.so -o test
- Run the test executable file.
LD_PRELOAD=/usr/ksal_zstd/libzstd.so ./test
The command output is as follows:compression and decompression succ!!