SeqAn3  3.0.1
The Modern C++ library for sequence analysis.
magic_header.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 <string>
17 #include <vector>
18 
24 #include <seqan3/std/type_traits>
25 #include <seqan3/std/span>
26 
27 namespace seqan3::detail
28 {
29 
32 struct gz_compression
33 {
35  static inline std::vector<std::string> file_extensions
36  {
37  {"gz"}
38  };
39 
41  static constexpr std::array<char, 3> magic_header{'\x1f', '\x8b', '\x08'};
42 };
43 
46 struct bz2_compression
47 {
49  static inline std::vector<std::string> file_extensions
50  {
51  {"bz2"}
52  };
53 
55  static constexpr std::array<char, 3> magic_header{'\x42', '\x5a', '\x68'};
56 };
57 
60 struct zstd_compression
61 {
63  static inline std::vector<std::string> file_extensions
64  {
65  {"zst"}
66  };
67 
69  static constexpr std::array<char, 4> magic_header{'\x28', '\xb5', '\x2f', '\xfd'};
70 };
71 
74 struct bgzf_compression
75 {
77  static inline std::vector<std::string> file_extensions
78  {
79  {"bgzf"}
80  };
81 
83  static constexpr std::array<char, 18> magic_header
84  {
85  // ID1 ID2 CM
86  gz_compression::magic_header[0], gz_compression::magic_header[1], gz_compression::magic_header[2],
87  // FLG [MTIME ] XFL OS [XLEN ]
88  '\x04', '\x00', '\x00', '\x00', '\x00', '\x00', '\xff', '\x06', '\x00',
89  // B C [SLEN ] [BSIZE ]
90  '\x42', '\x43', '\x02', '\x00', '\x00', '\x00'
91  };
92 
97  template <typename char_t, ptrdiff_t extend>
98  static bool validate_header(std::span<char_t, extend> header)
99  {
100  static_assert(seqan3::detail::weakly_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 *>(&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 *>(&header[14])) == magic_header[14]); // BGZF_XLEN
111  }
112 };
113 
117 using compression_formats = pack_traits::drop_front<void
118  #if SEQAN3_HAS_ZLIB
119  , gz_compression
120  , bgzf_compression
121  #endif // SEQAN3_HAS_ZLIB
122  #if SEQAN3_HAS_BZIP2
123  , bz2_compression
124  #endif // SEQAN3_HAS_BZIP2
125  #if SEQAN3_HAS_ZSTD
126  , zstd_compression
127  #endif // SEQAN3_HAS_ZSTD
128  >;
129 
130 } // namespace seqan3::detail
bit_manipulation.hpp
Provides utility functions for bit twiddling.
span
Provides std::span from the C++20 standard library.
type_traits
Provides C++20 additions to the type_traits header.
vector
template_inspection.hpp
Provides seqan3::type_list and auxiliary type traits.
seqan3::pack_traits::drop_front
typename decltype(detail::drop_front< pack_t... >())::type drop_front
Return a seqan3::type_list of all the types in the type pack, except the first.
Definition: traits.hpp:287
core_language.hpp
Provides concepts for core language types and relations that don't have concepts in C++20 (yet).
array
traits.hpp
Provides traits for seqan3::type_list.
type_list.hpp
Provides seqan3::type_list.
string