byteme
C++ wrappers for buffered inputs
Loading...
Searching...
No Matches
magic_numbers.hpp
Go to the documentation of this file.
1#ifndef BYTEME_MAGIC_NUMBERS_HPP
2#define BYTEME_MAGIC_NUMBERS_HPP
3
4#include <cstddef>
5
6#include "SelfClosingFILE.hpp"
7
13namespace byteme {
14
20inline bool is_zlib(const unsigned char* buffer, std::size_t n) {
21 if (n >= 2 && buffer[0] == 0x78) {
22 auto level = buffer[1];
23 // From https://en.wikipedia.org/wiki/List_of_file_signatures
24 if (level == 0x01 || level == 0x5e || level == 0x9c || level == 0xda || level == 0x20 || level == 0x7d || level == 0xbb || level == 0xf9) {
25 return true;
26 }
27 }
28 return false;
29}
30
36inline bool is_gzip(const unsigned char* buffer, std::size_t n) {
37 // ommitting the 0x08 requirement as Gzip could be used with non-DEFLATE algorithms.
38 return (n >= 2 && buffer[0] == 0x1f && buffer[1] == 0x8b);
39}
40
45inline bool is_gzip(const char* path) {
46 SelfClosingFILE file(path, "rb");
47 unsigned char header[3];
48 auto read = std::fread(header, sizeof(unsigned char), 3, file.handle);
49 return is_gzip(header, read);
50}
51
52}
53
54#endif
Simple byte readers and writers.
bool is_gzip(const unsigned char *buffer, std::size_t n)
Definition magic_numbers.hpp:36
bool is_zlib(const unsigned char *buffer, std::size_t n)
Definition magic_numbers.hpp:20