Monday, 15 August 2011

c pointers and javascript -



c pointers and javascript -

i have been working on conversion of c code javascript. don't homecoming same data.

i have thought on how handle pointers. in javascript i'll create array.

note: not total code, partials

origin:

// file.h unsigned char m_aucstate0[256]; unsigned char m_aucstate[256]; unsigned char m_uci; unsigned char m_ucj; unsigned char* m_pucstate1; unsigned char* m_pucstate2; // file.c unsigned char *puckeydata for(i=0; i<256; i++) { m_pucstate1 = m_aucstate0 + i; m_ucj += *m_pucstate1 + *(puckeydata+m_uci); m_pucstate2 = m_aucstate0 + m_ucj; //swaping m_uctemp = *m_pucstate1; *m_pucstate1 = *m_pucstate2; *m_pucstate2 = m_uctemp; m_uci = (m_uci + 1) % ikeylen; } memcpy(m_aucstate, m_aucstate0, 256);

javascript:

// buffer or array??? this.m_aucstate0 = new buffer(256) this.m_aucstate = new buffer(256) this.m_uci this.m_ucj this.m_pucstate1 = [] this.m_pucstate2 = [] (var = 0; < 256; i++) { this.m_pucstate1 = this.m_aucstate0 + this.m_ucj += this.m_pucstate1[0] + (puckeydata[0] + this.m_uci) this.m_pucstate2 = this.m_aucstate0 + this.m_ucj //swaping this.m_uctemp = this.m_pucstate1[0] this.m_pucstate1[0] = this.m_pucstate2[0] this.m_pucstate2[0] = this.m_uctemp this.m_uci = (this.m_uci + 1) % ikeylen } this.m_aucstate.copy(this.m_aucstate0, 0, 0, 256)

so thought because pointer returns address, address contains first byte of pointer data. if in array point first index of array right?

is did above right?

just context allow me add together 1 function:

javascript:

crypt.prototype.setup = function(puckeydata, ikeylen) { if (ikeylen < 1) throw new error("key length should @ to the lowest degree 1") var i; (i = 0; < 256; i++) this.m_aucstate0[i] = this.m_uci = 0 this.m_ucj = 0 (var = 0; < 256; i++) { this.m_pucstate1 = this.m_aucstate0 + this.m_ucj += this.m_pucstate1[i] + (puckeydata[i] + this.m_uci) this.m_pucstate2 = this.m_aucstate0 + this.m_ucj //swaping this.m_uctemp = this.m_pucstate1[i] this.m_pucstate1[i] = this.m_pucstate2[i] this.m_pucstate2[i] = this.m_uctemp this.m_uci = (this.m_uci + 1) % ikeylen } this.m_aucstate.copy(this.m_aucstate0, 0, 0, 256) //initialize indexes this.m_uci = 0 this.m_ucj = 0 //initialization finished this.m_binit = true }

cpp:

void carcfourprng::setkey(unsigned char *puckeydata, int ikeylen) { if(ikeylen < 1) throw exception("key length should @ to the lowest degree 1"); int i; for(i=0; i<256; i++) m_aucstate0[i] = i; m_uci = 0; m_ucj = 0; for(i=0; i<256; i++) { m_pucstate1 = m_aucstate0 + i; m_ucj += *m_pucstate1 + *(puckeydata+m_uci); m_pucstate2 = m_aucstate0 + m_ucj; //swaping m_uctemp = *m_pucstate1; *m_pucstate1 = *m_pucstate2; *m_pucstate2 = m_uctemp; m_uci = (m_uci + 1) % ikeylen; } memcpy(m_aucstate, m_aucstate0, 256); //initialize indexes m_uci = 0; m_ucj = 0; //initialization finished m_binit = true; }

what difference of m_pucstate1 , *m_pucstate1 in this:

m_pucstate1 = m_aucstate + m_uci; m_ucj += *m_pucstate1;

in javascript, there typed buffer objects: http://www.javascripture.com/arraybuffer

you find ctypes collection, in understanding used native os library calls.

also, don't know native js buffer object mention it. there 1 in nodejs, don't know features.

if insist of translating code one-by-one, these typed buffer objects may back upwards you. think it's not way while translating c javascript, terminology alters anyway. alters adding long pointer values forming array indices.

here 1 problem illustration in translation:

in c, write:

m_ucj += *m_pucstate1 + *(puckeydata+m_uci);

in javascript, write:

this.m_ucj += this.m_pucstate1[0] + (puckeydata[0] + this.m_uci);

the brackets in c term create m_uci altering address. in javascript should rather in square brackets, somehow this:

this.m_ucj += this.m_pucstate1[0] + puckeydata[0 + this.m_uci];

and can skip "0 +". shows how one-by-one translation between such different languages total of traps.

so let's assume utilize simplest javascript object, array []. suggestion. it's draft, should give thorough idea:

// define arrays var astate0 = []; // m_aucstate0 var astate = []; // m_aucstate // define helpers var state1index; // *m_pucstate1 var state2index; // *m_pucstate2 var i; // m_uci. there no such thing "uc" in javascript. var j; // m_ucj var iloop; // in loop. // it's readable have constant. var bufferlength = 255; // somewhere need: var keydata; var temp; var ikeylen; // here, give array size. it's done in javascript. // alternatively, fill 256 values anywhere. astate0[bufferlength] = 0; // console.log(state0.length) print 256 // ... // init i, j, ikeylen ... // ... (iloop = 0; iloop <= bufferlength; iloop++) { // this: // m_pucstate1 = m_aucstate0 + i; // m_ucj += *m_pucstate1 + *(puckeydata+m_uci); // becomes: state1index = iloop; j += astate0[state1index] + keydata[i]; // this: // m_pucstate2 = m_aucstate0 + m_ucj; // becomes: state2index = j; // this: // m_uctemp = *m_pucstate1; // *m_pucstate1 = *m_pucstate2; // *m_pucstate2 = m_uctemp; // becomes: temp = astate0[state1index]; astate0[state1index] = astate0[state2index]; astate0[state2index] = temp; // this: // m_uci = (m_uci + 1) % ikeylen; // becomes: = (i+1) % ikeylen; } // this: // memcpy(m_aucstate, m_aucstate0, 256); // clone. you'd need jquery or else. can write: (index in state0) { state[index] = state0[index]; }

finally, can drop j equal state2index, , state1index equal iloop.

but puzzle have utilize paper , pencil , draw boxes , arrows clear with.

hth :-)

javascript c arrays pointers

No comments:

Post a Comment