26 #ifndef LZ4F_HEADER_SIZE_MAX
27 #define LZ4F_HEADER_SIZE_MAX 32
38 typedef struct LZ4State
45 LZ4F_preferences_t prefs;
47 LZ4F_compressionContext_t ctx;
48 LZ4F_decompressionContext_t dtx;
65 bool needs_header_flush;
74 size_t overflowalloclen;
101 LZ4State_compression_init(LZ4State *
state)
111 if (
state->buflen < LZ4F_HEADER_SIZE_MAX)
112 state->buflen = LZ4F_HEADER_SIZE_MAX;
114 status = LZ4F_createCompressionContext(&
state->ctx, LZ4F_VERSION);
115 if (LZ4F_isError(status))
117 state->errcode = status;
122 status = LZ4F_compressBegin(
state->ctx,
125 if (LZ4F_isError(status))
127 state->errcode = status;
131 state->compressedlen = status;
150 LZ4F_decompressionContext_t ctx = NULL;
151 LZ4F_decompressOptions_t dec_opt;
152 LZ4F_errorCode_t status;
154 memset(&dec_opt, 0,
sizeof(dec_opt));
155 status = LZ4F_createDecompressionContext(&ctx, LZ4F_VERSION);
156 if (LZ4F_isError(status))
157 pg_fatal(
"could not create LZ4 decompression context: %s",
158 LZ4F_getErrorName(status));
163 while ((r = cs->
readF(AH, &readbuf, &readbuflen)) > 0)
170 readend = readbuf + r;
171 while (readp < readend)
174 size_t read_size = readend - readp;
177 status = LZ4F_decompress(ctx, outbuf, &out_size,
178 readp, &read_size, &dec_opt);
179 if (LZ4F_isError(status))
180 pg_fatal(
"could not decompress: %s",
181 LZ4F_getErrorName(status));
183 ahwrite(outbuf, 1, out_size, AH);
191 status = LZ4F_freeDecompressionContext(ctx);
192 if (LZ4F_isError(status))
193 pg_fatal(
"could not free LZ4 decompression context: %s",
194 LZ4F_getErrorName(status));
199 const void *
data,
size_t dLen)
210 state->needs_header_flush =
false;
222 status = LZ4F_compressUpdate(
state->ctx,
226 if (LZ4F_isError(status))
227 pg_fatal(
"could not compress data: %s",
228 LZ4F_getErrorName(status));
252 if (
state->needs_header_flush)
255 status = LZ4F_compressEnd(
state->ctx,
258 if (LZ4F_isError(status))
259 pg_fatal(
"could not end compression: %s",
260 LZ4F_getErrorName(status));
264 status = LZ4F_freeCompressionContext(
state->ctx);
265 if (LZ4F_isError(status))
266 pg_fatal(
"could not end compression: %s",
267 LZ4F_getErrorName(status));
283 cs->
readData = ReadDataFromArchiveLZ4;
285 cs->
end = EndCompressorLZ4;
300 if (!LZ4State_compression_init(
state))
301 pg_fatal(
"could not initialize LZ4 compression: %s",
302 LZ4F_getErrorName(
state->errcode));
305 state->needs_header_flush =
true;
325 return state->overflowlen == 0 && feof(
state->fp);
334 if (LZ4F_isError(
state->errcode))
353 LZ4Stream_init(LZ4State *
state,
int size,
bool compressing)
360 state->compressing = compressing;
361 state->inited =
true;
364 if (
state->compressing)
367 if (!LZ4State_compression_init(
state))
372 errno = (errno) ? errno : ENOSPC;
378 status = LZ4F_createDecompressionContext(&
state->dtx, LZ4F_VERSION);
379 if (LZ4F_isError(status))
381 state->errcode = status;
390 state->overflowlen = 0;
407 LZ4Stream_read_overflow(LZ4State *
state,
void *ptr,
int size,
bool eol_flag)
412 if (
state->overflowlen == 0)
418 readlen =
state->overflowlen;
420 if (eol_flag && (p = memchr(
state->overflowbuf,
'\n', readlen)))
422 readlen = p -
state->overflowbuf + 1;
424 memcpy(ptr,
state->overflowbuf, readlen);
425 state->overflowlen -= readlen;
427 if (
state->overflowlen > 0)
428 memmove(
state->overflowbuf,
state->overflowbuf + readlen,
state->overflowlen);
449 LZ4Stream_read_internal(LZ4State *
state,
void *ptr,
int ptrsize,
bool eol_flag)
454 bool eol_found =
false;
459 if (!LZ4Stream_init(
state,
size,
false ))
474 dsize = LZ4Stream_read_overflow(
state, ptr,
size, eol_flag);
475 if (dsize ==
size || (eol_flag && memchr(ptr,
'\n', dsize)))
485 rsize = fread(readbuf, 1,
size,
state->fp);
489 rp = (
char *) readbuf;
490 rend = (
char *) readbuf + rsize;
495 size_t outlen =
state->buflen;
496 size_t read_remain = rend - rp;
498 memset(
state->buffer, 0, outlen);
499 status = LZ4F_decompress(
state->dtx,
state->buffer, &outlen,
500 rp, &read_remain, NULL);
501 if (LZ4F_isError(status))
503 state->errcode = status;
514 if (outlen > 0 && dsize <
size && eol_found ==
false)
517 size_t lib = (!eol_flag) ?
size - dsize :
size - 1 - dsize;
518 size_t len = outlen < lib ? outlen : lib;
521 (p = memchr(
state->buffer,
'\n', outlen)) &&
522 (size_t) (p -
state->buffer + 1) <=
len)
528 memcpy((
char *) ptr + dsize,
state->buffer,
len);
540 while (
state->overflowlen + outlen >
state->overflowalloclen)
542 state->overflowalloclen *= 2;
544 state->overflowalloclen);
547 memcpy(
state->overflowbuf +
state->overflowlen,
state->buffer, outlen);
548 state->overflowlen += outlen;
551 }
while (rsize ==
size && dsize <
size && eol_found ==
false);
580 if (LZ4F_isError(status))
582 state->errcode = status;
586 if (fwrite(
state->buffer, 1, status,
state->fp) != status)
588 errno = (errno) ? errno : ENOSPC;
592 ptr = ((
const char *) ptr) +
chunk;
607 if ((ret = LZ4Stream_read_internal(
state, ptr,
size,
false)) < 0)
608 pg_fatal(
"could not read from input file: %s", LZ4Stream_get_error(CFH));
611 *rsize = (size_t) ret;
625 if (LZ4Stream_read_internal(
state, &
c, 1,
false) <= 0)
627 if (!LZ4Stream_eof(CFH))
628 pg_fatal(
"could not read from input file: %s", LZ4Stream_get_error(CFH));
630 pg_fatal(
"could not read from input file: end of file");
645 ret = LZ4Stream_read_internal(
state, ptr,
size - 1,
true);
646 if (ret < 0 || (ret == 0 && !LZ4Stream_eof(CFH)))
647 pg_fatal(
"could not read from input file: %s", LZ4Stream_get_error(CFH));
676 if (
state->compressing)
678 status = LZ4F_compressEnd(
state->ctx,
state->buffer,
state->buflen, NULL);
679 if (LZ4F_isError(status))
680 pg_fatal(
"could not end compression: %s",
681 LZ4F_getErrorName(status));
682 else if (fwrite(
state->buffer, 1, status,
state->fp) != status)
684 errno = (errno) ? errno : ENOSPC;
688 status = LZ4F_freeCompressionContext(
state->ctx);
689 if (LZ4F_isError(status))
690 pg_fatal(
"could not end compression: %s",
691 LZ4F_getErrorName(status));
695 status = LZ4F_freeDecompressionContext(
state->dtx);
696 if (LZ4F_isError(status))
697 pg_fatal(
"could not end decompression: %s",
698 LZ4F_getErrorName(status));
707 return fclose(fp) == 0;
711 LZ4Stream_open(
const char *path,
int fd,
const char *
mode,
720 fp = fopen(path,
mode);
723 state->errcode = errno;
780 pg_fatal(
"this build does not support compression with %s",
"LZ4");
787 pg_fatal(
"this build does not support compression with %s",
"LZ4");
#define DEFAULT_IO_BUFFER_SIZE
void InitCompressFileHandleLZ4(CompressFileHandle *CFH, const pg_compress_specification compression_spec)
void InitCompressorLZ4(CompressorState *cs, const pg_compress_specification compression_spec)
int errcode(int sqlerrcode)
int errmsg(const char *fmt,...)
void * pg_realloc(void *ptr, size_t size)
void * pg_malloc0(size_t size)
void * pg_malloc(size_t size)
if(TABLE==NULL||TABLE_index==NULL)
void ahwrite(const void *ptr, size_t size, size_t nmemb, ArchiveHandle *AH)
static PgChecksumMode mode
static int fd(const char *x, int i)
char * psprintf(const char *fmt,...)
static pg_noinline void Size size
bool(* open_write_func)(const char *path, const char *mode, CompressFileHandle *CFH)
const char *(* get_error_func)(CompressFileHandle *CFH)
bool(* write_func)(const void *ptr, size_t size, struct CompressFileHandle *CFH)
int(* getc_func)(CompressFileHandle *CFH)
char *(* gets_func)(char *s, int size, CompressFileHandle *CFH)
bool(* eof_func)(CompressFileHandle *CFH)
bool(* open_func)(const char *path, int fd, const char *mode, CompressFileHandle *CFH)
pg_compress_specification compression_spec
bool(* close_func)(CompressFileHandle *CFH)
bool(* read_func)(void *ptr, size_t size, size_t *rsize, CompressFileHandle *CFH)
void(* readData)(ArchiveHandle *AH, CompressorState *cs)
pg_compress_specification compression_spec
void(* end)(ArchiveHandle *AH, CompressorState *cs)
void(* writeData)(ArchiveHandle *AH, CompressorState *cs, const void *data, size_t dLen)