This library provides functions for encoding and decoding data using the B-encode format, commonly used in torrent files. It supports integers, strings, lists, and dictionaries as defined in the Bencode specification.
Features
- Encode and decode B-encoded data.
- Handle integers, strings, lists, and dictionaries.
- Read Bencoded data from files or strings.
- Print Bencoded data in a JSON-like format.
Clone the repository and compile the source files:
git clone https://github.com/afraz98/libbencode.git
cd libbencode
mkdir build
cd build
cmake ..
makeYou can decode B-encoded data from a file or a string.
To decode from a file, do the following:
#include "bencode.h"
int main() {
    bencode_value *val = bencode_decode_file("example.torrent");
    if (val) {
        print_bencode_value(val, 0);
        free_bencode_value(val);
    }
    return 0;
}To decode from a string, do the following:
#include "bencode.h"
int main() {
    const char *input = "d3:keyi42ee";
    bencode_value *val = bencode_decode_str(input);
    if (val) {
        print_bencode_value(val, 0);
        free_bencode_value(val);
    }
    return 0;
}You can encode data into B-encode format using the bencode_encode function.
#include "bencode.h"
int main() {
    // Create a b-encode integer
    bencode_value int_val = { .type = B_ENCODED_INTEGER, .integer = 42 };
    char output[256];
    size_t offset = 0;
    bencode_encode(&int_val, output, &offset);
    printf("Encoded B-encode: %s\n", output);
    return 0;
}A set of tests is provided to validate the functionality of the library. Compile and run the test suite as follows:
mkdir build
cd build
cmake ..
./test_bencodeRepresents a B-encode value.
typedef struct bencode_value_struct {
    bencode_type type;
    union {
        long long integer;
        struct {
            char* string;
            int strlen;
        } string;
        
        struct {
            struct bencode_value_struct** list_val;
            int llen;
        } list;
        
        struct {
            struct bencode_value_struct** keys;
            struct bencode_value_struct** values;
            int dlen;
        } dict;
    };
} bencode_value;Decodes B-encoded data from a file.
filename: Path to the file containing B-encoded data.
Returns a pointer to a bencode_value structure.
Decodes B-encoded data from a string.
input_str: The input string containing B-encoded data.
Returns a pointer to a bencode_value structure.
Encodes a bencode_value structure into B-encode format.
value: The bencode_value to encode.
output: The output buffer where the encoded data will be written.
offset: Pointer to the current offset in the output buffer.
Prints a bencode_value structure in a JSON-like format.
value: The bencode_value to print.
indent: The current indentation level for pretty printing.
Frees the memory allocated for a bencode_value structure.
obj: The bencode_value to free.
This project is licensed under the MIT License. See the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request with your improvements.
This library implements by the B-encode format used in torrent files. A description of this protocol is provided here.