From c2f2c0c2da6d90195ee1ff65c11ed7b94aca74bb Mon Sep 17 00:00:00 2001 From: John Jones Date: Wed, 26 Jul 2017 07:36:45 -0500 Subject: [PATCH] Added a compare method --- include/multiaddr/multiaddr.h | 2 ++ multiaddr.c | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/include/multiaddr/multiaddr.h b/include/multiaddr/multiaddr.h index 59d91da..fafc0fe 100644 --- a/include/multiaddr/multiaddr.h +++ b/include/multiaddr/multiaddr.h @@ -87,4 +87,6 @@ int multiaddress_get_ip_port(const struct MultiAddress* in); */ char* multiaddress_get_peer_id(const struct MultiAddress* in); +int multiaddress_compare(const struct MultiAddress* a, const struct MultiAddress* b); + #endif diff --git a/multiaddr.c b/multiaddr.c index 45e3b1c..5b7beb9 100644 --- a/multiaddr.c +++ b/multiaddr.c @@ -347,3 +347,20 @@ int multiaddress_decapsulate(struct MultiAddress * result, char * srci) } } +int multiaddress_compare(const struct MultiAddress* a, const struct MultiAddress* b) { + if (a == NULL && b == NULL) + return 0; + if (a == NULL && b != NULL) + return -1; + if (a != NULL && b == NULL) + return 1; + int total = b->bsize - a->bsize; + if (total != 0) + return total; + for(size_t i = 0; i < b->bsize; i++) { + total = b->bytes[i] - a->bytes[i]; + if (total != 0) + return total; + } + return 0; +}