Skip to content

Repository files navigation

BinaryParser

Release tagGitHub StarsLicenseTop langCommit count this past month

Tests on C99Tests on C11Tests on C17Tests 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:

#defineBPARSER_IMPLEMENTATION#include<binaryparser/parser.h>

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);

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.constchar*Utf= (constchar*)bparser_get(MyParser, "Utf");
constchar*Fivelen= (constchar*)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);

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)
{
conststructtm*Time=gmtime(_TheMillenium);
charFormatted[64];
strftime(Formatted, 64, "%d %a %B %Y @ %H:%M:%S", Time);
puts(Formatted);
}
if (_StartDateBP)
{
conststructtm*Time=gmtime(_StartDateBP);
charFormatted[64];
strftime(Formatted, 64, "%d %a %B %Y @ %H:%M:%S", Time);
puts(Formatted);
}
// Free the parser.bparser_destroy(MyParser);