SeqAn3  3.0.3
The Modern C++ library for sequence analysis.
magic_header.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2021, 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 <seqan3/std/span>
17 #include <string>
18 #include <seqan3/std/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
35  {
36  {"gz"}
37  };
38 
40  static constexpr std::array<char, 3> magic_header{'\x1f', '\x8b', '\x08'};
41 };
42 
45 struct bz2_compression
46 {
48  static inline std::vector<std::string> file_extensions
49  {
50  {"bz2"}
51  };
52 
54  static constexpr std::array<char, 3> magic_header{'\x42', '\x5a', '\x68'};
55 };
56 
59 struct zstd_compression
60 {
62  static inline std::vector<std::string> file_extensions
63  {
64  {"zst"}
65  };
66 
68  static constexpr std::array<char, 4> magic_header{'\x28', '\xb5', '\x2f', '\xfd'};
69 };
70 
73 struct bgzf_compression
74 {
76  static inline std::vector<std::string> file_extensions
77  {
78  {"bgzf"}
79  };
80 
82  static constexpr std::array<char, 18> magic_header
83  {
84  // ID1 ID2 CM
85  gz_compression::magic_header[0], gz_compression::magic_header[1], gz_compression::magic_header[2],
86  // FLG [MTIME ] XFL OS [XLEN ]
87  '\x04', '\x00', '\x00', '\x00', '\x00', '\x00', '\xff', '\x06', '\x00',
88  // B C [SLEN ] [BSIZE ]
89  '\x42', '\x43', '\x02', '\x00', '\x00', '\x00'
90  };
91 
96  template <typename char_t, size_t extend>
97  static bool validate_header(std::span<char_t, extend> header)
98  {
99  static_assert(seqan3::detail::weakly_equality_comparable_with<char_t, char>,
100  "The given char type of the span must be comparable with char.");
101 
102  return (header[0] == magic_header[0] && // GZ_ID1
103  header[1] == magic_header[1] && // GZ_ID2
104  header[2] == magic_header[2] && // GZ_CM
105  (header[3] & magic_header[3]) != 0 && // FLG_FEXTRA
106  to_little_endian(*reinterpret_cast<uint16_t const *>(&header[10])) == magic_header[10] && // BGZF_ID1
107  header[12] == magic_header[12] && // BGZF_ID2
108  header[13] == magic_header[13] && // BGZF_SLEN
109  to_little_endian(*reinterpret_cast<uint16_t const *>(&header[14])) == magic_header[14]); // BGZF_XLEN
110  }
111 };
112 
116 using compression_formats = pack_traits::drop_front<void
117  #if SEQAN3_HAS_ZLIB
118  , gz_compression
119  , bgzf_compression
120  #endif // SEQAN3_HAS_ZLIB
121  #if SEQAN3_HAS_BZIP2
122  , bz2_compression
123  #endif // SEQAN3_HAS_BZIP2
124  #if SEQAN3_HAS_ZSTD
125  , zstd_compression
126  #endif // SEQAN3_HAS_ZSTD
127  >;
128 
129 } // namespace seqan3::detail
Provides type traits for working with templates.
Provides concepts for core language types and relations that don't have concepts in C++20 (yet).
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:322
Provides std::span from the C++20 standard library.
Provides utility functions for bit twiddling.
Provides C++20 additions to the type_traits header.
Provides various traits for template packs.