proquint: Implemented IPv6 suport.

This commit is contained in:
Jose Marcial Vieira Bisneto 2016-12-01 19:29:43 -03:00
parent 9ba3112b97
commit 33afac194a
2 changed files with 40 additions and 34 deletions

View file

@ -46,28 +46,30 @@ static inline int vowsd(char c)
*/ */
int ProquintIsProquint(char *str) 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 is null, or length is invalid
if (!str || strlen(str) != 11 || str[5] != '-') { if (!str || ((l+1) % 6)) {
return 0; // it's not a proquint return 0; // it's not a proquint
} }
// run every position // run every position
for (i = 0 ; i < 11 ; i++) { for (i = 0 ; i < l ; i++) {
if (i == 5) i++; // skip -, already tested. if (((i+1) % 6) == 0) { // After each 5 characters
switch (i) { if (str[i] != '-') { // need a -
case 1: return 0; // or it's not a proquint
case 3: }
case 7: } else {
case 9: switch ((i+1) % 2) { // i + 1 to avoid zero division
case 0:
// compare with vowse array // compare with vowse array
c = vowsd(str[i]); c = vowsd(str[i]);
if (str[i] != vowse[c]) { if (str[i] != vowse[c]) {
return 0; // it's not a proquint return 0; // it's not a proquint
} }
break; break;
default: // 0,2,4,6,8,10 default:
// compare with conse array // compare with conse array
c = consd(str[i]); c = consd(str[i]);
if (str[i] != conse[c]) { if (str[i] != conse[c]) {
@ -75,6 +77,7 @@ int ProquintIsProquint(char *str)
} }
} }
} }
}
return 1; // passed on every value. return 1; // passed on every value.
} }
@ -86,22 +89,28 @@ int ProquintIsProquint(char *str)
* *
* @return {string} The given byte slice as an identifier. * @return {string} The given byte slice as an identifier.
*/ */
char *ProquintEncode(char *buf) char *ProquintEncode(char *buf, int size)
{ {
char *ret; char *ret;
int i, c; int i, c;
uint16_t n; uint16_t n;
if (size % 2) {
return NULL; // not multiple of 2
}
if (!buf) { if (!buf) {
return NULL; 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) { if (!ret) {
return NULL; 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); n = ((buf[i] & 0xff) << 8) | (buf[i + 1] & 0xff);
ret[c++] = conse[(n >> 12) & 0x0f]; ret[c++] = conse[(n >> 12) & 0x0f];
@ -126,20 +135,20 @@ char *ProquintEncode(char *buf)
char *ProquintDecode(char *str) char *ProquintDecode(char *str)
{ {
char *ret; char *ret;
int i, c; int i, c, l = strlen(str);
uint16_t x; uint16_t x;
// make sure its a valid Proquint string. // make sure its a valid Proquint string.
if (!ProquintIsProquint(str)) { if (!ProquintIsProquint(str) && ((l+1) % 3)==0) {
return NULL; return NULL;
} }
ret = malloc(4); ret = malloc((l+1)/3);
if (!ret) { if (!ret) {
return NULL; 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) | \ x =(consd(str[i + 0]) << 12) | \
(vowsd(str[i + 1]) << 10) | \ (vowsd(str[i + 1]) << 10) | \
(consd(str[i + 2]) << 6) | \ (consd(str[i + 2]) << 6) | \

View file

@ -5,10 +5,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include "ipfs/namesys/namesys.h"
int ProquintIsProquint(char *str);
char *ProquintEncode(char *buf);
char *ProquintDecode(char *str);
int main(void) { int main(void) {
char *r, *s; char *r, *s;
@ -24,7 +21,7 @@ int main(void) {
struct in_addr ip_addr; struct in_addr ip_addr;
memcpy (&(ip_addr.s_addr), r, sizeof(ip_addr.s_addr)); memcpy (&(ip_addr.s_addr), r, sizeof(ip_addr.s_addr));
printf ("%s\t%s", p[i], inet_ntoa(ip_addr)); printf ("%s\t%s", p[i], inet_ntoa(ip_addr));
s = ProquintEncode(r); s = ProquintEncode(r, sizeof(ip_addr.s_addr));
free (r); free (r);
if (s) { if (s) {
printf ("\t%s", s); printf ("\t%s", s);