node ffi works

pull/1/head
Avril 5 years ago
parent 2725851a6b
commit 87b60a0e9c
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -2,21 +2,10 @@ const ffi = require('ffi');
const struct = require('ref-struct');
const ref = require('ref');
const libhkana = ffi.Library('libkana_hash', {
'_kana_new_salt': ['int', ['pointer', 'long', 'pointer']],
'_kana_free_salt': ['int', ['pointer']],
'_kana_do': ['int', ['pointer', 'long', 'pointer', 'pointer', 'long']],
'_kana_length': ['int', ['pointer', 'long', 'pointer', 'pointer']],
});
const Kana = require('./kana');
const Salt = require('./salt');
let buffer= ref.alloc('long');
let sz = ref.alloc('long');
console.log(libhkana._kana_length(buffer, 2, null, sz));
console.log(sz.deref());
//console.log(new Kana(Kana.CRC64, new Salt("hello lolis")).once("hello worldチそぬとね"));
let output = new Buffer(sz);
console.log(libhkana._kana_do(buffer, 2, null, output, sz));
console.log(ref.readCString(output, 0));
module.exports.Kana = Kana;
module.exports.Salt = Salt;

@ -2,9 +2,110 @@ const ffi = require('ffi');
const struct = require('ref-struct');
const ref = require('ref');
const Salt = module.exports.Salt = struct({
'size': 'pointer',
const Salt = struct({
'type': 'char',
'size': 'int',
'body': 'pointer',
});
const PSalt = module.exports.PSalt = ref.refType(Salt);
const PSalt = ref.refType(Salt);
const Context = struct({
'algo': 'char',
'salt': Salt,
});
const PContext = ref.refType(Context);
const PLong = ref.refType(ref.types.long);
const lib = ffi.Library('libkhash', {
'khash_new_context': ['int', ['char', 'char', 'string', 'long', PContext]],
'khash_free_context': ['int', [PContext]],
'khash_clone_context': ['int', [PContext, PContext]],
'khash_length': ['int', [PContext, 'string', 'long', PLong]],
'khash_do': ['int', [PContext, 'string', 'long', 'string', 'long']],
});
const ctx_create = (algo, salt, salt_ref, salt_sz) => {
let ctx_ptr = ref.alloc(Context);
lib.khash_new_context(algo, salt, salt_ref, salt_sz, ctx_ptr);
return ctx_ptr;
};
const ctx_free = (ptr) => {
lib.khash_free_context(ptr);
return ptr.deref();
};
const ctx_clone = (src) => {
let dst = ref.alloc(Context);
lib.khash_clone_context(src,dst);
return dst;
};
const khash_length = (ctx, jsstring) => {
let string = ref.allocCString(jsstring);
let len = ref.alloc('long');
lib.khash_length(ctx, string, string.length, len);
return len.deref();
};
const khash_do = (ctx, jsstring, len) => {
let string = ref.allocCString(jsstring);
let buffer = Buffer.alloc(len+1);
lib.khash_do(ctx,string,string.length,buffer,len);
return ref.readCString(buffer,0);
};
const get_salt_type = (salt) => {
if (salt && salt.tag) {
switch(salt.tag)
{
case '__NONE':
return Kana.SALT_NONE;
case '__RANDOM':
return Kana.SALT_RANDOM;
case '__SPECIFIC':
return Kana.SALT_SPECIFIC;
default:
return Kana.SALT_DEFAULT;
}
}
else return Kana.SALT_DEFAULT;
};
function Kana(algo, salt)
{
const stype = get_salt_type(salt);
const fbuffer = salt ? salt.buffer || null : null;
this.ctx = ctx_create(algo || 0, stype, fbuffer, fbuffer ? fbuffer.length : 0);
}
const K = Kana.prototype;
K.finish = function() {
ctx_free(this.ctx);
};
K.once = function(string) {
let len = khash_length(this.ctx, string);
return khash_do(this.ctx, string, len);
};
Kana.ALGO_DEFAULT = 0;
Kana.ALGO_CRC32 = 1;
Kana.ALGO_CRC64 = 2;
Kana.ALGO_SHA256 = 3;
Kana.SALT_NONE = 0;
Kana.SALT_DEFAULT = 1;
Kana.SALT_SPECIFIC = 2;
Kana.SALT_RANDOM = 3;
module.exports = Kana;
/*
const Kana = require('./kana');
let buffer= ref.alloc('long');
let sz = ref.alloc('long');
console.log(libhkana._kana_length(buffer, 2, null, sz));
console.log(sz.deref());
let output = new Buffer(sz);
console.log(libhkana._kana_do(buffer, 2, null, output, sz));
console.log(ref.readCString(output, 0));*/

@ -0,0 +1,17 @@
const ref = require('ref');
function Salt(buffer, tag)
{
this.tag = tag || '__SPECIFIC';
if (buffer) {
this.buffer = ref.allocCString(buffer);
} else {
this.buffer = null;
}
}
Salt.None = new Salt(null, '__NONE');
Salt.Random = new Salt(null, '__RANDOM');
Salt.Default = new Salt(null, '__DEFAULT');
module.exports = Salt;
Loading…
Cancel
Save