Created
March 17, 2024 22:33
-
-
Save embg/9940726094f4cf2cef162cffe9319232 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> // malloc, free, exit | |
#include <stdio.h> // fprintf, perror, fopen, etc. | |
#define ZSTD_STATIC_LINKING_ONLY | |
#include <zstd.h> | |
#include "common.h" | |
int main() { | |
ZSTD_CCtx* cctx = ZSTD_createCCtx(); | |
ZSTD_DCtx* dctx = ZSTD_createDCtx(); | |
const char* src = "arbitrary"; | |
const size_t srcSize = 9; | |
size_t cBuffSize = ZSTD_compressBound(srcSize); | |
void* const cBuff = malloc_orDie(cBuffSize); | |
// Use dict id 16592821 = 0xfd2fb5 = high 3 bytes of ZSTDv07_MAGICNUMBER | |
// zstd --train --dictID=16592821 --maxdict=256 -B100 ~/silesia.tar | |
const char* dictFileName = "dictionary"; | |
void* const dict = malloc_orDie(1000); | |
const size_t dictSize = loadFile_orDie(dictFileName, dict, 1000); | |
const ZSTD_CDict* const cdict = ZSTD_createCDict(dict, dictSize, 3); | |
// Preconditions for corruption using ZSTDv07_MAGICNUMBER. | |
// Try commenting out any of these lines, and decompression will succeed! | |
// * Magicless format enabled | |
// * Checksum enabled | |
// * Reference a dictionary with dictID = 0xfd2fb5 | |
// Note: conditions are different for ZSTDv06_MAGICNUMBER, etc. | |
CHECK_ZSTD(ZSTD_CCtx_setParameter(cctx, ZSTD_c_format, ZSTD_f_zstd1_magicless)); | |
CHECK_ZSTD(ZSTD_CCtx_refCDict(cctx, cdict)); | |
CHECK_ZSTD(ZSTD_CCtx_setParameter(cctx, ZSTD_c_checksumFlag, 1)); | |
CHECK_ZSTD(ZSTD_DCtx_setParameter(dctx, ZSTD_d_format, ZSTD_f_zstd1_magicless)); | |
printf("Trying to compress...\n"); | |
size_t const cSize = ZSTD_compress2(cctx, cBuff, cBuffSize, src, srcSize); | |
CHECK_ZSTD(cSize); | |
printf("Compression succeeded!\n"); | |
// Try hexdump -C compressed.zst, look at the first 4 bytes :) | |
saveFile_orDie("compressed.zst", cBuff, cSize); | |
printf("Trying to decompress...\n"); | |
void* const dBuff = malloc_orDie(srcSize); | |
CHECK_ZSTD(ZSTD_decompress_usingDict(dctx, dBuff, srcSize, cBuff, cSize, dict, dictSize)); | |
printf("Decompression succeeded!\n"); | |
free(cBuff); | |
free(dBuff); | |
free(dict); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment