manuel-arguelles/drg2sbg

help porting to javascript

konsumer opened this issue · 6 comments

I am working on a port of this to javascript for a HTML5 idoser player.

I can't get mine to output the correct stuff & wondered if you could help. I think I have pretty accurately ported drg_get_uncoded_data, mine looks like this:

function drg_get_uncoded_data(data){
    var S = [
         22, 213, 140,  67, 234,  48, 108, 225,   6, 101, 194,  50,  44, 247,
         58, 145,  20,  80, 241,  60, 127, 154, 125,  33,  45, 166, 245,  84,
         28, 110, 220,  56, 195, 181, 238, 109,  69, 216,  31, 162,  61, 183,
         74,  71, 129, 148, 170, 111, 137, 164, 179, 178,   9,  41, 160, 219,
         77,  93,  97, 143,  14, 158, 118, 152,   0, 221, 192, 116,  86,  65,
         55, 173, 217,  32, 227, 119, 102, 115, 254, 132,  95,  23,  49,  73,
        211, 142,  66,  59,  85, 252, 138, 212, 243,  38, 134, 165, 184,  13,
        209, 124, 197, 141, 114,  43,  92, 133, 175, 205, 128,  68,  91, 104,
         64, 126,  39,  40,  46,  72, 139, 232, 182,   2, 131, 201, 188, 112,
        200,  78, 159, 113, 237,  99, 249,  90,   7,  47, 122,  36,  76, 117,
        222, 149,  96,  82, 100, 208, 151, 198, 228,  94,  87, 190,  42, 246,
         10, 169, 171, 120,  51, 236, 255, 215, 191, 223,  54, 103,  89, 135,
         57,  98, 176, 161,  24, 235,  26,   3, 250, 233, 121,  79, 207, 242,
        224,  11, 123, 193, 155, 157, 218, 186, 244,  75, 167,  63, 206,  81,
         29, 150, 229,   4,  15, 230,  37, 185,   1, 203,  35,  16, 136, 204,
        144, 253, 214, 168,  27, 189, 105, 231, 177,  18,  25,  52,  70,  88,
        196, 210, 163, 239, 156,  19,  34,  17, 202,  30,  21,  62, 147, 174,
        240, 130,   8, 180, 106, 172,  83,  12, 146, 251, 226,  53, 153, 107,
        199, 248, 187, 5
    ];

    var i = j = s = temp = 0;

    for (var b = 0; b < data.length; b++) {
        i = (i+1) % 256;
        j = (j + S[i]) % 256;
        temp = S[i];
        S[i] = S[j];
        S[j] = temp;
        s=S[i] + S[j];
        data[b] = data[b] ^ S[s % 256];
        console.log("b=%d, i=%d, j=%d, s[i]=%d, s[j]=%d, s=%d: %s (%d)", b, i, j, S[i], S[j], s, data[b], data[b].charCodeAt(0));
    }

    return data;
}

data param is base64-decoded chunk for the element.

I get this for title of Absinthe.drg:

b=0, i=1, j=213, s[i]=168, s[j]=213, s=381: 1 (49)
b=1, i=2, j=97, s[i]=13, s[j]=140, s=153: � (148)
b=2, i=3, j=164, s[i]=54, s[j]=67, s=121: q (113)
b=3, i=4, j=142, s[i]=96, s[j]=234, s=330: � (138)
b=4, i=5, j=190, s[i]=244, s[j]=48, s=292: + (43)
b=5, i=6, j=42, s[i]=74, s[j]=108, s=182: � (148)
b=6, i=7, j=11, s[i]=50, s[j]=225, s=275: T (84)
b=7, i=8, j=17, s[i]=80, s[j]=6, s=86: ' (39)
1�q�+�T'

I modified your for-loop to look like this:

for (b = 0; b < a; b++) {
        i = (i +1) % 256;
        j = (j + S[i]) % 256;
        temp = S[i];
        S[i] = S[j];
        S[j] = temp;
        s=S[i] + S[j];
        data[b] = data[b] ^ S[s % 256];
        printf("b=%d, i=%d, j=%d, s[i]=%d, s[j]=%d, s=%d: %c (%d)\n", (int)b, i, j, S[i], S[j], s, data[b], (int)data[b]);
    }

and it (correctly) outputs:

b=0, i=1, j=213, s[i]=168, s[j]=213, s=381: A (65)
b=1, i=2, j=97, s[i]=13, s[j]=140, s=153: b (98)
b=2, i=3, j=164, s[i]=54, s[j]=67, s=121: s (115)
b=3, i=4, j=142, s[i]=96, s[j]=234, s=330: i (105)
b=4, i=5, j=190, s[i]=244, s[j]=48, s=292: n (110)
b=5, i=6, j=42, s[i]=74, s[j]=108, s=182: t (116)
b=6, i=7, j=11, s[i]=50, s[j]=225, s=275: h (104)
b=7, i=8, j=17, s[i]=80, s[j]=6, s=86: e (101)
Absinthe

Everything seems to match up to the actual char output, for some reason. Any help you can provide would be much appreciated.

Hello, If you are able to output the title, you should be able to output the rest. Remember that the image is base64 encoded.

That is the issue I am having. I am unable to output the title. I am just including your code in the second example to show that the for-loop outputs all the same numbers but for some reason isn't showing the right chars.

Aha! I got it. There was some funky char/int type conversion going on, but I totally got it working by keeping it all in int space until return. Thanks! As a sidenote, what type of image is the image-field? I tried saving the output of your program as png, jpg, & bmp, and none of them worked.

Great news! :)

I currently don't have a drg file at hand to confirm it, but I'm almost
sure it is bmp. Just remember that it is base64 encoded.
On Jan 5, 2015 12:46 AM, "David Konsumer" notifications@github.com wrote:

Aha! I got it. There was some funky char/int type conversion going on, but
I totally got it working by keeping it all in int space until return.
Thanks! As a sidenote, what type of image is the image-field? I tried
saving the output of your program as png, jpg, & bmp, and none of them
worked.


Reply to this email directly or view it on GitHub
#1 (comment)
.

Sweet! Thanks so much. I will mark this issue closed, but attach a link to my js version when it's done.

https://github.com/brainbang/drg2sba

Now, I need to make some HTML5-thing that actually plays sbagen files!