Skip to content

Commit

Permalink
Merge pull request #3346 from daniellerozenblit/seekable-format-empty…
Browse files Browse the repository at this point in the history
…-string

Seekable format empty string
  • Loading branch information
daniellerozenblit authored Dec 14, 2022
2 parents 588073c + aece0f2 commit 72845eb
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
34 changes: 34 additions & 0 deletions contrib/seekable_format/tests/seekable_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,40 @@ int main(int argc, const char** argv)
}
printf("Success!\n");


printf("Test %u - check ZSTD magic in compressing empty string: ", testNb++);
{ // compressing empty string should return a zstd header
size_t const capacity = 255;
char* inBuffer = malloc(capacity);
assert(inBuffer != NULL);
inBuffer[0] = '\0';
void* const outBuffer = malloc(capacity);
assert(outBuffer != NULL);

ZSTD_seekable_CStream *s = ZSTD_seekable_createCStream();
ZSTD_seekable_initCStream(s, 1, 1, 255);

ZSTD_inBuffer input = { .src=inBuffer, .pos=0, .size=0 };
ZSTD_outBuffer output = { .dst=outBuffer, .pos=0, .size=capacity };

ZSTD_seekable_compressStream(s, &output, &input);
ZSTD_seekable_endStream(s, &output);

if((((char*)output.dst)[0] != '\x28') | (((char*)output.dst)[1] != '\xb5') | (((char*)output.dst)[2] != '\x2f') | (((char*)output.dst)[3] != '\xfd')) {
printf("%#02x %#02x %#02x %#02x\n", ((char*)output.dst)[0], ((char*)output.dst)[1] , ((char*)output.dst)[2] , ((char*)output.dst)[3] );

free(inBuffer);
free(outBuffer);
ZSTD_seekable_freeCStream(s);
goto _test_error;
}

free(inBuffer);
free(outBuffer);
ZSTD_seekable_freeCStream(s);
}
printf("Success!\n");

/* TODO: Add more tests */
printf("Finished tests\n");
return 0;
Expand Down
2 changes: 1 addition & 1 deletion contrib/seekable_format/zstdseek_compress.c
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ size_t ZSTD_seekable_writeSeekTable(ZSTD_frameLog* fl, ZSTD_outBuffer* output)

size_t ZSTD_seekable_endStream(ZSTD_seekable_CStream* zcs, ZSTD_outBuffer* output)
{
if (!zcs->writingSeekTable && zcs->frameDSize) {
if (!zcs->writingSeekTable) {
const size_t endFrame = ZSTD_seekable_endFrame(zcs, output);
if (ZSTD_isError(endFrame)) return endFrame;
/* return an accurate size hint */
Expand Down

0 comments on commit 72845eb

Please sign in to comment.