pluie-bin/dist/pluie-bin.js

368 lines
11 KiB
JavaScript
Raw Normal View History

2016-12-18 13:44:45 +00:00
(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){
2016-12-18 22:50:34 +00:00
if (typeof pluie == 'undefined') {
pluie = { bin : {}};
}
require('./lib/bin')(pluie.bin);
module.exports = pluie;
2016-12-18 13:44:45 +00:00
},{"./lib/bin":2}],2:[function(require,module,exports){
2016-12-18 22:50:34 +00:00
module.exports = function (ns) {
2016-12-18 23:39:08 +00:00
require('./encoder')(ns);
2016-12-18 13:44:45 +00:00
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
String.prototype.removeEndNullBytes = function() {
return this.replace(/\0+$/g, '');
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
String.prototype.removeStartNullBytes = function() {
return this.replace(/^\0+/g, '');
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 23:39:08 +00:00
ns.enc = new ns.TextEncoder("utf-8");
ns.dec = new ns.TextDecoder("utf-8", {fatal: true});
2016-12-18 22:50:34 +00:00
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ns.intFromBytes = function(bytes)
2016-12-18 13:44:45 +00:00
{
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;
2016-12-18 22:50:34 +00:00
};
2016-12-18 13:44:45 +00:00
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
ns.bytesFromInt = function(value, size)
2016-12-18 13:44:45 +00:00
{
var bytes = new Uint8Array(size);
while (value) {
bytes.set([value & 255], --size);
value >>= 8;
}
return bytes;
2016-12-18 22:50:34 +00:00
};
2016-12-18 13:44:45 +00:00
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
ns.pack = function(size, type, value)
2016-12-18 13:44:45 +00:00
{
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;
2016-12-18 22:50:34 +00:00
};
2016-12-18 13:44:45 +00:00
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
ns.unpack = function(size, type, bytes)
2016-12-18 13:44:45 +00:00
{
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;
2016-12-18 22:50:34 +00:00
};
2016-12-18 13:44:45 +00:00
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
ns.mergeBuffers = function(bufferArray)
2016-12-18 13:44:45 +00:00
{
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;
2016-12-18 22:50:34 +00:00
};
2016-12-18 13:44:45 +00:00
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
ns.getBuffer = function(array, size)
2016-12-18 13:44:45 +00:00
{
var buffer = new Uint8Array(!size ? array.length : size);
buffer.set(new Uint8Array(array), 0);
return buffer;
2016-12-18 22:50:34 +00:00
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
require('./file')(ns);
return ns;
2016-12-18 13:44:45 +00:00
}
2016-12-18 23:39:08 +00:00
},{"./encoder":3,"./file":4}],3:[function(require,module,exports){
function TextEncoderLite() {
}
function TextDecoderLite() {
}
(function () {
2016-12-18 22:50:34 +00:00
'use strict';
2016-12-18 23:39:08 +00:00
// 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){
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, '']
]));
}
}
2016-12-18 23:39:08 +00:00
},{"./header":5}],5:[function(require,module,exports){
2016-12-18 22:50:34 +00:00
module.exports = function (ns) {
2016-12-18 13:44:45 +00:00
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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]];
}
};
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2016-12-18 22:50:34 +00:00
ns.Header = function(def) {
2016-12-18 13:44:45 +00:00
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]);