Skip to content

Instantly share code, notes, and snippets.

@jkoenen
Created October 13, 2016 11:32
Show Gist options
  • Select an option

  • Save jkoenen/e2426ab340f01b24cf229b56d3100f69 to your computer and use it in GitHub Desktop.

Select an option

Save jkoenen/e2426ab340f01b24cf229b56d3100f69 to your computer and use it in GitHub Desktop.
#include "zstd.h"
#include "zdict.h"
#include <stdio.h>
#include <stdlib.h>
int main( int argc, char* argv[] )
{
const size_t sourceDataSize = 100000u;
const size_t maxDictionarySize = 100000u;
const int compressionLevel = 6;
const size_t blockSize = 65536u;
const size_t blockCount = ( sourceDataSize + blockSize - 1u ) / blockSize;
char* pSourceData = (char*)calloc( sourceDataSize, sizeof( char ) );
char* pTargetBuffer = (char*)calloc( sourceDataSize, sizeof( char ) );
char* pDictionary = (char*)calloc( maxDictionarySize, sizeof( char ) );
char* pCompressBuffer = (char*)calloc( blockSize, sizeof( char ) );
size_t* pBlockSizes = (size_t*)calloc( blockCount, sizeof( size_t ) );
for( size_t i = 0u; i < sourceDataSize; ++i )
{
pSourceData[ i ] = rand() % 20;
}
for( size_t i = 0u; i < blockCount; ++i )
{
pBlockSizes[ i ] = sourceDataSize - i * blockSize;
if( pBlockSizes[ i ] > blockSize )
{
pBlockSizes[ i ] = blockSize;
}
}
const size_t dictionarySize = ZDICT_trainFromBuffer( pDictionary, maxDictionarySize, pSourceData, pBlockSizes, (unsigned int)blockCount );
if( ZDICT_isError( dictionarySize ) )
{
printf( "Error: '%s'\n", ZDICT_getErrorName( dictionarySize ) );
return 1;
}
ZSTD_CCtx* pCCtx = ZSTD_createCCtx();
ZSTD_CDict* pCDict = ZSTD_createCDict( pDictionary, dictionarySize, compressionLevel );
ZSTD_DCtx* pDCtx = ZSTD_createDCtx();
ZSTD_DDict* pDDict = ZSTD_createDDict( pDictionary, dictionarySize );
size_t compressedSize = 0u;
size_t decompressedSize = 0u;
for( size_t i = 0u; i < blockCount; ++i )
{
const size_t cSize = ZSTD_compress_usingCDict( pCCtx, pCompressBuffer, blockSize, pSourceData + i * blockSize, pBlockSizes[ i ], pCDict );
compressedSize += cSize;
if( ZSTD_isError( cSize ) )
{
printf( "Error: '%s'\n", ZSTD_getErrorName( cSize ) );
return 2;
}
const size_t dSize = ZSTD_decompress_usingDDict( pDCtx, pTargetBuffer + i * blockSize, pBlockSizes[ i ], pCompressBuffer, cSize, pDDict );
decompressedSize += dSize;
if( ZSTD_isError( dSize ) )
{
printf( "Error: '%s'\n", ZSTD_getErrorName( dSize ) );
return 3;
}
}
if( decompressedSize != sourceDataSize )
{
printf( "Error: 'wrong decompressed size: %u'\n", (unsigned int)decompressedSize );
return 4;
}
ZSTD_freeDCtx( pDCtx );
ZSTD_freeDDict( pDDict );
ZSTD_freeCCtx( pCCtx );
ZSTD_freeCDict( pCDict );
free( pBlockSizes );
free( pCompressBuffer );
free( pDictionary );
free( pTargetBuffer );
free( pSourceData );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment