43#ifdef FREECIV_HAVE_LIBZ
69#define PLAIN_FILE_BUF_SIZE (8096*1024)
71#ifdef FREECIV_HAVE_LIBBZ2
81#ifdef FREECIV_HAVE_LIBLZMA
83#define PLAIN_FILE_BUF_SIZE_XZ PLAIN_FILE_BUF_SIZE
84#define XZ_DECODER_TEST_SIZE (4*1024)
88#define XZ_DECODER_MEMLIMIT (65*1024*1024)
89#define XZ_DECODER_MEMLIMIT_STEP (25*1024*1024)
90#define XZ_DECODER_MEMLIMIT_FINAL (100*1024*1024)
124#ifdef FREECIV_HAVE_LIBZSTD
126#define PLAIN_FILE_BUF_SIZE_ZSTD PLAIN_FILE_BUF_SIZE
156#ifdef FREECIV_HAVE_LIBZ
159#ifdef FREECIV_HAVE_LIBBZ2
162#ifdef FREECIV_HAVE_LIBLZMA
165#ifdef FREECIV_HAVE_LIBZSTD
178#ifdef FREECIV_HAVE_LIBZ
181#ifdef FREECIV_HAVE_LIBBZ2
184#ifdef FREECIV_HAVE_LIBLZMA
187#ifdef FREECIV_HAVE_LIBZSTD
195#define fz_method_validate(method) \
196 (fz_method_is_valid(method) ? method \
197 : (fc_assert_msg(fz_method_is_valid(method), \
198 "Unsupported compress method %d, reverting to plain.",\
244 if (mode[0] ==
'w') {
248#if defined(FREECIV_HAVE_LIBBZ2) || defined(FREECIV_HAVE_LIBLZMA) || defined (FREECIV_HAVE_LIBZSTD)
258#ifdef FREECIV_HAVE_LIBBZ2
267 fp->
u.bz2.file =
nullptr;
269 if (!fp->
u.bz2.file) {
294 fp->
u.bz2.firstbyte = -1;
296 fp->
u.bz2.firstbyte =
tmp;
298 fp->
u.bz2.eof =
TRUE;
299 }
else if (fp->
u.bz2.error !=
BZ_OK) {
307 fp->
u.bz2.firstbyte =
tmp;
320#ifdef FREECIV_HAVE_LIBLZMA
342 fp->
u.xz.stream.next_in = fp->
u.xz.in_buf;
343 fp->
u.xz.stream.avail_in =
len;
345 fp->
u.xz.stream.next_out = fp->
u.xz.out_buf;
349 fp->
u.xz.hack_byte_used =
TRUE;
352 fp->
u.xz.hack_byte_used =
FALSE;
358 fp->
u.xz.out_index = 0;
359 fp->
u.xz.total_read = 0;
360 fp->
u.xz.out_avail = fp->
u.xz.stream.total_out;
364 free(fp->
u.xz.out_buf);
368 free(fp->
u.xz.in_buf);
375#ifdef FREECIV_HAVE_LIBZSTD
384 fp->
u.zstd.nonconst_in =
fc_malloc(fp->
u.zstd.in_buf.size);
385 fp->
u.zstd.in_buf.src = fp->
u.zstd.nonconst_in;
387 len =
fread(fp->
u.zstd.nonconst_in, 1, fp->
u.zstd.in_buf.size,
391 fp->
u.zstd.in_buf.size =
len;
394 fp->
u.zstd.out_buf.dst =
fc_malloc(fp->
u.zstd.out_buf.size);
396 fp->
u.zstd.out_buf.pos = 0;
397 fp->
u.zstd.in_buf.pos = 0;
405 fp->
u.zstd.outbuf_pos = 0;
409 free(fp->
u.zstd.out_buf.dst);
412 free(fp->
u.zstd.nonconst_in);
419#ifdef FREECIV_HAVE_LIBZ
429#ifdef FREECIV_HAVE_LIBLZMA
438 fp->
u.xz.error =
ret;
444 fp->
u.xz.stream.next_in = fp->
u.xz.in_buf;
446 fp->
u.xz.stream.next_out = fp->
u.xz.out_buf;
448 fp->
u.xz.out_index = 0;
449 fp->
u.xz.total_read = 0;
458#ifdef FREECIV_HAVE_LIBZSTD
477 fp->
u.zstd.nonconst_in =
fc_malloc(fp->
u.zstd.in_buf.size);
478 fp->
u.zstd.in_buf.src = fp->
u.zstd.nonconst_in;
480 fp->
u.zstd.out_buf.dst =
fc_malloc(fp->
u.zstd.out_buf.size);
481 fp->
u.zstd.in_buf.pos = 0;
482 fp->
u.zstd.out_buf.pos = 0;
486#ifdef FREECIV_HAVE_LIBBZ2
496 if (fp->
u.bz2.error !=
BZ_OK) {
501 fp->
u.bz2.file =
nullptr;
504 fp->
u.bz2.file =
nullptr;
506 if (!fp->
u.bz2.file) {
515#ifdef FREECIV_HAVE_LIBZ
519 if (mode[0] ==
'w') {
587#ifdef FREECIV_HAVE_LIBLZMA
593 free(fp->
u.xz.in_buf);
594 free(fp->
u.xz.out_buf);
599#ifdef FREECIV_HAVE_LIBZSTD
601 if (fp->
mode ==
'w') {
603 while (fp->
u.zstd.out_buf.pos > 0) {
604 fwrite(fp->
u.zstd.out_buf.dst, 1,
605 fp->
u.zstd.out_buf.pos, fp->
u.zstd.
plain);
606 fp->
u.zstd.out_buf.pos = 0;
613 free(fp->
u.zstd.nonconst_in);
614 free(fp->
u.zstd.out_buf.dst);
619#ifdef FREECIV_HAVE_LIBBZ2
621 if (
'w' == fp->
mode) {
626 error = fp->
u.bz2.error;
629 return BZ_OK == error ? 0 : 1;
631#ifdef FREECIV_HAVE_LIBZ
635 return 0 > error ? error : 0;
696#ifdef FREECIV_HAVE_LIBLZMA
701 for (
i = 0;
i <
size - 1;
i += j) {
708 j++, fp->
u.xz.out_avail--) {
709 buffer[
i + j] = fp->
u.xz.out_buf[fp->
u.xz.out_index++];
710 fp->
u.xz.total_read++;
711 if (buffer[
i + j] ==
'\n') {
717 buffer[
i + j] =
'\0';
721 if (fp->
u.xz.hack_byte_used) {
724 fp->
u.xz.in_buf[0] = fp->
u.xz.hack_byte;
733 fp->
u.xz.hack_byte_used =
FALSE;
743 buffer[
i + j] =
'\0';
746 fp->
u.xz.stream.next_out = fp->
u.xz.out_buf;
749 fp->
u.xz.out_index = 0;
751 fp->
u.xz.stream.total_out - fp->
u.xz.total_read;
760 fp->
u.xz.stream.next_in = fp->
u.xz.in_buf;
761 fp->
u.xz.stream.avail_in =
len;
762 fp->
u.xz.stream.next_out = fp->
u.xz.out_buf;
764 if (fp->
u.xz.hack_byte_used) {
771 fp->
u.xz.stream.total_out - fp->
u.xz.total_read;
772 fp->
u.xz.out_index = 0;
784#ifdef FREECIV_HAVE_LIBZSTD
789 for (
i = 0;
i <
size - 1;) {
793 while (fp->
u.zstd.outbuf_pos < fp->
u.zstd.out_buf.pos) {
794 buffer[
i] = ((
char *)fp->
u.zstd.out_buf.dst)[fp->
u.zstd.outbuf_pos++];
795 if (buffer[
i] ==
'\n' ||
i ==
size - 2) {
796 buffer[
i + 1] =
'\0';
803 fp->
u.zstd.outbuf_pos = 0;
805 if (fp->
u.zstd.in_buf.pos != 0) {
807 for (j = 0; j < fp->
u.zstd.in_buf.size - fp->
u.zstd.in_buf.pos; j++) {
808 fp->
u.zstd.nonconst_in[j] = fp->
u.zstd.nonconst_in[j + fp->
u.zstd.in_buf.pos];
812 len =
fread(fp->
u.zstd.nonconst_in + j, 1, fp->
u.zstd.in_buf.size - j,
816 fp->
u.zstd.in_buf.size =
len + j;
820 fp->
u.zstd.out_buf.pos = 0;
821 fp->
u.zstd.in_buf.pos = 0;
831 if (fp->
u.zstd.out_buf.pos == 0 &&
len == 0) {
847#ifdef FREECIV_HAVE_LIBBZ2
855 if (fp->
u.bz2.firstbyte >= 0) {
856 buffer[0] = fp->
u.bz2.firstbyte;
857 fp->
u.bz2.firstbyte = -1;
860 if (!fp->
u.bz2.eof) {
865 if (!fp->
u.bz2.eof) {
868 && fp->
u.bz2.error ==
BZ_OK && buffer[
i - 1] !=
'\n' ;
873 if (fp->
u.bz2.error !=
BZ_OK
882 fp->
u.bz2.eof =
TRUE;
889#ifdef FREECIV_HAVE_LIBZ
903#ifdef FREECIV_HAVE_LIBLZMA
931 fp->
u.xz.stream.next_out = fp->
u.xz.out_buf;
932 }
while (fp->
u.xz.stream.avail_in > 0);
980#ifdef FREECIV_HAVE_LIBLZMA
989 log_error(
"Too much data: truncated in fz_fprintf (%u)",
993 fp->
u.xz.stream.next_in = fp->
u.xz.in_buf;
994 fp->
u.xz.stream.avail_in = num;
999 return strlen((
char *)fp->
u.xz.in_buf);
1004#ifdef FREECIV_HAVE_LIBZSTD
1013 log_error(
"Too much data: truncated in fz_fprintf (%u)",
1018 fp->
u.zstd.in_buf.pos = 0;
1019 fp->
u.zstd.in_buf.size = num;
1021 while (fp->
u.zstd.in_buf.pos < fp->
u.zstd.in_buf.size) {
1025 &fp->
u.zstd.out_buf,
1026 &fp->
u.zstd.in_buf);
1031 if (fp->
u.zstd.out_buf.pos > 0) {
1033 fp->
u.zstd.out_buf.pos, fp->
u.zstd.
plain);
1039 fp->
u.zstd.out_buf.pos = 0;
1047#ifdef FREECIV_HAVE_LIBBZ2
1056 log_error(
"Too much data: truncated in fz_fprintf (%lu)",
1057 (
unsigned long)
sizeof(buffer));
1060 if (fp->
u.bz2.error !=
BZ_OK) {
1067#ifdef FREECIV_HAVE_LIBZ
1076 log_error(
"Too much data: truncated in fz_fprintf (%lu)",
1077 (
unsigned long)
sizeof(buffer));
1079 return gzwrite(fp->
u.zlib, buffer, (
unsigned int)
strlen(buffer));
1108#ifdef FREECIV_HAVE_LIBLZMA
1117#ifdef FREECIV_HAVE_LIBZSTD
1124#ifdef FREECIV_HAVE_LIBBZ2
1126 return (
BZ_OK != fp->
u.bz2.error
1129#ifdef FREECIV_HAVE_LIBZ
1135 return 0 > error ? error : 0;
1163#ifdef FREECIV_HAVE_LIBLZMA
1169 switch (fp->
u.xz.error) {
1180 cleartext =
"Cannot calculate the integrity check";
1215#ifdef FREECIV_HAVE_LIBZSTD
1238#ifdef FREECIV_HAVE_LIBBZ2
1248 switch (fp->
u.bz2.error) {
1305#ifdef FREECIV_HAVE_LIBZ
#define fz_method_validate(method)
char * fz_fgets(char *buffer, int size, fz_FILE *fp)
int fz_fprintf(fz_FILE *fp, const char *format,...)
fz_FILE * fz_from_stream(FILE *stream)
fz_FILE * fz_from_memory(char *buffer, int size, bool control)
fz_FILE * fz_from_file(const char *filename, const char *in_mode, enum fz_method method, int compress_level)
const char * fz_strerror(fz_FILE *fp)
static bool fz_method_is_valid(enum fz_method method)
int fz_fclose(fz_FILE *fp)
int fz_ferror(fz_FILE *fp)
#define fc_assert_msg(condition, message,...)
#define fc_assert_ret_val(condition, val)
#define log_error(message,...)
int fc_snprintf(char *str, size_t n, const char *format,...)
const char * fc_strerror(fc_errno err)
int cat_snprintf(char *str, size_t n, const char *format,...)
int fc_vsnprintf(char *str, size_t n, const char *format, va_list ap)
FILE * fc_fopen(const char *filename, const char *opentype)
fc_errno fc_get_errno(void)
bool is_reg_file_for_access(const char *name, bool write_access)
#define sz_strlcpy(dest, src)
#define sz_strlcat(dest, src)