diff --git a/daemon/bencode.h b/daemon/bencode.h index c430e7ceb..35043ba36 100644 --- a/daemon/bencode.h +++ b/daemon/bencode.h @@ -7,6 +7,7 @@ #if defined(PKG_MALLOC) || defined(pkg_malloc) /* kamailio */ # include "../../mem/mem.h" +# include "../../str.h" # ifndef BENCODE_MALLOC # define BENCODE_MALLOC pkg_malloc # define BENCODE_FREE pkg_free @@ -87,6 +88,9 @@ bencode_item_t *bencode_dictionary_add_len(bencode_item_t *dict, const char *key /* Convenience function to add a string value to a dictionary */ static inline bencode_item_t *bencode_dictionary_add_string(bencode_item_t *dict, const char *key, const char *val); +/* Ditto, but for a "str" object */ +static inline bencode_item_t *bencode_dictionary_add_str(bencode_item_t *dict, const char *key, const str *val); + /* Convenience function to add an integer value to a dictionary */ static inline bencode_item_t *bencode_dictionary_add_integer(bencode_item_t *dict, const char *key, long long int val); @@ -108,6 +112,10 @@ bencode_item_t *bencode_string_len(bencode_buffer_t *buf, const char *s, int len * to bencode_string_len(). */ static inline bencode_item_t *bencode_string(bencode_buffer_t *buf, const char *s); +/* Creates a new byte-string object from a "str" object. The string does not have to be null- + * terminated. */ +static inline bencode_item_t *bencode_str(bencode_buffer_t *buf, const str *s); + /* Creates a new integer object. Returns NULL if no memory could be allocated. */ bencode_item_t *bencode_integer(bencode_buffer_t *buf, long long int i); @@ -208,13 +216,18 @@ bencode_item_t *bencode_dictionary_get_len(bencode_item_t *dict, const char *key * valid pointer. The returned string will be valid until dict's bencode_buffer_t object is destroyed. */ static inline char *bencode_dictionary_get_string(bencode_item_t *dict, const char *key, int *len); -/* Identical to bencode_dictionary_get_string() but fills in a "str" struct. Returns str->s. */ +/* Identical to bencode_dictionary_get_string() but fills in a "str" struct. Returns str->s, which + * may be NULL. */ static inline char *bencode_dictionary_get_str(bencode_item_t *dict, const char *key, str *str); /* Identical to bencode_dictionary_get() but returns the string in a newly allocated buffer (using the * BENCODE_MALLOC function), which remains valid even after bencode_buffer_t is destroyed. */ static inline char *bencode_dictionary_get_string_dup(bencode_item_t *dict, const char *key, int *len); +/* Combines bencode_dictionary_get_str() and bencode_dictionary_get_string_dup(). Fills in a "str" + * struct, but copies the string into a newly allocated buffer. Returns str->s. */ +static inline char *bencode_dictionary_get_str_dup(bencode_item_t *dict, const char *key, str *str); + /* Identical to bencode_dictionary_get_string() but expects an integer object. The parameter "defval" * specified which value should be returned if the key is not found or if the value is not an integer. */ static inline long long int bencode_dictionary_get_integer(bencode_item_t *dict, const char *key, long long int defval); @@ -229,6 +242,10 @@ static inline bencode_item_t *bencode_string(bencode_buffer_t *buf, const char * return bencode_string_len(buf, s, strlen(s)); } +static inline bencode_item_t *bencode_str(bencode_buffer_t *buf, const str *s) { + return bencode_string_len(buf, s->s, s->len); +} + static inline bencode_item_t *bencode_dictionary_add(bencode_item_t *dict, const char *key, bencode_item_t *val) { if (!key) return NULL; @@ -241,6 +258,12 @@ static inline bencode_item_t *bencode_dictionary_add_string(bencode_item_t *dict return bencode_dictionary_add(dict, key, bencode_string(dict->buffer, val)); } +static inline bencode_item_t *bencode_dictionary_add_str(bencode_item_t *dict, const char *key, const str *val) { + if (!val) + return NULL; + return bencode_dictionary_add(dict, key, bencode_str(dict->buffer, val)); +} + static inline bencode_item_t *bencode_dictionary_add_integer(bencode_item_t *dict, const char *key, long long int val) { return bencode_dictionary_add(dict, key, bencode_integer(dict->buffer, val)); } @@ -282,6 +305,11 @@ static inline char *bencode_dictionary_get_string_dup(bencode_item_t *dict, cons return ret; } +static inline char *bencode_dictionary_get_str_dup(bencode_item_t *dict, const char *key, str *str) { + str->s = bencode_dictionary_get_string_dup(dict, key, &str->len); + return str->s; +} + static inline long long int bencode_dictionary_get_integer(bencode_item_t *dict, const char *key, long long int defval) { bencode_item_t *val; val = bencode_dictionary_get(dict, key);