2016-11-24 12:50:05 +00:00
|
|
|
#include <stdlib.h>
|
2016-11-29 23:55:59 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
2016-11-24 12:50:05 +00:00
|
|
|
#include "ipfs/namesys/namesys.h"
|
|
|
|
#include "ipfs/cid/cid.h"
|
|
|
|
#include "ipfs/path/path.h"
|
|
|
|
|
2016-11-29 23:55:59 +00:00
|
|
|
const uint8_t conse[] = {'b', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'r', 's', 't', 'v', 'z'};
|
|
|
|
const uint8_t vowse[] = {'a', 'i', 'o', 'u'};
|
|
|
|
|
|
|
|
// Find decoded number from the encoded consonant.
|
|
|
|
static inline int consd(char c)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0 ; i < sizeof(conse) ; i++) {
|
|
|
|
if (c == conse[i]) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find decoded number of encoded vowel.
|
|
|
|
static inline int vowsd(char c)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0 ; i < sizeof(vowse) ; i++) {
|
|
|
|
if (c == vowse[i]) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests if a given string is a Proquint identifier
|
|
|
|
*
|
|
|
|
* @param {string} str The candidate string.
|
|
|
|
*
|
|
|
|
* @return {bool} Whether or not it qualifies.
|
|
|
|
* @return {error} Error
|
|
|
|
*/
|
|
|
|
int ProquintIsProquint(char *str)
|
|
|
|
{
|
2016-12-01 22:29:43 +00:00
|
|
|
int i, c, l = strlen(str);
|
2016-11-29 23:55:59 +00:00
|
|
|
|
2016-12-01 22:29:43 +00:00
|
|
|
// if str is null, or length is invalid
|
|
|
|
if (!str || ((l+1) % 6)) {
|
2016-11-29 23:55:59 +00:00
|
|
|
return 0; // it's not a proquint
|
|
|
|
}
|
|
|
|
|
|
|
|
// run every position
|
2016-12-01 22:29:43 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2016-11-29 23:55:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1; // passed on every value.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Encodes an arbitrary byte slice into an identifier.
|
|
|
|
*
|
|
|
|
* @param {[]byte} buf Slice of bytes to encode.
|
|
|
|
*
|
|
|
|
* @return {string} The given byte slice as an identifier.
|
|
|
|
*/
|
2016-12-01 22:29:43 +00:00
|
|
|
char *ProquintEncode(char *buf, int size)
|
2016-11-29 23:55:59 +00:00
|
|
|
{
|
|
|
|
char *ret;
|
|
|
|
int i, c;
|
|
|
|
uint16_t n;
|
|
|
|
|
2016-12-01 22:29:43 +00:00
|
|
|
if (size % 2) {
|
|
|
|
return NULL; // not multiple of 2
|
|
|
|
}
|
|
|
|
|
2016-11-29 23:55:59 +00:00
|
|
|
if (!buf) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-01 22:29:43 +00:00
|
|
|
// Each word (2 bytes) uses 5 ascii characters
|
|
|
|
// and one - or a NULL terminator.
|
|
|
|
ret = malloc(size * 3);
|
2016-11-29 23:55:59 +00:00
|
|
|
if (!ret) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-01 22:29:43 +00:00
|
|
|
for (i = 0, c = 0; i < size; i += 2) {
|
2016-11-29 23:55:59 +00:00
|
|
|
n = ((buf[i] & 0xff) << 8) | (buf[i + 1] & 0xff);
|
|
|
|
|
|
|
|
ret[c++] = conse[(n >> 12) & 0x0f];
|
|
|
|
ret[c++] = vowse[(n >> 10) & 0x03];
|
|
|
|
ret[c++] = conse[(n >> 6) & 0x0f];
|
|
|
|
ret[c++] = vowse[(n >> 4) & 0x03];
|
|
|
|
ret[c++] = conse[n & 0x0f];
|
|
|
|
ret[c++] = '-';
|
|
|
|
}
|
|
|
|
ret[--c] = '\0';
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decodes an identifier into its corresponding byte slice.
|
|
|
|
*
|
|
|
|
* @param {string} str Identifier to convert.
|
|
|
|
*
|
|
|
|
* @return {[]byte} The identifier as a byte slice.
|
|
|
|
*/
|
|
|
|
char *ProquintDecode(char *str)
|
|
|
|
{
|
|
|
|
char *ret;
|
2016-12-01 22:29:43 +00:00
|
|
|
int i, c, l = strlen(str);
|
2016-11-29 23:55:59 +00:00
|
|
|
uint16_t x;
|
|
|
|
|
|
|
|
// make sure its a valid Proquint string.
|
2016-12-01 22:29:43 +00:00
|
|
|
if (!ProquintIsProquint(str) && ((l+1) % 3)==0) {
|
2016-11-29 23:55:59 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-01 22:29:43 +00:00
|
|
|
ret = malloc((l+1)/3);
|
2016-11-29 23:55:59 +00:00
|
|
|
if (!ret) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-12-01 22:29:43 +00:00
|
|
|
for (i = 0, c = 0 ; i < l ; i += 6) {
|
2016-11-29 23:55:59 +00:00
|
|
|
x =(consd(str[i + 0]) << 12) | \
|
|
|
|
(vowsd(str[i + 1]) << 10) | \
|
|
|
|
(consd(str[i + 2]) << 6) | \
|
|
|
|
(vowsd(str[i + 3]) << 4) | \
|
|
|
|
(consd(str[i + 4]) << 0);
|
|
|
|
|
|
|
|
ret[c++] = x >> 8;
|
|
|
|
ret[c++] = x & 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-24 12:50:05 +00:00
|
|
|
// resolveOnce implements resolver. Decodes the proquint string.
|
|
|
|
int ProquintResolveOnce (char **p, char *name)
|
|
|
|
{
|
2016-11-29 23:55:59 +00:00
|
|
|
int err = ProquintIsProquint(name);
|
|
|
|
char buf[500];
|
2016-11-24 12:50:05 +00:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
*p = NULL;
|
|
|
|
err = ErrInvalidProquint;
|
|
|
|
} else {
|
2016-11-29 23:55:59 +00:00
|
|
|
err = ParsePath(buf, ProquintDecode(name));
|
|
|
|
if (!err) {
|
|
|
|
*p = malloc (strlen(buf) + 1);
|
|
|
|
if (p) {
|
|
|
|
strcpy(*p, buf);
|
|
|
|
}
|
|
|
|
}
|
2016-11-24 12:50:05 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|