libdht
bencode.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 naturalpolice
3  * SPDX-License-Identifier: MIT
4  *
5  * Licensed under the MIT License (see LICENSE).
6  */
7 
17 #ifndef DHT_BENCODE_H_
18 #define DHT_BENCODE_H_
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 #include <stdio.h>
25 
32 struct bvalue {
36  enum {
37  BVALUE_INTEGER,
38  BVALUE_STRING,
39  BVALUE_LIST,
40  BVALUE_DICTIONARY,
41  } type;
45  union {
49  long long int i;
53  struct {
54  unsigned char *bytes;
55  size_t len;
56  } s;
60  struct {
61  struct bvalue **array;
62  size_t len;
63  } l;
67  struct {
68  char **key;
69  struct bvalue **val;
70  size_t len;
71  } d;
72  };
73 };
74 
82 struct bvalue *bvalue_new_dict(void);
90 struct bvalue *bvalue_new_list(void);
99 struct bvalue *bvalue_new_integer(long long int i);
111 struct bvalue *bvalue_new_string(const unsigned char *s, size_t len);
119 struct bvalue *bvalue_copy(const struct bvalue *val);
127 void bvalue_free(struct bvalue *val);
128 
139 int bvalue_list_append(struct bvalue *list, struct bvalue *val);
152 int bvalue_dict_set(struct bvalue *dict, const char *key, struct bvalue *val);
153 
164 const struct bvalue *bvalue_dict_get(const struct bvalue *dict, const char *key);
175 const struct bvalue *bvalue_list_get(const struct bvalue *list, size_t pos);
191 const unsigned char *bvalue_string(const struct bvalue *val, size_t *len);
202 int bvalue_integer(const struct bvalue *val, int *intval);
213 int bvalue_integer_l(const struct bvalue *val, long int *intval);
224 int bvalue_integer_ll(const struct bvalue *val, long long int *intval);
225 
232 struct bvalue *bdecode_file(FILE *stream);
240 int bencode_file(const struct bvalue *val, FILE *stream);
241 
249 struct bvalue *bdecode_buf(const unsigned char *buf, size_t len);
259 int bencode_buf(const struct bvalue *val, unsigned char *buf, size_t len);
272 int bencode_buf_alloc(const struct bvalue *val, unsigned char **bufp);
273 
274 #ifdef __cplusplus
275 }
276 #endif
277 
278 #endif /* BENCODE_H_ */
int bvalue_list_append(struct bvalue *list, struct bvalue *val)
Append value to a list.
struct bvalue * bvalue_new_integer(long long int i)
Allocate an integer value.
unsigned char * bytes
string pointer (zero-terminated)
Definition: bencode.h:54
long long int i
Integer value.
Definition: bencode.h:49
struct bvalue ** array
array of elements
Definition: bencode.h:61
struct bvalue::@1::@4 l
List value.
void bvalue_free(struct bvalue *val)
Free bencoding value.
enum bvalue::@0 type
Type of the value contained.
struct bvalue * bvalue_new_string(const unsigned char *s, size_t len)
Allocate a string value.
size_t len
length of the string
Definition: bencode.h:55
struct bvalue * bvalue_new_list(void)
Allocate a list value.
struct bvalue * bdecode_file(FILE *stream)
Parse bencoded file.
int bencode_buf_alloc(const struct bvalue *val, unsigned char **bufp)
Allocate and bencode value to a string buffer.
struct bvalue * bvalue_new_dict(void)
Allocate a dictionary value.
char ** key
array of keys (in lexicographical order)
Definition: bencode.h:68
bencoding value.
Definition: bencode.h:32
struct bvalue::@1::@5 d
Dictionary value.
const unsigned char * bvalue_string(const struct bvalue *val, size_t *len)
Get string value.
int bvalue_integer_ll(const struct bvalue *val, long long int *intval)
Get integer (long long int) value.
struct bvalue * bvalue_copy(const struct bvalue *val)
Deep copy bencoding value.
struct bvalue ** val
array of values
Definition: bencode.h:69
int bencode_buf(const struct bvalue *val, unsigned char *buf, size_t len)
bencode value to a string buffer.
int bvalue_integer_l(const struct bvalue *val, long int *intval)
Get integer (long int) value.
int bvalue_dict_set(struct bvalue *dict, const char *key, struct bvalue *val)
Add a key-value pair to a dictionary.
const struct bvalue * bvalue_dict_get(const struct bvalue *dict, const char *key)
Get dictionary value.
const struct bvalue * bvalue_list_get(const struct bvalue *list, size_t pos)
Get list value.
struct bvalue * bdecode_buf(const unsigned char *buf, size_t len)
Parse bencoded string buffer.
int bvalue_integer(const struct bvalue *val, int *intval)
Get integer (int) value.
int bencode_file(const struct bvalue *val, FILE *stream)
bencode value to file.
struct bvalue::@1::@3 s
String value.