From cc8ff45cc13220b7c8897d3eb9f1cb161fd0ff25 Mon Sep 17 00:00:00 2001 From: John Jones Date: Mon, 13 Feb 2017 14:28:06 -0500 Subject: [PATCH] Added a method to copy MultiAddresses --- include/multiaddr/multiaddr.h | 2 ++ multiaddr.c | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/include/multiaddr/multiaddr.h b/include/multiaddr/multiaddr.h index 9c00544..04dee0d 100644 --- a/include/multiaddr/multiaddr.h +++ b/include/multiaddr/multiaddr.h @@ -47,6 +47,8 @@ struct MultiAddress* multiaddress_new_from_string(const char* straddress); //Con void multiaddress_free(struct MultiAddress* in); +int multiaddress_copy(const struct MultiAddress* source, struct MultiAddress* destination); + int multiaddress_encapsulate(struct MultiAddress * result, char * string); int multiaddress_decapsulate(struct MultiAddress * result, char * srci); diff --git a/multiaddr.c b/multiaddr.c index bf861a6..463e113 100644 --- a/multiaddr.c +++ b/multiaddr.c @@ -91,6 +91,31 @@ void multiaddress_free(struct MultiAddress* in) { } } +/** + * Copy a multiaddress from one memory location to another + * @param in the source + * @param out the destination. NOTE: memory for out should be preallocated + * @returns true(1) on success, otherwise false(0) + */ +int multiaddress_copy(const struct MultiAddress* in, struct MultiAddress* out) { + if (in != NULL && out != NULL) { + // memory allocation + out->bytes = malloc(in->bsize); + if (out->bytes != NULL) { + out->string = malloc(strlen(in->string) + 1); + if (out->string != NULL) { + // copy + out->bsize = in->bsize; + memcpy(out->bytes, in->bytes, out->bsize); + strcpy(out->string, in->string); + return 1; + } // string allocated + free(out->bytes); + } // bytes allocated + } // good parameters + return 0; +} + /** * Put a string into the MultiAddress and recalculate the bytes * @param result the struct