javascript - c 指针和 javascript

标签 javascript c arrays pointers

我一直致力于将 C 代码转换为 javascript。但它们只是不返​​回相同的数据。

我有一个关于如何处理指针的想法。在 javascript 中,我将创建一个数组。

Note: This are not the full code, only partials

来源:

// 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 = []


for (var i = 0; i < 256; i++)
{
  this.m_pucState1 = this.m_aucState0 + i
  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)

所以我的想法是因为指针返回一个地址,该地址包含指针数据的第一个字节。因此,如果在一个数组中,我也可以指向数组的第一个索引,对吗?

我上面的做法对吗?


为了上下文让我添加 1 个函数:

Javascript:

Crypt.prototype.setup = function(pucKeyData, iKeyLen) {
  if (iKeyLen < 1)
    throw new Error("Key Length should be at least 1")

  var i;

  for (i = 0; i < 256; i++)
    this.m_aucState0[i] = i

  this.m_ucI = 0
  this.m_ucJ = 0

  for (var i = 0; i < 256; i++)
  {
    this.m_pucState1 = this.m_aucState0 + i
    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 be at least 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;   
}  

m_pucState1*m_pucState1有什么区别:

m_pucState1 = m_aucState + m_ucI;   
m_ucJ += *m_pucState1; 

最佳答案

在 Javascript 中,有类型化的缓冲区对象:http://www.javascripture.com/ArrayBuffer

您还会找到有关 ctypes 集合的一些信息,但据我所知,它们仅用于 native 操作系统库调用。

此外,我不知道像您提到的那样的原生 JS Buffer 对象。 NodeJS里面有,不知道有什么特点。

如果您坚持一个一个地翻译您的代码,那么这些类型化的缓冲区对象可能会极大地支持您。我认为这不是一个好方法,因为从 C 翻译到 Javascript 时,您的术语无论如何都会改变。它从添加长指针值变为形成数组索引

这是您翻译中的一个问题示例:

在 C 中,你写:

m_ucJ += *m_pucState1 + *(pucKeyData+m_ucI);   

在 Javascript 中,你写:

this.m_ucJ += this.m_pucState1[0] + (pucKeyData[0] + this.m_ucI);

C 术语中的括号使 m_ucI 改变了地址。所以在 Javascript 中,这应该放在方括号中,就像这样:

this.m_ucJ += this.m_pucState1[0] + pucKeyData[0 + this.m_ucI];

然后就可以跳过“0+”了。这表明这些不同语言之间的一对一翻译是如何充满陷阱的。

因此,假设我们将使用最简单的 Javascript 对象,即数组 []。那么这是我的建议。这是一个草稿,但它应该给你一个完整的想法:

// 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 is no such thing as "uc" in Javascript.
var j; // m_ucj 
var iLoop; // i in loop.

// It's readable to have this constant.
var bufferLength = 255;

// Somewhere we need:
var keyData;
var temp;
var iKeyLen;


// Just for here, give the array a size. So it's done in Javascript. 
// Alternatively, fill it with 256 values from anywhere.
aState0[bufferLength] = 0;
// console.log(state0.length) will now print 256

// ...
// init i, j, iKeyLen ... 
// ...


for (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 = (i+1) % iKeyLen;    

}

// this:
// memcpy(m_aucState, m_aucState0, 256); 
// would be a clone. So you'd need jQuery or else. But you can simply write:
for (index in state0) {
    state[index] = state0[index];
}

最后,您可以删除 j,因为它等于 state2Index,而 state1Index 等于 iLoop

但这是一个谜题,您可能需要使用纸和铅笔并绘制一些方框和箭头才能弄清楚。

Hth :-)

关于javascript - c 指针和 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24348094/

相关文章:

c - 在不忙等待的情况下实现互斥锁

jquery - 必须访问 jquery 数组中的单个项目,然后模拟单击事件 (javascript)

java - 如何获取用于构造 String 的 byte[]?

javascript - AngularJS 加载服务然后调用 Controller 并渲染

javascript - 谷歌地图可以设置为慢速恒平移吗?就像一场全局革命?

javascript - 无法在 .js 文件中命中断点

java - 在不创建新副本的情况下获取数组的一部分

javascript - 作为对象参数的函数

C 程序读取/写入文件,同时递增文件中的数字

在 Julia 基准教程中编译 C 代码