Skip to content

Instantly share code, notes, and snippets.

@jonesnxt
Created July 3, 2015 02:48
Show Gist options
  • Save jonesnxt/3f540f9684098535ddf8 to your computer and use it in GitHub Desktop.
Save jonesnxt/3f540f9684098535ddf8 to your computer and use it in GitHub Desktop.
Convert to and from RS addresses in C
/*
NXT address converter,
Ported from original javascript (nxtchg)
To C by Jones
*/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int gexp[] = {1, 2, 4, 8, 16, 5, 10, 20, 13, 26, 17, 7, 14, 28, 29, 31, 27, 19, 3, 6, 12, 24, 21, 15, 30, 25, 23, 11, 22, 9, 18, 1};
int glog[] = {0, 0, 1, 18, 2, 5, 19, 11, 3, 29, 6, 27, 20, 8, 12, 23, 4, 10, 30, 17, 7, 22, 28, 26, 21, 25, 9, 16, 13, 14, 24, 15};
int cwmap[] = {3, 2, 1, 0, 7, 6, 5, 4, 13, 14, 15, 16, 12, 8, 9, 10, 11};
char alphabet[] = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
int gmult(int a, int b)
{
if (a == 0 || b == 0) return 0;
int idx = (glog[a] + glog[b]) % 31;
return gexp[idx];
}
int letterval(char letter)
{
int ret = 0;
if(letter < '9') ret = letter - '2';
else
{
ret = letter - 'A' + 8;
if(letter > 'I') ret --;
if(letter > 'O') ret --;
}
return ret;
}
long RS_decode(const char * rs)
{
int code[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int p=4;
for(int i=0;i<17;i++) {
code[cwmap[i]] = letterval(rs[p]);
p++;
if(rs[p] == '-') p++;
}
long out = 0;
for(int i=12;i>=0;i--)
{
out = out * 32 + code[i];
}
return out;
}
char * RS_encode(long id)
{
int code[] = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
char * acc = malloc(21);
sprintf(acc, "%ld", id);
int inp[32];
int out[32];
int pos = 0;
int len = 0;
for(int a=0;*(acc+a) != '\0'; a++)
len ++;
if (len == 20 && *acc != '1') return "error";
for (int i = 0; i < len; i++) {
inp[i] = (int) *(acc+i) - (int) '0';
}
free(acc);
int divide = 0;
int newlen = 0;
do // base 10 to base 32 conversion
{
divide = 0;
newlen = 0;
for (int i = 0; i < len; i++) {
divide = divide * 10 + inp[i];
if (divide >= 32) {
inp[newlen++] = divide >> 5;
divide &= 31;
} else if (newlen > 0) {
inp[newlen++] = 0;
}
}
len = newlen;
out[pos++] = divide;
} while (newlen);
for (int i = 0; i < 13; i++) // copy to code in reverse, pad with 0's
{
code[i] = (--pos >= 0 ? out[i] : 0);
}
int p[] = {0, 0, 0, 0};
for (int i = 12; i >= 0; i--) {
int fb = code[i] ^ p[3];
p[3] = p[2] ^ gmult(30, fb);
p[2] = p[1] ^ gmult(6, fb);
p[1] = p[0] ^ gmult(9, fb);
p[0] = gmult(17, fb);
}
code[13] = p[0];
code[14] = p[1];
code[15] = p[2];
code[16] = p[3];
char * rs = malloc(35);
strcpy(rs, "NXT-");
int j=4;
for (int i = 0; i < 17; i++) {
rs[j] = alphabet[code[cwmap[i]]];
j++;
if ((j % 5) == 3 && j < 20)
{
rs[j] = '-';
j++;
}
}
return rs;
}
int main(int argc, const char* argv[])
{
long id = atol(argv[1]);
char * rs = RS_encode(id);
int i=0; // print out the
while(rs[i] != '\0')
{
printf("%c", rs[i]);
i++;
}
printf("\n");
printf("%ld \n", RS_decode(rs));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment