From 33afac194a0d6e6f153d3408ebacb3a2565ba030 Mon Sep 17 00:00:00 2001 From: Jose Marcial Vieira Bisneto Date: Thu, 1 Dec 2016 19:29:43 -0300 Subject: [PATCH] proquint: Implemented IPv6 suport. --- namesys/proquint.c | 67 +++++++++++++++++++++++------------------ namesys/proquint_test.c | 7 ++--- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/namesys/proquint.c b/namesys/proquint.c index 1ff47a6..b40a95d 100644 --- a/namesys/proquint.c +++ b/namesys/proquint.c @@ -46,33 +46,36 @@ static inline int vowsd(char c) */ int ProquintIsProquint(char *str) { - int i, c; + int i, c, l = strlen(str); - // if str is null, or length isn't 11 or don't have - at middle - if (!str || strlen(str) != 11 || str[5] != '-') { + // if str is null, or length is invalid + if (!str || ((l+1) % 6)) { return 0; // it's not a proquint } // run every position - for (i = 0 ; i < 11 ; i++) { - if (i == 5) i++; // skip -, already tested. - switch (i) { - case 1: - case 3: - case 7: - case 9: - // compare with vowse array - c = vowsd(str[i]); - if (str[i] != vowse[c]) { - return 0; // it's not a proquint - } - break; - default: // 0,2,4,6,8,10 - // compare with conse array - c = consd(str[i]); - if (str[i] != conse[c]) { - return 0; // it's not a proquint - } + for (i = 0 ; i < l ; i++) { + if (((i+1) % 6) == 0) { // After each 5 characters + if (str[i] != '-') { // need a - + return 0; // or it's not a proquint + } + } else { + switch ((i+1) % 2) { // i + 1 to avoid zero division + case 0: + // compare with vowse array + c = vowsd(str[i]); + if (str[i] != vowse[c]) { + + return 0; // it's not a proquint + } + break; + default: + // compare with conse array + c = consd(str[i]); + if (str[i] != conse[c]) { + return 0; // it's not a proquint + } + } } } @@ -86,22 +89,28 @@ int ProquintIsProquint(char *str) * * @return {string} The given byte slice as an identifier. */ -char *ProquintEncode(char *buf) +char *ProquintEncode(char *buf, int size) { char *ret; int i, c; uint16_t n; + if (size % 2) { + return NULL; // not multiple of 2 + } + if (!buf) { return NULL; } - ret = malloc(12); + // Each word (2 bytes) uses 5 ascii characters + // and one - or a NULL terminator. + ret = malloc(size * 3); if (!ret) { return NULL; } - for (i = 0, c = 0; i < 4; i += 2) { + for (i = 0, c = 0; i < size; i += 2) { n = ((buf[i] & 0xff) << 8) | (buf[i + 1] & 0xff); ret[c++] = conse[(n >> 12) & 0x0f]; @@ -126,20 +135,20 @@ char *ProquintEncode(char *buf) char *ProquintDecode(char *str) { char *ret; - int i, c; + int i, c, l = strlen(str); uint16_t x; // make sure its a valid Proquint string. - if (!ProquintIsProquint(str)) { + if (!ProquintIsProquint(str) && ((l+1) % 3)==0) { return NULL; } - ret = malloc(4); + ret = malloc((l+1)/3); if (!ret) { return NULL; } - for (i = 0, c = 0 ; i < 11 ; i += 6) { + for (i = 0, c = 0 ; i < l ; i += 6) { x =(consd(str[i + 0]) << 12) | \ (vowsd(str[i + 1]) << 10) | \ (consd(str[i + 2]) << 6) | \ diff --git a/namesys/proquint_test.c b/namesys/proquint_test.c index 559e60d..cd8c85c 100644 --- a/namesys/proquint_test.c +++ b/namesys/proquint_test.c @@ -5,10 +5,7 @@ #include #include #include - -int ProquintIsProquint(char *str); -char *ProquintEncode(char *buf); -char *ProquintDecode(char *str); +#include "ipfs/namesys/namesys.h" int main(void) { char *r, *s; @@ -24,7 +21,7 @@ int main(void) { struct in_addr ip_addr; memcpy (&(ip_addr.s_addr), r, sizeof(ip_addr.s_addr)); printf ("%s\t%s", p[i], inet_ntoa(ip_addr)); - s = ProquintEncode(r); + s = ProquintEncode(r, sizeof(ip_addr.s_addr)); free (r); if (s) { printf ("\t%s", s);