pluie-bin/lib/file.js
2016-12-19 00:39:08 +01:00

63 lines
2.3 KiB
JavaScript

module.exports = function (ns) {
require('./header')(ns);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.File = function(binary, header) {
this.header = header;
this.binary = binary;
this.extractProperties = function(binary) {
if (!binary) binary = this.binary;
if (binary != null && binary.length >= this.header.def.length) {
this.header.def.map.forEach (function(prop) {
var bytes = binary.slice(prop.offset, prop.offset+prop.length);
var unpacked = ns.unpack(prop.size, prop.type, bytes);
prop.value = unpacked;
});
}
};
this.buildProperties = function(properties) {
var data = [];
this.header.buildProperties(properties);
this.header.def.map.forEach (function(prop) {
var packed = ns.pack(prop.size, prop.type, prop.value);
data.push(packed);
});
data = ns.mergeBuffers(data);
};
this.extractProperties(binary);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.smpFile = function(binary) {
return new ns.File(binary, new ns.Header([
['signature', 'a', 9, '∢SMP0.2'],
['size' , 'N', 1, ''],
['id' , 'N', 1, ''],
['from' , 'a', 32, ''],
['to' , 'a', 32, ''],
['channel' , 'a', 32, ''],
['command' , 'n', 1, ''],
['index' , 'n', 1, ''],
['split' , 'n', 1, '']
]));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.smpsFile = function(binary) {
return new ns.File(binary, new ns.Header([
['signature', 'a', 13, 'ﷸSMPS0.2∢'],
['size' , 'N', 1, ''],
['nonce' , 'a', 12, ''],
['cypher' , 'a', 135, '']
]));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.smpaFile = function(binary) {
return new ns.BaseFile(binary, new ns.Header([
['signature', 'a', 13, 'ﷸSMPA0.2∢'],
['size' , 'N', 1, ''],
['nonce' , 'a', 24, ''],
['cypher' , 'a', 199, '']
]));
}
}