libdht
|
Provides routines for parsing and formating bencoded data. More...
#include <stdio.h>
Go to the source code of this file.
Data Structures | |
struct | bvalue |
bencoding value. More... | |
Functions | |
struct bvalue * | bvalue_new_dict (void) |
Allocate a dictionary value. More... | |
struct bvalue * | bvalue_new_list (void) |
Allocate a list value. More... | |
struct bvalue * | bvalue_new_integer (long long int i) |
Allocate an integer value. More... | |
struct bvalue * | bvalue_new_string (const unsigned char *s, size_t len) |
Allocate a string value. More... | |
struct bvalue * | bvalue_copy (const struct bvalue *val) |
Deep copy bencoding value. More... | |
void | bvalue_free (struct bvalue *val) |
Free bencoding value. More... | |
int | bvalue_list_append (struct bvalue *list, struct bvalue *val) |
Append value to a list. More... | |
int | bvalue_dict_set (struct bvalue *dict, const char *key, struct bvalue *val) |
Add a key-value pair to a dictionary. More... | |
const struct bvalue * | bvalue_dict_get (const struct bvalue *dict, const char *key) |
Get dictionary value. More... | |
const struct bvalue * | bvalue_list_get (const struct bvalue *list, size_t pos) |
Get list value. More... | |
const unsigned char * | bvalue_string (const struct bvalue *val, size_t *len) |
Get string value. More... | |
int | bvalue_integer (const struct bvalue *val, int *intval) |
Get integer (int) value. More... | |
int | bvalue_integer_l (const struct bvalue *val, long int *intval) |
Get integer (long int) value. More... | |
int | bvalue_integer_ll (const struct bvalue *val, long long int *intval) |
Get integer (long long int) value. More... | |
struct bvalue * | bdecode_file (FILE *stream) |
Parse bencoded file. More... | |
int | bencode_file (const struct bvalue *val, FILE *stream) |
bencode value to file. More... | |
struct bvalue * | bdecode_buf (const unsigned char *buf, size_t len) |
Parse bencoded string buffer. More... | |
int | bencode_buf (const struct bvalue *val, unsigned char *buf, size_t len) |
bencode value to a string buffer. More... | |
int | bencode_buf_alloc (const struct bvalue *val, unsigned char **bufp) |
Allocate and bencode value to a string buffer. More... | |
Provides routines for parsing and formating bencoded data.
All BitTorrent specifications use a special data serialization format called bencoding. This API contains the tools necessary to parse and format bencoded data.
struct bvalue* bdecode_buf | ( | const unsigned char * | buf, |
size_t | len | ||
) |
Parse bencoded string buffer.
buf | string buffer to parse. |
len | Length to parse. |
struct bvalue* bdecode_file | ( | FILE * | stream | ) |
Parse bencoded file.
stream | Stream to parse (FILE pointer). |
int bencode_buf | ( | const struct bvalue * | val, |
unsigned char * | buf, | ||
size_t | len | ||
) |
bencode value to a string buffer.
val | Value to encode. |
buf | Memory buffer to write encoded data to. |
len | Length of the memory buffer. |
int bencode_buf_alloc | ( | const struct bvalue * | val, |
unsigned char ** | bufp | ||
) |
Allocate and bencode value to a string buffer.
This function is analog to bencode_buf, except it allocates a buffer large enough to hold the output. The buffered returned in bufp should be freed by the application when it is no longer needed.
val | Value to encode. |
bufp | Pointer to a location that will receive the address of the encoded data. |
int bencode_file | ( | const struct bvalue * | val, |
FILE * | stream | ||
) |
bencode value to file.
val | Value to encode. |
stream | Stream to write encoded data to. |
Deep copy bencoding value.
Recursively copy val an all of it's children.
val | The value to copy. |
Get dictionary value.
Lookup a value by key in a dictionary.
dict | The dictionary value to get the value from. Must be of type BVALUE_DICTIONARY. |
key | The key the value is set to in the dictionary. |
Add a key-value pair to a dictionary.
Set key key to value val in dictionary value dict. dict must be of type BVALUE_DICTIONARY. If the key is already set in dict, the old value is simply freed and replaced.
dict | The dictionary value to add the key-value pair to. |
key | The key to set. |
val | The value to corresponding to the key. |
void bvalue_free | ( | struct bvalue * | val | ) |
Free bencoding value.
Recursively free val and all of its children.
val | The value to free. |
int bvalue_integer | ( | const struct bvalue * | val, |
int * | intval | ||
) |
Get integer (int) value.
Returns the integer value of a value of type BVALUE_INTEGER.
val | The integer value. Must be of type BVALUE_INTEGER. |
intval | Pointer to an int that will receive the integer value. |
int bvalue_integer_l | ( | const struct bvalue * | val, |
long int * | intval | ||
) |
Get integer (long int) value.
Returns the integer value of a value of type BVALUE_INTEGER.
val | The integer value. Must be of type BVALUE_INTEGER. |
intval | Pointer to an int that will receive the integer value. |
int bvalue_integer_ll | ( | const struct bvalue * | val, |
long long int * | intval | ||
) |
Get integer (long long int) value.
Returns the integer value of a value of type BVALUE_INTEGER.
val | The integer value. Must be of type BVALUE_INTEGER. |
intval | Pointer to an int that will receive the integer value. |
Append value to a list.
Add val to the end of the list value list. list must be of type BVALUE_LIST.
list | The list value to append to. |
val | The value to append. |
Get list value.
Get a value by position in a list.
list | The list value to get the value from. Must be of type BVALUE_LIST. |
pos | The position of the value in the list. |
struct bvalue* bvalue_new_dict | ( | void | ) |
Allocate a dictionary value.
Builds a new empty dictionary value.
struct bvalue* bvalue_new_integer | ( | long long int | i | ) |
Allocate an integer value.
Builds a new integer value and set it to i.
i | Initial integer value. |
struct bvalue* bvalue_new_list | ( | void | ) |
Allocate a list value.
Builds a new empty list value.
struct bvalue* bvalue_new_string | ( | const unsigned char * | s, |
size_t | len | ||
) |
Allocate a string value.
Builds a new string value initially set to s. This function allocates its own copy of s, so the memory can be reused after the call. The internal string buffer is always null-terminated.
s | pointer to the initial string value. |
len | length (in bytes) of the intial string value. |
const unsigned char* bvalue_string | ( | const struct bvalue * | val, |
size_t * | len | ||
) |
Get string value.
Get C string pointer from a value of type BVALUE_STRING. The return value is a pointer to the value's own internal storage. The returned string is always null-terminated so it can be used in printf-like functions. However, since it is designed to also store binary (non-ASCII) data, it can contain embedded zeroes. This function will optionnaly return the length of the string in len if not NULL.
val | The string value. Must be of type BVALUE_STRING. |
len | Optional pointer to an integer that will receive the length of the string. |