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

368 lines
11 KiB
JavaScript
Raw Permalink 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){
module.exports = function (ns) {
require('./encoder')(ns);
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
String.prototype.removeEndNullBytes = function() {
return this.replace(/\0+$/g, '');
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
String.prototype.removeStartNullBytes = function() {
return this.replace(/^\0+/g, '');
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.enc = new ns.TextEncoder("utf-8");
ns.dec = new ns.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;
}
},{"./encoder":3,"./file":4}],3:[function(require,module,exports){
function TextEncoderLite() {
}
function TextDecoderLite() {
}
(function () {
'use strict';
// Taken from https://github.com/feross/buffer/blob/master/index.js
// Thanks Feross et al! :-)
function utf8ToBytes (string, units) {
units = units || Infinity
var codePoint
var length = string.length
var leadSurrogate = null
var bytes = []
var i = 0
for (; i < length; i++) {
codePoint = string.charCodeAt(i)
// is surrogate component
if (codePoint > 0xD7FF && codePoint < 0xE000) {
// last char was a lead
if (leadSurrogate) {
// 2 leads in a row
if (codePoint < 0xDC00) {
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
leadSurrogate = codePoint
continue
} else {
// valid surrogate pair
codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000
leadSurrogate = null
}
} else {
// no lead yet
if (codePoint > 0xDBFF) {
// unexpected trail
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
continue
} else if (i + 1 === length) {
// unpaired lead
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
continue
} else {
// valid lead
leadSurrogate = codePoint
continue
}
}
} else if (leadSurrogate) {
// valid bmp char, but last char was a lead
if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD)
leadSurrogate = null
}
// encode utf8
if (codePoint < 0x80) {
if ((units -= 1) < 0) break
bytes.push(codePoint)
} else if (codePoint < 0x800) {
if ((units -= 2) < 0) break
bytes.push(
codePoint >> 0x6 | 0xC0,
codePoint & 0x3F | 0x80
)
} else if (codePoint < 0x10000) {
if ((units -= 3) < 0) break
bytes.push(
codePoint >> 0xC | 0xE0,
codePoint >> 0x6 & 0x3F | 0x80,
codePoint & 0x3F | 0x80
)
} else if (codePoint < 0x200000) {
if ((units -= 4) < 0) break
bytes.push(
codePoint >> 0x12 | 0xF0,
codePoint >> 0xC & 0x3F | 0x80,
codePoint >> 0x6 & 0x3F | 0x80,
codePoint & 0x3F | 0x80
)
} else {
throw new Error('Invalid code point')
}
}
return bytes
}
function utf8Slice (buf, start, end) {
var res = ''
var tmp = ''
end = Math.min(buf.length, end || Infinity)
start = start || 0;
for (var i = start; i < end; i++) {
if (buf[i] <= 0x7F) {
res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i])
tmp = ''
} else {
tmp += '%' + buf[i].toString(16)
}
}
return res + decodeUtf8Char(tmp)
}
function decodeUtf8Char (str) {
try {
return decodeURIComponent(str)
} catch (err) {
return String.fromCharCode(0xFFFD) // UTF 8 invalid char
}
}
TextEncoderLite.prototype.encode = function (str) {
var result;
if ('undefined' === typeof Uint8Array) {
result = utf8ToBytes(str);
} else {
result = new Uint8Array(utf8ToBytes(str));
}
return result;
};
TextDecoderLite.prototype.decode = function (bytes) {
return utf8Slice(bytes, 0, bytes.length);
}
}());
module.exports = function(ns) {
ns.TextEncoder = TextEncoderLite;
ns.TextDecoder = TextDecoderLite;
}
},{}],4:[function(require,module,exports){
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":5}],5:[function(require,module,exports){
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]);