Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C Buffer #6

Closed
lukouko opened this issue Mar 10, 2015 · 1 comment
Closed

C Buffer #6

lukouko opened this issue Mar 10, 2015 · 1 comment

Comments

@lukouko
Copy link

lukouko commented Mar 10, 2015

I cannot seem to figure this one out. I'm sure the answer is simple, I just cannot get it right. Any help would be appreciated.

I'm using ref-array with node-ffi. I have a c library function which has a signature like this:

var ArrayType = require('ref-array')
var int8Array = ArrayType(ref.types.int8, 10000);
var uint32Ptr = ref.refType('uint32');

var libmylibrary = ffi.Library('libmylibrary', { ...,
  'readData': [ 'int' , [int8Array, uint32Ptr ] ]
});

readData populates the int8Array with a collection of header(uint16) and body (basically structures) pairs. The uint32Ptr argument is populated with the total number of bytes written to the passed int8Array.

My problem is that after I call readData, I cannot figure out how to reinterpret pieces of the int8Array as another data type. I'm fairly confident that the readData call is populating the array correctly.

I need to do something like this:

libmylibrary.readData(int8ArrayInst, uint32PtrInst);

var currentPosi = 0;
var endPosi = uint32PtrInst.deref();
while (currentPosi < endPosi) {
    var bodyLength = howToDoThis.reinterpret(int8ArrayInst, currentPosi, ref.types.uint16);
    currentPosi += ref.types.uint16.size;

    var bodyData = howToDoThis.reinterpret(int8ArrayInst, currentPosi, myTypes.someStruct);
    currentPosi += bodyLength;
}

Is this possible?

@TooTallNate
Copy link
Owner

So rather than using int8Array type, just specify "pointer" (or "void *"), and pass in a Buffer instance big enough to hold your data (10000 bytes it looks like from the code you've given).

Then, you'd basically use the built-in Buffer read functions (readUint16LE(), etc.) to read out the values, incrementing the position as you go along. You said that the body is basically a struct, so you might be able to utilize ref-struct here as well.

Hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants