diff --git a/daemon/bencode.c b/daemon/bencode.c index 66a5e8c..23c1493 100644 --- a/daemon/bencode.c +++ b/daemon/bencode.c @@ -212,6 +212,14 @@ static bencode_item_t *__bencode_string_alloc(bencode_buffer_t *buf, const void return ret; } +bencode_item_t *bencode_string_len_dup(bencode_buffer_t *buf, const char *s, int len) { + char *sd = __bencode_alloc(buf, len); + if (!sd) + return NULL; + memcpy(sd, s, len); + return bencode_string_len(buf, sd, len); +} + bencode_item_t *bencode_string_len(bencode_buffer_t *buf, const char *s, int len) { return __bencode_string_alloc(buf, s, len, len, 1, BENCODE_STRING); } diff --git a/daemon/bencode.h b/daemon/bencode.h index fdc270e..1a1ff77 100644 --- a/daemon/bencode.h +++ b/daemon/bencode.h @@ -150,6 +150,11 @@ 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); +/* Identical to the above two functions, but copies the string into the bencode_buffer_t object. + * Thus, the given string doesn't have to remain valid and accessible afterwards. */ +bencode_item_t *bencode_string_len_dup(bencode_buffer_t *buf, const char *s, int len); +static inline bencode_item_t *bencode_string_dup(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); @@ -336,6 +341,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_string_dup(bencode_buffer_t *buf, const char *s) { + return bencode_string_len_dup(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); }