Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
use 4 byte stride, start simplifying ROT function
  • Loading branch information
x42 committed Dec 28, 2012
1 parent 5034ad2 commit a1d364b
Showing 1 changed file with 73 additions and 13 deletions.
86 changes: 73 additions & 13 deletions digimagic.c
Expand Up @@ -5,6 +5,12 @@
#include <string.h>
#include <stdint.h>


#define BYTE_PER_SAMPLE (4)
#define MAGIC_DIGI_BYTE (1)

#define MAGIC_BYTE_OFF(x) ( (x * BYTE_PER_SAMPLE) + MAGIC_DIGI_BYTE)

uint8_t digiscrt(uint8_t idx, int off) {

const uint8_t s[256][14] = {
Expand Down Expand Up @@ -263,27 +269,61 @@ uint8_t digiscrt(uint8_t idx, int off) {
{ 0x83, 0x7d, 0x52, 0x3e, 0x41, 0xcf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x82, 0x7e, 0x51, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x81, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // XXX 255 - data missing
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
};

if (off == 0) return idx;
if (off > 14) return 0x00;
return s[idx][off-1];
}

const uint8_t digiscrt2(uint8_t idx, int off) {
const uint8_t len[16] = {0, 1, 3, 5, 7, 9, 11, 13, 14, 12, 10, 8, 6, 4, 2, 0};
const uint8_t nib[15] = {0x8, 0x7, 0x9, 0x6, 0xa, 0x5, 0xb, 0x4, 0xc, 0x3, 0xd, 0x2, 0xe, 0x1, 0xf};
//const uint8_t hir[16] = {0x0, 0x6, 0xf, 0x8, 0x7, 0x5, 0x3, 0x4,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, 0x1, 0x2},
{ /*0xc,*/ 0xd, 0xe, 0x1, 0x2, 0xb, 0xa, 0x0, 0x6, 0xf, 0x8, 0x7, 0x5, 0x3, 0x4},
{ /*0xd,*/ 0xe, 0x1, 0x2, 0xb, 0xa, 0x0, 0x6, 0xf, 0x8, 0x7, 0x5, 0x3, 0x4, 0xc},
{ /*0xe,*/ 0x1, 0x2, 0xb, 0xa, 0x0, 0x6, 0xf, 0x8, 0x7, 0x5, 0x3, 0x4, 0xc, 0xd},
{ /*0xf,*/ 0x8, 0x7, 0x5, 0x3, 0x4, 0xc, 0xd, 0xe, 0x1, 0x2, 0xb, 0xa, 0x0, 0x6},
};

if (off==0) return idx;
off--;

const uint8_t ln = idx&0xf;
const uint8_t hn = (idx>>4)&0xf;

if (len[ln] <= off) return 0x00;
return (nib[15 - len[ln] + off]) | (hib[hn][off]<<4);
}



void digi_encode(uint8_t *data, int nch) {
int c;
uint8_t carry = 0x00;
uint8_t idx = 0;
int off = 0;

for (c=0; c< nch; ++c) {
if (data[c*3 +1] != 0x00) { off = 0; }

data[c*3 +1] ^= carry;

if (off == 0) { idx = data[c*3 +1]; }
carry=digiscrt(idx, ++off);
if (data[MAGIC_BYTE_OFF(c)] != 0x00) { off = 0; }
data[MAGIC_BYTE_OFF(c)] ^= carry;
if (off == 0) { idx = data[MAGIC_BYTE_OFF(c)]; }
carry=digiscrt2(idx, ++off);
}
}

Expand All @@ -294,23 +334,43 @@ void hexdump(uint8_t *data, int nch, char *annotation) {
int c;
printf("%10s: ", annotation);
for (c=0; c< nch; ++c) {
printf("%02x ", data[c*3 +1]);
printf("%02x ", data[MAGIC_BYTE_OFF(c)]);
}
printf("\n");
}


int main(int argc, char **argv) {
int nch = 18;
uint8_t *snd = calloc(3 * nch, sizeof(uint8_t));
snd[1]=snd[4*3 + 1] = 0x14;

#if 1
int x,y;
for (x=0;x<=0xff; ++x) {
for (y=0;y<=0xf; ++y) {
printf("%x,%x -> %02x | %02x %s\n", x, y, digiscrt(x,y), digiscrt2(x,y), digiscrt(x,y) == digiscrt2(x,y)?"":"!!!!!!");
}
}
#endif

const int nch = 18;
uint8_t *snd = calloc(BYTE_PER_SAMPLE * nch, sizeof(uint8_t));

memset(snd, 0, BYTE_PER_SAMPLE * nch * sizeof(uint8_t));
hexdump(snd, nch, "input");
digi_encode(snd, nch);
hexdump(snd, nch, "output");
printf(" expect: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\n");

printf("\n");
memset(snd, 0, BYTE_PER_SAMPLE * nch * sizeof(uint8_t));
snd[MAGIC_DIGI_BYTE]=snd[4*BYTE_PER_SAMPLE + MAGIC_DIGI_BYTE] = 0x14;
hexdump(snd, nch, "input");
digi_encode(snd, nch);
hexdump(snd, nch, "output");
printf(" expect: 14 2c b3 ad 16 2a b5 ab 04 6c f3 8d 72 5e 31 4f 00 00\n");

printf("\n");
memset(snd, 0, 3 * nch * sizeof(uint8_t));
snd[1]=snd[4*3 + 1] = 0x85;
memset(snd, 0, BYTE_PER_SAMPLE * nch * sizeof(uint8_t));
snd[MAGIC_DIGI_BYTE]=snd[4*BYTE_PER_SAMPLE + MAGIC_DIGI_BYTE] = 0x85;
hexdump(snd, nch, "input");
digi_encode(snd, nch);
hexdump(snd, nch, "output");
Expand Down

0 comments on commit a1d364b

Please sign in to comment.