From 834a3f2151dd8738a1f878489f6207664c4af5aa Mon Sep 17 00:00:00 2001 From: Benjamin Barenblat Date: Sun, 14 Jul 2013 17:14:53 -0700 Subject: Imported Upstream version 1.1.1 --- src/bencode.h | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/bencode.h (limited to 'src/bencode.h') diff --git a/src/bencode.h b/src/bencode.h new file mode 100644 index 0000000..23fc51a --- /dev/null +++ b/src/bencode.h @@ -0,0 +1,66 @@ +/* + * C implementation of a bencode decoder. + * This is the format defined by BitTorrent: + * http://wiki.theory.org/BitTorrentSpecification#bencoding + * + * The only external requirements are a few [standard] function calls and + * the gint64 type. Any sane system should provide all of these things. + * + * This is released into the public domain. + * Written by Mike Frysinger . + */ + +/* USAGE: + * - pass the string full of the bencoded data to be_decode() + * - parse the resulting tree however you like + * - call be_free() on the tree to release resources + */ + +#ifndef _BENCODE_H +#define _BENCODE_H + +#ifdef __cplusplus +extern "C" { +#endif + + typedef enum { + BE_STR, + BE_INT, + BE_LIST, + BE_DICT + } be_type; + + struct be_dict; + struct be_node; + +/* + * XXX: the "val" field of be_dict and be_node can be confusing ... + */ + + typedef struct be_dict { + char *key; + struct be_node *val; + } be_dict; + + typedef struct be_node { + be_type type; + union { + char *s; + gint64 i; + struct be_node **l; + struct be_dict *d; + } val; + } be_node; + + gint64 be_str_len(be_node * node); + be_node *be_decode(const char *bencode); + be_node *be_decoden(const char *bencode, gint64 bencode_len); + void be_free(be_node * node); + void be_dump(be_node * node); + be_node *be_dict_find(be_node * node, char *key, int type); + gboolean be_validate_node(be_node * node, gint type); + +#ifdef __cplusplus +} +#endif +#endif -- cgit v1.2.3