pluie-bin/dist/pluie-bin.js
2016-12-18 23:50:34 +01:00

230 lines
7.9 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
if (typeof pluie == 'undefined') {
pluie = { bin : {}};
}
require('./lib/bin')(pluie.bin);
module.exports = pluie;
},{"./lib/bin":2}],2:[function(require,module,exports){
'use strict';
module.exports = function (ns) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
String.prototype.removeEndNullBytes = function() {
return this.replace(/\0+$/g, '');
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
String.prototype.removeStartNullBytes = function() {
return this.replace(/^\0+/g, '');
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.enc = new TextEncoder("utf-8");
ns.dec = new TextDecoder("utf-8", {fatal: true});
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.intFromBytes = function(bytes)
{
var value = null;
if (bytes!=null && bytes.length > 0) {
// Big Endian
for (var i = value = 0, lim = bytes.length; i < lim; i++) {
value = value * 256 + bytes[i];
}
}
return value;
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.bytesFromInt = function(value, size)
{
var bytes = new Uint8Array(size);
while (value) {
bytes.set([value & 255], --size);
value >>= 8;
}
return bytes;
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.pack = function(size, type, value)
{
var packed = null
if (type == 'n' || type == 'N') {
// js int max on 32 signed bits
if (value >= 0 && value <= 2147483647) {
packed = this.bytesFromInt(value, type == 'n' ? 2 : 4);
}
}
else if (type == 'a') {
packed = new Uint8Array(size);
packed.set(this.enc.encode(value), 0);
}
return packed;
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.unpack = function(size, type, bytes)
{
var unpacked = null
if (type == 'n' || type == 'N') {
var selByte = bytes.slice(0, size*(type == 'n' ? 2 : 4));
unpacked = this.intFromBytes(selByte);
}
else if (type == 'a') {
unpacked = this.dec.decode(bytes.slice(0, size)).removeEndNullBytes();
}
return unpacked;
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.mergeBuffers = function(bufferArray)
{
var maxLength = 0;
for(var i=0, lim=bufferArray.length; i < lim; i++) {
if (bufferArray[i] != null) {
maxLength += bufferArray[i].byteLength;
}
}
var tmp = new Uint8Array(maxLength);
for(var i=0, pos = 0, lim=bufferArray.length; i < lim; i++) {
if (bufferArray[i] != null) {
tmp.set(new Uint8Array(bufferArray[i]), pos);
pos += bufferArray[i].byteLength;
}
}
return tmp;
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.getBuffer = function(array, size)
{
var buffer = new Uint8Array(!size ? array.length : size);
buffer.set(new Uint8Array(array), 0);
return buffer;
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
require('./file')(ns);
return ns;
}
},{"./file":3}],3:[function(require,module,exports){
'use strict';
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, '']
]));
}
}
},{"./header":4}],4:[function(require,module,exports){
'use strict';
module.exports = function (ns) {
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var HeaderProperty = function(name, type, size, defaultValue)
{
this.name = name;
this.type = type;
this.size = size;
this.length = type == 'N' ? size*4 : (type == 'n' ? size*2 : size );
this.value = defaultValue;
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
var HeaderDef = function(mapData)
{
var map = [];
mapData.forEach(function (def) {
map.push(new HeaderProperty(def[0], def[1], def[2], def[3]));
});
this.map = map;
var keys = [];
var indexes = {};
var offset = 0;
var index = 0;
this.map.forEach(function(prop) {
prop.offset = offset;
keys[keys.length] = prop.name;
offset += prop.length;
indexes[prop.name] = index++;
});
this.indexes = indexes;
this.keys = keys;
this.length = offset;
this.getProperty = function(key) {
return this.map[this.indexes[key]];
}
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.Header = function(def) {
this.def = new HeaderDef(def);
this.value = function(key) {
return this.def.getProperty(key).value;
};
this.buildProperties = function(properties) {
this.def.map.forEach(function(prop) {
if (prop.name != 'signature') {
if (typeof properties[prop.name] != 'undefined') {
prop.value = properties[prop.name];
}
}
});
};
}
}
},{}]},{},[1]);