C-Utils Repository branch, master, updated. 8acceabb88d7ba12c4d34099cfd23b19ee73c180
This is an automated email from the git hooks/post-receive script. It was generated because a ref change was pushed to the repository containing the project "C-Utils Repository". The branch, master has been updated via 8acceabb88d7ba12c4d34099cfd23b19ee73c180 (commit) via 783495cf74811fa0563af750ac1a07bb45d181bd (commit) via fc0c0934feba5c8c1b6624b3940458482106d448 (commit) from e11f958e01bea27318a047161add9842f297b0f4 (commit) Those revisions listed above that are new to this repository have not appeared on any other notification email; so we list those revisions in full, below. - Log ----------------------------------------------------------------- commit 8acceabb88d7ba12c4d34099cfd23b19ee73c180 Merge: 783495cf74811fa0563af750ac1a07bb45d181bd e11f958e01bea27318a047161add9842f297b0f4 Author: Kevin Harms <[email protected]> Date: Fri Nov 22 16:43:14 2013 -0600 Merge branch 'master' of git://git.mcs.anl.gov/c-utils commit 783495cf74811fa0563af750ac1a07bb45d181bd Author: Kevin Harms <[email protected]> Date: Thu Nov 21 17:10:02 2013 -0600 change triton_uint128_{to,from}_string to work with ipv6 notation commit fc0c0934feba5c8c1b6624b3940458482106d448 Author: Kevin Harms <[email protected]> Date: Tue Oct 15 17:54:39 2013 -0500 this macro isn't supported on older automake versions so this change supports both ways ----------------------------------------------------------------------- Summary of changes: configure.ac | 2 +- src/triton-uint128.h | 57 +++++++++++++++++++++++++++++++++++++++++++++---- tests/test-uint128.c | 21 +++++++++++------- 3 files changed, 66 insertions(+), 14 deletions(-) Diff of changes: diff --git a/configure.ac b/configure.ac index 4a6007b..ccd1b0c 100644 --- a/configure.ac +++ b/configure.ac @@ -14,7 +14,7 @@ AC_CONFIG_HEADER([c-utils-config.h]) AC_PROG_CC AM_PROG_CC_C_O -AM_PROG_AR +m4_ifdef([AM_PROG_AR], [AM_PROG_AR]) LT_INIT([disable-shared]) AC_PROG_AWK AC_PROG_LN_S diff --git a/src/triton-uint128.h b/src/triton-uint128.h index 10184a6..48818e4 100644 --- a/src/triton-uint128.h +++ b/src/triton-uint128.h @@ -10,6 +10,10 @@ #include <stdio.h> #include <assert.h> #include <stdint.h> +#include <stdlib.h> +#include <arpa/inet.h> +#include <string.h> +#include <endian.h> /* unsigned 128 bit integer type */ typedef struct { uint64_t l; uint64_t u; } uint128_t; @@ -17,6 +21,7 @@ typedef struct { uint64_t l; uint64_t u; } uint128_t; #define triton_uint128_setzero(t) (t).l = 0, (t).u = 0 #define triton_uint128_iszero(t) ((t).l == 0 && (t).u == 0) #define triton_uint128_setmax(t) (t).l = UINT64_MAX, (t).u = UINT64_MAX +#define triton_uint128_ismax(t) ((t).l == UINT64_MAX && (t).u == UINT64_MAX) #define triton_uint128_set(from, to) ((to).l = (from).l, (to).u = (from).u) @@ -43,12 +48,54 @@ typedef struct { uint64_t l; uint64_t u; } uint128_t; #endif #endif -/* convert to string in uint64_t:uint64_t format (for now) */ -static inline void triton_uint128_to_string(char* buf, int buflen, uint128_t big) +/* convert uint128 to string which is in ipv6 notation, + * NULL on failure, + * user must free buffer when done */ +static inline char* triton_uint128_to_string(uint128_t big) { - assert(buflen >= TRITON_UINT128_STRLEN); - snprintf(buf, buflen, "%llu:%llu", llu(big.u), llu(big.l)); - return; + char *buf = malloc(INET6_ADDRSTRLEN); + const char *ptr; + + if (buf) + { + struct in6_addr ipv6_addr; + uint64_t *upper = (uint64_t *) &ipv6_addr.s6_addr[0]; + uint64_t *lower = (uint64_t *) &ipv6_addr.s6_addr[8]; + *upper = htobe64(big.u); + *lower = htobe64(big.l); + ptr = inet_ntop(AF_INET6, &ipv6_addr, buf, INET6_ADDRSTRLEN); + if (!ptr) + { + free(buf); /* free buffer on failure */ + buf = NULL; + } + } + + return buf; +} + +/* convert a string to a uint128_t, string is in ivp6 notation. + returns max oid on failure */ +static inline uint128_t triton_uint128_from_string (const char* str) +{ + struct in6_addr ipv6_addr; + uint128_t big; + int r; + uint64_t *upper = (uint64_t *) &ipv6_addr.s6_addr[0]; + uint64_t *lower = (uint64_t *) &ipv6_addr.s6_addr[8]; + + r = inet_pton(AF_INET6, str, &ipv6_addr); + if (r == 1) + { + big.u = be64toh(*upper); + big.l = be64toh(*lower); + } + else + { + triton_uint128_setmax(big); + } + + return big; } /* assign a 64 bit value to a uint128_t */ diff --git a/tests/test-uint128.c b/tests/test-uint128.c index 5d730b6..464368f 100644 --- a/tests/test-uint128.c +++ b/tests/test-uint128.c @@ -14,30 +14,35 @@ int main(int argc, char *argv[]) { uint128_t big; - char big_str[TRITON_UINT128_STRLEN]; + char *big_str; int testcase, failcount; tap_begin(testcase, failcount); big = triton_uint128_from_uint64(0); - triton_uint128_to_string(big_str, TRITON_UINT128_STRLEN, big); - tap("triton_uint128_from_uint64(0)", strcmp(big_str, "0:0") == 0, + big_str = triton_uint128_to_string(big); + tap("triton_uint128_from_uint64(0)", strcmp(big_str, "::") == 0, testcase, failcount); + if (big_str) free(big_str); big = triton_uint128_inc(big); - triton_uint128_to_string(big_str, TRITON_UINT128_STRLEN, big); - tap("triton_uint128_inc(0)", strcmp(big_str, "0:1") == 0, + big_str = triton_uint128_to_string(big); + tap("triton_uint128_inc(0)", strcmp(big_str, "::1") == 0, testcase, failcount); + if (big_str) free(big_str); big = triton_uint128_from_uint64(UINT64_MAX); - triton_uint128_to_string(big_str, TRITON_UINT128_STRLEN, big); + big_str = triton_uint128_to_string(big); tap("triton_uint128_from_uint64(UINT64_MAX)", big.l == UINT64_MAX, testcase, failcount); + if (big_str) free(big_str); big = triton_uint128_inc(big); - triton_uint128_to_string(big_str, TRITON_UINT128_STRLEN, big); - tap("triton_uint128_inc(UINT64_MAX)", strcmp(big_str, "1:0") == 0, + big_str = triton_uint128_to_string(big); + printf("%s\n", big_str); + tap("triton_uint128_inc(UINT64_MAX)", strcmp(big_str, "0:0:0:1::") == 0, testcase, failcount); + if (big_str) free(big_str); tap_end(testcase, failcount); hooks/post-receive -- C-Utils Repository
participants (1)
-
noreply@mcs.anl.gov