pluie-bin/lib/file.js

63 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-12-18 22:50:34 +00:00
module.exports = function (ns) {
require('./header')(ns);
2016-12-18 13:44:45 +00:00
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
ns.File = function(binary, header) {
2016-12-18 13:44:45 +00:00
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);
2016-12-18 22:50:34 +00:00
var unpacked = ns.unpack(prop.size, prop.type, bytes);
2016-12-18 13:44:45 +00:00
prop.value = unpacked;
});
}
};
this.buildProperties = function(properties) {
var data = [];
this.header.buildProperties(properties);
this.header.def.map.forEach (function(prop) {
2016-12-18 22:50:34 +00:00
var packed = ns.pack(prop.size, prop.type, prop.value);
2016-12-18 13:44:45 +00:00
data.push(packed);
});
2016-12-18 22:50:34 +00:00
data = ns.mergeBuffers(data);
2016-12-18 13:44:45 +00:00
};
this.extractProperties(binary);
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
ns.smpFile = function(binary) {
return new ns.File(binary, new ns.Header([
2016-12-18 13:44:45 +00:00
['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, '']
]));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
ns.smpsFile = function(binary) {
return new ns.File(binary, new ns.Header([
2016-12-18 13:44:45 +00:00
['signature', 'a', 13, 'ﷸSMPS0.2∢'],
['size' , 'N', 1, ''],
['nonce' , 'a', 12, ''],
['cypher' , 'a', 135, '']
]));
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
ns.smpaFile = function(binary) {
return new ns.BaseFile(binary, new ns.Header([
2016-12-18 13:44:45 +00:00
['signature', 'a', 13, 'ﷸSMPA0.2∢'],
['size' , 'N', 1, ''],
['nonce' , 'a', 24, ''],
['cypher' , 'a', 199, '']
]));
}
}