SeqAn3  3.2.0-rc.1
The Modern C++ library for sequence analysis.
magic_header.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2022, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
15 #include <array>
16 #include <span>
17 #include <string>
18 #include <type_traits>
19 #include <vector>
20 
25 
26 namespace seqan3::detail
27 {
28 
31 struct gz_compression
32 {
34  static inline std::vector<std::string> file_extensions{{"gz"}};
35 
37  static constexpr std::array<char, 3> magic_header{'\x1f', '\x8b', '\x08'};
38 };
39 
42 struct bz2_compression
43 {
45  static inline std::vector<std::string> file_extensions{{"bz2"}};
46 
48  static constexpr std::array<char, 3> magic_header{'\x42', '\x5a', '\x68'};
49 };
50 
53 struct zstd_compression
54 {
56  static inline std::vector<std::string> file_extensions{{"zst"}};
57 
59  static constexpr std::array<char, 4> magic_header{'\x28', '\xb5', '\x2f', '\xfd'};
60 };
61 
64 struct bgzf_compression
65 {
67  static inline std::vector<std::string> file_extensions{{"bgzf"}};
68 
70  static constexpr std::array<char, 18> magic_header{
71  // ID1 ID2 CM
72  gz_compression::magic_header[0],
73  gz_compression::magic_header[1],
74  gz_compression::magic_header[2],
75  // FLG [MTIME ] XFL OS [XLEN ]
76  '\x04',
77  '\x00',
78  '\x00',
79  '\x00',
80  '\x00',
81  '\x00',
82  '\xff',
83  '\x06',
84  '\x00',
85  // B C [SLEN ] [BSIZE ]
86  '\x42',
87  '\x43',
88  '\x02',
89  '\x00',
90  '\x00',
91  '\x00'};
92 
97  template <typename char_t, size_t extend>
98  static bool validate_header(std::span<char_t, extend> header)
99  {
100  static_assert(std::equality_comparable_with<char_t, char>,
101  "The given char type of the span must be comparable with char.");
102 
103  return (header[0] == magic_header[0] && // GZ_ID1
104  header[1] == magic_header[1] && // GZ_ID2
105  header[2] == magic_header[2] && // GZ_CM
106  (header[3] & magic_header[3]) != 0 && // FLG_FEXTRA
107  to_little_endian(*reinterpret_cast<uint16_t const *>(&header[10])) == magic_header[10] && // BGZF_ID1
108  header[12] == magic_header[12] && // BGZF_ID2
109  header[13] == magic_header[13] && // BGZF_SLEN
110  to_little_endian(*reinterpret_cast<uint16_t const *>(&header[14])) == magic_header[14]); // BGZF_XLEN
111  }
112 };
113 
117 using compression_formats = pack_traits::drop_front<void
118 #if defined(SEQAN3_HAS_ZLIB)
119  ,
120  gz_compression,
121  bgzf_compression
122 #endif // defined(SEQAN3_HAS_ZLIB)
123 #if defined(SEQAN3_HAS_BZIP2)
124  ,
125  bz2_compression
126 #endif // defined(SEQAN3_HAS_BZIP2)
127 #if SEQAN3_HAS_ZSTD
128  ,
129  zstd_compression
130 #endif // SEQAN3_HAS_ZSTD
131  >;
132 
133 } // namespace seqan3::detail
Provides std::span from the C++20 standard library.
Provides type traits for working with templates.
Provides utility functions for bit twiddling.
Provides various traits for template packs.
Provides concepts that do not have equivalents in C++20.