| 1 |
// This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell, |
| 2 |
// Christopher Chang. |
| 3 |
// |
| 4 |
// This library is free software: you can redistribute it and/or modify it |
| 5 |
// under the terms of the GNU Lesser General Public License as published by the |
| 6 |
// Free Software Foundation, either version 3 of the License, or (at your |
| 7 |
// option) any later version. |
| 8 |
// |
| 9 |
// This library is distributed in the hope that it will be useful, but WITHOUT |
| 10 |
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 |
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 12 |
// for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU Lesser General Public License |
| 15 |
// along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
#include <errno.h> |
| 18 |
#include "plink2_compress_stream.h" |
| 19 |
|
| 20 |
#ifdef __cplusplus |
| 21 |
namespace plink2 { |
| 22 |
#endif |
| 23 |
|
| 24 |
uint32_t g_zst_level = 0; |
| 25 |
|
| 26 |
PglErr InitCstreamNoop(const char* out_fname, uint32_t do_append, char* overflow_buf, CompressStreamState* css_ptr) { |
| 27 |
// css_ptr->z_outfile = nullptr; |
| 28 |
css_ptr->cctx = nullptr; |
| 29 |
// can't use fopen_checked since we need to be able to append |
| 30 |
css_ptr->outfile = fopen(out_fname, do_append? FOPEN_AB : FOPEN_WB); |
| 31 |
if (unlikely(!css_ptr->outfile)) { |
| 32 |
logputs("\n"); |
| 33 |
logerrprintfww(kErrprintfFopen, out_fname, strerror(errno)); |
| 34 |
return kPglRetOpenFail; |
| 35 |
} |
| 36 |
css_ptr->overflow_buf = overflow_buf; |
| 37 |
return kPglRetSuccess; |
| 38 |
} |
| 39 |
|
| 40 |
PglErr InitCstreamZstd(const char* out_fname, uint32_t do_append, __maybe_unused uint32_t thread_ct, uintptr_t overflow_buf_size, char* overflow_buf, unsigned char* compress_wkspace, CompressStreamState* css_ptr) { |
| 41 |
css_ptr->outfile = nullptr; |
| 42 |
css_ptr->cctx = ZSTD_createCCtx(); |
| 43 |
if (unlikely(!css_ptr->cctx)) { |
| 44 |
return kPglRetNomem; |
| 45 |
} |
| 46 |
__maybe_unused size_t retval = ZSTD_CCtx_setParameter(css_ptr->cctx, ZSTD_c_compressionLevel, g_zst_level); |
| 47 |
assert(!ZSTD_isError(retval)); |
| 48 |
#ifdef ZSTD_MULTITHREAD |
| 49 |
// ignore failure; if zstd is dynamically linked and was built without MT |
| 50 |
// support, so be it |
| 51 |
ZSTD_CCtx_setParameter(css_ptr->cctx, ZSTD_c_nbWorkers, thread_ct); |
| 52 |
#endif |
| 53 |
css_ptr->outfile = fopen(out_fname, do_append? FOPEN_AB : FOPEN_WB); |
| 54 |
if (unlikely(!css_ptr->outfile)) { |
| 55 |
logputs("\n"); |
| 56 |
logerrprintfww(kErrprintfFopen, out_fname, strerror(errno)); |
| 57 |
ZSTD_freeCCtx(css_ptr->cctx); // might return an error later? |
| 58 |
return kPglRetOpenFail; |
| 59 |
} |
| 60 |
css_ptr->output.dst = compress_wkspace; |
| 61 |
css_ptr->output.size = CstreamWkspaceReq(overflow_buf_size); |
| 62 |
css_ptr->output.pos = 0; |
| 63 |
css_ptr->overflow_buf = overflow_buf; |
| 64 |
return kPglRetSuccess; |
| 65 |
} |
| 66 |
|
| 67 |
// possible todo: replace output_zst with an enum which permits gzipping |
| 68 |
PglErr InitCstream(const char* out_fname, uint32_t do_append, uint32_t output_zst, uint32_t thread_ct, uintptr_t overflow_buf_size, char* overflow_buf, unsigned char* compress_wkspace, CompressStreamState* css_ptr) { |
| 69 |
if (!output_zst) { |
| 70 |
return InitCstreamNoop(out_fname, do_append, overflow_buf, css_ptr); |
| 71 |
} |
| 72 |
return InitCstreamZstd(out_fname, do_append, thread_ct, overflow_buf_size, overflow_buf, compress_wkspace, css_ptr); |
| 73 |
} |
| 74 |
|
| 75 |
PglErr InitCstreamAlloc(const char* out_fname, uint32_t do_append, uint32_t output_zst, uint32_t thread_ct, uintptr_t overflow_buf_size, CompressStreamState* css_ptr, char** cswritepp) { |
| 76 |
char* overflow_buf; |
| 77 |
if (unlikely(bigstack_alloc_c(overflow_buf_size, &overflow_buf))) { |
| 78 |
return kPglRetNomem; |
| 79 |
} |
| 80 |
unsigned char* compress_wkspace = nullptr; |
| 81 |
if (output_zst) { |
| 82 |
if (unlikely(bigstack_alloc_uc(CstreamWkspaceReq(overflow_buf_size), &compress_wkspace))) { |
| 83 |
return kPglRetNomem; |
| 84 |
} |
| 85 |
} |
| 86 |
PglErr reterr = InitCstream(out_fname, do_append, output_zst, thread_ct, overflow_buf_size, overflow_buf, compress_wkspace, css_ptr); |
| 87 |
*cswritepp = overflow_buf; |
| 88 |
return reterr; |
| 89 |
} |
| 90 |
|
| 91 |
BoolErr ForceUncompressedCswrite(CompressStreamState* css_ptr, char** writep_ptr) { |
| 92 |
char* writep = *writep_ptr; |
| 93 |
if (css_ptr->overflow_buf != writep) { |
| 94 |
if (unlikely(fwrite_checked(css_ptr->overflow_buf, writep - css_ptr->overflow_buf, css_ptr->outfile))) { |
| 95 |
return 1; |
| 96 |
} |
| 97 |
*writep_ptr = css_ptr->overflow_buf; |
| 98 |
} |
| 99 |
return 0; |
| 100 |
} |
| 101 |
|
| 102 |
BoolErr ForceCompressedCswrite(CompressStreamState* css_ptr, char** writep_ptr) { |
| 103 |
char* overflow_buf = css_ptr->overflow_buf; |
| 104 |
char* writep = *writep_ptr; |
| 105 |
if (overflow_buf != writep) { |
| 106 |
const uintptr_t in_size = writep - overflow_buf; |
| 107 |
ZSTD_inBuffer input = {overflow_buf, in_size, 0}; |
| 108 |
while (1) { |
| 109 |
// todo: conditionally support seekable files |
| 110 |
__maybe_unused size_t retval = ZSTD_compressStream2(css_ptr->cctx, &css_ptr->output, &input, ZSTD_e_continue); |
| 111 |
assert(!ZSTD_isError(retval)); |
| 112 |
if (css_ptr->output.pos >= kCompressStreamBlock) { |
| 113 |
if (unlikely(fwrite_checked(css_ptr->output.dst, css_ptr->output.pos, css_ptr->outfile))) { |
| 114 |
return 1; |
| 115 |
} |
| 116 |
css_ptr->output.pos = 0; |
| 117 |
} |
| 118 |
const uintptr_t bytes_left = input.size - input.pos; |
| 119 |
if (bytes_left < kCompressStreamBlock) { |
| 120 |
memmove(overflow_buf, &(overflow_buf[in_size - bytes_left]), bytes_left); |
| 121 |
*writep_ptr = &(overflow_buf[bytes_left]); |
| 122 |
break; |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
return 0; |
| 127 |
} |
| 128 |
|
| 129 |
BoolErr CsputsStd(const char* readp, uint32_t byte_ct, CompressStreamState* css_ptr, char** writep_ptr) { |
| 130 |
char* writep = *writep_ptr; |
| 131 |
char* overflow_buf = css_ptr->overflow_buf; |
| 132 |
uint32_t cur_write_space = 2 * kCompressStreamBlock - S_CAST(uintptr_t, writep - overflow_buf); |
| 133 |
if (IsUncompressedCstream(css_ptr)) { |
| 134 |
while (byte_ct > cur_write_space) { |
| 135 |
memcpy(writep, readp, cur_write_space); |
| 136 |
if (unlikely(!fwrite_unlocked(overflow_buf, 2 * kCompressStreamBlock, 1, css_ptr->outfile))) { |
| 137 |
return 1; |
| 138 |
} |
| 139 |
writep = overflow_buf; |
| 140 |
readp = &(readp[cur_write_space]); |
| 141 |
byte_ct -= cur_write_space; |
| 142 |
cur_write_space = 2 * kCompressStreamBlock; |
| 143 |
} |
| 144 |
} else { |
| 145 |
while (byte_ct > cur_write_space) { |
| 146 |
memcpy(writep, readp, cur_write_space); |
| 147 |
ZSTD_inBuffer input = {overflow_buf, 2 * kCompressStreamBlock, 0}; |
| 148 |
while (1) { |
| 149 |
// todo: conditionally support seekable files |
| 150 |
__maybe_unused size_t retval = ZSTD_compressStream2(css_ptr->cctx, &css_ptr->output, &input, ZSTD_e_continue); |
| 151 |
assert(!ZSTD_isError(retval)); |
| 152 |
if (css_ptr->output.pos >= kCompressStreamBlock) { |
| 153 |
if (unlikely(!fwrite_unlocked(css_ptr->output.dst, css_ptr->output.pos, 1, css_ptr->outfile))) { |
| 154 |
return 1; |
| 155 |
} |
| 156 |
css_ptr->output.pos = 0; |
| 157 |
} |
| 158 |
const uintptr_t bytes_left = input.size - input.pos; |
| 159 |
if (bytes_left < kCompressStreamBlock) { |
| 160 |
memmove(overflow_buf, &(overflow_buf[2 * kCompressStreamBlock - bytes_left]), bytes_left); |
| 161 |
writep = &(overflow_buf[retval]); |
| 162 |
break; |
| 163 |
} |
| 164 |
} |
| 165 |
readp = &(readp[cur_write_space]); |
| 166 |
byte_ct -= cur_write_space; |
| 167 |
cur_write_space = 2 * kCompressStreamBlock - S_CAST(uintptr_t, writep - overflow_buf); |
| 168 |
} |
| 169 |
} |
| 170 |
memcpy(writep, readp, byte_ct); |
| 171 |
*writep_ptr = &(writep[byte_ct]); |
| 172 |
return Cswrite(css_ptr, writep_ptr); |
| 173 |
} |
| 174 |
|
| 175 |
BoolErr UncompressedCswriteCloseNull(CompressStreamState* css_ptr, char* writep) { |
| 176 |
ForceUncompressedCswrite(css_ptr, &writep); |
| 177 |
css_ptr->overflow_buf = nullptr; |
| 178 |
int32_t ii = ferror_unlocked(css_ptr->outfile); |
| 179 |
int32_t jj = fclose(css_ptr->outfile); |
| 180 |
return ii || jj; |
| 181 |
} |
| 182 |
|
| 183 |
BoolErr CompressedCswriteCloseNull(CompressStreamState* css_ptr, char* writep) { |
| 184 |
char* overflow_buf = css_ptr->overflow_buf; |
| 185 |
const uintptr_t in_size = writep - overflow_buf; |
| 186 |
ZSTD_inBuffer input = {overflow_buf, in_size, 0}; |
| 187 |
BoolErr reterr = 0; |
| 188 |
while (1) { |
| 189 |
__maybe_unused size_t retval = ZSTD_compressStream2(css_ptr->cctx, &css_ptr->output, &input, ZSTD_e_end); |
| 190 |
assert(!ZSTD_isError(retval)); |
| 191 |
if (css_ptr->output.pos) { |
| 192 |
if (unlikely(fwrite_checked(css_ptr->output.dst, css_ptr->output.pos, css_ptr->outfile))) { |
| 193 |
reterr = 1; |
| 194 |
} |
| 195 |
css_ptr->output.pos = 0; |
| 196 |
} |
| 197 |
if (!retval) { |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
ZSTD_freeCCtx(css_ptr->cctx); // might return an error later? |
| 202 |
css_ptr->overflow_buf = nullptr; |
| 203 |
int32_t ii = ferror_unlocked(css_ptr->outfile); |
| 204 |
int32_t jj = fclose(css_ptr->outfile); |
| 205 |
return reterr || ii || jj; |
| 206 |
} |
| 207 |
|
| 208 |
BoolErr CswriteCloseNull(CompressStreamState* css_ptr, char* writep) { |
| 209 |
if (IsUncompressedCstream(css_ptr)) { |
| 210 |
return UncompressedCswriteCloseNull(css_ptr, writep); |
| 211 |
} |
| 212 |
return CompressedCswriteCloseNull(css_ptr, writep); |
| 213 |
} |
| 214 |
|
| 215 |
#ifdef __cplusplus |
| 216 |
} |
| 217 |
#endif |