cpp-filetype
 
Loading...
Searching...
No Matches
filetype.hpp File Reference

Main header file for the cpp-filetype library. More...

#include <array>
#include <cstdint>
#include <string>
#include <string_view>
#include <vector>
#include "types.hpp"

Go to the source code of this file.

Functions

bool filetype::is_valid_buffer (const std::vector< uint8_t > &bytes, size_t min_size=8)
 Validate if a buffer has sufficient data for type detection.
 
bool filetype::match_magic (const std::vector< uint8_t > &bytes, const uint8_t *magic, size_t magic_size, size_t offset=0)
 Compare bytes with a magic number pattern.
 
template<size_t N>
bool filetype::match_magic (const std::vector< uint8_t > &bytes, const std::array< uint8_t, N > &magic, size_t offset=0)
 Template version of match_magic for std::array magic numbers.
 
bool filetype::is_type (const std::vector< uint8_t > &bytes, const uint8_t *magic, size_t magic_size, size_t offset=0)
 Check if the file type matches a specific format.
 
template<size_t N>
bool filetype::is_type (const std::vector< uint8_t > &bytes, const std::array< uint8_t, N > &magic, size_t offset=0)
 Template version of is_type for std::array magic numbers.
 
const Typefiletype::match (const std::vector< uint8_t > &bytes)
 Detect file type from a byte buffer.
 
const Typefiletype::match_file (std::string_view filepath, size_t max_read_size=8192)
 Detect file type from a file path.
 
bool filetype::is (const std::vector< uint8_t > &bytes, const Type &type)
 Check if file has a specific type.
 
bool filetype::is_image (const std::vector< uint8_t > &bytes)
 Check if file is an image.
 
bool filetype::is_document (const std::vector< uint8_t > &bytes)
 Check if file is a document.
 
bool filetype::is_archive (const std::vector< uint8_t > &bytes)
 Check if file is an archive.
 
bool filetype::is_audio (const std::vector< uint8_t > &bytes)
 Check if file is an audio file.
 
bool filetype::is_video (const std::vector< uint8_t > &bytes)
 Check if file is a video file.
 
const Typefiletype::matcher::match_image (const std::vector< uint8_t > &bytes)
 Match image file types.
 
const Typefiletype::matcher::match_document (const std::vector< uint8_t > &bytes)
 Match document file types.
 
const Typefiletype::matcher::match_archive (const std::vector< uint8_t > &bytes)
 Match archive file types.
 
const Typefiletype::matcher::match_audio (const std::vector< uint8_t > &bytes)
 Match audio file types.
 
const Typefiletype::matcher::match_video (const std::vector< uint8_t > &bytes)
 Match video file types.
 

Detailed Description

Main header file for the cpp-filetype library.

This library provides functionality to detect file types based on their magic numbers. Supported file types include images, documents, archives, audio, and video formats.

Function Documentation

◆ is()

bool filetype::is ( const std::vector< uint8_t > & bytes,
const Type & type )

Check if file has a specific type.

Parameters
bytesBuffer containing file data.
typeType to check against.
Returns
true if file matches the given type.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ is_archive()

bool filetype::is_archive ( const std::vector< uint8_t > & bytes)

Check if file is an archive.

Parameters
bytesBuffer containing file data.
Returns
true if file is an archive.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ is_audio()

bool filetype::is_audio ( const std::vector< uint8_t > & bytes)

Check if file is an audio file.

Parameters
bytesBuffer containing file data.
Returns
true if file is an audio file.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ is_document()

bool filetype::is_document ( const std::vector< uint8_t > & bytes)

Check if file is a document.

Parameters
bytesBuffer containing file data.
Returns
true if file is a document.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ is_image()

bool filetype::is_image ( const std::vector< uint8_t > & bytes)

Check if file is an image.

Parameters
bytesBuffer containing file data.
Returns
true if file is an image.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ is_type() [1/2]

template<size_t N>
bool filetype::is_type ( const std::vector< uint8_t > & bytes,
const std::array< uint8_t, N > & magic,
size_t offset = 0 )

Template version of is_type for std::array magic numbers.

Template Parameters
NSize of the magic number array.
Parameters
bytesBuffer containing file data.
magicArray containing the magic number sequence.
offsetOffset where the magic number should appear (default: 0).
Returns
true if the file matches the specified format.

◆ is_type() [2/2]

bool filetype::is_type ( const std::vector< uint8_t > & bytes,
const uint8_t * magic,
size_t magic_size,
size_t offset = 0 )

Check if the file type matches a specific format.

Parameters
bytesBuffer containing file data.
magicMagic number sequence to match.
magic_sizeSize of the magic number sequence.
offsetOffset where the magic number should appear (default: 0).
Returns
true if the file matches the specified format.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ is_valid_buffer()

bool filetype::is_valid_buffer ( const std::vector< uint8_t > & bytes,
size_t min_size = 8 )

Validate if a buffer has sufficient data for type detection.

Parameters
bytesInput buffer to validate.
min_sizeMinimum required size for the buffer (default: 8 bytes).
Returns
true if buffer is valid and has sufficient size.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ is_video()

bool filetype::is_video ( const std::vector< uint8_t > & bytes)

Check if file is a video file.

Parameters
bytesBuffer containing file data.
Returns
true if file is a video file.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ match()

const Type * filetype::match ( const std::vector< uint8_t > & bytes)

Detect file type from a byte buffer.

This function attempts to detect the file type by comparing the buffer's contents with known magic numbers of various file formats.

Parameters
bytesBuffer containing the file data to analyze.
Returns
Pointer to the detected file type, or nullptr if type could not be determined.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ match_archive()

const Type * filetype::matcher::match_archive ( const std::vector< uint8_t > & bytes)

Match archive file types.

Parameters
bytesBuffer containing file data.
Returns
Pointer to the detected archive type, or nullptr if not an archive.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ match_audio()

const Type * filetype::matcher::match_audio ( const std::vector< uint8_t > & bytes)

Match audio file types.

Parameters
bytesBuffer containing file data.
Returns
Pointer to the detected audio type, or nullptr if not an audio file.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ match_document()

const Type * filetype::matcher::match_document ( const std::vector< uint8_t > & bytes)

Match document file types.

Parameters
bytesBuffer containing file data.
Returns
Pointer to the detected document type, or nullptr if not a document.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ match_file()

const Type * filetype::match_file ( std::string_view filepath,
size_t max_read_size = 8192 )

Detect file type from a file path.

This function reads the beginning of the file and attempts to detect its type by comparing with known magic numbers.

Parameters
filepathPath to the file to analyze.
max_read_sizeMaximum number of bytes to read from the file (default: 8192).
Returns
Pointer to the detected file type, or nullptr if type could not be determined.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ match_image()

const Type * filetype::matcher::match_image ( const std::vector< uint8_t > & bytes)

Match image file types.

Parameters
bytesBuffer containing file data.
Returns
Pointer to the detected image type, or nullptr if not an image.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ match_magic() [1/2]

template<size_t N>
bool filetype::match_magic ( const std::vector< uint8_t > & bytes,
const std::array< uint8_t, N > & magic,
size_t offset = 0 )

Template version of match_magic for std::array magic numbers.

Template Parameters
NSize of the magic number array.
Parameters
bytesBuffer containing file data.
magicArray containing the magic number sequence.
offsetOffset in bytes where to start matching (default: 0).
Returns
true if bytes match the magic number pattern.

◆ match_magic() [2/2]

bool filetype::match_magic ( const std::vector< uint8_t > & bytes,
const uint8_t * magic,
size_t magic_size,
size_t offset = 0 )

Compare bytes with a magic number pattern.

Parameters
bytesBuffer containing file data.
magicPointer to magic number sequence.
magic_sizeLength of the magic number sequence.
offsetOffset in bytes where to start matching (default: 0).
Returns
true if bytes match the magic number pattern.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.

◆ match_video()

const Type * filetype::matcher::match_video ( const std::vector< uint8_t > & bytes)

Match video file types.

Parameters
bytesBuffer containing file data.
Returns
Pointer to the detected video type, or nullptr if not a video file.
Examples
/Users/agent-hellboy/cpp-filetype/include/filetype/filetype.hpp.