2017-08-31 19:20:50 +00:00
|
|
|
/*
|
2017-04-10 23:22:35 +00:00
|
|
|
* Copyright (c) 2017-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
2017-08-31 19:20:50 +00:00
|
|
|
* This source code is licensed under both the BSD-style license (found in the
|
|
|
|
* LICENSE file in the root directory of this source tree) and the GPLv2 (found
|
|
|
|
* in the COPYING file in the root directory of this source tree).
|
2017-04-10 23:22:35 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h> /* malloc, free */
|
2018-12-13 20:14:36 +00:00
|
|
|
#include <limits.h> /* UINT_MAX */
|
|
|
|
#include <assert.h>
|
2017-04-10 23:22:35 +00:00
|
|
|
|
|
|
|
#define XXH_STATIC_LINKING_ONLY
|
2017-04-11 21:38:56 +00:00
|
|
|
#define XXH_NAMESPACE ZSTD_
|
2017-04-10 23:22:35 +00:00
|
|
|
#include "xxhash.h"
|
|
|
|
|
2017-04-14 00:47:55 +00:00
|
|
|
#define ZSTD_STATIC_LINKING_ONLY
|
|
|
|
#include "zstd.h"
|
|
|
|
#include "zstd_errors.h"
|
|
|
|
#include "mem.h"
|
2017-04-11 21:38:56 +00:00
|
|
|
#include "zstd_seekable.h"
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-14 00:47:55 +00:00
|
|
|
#define CHECK_Z(f) { size_t const ret = (f); if (ret != 0) return ret; }
|
|
|
|
|
|
|
|
#undef ERROR
|
|
|
|
#define ERROR(name) ((size_t)-ZSTD_error_##name)
|
|
|
|
|
|
|
|
#undef MIN
|
|
|
|
#undef MAX
|
|
|
|
#define MIN(a, b) ((a) < (b) ? (a) : (b))
|
|
|
|
#define MAX(a, b) ((a) > (b) ? (a) : (b))
|
|
|
|
|
2017-04-10 23:22:35 +00:00
|
|
|
typedef struct {
|
|
|
|
U32 cSize;
|
|
|
|
U32 dSize;
|
|
|
|
U32 checksum;
|
2017-04-12 18:06:00 +00:00
|
|
|
} framelogEntry_t;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
struct ZSTD_frameLog_s {
|
2017-04-12 18:06:00 +00:00
|
|
|
framelogEntry_t* entries;
|
2017-04-10 23:22:35 +00:00
|
|
|
U32 size;
|
|
|
|
U32 capacity;
|
2017-04-28 19:17:09 +00:00
|
|
|
|
|
|
|
int checksumFlag;
|
|
|
|
|
|
|
|
/* for use when streaming out the seek table */
|
|
|
|
U32 seekTablePos;
|
|
|
|
U32 seekTableIndex;
|
2017-04-12 18:06:00 +00:00
|
|
|
} framelog_t;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
|
|
|
struct ZSTD_seekable_CStream_s {
|
|
|
|
ZSTD_CStream* cstream;
|
2017-04-28 19:17:09 +00:00
|
|
|
ZSTD_frameLog framelog;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
U32 frameCSize;
|
|
|
|
U32 frameDSize;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
|
|
|
XXH64_state_t xxhState;
|
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
U32 maxFrameSize;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-11 18:40:34 +00:00
|
|
|
int writingSeekTable;
|
2017-04-10 23:22:35 +00:00
|
|
|
};
|
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
size_t ZSTD_seekable_frameLog_allocVec(ZSTD_frameLog* fl)
|
|
|
|
{
|
|
|
|
/* allocate some initial space */
|
|
|
|
size_t const FRAMELOG_STARTING_CAPACITY = 16;
|
|
|
|
fl->entries = (framelogEntry_t*)malloc(
|
|
|
|
sizeof(framelogEntry_t) * FRAMELOG_STARTING_CAPACITY);
|
|
|
|
if (fl->entries == NULL) return ERROR(memory_allocation);
|
|
|
|
fl->capacity = FRAMELOG_STARTING_CAPACITY;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
if (fl == NULL) return NULL;
|
|
|
|
|
|
|
|
if (ZSTD_isError(ZSTD_seekable_frameLog_allocVec(fl))) {
|
|
|
|
free(fl);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fl->checksumFlag = checksumFlag;
|
|
|
|
fl->seekTablePos = 0;
|
|
|
|
fl->seekTableIndex = 0;
|
|
|
|
fl->size = 0;
|
|
|
|
|
|
|
|
return fl;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ZSTD_seekable_freeFrameLog(ZSTD_frameLog* fl)
|
|
|
|
{
|
|
|
|
ZSTD_seekable_frameLog_freeVec(fl);
|
|
|
|
free(fl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-10 23:22:35 +00:00
|
|
|
ZSTD_seekable_CStream* ZSTD_seekable_createCStream()
|
|
|
|
{
|
|
|
|
ZSTD_seekable_CStream* zcs = malloc(sizeof(ZSTD_seekable_CStream));
|
|
|
|
|
|
|
|
if (zcs == NULL) return NULL;
|
|
|
|
|
|
|
|
memset(zcs, 0, sizeof(*zcs));
|
|
|
|
|
|
|
|
zcs->cstream = ZSTD_createCStream();
|
|
|
|
if (zcs->cstream == NULL) goto failed1;
|
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
if (ZSTD_isError(ZSTD_seekable_frameLog_allocVec(&zcs->framelog))) goto failed2;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
|
|
|
return zcs;
|
|
|
|
|
|
|
|
failed2:
|
|
|
|
ZSTD_freeCStream(zcs->cstream);
|
|
|
|
failed1:
|
|
|
|
free(zcs);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ZSTD_seekable_freeCStream(ZSTD_seekable_CStream* zcs)
|
|
|
|
{
|
2017-04-11 18:40:34 +00:00
|
|
|
if (zcs == NULL) return 0; /* support free on null */
|
2017-04-10 23:22:35 +00:00
|
|
|
ZSTD_freeCStream(zcs->cstream);
|
2017-04-28 19:17:09 +00:00
|
|
|
ZSTD_seekable_frameLog_freeVec(&zcs->framelog);
|
2017-04-10 23:22:35 +00:00
|
|
|
free(zcs);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ZSTD_seekable_initCStream(ZSTD_seekable_CStream* zcs,
|
|
|
|
int compressionLevel,
|
2017-04-11 18:40:34 +00:00
|
|
|
int checksumFlag,
|
fix confusion between unsigned <-> U32
as suggested in #1441.
generally U32 and unsigned are the same thing,
except when they are not ...
case : 32-bit compilation for MIPS (uint32_t == unsigned long)
A vast majority of transformation consists in transforming U32 into unsigned.
In rare cases, it's the other way around (typically for internal code, such as seeds).
Among a few issues this patches solves :
- some parameters were declared with type `unsigned` in *.h,
but with type `U32` in their implementation *.c .
- some parameters have type unsigned*,
but the caller user a pointer to U32 instead.
These fixes are useful.
However, the bulk of changes is about %u formating,
which requires unsigned type,
but generally receives U32 values instead,
often just for brevity (U32 is shorter than unsigned).
These changes are generally minor, or even annoying.
As a consequence, the amount of code changed is larger than I would expect for such a patch.
Testing is also a pain :
it requires manually modifying `mem.h`,
in order to lie about `U32`
and force it to be an `unsigned long` typically.
On a 64-bit system, this will break the equivalence unsigned == U32.
Unfortunately, it will also break a few static_assert(), controlling structure sizes.
So it also requires modifying `debug.h` to make `static_assert()` a noop.
And then reverting these changes.
So it's inconvenient, and as a consequence,
this property is currently not checked during CI tests.
Therefore, these problems can emerge again in the future.
I wonder if it is worth ensuring proper distinction of U32 != unsigned in CI tests.
It's another restriction for coding, adding more frustration during merge tests,
since most platforms don't need this distinction (hence contributor will not see it),
and while this can matter in theory, the number of platforms impacted seems minimal.
Thoughts ?
2018-12-22 00:19:44 +00:00
|
|
|
unsigned maxFrameSize)
|
2017-04-10 23:22:35 +00:00
|
|
|
{
|
2017-04-12 18:06:00 +00:00
|
|
|
zcs->framelog.size = 0;
|
|
|
|
zcs->frameCSize = 0;
|
|
|
|
zcs->frameDSize = 0;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
/* make sure maxFrameSize has a reasonable value */
|
|
|
|
if (maxFrameSize > ZSTD_SEEKABLE_MAX_FRAME_DECOMPRESSED_SIZE) {
|
2018-03-15 23:29:28 +00:00
|
|
|
return ERROR(frameParameter_unsupported);
|
2017-04-10 23:22:35 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
zcs->maxFrameSize = maxFrameSize
|
|
|
|
? maxFrameSize
|
|
|
|
: ZSTD_SEEKABLE_MAX_FRAME_DECOMPRESSED_SIZE;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
zcs->framelog.checksumFlag = checksumFlag;
|
|
|
|
if (zcs->framelog.checksumFlag) {
|
2017-04-10 23:22:35 +00:00
|
|
|
XXH64_reset(&zcs->xxhState, 0);
|
|
|
|
}
|
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
zcs->framelog.seekTablePos = 0;
|
|
|
|
zcs->framelog.seekTableIndex = 0;
|
2017-04-11 18:40:34 +00:00
|
|
|
zcs->writingSeekTable = 0;
|
|
|
|
|
2017-04-10 23:22:35 +00:00
|
|
|
return ZSTD_initCStream(zcs->cstream, compressionLevel);
|
|
|
|
}
|
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
size_t ZSTD_seekable_logFrame(ZSTD_frameLog* fl,
|
2018-12-13 20:14:36 +00:00
|
|
|
unsigned compressedSize,
|
|
|
|
unsigned decompressedSize,
|
|
|
|
unsigned checksum)
|
2017-04-10 23:22:35 +00:00
|
|
|
{
|
2017-04-28 19:17:09 +00:00
|
|
|
if (fl->size == ZSTD_SEEKABLE_MAXFRAMES)
|
2017-04-12 18:06:00 +00:00
|
|
|
return ERROR(frameIndex_tooLarge);
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-11 18:40:34 +00:00
|
|
|
/* grow the buffer if required */
|
2017-04-28 19:17:09 +00:00
|
|
|
if (fl->size == fl->capacity) {
|
2017-04-11 18:40:34 +00:00
|
|
|
/* exponential size increase for constant amortized runtime */
|
2017-04-28 19:17:09 +00:00
|
|
|
size_t const newCapacity = fl->capacity * 2;
|
|
|
|
framelogEntry_t* const newEntries = realloc(fl->entries,
|
2017-04-12 18:06:00 +00:00
|
|
|
sizeof(framelogEntry_t) * newCapacity);
|
2017-04-10 23:22:35 +00:00
|
|
|
|
|
|
|
if (newEntries == NULL) return ERROR(memory_allocation);
|
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
fl->entries = newEntries;
|
2018-12-13 20:14:36 +00:00
|
|
|
assert(newCapacity <= UINT_MAX);
|
|
|
|
fl->capacity = (U32)newCapacity;
|
2017-04-10 23:22:35 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
fl->entries[fl->size] = (framelogEntry_t){
|
|
|
|
compressedSize, decompressedSize, checksum
|
2017-04-14 00:47:55 +00:00
|
|
|
};
|
2017-04-28 19:17:09 +00:00
|
|
|
fl->size++;
|
2017-04-14 00:47:55 +00:00
|
|
|
|
2017-04-10 23:22:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
size_t ZSTD_seekable_endFrame(ZSTD_seekable_CStream* zcs, ZSTD_outBuffer* output)
|
2017-04-10 23:22:35 +00:00
|
|
|
{
|
|
|
|
size_t const prevOutPos = output->pos;
|
2017-04-11 18:40:34 +00:00
|
|
|
/* end the frame */
|
2017-04-10 23:22:35 +00:00
|
|
|
size_t ret = ZSTD_endStream(zcs->cstream, output);
|
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
zcs->frameCSize += output->pos - prevOutPos;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
|
|
|
/* need to flush before doing the rest */
|
|
|
|
if (ret) return ret;
|
|
|
|
|
|
|
|
/* frame done */
|
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
/* store the frame data for later */
|
2017-04-28 19:17:09 +00:00
|
|
|
ret = ZSTD_seekable_logFrame(
|
|
|
|
&zcs->framelog, zcs->frameCSize, zcs->frameDSize,
|
|
|
|
zcs->framelog.checksumFlag
|
|
|
|
? XXH64_digest(&zcs->xxhState) & 0xFFFFFFFFU
|
|
|
|
: 0);
|
2017-04-10 23:22:35 +00:00
|
|
|
if (ret) return ret;
|
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
/* reset for the next frame */
|
|
|
|
zcs->frameCSize = 0;
|
|
|
|
zcs->frameDSize = 0;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
|
|
|
ZSTD_resetCStream(zcs->cstream, 0);
|
2017-04-28 19:17:09 +00:00
|
|
|
if (zcs->framelog.checksumFlag)
|
2017-04-10 23:22:35 +00:00
|
|
|
XXH64_reset(&zcs->xxhState, 0);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ZSTD_seekable_compressStream(ZSTD_seekable_CStream* zcs, ZSTD_outBuffer* output, ZSTD_inBuffer* input)
|
|
|
|
{
|
|
|
|
const BYTE* const inBase = (const BYTE*) input->src + input->pos;
|
|
|
|
size_t inLen = input->size - input->pos;
|
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
inLen = MIN(inLen, (size_t)(zcs->maxFrameSize - zcs->frameDSize));
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
/* if we haven't finished flushing the last frame, don't start writing a new one */
|
2017-04-10 23:22:35 +00:00
|
|
|
if (inLen > 0) {
|
|
|
|
ZSTD_inBuffer inTmp = { inBase, inLen, 0 };
|
|
|
|
size_t const prevOutPos = output->pos;
|
|
|
|
|
|
|
|
size_t const ret = ZSTD_compressStream(zcs->cstream, output, &inTmp);
|
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
if (zcs->framelog.checksumFlag) {
|
2017-04-10 23:22:35 +00:00
|
|
|
XXH64_update(&zcs->xxhState, inBase, inTmp.pos);
|
|
|
|
}
|
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
zcs->frameCSize += output->pos - prevOutPos;
|
|
|
|
zcs->frameDSize += inTmp.pos;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
|
|
|
input->pos += inTmp.pos;
|
|
|
|
|
|
|
|
if (ZSTD_isError(ret)) return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
if (zcs->maxFrameSize == zcs->frameDSize) {
|
|
|
|
/* log the frame and start over */
|
|
|
|
size_t const ret = ZSTD_seekable_endFrame(zcs, output);
|
2017-04-10 23:22:35 +00:00
|
|
|
if (ZSTD_isError(ret)) return ret;
|
2017-04-11 18:40:34 +00:00
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
/* get the client ready for the next frame */
|
|
|
|
return (size_t)zcs->maxFrameSize;
|
2017-04-10 23:22:35 +00:00
|
|
|
}
|
|
|
|
|
2017-04-12 18:06:00 +00:00
|
|
|
return (size_t)(zcs->maxFrameSize - zcs->frameDSize);
|
2017-04-10 23:22:35 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
static inline size_t ZSTD_seekable_seekTableSize(const ZSTD_frameLog* fl)
|
2017-04-10 23:22:35 +00:00
|
|
|
{
|
2017-04-28 19:17:09 +00:00
|
|
|
size_t const sizePerFrame = 8 + (fl->checksumFlag?4:0);
|
2018-11-14 00:56:32 +00:00
|
|
|
size_t const seekTableLen = ZSTD_SKIPPABLEHEADERSIZE +
|
2017-04-28 19:17:09 +00:00
|
|
|
sizePerFrame * fl->size +
|
2017-04-10 23:22:35 +00:00
|
|
|
ZSTD_seekTableFooterSize;
|
|
|
|
|
|
|
|
return seekTableLen;
|
|
|
|
}
|
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
static inline size_t ZSTD_stwrite32(ZSTD_frameLog* fl,
|
2017-04-14 00:47:55 +00:00
|
|
|
ZSTD_outBuffer* output, U32 const value,
|
|
|
|
U32 const offset)
|
2017-04-10 23:22:35 +00:00
|
|
|
{
|
2017-04-28 19:17:09 +00:00
|
|
|
if (fl->seekTablePos < offset + 4) {
|
2017-04-14 00:47:55 +00:00
|
|
|
BYTE tmp[4]; /* so that we can work with buffers too small to write a whole word to */
|
|
|
|
size_t const lenWrite =
|
2017-04-28 19:17:09 +00:00
|
|
|
MIN(output->size - output->pos, offset + 4 - fl->seekTablePos);
|
2017-04-14 00:47:55 +00:00
|
|
|
MEM_writeLE32(tmp, value);
|
|
|
|
memcpy((BYTE*)output->dst + output->pos,
|
2017-04-28 19:17:09 +00:00
|
|
|
tmp + (fl->seekTablePos - offset), lenWrite);
|
2017-04-14 00:47:55 +00:00
|
|
|
output->pos += lenWrite;
|
2017-04-28 19:17:09 +00:00
|
|
|
fl->seekTablePos += lenWrite;
|
2017-04-14 00:47:55 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
if (lenWrite < 4) return ZSTD_seekable_seekTableSize(fl) - fl->seekTablePos;
|
2017-04-14 00:47:55 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
size_t ZSTD_seekable_writeSeekTable(ZSTD_frameLog* fl, ZSTD_outBuffer* output)
|
2017-04-14 00:47:55 +00:00
|
|
|
{
|
|
|
|
/* seekTableIndex: the current index in the table and
|
|
|
|
* seekTableSize: the amount of the table written so far
|
2017-04-11 18:40:34 +00:00
|
|
|
*
|
|
|
|
* This function is written this way so that if it has to return early
|
|
|
|
* because of a small buffer, it can keep going where it left off.
|
|
|
|
*/
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
size_t const sizePerFrame = 8 + (fl->checksumFlag?4:0);
|
|
|
|
size_t const seekTableLen = ZSTD_seekable_seekTableSize(fl);
|
2017-04-11 18:40:34 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
CHECK_Z(ZSTD_stwrite32(fl, output, ZSTD_MAGIC_SKIPPABLE_START | 0xE, 0));
|
2018-12-13 20:14:36 +00:00
|
|
|
assert(seekTableLen <= (size_t)UINT_MAX);
|
|
|
|
CHECK_Z(ZSTD_stwrite32(fl, output, (U32)seekTableLen - ZSTD_SKIPPABLEHEADERSIZE, 4));
|
2017-04-14 00:47:55 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
while (fl->seekTableIndex < fl->size) {
|
2018-12-13 20:14:36 +00:00
|
|
|
unsigned long long const start = ZSTD_SKIPPABLEHEADERSIZE + sizePerFrame * fl->seekTableIndex;
|
|
|
|
assert(start + 8 <= UINT_MAX);
|
2017-04-28 19:17:09 +00:00
|
|
|
CHECK_Z(ZSTD_stwrite32(fl, output,
|
|
|
|
fl->entries[fl->seekTableIndex].cSize,
|
2018-12-13 20:14:36 +00:00
|
|
|
(U32)start + 0));
|
2017-04-14 00:47:55 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
CHECK_Z(ZSTD_stwrite32(fl, output,
|
|
|
|
fl->entries[fl->seekTableIndex].dSize,
|
2018-12-13 20:14:36 +00:00
|
|
|
(U32)start + 4));
|
2017-04-14 00:47:55 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
if (fl->checksumFlag) {
|
2017-04-14 00:47:55 +00:00
|
|
|
CHECK_Z(ZSTD_stwrite32(
|
2017-04-28 19:17:09 +00:00
|
|
|
fl, output, fl->entries[fl->seekTableIndex].checksum,
|
2018-12-13 20:14:36 +00:00
|
|
|
(U32)start + 8));
|
2017-04-10 23:22:35 +00:00
|
|
|
}
|
2017-04-11 18:40:34 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
fl->seekTableIndex++;
|
2017-04-10 23:22:35 +00:00
|
|
|
}
|
|
|
|
|
2018-12-13 20:14:36 +00:00
|
|
|
assert(seekTableLen <= UINT_MAX);
|
2017-04-28 19:17:09 +00:00
|
|
|
CHECK_Z(ZSTD_stwrite32(fl, output, fl->size,
|
2018-12-13 20:14:36 +00:00
|
|
|
(U32)seekTableLen - ZSTD_seekTableFooterSize));
|
2017-04-11 18:40:34 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
if (output->size - output->pos < 1) return seekTableLen - fl->seekTablePos;
|
|
|
|
if (fl->seekTablePos < seekTableLen - 4) {
|
2017-04-11 18:40:34 +00:00
|
|
|
BYTE sfd = 0;
|
2017-04-28 19:17:09 +00:00
|
|
|
sfd |= (fl->checksumFlag) << 7;
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-14 00:47:55 +00:00
|
|
|
((BYTE*)output->dst)[output->pos] = sfd;
|
2017-04-11 18:40:34 +00:00
|
|
|
output->pos++;
|
2017-04-28 19:17:09 +00:00
|
|
|
fl->seekTablePos++;
|
2017-04-10 23:22:35 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
CHECK_Z(ZSTD_stwrite32(fl, output, ZSTD_SEEKABLE_MAGICNUMBER,
|
2018-12-13 20:14:36 +00:00
|
|
|
(U32)seekTableLen - 4));
|
2017-04-10 23:22:35 +00:00
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
if (fl->seekTablePos != seekTableLen) return ERROR(GENERIC);
|
2017-04-10 23:22:35 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t ZSTD_seekable_endStream(ZSTD_seekable_CStream* zcs, ZSTD_outBuffer* output)
|
|
|
|
{
|
2017-04-12 18:06:00 +00:00
|
|
|
if (!zcs->writingSeekTable && zcs->frameDSize) {
|
|
|
|
const size_t endFrame = ZSTD_seekable_endFrame(zcs, output);
|
|
|
|
if (ZSTD_isError(endFrame)) return endFrame;
|
2017-04-10 23:22:35 +00:00
|
|
|
/* return an accurate size hint */
|
2017-04-28 19:17:09 +00:00
|
|
|
if (endFrame) return endFrame + ZSTD_seekable_seekTableSize(&zcs->framelog);
|
2017-04-10 23:22:35 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 18:40:34 +00:00
|
|
|
zcs->writingSeekTable = 1;
|
|
|
|
|
2017-04-28 19:17:09 +00:00
|
|
|
return ZSTD_seekable_writeSeekTable(&zcs->framelog, output);
|
2017-04-10 23:22:35 +00:00
|
|
|
}
|