多态参数
输入多态性
// ES6 syntax: import koffi from 'koffi';
const koffi = require('koffi');
const lib = koffi.load('libc.so.6');
const FILE = koffi.opaque('FILE');
const PngHeader = koffi.pack('PngHeader', {
signature: koffi.array('uint8_t', 8),
ihdr: koffi.pack({
length: 'uint32_be_t',
chunk: koffi.array('char', 4),
width: 'uint32_be_t',
height: 'uint32_be_t',
depth: 'uint8_t',
color: 'uint8_t',
compression: 'uint8_t',
filter: 'uint8_t',
interlace: 'uint8_t',
crc: 'uint32_be_t'
})
});
const fopen = lib.func('FILE *fopen(const char *path, const char *mode)');
const fclose = lib.func('int fclose(FILE *fp)');
const fread = lib.func('size_t fread(_Out_ void *ptr, size_t size, size_t nmemb, FILE *fp)');
let filename = process.argv[2];
if (filename == null)
throw new Error('Usage: node png.js <image.png>');
let hdr = {};
{
let fp = fopen(filename, 'rb');
if (!fp)
throw new Error(`Failed to open '${filename}'`);
try {
let len = fread(koffi.as(hdr, 'PngHeader *'), 1, koffi.sizeof(PngHeader), fp);
if (len < koffi.sizeof(PngHeader))
throw new Error('Failed to read PNG header');
} finally {
fclose(fp);
}
}
console.log('PNG header:', hdr);
输出缓冲器
Last updated