various minor style fixes

This commit is contained in:
Yann Collet 2021-03-02 16:03:18 -08:00
parent 029f974ddc
commit ac95a30455
3 changed files with 32 additions and 40 deletions

View File

@ -1,6 +1,7 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <assert.h>
#include "zstd_seekable.h" #include "zstd_seekable.h"
@ -35,20 +36,21 @@ int main(int argc, const char** argv)
const size_t uncompressed_size = 32; const size_t uncompressed_size = 32;
uint8_t uncompressed_data[32]; uint8_t uncompressed_data[32];
ZSTD_seekable* stream = ZSTD_seekable_create(); ZSTD_seekable* const stream = ZSTD_seekable_create();
size_t status = ZSTD_seekable_initBuff(stream, compressed_data, compressed_size); assert(stream != NULL);
if (ZSTD_isError(status)) { { size_t const status = ZSTD_seekable_initBuff(stream, compressed_data, compressed_size);
ZSTD_seekable_free(stream); if (ZSTD_isError(status)) {
goto _test_error; ZSTD_seekable_free(stream);
} goto _test_error;
} }
const size_t offset = 2;
/* Should return an error, but not hang */ /* Should return an error, but not hang */
status = ZSTD_seekable_decompress(stream, uncompressed_data, uncompressed_size, offset); { const size_t offset = 2;
if (!ZSTD_isError(status)) { size_t const status = ZSTD_seekable_decompress(stream, uncompressed_data, uncompressed_size, offset);
ZSTD_seekable_free(stream); if (!ZSTD_isError(status)) {
goto _test_error; ZSTD_seekable_free(stream);
} goto _test_error;
} }
ZSTD_seekable_free(stream); ZSTD_seekable_free(stream);
} }

View File

@ -19,6 +19,7 @@
#include "zstd.h" #include "zstd.h"
#include "zstd_errors.h" #include "zstd_errors.h"
#include "mem.h" #include "mem.h"
#include "zstd_seekable.h" #include "zstd_seekable.h"
#define CHECK_Z(f) { size_t const ret = (f); if (ret != 0) return ret; } #define CHECK_Z(f) { size_t const ret = (f); if (ret != 0) return ret; }
@ -75,7 +76,7 @@ size_t ZSTD_seekable_frameLog_allocVec(ZSTD_frameLog* fl)
return 0; 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); if (fl != NULL) free(fl->entries);
return 0; return 0;
@ -83,7 +84,7 @@ size_t ZSTD_seekable_frameLog_freeVec(ZSTD_frameLog* fl)
ZSTD_frameLog* ZSTD_seekable_createFrameLog(int checksumFlag) 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 (fl == NULL) return NULL;
if (ZSTD_isError(ZSTD_seekable_frameLog_allocVec(fl))) { if (ZSTD_isError(ZSTD_seekable_frameLog_allocVec(fl))) {
@ -106,10 +107,9 @@ size_t ZSTD_seekable_freeFrameLog(ZSTD_frameLog* fl)
return 0; 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; if (zcs == NULL) return NULL;
memset(zcs, 0, sizeof(*zcs)); memset(zcs, 0, sizeof(*zcs));
@ -134,7 +134,6 @@ size_t ZSTD_seekable_freeCStream(ZSTD_seekable_CStream* zcs)
ZSTD_freeCStream(zcs->cstream); ZSTD_freeCStream(zcs->cstream);
ZSTD_seekable_frameLog_freeVec(&zcs->framelog); ZSTD_seekable_frameLog_freeVec(&zcs->framelog);
free(zcs); free(zcs);
return 0; return 0;
} }
@ -152,9 +151,8 @@ size_t ZSTD_seekable_initCStream(ZSTD_seekable_CStream* zcs,
return ERROR(frameParameter_unsupported); return ERROR(frameParameter_unsupported);
} }
zcs->maxFrameSize = maxFrameSize zcs->maxFrameSize = maxFrameSize ?
? maxFrameSize maxFrameSize : ZSTD_SEEKABLE_MAX_FRAME_DECOMPRESSED_SIZE;
: ZSTD_SEEKABLE_MAX_FRAME_DECOMPRESSED_SIZE;
zcs->framelog.checksumFlag = checksumFlag; zcs->framelog.checksumFlag = checksumFlag;
if (zcs->framelog.checksumFlag) { if (zcs->framelog.checksumFlag) {
@ -224,8 +222,7 @@ size_t ZSTD_seekable_endFrame(ZSTD_seekable_CStream* zcs, ZSTD_outBuffer* output
zcs->frameDSize = 0; zcs->frameDSize = 0;
ZSTD_CCtx_reset(zcs->cstream, ZSTD_reset_session_only); ZSTD_CCtx_reset(zcs->cstream, ZSTD_reset_session_only);
if (zcs->framelog.checksumFlag) if (zcs->framelog.checksumFlag) XXH64_reset(&zcs->xxhState, 0);
XXH64_reset(&zcs->xxhState, 0);
return 0; return 0;
} }

View File

@ -107,7 +107,8 @@ typedef struct {
static int ZSTD_seekable_read_buff(void* opaque, void* buffer, size_t n) 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; if (buff->pos + n > buff->size) return -1;
memcpy(buffer, (const BYTE*)buff->ptr + buff->pos, n); memcpy(buffer, (const BYTE*)buff->ptr + buff->pos, n);
buff->pos += n; buff->pos += n;
@ -118,6 +119,7 @@ static int ZSTD_seekable_seek_buff(void* opaque, long long offset, int origin)
{ {
buffWrapper_t* const buff = (buffWrapper_t*) opaque; buffWrapper_t* const buff = (buffWrapper_t*) opaque;
unsigned long long newOffset; unsigned long long newOffset;
assert(buff != NULL);
assert(offset >= 0); assert(offset >= 0);
switch (origin) { switch (origin) {
case SEEK_SET: case SEEK_SET:
@ -174,8 +176,7 @@ struct ZSTD_seekable_s {
ZSTD_seekable* ZSTD_seekable_create(void) 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; if (zs == NULL) return NULL;
/* also initializes stage to zsds_init */ /* also initializes stage to zsds_init */
@ -208,7 +209,7 @@ ZSTD_seekTable* ZSTD_seekTable_create_fromSeekable(const ZSTD_seekable* zs)
st->tableLen = zs->seekTable.tableLen; st->tableLen = zs->seekTable.tableLen;
/* Allocate an extra entry at the end to match logic of initial allocation */ /* 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); seekEntry_t* const entries = (seekEntry_t*)malloc(entriesSize);
if (entries==NULL) { if (entries==NULL) {
free(st); free(st);
@ -244,7 +245,7 @@ unsigned ZSTD_seekTable_offsetToFrameIndex(const ZSTD_seekTable* st, unsigned lo
assert(st->tableLen <= UINT_MAX); assert(st->tableLen <= UINT_MAX);
if (pos >= st->entries[st->tableLen].dOffset) { if (pos >= st->entries[st->tableLen].dOffset) {
return (U32)st->tableLen; return (unsigned)st->tableLen;
} }
while (lo + 1 < hi) { while (lo + 1 < hi) {
@ -333,8 +334,7 @@ static size_t ZSTD_seekable_loadSeekTable(ZSTD_seekable* zs)
/* check reserved bits */ /* check reserved bits */
if ((checksumFlag >> 2) & 0x1f) { if ((checksumFlag >> 2) & 0x1f) {
return ERROR(corruption_detected); return ERROR(corruption_detected);
} } }
}
{ U32 const numFrames = MEM_readLE32(zs->inBuff); { U32 const numFrames = MEM_readLE32(zs->inBuff);
U32 const sizePerEntry = 8 + (checksumFlag?4:0); U32 const sizePerEntry = 8 + (checksumFlag?4:0);
@ -342,12 +342,9 @@ static size_t ZSTD_seekable_loadSeekTable(ZSTD_seekable* zs)
U32 const frameSize = tableSize + ZSTD_seekTableFooterSize + ZSTD_SKIPPABLEHEADERSIZE; U32 const frameSize = tableSize + ZSTD_seekTableFooterSize + ZSTD_SKIPPABLEHEADERSIZE;
U32 remaining = frameSize - ZSTD_seekTableFooterSize; /* don't need to re-read footer */ 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.seek(src.opaque, -(S64)frameSize, SEEK_END));
CHECK_IO(src.read(src.opaque, zs->inBuff, toRead)); CHECK_IO(src.read(src.opaque, zs->inBuff, toRead));
remaining -= toRead; remaining -= toRead;
} }
@ -360,19 +357,15 @@ static size_t ZSTD_seekable_loadSeekTable(ZSTD_seekable* zs)
{ /* Allocate an extra entry at the end so that we can do size { /* Allocate an extra entry at the end so that we can do size
* computations on the last element without special case */ * 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 idx = 0;
U32 pos = 8; U32 pos = 8;
U64 cOffset = 0; U64 cOffset = 0;
U64 dOffset = 0; U64 dOffset = 0;
if (!entries) { if (entries == NULL) return ERROR(memory_allocation);
free(entries);
return ERROR(memory_allocation);
}
/* compute cumulative positions */ /* compute cumulative positions */
for (; idx < numFrames; idx++) { for (; idx < numFrames; idx++) {