BinaryParser v1.0.0
Parse binary files with ease.
Loading...
Searching...
No Matches
BinaryParser

Release tag  GitHub Stars  License  Top lang  Commit count this past month

Tests on C99  Tests on C11  Tests on C17  Tests on C23

BinaryParser logo

Binary file parsing, made easy and straight-forward.

We all know that there are fancy text-based data files, like JSON, TOML, YAML, XML or tons more that exist... But you'll have to work with binary files at some point. This is where BinaryParser comes in!

What does it do?

BinaryParser, is a simple header-only library written in C. It allows you to read raw binary files by specifying the structure of said binary file. This allows you to quickly read binary files without having to dabble with file IO. BinaryParser lets you read data type such as ASCII strings, UTF8 strings, integers, floats, arbitrary bytes, UNIX timestamp, booleans.

BinaryParser also allows you to run a function on a specified byte range without saving it, or running a function on each byte in a specified range, allowing you to operate on it on the fly or even define how BinaryParser should store it. You may also define callbacks for malloc, calloc, realloc and free that BinaryParser will use over the default definitions.

How to use

BinaryParser is coded in a nature similar to Sean Barretts' stb libaries, and all you need to do is to put this:

#define BPARSER_IMPLEMENTATION
Parser for binary files.

In one of your C files that include the header, like your main.c or possibly even a standalone C file that you can convert into a static library and link your program against it. Whichever method you choose, that define must be in one AND ONLY IN ONE C file.

BinaryParser also stores its' version information in version.h.

Examples

Enough talking! Here are some examples... Each one also includes a link to the full example program, which also has much more safety checks.

Reading numbers

@ [./examples/001-Reading-Numbers/001-Reading-Numbers.c]()

// Make the parser.
bparser_parser_t* MyParser = bparser_init(NULL, NULL, NULL, NULL);
// What to read.
bparser_read_double(MyParser, "Double1");
bparser_read_uint64(MyParser, "Big");
bparser_read_double(MyParser, "Double2");
// Read our file `example.bin`
bparser_fread(MyParser, "./example.bin");
// Get the parsed data.
double* Double1 = (double*)bparser_get(MyParser, "Double1");
uint64_t* Big = (uint64_t*)bparser_get(MyParser, "Big");
double* Double2 = (double*)bparser_get(MyParser, "Double2");
// Print them:
if (Double1)
{
printf("Double1 = %.1lf\n", *Double1);
}
if (Big)
{
printf("Big = %lld\n", *Big);
}
if (Double2)
{
printf("Double2 = %.1lf\n", *Double2);
}
// Free the parser.
bparser_destroy(MyParser);
#define bparser_read_uint64(Parser, Name)
Shortcut for reading an uint64.
Definition parser.h:633
bparser_parser_t * bparser_init(bparser_malloc_like_f MallocLikeFunc, bparser_calloc_like_f CallocLikeFunc, bparser_realloc_like_f ReallocLikeFunc, bparser_free_like_f FreeLikeFunc)
Create a new parser instance.
void bparser_destroy(bparser_parser_t *MParser)
Destroy a parser instance.
char bparser_fread(bparser_parser_t *MParser, const char *Filename)
Read file and parse according to saved rules.
char bparser_read_double(bparser_parser_t *MParser, const char *Name)
Read a double from the file.
void * bparser_get(const bparser_parser_t *MParser, const char *Name)
Get parsed data.
The parser.
Definition parser.h:832

UTF8 & Unicode

@ [./examples/004-Utf8-And-Unicode/004-Utf8-And-Unicode.c]()

// Make the parser.
bparser_parser_t* MyParser = bparser_init(NULL, NULL, NULL, NULL);
// What to read.
bparser_read_utf8_null_str(MyParser, "Utf");
bparser_read_utf8_fixed_len_str(MyParser, "5len", 5);
// Read our file `example.bin`
bparser_fread(MyParser, "./example.bin");
// Get the parsed data.
const char* Utf = (const char*)bparser_get(MyParser, "Utf");
const char* Fivelen = (const char*)bparser_get(MyParser, "5len");
// Print them:
if (Utf)
{
printf("Utf = %s\n", Utf);
}
if (Fivelen)
{
printf("5len = %s\n", Fivelen);
}
// Free the parser.
bparser_destroy(MyParser);
char bparser_read_utf8_null_str(bparser_parser_t *MParser, const char *Name)
Read an UTF8 null terminated string from the file.
char bparser_read_utf8_fixed_len_str(bparser_parser_t *MParser, const char *Name, size_t Length)
Read an UTF8 string of specified length from the file.

Reading Unix Time

@ [./examples/012-Unix-Time/012-Unix-Time.c]()

// Create the parser.
bparser_parser_t* MyParser = bparser_init(NULL, NULL, NULL, NULL);
// What to read.
bparser_read_unix_time(MyParser, "TheMillenium");
bparser_read_unix_time(MyParser, "StartDateBP");
// Parse.
bparser_fread(MyParser, "./example.bin");
// Get the data.
time_t* _TheMillenium = (time_t*)bparser_get(MyParser, "TheMillenium");
time_t* _StartDateBP = (time_t*)bparser_get(MyParser, "StartDateBP");
// Print it.
if (_TheMillenium)
{
const struct tm* Time = gmtime(_TheMillenium);
char Formatted[64];
strftime(Formatted, 64, "%d %a %B %Y @ %H:%M:%S", Time);
puts(Formatted);
}
if (_StartDateBP)
{
const struct tm* Time = gmtime(_StartDateBP);
char Formatted[64];
strftime(Formatted, 64, "%d %a %B %Y @ %H:%M:%S", Time);
puts(Formatted);
}
// Free the parser.
bparser_destroy(MyParser);
char bparser_read_unix_time(bparser_parser_t *MParser, const char *Name)
Read a 4 byte unsigned integer number and parse it as an Unix timestamp.