Debian Codegraph @

include/plink2_zstfile.h

raw
1 #ifndef __PLINK2_ZSTFILE_H__
2 #define __PLINK2_ZSTFILE_H__
3
4 // This library is part of PLINK 2.0, copyright (C) 2005-2025 Shaun Purcell,
5 // Christopher Chang.
6 //
7 // This library is free software: you can redistribute it and/or modify it
8 // under the terms of the GNU Lesser General Public License as published by the
9 // Free Software Foundation, either version 3 of the License, or (at your
10 // option) any later version.
11 //
12 // This library is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU Lesser General Public License
18 // along with this library. If not, see <http://www.gnu.org/licenses/>.
19
20
21 // This file defines a simple zstRFILE type, which intentionally does not try
22 // to offer everything zlib does. (It doesn't even pass uncompressed data
23 // through, since we can already use gzFile for that.) The typename has
24 // all-caps FILE at the end to indicate that, like FILE and unlike gzFile, it's
25 // a struct instead of a pointer-to-struct.
26
27 #include "plink2_base.h"
28
29 #ifdef IGNORE_BUNDLED_ZSTD
30 # include <zstd.h>
31 # if !defined(ZSTD_VERSION_NUMBER) || (ZSTD_VERSION_NUMBER < 10404)
32 # error "plink2_zstfile requires zstd 1.4.4 or later."
33 # endif
34 #else
35 # include "../zstd/lib/zstd.h"
36 #endif
37
38 #ifdef __cplusplus
39 namespace plink2 {
40 #endif
41
42 // may add kFileZstdSeekable later
43 ENUM_U31_DEF_START()
44 kFileUncompressed,
45 kFileGzip,
46 kFileBgzf,
47 kFileZstd
48 ENUM_U31_DEF_END(FileCompressionType);
49
50 HEADER_INLINE uint32_t IsZstdFrame(uint32_t magic4) {
51 return (magic4 == ZSTD_MAGICNUMBER) || ((magic4 & ZSTD_MAGIC_SKIPPABLE_MASK) == ZSTD_MAGIC_SKIPPABLE_START);
52 }
53
54 typedef struct zstRFILEMainStruct {
55 FILE* ff;
56 ZSTD_DStream* zds;
57 ZSTD_inBuffer zib;
58 const char* errmsg;
59 PglErr reterr; // kPglRetSkipped == ordinary eof
60 } zstRFILEMain;
61
62 typedef struct zstRFILEStruct {
63 #ifdef __cplusplus
64 zstRFILEMain& GET_PRIVATE_m() { return m; }
65 zstRFILEMain const& GET_PRIVATE_m() const { return m; }
66 private:
67 #endif
68 zstRFILEMain m;
69 } zstRFILE;
70
71 void PreinitZstRfile(zstRFILE* zrf_ptr);
72
73 extern const char kShortErrZstdPrefixUnknown[];
74 extern const char kShortErrZstdCorruptionDetected[];
75
76 // Assumes file type is already known to be kFileZstd.
77 // Can return nomem, open-fail, or read-fail.
78 PglErr ZstRfileOpen(const char* fname, zstRFILE* zrf_ptr);
79
80 // This has essentially the same behavior as gzread. (to determine: can we use
81 // zlib's negative error codes, or is it necessary to make those
82 // zstd-specific?)
83 int32_t zstread(zstRFILE* zrf_ptr, void* dst, uint32_t len);
84
85 void zstrewind(zstRFILE* zrf_ptr);
86
87 HEADER_INLINE int32_t ZstRfileIsOpen(const zstRFILE* zrf_ptr) {
88 return (GET_PRIVATE(*zrf_ptr, m).ff != nullptr);
89 }
90
91 HEADER_INLINE int32_t zsteof(const zstRFILE* zrf_ptr) {
92 return (GET_PRIVATE(*zrf_ptr, m).reterr == kPglRetEof);
93 }
94
95 HEADER_INLINE const char* zsterror(const zstRFILE* zrf_ptr) {
96 return GET_PRIVATE(*zrf_ptr, m).errmsg;
97 }
98
99 HEADER_INLINE PglErr ZstRfileErrcode(const zstRFILE* zrf_ptr) {
100 const PglErr reterr = GET_PRIVATE(*zrf_ptr, m).reterr;
101 if (reterr == kPglRetEof) {
102 return kPglRetSuccess;
103 }
104 return reterr;
105 }
106
107 // If you need clearerr functionality, call CleanupZstRfile(). (Could provide
108 // a ZstRewindAndClearerr function too. Don't think it's worth the trouble to
109 // support any other clearerr use patterns, though.)
110
111 // Ok to pass reterrp == nullptr.
112 // Returns nonzero iff file-close fails, and either reterrp == nullptr or
113 // *reterrp == kPglRetSuccess. In the latter case, *reterrp is set to
114 // kPglRetReadFail. (Note that this does *not* retrieve the existing
115 // zrfp->reterr value; caller is responsible for checking ZstRfileErrcode()
116 // first when they care.)
117 BoolErr CleanupZstRfile(zstRFILE* zrf_ptr, PglErr* reterrp);
118
119 #ifdef __cplusplus
120 } // namespace plink2
121 #endif
122
123 #endif // __PLINK2_ZSTFILE_H__