Added a method to copy MultiAddresses

This commit is contained in:
John Jones 2017-02-13 14:28:06 -05:00
parent bb488f7e82
commit cc8ff45cc1
2 changed files with 27 additions and 0 deletions

View file

@ -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);

View file

@ -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