#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
+#include <assert.h>
#include "zstd_seekable.h"
const size_t uncompressed_size = 32;
uint8_t uncompressed_data[32];
- ZSTD_seekable* stream = ZSTD_seekable_create();
- size_t status = ZSTD_seekable_initBuff(stream, compressed_data, compressed_size);
- if (ZSTD_isError(status)) {
- ZSTD_seekable_free(stream);
- goto _test_error;
- }
+ ZSTD_seekable* const stream = ZSTD_seekable_create();
+ assert(stream != NULL);
+ { size_t const status = ZSTD_seekable_initBuff(stream, compressed_data, compressed_size);
+ if (ZSTD_isError(status)) {
+ ZSTD_seekable_free(stream);
+ goto _test_error;
+ } }
- const size_t offset = 2;
/* Should return an error, but not hang */
- status = ZSTD_seekable_decompress(stream, uncompressed_data, uncompressed_size, offset);
- if (!ZSTD_isError(status)) {
- ZSTD_seekable_free(stream);
- goto _test_error;
- }
+ { const size_t offset = 2;
+ size_t const status = ZSTD_seekable_decompress(stream, uncompressed_data, uncompressed_size, offset);
+ if (!ZSTD_isError(status)) {
+ ZSTD_seekable_free(stream);
+ goto _test_error;
+ } }
ZSTD_seekable_free(stream);
}
#include "zstd.h"
#include "zstd_errors.h"
#include "mem.h"
+
#include "zstd_seekable.h"
#define CHECK_Z(f) { size_t const ret = (f); if (ret != 0) return ret; }
return 0;
}
-size_t ZSTD_seekable_frameLog_freeVec(ZSTD_frameLog* fl)
+static size_t ZSTD_seekable_frameLog_freeVec(ZSTD_frameLog* fl)
{
if (fl != NULL) free(fl->entries);
return 0;
ZSTD_frameLog* ZSTD_seekable_createFrameLog(int checksumFlag)
{
- ZSTD_frameLog* fl = malloc(sizeof(ZSTD_frameLog));
+ ZSTD_frameLog* const fl = malloc(sizeof(ZSTD_frameLog));
if (fl == NULL) return NULL;
if (ZSTD_isError(ZSTD_seekable_frameLog_allocVec(fl))) {
return 0;
}
-ZSTD_seekable_CStream* ZSTD_seekable_createCStream()
+ZSTD_seekable_CStream* ZSTD_seekable_createCStream(void)
{
- ZSTD_seekable_CStream* zcs = malloc(sizeof(ZSTD_seekable_CStream));
-
+ ZSTD_seekable_CStream* const zcs = malloc(sizeof(ZSTD_seekable_CStream));
if (zcs == NULL) return NULL;
memset(zcs, 0, sizeof(*zcs));
ZSTD_freeCStream(zcs->cstream);
ZSTD_seekable_frameLog_freeVec(&zcs->framelog);
free(zcs);
-
return 0;
}
return ERROR(frameParameter_unsupported);
}
- zcs->maxFrameSize = maxFrameSize
- ? maxFrameSize
- : ZSTD_SEEKABLE_MAX_FRAME_DECOMPRESSED_SIZE;
+ zcs->maxFrameSize = maxFrameSize ?
+ maxFrameSize : ZSTD_SEEKABLE_MAX_FRAME_DECOMPRESSED_SIZE;
zcs->framelog.checksumFlag = checksumFlag;
if (zcs->framelog.checksumFlag) {
zcs->frameDSize = 0;
ZSTD_CCtx_reset(zcs->cstream, ZSTD_reset_session_only);
- if (zcs->framelog.checksumFlag)
- XXH64_reset(&zcs->xxhState, 0);
+ if (zcs->framelog.checksumFlag) XXH64_reset(&zcs->xxhState, 0);
return 0;
}
static int ZSTD_seekable_read_buff(void* opaque, void* buffer, size_t n)
{
- buffWrapper_t* buff = (buffWrapper_t*) opaque;
+ buffWrapper_t* const buff = (buffWrapper_t*)opaque;
+ assert(buff != NULL);
if (buff->pos + n > buff->size) return -1;
memcpy(buffer, (const BYTE*)buff->ptr + buff->pos, n);
buff->pos += n;
{
buffWrapper_t* const buff = (buffWrapper_t*) opaque;
unsigned long long newOffset;
+ assert(buff != NULL);
assert(offset >= 0);
switch (origin) {
case SEEK_SET:
ZSTD_seekable* ZSTD_seekable_create(void)
{
- ZSTD_seekable* zs = malloc(sizeof(ZSTD_seekable));
-
+ ZSTD_seekable* const zs = malloc(sizeof(ZSTD_seekable));
if (zs == NULL) return NULL;
/* also initializes stage to zsds_init */
st->tableLen = zs->seekTable.tableLen;
/* Allocate an extra entry at the end to match logic of initial allocation */
- size_t entriesSize = sizeof(seekEntry_t) * (zs->seekTable.tableLen + 1);
+ size_t const entriesSize = sizeof(seekEntry_t) * (zs->seekTable.tableLen + 1);
seekEntry_t* const entries = (seekEntry_t*)malloc(entriesSize);
if (entries==NULL) {
free(st);
assert(st->tableLen <= UINT_MAX);
if (pos >= st->entries[st->tableLen].dOffset) {
- return (U32)st->tableLen;
+ return (unsigned)st->tableLen;
}
while (lo + 1 < hi) {
/* check reserved bits */
if ((checksumFlag >> 2) & 0x1f) {
return ERROR(corruption_detected);
- }
- }
+ } }
{ U32 const numFrames = MEM_readLE32(zs->inBuff);
U32 const sizePerEntry = 8 + (checksumFlag?4:0);
U32 const frameSize = tableSize + ZSTD_seekTableFooterSize + ZSTD_SKIPPABLEHEADERSIZE;
U32 remaining = frameSize - ZSTD_seekTableFooterSize; /* don't need to re-read footer */
- {
- U32 const toRead = MIN(remaining, SEEKABLE_BUFF_SIZE);
-
+ { U32 const toRead = MIN(remaining, SEEKABLE_BUFF_SIZE);
CHECK_IO(src.seek(src.opaque, -(S64)frameSize, SEEK_END));
CHECK_IO(src.read(src.opaque, zs->inBuff, toRead));
-
remaining -= toRead;
}
{ /* Allocate an extra entry at the end so that we can do size
* computations on the last element without special case */
- seekEntry_t* entries = (seekEntry_t*)malloc(sizeof(seekEntry_t) * (numFrames + 1));
+ seekEntry_t* const entries = (seekEntry_t*)malloc(sizeof(seekEntry_t) * (numFrames + 1));
U32 idx = 0;
U32 pos = 8;
-
U64 cOffset = 0;
U64 dOffset = 0;
- if (!entries) {
- free(entries);
- return ERROR(memory_allocation);
- }
+ if (entries == NULL) return ERROR(memory_allocation);
/* compute cumulative positions */
for (; idx < numFrames; idx++) {