byteme
Read/write bytes from various sources
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
46inline bool is_zlib_or_gzip(const unsigned char* buffer, std::size_t n) {
47 return is_zlib(buffer, n) || is_gzip(buffer, n);
48}
49
54inline bool is_gzip(const char* path) {
55 SelfClosingFILE file(path, "rb");
56 unsigned char header[3];
57 auto read = std::fread(header, sizeof(unsigned char), 3, file.handle);
58 return is_gzip(header, read);
59}
60
66enum class ZlibCompressionMode : char { DEFLATE, ZLIB, GZIP };
67
68}
69
70#endif
Simple byte readers and writers.
Definition BufferedReader.hpp:21
ZlibCompressionMode
Definition magic_numbers.hpp:66
bool is_zlib_or_gzip(const unsigned char *buffer, std::size_t n)
Definition magic_numbers.hpp:46
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