mirror of https://github.com/Cesura/pastey.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
9868 lines
280 KiB
9868 lines
280 KiB
(function(e){if("function"==typeof bootstrap)bootstrap("fernet",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeFernet=e}else"undefined"!=typeof window?window.fernet=e():global.fernet=e()})(function(){var define,ses,bootstrap,module,exports;
|
|
return (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);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.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){
|
|
var Buffer=require("__browserify_Buffer").Buffer;var CryptoJS = require('crypto-js/core');
|
|
var AES = require('crypto-js/aes');
|
|
var Utf8 = require('crypto-js/enc-utf8');
|
|
var Latin1 = require('crypto-js/enc-latin1');
|
|
var Hex = require('crypto-js/enc-hex');
|
|
var Base64 = require('crypto-js/enc-base64');
|
|
var HmacSHA256 = require('crypto-js/hmac-sha256');
|
|
var URLBase64 = require('urlsafe-base64');
|
|
var crypto = require('crypto');
|
|
|
|
//lpad a string for some hex conversions
|
|
String.prototype.lpad = function (padString, length) {
|
|
var str = this;
|
|
while (str.length < length) str = padString + str;
|
|
return str;
|
|
}
|
|
|
|
//Makes a Base64 string a url-safe base64 string
|
|
var urlsafe = function urlsafe(string) {
|
|
return string.replace(/\+/g, '-').replace(/\//g, '_') //.replace(/=+$/, '')
|
|
}
|
|
|
|
// parse a Hex string to an Int
|
|
var parseHex = function parseHex(hexString) {
|
|
return parseInt('0x' + hexString);
|
|
}
|
|
|
|
// turn bits into number of chars in a hex string
|
|
var hexBits = function hexBits(bits) {
|
|
return bits / 8 * 2;
|
|
}
|
|
|
|
// convert base64 string to hex string
|
|
var decode64toHex = function decode64(string) {
|
|
var s = URLBase64.decode(string.replace(/=+$/, ''));
|
|
return (new Buffer(s)).toString('hex');
|
|
}
|
|
|
|
// convert array to hex string
|
|
var ArrayToHex = function ArrayToHex(array) {
|
|
var hex = '';
|
|
for (var _byte in array) {
|
|
hex += Number(_byte).toString(16).lpad('0', 2);
|
|
}
|
|
return hex;
|
|
}
|
|
|
|
var randomHex = function (size) {
|
|
return crypto.randomBytes(128 / 8).toString('hex')
|
|
}
|
|
|
|
var setIV = function setIV(iv_array) {
|
|
if (iv_array) {
|
|
this.ivHex = ArrayToHex(iv_array);
|
|
} else {
|
|
this.ivHex = randomHex(128 / 8);
|
|
}
|
|
this.iv = Hex.parse(this.ivHex);
|
|
return this.ivHex;
|
|
}
|
|
|
|
//convert Time object or now into WordArray
|
|
var timeBytes = function timeBytes(time) {
|
|
if (time) {
|
|
time = (time / 1000)
|
|
} else {
|
|
time = (Math.round(new Date() / 1000))
|
|
}
|
|
var hexTime = time.toString(16).lpad('0', '16')
|
|
return Hex.parse(hexTime);
|
|
}
|
|
|
|
|
|
var fernet = function fernet(opts) {
|
|
this.Hex = Hex;
|
|
this.Base64 = Base64;
|
|
this.parseHex = parseHex;
|
|
this.decode64toHex = decode64toHex;
|
|
this.hexBits = hexBits;
|
|
this.urlsafe = urlsafe;
|
|
|
|
//Sets the secret from base64 encoded value
|
|
this.setSecret = function setSecret(secret64) {
|
|
this.secret = new this.Secret(secret64);
|
|
return this.secret;
|
|
}
|
|
|
|
this.ArrayToHex = ArrayToHex;
|
|
this.setIV = setIV;
|
|
|
|
this.encryptMessage = function (message, encryptionKey, iv) {
|
|
var encrypted = AES.encrypt(message, encryptionKey, { iv: iv });
|
|
return encrypted.ciphertext;
|
|
}
|
|
|
|
this.decryptMessage = function (cipherText, encryptionKey, iv) {
|
|
var encrypted = {};
|
|
encrypted.key = encryptionKey;
|
|
encrypted.iv = iv;
|
|
encrypted.ciphertext = cipherText;
|
|
|
|
var decrypted = AES.decrypt(encrypted, encryptionKey, { iv: iv });
|
|
|
|
return decrypted.toString(Utf8);
|
|
}
|
|
|
|
this.timeBytes = timeBytes;
|
|
|
|
this.createToken = function (signingKey, time, iv, cipherText) {
|
|
var hmac = this.createHmac(signingKey, time, iv, cipherText);
|
|
var tokenWords = Hex.parse(this.versionHex);
|
|
tokenWords = tokenWords.concat(time);
|
|
tokenWords = tokenWords.concat(iv);
|
|
tokenWords = tokenWords.concat(cipherText);
|
|
tokenWords = tokenWords.concat(hmac);
|
|
return urlsafe(tokenWords.toString(Base64));
|
|
}
|
|
|
|
this.createHmac = function createHmac(signingKey, time, iv, cipherText) {
|
|
var hmacWords = Hex.parse(this.versionHex);
|
|
hmacWords = hmacWords.concat(time);
|
|
hmacWords = hmacWords.concat(iv);
|
|
hmacWords = hmacWords.concat(cipherText);
|
|
return HmacSHA256(hmacWords, signingKey);
|
|
}
|
|
|
|
this.Secret = require('./lib/secret');
|
|
this.Token = require('./lib/token')(this);
|
|
|
|
opts = opts || {};
|
|
this.ttl = opts.ttl || 60;
|
|
// because (0 || x) always equals x
|
|
if (opts.ttl === 0) this.ttl = 0;
|
|
this.versionHex = '80';
|
|
this.setIV(opts.iv);
|
|
if (opts.secret) { this.setSecret(opts.secret) }
|
|
}
|
|
|
|
exports = module.exports = fernet;
|
|
fernet.call(exports)
|
|
|
|
},{"./lib/secret":2,"./lib/token":3,"__browserify_Buffer":29,"crypto":11,"crypto-js/aes":16,"crypto-js/core":18,"crypto-js/enc-base64":19,"crypto-js/enc-hex":20,"crypto-js/enc-latin1":21,"crypto-js/enc-utf8":22,"crypto-js/hmac-sha256":24,"urlsafe-base64":31}],2:[function(require,module,exports){
|
|
var f = require('../fernet');
|
|
|
|
var Secret = function (secret64) {
|
|
var secret = f.decode64toHex(secret64);
|
|
if (secret.length !== f.hexBits(256)) {
|
|
throw new Error('Secret must be 32 url-safe base64-encoded bytes.');
|
|
}
|
|
this.signingKeyHex = secret.slice(0, f.hexBits(128));
|
|
this.signingKey = f.Hex.parse(this.signingKeyHex);
|
|
this.encryptionKeyHex = secret.slice(f.hexBits(128));
|
|
this.encryptionKey = f.Hex.parse(this.encryptionKeyHex);
|
|
}
|
|
|
|
exports = module.exports = Secret;
|
|
|
|
},{"../fernet":1}],3:[function(require,module,exports){
|
|
var fernet = require('../fernet');
|
|
|
|
//TokenFoctory
|
|
module = module.exports = function (parent) {
|
|
var Token = function Token(opts) {
|
|
opts = opts || {};
|
|
this.secret = opts.secret || parent.secret;
|
|
this.ttl = opts.ttl || parent.ttl;
|
|
if (opts.ttl === 0) this.ttl = 0;
|
|
this.message = opts.message;
|
|
this.cipherText = opts.cipherText;
|
|
this.token = opts.token;
|
|
this.version = opts.version || fernet.parseHex(parent.versionHex);
|
|
this.optsIV = opts.iv;
|
|
this.maxClockSkew = 60;
|
|
if (opts.time) this.setTime(Date.parse(opts.time));
|
|
else this.setTime();
|
|
}
|
|
|
|
Token.prototype = {
|
|
setIV: fernet.setIV,
|
|
setTime: function tokenSetTime(time) {
|
|
this.time = fernet.timeBytes(time);
|
|
},
|
|
toString: function tokenToString() {
|
|
if (this.encoded) {
|
|
return this.token
|
|
} else {
|
|
return this.message
|
|
}
|
|
},
|
|
encode: function encodeToken(message) {
|
|
if (!this.secret) throw (new Error("Secret not set"));
|
|
this.encoded = true;
|
|
this.setIV(this.optsIV); //if null will always be a fresh IV
|
|
this.message = message || this.message;
|
|
this.cipherText = fernet.encryptMessage(this.message, this.secret.encryptionKey, this.iv);
|
|
this.token = fernet.createToken(this.secret.signingKey, this.time, this.iv, this.cipherText)
|
|
return this.token;
|
|
},
|
|
decode: function decodeToken(token) {
|
|
if (!this.secret) throw (new Error("Secret not set"));
|
|
this.encoded = false;
|
|
this.token = token || this.token;
|
|
|
|
var tokenString = fernet.decode64toHex(this.token);
|
|
var versionOffset = fernet.hexBits(8);
|
|
var timeOffset = versionOffset + fernet.hexBits(64);
|
|
var ivOffset = timeOffset + fernet.hexBits(128);
|
|
var hmacOffset = tokenString.length - fernet.hexBits(256);
|
|
var timeInt = fernet.parseHex(tokenString.slice(versionOffset, timeOffset));
|
|
|
|
this.version = fernet.parseHex(tokenString.slice(0, versionOffset));
|
|
|
|
if (this.version != 128) {
|
|
throw new Error("Invalid version");
|
|
}
|
|
|
|
this.time = new Date(timeInt * 1000);
|
|
|
|
var currentTime = new Date()
|
|
var timeDiff = (currentTime - this.time) / 1000;
|
|
|
|
if (this.ttl > 0) {
|
|
if (timeDiff > this.ttl) {
|
|
throw new Error("Invalid Token: TTL");
|
|
}
|
|
|
|
if (((currentTime / 1000) + this.maxClockSkew) < timeInt) {
|
|
throw new Error("far-future timestamp");
|
|
}
|
|
}
|
|
|
|
this.ivHex = tokenString.slice(timeOffset, ivOffset);
|
|
this.iv = fernet.Hex.parse(this.ivHex);
|
|
this.cipherTextHex = tokenString.slice(ivOffset, hmacOffset);
|
|
this.cipherText = fernet.Hex.parse(this.cipherTextHex);
|
|
this.hmacHex = tokenString.slice(hmacOffset);
|
|
var decodedHmac = fernet.createHmac(this.secret.signingKey, fernet.timeBytes(this.time), this.iv, this.cipherText);
|
|
var decodedHmacHex = decodedHmac.toString(fernet.Hex);
|
|
|
|
var accum = 0
|
|
for (var i = 0; i < 64; i++) {
|
|
accum += decodedHmacHex.charCodeAt(i) ^ this.hmacHex.charCodeAt(i)
|
|
}
|
|
if (accum != 0) throw new Error("Invalid Token: HMAC");
|
|
|
|
this.message = fernet.decryptMessage(this.cipherText, this.secret.encryptionKey, this.iv)
|
|
return this.message;
|
|
}
|
|
}
|
|
|
|
return Token;
|
|
}
|
|
|
|
//exports = module.exports = Token;
|
|
|
|
},{"../fernet":1}],4:[function(require,module,exports){
|
|
(function (exports) {
|
|
'use strict';
|
|
|
|
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
|
|
function b64ToByteArray(b64) {
|
|
var i, j, l, tmp, placeHolders, arr;
|
|
|
|
if (b64.length % 4 > 0) {
|
|
throw 'Invalid string. Length must be a multiple of 4';
|
|
}
|
|
|
|
// the number of equal signs (place holders)
|
|
// if there are two placeholders, than the two characters before it
|
|
// represent one byte
|
|
// if there is only one, then the three characters before it represent 2 bytes
|
|
// this is just a cheap hack to not do indexOf twice
|
|
placeHolders = b64.indexOf('=');
|
|
placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0;
|
|
|
|
// base64 is 4/3 + up to two characters of the original data
|
|
arr = [];//new Uint8Array(b64.length * 3 / 4 - placeHolders);
|
|
|
|
// if there are placeholders, only get up to the last complete 4 chars
|
|
l = placeHolders > 0 ? b64.length - 4 : b64.length;
|
|
|
|
for (i = 0, j = 0; i < l; i += 4, j += 3) {
|
|
tmp = (lookup.indexOf(b64[i]) << 18) | (lookup.indexOf(b64[i + 1]) << 12) | (lookup.indexOf(b64[i + 2]) << 6) | lookup.indexOf(b64[i + 3]);
|
|
arr.push((tmp & 0xFF0000) >> 16);
|
|
arr.push((tmp & 0xFF00) >> 8);
|
|
arr.push(tmp & 0xFF);
|
|
}
|
|
|
|
if (placeHolders === 2) {
|
|
tmp = (lookup.indexOf(b64[i]) << 2) | (lookup.indexOf(b64[i + 1]) >> 4);
|
|
arr.push(tmp & 0xFF);
|
|
} else if (placeHolders === 1) {
|
|
tmp = (lookup.indexOf(b64[i]) << 10) | (lookup.indexOf(b64[i + 1]) << 4) | (lookup.indexOf(b64[i + 2]) >> 2);
|
|
arr.push((tmp >> 8) & 0xFF);
|
|
arr.push(tmp & 0xFF);
|
|
}
|
|
|
|
return arr;
|
|
}
|
|
|
|
function uint8ToBase64(uint8) {
|
|
var i,
|
|
extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
|
|
output = "",
|
|
temp, length;
|
|
|
|
function tripletToBase64 (num) {
|
|
return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F];
|
|
};
|
|
|
|
// go through the array every three bytes, we'll deal with trailing stuff later
|
|
for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
|
|
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
|
|
output += tripletToBase64(temp);
|
|
}
|
|
|
|
// pad the end with zeros, but make sure to not forget the extra bytes
|
|
switch (extraBytes) {
|
|
case 1:
|
|
temp = uint8[uint8.length - 1];
|
|
output += lookup[temp >> 2];
|
|
output += lookup[(temp << 4) & 0x3F];
|
|
output += '==';
|
|
break;
|
|
case 2:
|
|
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]);
|
|
output += lookup[temp >> 10];
|
|
output += lookup[(temp >> 4) & 0x3F];
|
|
output += lookup[(temp << 2) & 0x3F];
|
|
output += '=';
|
|
break;
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
module.exports.toByteArray = b64ToByteArray;
|
|
module.exports.fromByteArray = uint8ToBase64;
|
|
}());
|
|
|
|
},{}],5:[function(require,module,exports){
|
|
// UTILITY
|
|
var util = require('util');
|
|
var Buffer = require("buffer").Buffer;
|
|
var pSlice = Array.prototype.slice;
|
|
|
|
function objectKeys(object) {
|
|
if (Object.keys) return Object.keys(object);
|
|
var result = [];
|
|
for (var name in object) {
|
|
if (Object.prototype.hasOwnProperty.call(object, name)) {
|
|
result.push(name);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// 1. The assert module provides functions that throw
|
|
// AssertionError's when particular conditions are not met. The
|
|
// assert module must conform to the following interface.
|
|
|
|
var assert = module.exports = ok;
|
|
|
|
// 2. The AssertionError is defined in assert.
|
|
// new assert.AssertionError({ message: message,
|
|
// actual: actual,
|
|
// expected: expected })
|
|
|
|
assert.AssertionError = function AssertionError(options) {
|
|
this.name = 'AssertionError';
|
|
this.message = options.message;
|
|
this.actual = options.actual;
|
|
this.expected = options.expected;
|
|
this.operator = options.operator;
|
|
var stackStartFunction = options.stackStartFunction || fail;
|
|
|
|
if (Error.captureStackTrace) {
|
|
Error.captureStackTrace(this, stackStartFunction);
|
|
}
|
|
};
|
|
|
|
// assert.AssertionError instanceof Error
|
|
util.inherits(assert.AssertionError, Error);
|
|
|
|
function replacer(key, value) {
|
|
if (value === undefined) {
|
|
return '' + value;
|
|
}
|
|
if (typeof value === 'number' && (isNaN(value) || !isFinite(value))) {
|
|
return value.toString();
|
|
}
|
|
if (typeof value === 'function' || value instanceof RegExp) {
|
|
return value.toString();
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function truncate(s, n) {
|
|
if (typeof s == 'string') {
|
|
return s.length < n ? s : s.slice(0, n);
|
|
} else {
|
|
return s;
|
|
}
|
|
}
|
|
|
|
assert.AssertionError.prototype.toString = function() {
|
|
if (this.message) {
|
|
return [this.name + ':', this.message].join(' ');
|
|
} else {
|
|
return [
|
|
this.name + ':',
|
|
truncate(JSON.stringify(this.actual, replacer), 128),
|
|
this.operator,
|
|
truncate(JSON.stringify(this.expected, replacer), 128)
|
|
].join(' ');
|
|
}
|
|
};
|
|
|
|
// At present only the three keys mentioned above are used and
|
|
// understood by the spec. Implementations or sub modules can pass
|
|
// other keys to the AssertionError's constructor - they will be
|
|
// ignored.
|
|
|
|
// 3. All of the following functions must throw an AssertionError
|
|
// when a corresponding condition is not met, with a message that
|
|
// may be undefined if not provided. All assertion methods provide
|
|
// both the actual and expected values to the assertion error for
|
|
// display purposes.
|
|
|
|
function fail(actual, expected, message, operator, stackStartFunction) {
|
|
throw new assert.AssertionError({
|
|
message: message,
|
|
actual: actual,
|
|
expected: expected,
|
|
operator: operator,
|
|
stackStartFunction: stackStartFunction
|
|
});
|
|
}
|
|
|
|
// EXTENSION! allows for well behaved errors defined elsewhere.
|
|
assert.fail = fail;
|
|
|
|
// 4. Pure assertion tests whether a value is truthy, as determined
|
|
// by !!guard.
|
|
// assert.ok(guard, message_opt);
|
|
// This statement is equivalent to assert.equal(true, guard,
|
|
// message_opt);. To test strictly for the value true, use
|
|
// assert.strictEqual(true, guard, message_opt);.
|
|
|
|
function ok(value, message) {
|
|
if (!!!value) fail(value, true, message, '==', assert.ok);
|
|
}
|
|
assert.ok = ok;
|
|
|
|
// 5. The equality assertion tests shallow, coercive equality with
|
|
// ==.
|
|
// assert.equal(actual, expected, message_opt);
|
|
|
|
assert.equal = function equal(actual, expected, message) {
|
|
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
|
|
};
|
|
|
|
// 6. The non-equality assertion tests for whether two objects are not equal
|
|
// with != assert.notEqual(actual, expected, message_opt);
|
|
|
|
assert.notEqual = function notEqual(actual, expected, message) {
|
|
if (actual == expected) {
|
|
fail(actual, expected, message, '!=', assert.notEqual);
|
|
}
|
|
};
|
|
|
|
// 7. The equivalence assertion tests a deep equality relation.
|
|
// assert.deepEqual(actual, expected, message_opt);
|
|
|
|
assert.deepEqual = function deepEqual(actual, expected, message) {
|
|
if (!_deepEqual(actual, expected)) {
|
|
fail(actual, expected, message, 'deepEqual', assert.deepEqual);
|
|
}
|
|
};
|
|
|
|
function _deepEqual(actual, expected) {
|
|
// 7.1. All identical values are equivalent, as determined by ===.
|
|
if (actual === expected) {
|
|
return true;
|
|
|
|
} else if (Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
|
|
if (actual.length != expected.length) return false;
|
|
|
|
for (var i = 0; i < actual.length; i++) {
|
|
if (actual[i] !== expected[i]) return false;
|
|
}
|
|
|
|
return true;
|
|
|
|
// 7.2. If the expected value is a Date object, the actual value is
|
|
// equivalent if it is also a Date object that refers to the same time.
|
|
} else if (actual instanceof Date && expected instanceof Date) {
|
|
return actual.getTime() === expected.getTime();
|
|
|
|
// 7.3. Other pairs that do not both pass typeof value == 'object',
|
|
// equivalence is determined by ==.
|
|
} else if (typeof actual != 'object' && typeof expected != 'object') {
|
|
return actual == expected;
|
|
|
|
// 7.4. For all other Object pairs, including Array objects, equivalence is
|
|
// determined by having the same number of owned properties (as verified
|
|
// with Object.prototype.hasOwnProperty.call), the same set of keys
|
|
// (although not necessarily the same order), equivalent values for every
|
|
// corresponding key, and an identical 'prototype' property. Note: this
|
|
// accounts for both named and indexed properties on Arrays.
|
|
} else {
|
|
return objEquiv(actual, expected);
|
|
}
|
|
}
|
|
|
|
function isUndefinedOrNull(value) {
|
|
return value === null || value === undefined;
|
|
}
|
|
|
|
function isArguments(object) {
|
|
return Object.prototype.toString.call(object) == '[object Arguments]';
|
|
}
|
|
|
|
function objEquiv(a, b) {
|
|
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
|
|
return false;
|
|
// an identical 'prototype' property.
|
|
if (a.prototype !== b.prototype) return false;
|
|
//~~~I've managed to break Object.keys through screwy arguments passing.
|
|
// Converting to array solves the problem.
|
|
if (isArguments(a)) {
|
|
if (!isArguments(b)) {
|
|
return false;
|
|
}
|
|
a = pSlice.call(a);
|
|
b = pSlice.call(b);
|
|
return _deepEqual(a, b);
|
|
}
|
|
try {
|
|
var ka = objectKeys(a),
|
|
kb = objectKeys(b),
|
|
key, i;
|
|
} catch (e) {//happens when one is a string literal and the other isn't
|
|
return false;
|
|
}
|
|
// having the same number of owned properties (keys incorporates
|
|
// hasOwnProperty)
|
|
if (ka.length != kb.length)
|
|
return false;
|
|
//the same set of keys (although not necessarily the same order),
|
|
ka.sort();
|
|
kb.sort();
|
|
//~~~cheap key test
|
|
for (i = ka.length - 1; i >= 0; i--) {
|
|
if (ka[i] != kb[i])
|
|
return false;
|
|
}
|
|
//equivalent values for every corresponding key, and
|
|
//~~~possibly expensive deep test
|
|
for (i = ka.length - 1; i >= 0; i--) {
|
|
key = ka[i];
|
|
if (!_deepEqual(a[key], b[key])) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// 8. The non-equivalence assertion tests for any deep inequality.
|
|
// assert.notDeepEqual(actual, expected, message_opt);
|
|
|
|
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
|
if (_deepEqual(actual, expected)) {
|
|
fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
|
|
}
|
|
};
|
|
|
|
// 9. The strict equality assertion tests strict equality, as determined by ===.
|
|
// assert.strictEqual(actual, expected, message_opt);
|
|
|
|
assert.strictEqual = function strictEqual(actual, expected, message) {
|
|
if (actual !== expected) {
|
|
fail(actual, expected, message, '===', assert.strictEqual);
|
|
}
|
|
};
|
|
|
|
// 10. The strict non-equality assertion tests for strict inequality, as
|
|
// determined by !==. assert.notStrictEqual(actual, expected, message_opt);
|
|
|
|
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
|
|
if (actual === expected) {
|
|
fail(actual, expected, message, '!==', assert.notStrictEqual);
|
|
}
|
|
};
|
|
|
|
function expectedException(actual, expected) {
|
|
if (!actual || !expected) {
|
|
return false;
|
|
}
|
|
|
|
if (expected instanceof RegExp) {
|
|
return expected.test(actual);
|
|
} else if (actual instanceof expected) {
|
|
return true;
|
|
} else if (expected.call({}, actual) === true) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function _throws(shouldThrow, block, expected, message) {
|
|
var actual;
|
|
|
|
if (typeof expected === 'string') {
|
|
message = expected;
|
|
expected = null;
|
|
}
|
|
|
|
try {
|
|
block();
|
|
} catch (e) {
|
|
actual = e;
|
|
}
|
|
|
|
message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
|
|
(message ? ' ' + message : '.');
|
|
|
|
if (shouldThrow && !actual) {
|
|
fail('Missing expected exception' + message);
|
|
}
|
|
|
|
if (!shouldThrow && expectedException(actual, expected)) {
|
|
fail('Got unwanted exception' + message);
|
|
}
|
|
|
|
if ((shouldThrow && actual && expected &&
|
|
!expectedException(actual, expected)) || (!shouldThrow && actual)) {
|
|
throw actual;
|
|
}
|
|
}
|
|
|
|
// 11. Expected to throw an error:
|
|
// assert.throws(block, Error_opt, message_opt);
|
|
|
|
assert.throws = function(block, /*optional*/error, /*optional*/message) {
|
|
_throws.apply(this, [true].concat(pSlice.call(arguments)));
|
|
};
|
|
|
|
// EXTENSION! This is annoying to write outside this module.
|
|
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
|
|
_throws.apply(this, [false].concat(pSlice.call(arguments)));
|
|
};
|
|
|
|
assert.ifError = function(err) { if (err) {throw err;}};
|
|
|
|
},{"buffer":9,"util":7}],6:[function(require,module,exports){
|
|
var process=require("__browserify_process");if (!process.EventEmitter) process.EventEmitter = function () {};
|
|
|
|
var EventEmitter = exports.EventEmitter = process.EventEmitter;
|
|
var isArray = typeof Array.isArray === 'function'
|
|
? Array.isArray
|
|
: function (xs) {
|
|
return Object.prototype.toString.call(xs) === '[object Array]'
|
|
}
|
|
;
|
|
function indexOf (xs, x) {
|
|
if (xs.indexOf) return xs.indexOf(x);
|
|
for (var i = 0; i < xs.length; i++) {
|
|
if (x === xs[i]) return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// By default EventEmitters will print a warning if more than
|
|
// 10 listeners are added to it. This is a useful default which
|
|
// helps finding memory leaks.
|
|
//
|
|
// Obviously not all Emitters should be limited to 10. This function allows
|
|
// that to be increased. Set to zero for unlimited.
|
|
var defaultMaxListeners = 10;
|
|
EventEmitter.prototype.setMaxListeners = function(n) {
|
|
if (!this._events) this._events = {};
|
|
this._events.maxListeners = n;
|
|
};
|
|
|
|
|
|
EventEmitter.prototype.emit = function(type) {
|
|
// If there is no 'error' event listener then throw.
|
|
if (type === 'error') {
|
|
if (!this._events || !this._events.error ||
|
|
(isArray(this._events.error) && !this._events.error.length))
|
|
{
|
|
if (arguments[1] instanceof Error) {
|
|
throw arguments[1]; // Unhandled 'error' event
|
|
} else {
|
|
throw new Error("Uncaught, unspecified 'error' event.");
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!this._events) return false;
|
|
var handler = this._events[type];
|
|
if (!handler) return false;
|
|
|
|
if (typeof handler == 'function') {
|
|
switch (arguments.length) {
|
|
// fast cases
|
|
case 1:
|
|
handler.call(this);
|
|
break;
|
|
case 2:
|
|
handler.call(this, arguments[1]);
|
|
break;
|
|
case 3:
|
|
handler.call(this, arguments[1], arguments[2]);
|
|
break;
|
|
// slower
|
|
default:
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
handler.apply(this, args);
|
|
}
|
|
return true;
|
|
|
|
} else if (isArray(handler)) {
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
var listeners = handler.slice();
|
|
for (var i = 0, l = listeners.length; i < l; i++) {
|
|
listeners[i].apply(this, args);
|
|
}
|
|
return true;
|
|
|
|
} else {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
// EventEmitter is defined in src/node_events.cc
|
|
// EventEmitter.prototype.emit() is also defined there.
|
|
EventEmitter.prototype.addListener = function(type, listener) {
|
|
if ('function' !== typeof listener) {
|
|
throw new Error('addListener only takes instances of Function');
|
|
}
|
|
|
|
if (!this._events) this._events = {};
|
|
|
|
// To avoid recursion in the case that type == "newListeners"! Before
|
|
// adding it to the listeners, first emit "newListeners".
|
|
this.emit('newListener', type, listener);
|
|
|
|
if (!this._events[type]) {
|
|
// Optimize the case of one listener. Don't need the extra array object.
|
|
this._events[type] = listener;
|
|
} else if (isArray(this._events[type])) {
|
|
|
|
// Check for listener leak
|
|
if (!this._events[type].warned) {
|
|
var m;
|
|
if (this._events.maxListeners !== undefined) {
|
|
m = this._events.maxListeners;
|
|
} else {
|
|
m = defaultMaxListeners;
|
|
}
|
|
|
|
if (m && m > 0 && this._events[type].length > m) {
|
|
this._events[type].warned = true;
|
|
console.error('(node) warning: possible EventEmitter memory ' +
|
|
'leak detected. %d listeners added. ' +
|
|
'Use emitter.setMaxListeners() to increase limit.',
|
|
this._events[type].length);
|
|
console.trace();
|
|
}
|
|
}
|
|
|
|
// If we've already got an array, just append.
|
|
this._events[type].push(listener);
|
|
} else {
|
|
// Adding the second element, need to change to array.
|
|
this._events[type] = [this._events[type], listener];
|
|
}
|
|
|
|
return this;
|
|
};
|
|
|
|
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
|
|
|
EventEmitter.prototype.once = function(type, listener) {
|
|
var self = this;
|
|
self.on(type, function g() {
|
|
self.removeListener(type, g);
|
|
listener.apply(this, arguments);
|
|
});
|
|
|
|
return this;
|
|
};
|
|
|
|
EventEmitter.prototype.removeListener = function(type, listener) {
|
|
if ('function' !== typeof listener) {
|
|
throw new Error('removeListener only takes instances of Function');
|
|
}
|
|
|
|
// does not use listeners(), so no side effect of creating _events[type]
|
|
if (!this._events || !this._events[type]) return this;
|
|
|
|
var list = this._events[type];
|
|
|
|
if (isArray(list)) {
|
|
var i = indexOf(list, listener);
|
|
if (i < 0) return this;
|
|
list.splice(i, 1);
|
|
if (list.length == 0)
|
|
delete this._events[type];
|
|
} else if (this._events[type] === listener) {
|
|
delete this._events[type];
|
|
}
|
|
|
|
return this;
|
|
};
|
|
|
|
EventEmitter.prototype.removeAllListeners = function(type) {
|
|
if (arguments.length === 0) {
|
|
this._events = {};
|
|
return this;
|
|
}
|
|
|
|
// does not use listeners(), so no side effect of creating _events[type]
|
|
if (type && this._events && this._events[type]) this._events[type] = null;
|
|
return this;
|
|
};
|
|
|
|
EventEmitter.prototype.listeners = function(type) {
|
|
if (!this._events) this._events = {};
|
|
if (!this._events[type]) this._events[type] = [];
|
|
if (!isArray(this._events[type])) {
|
|
this._events[type] = [this._events[type]];
|
|
}
|
|
return this._events[type];
|
|
};
|
|
|
|
EventEmitter.listenerCount = function(emitter, type) {
|
|
var ret;
|
|
if (!emitter._events || !emitter._events[type])
|
|
ret = 0;
|
|
else if (typeof emitter._events[type] === 'function')
|
|
ret = 1;
|
|
else
|
|
ret = emitter._events[type].length;
|
|
return ret;
|
|
};
|
|
|
|
},{"__browserify_process":30}],7:[function(require,module,exports){
|
|
var events = require('events');
|
|
|
|
exports.isArray = isArray;
|
|
exports.isDate = function(obj){return Object.prototype.toString.call(obj) === '[object Date]'};
|
|
exports.isRegExp = function(obj){return Object.prototype.toString.call(obj) === '[object RegExp]'};
|
|
|
|
|
|
exports.print = function () {};
|
|
exports.puts = function () {};
|
|
exports.debug = function() {};
|
|
|
|
exports.inspect = function(obj, showHidden, depth, colors) {
|
|
var seen = [];
|
|
|
|
var stylize = function(str, styleType) {
|
|
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
|
|
var styles =
|
|
{ 'bold' : [1, 22],
|
|
'italic' : [3, 23],
|
|
'underline' : [4, 24],
|
|
'inverse' : [7, 27],
|
|
'white' : [37, 39],
|
|
'grey' : [90, 39],
|
|
'black' : [30, 39],
|
|
'blue' : [34, 39],
|
|
'cyan' : [36, 39],
|
|
'green' : [32, 39],
|
|
'magenta' : [35, 39],
|
|
'red' : [31, 39],
|
|
'yellow' : [33, 39] };
|
|
|
|
var style =
|
|
{ 'special': 'cyan',
|
|
'number': 'blue',
|
|
'boolean': 'yellow',
|
|
'undefined': 'grey',
|
|
'null': 'bold',
|
|
'string': 'green',
|
|
'date': 'magenta',
|
|
// "name": intentionally not styling
|
|
'regexp': 'red' }[styleType];
|
|
|
|
if (style) {
|
|
return '\u001b[' + styles[style][0] + 'm' + str +
|
|
'\u001b[' + styles[style][1] + 'm';
|
|
} else {
|
|
return str;
|
|
}
|
|
};
|
|
if (! colors) {
|
|
stylize = function(str, styleType) { return str; };
|
|
}
|
|
|
|
function format(value, recurseTimes) {
|
|
// Provide a hook for user-specified inspect functions.
|
|
// Check that value is an object with an inspect function on it
|
|
if (value && typeof value.inspect === 'function' &&
|
|
// Filter out the util module, it's inspect function is special
|
|
value !== exports &&
|
|
// Also filter out any prototype objects using the circular check.
|
|
!(value.constructor && value.constructor.prototype === value)) {
|
|
return value.inspect(recurseTimes);
|
|
}
|
|
|
|
// Primitive types cannot have properties
|
|
switch (typeof value) {
|
|
case 'undefined':
|
|
return stylize('undefined', 'undefined');
|
|
|
|
case 'string':
|
|
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
|
|
.replace(/'/g, "\\'")
|
|
.replace(/\\"/g, '"') + '\'';
|
|
return stylize(simple, 'string');
|
|
|
|
case 'number':
|
|
return stylize('' + value, 'number');
|
|
|
|
case 'boolean':
|
|
return stylize('' + value, 'boolean');
|
|
}
|
|
// For some reason typeof null is "object", so special case here.
|
|
if (value === null) {
|
|
return stylize('null', 'null');
|
|
}
|
|
|
|
// Look up the keys of the object.
|
|
var visible_keys = Object_keys(value);
|
|
var keys = showHidden ? Object_getOwnPropertyNames(value) : visible_keys;
|
|
|
|
// Functions without properties can be shortcutted.
|
|
if (typeof value === 'function' && keys.length === 0) {
|
|
if (isRegExp(value)) {
|
|
return stylize('' + value, 'regexp');
|
|
} else {
|
|
var name = value.name ? ': ' + value.name : '';
|
|
return stylize('[Function' + name + ']', 'special');
|
|
}
|
|
}
|
|
|
|
// Dates without properties can be shortcutted
|
|
if (isDate(value) && keys.length === 0) {
|
|
return stylize(value.toUTCString(), 'date');
|
|
}
|
|
|
|
var base, type, braces;
|
|
// Determine the object type
|
|
if (isArray(value)) {
|
|
type = 'Array';
|
|
braces = ['[', ']'];
|
|
} else {
|
|
type = 'Object';
|
|
braces = ['{', '}'];
|
|
}
|
|
|
|
// Make functions say that they are functions
|
|
if (typeof value === 'function') {
|
|
var n = value.name ? ': ' + value.name : '';
|
|
base = (isRegExp(value)) ? ' ' + value : ' [Function' + n + ']';
|
|
} else {
|
|
base = '';
|
|
}
|
|
|
|
// Make dates with properties first say the date
|
|
if (isDate(value)) {
|
|
base = ' ' + value.toUTCString();
|
|
}
|
|
|
|
if (keys.length === 0) {
|
|
return braces[0] + base + braces[1];
|
|
}
|
|
|
|
if (recurseTimes < 0) {
|
|
if (isRegExp(value)) {
|
|
return stylize('' + value, 'regexp');
|
|
} else {
|
|
return stylize('[Object]', 'special');
|
|
}
|
|
}
|
|
|
|
seen.push(value);
|
|
|
|
var output = keys.map(function(key) {
|
|
var name, str;
|
|
if (value.__lookupGetter__) {
|
|
if (value.__lookupGetter__(key)) {
|
|
if (value.__lookupSetter__(key)) {
|
|
str = stylize('[Getter/Setter]', 'special');
|
|
} else {
|
|
str = stylize('[Getter]', 'special');
|
|
}
|
|
} else {
|
|
if (value.__lookupSetter__(key)) {
|
|
str = stylize('[Setter]', 'special');
|
|
}
|
|
}
|
|
}
|
|
if (visible_keys.indexOf(key) < 0) {
|
|
name = '[' + key + ']';
|
|
}
|
|
if (!str) {
|
|
if (seen.indexOf(value[key]) < 0) {
|
|
if (recurseTimes === null) {
|
|
str = format(value[key]);
|
|
} else {
|
|
str = format(value[key], recurseTimes - 1);
|
|
}
|
|
if (str.indexOf('\n') > -1) {
|
|
if (isArray(value)) {
|
|
str = str.split('\n').map(function(line) {
|
|
return ' ' + line;
|
|
}).join('\n').substr(2);
|
|
} else {
|
|
str = '\n' + str.split('\n').map(function(line) {
|
|
return ' ' + line;
|
|
}).join('\n');
|
|
}
|
|
}
|
|
} else {
|
|
str = stylize('[Circular]', 'special');
|
|
}
|
|
}
|
|
if (typeof name === 'undefined') {
|
|
if (type === 'Array' && key.match(/^\d+$/)) {
|
|
return str;
|
|
}
|
|
name = JSON.stringify('' + key);
|
|
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
|
|
name = name.substr(1, name.length - 2);
|
|
name = stylize(name, 'name');
|
|
} else {
|
|
name = name.replace(/'/g, "\\'")
|
|
.replace(/\\"/g, '"')
|
|
.replace(/(^"|"$)/g, "'");
|
|
name = stylize(name, 'string');
|
|
}
|
|
}
|
|
|
|
return name + ': ' + str;
|
|
});
|
|
|
|
seen.pop();
|
|
|
|
var numLinesEst = 0;
|
|
var length = output.reduce(function(prev, cur) {
|
|
numLinesEst++;
|
|
if (cur.indexOf('\n') >= 0) numLinesEst++;
|
|
return prev + cur.length + 1;
|
|
}, 0);
|
|
|
|
if (length > 50) {
|
|
output = braces[0] +
|
|
(base === '' ? '' : base + '\n ') +
|
|
' ' +
|
|
output.join(',\n ') +
|
|
' ' +
|
|
braces[1];
|
|
|
|
} else {
|
|
output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
|
|
}
|
|
|
|
return output;
|
|
}
|
|
return format(obj, (typeof depth === 'undefined' ? 2 : depth));
|
|
};
|
|
|
|
|
|
function isArray(ar) {
|
|
return Array.isArray(ar) ||
|
|
(typeof ar === 'object' && Object.prototype.toString.call(ar) === '[object Array]');
|
|
}
|
|
|
|
|
|
function isRegExp(re) {
|
|
typeof re === 'object' && Object.prototype.toString.call(re) === '[object RegExp]';
|
|
}
|
|
|
|
|
|
function isDate(d) {
|
|
return typeof d === 'object' && Object.prototype.toString.call(d) === '[object Date]';
|
|
}
|
|
|
|
function pad(n) {
|
|
return n < 10 ? '0' + n.toString(10) : n.toString(10);
|
|
}
|
|
|
|
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
|
|
'Oct', 'Nov', 'Dec'];
|
|
|
|
// 26 Feb 16:19:34
|
|
function timestamp() {
|
|
var d = new Date();
|
|
var time = [pad(d.getHours()),
|
|
pad(d.getMinutes()),
|
|
pad(d.getSeconds())].join(':');
|
|
return [d.getDate(), months[d.getMonth()], time].join(' ');
|
|
}
|
|
|
|
exports.log = function (msg) {};
|
|
|
|
exports.pump = null;
|
|
|
|
var Object_keys = Object.keys || function (obj) {
|
|
var res = [];
|
|
for (var key in obj) res.push(key);
|
|
return res;
|
|
};
|
|
|
|
var Object_getOwnPropertyNames = Object.getOwnPropertyNames || function (obj) {
|
|
var res = [];
|
|
for (var key in obj) {
|
|
if (Object.hasOwnProperty.call(obj, key)) res.push(key);
|
|
}
|
|
return res;
|
|
};
|
|
|
|
var Object_create = Object.create || function (prototype, properties) {
|
|
// from es5-shim
|
|
var object;
|
|
if (prototype === null) {
|
|
object = { '__proto__' : null };
|
|
}
|
|
else {
|
|
if (typeof prototype !== 'object') {
|
|
throw new TypeError(
|
|
'typeof prototype[' + (typeof prototype) + '] != \'object\''
|
|
);
|
|
}
|
|
var Type = function () {};
|
|
Type.prototype = prototype;
|
|
object = new Type();
|
|
object.__proto__ = prototype;
|
|
}
|
|
if (typeof properties !== 'undefined' && Object.defineProperties) {
|
|
Object.defineProperties(object, properties);
|
|
}
|
|
return object;
|
|
};
|
|
|
|
exports.inherits = function(ctor, superCtor) {
|
|
ctor.super_ = superCtor;
|
|
ctor.prototype = Object_create(superCtor.prototype, {
|
|
constructor: {
|
|
value: ctor,
|
|
enumerable: false,
|
|
writable: true,
|
|
configurable: true
|
|
}
|
|
});
|
|
};
|
|
|
|
var formatRegExp = /%[sdj%]/g;
|
|
exports.format = function(f) {
|
|
if (typeof f !== 'string') {
|
|
var objects = [];
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
objects.push(exports.inspect(arguments[i]));
|
|
}
|
|
return objects.join(' ');
|
|
}
|
|
|
|
var i = 1;
|
|
var args = arguments;
|
|
var len = args.length;
|
|
var str = String(f).replace(formatRegExp, function(x) {
|
|
if (x === '%%') return '%';
|
|
if (i >= len) return x;
|
|
switch (x) {
|
|
case '%s': return String(args[i++]);
|
|
case '%d': return Number(args[i++]);
|
|
case '%j': return JSON.stringify(args[i++]);
|
|
default:
|
|
return x;
|
|
}
|
|
});
|
|
for(var x = args[i]; i < len; x = args[++i]){
|
|
if (x === null || typeof x !== 'object') {
|
|
str += ' ' + x;
|
|
} else {
|
|
str += ' ' + exports.inspect(x);
|
|
}
|
|
}
|
|
return str;
|
|
};
|
|
|
|
},{"events":6}],8:[function(require,module,exports){
|
|
exports.readIEEE754 = function(buffer, offset, isBE, mLen, nBytes) {
|
|
var e, m,
|
|
eLen = nBytes * 8 - mLen - 1,
|
|
eMax = (1 << eLen) - 1,
|
|
eBias = eMax >> 1,
|
|
nBits = -7,
|
|
i = isBE ? 0 : (nBytes - 1),
|
|
d = isBE ? 1 : -1,
|
|
s = buffer[offset + i];
|
|
|
|
i += d;
|
|
|
|
e = s & ((1 << (-nBits)) - 1);
|
|
s >>= (-nBits);
|
|
nBits += eLen;
|
|
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
|
|
|
|
m = e & ((1 << (-nBits)) - 1);
|
|
e >>= (-nBits);
|
|
nBits += mLen;
|
|
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
|
|
|
|
if (e === 0) {
|
|
e = 1 - eBias;
|
|
} else if (e === eMax) {
|
|
return m ? NaN : ((s ? -1 : 1) * Infinity);
|
|
} else {
|
|
m = m + Math.pow(2, mLen);
|
|
e = e - eBias;
|
|
}
|
|
return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
|
|
};
|
|
|
|
exports.writeIEEE754 = function(buffer, value, offset, isBE, mLen, nBytes) {
|
|
var e, m, c,
|
|
eLen = nBytes * 8 - mLen - 1,
|
|
eMax = (1 << eLen) - 1,
|
|
eBias = eMax >> 1,
|
|
rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0),
|
|
i = isBE ? (nBytes - 1) : 0,
|
|
d = isBE ? -1 : 1,
|
|
s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
|
|
|
|
value = Math.abs(value);
|
|
|
|
if (isNaN(value) || value === Infinity) {
|
|
m = isNaN(value) ? 1 : 0;
|
|
e = eMax;
|
|
} else {
|
|
e = Math.floor(Math.log(value) / Math.LN2);
|
|
if (value * (c = Math.pow(2, -e)) < 1) {
|
|
e--;
|
|
c *= 2;
|
|
}
|
|
if (e + eBias >= 1) {
|
|
value += rt / c;
|
|
} else {
|
|
value += rt * Math.pow(2, 1 - eBias);
|
|
}
|
|
if (value * c >= 2) {
|
|
e++;
|
|
c /= 2;
|
|
}
|
|
|
|
if (e + eBias >= eMax) {
|
|
m = 0;
|
|
e = eMax;
|
|
} else if (e + eBias >= 1) {
|
|
m = (value * c - 1) * Math.pow(2, mLen);
|
|
e = e + eBias;
|
|
} else {
|
|
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
|
|
e = 0;
|
|
}
|
|
}
|
|
|
|
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8);
|
|
|
|
e = (e << mLen) | m;
|
|
eLen += mLen;
|
|
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8);
|
|
|
|
buffer[offset + i - d] |= s * 128;
|
|
};
|
|
|
|
},{}],9:[function(require,module,exports){
|
|
var assert = require('assert');
|
|
exports.Buffer = Buffer;
|
|
exports.SlowBuffer = Buffer;
|
|
Buffer.poolSize = 8192;
|
|
exports.INSPECT_MAX_BYTES = 50;
|
|
|
|
function Buffer(subject, encoding, offset) {
|
|
if (!(this instanceof Buffer)) {
|
|
return new Buffer(subject, encoding, offset);
|
|
}
|
|
this.parent = this;
|
|
this.offset = 0;
|
|
|
|
var type;
|
|
|
|
// Are we slicing?
|
|
if (typeof offset === 'number') {
|
|
this.length = coerce(encoding);
|
|
this.offset = offset;
|
|
} else {
|
|
// Find the length
|
|
switch (type = typeof subject) {
|
|
case 'number':
|
|
this.length = coerce(subject);
|
|
break;
|
|
|
|
case 'string':
|
|
this.length = Buffer.byteLength(subject, encoding);
|
|
break;
|
|
|
|
case 'object': // Assume object is an array
|
|
this.length = coerce(subject.length);
|
|
break;
|
|
|
|
default:
|
|
throw new Error('First argument needs to be a number, ' +
|
|
'array or string.');
|
|
}
|
|
|
|
// Treat array-ish objects as a byte array.
|
|
if (isArrayIsh(subject)) {
|
|
for (var i = 0; i < this.length; i++) {
|
|
if (subject instanceof Buffer) {
|
|
this[i] = subject.readUInt8(i);
|
|
}
|
|
else {
|
|
this[i] = subject[i];
|
|
}
|
|
}
|
|
} else if (type == 'string') {
|
|
// We are a string
|
|
this.length = this.write(subject, 0, encoding);
|
|
} else if (type === 'number') {
|
|
for (var i = 0; i < this.length; i++) {
|
|
this[i] = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Buffer.prototype.get = function get(i) {
|
|
if (i < 0 || i >= this.length) throw new Error('oob');
|
|
return this[i];
|
|
};
|
|
|
|
Buffer.prototype.set = function set(i, v) {
|
|
if (i < 0 || i >= this.length) throw new Error('oob');
|
|
return this[i] = v;
|
|
};
|
|
|
|
Buffer.byteLength = function (str, encoding) {
|
|
switch (encoding || "utf8") {
|
|
case 'hex':
|
|
return str.length / 2;
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
return utf8ToBytes(str).length;
|
|
|
|
case 'ascii':
|
|
case 'binary':
|
|
return str.length;
|
|
|
|
case 'base64':
|
|
return base64ToBytes(str).length;
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
};
|
|
|
|
Buffer.prototype.utf8Write = function (string, offset, length) {
|
|
var bytes, pos;
|
|
return Buffer._charsWritten = blitBuffer(utf8ToBytes(string), this, offset, length);
|
|
};
|
|
|
|
Buffer.prototype.asciiWrite = function (string, offset, length) {
|
|
var bytes, pos;
|
|
return Buffer._charsWritten = blitBuffer(asciiToBytes(string), this, offset, length);
|
|
};
|
|
|
|
Buffer.prototype.binaryWrite = Buffer.prototype.asciiWrite;
|
|
|
|
Buffer.prototype.base64Write = function (string, offset, length) {
|
|
var bytes, pos;
|
|
return Buffer._charsWritten = blitBuffer(base64ToBytes(string), this, offset, length);
|
|
};
|
|
|
|
Buffer.prototype.base64Slice = function (start, end) {
|
|
var bytes = Array.prototype.slice.apply(this, arguments)
|
|
return require("base64-js").fromByteArray(bytes);
|
|
};
|
|
|
|
Buffer.prototype.utf8Slice = function () {
|
|
var bytes = Array.prototype.slice.apply(this, arguments);
|
|
var res = "";
|
|
var tmp = "";
|
|
var i = 0;
|
|
while (i < bytes.length) {
|
|
if (bytes[i] <= 0x7F) {
|
|
res += decodeUtf8Char(tmp) + String.fromCharCode(bytes[i]);
|
|
tmp = "";
|
|
} else
|
|
tmp += "%" + bytes[i].toString(16);
|
|
|
|
i++;
|
|
}
|
|
|
|
return res + decodeUtf8Char(tmp);
|
|
}
|
|
|
|
Buffer.prototype.asciiSlice = function () {
|
|
var bytes = Array.prototype.slice.apply(this, arguments);
|
|
var ret = "";
|
|
for (var i = 0; i < bytes.length; i++)
|
|
ret += String.fromCharCode(bytes[i]);
|
|
return ret;
|
|
}
|
|
|
|
Buffer.prototype.binarySlice = Buffer.prototype.asciiSlice;
|
|
|
|
Buffer.prototype.inspect = function() {
|
|
var out = [],
|
|
len = this.length;
|
|
for (var i = 0; i < len; i++) {
|
|
out[i] = toHex(this[i]);
|
|
if (i == exports.INSPECT_MAX_BYTES) {
|
|
out[i + 1] = '...';
|
|
break;
|
|
}
|
|
}
|
|
return '<Buffer ' + out.join(' ') + '>';
|
|
};
|
|
|
|
|
|
Buffer.prototype.hexSlice = function(start, end) {
|
|
var len = this.length;
|
|
|
|
if (!start || start < 0) start = 0;
|
|
if (!end || end < 0 || end > len) end = len;
|
|
|
|
var out = '';
|
|
for (var i = start; i < end; i++) {
|
|
out += toHex(this[i]);
|
|
}
|
|
return out;
|
|
};
|
|
|
|
|
|
Buffer.prototype.toString = function(encoding, start, end) {
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
|
start = +start || 0;
|
|
if (typeof end == 'undefined') end = this.length;
|
|
|
|
// Fastpath empty strings
|
|
if (+end == start) {
|
|
return '';
|
|
}
|
|
|
|
switch (encoding) {
|
|
case 'hex':
|
|
return this.hexSlice(start, end);
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
return this.utf8Slice(start, end);
|
|
|
|
case 'ascii':
|
|
return this.asciiSlice(start, end);
|
|
|
|
case 'binary':
|
|
return this.binarySlice(start, end);
|
|
|
|
case 'base64':
|
|
return this.base64Slice(start, end);
|
|
|
|
case 'ucs2':
|
|
case 'ucs-2':
|
|
return this.ucs2Slice(start, end);
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
};
|
|
|
|
|
|
Buffer.prototype.hexWrite = function(string, offset, length) {
|
|
offset = +offset || 0;
|
|
var remaining = this.length - offset;
|
|
if (!length) {
|
|
length = remaining;
|
|
} else {
|
|
length = +length;
|
|
if (length > remaining) {
|
|
length = remaining;
|
|
}
|
|
}
|
|
|
|
// must be an even number of digits
|
|
var strLen = string.length;
|
|
if (strLen % 2) {
|
|
throw new Error('Invalid hex string');
|
|
}
|
|
if (length > strLen / 2) {
|
|
length = strLen / 2;
|
|
}
|
|
for (var i = 0; i < length; i++) {
|
|
var byte = parseInt(string.substr(i * 2, 2), 16);
|
|
if (isNaN(byte)) throw new Error('Invalid hex string');
|
|
this[offset + i] = byte;
|
|
}
|
|
Buffer._charsWritten = i * 2;
|
|
return i;
|
|
};
|
|
|
|
|
|
Buffer.prototype.write = function(string, offset, length, encoding) {
|
|
// Support both (string, offset, length, encoding)
|
|
// and the legacy (string, encoding, offset, length)
|
|
if (isFinite(offset)) {
|
|
if (!isFinite(length)) {
|
|
encoding = length;
|
|
length = undefined;
|
|
}
|
|
} else { // legacy
|
|
var swap = encoding;
|
|
encoding = offset;
|
|
offset = length;
|
|
length = swap;
|
|
}
|
|
|
|
offset = +offset || 0;
|
|
var remaining = this.length - offset;
|
|
if (!length) {
|
|
length = remaining;
|
|
} else {
|
|
length = +length;
|
|
if (length > remaining) {
|
|
length = remaining;
|
|
}
|
|
}
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
|
|
|
switch (encoding) {
|
|
case 'hex':
|
|
return this.hexWrite(string, offset, length);
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
return this.utf8Write(string, offset, length);
|
|
|
|
case 'ascii':
|
|
return this.asciiWrite(string, offset, length);
|
|
|
|
case 'binary':
|
|
return this.binaryWrite(string, offset, length);
|
|
|
|
case 'base64':
|
|
return this.base64Write(string, offset, length);
|
|
|
|
case 'ucs2':
|
|
case 'ucs-2':
|
|
return this.ucs2Write(string, offset, length);
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
};
|
|
|
|
|
|
// slice(start, end)
|
|
Buffer.prototype.slice = function(start, end) {
|
|
if (end === undefined) end = this.length;
|
|
|
|
if (end > this.length) {
|
|
throw new Error('oob');
|
|
}
|
|
if (start > end) {
|
|
throw new Error('oob');
|
|
}
|
|
|
|
return new Buffer(this, end - start, +start);
|
|
};
|
|
|
|
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
|
|
Buffer.prototype.copy = function(target, target_start, start, end) {
|
|
var source = this;
|
|
start || (start = 0);
|
|
if (end === undefined || isNaN(end)) {
|
|
end = this.length;
|
|
}
|
|
target_start || (target_start = 0);
|
|
|
|
if (end < start) throw new Error('sourceEnd < sourceStart');
|
|
|
|
// Copy 0 bytes; we're done
|
|
if (end === start) return 0;
|
|
if (target.length == 0 || source.length == 0) return 0;
|
|
|
|
if (target_start < 0 || target_start >= target.length) {
|
|
throw new Error('targetStart out of bounds');
|
|
}
|
|
|
|
if (start < 0 || start >= source.length) {
|
|
throw new Error('sourceStart out of bounds');
|
|
}
|
|
|
|
if (end < 0 || end > source.length) {
|
|
throw new Error('sourceEnd out of bounds');
|
|
}
|
|
|
|
// Are we oob?
|
|
if (end > this.length) {
|
|
end = this.length;
|
|
}
|
|
|
|
if (target.length - target_start < end - start) {
|
|
end = target.length - target_start + start;
|
|
}
|
|
|
|
var temp = [];
|
|
for (var i=start; i<end; i++) {
|
|
assert.ok(typeof this[i] !== 'undefined', "copying undefined buffer bytes!");
|
|
temp.push(this[i]);
|
|
}
|
|
|
|
for (var i=target_start; i<target_start+temp.length; i++) {
|
|
target[i] = temp[i-target_start];
|
|
}
|
|
};
|
|
|
|
// fill(value, start=0, end=buffer.length)
|
|
Buffer.prototype.fill = function fill(value, start, end) {
|
|
value || (value = 0);
|
|
start || (start = 0);
|
|
end || (end = this.length);
|
|
|
|
if (typeof value === 'string') {
|
|
value = value.charCodeAt(0);
|
|
}
|
|
if (!(typeof value === 'number') || isNaN(value)) {
|
|
throw new Error('value is not a number');
|
|
}
|
|
|
|
if (end < start) throw new Error('end < start');
|
|
|
|
// Fill 0 bytes; we're done
|
|
if (end === start) return 0;
|
|
if (this.length == 0) return 0;
|
|
|
|
if (start < 0 || start >= this.length) {
|
|
throw new Error('start out of bounds');
|
|
}
|
|
|
|
if (end < 0 || end > this.length) {
|
|
throw new Error('end out of bounds');
|
|
}
|
|
|
|
for (var i = start; i < end; i++) {
|
|
this[i] = value;
|
|
}
|
|
}
|
|
|
|
// Static methods
|
|
Buffer.isBuffer = function isBuffer(b) {
|
|
return b instanceof Buffer || b instanceof Buffer;
|
|
};
|
|
|
|
Buffer.concat = function (list, totalLength) {
|
|
if (!isArray(list)) {
|
|
throw new Error("Usage: Buffer.concat(list, [totalLength])\n \
|
|
list should be an Array.");
|
|
}
|
|
|
|
if (list.length === 0) {
|
|
return new Buffer(0);
|
|
} else if (list.length === 1) {
|
|
return list[0];
|
|
}
|
|
|
|
if (typeof totalLength !== 'number') {
|
|
totalLength = 0;
|
|
for (var i = 0; i < list.length; i++) {
|
|
var buf = list[i];
|
|
totalLength += buf.length;
|
|
}
|
|
}
|
|
|
|
var buffer = new Buffer(totalLength);
|
|
var pos = 0;
|
|
for (var i = 0; i < list.length; i++) {
|
|
var buf = list[i];
|
|
buf.copy(buffer, pos);
|
|
pos += buf.length;
|
|
}
|
|
return buffer;
|
|
};
|
|
|
|
// helpers
|
|
|
|
function coerce(length) {
|
|
// Coerce length to a number (possibly NaN), round up
|
|
// in case it's fractional (e.g. 123.456) then do a
|
|
// double negate to coerce a NaN to 0. Easy, right?
|
|
length = ~~Math.ceil(+length);
|
|
return length < 0 ? 0 : length;
|
|
}
|
|
|
|
function isArray(subject) {
|
|
return (Array.isArray ||
|
|
function(subject){
|
|
return {}.toString.apply(subject) == '[object Array]'
|
|
})
|
|
(subject)
|
|
}
|
|
|
|
function isArrayIsh(subject) {
|
|
return isArray(subject) || Buffer.isBuffer(subject) ||
|
|
subject && typeof subject === 'object' &&
|
|
typeof subject.length === 'number';
|
|
}
|
|
|
|
function toHex(n) {
|
|
if (n < 16) return '0' + n.toString(16);
|
|
return n.toString(16);
|
|
}
|
|
|
|
function utf8ToBytes(str) {
|
|
var byteArray = [];
|
|
for (var i = 0; i < str.length; i++)
|
|
if (str.charCodeAt(i) <= 0x7F)
|
|
byteArray.push(str.charCodeAt(i));
|
|
else {
|
|
var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
|
|
for (var j = 0; j < h.length; j++)
|
|
byteArray.push(parseInt(h[j], 16));
|
|
}
|
|
|
|
return byteArray;
|
|
}
|
|
|
|
function asciiToBytes(str) {
|
|
var byteArray = []
|
|
for (var i = 0; i < str.length; i++ )
|
|
// Node's code seems to be doing this and not & 0x7F..
|
|
byteArray.push( str.charCodeAt(i) & 0xFF );
|
|
|
|
return byteArray;
|
|
}
|
|
|
|
function base64ToBytes(str) {
|
|
return require("base64-js").toByteArray(str);
|
|
}
|
|
|
|
function blitBuffer(src, dst, offset, length) {
|
|
var pos, i = 0;
|
|
while (i < length) {
|
|
if ((i+offset >= dst.length) || (i >= src.length))
|
|
break;
|
|
|
|
dst[i + offset] = src[i];
|
|
i++;
|
|
}
|
|
return i;
|
|
}
|
|
|
|
function decodeUtf8Char(str) {
|
|
try {
|
|
return decodeURIComponent(str);
|
|
} catch (err) {
|
|
return String.fromCharCode(0xFFFD); // UTF 8 invalid char
|
|
}
|
|
}
|
|
|
|
// read/write bit-twiddling
|
|
|
|
Buffer.prototype.readUInt8 = function(offset, noAssert) {
|
|
var buffer = this;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
if (offset >= buffer.length) return;
|
|
|
|
return buffer[offset];
|
|
};
|
|
|
|
function readUInt16(buffer, offset, isBigEndian, noAssert) {
|
|
var val = 0;
|
|
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
if (offset >= buffer.length) return 0;
|
|
|
|
if (isBigEndian) {
|
|
val = buffer[offset] << 8;
|
|
if (offset + 1 < buffer.length) {
|
|
val |= buffer[offset + 1];
|
|
}
|
|
} else {
|
|
val = buffer[offset];
|
|
if (offset + 1 < buffer.length) {
|
|
val |= buffer[offset + 1] << 8;
|
|
}
|
|
}
|
|
|
|
return val;
|
|
}
|
|
|
|
Buffer.prototype.readUInt16LE = function(offset, noAssert) {
|
|
return readUInt16(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readUInt16BE = function(offset, noAssert) {
|
|
return readUInt16(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readUInt32(buffer, offset, isBigEndian, noAssert) {
|
|
var val = 0;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
if (offset >= buffer.length) return 0;
|
|
|
|
if (isBigEndian) {
|
|
if (offset + 1 < buffer.length)
|
|
val = buffer[offset + 1] << 16;
|
|
if (offset + 2 < buffer.length)
|
|
val |= buffer[offset + 2] << 8;
|
|
if (offset + 3 < buffer.length)
|
|
val |= buffer[offset + 3];
|
|
val = val + (buffer[offset] << 24 >>> 0);
|
|
} else {
|
|
if (offset + 2 < buffer.length)
|
|
val = buffer[offset + 2] << 16;
|
|
if (offset + 1 < buffer.length)
|
|
val |= buffer[offset + 1] << 8;
|
|
val |= buffer[offset];
|
|
if (offset + 3 < buffer.length)
|
|
val = val + (buffer[offset + 3] << 24 >>> 0);
|
|
}
|
|
|
|
return val;
|
|
}
|
|
|
|
Buffer.prototype.readUInt32LE = function(offset, noAssert) {
|
|
return readUInt32(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readUInt32BE = function(offset, noAssert) {
|
|
return readUInt32(this, offset, true, noAssert);
|
|
};
|
|
|
|
|
|
/*
|
|
* Signed integer types, yay team! A reminder on how two's complement actually
|
|
* works. The first bit is the signed bit, i.e. tells us whether or not the
|
|
* number should be positive or negative. If the two's complement value is
|
|
* positive, then we're done, as it's equivalent to the unsigned representation.
|
|
*
|
|
* Now if the number is positive, you're pretty much done, you can just leverage
|
|
* the unsigned translations and return those. Unfortunately, negative numbers
|
|
* aren't quite that straightforward.
|
|
*
|
|
* At first glance, one might be inclined to use the traditional formula to
|
|
* translate binary numbers between the positive and negative values in two's
|
|
* complement. (Though it doesn't quite work for the most negative value)
|
|
* Mainly:
|
|
* - invert all the bits
|
|
* - add one to the result
|
|
*
|
|
* Of course, this doesn't quite work in Javascript. Take for example the value
|
|
* of -128. This could be represented in 16 bits (big-endian) as 0xff80. But of
|
|
* course, Javascript will do the following:
|
|
*
|
|
* > ~0xff80
|
|
* -65409
|
|
*
|
|
* Whoh there, Javascript, that's not quite right. But wait, according to
|
|
* Javascript that's perfectly correct. When Javascript ends up seeing the
|
|
* constant 0xff80, it has no notion that it is actually a signed number. It
|
|
* assumes that we've input the unsigned value 0xff80. Thus, when it does the
|
|
* binary negation, it casts it into a signed value, (positive 0xff80). Then
|
|
* when you perform binary negation on that, it turns it into a negative number.
|
|
*
|
|
* Instead, we're going to have to use the following general formula, that works
|
|
* in a rather Javascript friendly way. I'm glad we don't support this kind of
|
|
* weird numbering scheme in the kernel.
|
|
*
|
|
* (BIT-MAX - (unsigned)val + 1) * -1
|
|
*
|
|
* The astute observer, may think that this doesn't make sense for 8-bit numbers
|
|
* (really it isn't necessary for them). However, when you get 16-bit numbers,
|
|
* you do. Let's go back to our prior example and see how this will look:
|
|
*
|
|
* (0xffff - 0xff80 + 1) * -1
|
|
* (0x007f + 1) * -1
|
|
* (0x0080) * -1
|
|
*/
|
|
Buffer.prototype.readInt8 = function(offset, noAssert) {
|
|
var buffer = this;
|
|
var neg;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
if (offset >= buffer.length) return;
|
|
|
|
neg = buffer[offset] & 0x80;
|
|
if (!neg) {
|
|
return (buffer[offset]);
|
|
}
|
|
|
|
return ((0xff - buffer[offset] + 1) * -1);
|
|
};
|
|
|
|
function readInt16(buffer, offset, isBigEndian, noAssert) {
|
|
var neg, val;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
val = readUInt16(buffer, offset, isBigEndian, noAssert);
|
|
neg = val & 0x8000;
|
|
if (!neg) {
|
|
return val;
|
|
}
|
|
|
|
return (0xffff - val + 1) * -1;
|
|
}
|
|
|
|
Buffer.prototype.readInt16LE = function(offset, noAssert) {
|
|
return readInt16(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readInt16BE = function(offset, noAssert) {
|
|
return readInt16(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readInt32(buffer, offset, isBigEndian, noAssert) {
|
|
var neg, val;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
val = readUInt32(buffer, offset, isBigEndian, noAssert);
|
|
neg = val & 0x80000000;
|
|
if (!neg) {
|
|
return (val);
|
|
}
|
|
|
|
return (0xffffffff - val + 1) * -1;
|
|
}
|
|
|
|
Buffer.prototype.readInt32LE = function(offset, noAssert) {
|
|
return readInt32(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readInt32BE = function(offset, noAssert) {
|
|
return readInt32(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readFloat(buffer, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
return require('./buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
|
|
23, 4);
|
|
}
|
|
|
|
Buffer.prototype.readFloatLE = function(offset, noAssert) {
|
|
return readFloat(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readFloatBE = function(offset, noAssert) {
|
|
return readFloat(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readDouble(buffer, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset + 7 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
return require('./buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
|
|
52, 8);
|
|
}
|
|
|
|
Buffer.prototype.readDoubleLE = function(offset, noAssert) {
|
|
return readDouble(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readDoubleBE = function(offset, noAssert) {
|
|
return readDouble(this, offset, true, noAssert);
|
|
};
|
|
|
|
|
|
/*
|
|
* We have to make sure that the value is a valid integer. This means that it is
|
|
* non-negative. It has no fractional component and that it does not exceed the
|
|
* maximum allowed value.
|
|
*
|
|
* value The number to check for validity
|
|
*
|
|
* max The maximum value
|
|
*/
|
|
function verifuint(value, max) {
|
|
assert.ok(typeof (value) == 'number',
|
|
'cannot write a non-number as a number');
|
|
|
|
assert.ok(value >= 0,
|
|
'specified a negative value for writing an unsigned value');
|
|
|
|
assert.ok(value <= max, 'value is larger than maximum value for type');
|
|
|
|
assert.ok(Math.floor(value) === value, 'value has a fractional component');
|
|
}
|
|
|
|
Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
|
|
var buffer = this;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'trying to write beyond buffer length');
|
|
|
|
verifuint(value, 0xff);
|
|
}
|
|
|
|
if (offset < buffer.length) {
|
|
buffer[offset] = value;
|
|
}
|
|
};
|
|
|
|
function writeUInt16(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'trying to write beyond buffer length');
|
|
|
|
verifuint(value, 0xffff);
|
|
}
|
|
|
|
for (var i = 0; i < Math.min(buffer.length - offset, 2); i++) {
|
|
buffer[offset + i] =
|
|
(value & (0xff << (8 * (isBigEndian ? 1 - i : i)))) >>>
|
|
(isBigEndian ? 1 - i : i) * 8;
|
|
}
|
|
|
|
}
|
|
|
|
Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
|
|
writeUInt16(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
|
|
writeUInt16(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeUInt32(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'trying to write beyond buffer length');
|
|
|
|
verifuint(value, 0xffffffff);
|
|
}
|
|
|
|
for (var i = 0; i < Math.min(buffer.length - offset, 4); i++) {
|
|
buffer[offset + i] =
|
|
(value >>> (isBigEndian ? 3 - i : i) * 8) & 0xff;
|
|
}
|
|
}
|
|
|
|
Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
|
|
writeUInt32(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
|
|
writeUInt32(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
|
|
/*
|
|
* We now move onto our friends in the signed number category. Unlike unsigned
|
|
* numbers, we're going to have to worry a bit more about how we put values into
|
|
* arrays. Since we are only worrying about signed 32-bit values, we're in
|
|
* slightly better shape. Unfortunately, we really can't do our favorite binary
|
|
* & in this system. It really seems to do the wrong thing. For example:
|
|
*
|
|
* > -32 & 0xff
|
|
* 224
|
|
*
|
|
* What's happening above is really: 0xe0 & 0xff = 0xe0. However, the results of
|
|
* this aren't treated as a signed number. Ultimately a bad thing.
|
|
*
|
|
* What we're going to want to do is basically create the unsigned equivalent of
|
|
* our representation and pass that off to the wuint* functions. To do that
|
|
* we're going to do the following:
|
|
*
|
|
* - if the value is positive
|
|
* we can pass it directly off to the equivalent wuint
|
|
* - if the value is negative
|
|
* we do the following computation:
|
|
* mb + val + 1, where
|
|
* mb is the maximum unsigned value in that byte size
|
|
* val is the Javascript negative integer
|
|
*
|
|
*
|
|
* As a concrete value, take -128. In signed 16 bits this would be 0xff80. If
|
|
* you do out the computations:
|
|
*
|
|
* 0xffff - 128 + 1
|
|
* 0xffff - 127
|
|
* 0xff80
|
|
*
|
|
* You can then encode this value as the signed version. This is really rather
|
|
* hacky, but it should work and get the job done which is our goal here.
|
|
*/
|
|
|
|
/*
|
|
* A series of checks to make sure we actually have a signed 32-bit number
|
|
*/
|
|
function verifsint(value, max, min) {
|
|
assert.ok(typeof (value) == 'number',
|
|
'cannot write a non-number as a number');
|
|
|
|
assert.ok(value <= max, 'value larger than maximum allowed value');
|
|
|
|
assert.ok(value >= min, 'value smaller than minimum allowed value');
|
|
|
|
assert.ok(Math.floor(value) === value, 'value has a fractional component');
|
|
}
|
|
|
|
function verifIEEE754(value, max, min) {
|
|
assert.ok(typeof (value) == 'number',
|
|
'cannot write a non-number as a number');
|
|
|
|
assert.ok(value <= max, 'value larger than maximum allowed value');
|
|
|
|
assert.ok(value >= min, 'value smaller than minimum allowed value');
|
|
}
|
|
|
|
Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
|
|
var buffer = this;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifsint(value, 0x7f, -0x80);
|
|
}
|
|
|
|
if (value >= 0) {
|
|
buffer.writeUInt8(value, offset, noAssert);
|
|
} else {
|
|
buffer.writeUInt8(0xff + value + 1, offset, noAssert);
|
|
}
|
|
};
|
|
|
|
function writeInt16(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifsint(value, 0x7fff, -0x8000);
|
|
}
|
|
|
|
if (value >= 0) {
|
|
writeUInt16(buffer, value, offset, isBigEndian, noAssert);
|
|
} else {
|
|
writeUInt16(buffer, 0xffff + value + 1, offset, isBigEndian, noAssert);
|
|
}
|
|
}
|
|
|
|
Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
|
|
writeInt16(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
|
|
writeInt16(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeInt32(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifsint(value, 0x7fffffff, -0x80000000);
|
|
}
|
|
|
|
if (value >= 0) {
|
|
writeUInt32(buffer, value, offset, isBigEndian, noAssert);
|
|
} else {
|
|
writeUInt32(buffer, 0xffffffff + value + 1, offset, isBigEndian, noAssert);
|
|
}
|
|
}
|
|
|
|
Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
|
|
writeInt32(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
|
|
writeInt32(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeFloat(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38);
|
|
}
|
|
|
|
require('./buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
|
|
23, 4);
|
|
}
|
|
|
|
Buffer.prototype.writeFloatLE = function(value, offset, noAssert) {
|
|
writeFloat(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeFloatBE = function(value, offset, noAssert) {
|
|
writeFloat(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeDouble(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 7 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308);
|
|
}
|
|
|
|
require('./buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
|
|
52, 8);
|
|
}
|
|
|
|
Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) {
|
|
writeDouble(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) {
|
|
writeDouble(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
},{"./buffer_ieee754":8,"assert":5,"base64-js":4}],10:[function(require,module,exports){
|
|
var Buffer = require('buffer').Buffer;
|
|
var intSize = 4;
|
|
var zeroBuffer = new Buffer(intSize); zeroBuffer.fill(0);
|
|
var chrsz = 8;
|
|
|
|
function toArray(buf, bigEndian) {
|
|
if ((buf.length % intSize) !== 0) {
|
|
var len = buf.length + (intSize - (buf.length % intSize));
|
|
buf = Buffer.concat([buf, zeroBuffer], len);
|
|
}
|
|
|
|
var arr = [];
|
|
var fn = bigEndian ? buf.readInt32BE : buf.readInt32LE;
|
|
for (var i = 0; i < buf.length; i += intSize) {
|
|
arr.push(fn.call(buf, i));
|
|
}
|
|
return arr;
|
|
}
|
|
|
|
function toBuffer(arr, size, bigEndian) {
|
|
var buf = new Buffer(size);
|
|
var fn = bigEndian ? buf.writeInt32BE : buf.writeInt32LE;
|
|
for (var i = 0; i < arr.length; i++) {
|
|
fn.call(buf, arr[i], i * 4, true);
|
|
}
|
|
return buf;
|
|
}
|
|
|
|
function hash(buf, fn, hashSize, bigEndian) {
|
|
if (!Buffer.isBuffer(buf)) buf = new Buffer(buf);
|
|
var arr = fn(toArray(buf, bigEndian), buf.length * chrsz);
|
|
return toBuffer(arr, hashSize, bigEndian);
|
|
}
|
|
|
|
module.exports = { hash: hash };
|
|
|
|
},{"buffer":9}],11:[function(require,module,exports){
|
|
var Buffer = require('buffer').Buffer
|
|
var sha = require('./sha')
|
|
var sha256 = require('./sha256')
|
|
var rng = require('./rng')
|
|
var md5 = require('./md5')
|
|
|
|
var algorithms = {
|
|
sha1: sha,
|
|
sha256: sha256,
|
|
md5: md5
|
|
}
|
|
|
|
var blocksize = 64
|
|
var zeroBuffer = new Buffer(blocksize); zeroBuffer.fill(0)
|
|
function hmac(fn, key, data) {
|
|
if(!Buffer.isBuffer(key)) key = new Buffer(key)
|
|
if(!Buffer.isBuffer(data)) data = new Buffer(data)
|
|
|
|
if(key.length > blocksize) {
|
|
key = fn(key)
|
|
} else if(key.length < blocksize) {
|
|
key = Buffer.concat([key, zeroBuffer], blocksize)
|
|
}
|
|
|
|
var ipad = new Buffer(blocksize), opad = new Buffer(blocksize)
|
|
for(var i = 0; i < blocksize; i++) {
|
|
ipad[i] = key[i] ^ 0x36
|
|
opad[i] = key[i] ^ 0x5C
|
|
}
|
|
|
|
var hash = fn(Buffer.concat([ipad, data]))
|
|
return fn(Buffer.concat([opad, hash]))
|
|
}
|
|
|
|
function hash(alg, key) {
|
|
alg = alg || 'sha1'
|
|
var fn = algorithms[alg]
|
|
var bufs = []
|
|
var length = 0
|
|
if(!fn) error('algorithm:', alg, 'is not yet supported')
|
|
return {
|
|
update: function (data) {
|
|
if(!Buffer.isBuffer(data)) data = new Buffer(data)
|
|
|
|
bufs.push(data)
|
|
length += data.length
|
|
return this
|
|
},
|
|
digest: function (enc) {
|
|
var buf = Buffer.concat(bufs)
|
|
var r = key ? hmac(fn, key, buf) : fn(buf)
|
|
bufs = null
|
|
return enc ? r.toString(enc) : r
|
|
}
|
|
}
|
|
}
|
|
|
|
function error () {
|
|
var m = [].slice.call(arguments).join(' ')
|
|
throw new Error([
|
|
m,
|
|
'we accept pull requests',
|
|
'http://github.com/dominictarr/crypto-browserify'
|
|
].join('\n'))
|
|
}
|
|
|
|
exports.createHash = function (alg) { return hash(alg) }
|
|
exports.createHmac = function (alg, key) { return hash(alg, key) }
|
|
exports.randomBytes = function(size, callback) {
|
|
if (callback && callback.call) {
|
|
try {
|
|
callback.call(this, undefined, new Buffer(rng(size)))
|
|
} catch (err) { callback(err) }
|
|
} else {
|
|
return new Buffer(rng(size))
|
|
}
|
|
}
|
|
|
|
function each(a, f) {
|
|
for(var i in a)
|
|
f(a[i], i)
|
|
}
|
|
|
|
// the least I can do is make error messages for the rest of the node.js/crypto api.
|
|
each(['createCredentials'
|
|
, 'createCipher'
|
|
, 'createCipheriv'
|
|
, 'createDecipher'
|
|
, 'createDecipheriv'
|
|
, 'createSign'
|
|
, 'createVerify'
|
|
, 'createDiffieHellman'
|
|
, 'pbkdf2'], function (name) {
|
|
exports[name] = function () {
|
|
error('sorry,', name, 'is not implemented yet')
|
|
}
|
|
})
|
|
|
|
},{"./md5":12,"./rng":13,"./sha":14,"./sha256":15,"buffer":9}],12:[function(require,module,exports){
|
|
/*
|
|
* A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
|
|
* Digest Algorithm, as defined in RFC 1321.
|
|
* Version 2.1 Copyright (C) Paul Johnston 1999 - 2002.
|
|
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
|
|
* Distributed under the BSD License
|
|
* See http://pajhome.org.uk/crypt/md5 for more info.
|
|
*/
|
|
|
|
var helpers = require('./helpers');
|
|
|
|
/*
|
|
* Perform a simple self-test to see if the VM is working
|
|
*/
|
|
function md5_vm_test()
|
|
{
|
|
return hex_md5("abc") == "900150983cd24fb0d6963f7d28e17f72";
|
|
}
|
|
|
|
/*
|
|
* Calculate the MD5 of an array of little-endian words, and a bit length
|
|
*/
|
|
function core_md5(x, len)
|
|
{
|
|
/* append padding */
|
|
x[len >> 5] |= 0x80 << ((len) % 32);
|
|
x[(((len + 64) >>> 9) << 4) + 14] = len;
|
|
|
|
var a = 1732584193;
|
|
var b = -271733879;
|
|
var c = -1732584194;
|
|
var d = 271733878;
|
|
|
|
for(var i = 0; i < x.length; i += 16)
|
|
{
|
|
var olda = a;
|
|
var oldb = b;
|
|
var oldc = c;
|
|
var oldd = d;
|
|
|
|
a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
|
|
d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
|
|
c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
|
|
b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
|
|
a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
|
|
d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
|
|
c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
|
|
b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
|
|
a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
|
|
d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
|
|
c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
|
|
b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
|
|
a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
|
|
d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
|
|
c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
|
|
b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
|
|
|
|
a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
|
|
d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
|
|
c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
|
|
b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
|
|
a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
|
|
d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
|
|
c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
|
|
b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
|
|
a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
|
|
d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
|
|
c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
|
|
b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
|
|
a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
|
|
d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
|
|
c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
|
|
b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
|
|
|
|
a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
|
|
d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
|
|
c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
|
|
b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
|
|
a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
|
|
d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
|
|
c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
|
|
b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
|
|
a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
|
|
d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
|
|
c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
|
|
b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
|
|
a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
|
|
d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
|
|
c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
|
|
b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
|
|
|
|
a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
|
|
d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
|
|
c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
|
|
b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
|
|
a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
|
|
d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
|
|
c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
|
|
b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
|
|
a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
|
|
d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
|
|
c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
|
|
b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
|
|
a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
|
|
d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
|
|
c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
|
|
b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
|
|
|
|
a = safe_add(a, olda);
|
|
b = safe_add(b, oldb);
|
|
c = safe_add(c, oldc);
|
|
d = safe_add(d, oldd);
|
|
}
|
|
return Array(a, b, c, d);
|
|
|
|
}
|
|
|
|
/*
|
|
* These functions implement the four basic operations the algorithm uses.
|
|
*/
|
|
function md5_cmn(q, a, b, x, s, t)
|
|
{
|
|
return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
|
|
}
|
|
function md5_ff(a, b, c, d, x, s, t)
|
|
{
|
|
return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
|
|
}
|
|
function md5_gg(a, b, c, d, x, s, t)
|
|
{
|
|
return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
|
|
}
|
|
function md5_hh(a, b, c, d, x, s, t)
|
|
{
|
|
return md5_cmn(b ^ c ^ d, a, b, x, s, t);
|
|
}
|
|
function md5_ii(a, b, c, d, x, s, t)
|
|
{
|
|
return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
|
|
}
|
|
|
|
/*
|
|
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
|
|
* to work around bugs in some JS interpreters.
|
|
*/
|
|
function safe_add(x, y)
|
|
{
|
|
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
|
|
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
|
|
return (msw << 16) | (lsw & 0xFFFF);
|
|
}
|
|
|
|
/*
|
|
* Bitwise rotate a 32-bit number to the left.
|
|
*/
|
|
function bit_rol(num, cnt)
|
|
{
|
|
return (num << cnt) | (num >>> (32 - cnt));
|
|
}
|
|
|
|
module.exports = function md5(buf) {
|
|
return helpers.hash(buf, core_md5, 16);
|
|
};
|
|
|
|
},{"./helpers":10}],13:[function(require,module,exports){
|
|
// Original code adapted from Robert Kieffer.
|
|
// details at https://github.com/broofa/node-uuid
|
|
(function() {
|
|
var _global = this;
|
|
|
|
var mathRNG, whatwgRNG;
|
|
|
|
// NOTE: Math.random() does not guarantee "cryptographic quality"
|
|
mathRNG = function(size) {
|
|
var bytes = new Array(size);
|
|
var r;
|
|
|
|
for (var i = 0, r; i < size; i++) {
|
|
if ((i & 0x03) == 0) r = Math.random() * 0x100000000;
|
|
bytes[i] = r >>> ((i & 0x03) << 3) & 0xff;
|
|
}
|
|
|
|
return bytes;
|
|
}
|
|
|
|
if (_global.crypto && crypto.getRandomValues) {
|
|
whatwgRNG = function(size) {
|
|
var bytes = new Uint8Array(size);
|
|
crypto.getRandomValues(bytes);
|
|
return bytes;
|
|
}
|
|
}
|
|
|
|
module.exports = whatwgRNG || mathRNG;
|
|
|
|
}())
|
|
|
|
},{}],14:[function(require,module,exports){
|
|
/*
|
|
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
|
|
* in FIPS PUB 180-1
|
|
* Version 2.1a Copyright Paul Johnston 2000 - 2002.
|
|
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
|
|
* Distributed under the BSD License
|
|
* See http://pajhome.org.uk/crypt/md5 for details.
|
|
*/
|
|
|
|
var helpers = require('./helpers');
|
|
|
|
/*
|
|
* Calculate the SHA-1 of an array of big-endian words, and a bit length
|
|
*/
|
|
function core_sha1(x, len)
|
|
{
|
|
/* append padding */
|
|
x[len >> 5] |= 0x80 << (24 - len % 32);
|
|
x[((len + 64 >> 9) << 4) + 15] = len;
|
|
|
|
var w = Array(80);
|
|
var a = 1732584193;
|
|
var b = -271733879;
|
|
var c = -1732584194;
|
|
var d = 271733878;
|
|
var e = -1009589776;
|
|
|
|
for(var i = 0; i < x.length; i += 16)
|
|
{
|
|
var olda = a;
|
|
var oldb = b;
|
|
var oldc = c;
|
|
var oldd = d;
|
|
var olde = e;
|
|
|
|
for(var j = 0; j < 80; j++)
|
|
{
|
|
if(j < 16) w[j] = x[i + j];
|
|
else w[j] = rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
|
|
var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
|
|
safe_add(safe_add(e, w[j]), sha1_kt(j)));
|
|
e = d;
|
|
d = c;
|
|
c = rol(b, 30);
|
|
b = a;
|
|
a = t;
|
|
}
|
|
|
|
a = safe_add(a, olda);
|
|
b = safe_add(b, oldb);
|
|
c = safe_add(c, oldc);
|
|
d = safe_add(d, oldd);
|
|
e = safe_add(e, olde);
|
|
}
|
|
return Array(a, b, c, d, e);
|
|
|
|
}
|
|
|
|
/*
|
|
* Perform the appropriate triplet combination function for the current
|
|
* iteration
|
|
*/
|
|
function sha1_ft(t, b, c, d)
|
|
{
|
|
if(t < 20) return (b & c) | ((~b) & d);
|
|
if(t < 40) return b ^ c ^ d;
|
|
if(t < 60) return (b & c) | (b & d) | (c & d);
|
|
return b ^ c ^ d;
|
|
}
|
|
|
|
/*
|
|
* Determine the appropriate additive constant for the current iteration
|
|
*/
|
|
function sha1_kt(t)
|
|
{
|
|
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
|
|
(t < 60) ? -1894007588 : -899497514;
|
|
}
|
|
|
|
/*
|
|
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
|
|
* to work around bugs in some JS interpreters.
|
|
*/
|
|
function safe_add(x, y)
|
|
{
|
|
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
|
|
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
|
|
return (msw << 16) | (lsw & 0xFFFF);
|
|
}
|
|
|
|
/*
|
|
* Bitwise rotate a 32-bit number to the left.
|
|
*/
|
|
function rol(num, cnt)
|
|
{
|
|
return (num << cnt) | (num >>> (32 - cnt));
|
|
}
|
|
|
|
module.exports = function sha1(buf) {
|
|
return helpers.hash(buf, core_sha1, 20, true);
|
|
};
|
|
|
|
},{"./helpers":10}],15:[function(require,module,exports){
|
|
|
|
/**
|
|
* A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined
|
|
* in FIPS 180-2
|
|
* Version 2.2-beta Copyright Angel Marin, Paul Johnston 2000 - 2009.
|
|
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
|
|
*
|
|
*/
|
|
|
|
var helpers = require('./helpers');
|
|
|
|
var safe_add = function(x, y) {
|
|
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
|
|
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
|
|
return (msw << 16) | (lsw & 0xFFFF);
|
|
};
|
|
|
|
var S = function(X, n) {
|
|
return (X >>> n) | (X << (32 - n));
|
|
};
|
|
|
|
var R = function(X, n) {
|
|
return (X >>> n);
|
|
};
|
|
|
|
var Ch = function(x, y, z) {
|
|
return ((x & y) ^ ((~x) & z));
|
|
};
|
|
|
|
var Maj = function(x, y, z) {
|
|
return ((x & y) ^ (x & z) ^ (y & z));
|
|
};
|
|
|
|
var Sigma0256 = function(x) {
|
|
return (S(x, 2) ^ S(x, 13) ^ S(x, 22));
|
|
};
|
|
|
|
var Sigma1256 = function(x) {
|
|
return (S(x, 6) ^ S(x, 11) ^ S(x, 25));
|
|
};
|
|
|
|
var Gamma0256 = function(x) {
|
|
return (S(x, 7) ^ S(x, 18) ^ R(x, 3));
|
|
};
|
|
|
|
var Gamma1256 = function(x) {
|
|
return (S(x, 17) ^ S(x, 19) ^ R(x, 10));
|
|
};
|
|
|
|
var core_sha256 = function(m, l) {
|
|
var K = new Array(0x428A2F98,0x71374491,0xB5C0FBCF,0xE9B5DBA5,0x3956C25B,0x59F111F1,0x923F82A4,0xAB1C5ED5,0xD807AA98,0x12835B01,0x243185BE,0x550C7DC3,0x72BE5D74,0x80DEB1FE,0x9BDC06A7,0xC19BF174,0xE49B69C1,0xEFBE4786,0xFC19DC6,0x240CA1CC,0x2DE92C6F,0x4A7484AA,0x5CB0A9DC,0x76F988DA,0x983E5152,0xA831C66D,0xB00327C8,0xBF597FC7,0xC6E00BF3,0xD5A79147,0x6CA6351,0x14292967,0x27B70A85,0x2E1B2138,0x4D2C6DFC,0x53380D13,0x650A7354,0x766A0ABB,0x81C2C92E,0x92722C85,0xA2BFE8A1,0xA81A664B,0xC24B8B70,0xC76C51A3,0xD192E819,0xD6990624,0xF40E3585,0x106AA070,0x19A4C116,0x1E376C08,0x2748774C,0x34B0BCB5,0x391C0CB3,0x4ED8AA4A,0x5B9CCA4F,0x682E6FF3,0x748F82EE,0x78A5636F,0x84C87814,0x8CC70208,0x90BEFFFA,0xA4506CEB,0xBEF9A3F7,0xC67178F2);
|
|
var HASH = new Array(0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19);
|
|
var W = new Array(64);
|
|
var a, b, c, d, e, f, g, h, i, j;
|
|
var T1, T2;
|
|
/* append padding */
|
|
m[l >> 5] |= 0x80 << (24 - l % 32);
|
|
m[((l + 64 >> 9) << 4) + 15] = l;
|
|
for (var i = 0; i < m.length; i += 16) {
|
|
a = HASH[0]; b = HASH[1]; c = HASH[2]; d = HASH[3]; e = HASH[4]; f = HASH[5]; g = HASH[6]; h = HASH[7];
|
|
for (var j = 0; j < 64; j++) {
|
|
if (j < 16) {
|
|
W[j] = m[j + i];
|
|
} else {
|
|
W[j] = safe_add(safe_add(safe_add(Gamma1256(W[j - 2]), W[j - 7]), Gamma0256(W[j - 15])), W[j - 16]);
|
|
}
|
|
T1 = safe_add(safe_add(safe_add(safe_add(h, Sigma1256(e)), Ch(e, f, g)), K[j]), W[j]);
|
|
T2 = safe_add(Sigma0256(a), Maj(a, b, c));
|
|
h = g; g = f; f = e; e = safe_add(d, T1); d = c; c = b; b = a; a = safe_add(T1, T2);
|
|
}
|
|
HASH[0] = safe_add(a, HASH[0]); HASH[1] = safe_add(b, HASH[1]); HASH[2] = safe_add(c, HASH[2]); HASH[3] = safe_add(d, HASH[3]);
|
|
HASH[4] = safe_add(e, HASH[4]); HASH[5] = safe_add(f, HASH[5]); HASH[6] = safe_add(g, HASH[6]); HASH[7] = safe_add(h, HASH[7]);
|
|
}
|
|
return HASH;
|
|
};
|
|
|
|
module.exports = function sha256(buf) {
|
|
return helpers.hash(buf, core_sha256, 32, true);
|
|
};
|
|
|
|
},{"./helpers":10}],16:[function(require,module,exports){
|
|
;(function (root, factory, undef) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"), require("./enc-base64"), require("./md5"), require("./evpkdf"), require("./cipher-core"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core", "./enc-base64", "./md5", "./evpkdf", "./cipher-core"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
(function () {
|
|
// Shortcuts
|
|
var C = CryptoJS;
|
|
var C_lib = C.lib;
|
|
var BlockCipher = C_lib.BlockCipher;
|
|
var C_algo = C.algo;
|
|
|
|
// Lookup tables
|
|
var SBOX = [];
|
|
var INV_SBOX = [];
|
|
var SUB_MIX_0 = [];
|
|
var SUB_MIX_1 = [];
|
|
var SUB_MIX_2 = [];
|
|
var SUB_MIX_3 = [];
|
|
var INV_SUB_MIX_0 = [];
|
|
var INV_SUB_MIX_1 = [];
|
|
var INV_SUB_MIX_2 = [];
|
|
var INV_SUB_MIX_3 = [];
|
|
|
|
// Compute lookup tables
|
|
(function () {
|
|
// Compute double table
|
|
var d = [];
|
|
for (var i = 0; i < 256; i++) {
|
|
if (i < 128) {
|
|
d[i] = i << 1;
|
|
} else {
|
|
d[i] = (i << 1) ^ 0x11b;
|
|
}
|
|
}
|
|
|
|
// Walk GF(2^8)
|
|
var x = 0;
|
|
var xi = 0;
|
|
for (var i = 0; i < 256; i++) {
|
|
// Compute sbox
|
|
var sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
|
|
sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
|
|
SBOX[x] = sx;
|
|
INV_SBOX[sx] = x;
|
|
|
|
// Compute multiplication
|
|
var x2 = d[x];
|
|
var x4 = d[x2];
|
|
var x8 = d[x4];
|
|
|
|
// Compute sub bytes, mix columns tables
|
|
var t = (d[sx] * 0x101) ^ (sx * 0x1010100);
|
|
SUB_MIX_0[x] = (t << 24) | (t >>> 8);
|
|
SUB_MIX_1[x] = (t << 16) | (t >>> 16);
|
|
SUB_MIX_2[x] = (t << 8) | (t >>> 24);
|
|
SUB_MIX_3[x] = t;
|
|
|
|
// Compute inv sub bytes, inv mix columns tables
|
|
var t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
|
|
INV_SUB_MIX_0[sx] = (t << 24) | (t >>> 8);
|
|
INV_SUB_MIX_1[sx] = (t << 16) | (t >>> 16);
|
|
INV_SUB_MIX_2[sx] = (t << 8) | (t >>> 24);
|
|
INV_SUB_MIX_3[sx] = t;
|
|
|
|
// Compute next counter
|
|
if (!x) {
|
|
x = xi = 1;
|
|
} else {
|
|
x = x2 ^ d[d[d[x8 ^ x2]]];
|
|
xi ^= d[d[xi]];
|
|
}
|
|
}
|
|
}());
|
|
|
|
// Precomputed Rcon lookup
|
|
var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
|
|
|
|
/**
|
|
* AES block cipher algorithm.
|
|
*/
|
|
var AES = C_algo.AES = BlockCipher.extend({
|
|
_doReset: function () {
|
|
// Skip reset of nRounds has been set before and key did not change
|
|
if (this._nRounds && this._keyPriorReset === this._key) {
|
|
return;
|
|
}
|
|
|
|
// Shortcuts
|
|
var key = this._keyPriorReset = this._key;
|
|
var keyWords = key.words;
|
|
var keySize = key.sigBytes / 4;
|
|
|
|
// Compute number of rounds
|
|
var nRounds = this._nRounds = keySize + 6;
|
|
|
|
// Compute number of key schedule rows
|
|
var ksRows = (nRounds + 1) * 4;
|
|
|
|
// Compute key schedule
|
|
var keySchedule = this._keySchedule = [];
|
|
for (var ksRow = 0; ksRow < ksRows; ksRow++) {
|
|
if (ksRow < keySize) {
|
|
keySchedule[ksRow] = keyWords[ksRow];
|
|
} else {
|
|
var t = keySchedule[ksRow - 1];
|
|
|
|
if (!(ksRow % keySize)) {
|
|
// Rot word
|
|
t = (t << 8) | (t >>> 24);
|
|
|
|
// Sub word
|
|
t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
|
|
|
|
// Mix Rcon
|
|
t ^= RCON[(ksRow / keySize) | 0] << 24;
|
|
} else if (keySize > 6 && ksRow % keySize == 4) {
|
|
// Sub word
|
|
t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
|
|
}
|
|
|
|
keySchedule[ksRow] = keySchedule[ksRow - keySize] ^ t;
|
|
}
|
|
}
|
|
|
|
// Compute inv key schedule
|
|
var invKeySchedule = this._invKeySchedule = [];
|
|
for (var invKsRow = 0; invKsRow < ksRows; invKsRow++) {
|
|
var ksRow = ksRows - invKsRow;
|
|
|
|
if (invKsRow % 4) {
|
|
var t = keySchedule[ksRow];
|
|
} else {
|
|
var t = keySchedule[ksRow - 4];
|
|
}
|
|
|
|
if (invKsRow < 4 || ksRow <= 4) {
|
|
invKeySchedule[invKsRow] = t;
|
|
} else {
|
|
invKeySchedule[invKsRow] = INV_SUB_MIX_0[SBOX[t >>> 24]] ^ INV_SUB_MIX_1[SBOX[(t >>> 16) & 0xff]] ^
|
|
INV_SUB_MIX_2[SBOX[(t >>> 8) & 0xff]] ^ INV_SUB_MIX_3[SBOX[t & 0xff]];
|
|
}
|
|
}
|
|
},
|
|
|
|
encryptBlock: function (M, offset) {
|
|
this._doCryptBlock(M, offset, this._keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX);
|
|
},
|
|
|
|
decryptBlock: function (M, offset) {
|
|
// Swap 2nd and 4th rows
|
|
var t = M[offset + 1];
|
|
M[offset + 1] = M[offset + 3];
|
|
M[offset + 3] = t;
|
|
|
|
this._doCryptBlock(M, offset, this._invKeySchedule, INV_SUB_MIX_0, INV_SUB_MIX_1, INV_SUB_MIX_2, INV_SUB_MIX_3, INV_SBOX);
|
|
|
|
// Inv swap 2nd and 4th rows
|
|
var t = M[offset + 1];
|
|
M[offset + 1] = M[offset + 3];
|
|
M[offset + 3] = t;
|
|
},
|
|
|
|
_doCryptBlock: function (M, offset, keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX) {
|
|
// Shortcut
|
|
var nRounds = this._nRounds;
|
|
|
|
// Get input, add round key
|
|
var s0 = M[offset] ^ keySchedule[0];
|
|
var s1 = M[offset + 1] ^ keySchedule[1];
|
|
var s2 = M[offset + 2] ^ keySchedule[2];
|
|
var s3 = M[offset + 3] ^ keySchedule[3];
|
|
|
|
// Key schedule row counter
|
|
var ksRow = 4;
|
|
|
|
// Rounds
|
|
for (var round = 1; round < nRounds; round++) {
|
|
// Shift rows, sub bytes, mix columns, add round key
|
|
var t0 = SUB_MIX_0[s0 >>> 24] ^ SUB_MIX_1[(s1 >>> 16) & 0xff] ^ SUB_MIX_2[(s2 >>> 8) & 0xff] ^ SUB_MIX_3[s3 & 0xff] ^ keySchedule[ksRow++];
|
|
var t1 = SUB_MIX_0[s1 >>> 24] ^ SUB_MIX_1[(s2 >>> 16) & 0xff] ^ SUB_MIX_2[(s3 >>> 8) & 0xff] ^ SUB_MIX_3[s0 & 0xff] ^ keySchedule[ksRow++];
|
|
var t2 = SUB_MIX_0[s2 >>> 24] ^ SUB_MIX_1[(s3 >>> 16) & 0xff] ^ SUB_MIX_2[(s0 >>> 8) & 0xff] ^ SUB_MIX_3[s1 & 0xff] ^ keySchedule[ksRow++];
|
|
var t3 = SUB_MIX_0[s3 >>> 24] ^ SUB_MIX_1[(s0 >>> 16) & 0xff] ^ SUB_MIX_2[(s1 >>> 8) & 0xff] ^ SUB_MIX_3[s2 & 0xff] ^ keySchedule[ksRow++];
|
|
|
|
// Update state
|
|
s0 = t0;
|
|
s1 = t1;
|
|
s2 = t2;
|
|
s3 = t3;
|
|
}
|
|
|
|
// Shift rows, sub bytes, add round key
|
|
var t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
|
|
var t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
|
|
var t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
|
|
var t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];
|
|
|
|
// Set output
|
|
M[offset] = t0;
|
|
M[offset + 1] = t1;
|
|
M[offset + 2] = t2;
|
|
M[offset + 3] = t3;
|
|
},
|
|
|
|
keySize: 256/32
|
|
});
|
|
|
|
/**
|
|
* Shortcut functions to the cipher's object interface.
|
|
*
|
|
* @example
|
|
*
|
|
* var ciphertext = CryptoJS.AES.encrypt(message, key, cfg);
|
|
* var plaintext = CryptoJS.AES.decrypt(ciphertext, key, cfg);
|
|
*/
|
|
C.AES = BlockCipher._createHelper(AES);
|
|
}());
|
|
|
|
|
|
return CryptoJS.AES;
|
|
|
|
}));
|
|
},{"./cipher-core":17,"./core":18,"./enc-base64":19,"./evpkdf":23,"./md5":26}],17:[function(require,module,exports){
|
|
;(function (root, factory) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
/**
|
|
* Cipher core components.
|
|
*/
|
|
CryptoJS.lib.Cipher || (function (undefined) {
|
|
// Shortcuts
|
|
var C = CryptoJS;
|
|
var C_lib = C.lib;
|
|
var Base = C_lib.Base;
|
|
var WordArray = C_lib.WordArray;
|
|
var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm;
|
|
var C_enc = C.enc;
|
|
var Utf8 = C_enc.Utf8;
|
|
var Base64 = C_enc.Base64;
|
|
var C_algo = C.algo;
|
|
var EvpKDF = C_algo.EvpKDF;
|
|
|
|
/**
|
|
* Abstract base cipher template.
|
|
*
|
|
* @property {number} keySize This cipher's key size. Default: 4 (128 bits)
|
|
* @property {number} ivSize This cipher's IV size. Default: 4 (128 bits)
|
|
* @property {number} _ENC_XFORM_MODE A constant representing encryption mode.
|
|
* @property {number} _DEC_XFORM_MODE A constant representing decryption mode.
|
|
*/
|
|
var Cipher = C_lib.Cipher = BufferedBlockAlgorithm.extend({
|
|
/**
|
|
* Configuration options.
|
|
*
|
|
* @property {WordArray} iv The IV to use for this operation.
|
|
*/
|
|
cfg: Base.extend(),
|
|
|
|
/**
|
|
* Creates this cipher in encryption mode.
|
|
*
|
|
* @param {WordArray} key The key.
|
|
* @param {Object} cfg (Optional) The configuration options to use for this operation.
|
|
*
|
|
* @return {Cipher} A cipher instance.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var cipher = CryptoJS.algo.AES.createEncryptor(keyWordArray, { iv: ivWordArray });
|
|
*/
|
|
createEncryptor: function (key, cfg) {
|
|
return this.create(this._ENC_XFORM_MODE, key, cfg);
|
|
},
|
|
|
|
/**
|
|
* Creates this cipher in decryption mode.
|
|
*
|
|
* @param {WordArray} key The key.
|
|
* @param {Object} cfg (Optional) The configuration options to use for this operation.
|
|
*
|
|
* @return {Cipher} A cipher instance.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var cipher = CryptoJS.algo.AES.createDecryptor(keyWordArray, { iv: ivWordArray });
|
|
*/
|
|
createDecryptor: function (key, cfg) {
|
|
return this.create(this._DEC_XFORM_MODE, key, cfg);
|
|
},
|
|
|
|
/**
|
|
* Initializes a newly created cipher.
|
|
*
|
|
* @param {number} xformMode Either the encryption or decryption transormation mode constant.
|
|
* @param {WordArray} key The key.
|
|
* @param {Object} cfg (Optional) The configuration options to use for this operation.
|
|
*
|
|
* @example
|
|
*
|
|
* var cipher = CryptoJS.algo.AES.create(CryptoJS.algo.AES._ENC_XFORM_MODE, keyWordArray, { iv: ivWordArray });
|
|
*/
|
|
init: function (xformMode, key, cfg) {
|
|
// Apply config defaults
|
|
this.cfg = this.cfg.extend(cfg);
|
|
|
|
// Store transform mode and key
|
|
this._xformMode = xformMode;
|
|
this._key = key;
|
|
|
|
// Set initial values
|
|
this.reset();
|
|
},
|
|
|
|
/**
|
|
* Resets this cipher to its initial state.
|
|
*
|
|
* @example
|
|
*
|
|
* cipher.reset();
|
|
*/
|
|
reset: function () {
|
|
// Reset data buffer
|
|
BufferedBlockAlgorithm.reset.call(this);
|
|
|
|
// Perform concrete-cipher logic
|
|
this._doReset();
|
|
},
|
|
|
|
/**
|
|
* Adds data to be encrypted or decrypted.
|
|
*
|
|
* @param {WordArray|string} dataUpdate The data to encrypt or decrypt.
|
|
*
|
|
* @return {WordArray} The data after processing.
|
|
*
|
|
* @example
|
|
*
|
|
* var encrypted = cipher.process('data');
|
|
* var encrypted = cipher.process(wordArray);
|
|
*/
|
|
process: function (dataUpdate) {
|
|
// Append
|
|
this._append(dataUpdate);
|
|
|
|
// Process available blocks
|
|
return this._process();
|
|
},
|
|
|
|
/**
|
|
* Finalizes the encryption or decryption process.
|
|
* Note that the finalize operation is effectively a destructive, read-once operation.
|
|
*
|
|
* @param {WordArray|string} dataUpdate The final data to encrypt or decrypt.
|
|
*
|
|
* @return {WordArray} The data after final processing.
|
|
*
|
|
* @example
|
|
*
|
|
* var encrypted = cipher.finalize();
|
|
* var encrypted = cipher.finalize('data');
|
|
* var encrypted = cipher.finalize(wordArray);
|
|
*/
|
|
finalize: function (dataUpdate) {
|
|
// Final data update
|
|
if (dataUpdate) {
|
|
this._append(dataUpdate);
|
|
}
|
|
|
|
// Perform concrete-cipher logic
|
|
var finalProcessedData = this._doFinalize();
|
|
|
|
return finalProcessedData;
|
|
},
|
|
|
|
keySize: 128/32,
|
|
|
|
ivSize: 128/32,
|
|
|
|
_ENC_XFORM_MODE: 1,
|
|
|
|
_DEC_XFORM_MODE: 2,
|
|
|
|
/**
|
|
* Creates shortcut functions to a cipher's object interface.
|
|
*
|
|
* @param {Cipher} cipher The cipher to create a helper for.
|
|
*
|
|
* @return {Object} An object with encrypt and decrypt shortcut functions.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var AES = CryptoJS.lib.Cipher._createHelper(CryptoJS.algo.AES);
|
|
*/
|
|
_createHelper: (function () {
|
|
function selectCipherStrategy(key) {
|
|
if (typeof key == 'string') {
|
|
return PasswordBasedCipher;
|
|
} else {
|
|
return SerializableCipher;
|
|
}
|
|
}
|
|
|
|
return function (cipher) {
|
|
return {
|
|
encrypt: function (message, key, cfg) {
|
|
return selectCipherStrategy(key).encrypt(cipher, message, key, cfg);
|
|
},
|
|
|
|
decrypt: function (ciphertext, key, cfg) {
|
|
return selectCipherStrategy(key).decrypt(cipher, ciphertext, key, cfg);
|
|
}
|
|
};
|
|
};
|
|
}())
|
|
});
|
|
|
|
/**
|
|
* Abstract base stream cipher template.
|
|
*
|
|
* @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 1 (32 bits)
|
|
*/
|
|
var StreamCipher = C_lib.StreamCipher = Cipher.extend({
|
|
_doFinalize: function () {
|
|
// Process partial blocks
|
|
var finalProcessedBlocks = this._process(!!'flush');
|
|
|
|
return finalProcessedBlocks;
|
|
},
|
|
|
|
blockSize: 1
|
|
});
|
|
|
|
/**
|
|
* Mode namespace.
|
|
*/
|
|
var C_mode = C.mode = {};
|
|
|
|
/**
|
|
* Abstract base block cipher mode template.
|
|
*/
|
|
var BlockCipherMode = C_lib.BlockCipherMode = Base.extend({
|
|
/**
|
|
* Creates this mode for encryption.
|
|
*
|
|
* @param {Cipher} cipher A block cipher instance.
|
|
* @param {Array} iv The IV words.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var mode = CryptoJS.mode.CBC.createEncryptor(cipher, iv.words);
|
|
*/
|
|
createEncryptor: function (cipher, iv) {
|
|
return this.Encryptor.create(cipher, iv);
|
|
},
|
|
|
|
/**
|
|
* Creates this mode for decryption.
|
|
*
|
|
* @param {Cipher} cipher A block cipher instance.
|
|
* @param {Array} iv The IV words.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var mode = CryptoJS.mode.CBC.createDecryptor(cipher, iv.words);
|
|
*/
|
|
createDecryptor: function (cipher, iv) {
|
|
return this.Decryptor.create(cipher, iv);
|
|
},
|
|
|
|
/**
|
|
* Initializes a newly created mode.
|
|
*
|
|
* @param {Cipher} cipher A block cipher instance.
|
|
* @param {Array} iv The IV words.
|
|
*
|
|
* @example
|
|
*
|
|
* var mode = CryptoJS.mode.CBC.Encryptor.create(cipher, iv.words);
|
|
*/
|
|
init: function (cipher, iv) {
|
|
this._cipher = cipher;
|
|
this._iv = iv;
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Cipher Block Chaining mode.
|
|
*/
|
|
var CBC = C_mode.CBC = (function () {
|
|
/**
|
|
* Abstract base CBC mode.
|
|
*/
|
|
var CBC = BlockCipherMode.extend();
|
|
|
|
/**
|
|
* CBC encryptor.
|
|
*/
|
|
CBC.Encryptor = CBC.extend({
|
|
/**
|
|
* Processes the data block at offset.
|
|
*
|
|
* @param {Array} words The data words to operate on.
|
|
* @param {number} offset The offset where the block starts.
|
|
*
|
|
* @example
|
|
*
|
|
* mode.processBlock(data.words, offset);
|
|
*/
|
|
processBlock: function (words, offset) {
|
|
// Shortcuts
|
|
var cipher = this._cipher;
|
|
var blockSize = cipher.blockSize;
|
|
|
|
// XOR and encrypt
|
|
xorBlock.call(this, words, offset, blockSize);
|
|
cipher.encryptBlock(words, offset);
|
|
|
|
// Remember this block to use with next block
|
|
this._prevBlock = words.slice(offset, offset + blockSize);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* CBC decryptor.
|
|
*/
|
|
CBC.Decryptor = CBC.extend({
|
|
/**
|
|
* Processes the data block at offset.
|
|
*
|
|
* @param {Array} words The data words to operate on.
|
|
* @param {number} offset The offset where the block starts.
|
|
*
|
|
* @example
|
|
*
|
|
* mode.processBlock(data.words, offset);
|
|
*/
|
|
processBlock: function (words, offset) {
|
|
// Shortcuts
|
|
var cipher = this._cipher;
|
|
var blockSize = cipher.blockSize;
|
|
|
|
// Remember this block to use with next block
|
|
var thisBlock = words.slice(offset, offset + blockSize);
|
|
|
|
// Decrypt and XOR
|
|
cipher.decryptBlock(words, offset);
|
|
xorBlock.call(this, words, offset, blockSize);
|
|
|
|
// This block becomes the previous block
|
|
this._prevBlock = thisBlock;
|
|
}
|
|
});
|
|
|
|
function xorBlock(words, offset, blockSize) {
|
|
// Shortcut
|
|
var iv = this._iv;
|
|
|
|
// Choose mixing block
|
|
if (iv) {
|
|
var block = iv;
|
|
|
|
// Remove IV for subsequent blocks
|
|
this._iv = undefined;
|
|
} else {
|
|
var block = this._prevBlock;
|
|
}
|
|
|
|
// XOR blocks
|
|
for (var i = 0; i < blockSize; i++) {
|
|
words[offset + i] ^= block[i];
|
|
}
|
|
}
|
|
|
|
return CBC;
|
|
}());
|
|
|
|
/**
|
|
* Padding namespace.
|
|
*/
|
|
var C_pad = C.pad = {};
|
|
|
|
/**
|
|
* PKCS #5/7 padding strategy.
|
|
*/
|
|
var Pkcs7 = C_pad.Pkcs7 = {
|
|
/**
|
|
* Pads data using the algorithm defined in PKCS #5/7.
|
|
*
|
|
* @param {WordArray} data The data to pad.
|
|
* @param {number} blockSize The multiple that the data should be padded to.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* CryptoJS.pad.Pkcs7.pad(wordArray, 4);
|
|
*/
|
|
pad: function (data, blockSize) {
|
|
// Shortcut
|
|
var blockSizeBytes = blockSize * 4;
|
|
|
|
// Count padding bytes
|
|
var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
|
|
|
|
// Create padding word
|
|
var paddingWord = (nPaddingBytes << 24) | (nPaddingBytes << 16) | (nPaddingBytes << 8) | nPaddingBytes;
|
|
|
|
// Create padding
|
|
var paddingWords = [];
|
|
for (var i = 0; i < nPaddingBytes; i += 4) {
|
|
paddingWords.push(paddingWord);
|
|
}
|
|
var padding = WordArray.create(paddingWords, nPaddingBytes);
|
|
|
|
// Add padding
|
|
data.concat(padding);
|
|
},
|
|
|
|
/**
|
|
* Unpads data that had been padded using the algorithm defined in PKCS #5/7.
|
|
*
|
|
* @param {WordArray} data The data to unpad.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* CryptoJS.pad.Pkcs7.unpad(wordArray);
|
|
*/
|
|
unpad: function (data) {
|
|
// Get number of padding bytes from last byte
|
|
var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
|
|
|
|
// Remove padding
|
|
data.sigBytes -= nPaddingBytes;
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Abstract base block cipher template.
|
|
*
|
|
* @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 4 (128 bits)
|
|
*/
|
|
var BlockCipher = C_lib.BlockCipher = Cipher.extend({
|
|
/**
|
|
* Configuration options.
|
|
*
|
|
* @property {Mode} mode The block mode to use. Default: CBC
|
|
* @property {Padding} padding The padding strategy to use. Default: Pkcs7
|
|
*/
|
|
cfg: Cipher.cfg.extend({
|
|
mode: CBC,
|
|
padding: Pkcs7
|
|
}),
|
|
|
|
reset: function () {
|
|
// Reset cipher
|
|
Cipher.reset.call(this);
|
|
|
|
// Shortcuts
|
|
var cfg = this.cfg;
|
|
var iv = cfg.iv;
|
|
var mode = cfg.mode;
|
|
|
|
// Reset block mode
|
|
if (this._xformMode == this._ENC_XFORM_MODE) {
|
|
var modeCreator = mode.createEncryptor;
|
|
} else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
|
|
var modeCreator = mode.createDecryptor;
|
|
|
|
// Keep at least one block in the buffer for unpadding
|
|
this._minBufferSize = 1;
|
|
}
|
|
this._mode = modeCreator.call(mode, this, iv && iv.words);
|
|
},
|
|
|
|
_doProcessBlock: function (words, offset) {
|
|
this._mode.processBlock(words, offset);
|
|
},
|
|
|
|
_doFinalize: function () {
|
|
// Shortcut
|
|
var padding = this.cfg.padding;
|
|
|
|
// Finalize
|
|
if (this._xformMode == this._ENC_XFORM_MODE) {
|
|
// Pad data
|
|
padding.pad(this._data, this.blockSize);
|
|
|
|
// Process final blocks
|
|
var finalProcessedBlocks = this._process(!!'flush');
|
|
} else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
|
|
// Process final blocks
|
|
var finalProcessedBlocks = this._process(!!'flush');
|
|
|
|
// Unpad data
|
|
padding.unpad(finalProcessedBlocks);
|
|
}
|
|
|
|
return finalProcessedBlocks;
|
|
},
|
|
|
|
blockSize: 128/32
|
|
});
|
|
|
|
/**
|
|
* A collection of cipher parameters.
|
|
*
|
|
* @property {WordArray} ciphertext The raw ciphertext.
|
|
* @property {WordArray} key The key to this ciphertext.
|
|
* @property {WordArray} iv The IV used in the ciphering operation.
|
|
* @property {WordArray} salt The salt used with a key derivation function.
|
|
* @property {Cipher} algorithm The cipher algorithm.
|
|
* @property {Mode} mode The block mode used in the ciphering operation.
|
|
* @property {Padding} padding The padding scheme used in the ciphering operation.
|
|
* @property {number} blockSize The block size of the cipher.
|
|
* @property {Format} formatter The default formatting strategy to convert this cipher params object to a string.
|
|
*/
|
|
var CipherParams = C_lib.CipherParams = Base.extend({
|
|
/**
|
|
* Initializes a newly created cipher params object.
|
|
*
|
|
* @param {Object} cipherParams An object with any of the possible cipher parameters.
|
|
*
|
|
* @example
|
|
*
|
|
* var cipherParams = CryptoJS.lib.CipherParams.create({
|
|
* ciphertext: ciphertextWordArray,
|
|
* key: keyWordArray,
|
|
* iv: ivWordArray,
|
|
* salt: saltWordArray,
|
|
* algorithm: CryptoJS.algo.AES,
|
|
* mode: CryptoJS.mode.CBC,
|
|
* padding: CryptoJS.pad.PKCS7,
|
|
* blockSize: 4,
|
|
* formatter: CryptoJS.format.OpenSSL
|
|
* });
|
|
*/
|
|
init: function (cipherParams) {
|
|
this.mixIn(cipherParams);
|
|
},
|
|
|
|
/**
|
|
* Converts this cipher params object to a string.
|
|
*
|
|
* @param {Format} formatter (Optional) The formatting strategy to use.
|
|
*
|
|
* @return {string} The stringified cipher params.
|
|
*
|
|
* @throws Error If neither the formatter nor the default formatter is set.
|
|
*
|
|
* @example
|
|
*
|
|
* var string = cipherParams + '';
|
|
* var string = cipherParams.toString();
|
|
* var string = cipherParams.toString(CryptoJS.format.OpenSSL);
|
|
*/
|
|
toString: function (formatter) {
|
|
return (formatter || this.formatter).stringify(this);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Format namespace.
|
|
*/
|
|
var C_format = C.format = {};
|
|
|
|
/**
|
|
* OpenSSL formatting strategy.
|
|
*/
|
|
var OpenSSLFormatter = C_format.OpenSSL = {
|
|
/**
|
|
* Converts a cipher params object to an OpenSSL-compatible string.
|
|
*
|
|
* @param {CipherParams} cipherParams The cipher params object.
|
|
*
|
|
* @return {string} The OpenSSL-compatible string.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var openSSLString = CryptoJS.format.OpenSSL.stringify(cipherParams);
|
|
*/
|
|
stringify: function (cipherParams) {
|
|
// Shortcuts
|
|
var ciphertext = cipherParams.ciphertext;
|
|
var salt = cipherParams.salt;
|
|
|
|
// Format
|
|
if (salt) {
|
|
var wordArray = WordArray.create([0x53616c74, 0x65645f5f]).concat(salt).concat(ciphertext);
|
|
} else {
|
|
var wordArray = ciphertext;
|
|
}
|
|
|
|
return wordArray.toString(Base64);
|
|
},
|
|
|
|
/**
|
|
* Converts an OpenSSL-compatible string to a cipher params object.
|
|
*
|
|
* @param {string} openSSLStr The OpenSSL-compatible string.
|
|
*
|
|
* @return {CipherParams} The cipher params object.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var cipherParams = CryptoJS.format.OpenSSL.parse(openSSLString);
|
|
*/
|
|
parse: function (openSSLStr) {
|
|
// Parse base64
|
|
var ciphertext = Base64.parse(openSSLStr);
|
|
|
|
// Shortcut
|
|
var ciphertextWords = ciphertext.words;
|
|
|
|
// Test for salt
|
|
if (ciphertextWords[0] == 0x53616c74 && ciphertextWords[1] == 0x65645f5f) {
|
|
// Extract salt
|
|
var salt = WordArray.create(ciphertextWords.slice(2, 4));
|
|
|
|
// Remove salt from ciphertext
|
|
ciphertextWords.splice(0, 4);
|
|
ciphertext.sigBytes -= 16;
|
|
}
|
|
|
|
return CipherParams.create({ ciphertext: ciphertext, salt: salt });
|
|
}
|
|
};
|
|
|
|
/**
|
|
* A cipher wrapper that returns ciphertext as a serializable cipher params object.
|
|
*/
|
|
var SerializableCipher = C_lib.SerializableCipher = Base.extend({
|
|
/**
|
|
* Configuration options.
|
|
*
|
|
* @property {Formatter} format The formatting strategy to convert cipher param objects to and from a string. Default: OpenSSL
|
|
*/
|
|
cfg: Base.extend({
|
|
format: OpenSSLFormatter
|
|
}),
|
|
|
|
/**
|
|
* Encrypts a message.
|
|
*
|
|
* @param {Cipher} cipher The cipher algorithm to use.
|
|
* @param {WordArray|string} message The message to encrypt.
|
|
* @param {WordArray} key The key.
|
|
* @param {Object} cfg (Optional) The configuration options to use for this operation.
|
|
*
|
|
* @return {CipherParams} A cipher params object.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key);
|
|
* var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv });
|
|
* var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv, format: CryptoJS.format.OpenSSL });
|
|
*/
|
|
encrypt: function (cipher, message, key, cfg) {
|
|
// Apply config defaults
|
|
cfg = this.cfg.extend(cfg);
|
|
|
|
// Encrypt
|
|
var encryptor = cipher.createEncryptor(key, cfg);
|
|
var ciphertext = encryptor.finalize(message);
|
|
|
|
// Shortcut
|
|
var cipherCfg = encryptor.cfg;
|
|
|
|
// Create and return serializable cipher params
|
|
return CipherParams.create({
|
|
ciphertext: ciphertext,
|
|
key: key,
|
|
iv: cipherCfg.iv,
|
|
algorithm: cipher,
|
|
mode: cipherCfg.mode,
|
|
padding: cipherCfg.padding,
|
|
blockSize: cipher.blockSize,
|
|
formatter: cfg.format
|
|
});
|
|
},
|
|
|
|
/**
|
|
* Decrypts serialized ciphertext.
|
|
*
|
|
* @param {Cipher} cipher The cipher algorithm to use.
|
|
* @param {CipherParams|string} ciphertext The ciphertext to decrypt.
|
|
* @param {WordArray} key The key.
|
|
* @param {Object} cfg (Optional) The configuration options to use for this operation.
|
|
*
|
|
* @return {WordArray} The plaintext.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, key, { iv: iv, format: CryptoJS.format.OpenSSL });
|
|
* var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, key, { iv: iv, format: CryptoJS.format.OpenSSL });
|
|
*/
|
|
decrypt: function (cipher, ciphertext, key, cfg) {
|
|
// Apply config defaults
|
|
cfg = this.cfg.extend(cfg);
|
|
|
|
// Convert string to CipherParams
|
|
ciphertext = this._parse(ciphertext, cfg.format);
|
|
|
|
// Decrypt
|
|
var plaintext = cipher.createDecryptor(key, cfg).finalize(ciphertext.ciphertext);
|
|
|
|
return plaintext;
|
|
},
|
|
|
|
/**
|
|
* Converts serialized ciphertext to CipherParams,
|
|
* else assumed CipherParams already and returns ciphertext unchanged.
|
|
*
|
|
* @param {CipherParams|string} ciphertext The ciphertext.
|
|
* @param {Formatter} format The formatting strategy to use to parse serialized ciphertext.
|
|
*
|
|
* @return {CipherParams} The unserialized ciphertext.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format);
|
|
*/
|
|
_parse: function (ciphertext, format) {
|
|
if (typeof ciphertext == 'string') {
|
|
return format.parse(ciphertext, this);
|
|
} else {
|
|
return ciphertext;
|
|
}
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Key derivation function namespace.
|
|
*/
|
|
var C_kdf = C.kdf = {};
|
|
|
|
/**
|
|
* OpenSSL key derivation function.
|
|
*/
|
|
var OpenSSLKdf = C_kdf.OpenSSL = {
|
|
/**
|
|
* Derives a key and IV from a password.
|
|
*
|
|
* @param {string} password The password to derive from.
|
|
* @param {number} keySize The size in words of the key to generate.
|
|
* @param {number} ivSize The size in words of the IV to generate.
|
|
* @param {WordArray|string} salt (Optional) A 64-bit salt to use. If omitted, a salt will be generated randomly.
|
|
*
|
|
* @return {CipherParams} A cipher params object with the key, IV, and salt.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32);
|
|
* var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32, 'saltsalt');
|
|
*/
|
|
execute: function (password, keySize, ivSize, salt) {
|
|
// Generate random salt
|
|
if (!salt) {
|
|
salt = WordArray.random(64/8);
|
|
}
|
|
|
|
// Derive key and IV
|
|
var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt);
|
|
|
|
// Separate key and IV
|
|
var iv = WordArray.create(key.words.slice(keySize), ivSize * 4);
|
|
key.sigBytes = keySize * 4;
|
|
|
|
// Return params
|
|
return CipherParams.create({ key: key, iv: iv, salt: salt });
|
|
}
|
|
};
|
|
|
|
/**
|
|
* A serializable cipher wrapper that derives the key from a password,
|
|
* and returns ciphertext as a serializable cipher params object.
|
|
*/
|
|
var PasswordBasedCipher = C_lib.PasswordBasedCipher = SerializableCipher.extend({
|
|
/**
|
|
* Configuration options.
|
|
*
|
|
* @property {KDF} kdf The key derivation function to use to generate a key and IV from a password. Default: OpenSSL
|
|
*/
|
|
cfg: SerializableCipher.cfg.extend({
|
|
kdf: OpenSSLKdf
|
|
}),
|
|
|
|
/**
|
|
* Encrypts a message using a password.
|
|
*
|
|
* @param {Cipher} cipher The cipher algorithm to use.
|
|
* @param {WordArray|string} message The message to encrypt.
|
|
* @param {string} password The password.
|
|
* @param {Object} cfg (Optional) The configuration options to use for this operation.
|
|
*
|
|
* @return {CipherParams} A cipher params object.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password');
|
|
* var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password', { format: CryptoJS.format.OpenSSL });
|
|
*/
|
|
encrypt: function (cipher, message, password, cfg) {
|
|
// Apply config defaults
|
|
cfg = this.cfg.extend(cfg);
|
|
|
|
// Derive key and other params
|
|
var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize);
|
|
|
|
// Add IV to config
|
|
cfg.iv = derivedParams.iv;
|
|
|
|
// Encrypt
|
|
var ciphertext = SerializableCipher.encrypt.call(this, cipher, message, derivedParams.key, cfg);
|
|
|
|
// Mix in derived params
|
|
ciphertext.mixIn(derivedParams);
|
|
|
|
return ciphertext;
|
|
},
|
|
|
|
/**
|
|
* Decrypts serialized ciphertext using a password.
|
|
*
|
|
* @param {Cipher} cipher The cipher algorithm to use.
|
|
* @param {CipherParams|string} ciphertext The ciphertext to decrypt.
|
|
* @param {string} password The password.
|
|
* @param {Object} cfg (Optional) The configuration options to use for this operation.
|
|
*
|
|
* @return {WordArray} The plaintext.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, 'password', { format: CryptoJS.format.OpenSSL });
|
|
* var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, 'password', { format: CryptoJS.format.OpenSSL });
|
|
*/
|
|
decrypt: function (cipher, ciphertext, password, cfg) {
|
|
// Apply config defaults
|
|
cfg = this.cfg.extend(cfg);
|
|
|
|
// Convert string to CipherParams
|
|
ciphertext = this._parse(ciphertext, cfg.format);
|
|
|
|
// Derive key and other params
|
|
var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt);
|
|
|
|
// Add IV to config
|
|
cfg.iv = derivedParams.iv;
|
|
|
|
// Decrypt
|
|
var plaintext = SerializableCipher.decrypt.call(this, cipher, ciphertext, derivedParams.key, cfg);
|
|
|
|
return plaintext;
|
|
}
|
|
});
|
|
}());
|
|
|
|
|
|
}));
|
|
},{"./core":18}],18:[function(require,module,exports){
|
|
;(function (root, factory) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory();
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define([], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
root.CryptoJS = factory();
|
|
}
|
|
}(this, function () {
|
|
|
|
/**
|
|
* CryptoJS core components.
|
|
*/
|
|
var CryptoJS = CryptoJS || (function (Math, undefined) {
|
|
/*
|
|
* Local polyfil of Object.create
|
|
*/
|
|
var create = Object.create || (function () {
|
|
function F() {};
|
|
|
|
return function (obj) {
|
|
var subtype;
|
|
|
|
F.prototype = obj;
|
|
|
|
subtype = new F();
|
|
|
|
F.prototype = null;
|
|
|
|
return subtype;
|
|
};
|
|
}())
|
|
|
|
/**
|
|
* CryptoJS namespace.
|
|
*/
|
|
var C = {};
|
|
|
|
/**
|
|
* Library namespace.
|
|
*/
|
|
var C_lib = C.lib = {};
|
|
|
|
/**
|
|
* Base object for prototypal inheritance.
|
|
*/
|
|
var Base = C_lib.Base = (function () {
|
|
|
|
|
|
return {
|
|
/**
|
|
* Creates a new object that inherits from this object.
|
|
*
|
|
* @param {Object} overrides Properties to copy into the new object.
|
|
*
|
|
* @return {Object} The new object.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var MyType = CryptoJS.lib.Base.extend({
|
|
* field: 'value',
|
|
*
|
|
* method: function () {
|
|
* }
|
|
* });
|
|
*/
|
|
extend: function (overrides) {
|
|
// Spawn
|
|
var subtype = create(this);
|
|
|
|
// Augment
|
|
if (overrides) {
|
|
subtype.mixIn(overrides);
|
|
}
|
|
|
|
// Create default initializer
|
|
if (!subtype.hasOwnProperty('init') || this.init === subtype.init) {
|
|
subtype.init = function () {
|
|
subtype.$super.init.apply(this, arguments);
|
|
};
|
|
}
|
|
|
|
// Initializer's prototype is the subtype object
|
|
subtype.init.prototype = subtype;
|
|
|
|
// Reference supertype
|
|
subtype.$super = this;
|
|
|
|
return subtype;
|
|
},
|
|
|
|
/**
|
|
* Extends this object and runs the init method.
|
|
* Arguments to create() will be passed to init().
|
|
*
|
|
* @return {Object} The new object.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var instance = MyType.create();
|
|
*/
|
|
create: function () {
|
|
var instance = this.extend();
|
|
instance.init.apply(instance, arguments);
|
|
|
|
return instance;
|
|
},
|
|
|
|
/**
|
|
* Initializes a newly created object.
|
|
* Override this method to add some logic when your objects are created.
|
|
*
|
|
* @example
|
|
*
|
|
* var MyType = CryptoJS.lib.Base.extend({
|
|
* init: function () {
|
|
* // ...
|
|
* }
|
|
* });
|
|
*/
|
|
init: function () {
|
|
},
|
|
|
|
/**
|
|
* Copies properties into this object.
|
|
*
|
|
* @param {Object} properties The properties to mix in.
|
|
*
|
|
* @example
|
|
*
|
|
* MyType.mixIn({
|
|
* field: 'value'
|
|
* });
|
|
*/
|
|
mixIn: function (properties) {
|
|
for (var propertyName in properties) {
|
|
if (properties.hasOwnProperty(propertyName)) {
|
|
this[propertyName] = properties[propertyName];
|
|
}
|
|
}
|
|
|
|
// IE won't copy toString using the loop above
|
|
if (properties.hasOwnProperty('toString')) {
|
|
this.toString = properties.toString;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Creates a copy of this object.
|
|
*
|
|
* @return {Object} The clone.
|
|
*
|
|
* @example
|
|
*
|
|
* var clone = instance.clone();
|
|
*/
|
|
clone: function () {
|
|
return this.init.prototype.extend(this);
|
|
}
|
|
};
|
|
}());
|
|
|
|
/**
|
|
* An array of 32-bit words.
|
|
*
|
|
* @property {Array} words The array of 32-bit words.
|
|
* @property {number} sigBytes The number of significant bytes in this word array.
|
|
*/
|
|
var WordArray = C_lib.WordArray = Base.extend({
|
|
/**
|
|
* Initializes a newly created word array.
|
|
*
|
|
* @param {Array} words (Optional) An array of 32-bit words.
|
|
* @param {number} sigBytes (Optional) The number of significant bytes in the words.
|
|
*
|
|
* @example
|
|
*
|
|
* var wordArray = CryptoJS.lib.WordArray.create();
|
|
* var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]);
|
|
* var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6);
|
|
*/
|
|
init: function (words, sigBytes) {
|
|
words = this.words = words || [];
|
|
|
|
if (sigBytes != undefined) {
|
|
this.sigBytes = sigBytes;
|
|
} else {
|
|
this.sigBytes = words.length * 4;
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Converts this word array to a string.
|
|
*
|
|
* @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
|
|
*
|
|
* @return {string} The stringified word array.
|
|
*
|
|
* @example
|
|
*
|
|
* var string = wordArray + '';
|
|
* var string = wordArray.toString();
|
|
* var string = wordArray.toString(CryptoJS.enc.Utf8);
|
|
*/
|
|
toString: function (encoder) {
|
|
return (encoder || Hex).stringify(this);
|
|
},
|
|
|
|
/**
|
|
* Concatenates a word array to this word array.
|
|
*
|
|
* @param {WordArray} wordArray The word array to append.
|
|
*
|
|
* @return {WordArray} This word array.
|
|
*
|
|
* @example
|
|
*
|
|
* wordArray1.concat(wordArray2);
|
|
*/
|
|
concat: function (wordArray) {
|
|
// Shortcuts
|
|
var thisWords = this.words;
|
|
var thatWords = wordArray.words;
|
|
var thisSigBytes = this.sigBytes;
|
|
var thatSigBytes = wordArray.sigBytes;
|
|
|
|
// Clamp excess bits
|
|
this.clamp();
|
|
|
|
// Concat
|
|
if (thisSigBytes % 4) {
|
|
// Copy one byte at a time
|
|
for (var i = 0; i < thatSigBytes; i++) {
|
|
var thatByte = (thatWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
|
thisWords[(thisSigBytes + i) >>> 2] |= thatByte << (24 - ((thisSigBytes + i) % 4) * 8);
|
|
}
|
|
} else {
|
|
// Copy one word at a time
|
|
for (var i = 0; i < thatSigBytes; i += 4) {
|
|
thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2];
|
|
}
|
|
}
|
|
this.sigBytes += thatSigBytes;
|
|
|
|
// Chainable
|
|
return this;
|
|
},
|
|
|
|
/**
|
|
* Removes insignificant bits.
|
|
*
|
|
* @example
|
|
*
|
|
* wordArray.clamp();
|
|
*/
|
|
clamp: function () {
|
|
// Shortcuts
|
|
var words = this.words;
|
|
var sigBytes = this.sigBytes;
|
|
|
|
// Clamp
|
|
words[sigBytes >>> 2] &= 0xffffffff << (32 - (sigBytes % 4) * 8);
|
|
words.length = Math.ceil(sigBytes / 4);
|
|
},
|
|
|
|
/**
|
|
* Creates a copy of this word array.
|
|
*
|
|
* @return {WordArray} The clone.
|
|
*
|
|
* @example
|
|
*
|
|
* var clone = wordArray.clone();
|
|
*/
|
|
clone: function () {
|
|
var clone = Base.clone.call(this);
|
|
clone.words = this.words.slice(0);
|
|
|
|
return clone;
|
|
},
|
|
|
|
/**
|
|
* Creates a word array filled with random bytes.
|
|
*
|
|
* @param {number} nBytes The number of random bytes to generate.
|
|
*
|
|
* @return {WordArray} The random word array.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var wordArray = CryptoJS.lib.WordArray.random(16);
|
|
*/
|
|
random: function (nBytes) {
|
|
var words = [];
|
|
|
|
var r = (function (m_w) {
|
|
var m_w = m_w;
|
|
var m_z = 0x3ade68b1;
|
|
var mask = 0xffffffff;
|
|
|
|
return function () {
|
|
m_z = (0x9069 * (m_z & 0xFFFF) + (m_z >> 0x10)) & mask;
|
|
m_w = (0x4650 * (m_w & 0xFFFF) + (m_w >> 0x10)) & mask;
|
|
var result = ((m_z << 0x10) + m_w) & mask;
|
|
result /= 0x100000000;
|
|
result += 0.5;
|
|
return result * (Math.random() > .5 ? 1 : -1);
|
|
}
|
|
});
|
|
|
|
for (var i = 0, rcache; i < nBytes; i += 4) {
|
|
var _r = r((rcache || Math.random()) * 0x100000000);
|
|
|
|
rcache = _r() * 0x3ade67b7;
|
|
words.push((_r() * 0x100000000) | 0);
|
|
}
|
|
|
|
return new WordArray.init(words, nBytes);
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Encoder namespace.
|
|
*/
|
|
var C_enc = C.enc = {};
|
|
|
|
/**
|
|
* Hex encoding strategy.
|
|
*/
|
|
var Hex = C_enc.Hex = {
|
|
/**
|
|
* Converts a word array to a hex string.
|
|
*
|
|
* @param {WordArray} wordArray The word array.
|
|
*
|
|
* @return {string} The hex string.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var hexString = CryptoJS.enc.Hex.stringify(wordArray);
|
|
*/
|
|
stringify: function (wordArray) {
|
|
// Shortcuts
|
|
var words = wordArray.words;
|
|
var sigBytes = wordArray.sigBytes;
|
|
|
|
// Convert
|
|
var hexChars = [];
|
|
for (var i = 0; i < sigBytes; i++) {
|
|
var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
|
hexChars.push((bite >>> 4).toString(16));
|
|
hexChars.push((bite & 0x0f).toString(16));
|
|
}
|
|
|
|
return hexChars.join('');
|
|
},
|
|
|
|
/**
|
|
* Converts a hex string to a word array.
|
|
*
|
|
* @param {string} hexStr The hex string.
|
|
*
|
|
* @return {WordArray} The word array.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var wordArray = CryptoJS.enc.Hex.parse(hexString);
|
|
*/
|
|
parse: function (hexStr) {
|
|
// Shortcut
|
|
var hexStrLength = hexStr.length;
|
|
|
|
// Convert
|
|
var words = [];
|
|
for (var i = 0; i < hexStrLength; i += 2) {
|
|
words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4);
|
|
}
|
|
|
|
return new WordArray.init(words, hexStrLength / 2);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Latin1 encoding strategy.
|
|
*/
|
|
var Latin1 = C_enc.Latin1 = {
|
|
/**
|
|
* Converts a word array to a Latin1 string.
|
|
*
|
|
* @param {WordArray} wordArray The word array.
|
|
*
|
|
* @return {string} The Latin1 string.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var latin1String = CryptoJS.enc.Latin1.stringify(wordArray);
|
|
*/
|
|
stringify: function (wordArray) {
|
|
// Shortcuts
|
|
var words = wordArray.words;
|
|
var sigBytes = wordArray.sigBytes;
|
|
|
|
// Convert
|
|
var latin1Chars = [];
|
|
for (var i = 0; i < sigBytes; i++) {
|
|
var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
|
latin1Chars.push(String.fromCharCode(bite));
|
|
}
|
|
|
|
return latin1Chars.join('');
|
|
},
|
|
|
|
/**
|
|
* Converts a Latin1 string to a word array.
|
|
*
|
|
* @param {string} latin1Str The Latin1 string.
|
|
*
|
|
* @return {WordArray} The word array.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var wordArray = CryptoJS.enc.Latin1.parse(latin1String);
|
|
*/
|
|
parse: function (latin1Str) {
|
|
// Shortcut
|
|
var latin1StrLength = latin1Str.length;
|
|
|
|
// Convert
|
|
var words = [];
|
|
for (var i = 0; i < latin1StrLength; i++) {
|
|
words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
|
|
}
|
|
|
|
return new WordArray.init(words, latin1StrLength);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* UTF-8 encoding strategy.
|
|
*/
|
|
var Utf8 = C_enc.Utf8 = {
|
|
/**
|
|
* Converts a word array to a UTF-8 string.
|
|
*
|
|
* @param {WordArray} wordArray The word array.
|
|
*
|
|
* @return {string} The UTF-8 string.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var utf8String = CryptoJS.enc.Utf8.stringify(wordArray);
|
|
*/
|
|
stringify: function (wordArray) {
|
|
try {
|
|
return decodeURIComponent(escape(Latin1.stringify(wordArray)));
|
|
} catch (e) {
|
|
throw new Error('Malformed UTF-8 data');
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Converts a UTF-8 string to a word array.
|
|
*
|
|
* @param {string} utf8Str The UTF-8 string.
|
|
*
|
|
* @return {WordArray} The word array.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var wordArray = CryptoJS.enc.Utf8.parse(utf8String);
|
|
*/
|
|
parse: function (utf8Str) {
|
|
return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Abstract buffered block algorithm template.
|
|
*
|
|
* The property blockSize must be implemented in a concrete subtype.
|
|
*
|
|
* @property {number} _minBufferSize The number of blocks that should be kept unprocessed in the buffer. Default: 0
|
|
*/
|
|
var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm = Base.extend({
|
|
/**
|
|
* Resets this block algorithm's data buffer to its initial state.
|
|
*
|
|
* @example
|
|
*
|
|
* bufferedBlockAlgorithm.reset();
|
|
*/
|
|
reset: function () {
|
|
// Initial values
|
|
this._data = new WordArray.init();
|
|
this._nDataBytes = 0;
|
|
},
|
|
|
|
/**
|
|
* Adds new data to this block algorithm's buffer.
|
|
*
|
|
* @param {WordArray|string} data The data to append. Strings are converted to a WordArray using UTF-8.
|
|
*
|
|
* @example
|
|
*
|
|
* bufferedBlockAlgorithm._append('data');
|
|
* bufferedBlockAlgorithm._append(wordArray);
|
|
*/
|
|
_append: function (data) {
|
|
// Convert string to WordArray, else assume WordArray already
|
|
if (typeof data == 'string') {
|
|
data = Utf8.parse(data);
|
|
}
|
|
|
|
// Append
|
|
this._data.concat(data);
|
|
this._nDataBytes += data.sigBytes;
|
|
},
|
|
|
|
/**
|
|
* Processes available data blocks.
|
|
*
|
|
* This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
|
|
*
|
|
* @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
|
|
*
|
|
* @return {WordArray} The processed data.
|
|
*
|
|
* @example
|
|
*
|
|
* var processedData = bufferedBlockAlgorithm._process();
|
|
* var processedData = bufferedBlockAlgorithm._process(!!'flush');
|
|
*/
|
|
_process: function (doFlush) {
|
|
// Shortcuts
|
|
var data = this._data;
|
|
var dataWords = data.words;
|
|
var dataSigBytes = data.sigBytes;
|
|
var blockSize = this.blockSize;
|
|
var blockSizeBytes = blockSize * 4;
|
|
|
|
// Count blocks ready
|
|
var nBlocksReady = dataSigBytes / blockSizeBytes;
|
|
if (doFlush) {
|
|
// Round up to include partial blocks
|
|
nBlocksReady = Math.ceil(nBlocksReady);
|
|
} else {
|
|
// Round down to include only full blocks,
|
|
// less the number of blocks that must remain in the buffer
|
|
nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
|
|
}
|
|
|
|
// Count words ready
|
|
var nWordsReady = nBlocksReady * blockSize;
|
|
|
|
// Count bytes ready
|
|
var nBytesReady = Math.min(nWordsReady * 4, dataSigBytes);
|
|
|
|
// Process blocks
|
|
if (nWordsReady) {
|
|
for (var offset = 0; offset < nWordsReady; offset += blockSize) {
|
|
// Perform concrete-algorithm logic
|
|
this._doProcessBlock(dataWords, offset);
|
|
}
|
|
|
|
// Remove processed words
|
|
var processedWords = dataWords.splice(0, nWordsReady);
|
|
data.sigBytes -= nBytesReady;
|
|
}
|
|
|
|
// Return processed words
|
|
return new WordArray.init(processedWords, nBytesReady);
|
|
},
|
|
|
|
/**
|
|
* Creates a copy of this object.
|
|
*
|
|
* @return {Object} The clone.
|
|
*
|
|
* @example
|
|
*
|
|
* var clone = bufferedBlockAlgorithm.clone();
|
|
*/
|
|
clone: function () {
|
|
var clone = Base.clone.call(this);
|
|
clone._data = this._data.clone();
|
|
|
|
return clone;
|
|
},
|
|
|
|
_minBufferSize: 0
|
|
});
|
|
|
|
/**
|
|
* Abstract hasher template.
|
|
*
|
|
* @property {number} blockSize The number of 32-bit words this hasher operates on. Default: 16 (512 bits)
|
|
*/
|
|
var Hasher = C_lib.Hasher = BufferedBlockAlgorithm.extend({
|
|
/**
|
|
* Configuration options.
|
|
*/
|
|
cfg: Base.extend(),
|
|
|
|
/**
|
|
* Initializes a newly created hasher.
|
|
*
|
|
* @param {Object} cfg (Optional) The configuration options to use for this hash computation.
|
|
*
|
|
* @example
|
|
*
|
|
* var hasher = CryptoJS.algo.SHA256.create();
|
|
*/
|
|
init: function (cfg) {
|
|
// Apply config defaults
|
|
this.cfg = this.cfg.extend(cfg);
|
|
|
|
// Set initial values
|
|
this.reset();
|
|
},
|
|
|
|
/**
|
|
* Resets this hasher to its initial state.
|
|
*
|
|
* @example
|
|
*
|
|
* hasher.reset();
|
|
*/
|
|
reset: function () {
|
|
// Reset data buffer
|
|
BufferedBlockAlgorithm.reset.call(this);
|
|
|
|
// Perform concrete-hasher logic
|
|
this._doReset();
|
|
},
|
|
|
|
/**
|
|
* Updates this hasher with a message.
|
|
*
|
|
* @param {WordArray|string} messageUpdate The message to append.
|
|
*
|
|
* @return {Hasher} This hasher.
|
|
*
|
|
* @example
|
|
*
|
|
* hasher.update('message');
|
|
* hasher.update(wordArray);
|
|
*/
|
|
update: function (messageUpdate) {
|
|
// Append
|
|
this._append(messageUpdate);
|
|
|
|
// Update the hash
|
|
this._process();
|
|
|
|
// Chainable
|
|
return this;
|
|
},
|
|
|
|
/**
|
|
* Finalizes the hash computation.
|
|
* Note that the finalize operation is effectively a destructive, read-once operation.
|
|
*
|
|
* @param {WordArray|string} messageUpdate (Optional) A final message update.
|
|
*
|
|
* @return {WordArray} The hash.
|
|
*
|
|
* @example
|
|
*
|
|
* var hash = hasher.finalize();
|
|
* var hash = hasher.finalize('message');
|
|
* var hash = hasher.finalize(wordArray);
|
|
*/
|
|
finalize: function (messageUpdate) {
|
|
// Final message update
|
|
if (messageUpdate) {
|
|
this._append(messageUpdate);
|
|
}
|
|
|
|
// Perform concrete-hasher logic
|
|
var hash = this._doFinalize();
|
|
|
|
return hash;
|
|
},
|
|
|
|
blockSize: 512/32,
|
|
|
|
/**
|
|
* Creates a shortcut function to a hasher's object interface.
|
|
*
|
|
* @param {Hasher} hasher The hasher to create a helper for.
|
|
*
|
|
* @return {Function} The shortcut function.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256);
|
|
*/
|
|
_createHelper: function (hasher) {
|
|
return function (message, cfg) {
|
|
return new hasher.init(cfg).finalize(message);
|
|
};
|
|
},
|
|
|
|
/**
|
|
* Creates a shortcut function to the HMAC's object interface.
|
|
*
|
|
* @param {Hasher} hasher The hasher to use in this HMAC helper.
|
|
*
|
|
* @return {Function} The shortcut function.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256);
|
|
*/
|
|
_createHmacHelper: function (hasher) {
|
|
return function (message, key) {
|
|
return new C_algo.HMAC.init(hasher, key).finalize(message);
|
|
};
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Algorithm namespace.
|
|
*/
|
|
var C_algo = C.algo = {};
|
|
|
|
return C;
|
|
}(Math));
|
|
|
|
|
|
return CryptoJS;
|
|
|
|
}));
|
|
},{}],19:[function(require,module,exports){
|
|
;(function (root, factory) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
(function () {
|
|
// Shortcuts
|
|
var C = CryptoJS;
|
|
var C_lib = C.lib;
|
|
var WordArray = C_lib.WordArray;
|
|
var C_enc = C.enc;
|
|
|
|
/**
|
|
* Base64 encoding strategy.
|
|
*/
|
|
var Base64 = C_enc.Base64 = {
|
|
/**
|
|
* Converts a word array to a Base64 string.
|
|
*
|
|
* @param {WordArray} wordArray The word array.
|
|
*
|
|
* @return {string} The Base64 string.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var base64String = CryptoJS.enc.Base64.stringify(wordArray);
|
|
*/
|
|
stringify: function (wordArray) {
|
|
// Shortcuts
|
|
var words = wordArray.words;
|
|
var sigBytes = wordArray.sigBytes;
|
|
var map = this._map;
|
|
|
|
// Clamp excess bits
|
|
wordArray.clamp();
|
|
|
|
// Convert
|
|
var base64Chars = [];
|
|
for (var i = 0; i < sigBytes; i += 3) {
|
|
var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
|
var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
|
|
var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
|
|
|
|
var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
|
|
|
|
for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
|
|
base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
|
|
}
|
|
}
|
|
|
|
// Add padding
|
|
var paddingChar = map.charAt(64);
|
|
if (paddingChar) {
|
|
while (base64Chars.length % 4) {
|
|
base64Chars.push(paddingChar);
|
|
}
|
|
}
|
|
|
|
return base64Chars.join('');
|
|
},
|
|
|
|
/**
|
|
* Converts a Base64 string to a word array.
|
|
*
|
|
* @param {string} base64Str The Base64 string.
|
|
*
|
|
* @return {WordArray} The word array.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var wordArray = CryptoJS.enc.Base64.parse(base64String);
|
|
*/
|
|
parse: function (base64Str) {
|
|
// Shortcuts
|
|
var base64StrLength = base64Str.length;
|
|
var map = this._map;
|
|
var reverseMap = this._reverseMap;
|
|
|
|
if (!reverseMap) {
|
|
reverseMap = this._reverseMap = [];
|
|
for (var j = 0; j < map.length; j++) {
|
|
reverseMap[map.charCodeAt(j)] = j;
|
|
}
|
|
}
|
|
|
|
// Ignore padding
|
|
var paddingChar = map.charAt(64);
|
|
if (paddingChar) {
|
|
var paddingIndex = base64Str.indexOf(paddingChar);
|
|
if (paddingIndex !== -1) {
|
|
base64StrLength = paddingIndex;
|
|
}
|
|
}
|
|
|
|
// Convert
|
|
return parseLoop(base64Str, base64StrLength, reverseMap);
|
|
|
|
},
|
|
|
|
_map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
|
|
};
|
|
|
|
function parseLoop(base64Str, base64StrLength, reverseMap) {
|
|
var words = [];
|
|
var nBytes = 0;
|
|
for (var i = 0; i < base64StrLength; i++) {
|
|
if (i % 4) {
|
|
var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2);
|
|
var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2);
|
|
words[nBytes >>> 2] |= (bits1 | bits2) << (24 - (nBytes % 4) * 8);
|
|
nBytes++;
|
|
}
|
|
}
|
|
return WordArray.create(words, nBytes);
|
|
}
|
|
}());
|
|
|
|
|
|
return CryptoJS.enc.Base64;
|
|
|
|
}));
|
|
},{"./core":18}],20:[function(require,module,exports){
|
|
;(function (root, factory) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
return CryptoJS.enc.Hex;
|
|
|
|
}));
|
|
},{"./core":18}],21:[function(require,module,exports){
|
|
;(function (root, factory) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
return CryptoJS.enc.Latin1;
|
|
|
|
}));
|
|
},{"./core":18}],22:[function(require,module,exports){
|
|
;(function (root, factory) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
return CryptoJS.enc.Utf8;
|
|
|
|
}));
|
|
},{"./core":18}],23:[function(require,module,exports){
|
|
;(function (root, factory, undef) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"), require("./sha1"), require("./hmac"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core", "./sha1", "./hmac"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
(function () {
|
|
// Shortcuts
|
|
var C = CryptoJS;
|
|
var C_lib = C.lib;
|
|
var Base = C_lib.Base;
|
|
var WordArray = C_lib.WordArray;
|
|
var C_algo = C.algo;
|
|
var MD5 = C_algo.MD5;
|
|
|
|
/**
|
|
* This key derivation function is meant to conform with EVP_BytesToKey.
|
|
* www.openssl.org/docs/crypto/EVP_BytesToKey.html
|
|
*/
|
|
var EvpKDF = C_algo.EvpKDF = Base.extend({
|
|
/**
|
|
* Configuration options.
|
|
*
|
|
* @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
|
|
* @property {Hasher} hasher The hash algorithm to use. Default: MD5
|
|
* @property {number} iterations The number of iterations to perform. Default: 1
|
|
*/
|
|
cfg: Base.extend({
|
|
keySize: 128/32,
|
|
hasher: MD5,
|
|
iterations: 1
|
|
}),
|
|
|
|
/**
|
|
* Initializes a newly created key derivation function.
|
|
*
|
|
* @param {Object} cfg (Optional) The configuration options to use for the derivation.
|
|
*
|
|
* @example
|
|
*
|
|
* var kdf = CryptoJS.algo.EvpKDF.create();
|
|
* var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8 });
|
|
* var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8, iterations: 1000 });
|
|
*/
|
|
init: function (cfg) {
|
|
this.cfg = this.cfg.extend(cfg);
|
|
},
|
|
|
|
/**
|
|
* Derives a key from a password.
|
|
*
|
|
* @param {WordArray|string} password The password.
|
|
* @param {WordArray|string} salt A salt.
|
|
*
|
|
* @return {WordArray} The derived key.
|
|
*
|
|
* @example
|
|
*
|
|
* var key = kdf.compute(password, salt);
|
|
*/
|
|
compute: function (password, salt) {
|
|
// Shortcut
|
|
var cfg = this.cfg;
|
|
|
|
// Init hasher
|
|
var hasher = cfg.hasher.create();
|
|
|
|
// Initial values
|
|
var derivedKey = WordArray.create();
|
|
|
|
// Shortcuts
|
|
var derivedKeyWords = derivedKey.words;
|
|
var keySize = cfg.keySize;
|
|
var iterations = cfg.iterations;
|
|
|
|
// Generate key
|
|
while (derivedKeyWords.length < keySize) {
|
|
if (block) {
|
|
hasher.update(block);
|
|
}
|
|
var block = hasher.update(password).finalize(salt);
|
|
hasher.reset();
|
|
|
|
// Iterations
|
|
for (var i = 1; i < iterations; i++) {
|
|
block = hasher.finalize(block);
|
|
hasher.reset();
|
|
}
|
|
|
|
derivedKey.concat(block);
|
|
}
|
|
derivedKey.sigBytes = keySize * 4;
|
|
|
|
return derivedKey;
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Derives a key from a password.
|
|
*
|
|
* @param {WordArray|string} password The password.
|
|
* @param {WordArray|string} salt A salt.
|
|
* @param {Object} cfg (Optional) The configuration options to use for this computation.
|
|
*
|
|
* @return {WordArray} The derived key.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var key = CryptoJS.EvpKDF(password, salt);
|
|
* var key = CryptoJS.EvpKDF(password, salt, { keySize: 8 });
|
|
* var key = CryptoJS.EvpKDF(password, salt, { keySize: 8, iterations: 1000 });
|
|
*/
|
|
C.EvpKDF = function (password, salt, cfg) {
|
|
return EvpKDF.create(cfg).compute(password, salt);
|
|
};
|
|
}());
|
|
|
|
|
|
return CryptoJS.EvpKDF;
|
|
|
|
}));
|
|
},{"./core":18,"./hmac":25,"./sha1":27}],24:[function(require,module,exports){
|
|
;(function (root, factory, undef) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"), require("./sha256"), require("./hmac"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core", "./sha256", "./hmac"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
return CryptoJS.HmacSHA256;
|
|
|
|
}));
|
|
},{"./core":18,"./hmac":25,"./sha256":28}],25:[function(require,module,exports){
|
|
;(function (root, factory) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
(function () {
|
|
// Shortcuts
|
|
var C = CryptoJS;
|
|
var C_lib = C.lib;
|
|
var Base = C_lib.Base;
|
|
var C_enc = C.enc;
|
|
var Utf8 = C_enc.Utf8;
|
|
var C_algo = C.algo;
|
|
|
|
/**
|
|
* HMAC algorithm.
|
|
*/
|
|
var HMAC = C_algo.HMAC = Base.extend({
|
|
/**
|
|
* Initializes a newly created HMAC.
|
|
*
|
|
* @param {Hasher} hasher The hash algorithm to use.
|
|
* @param {WordArray|string} key The secret key.
|
|
*
|
|
* @example
|
|
*
|
|
* var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
|
|
*/
|
|
init: function (hasher, key) {
|
|
// Init hasher
|
|
hasher = this._hasher = new hasher.init();
|
|
|
|
// Convert string to WordArray, else assume WordArray already
|
|
if (typeof key == 'string') {
|
|
key = Utf8.parse(key);
|
|
}
|
|
|
|
// Shortcuts
|
|
var hasherBlockSize = hasher.blockSize;
|
|
var hasherBlockSizeBytes = hasherBlockSize * 4;
|
|
|
|
// Allow arbitrary length keys
|
|
if (key.sigBytes > hasherBlockSizeBytes) {
|
|
key = hasher.finalize(key);
|
|
}
|
|
|
|
// Clamp excess bits
|
|
key.clamp();
|
|
|
|
// Clone key for inner and outer pads
|
|
var oKey = this._oKey = key.clone();
|
|
var iKey = this._iKey = key.clone();
|
|
|
|
// Shortcuts
|
|
var oKeyWords = oKey.words;
|
|
var iKeyWords = iKey.words;
|
|
|
|
// XOR keys with pad constants
|
|
for (var i = 0; i < hasherBlockSize; i++) {
|
|
oKeyWords[i] ^= 0x5c5c5c5c;
|
|
iKeyWords[i] ^= 0x36363636;
|
|
}
|
|
oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes;
|
|
|
|
// Set initial values
|
|
this.reset();
|
|
},
|
|
|
|
/**
|
|
* Resets this HMAC to its initial state.
|
|
*
|
|
* @example
|
|
*
|
|
* hmacHasher.reset();
|
|
*/
|
|
reset: function () {
|
|
// Shortcut
|
|
var hasher = this._hasher;
|
|
|
|
// Reset
|
|
hasher.reset();
|
|
hasher.update(this._iKey);
|
|
},
|
|
|
|
/**
|
|
* Updates this HMAC with a message.
|
|
*
|
|
* @param {WordArray|string} messageUpdate The message to append.
|
|
*
|
|
* @return {HMAC} This HMAC instance.
|
|
*
|
|
* @example
|
|
*
|
|
* hmacHasher.update('message');
|
|
* hmacHasher.update(wordArray);
|
|
*/
|
|
update: function (messageUpdate) {
|
|
this._hasher.update(messageUpdate);
|
|
|
|
// Chainable
|
|
return this;
|
|
},
|
|
|
|
/**
|
|
* Finalizes the HMAC computation.
|
|
* Note that the finalize operation is effectively a destructive, read-once operation.
|
|
*
|
|
* @param {WordArray|string} messageUpdate (Optional) A final message update.
|
|
*
|
|
* @return {WordArray} The HMAC.
|
|
*
|
|
* @example
|
|
*
|
|
* var hmac = hmacHasher.finalize();
|
|
* var hmac = hmacHasher.finalize('message');
|
|
* var hmac = hmacHasher.finalize(wordArray);
|
|
*/
|
|
finalize: function (messageUpdate) {
|
|
// Shortcut
|
|
var hasher = this._hasher;
|
|
|
|
// Compute HMAC
|
|
var innerHash = hasher.finalize(messageUpdate);
|
|
hasher.reset();
|
|
var hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
|
|
|
|
return hmac;
|
|
}
|
|
});
|
|
}());
|
|
|
|
|
|
}));
|
|
},{"./core":18}],26:[function(require,module,exports){
|
|
;(function (root, factory) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
(function (Math) {
|
|
// Shortcuts
|
|
var C = CryptoJS;
|
|
var C_lib = C.lib;
|
|
var WordArray = C_lib.WordArray;
|
|
var Hasher = C_lib.Hasher;
|
|
var C_algo = C.algo;
|
|
|
|
// Constants table
|
|
var T = [];
|
|
|
|
// Compute constants
|
|
(function () {
|
|
for (var i = 0; i < 64; i++) {
|
|
T[i] = (Math.abs(Math.sin(i + 1)) * 0x100000000) | 0;
|
|
}
|
|
}());
|
|
|
|
/**
|
|
* MD5 hash algorithm.
|
|
*/
|
|
var MD5 = C_algo.MD5 = Hasher.extend({
|
|
_doReset: function () {
|
|
this._hash = new WordArray.init([
|
|
0x67452301, 0xefcdab89,
|
|
0x98badcfe, 0x10325476
|
|
]);
|
|
},
|
|
|
|
_doProcessBlock: function (M, offset) {
|
|
// Swap endian
|
|
for (var i = 0; i < 16; i++) {
|
|
// Shortcuts
|
|
var offset_i = offset + i;
|
|
var M_offset_i = M[offset_i];
|
|
|
|
M[offset_i] = (
|
|
(((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
|
|
(((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
|
|
);
|
|
}
|
|
|
|
// Shortcuts
|
|
var H = this._hash.words;
|
|
|
|
var M_offset_0 = M[offset + 0];
|
|
var M_offset_1 = M[offset + 1];
|
|
var M_offset_2 = M[offset + 2];
|
|
var M_offset_3 = M[offset + 3];
|
|
var M_offset_4 = M[offset + 4];
|
|
var M_offset_5 = M[offset + 5];
|
|
var M_offset_6 = M[offset + 6];
|
|
var M_offset_7 = M[offset + 7];
|
|
var M_offset_8 = M[offset + 8];
|
|
var M_offset_9 = M[offset + 9];
|
|
var M_offset_10 = M[offset + 10];
|
|
var M_offset_11 = M[offset + 11];
|
|
var M_offset_12 = M[offset + 12];
|
|
var M_offset_13 = M[offset + 13];
|
|
var M_offset_14 = M[offset + 14];
|
|
var M_offset_15 = M[offset + 15];
|
|
|
|
// Working varialbes
|
|
var a = H[0];
|
|
var b = H[1];
|
|
var c = H[2];
|
|
var d = H[3];
|
|
|
|
// Computation
|
|
a = FF(a, b, c, d, M_offset_0, 7, T[0]);
|
|
d = FF(d, a, b, c, M_offset_1, 12, T[1]);
|
|
c = FF(c, d, a, b, M_offset_2, 17, T[2]);
|
|
b = FF(b, c, d, a, M_offset_3, 22, T[3]);
|
|
a = FF(a, b, c, d, M_offset_4, 7, T[4]);
|
|
d = FF(d, a, b, c, M_offset_5, 12, T[5]);
|
|
c = FF(c, d, a, b, M_offset_6, 17, T[6]);
|
|
b = FF(b, c, d, a, M_offset_7, 22, T[7]);
|
|
a = FF(a, b, c, d, M_offset_8, 7, T[8]);
|
|
d = FF(d, a, b, c, M_offset_9, 12, T[9]);
|
|
c = FF(c, d, a, b, M_offset_10, 17, T[10]);
|
|
b = FF(b, c, d, a, M_offset_11, 22, T[11]);
|
|
a = FF(a, b, c, d, M_offset_12, 7, T[12]);
|
|
d = FF(d, a, b, c, M_offset_13, 12, T[13]);
|
|
c = FF(c, d, a, b, M_offset_14, 17, T[14]);
|
|
b = FF(b, c, d, a, M_offset_15, 22, T[15]);
|
|
|
|
a = GG(a, b, c, d, M_offset_1, 5, T[16]);
|
|
d = GG(d, a, b, c, M_offset_6, 9, T[17]);
|
|
c = GG(c, d, a, b, M_offset_11, 14, T[18]);
|
|
b = GG(b, c, d, a, M_offset_0, 20, T[19]);
|
|
a = GG(a, b, c, d, M_offset_5, 5, T[20]);
|
|
d = GG(d, a, b, c, M_offset_10, 9, T[21]);
|
|
c = GG(c, d, a, b, M_offset_15, 14, T[22]);
|
|
b = GG(b, c, d, a, M_offset_4, 20, T[23]);
|
|
a = GG(a, b, c, d, M_offset_9, 5, T[24]);
|
|
d = GG(d, a, b, c, M_offset_14, 9, T[25]);
|
|
c = GG(c, d, a, b, M_offset_3, 14, T[26]);
|
|
b = GG(b, c, d, a, M_offset_8, 20, T[27]);
|
|
a = GG(a, b, c, d, M_offset_13, 5, T[28]);
|
|
d = GG(d, a, b, c, M_offset_2, 9, T[29]);
|
|
c = GG(c, d, a, b, M_offset_7, 14, T[30]);
|
|
b = GG(b, c, d, a, M_offset_12, 20, T[31]);
|
|
|
|
a = HH(a, b, c, d, M_offset_5, 4, T[32]);
|
|
d = HH(d, a, b, c, M_offset_8, 11, T[33]);
|
|
c = HH(c, d, a, b, M_offset_11, 16, T[34]);
|
|
b = HH(b, c, d, a, M_offset_14, 23, T[35]);
|
|
a = HH(a, b, c, d, M_offset_1, 4, T[36]);
|
|
d = HH(d, a, b, c, M_offset_4, 11, T[37]);
|
|
c = HH(c, d, a, b, M_offset_7, 16, T[38]);
|
|
b = HH(b, c, d, a, M_offset_10, 23, T[39]);
|
|
a = HH(a, b, c, d, M_offset_13, 4, T[40]);
|
|
d = HH(d, a, b, c, M_offset_0, 11, T[41]);
|
|
c = HH(c, d, a, b, M_offset_3, 16, T[42]);
|
|
b = HH(b, c, d, a, M_offset_6, 23, T[43]);
|
|
a = HH(a, b, c, d, M_offset_9, 4, T[44]);
|
|
d = HH(d, a, b, c, M_offset_12, 11, T[45]);
|
|
c = HH(c, d, a, b, M_offset_15, 16, T[46]);
|
|
b = HH(b, c, d, a, M_offset_2, 23, T[47]);
|
|
|
|
a = II(a, b, c, d, M_offset_0, 6, T[48]);
|
|
d = II(d, a, b, c, M_offset_7, 10, T[49]);
|
|
c = II(c, d, a, b, M_offset_14, 15, T[50]);
|
|
b = II(b, c, d, a, M_offset_5, 21, T[51]);
|
|
a = II(a, b, c, d, M_offset_12, 6, T[52]);
|
|
d = II(d, a, b, c, M_offset_3, 10, T[53]);
|
|
c = II(c, d, a, b, M_offset_10, 15, T[54]);
|
|
b = II(b, c, d, a, M_offset_1, 21, T[55]);
|
|
a = II(a, b, c, d, M_offset_8, 6, T[56]);
|
|
d = II(d, a, b, c, M_offset_15, 10, T[57]);
|
|
c = II(c, d, a, b, M_offset_6, 15, T[58]);
|
|
b = II(b, c, d, a, M_offset_13, 21, T[59]);
|
|
a = II(a, b, c, d, M_offset_4, 6, T[60]);
|
|
d = II(d, a, b, c, M_offset_11, 10, T[61]);
|
|
c = II(c, d, a, b, M_offset_2, 15, T[62]);
|
|
b = II(b, c, d, a, M_offset_9, 21, T[63]);
|
|
|
|
// Intermediate hash value
|
|
H[0] = (H[0] + a) | 0;
|
|
H[1] = (H[1] + b) | 0;
|
|
H[2] = (H[2] + c) | 0;
|
|
H[3] = (H[3] + d) | 0;
|
|
},
|
|
|
|
_doFinalize: function () {
|
|
// Shortcuts
|
|
var data = this._data;
|
|
var dataWords = data.words;
|
|
|
|
var nBitsTotal = this._nDataBytes * 8;
|
|
var nBitsLeft = data.sigBytes * 8;
|
|
|
|
// Add padding
|
|
dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
|
|
|
|
var nBitsTotalH = Math.floor(nBitsTotal / 0x100000000);
|
|
var nBitsTotalL = nBitsTotal;
|
|
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = (
|
|
(((nBitsTotalH << 8) | (nBitsTotalH >>> 24)) & 0x00ff00ff) |
|
|
(((nBitsTotalH << 24) | (nBitsTotalH >>> 8)) & 0xff00ff00)
|
|
);
|
|
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
|
|
(((nBitsTotalL << 8) | (nBitsTotalL >>> 24)) & 0x00ff00ff) |
|
|
(((nBitsTotalL << 24) | (nBitsTotalL >>> 8)) & 0xff00ff00)
|
|
);
|
|
|
|
data.sigBytes = (dataWords.length + 1) * 4;
|
|
|
|
// Hash final blocks
|
|
this._process();
|
|
|
|
// Shortcuts
|
|
var hash = this._hash;
|
|
var H = hash.words;
|
|
|
|
// Swap endian
|
|
for (var i = 0; i < 4; i++) {
|
|
// Shortcut
|
|
var H_i = H[i];
|
|
|
|
H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
|
|
(((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
|
|
}
|
|
|
|
// Return final computed hash
|
|
return hash;
|
|
},
|
|
|
|
clone: function () {
|
|
var clone = Hasher.clone.call(this);
|
|
clone._hash = this._hash.clone();
|
|
|
|
return clone;
|
|
}
|
|
});
|
|
|
|
function FF(a, b, c, d, x, s, t) {
|
|
var n = a + ((b & c) | (~b & d)) + x + t;
|
|
return ((n << s) | (n >>> (32 - s))) + b;
|
|
}
|
|
|
|
function GG(a, b, c, d, x, s, t) {
|
|
var n = a + ((b & d) | (c & ~d)) + x + t;
|
|
return ((n << s) | (n >>> (32 - s))) + b;
|
|
}
|
|
|
|
function HH(a, b, c, d, x, s, t) {
|
|
var n = a + (b ^ c ^ d) + x + t;
|
|
return ((n << s) | (n >>> (32 - s))) + b;
|
|
}
|
|
|
|
function II(a, b, c, d, x, s, t) {
|
|
var n = a + (c ^ (b | ~d)) + x + t;
|
|
return ((n << s) | (n >>> (32 - s))) + b;
|
|
}
|
|
|
|
/**
|
|
* Shortcut function to the hasher's object interface.
|
|
*
|
|
* @param {WordArray|string} message The message to hash.
|
|
*
|
|
* @return {WordArray} The hash.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var hash = CryptoJS.MD5('message');
|
|
* var hash = CryptoJS.MD5(wordArray);
|
|
*/
|
|
C.MD5 = Hasher._createHelper(MD5);
|
|
|
|
/**
|
|
* Shortcut function to the HMAC's object interface.
|
|
*
|
|
* @param {WordArray|string} message The message to hash.
|
|
* @param {WordArray|string} key The secret key.
|
|
*
|
|
* @return {WordArray} The HMAC.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var hmac = CryptoJS.HmacMD5(message, key);
|
|
*/
|
|
C.HmacMD5 = Hasher._createHmacHelper(MD5);
|
|
}(Math));
|
|
|
|
|
|
return CryptoJS.MD5;
|
|
|
|
}));
|
|
},{"./core":18}],27:[function(require,module,exports){
|
|
;(function (root, factory) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
(function () {
|
|
// Shortcuts
|
|
var C = CryptoJS;
|
|
var C_lib = C.lib;
|
|
var WordArray = C_lib.WordArray;
|
|
var Hasher = C_lib.Hasher;
|
|
var C_algo = C.algo;
|
|
|
|
// Reusable object
|
|
var W = [];
|
|
|
|
/**
|
|
* SHA-1 hash algorithm.
|
|
*/
|
|
var SHA1 = C_algo.SHA1 = Hasher.extend({
|
|
_doReset: function () {
|
|
this._hash = new WordArray.init([
|
|
0x67452301, 0xefcdab89,
|
|
0x98badcfe, 0x10325476,
|
|
0xc3d2e1f0
|
|
]);
|
|
},
|
|
|
|
_doProcessBlock: function (M, offset) {
|
|
// Shortcut
|
|
var H = this._hash.words;
|
|
|
|
// Working variables
|
|
var a = H[0];
|
|
var b = H[1];
|
|
var c = H[2];
|
|
var d = H[3];
|
|
var e = H[4];
|
|
|
|
// Computation
|
|
for (var i = 0; i < 80; i++) {
|
|
if (i < 16) {
|
|
W[i] = M[offset + i] | 0;
|
|
} else {
|
|
var n = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
|
|
W[i] = (n << 1) | (n >>> 31);
|
|
}
|
|
|
|
var t = ((a << 5) | (a >>> 27)) + e + W[i];
|
|
if (i < 20) {
|
|
t += ((b & c) | (~b & d)) + 0x5a827999;
|
|
} else if (i < 40) {
|
|
t += (b ^ c ^ d) + 0x6ed9eba1;
|
|
} else if (i < 60) {
|
|
t += ((b & c) | (b & d) | (c & d)) - 0x70e44324;
|
|
} else /* if (i < 80) */ {
|
|
t += (b ^ c ^ d) - 0x359d3e2a;
|
|
}
|
|
|
|
e = d;
|
|
d = c;
|
|
c = (b << 30) | (b >>> 2);
|
|
b = a;
|
|
a = t;
|
|
}
|
|
|
|
// Intermediate hash value
|
|
H[0] = (H[0] + a) | 0;
|
|
H[1] = (H[1] + b) | 0;
|
|
H[2] = (H[2] + c) | 0;
|
|
H[3] = (H[3] + d) | 0;
|
|
H[4] = (H[4] + e) | 0;
|
|
},
|
|
|
|
_doFinalize: function () {
|
|
// Shortcuts
|
|
var data = this._data;
|
|
var dataWords = data.words;
|
|
|
|
var nBitsTotal = this._nDataBytes * 8;
|
|
var nBitsLeft = data.sigBytes * 8;
|
|
|
|
// Add padding
|
|
dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
|
|
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
|
|
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
|
|
data.sigBytes = dataWords.length * 4;
|
|
|
|
// Hash final blocks
|
|
this._process();
|
|
|
|
// Return final computed hash
|
|
return this._hash;
|
|
},
|
|
|
|
clone: function () {
|
|
var clone = Hasher.clone.call(this);
|
|
clone._hash = this._hash.clone();
|
|
|
|
return clone;
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Shortcut function to the hasher's object interface.
|
|
*
|
|
* @param {WordArray|string} message The message to hash.
|
|
*
|
|
* @return {WordArray} The hash.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var hash = CryptoJS.SHA1('message');
|
|
* var hash = CryptoJS.SHA1(wordArray);
|
|
*/
|
|
C.SHA1 = Hasher._createHelper(SHA1);
|
|
|
|
/**
|
|
* Shortcut function to the HMAC's object interface.
|
|
*
|
|
* @param {WordArray|string} message The message to hash.
|
|
* @param {WordArray|string} key The secret key.
|
|
*
|
|
* @return {WordArray} The HMAC.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var hmac = CryptoJS.HmacSHA1(message, key);
|
|
*/
|
|
C.HmacSHA1 = Hasher._createHmacHelper(SHA1);
|
|
}());
|
|
|
|
|
|
return CryptoJS.SHA1;
|
|
|
|
}));
|
|
},{"./core":18}],28:[function(require,module,exports){
|
|
;(function (root, factory) {
|
|
if (typeof exports === "object") {
|
|
// CommonJS
|
|
module.exports = exports = factory(require("./core"));
|
|
}
|
|
else if (typeof define === "function" && define.amd) {
|
|
// AMD
|
|
define(["./core"], factory);
|
|
}
|
|
else {
|
|
// Global (browser)
|
|
factory(root.CryptoJS);
|
|
}
|
|
}(this, function (CryptoJS) {
|
|
|
|
(function (Math) {
|
|
// Shortcuts
|
|
var C = CryptoJS;
|
|
var C_lib = C.lib;
|
|
var WordArray = C_lib.WordArray;
|
|
var Hasher = C_lib.Hasher;
|
|
var C_algo = C.algo;
|
|
|
|
// Initialization and round constants tables
|
|
var H = [];
|
|
var K = [];
|
|
|
|
// Compute constants
|
|
(function () {
|
|
function isPrime(n) {
|
|
var sqrtN = Math.sqrt(n);
|
|
for (var factor = 2; factor <= sqrtN; factor++) {
|
|
if (!(n % factor)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
function getFractionalBits(n) {
|
|
return ((n - (n | 0)) * 0x100000000) | 0;
|
|
}
|
|
|
|
var n = 2;
|
|
var nPrime = 0;
|
|
while (nPrime < 64) {
|
|
if (isPrime(n)) {
|
|
if (nPrime < 8) {
|
|
H[nPrime] = getFractionalBits(Math.pow(n, 1 / 2));
|
|
}
|
|
K[nPrime] = getFractionalBits(Math.pow(n, 1 / 3));
|
|
|
|
nPrime++;
|
|
}
|
|
|
|
n++;
|
|
}
|
|
}());
|
|
|
|
// Reusable object
|
|
var W = [];
|
|
|
|
/**
|
|
* SHA-256 hash algorithm.
|
|
*/
|
|
var SHA256 = C_algo.SHA256 = Hasher.extend({
|
|
_doReset: function () {
|
|
this._hash = new WordArray.init(H.slice(0));
|
|
},
|
|
|
|
_doProcessBlock: function (M, offset) {
|
|
// Shortcut
|
|
var H = this._hash.words;
|
|
|
|
// Working variables
|
|
var a = H[0];
|
|
var b = H[1];
|
|
var c = H[2];
|
|
var d = H[3];
|
|
var e = H[4];
|
|
var f = H[5];
|
|
var g = H[6];
|
|
var h = H[7];
|
|
|
|
// Computation
|
|
for (var i = 0; i < 64; i++) {
|
|
if (i < 16) {
|
|
W[i] = M[offset + i] | 0;
|
|
} else {
|
|
var gamma0x = W[i - 15];
|
|
var gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
|
|
((gamma0x << 14) | (gamma0x >>> 18)) ^
|
|
(gamma0x >>> 3);
|
|
|
|
var gamma1x = W[i - 2];
|
|
var gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
|
|
((gamma1x << 13) | (gamma1x >>> 19)) ^
|
|
(gamma1x >>> 10);
|
|
|
|
W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
|
|
}
|
|
|
|
var ch = (e & f) ^ (~e & g);
|
|
var maj = (a & b) ^ (a & c) ^ (b & c);
|
|
|
|
var sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
|
|
var sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25));
|
|
|
|
var t1 = h + sigma1 + ch + K[i] + W[i];
|
|
var t2 = sigma0 + maj;
|
|
|
|
h = g;
|
|
g = f;
|
|
f = e;
|
|
e = (d + t1) | 0;
|
|
d = c;
|
|
c = b;
|
|
b = a;
|
|
a = (t1 + t2) | 0;
|
|
}
|
|
|
|
// Intermediate hash value
|
|
H[0] = (H[0] + a) | 0;
|
|
H[1] = (H[1] + b) | 0;
|
|
H[2] = (H[2] + c) | 0;
|
|
H[3] = (H[3] + d) | 0;
|
|
H[4] = (H[4] + e) | 0;
|
|
H[5] = (H[5] + f) | 0;
|
|
H[6] = (H[6] + g) | 0;
|
|
H[7] = (H[7] + h) | 0;
|
|
},
|
|
|
|
_doFinalize: function () {
|
|
// Shortcuts
|
|
var data = this._data;
|
|
var dataWords = data.words;
|
|
|
|
var nBitsTotal = this._nDataBytes * 8;
|
|
var nBitsLeft = data.sigBytes * 8;
|
|
|
|
// Add padding
|
|
dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
|
|
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
|
|
dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
|
|
data.sigBytes = dataWords.length * 4;
|
|
|
|
// Hash final blocks
|
|
this._process();
|
|
|
|
// Return final computed hash
|
|
return this._hash;
|
|
},
|
|
|
|
clone: function () {
|
|
var clone = Hasher.clone.call(this);
|
|
clone._hash = this._hash.clone();
|
|
|
|
return clone;
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Shortcut function to the hasher's object interface.
|
|
*
|
|
* @param {WordArray|string} message The message to hash.
|
|
*
|
|
* @return {WordArray} The hash.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var hash = CryptoJS.SHA256('message');
|
|
* var hash = CryptoJS.SHA256(wordArray);
|
|
*/
|
|
C.SHA256 = Hasher._createHelper(SHA256);
|
|
|
|
/**
|
|
* Shortcut function to the HMAC's object interface.
|
|
*
|
|
* @param {WordArray|string} message The message to hash.
|
|
* @param {WordArray|string} key The secret key.
|
|
*
|
|
* @return {WordArray} The HMAC.
|
|
*
|
|
* @static
|
|
*
|
|
* @example
|
|
*
|
|
* var hmac = CryptoJS.HmacSHA256(message, key);
|
|
*/
|
|
C.HmacSHA256 = Hasher._createHmacHelper(SHA256);
|
|
}(Math));
|
|
|
|
|
|
return CryptoJS.SHA256;
|
|
|
|
}));
|
|
},{"./core":18}],29:[function(require,module,exports){
|
|
require=(function(e,t,n,r){function i(r){if(!n[r]){if(!t[r]){if(e)return e(r);throw new Error("Cannot find module '"+r+"'")}var s=n[r]={exports:{}};t[r][0](function(e){var n=t[r][1][e];return i(n?n:e)},s,s.exports)}return n[r].exports}for(var s=0;s<r.length;s++)i(r[s]);return i})(typeof require!=="undefined"&&require,{1:[function(require,module,exports){
|
|
// UTILITY
|
|
var util = require('util');
|
|
var Buffer = require("buffer").Buffer;
|
|
var pSlice = Array.prototype.slice;
|
|
|
|
function objectKeys(object) {
|
|
if (Object.keys) return Object.keys(object);
|
|
var result = [];
|
|
for (var name in object) {
|
|
if (Object.prototype.hasOwnProperty.call(object, name)) {
|
|
result.push(name);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// 1. The assert module provides functions that throw
|
|
// AssertionError's when particular conditions are not met. The
|
|
// assert module must conform to the following interface.
|
|
|
|
var assert = module.exports = ok;
|
|
|
|
// 2. The AssertionError is defined in assert.
|
|
// new assert.AssertionError({ message: message,
|
|
// actual: actual,
|
|
// expected: expected })
|
|
|
|
assert.AssertionError = function AssertionError(options) {
|
|
this.name = 'AssertionError';
|
|
this.message = options.message;
|
|
this.actual = options.actual;
|
|
this.expected = options.expected;
|
|
this.operator = options.operator;
|
|
var stackStartFunction = options.stackStartFunction || fail;
|
|
|
|
if (Error.captureStackTrace) {
|
|
Error.captureStackTrace(this, stackStartFunction);
|
|
}
|
|
};
|
|
util.inherits(assert.AssertionError, Error);
|
|
|
|
function replacer(key, value) {
|
|
if (value === undefined) {
|
|
return '' + value;
|
|
}
|
|
if (typeof value === 'number' && (isNaN(value) || !isFinite(value))) {
|
|
return value.toString();
|
|
}
|
|
if (typeof value === 'function' || value instanceof RegExp) {
|
|
return value.toString();
|
|
}
|
|
return value;
|
|
}
|
|
|
|
function truncate(s, n) {
|
|
if (typeof s == 'string') {
|
|
return s.length < n ? s : s.slice(0, n);
|
|
} else {
|
|
return s;
|
|
}
|
|
}
|
|
|
|
assert.AssertionError.prototype.toString = function() {
|
|
if (this.message) {
|
|
return [this.name + ':', this.message].join(' ');
|
|
} else {
|
|
return [
|
|
this.name + ':',
|
|
truncate(JSON.stringify(this.actual, replacer), 128),
|
|
this.operator,
|
|
truncate(JSON.stringify(this.expected, replacer), 128)
|
|
].join(' ');
|
|
}
|
|
};
|
|
|
|
// assert.AssertionError instanceof Error
|
|
|
|
assert.AssertionError.__proto__ = Error.prototype;
|
|
|
|
// At present only the three keys mentioned above are used and
|
|
// understood by the spec. Implementations or sub modules can pass
|
|
// other keys to the AssertionError's constructor - they will be
|
|
// ignored.
|
|
|
|
// 3. All of the following functions must throw an AssertionError
|
|
// when a corresponding condition is not met, with a message that
|
|
// may be undefined if not provided. All assertion methods provide
|
|
// both the actual and expected values to the assertion error for
|
|
// display purposes.
|
|
|
|
function fail(actual, expected, message, operator, stackStartFunction) {
|
|
throw new assert.AssertionError({
|
|
message: message,
|
|
actual: actual,
|
|
expected: expected,
|
|
operator: operator,
|
|
stackStartFunction: stackStartFunction
|
|
});
|
|
}
|
|
|
|
// EXTENSION! allows for well behaved errors defined elsewhere.
|
|
assert.fail = fail;
|
|
|
|
// 4. Pure assertion tests whether a value is truthy, as determined
|
|
// by !!guard.
|
|
// assert.ok(guard, message_opt);
|
|
// This statement is equivalent to assert.equal(true, guard,
|
|
// message_opt);. To test strictly for the value true, use
|
|
// assert.strictEqual(true, guard, message_opt);.
|
|
|
|
function ok(value, message) {
|
|
if (!!!value) fail(value, true, message, '==', assert.ok);
|
|
}
|
|
assert.ok = ok;
|
|
|
|
// 5. The equality assertion tests shallow, coercive equality with
|
|
// ==.
|
|
// assert.equal(actual, expected, message_opt);
|
|
|
|
assert.equal = function equal(actual, expected, message) {
|
|
if (actual != expected) fail(actual, expected, message, '==', assert.equal);
|
|
};
|
|
|
|
// 6. The non-equality assertion tests for whether two objects are not equal
|
|
// with != assert.notEqual(actual, expected, message_opt);
|
|
|
|
assert.notEqual = function notEqual(actual, expected, message) {
|
|
if (actual == expected) {
|
|
fail(actual, expected, message, '!=', assert.notEqual);
|
|
}
|
|
};
|
|
|
|
// 7. The equivalence assertion tests a deep equality relation.
|
|
// assert.deepEqual(actual, expected, message_opt);
|
|
|
|
assert.deepEqual = function deepEqual(actual, expected, message) {
|
|
if (!_deepEqual(actual, expected)) {
|
|
fail(actual, expected, message, 'deepEqual', assert.deepEqual);
|
|
}
|
|
};
|
|
|
|
function _deepEqual(actual, expected) {
|
|
// 7.1. All identical values are equivalent, as determined by ===.
|
|
if (actual === expected) {
|
|
return true;
|
|
|
|
} else if (Buffer.isBuffer(actual) && Buffer.isBuffer(expected)) {
|
|
if (actual.length != expected.length) return false;
|
|
|
|
for (var i = 0; i < actual.length; i++) {
|
|
if (actual[i] !== expected[i]) return false;
|
|
}
|
|
|
|
return true;
|
|
|
|
// 7.2. If the expected value is a Date object, the actual value is
|
|
// equivalent if it is also a Date object that refers to the same time.
|
|
} else if (actual instanceof Date && expected instanceof Date) {
|
|
return actual.getTime() === expected.getTime();
|
|
|
|
// 7.3. Other pairs that do not both pass typeof value == 'object',
|
|
// equivalence is determined by ==.
|
|
} else if (typeof actual != 'object' && typeof expected != 'object') {
|
|
return actual == expected;
|
|
|
|
// 7.4. For all other Object pairs, including Array objects, equivalence is
|
|
// determined by having the same number of owned properties (as verified
|
|
// with Object.prototype.hasOwnProperty.call), the same set of keys
|
|
// (although not necessarily the same order), equivalent values for every
|
|
// corresponding key, and an identical 'prototype' property. Note: this
|
|
// accounts for both named and indexed properties on Arrays.
|
|
} else {
|
|
return objEquiv(actual, expected);
|
|
}
|
|
}
|
|
|
|
function isUndefinedOrNull(value) {
|
|
return value === null || value === undefined;
|
|
}
|
|
|
|
function isArguments(object) {
|
|
return Object.prototype.toString.call(object) == '[object Arguments]';
|
|
}
|
|
|
|
function objEquiv(a, b) {
|
|
if (isUndefinedOrNull(a) || isUndefinedOrNull(b))
|
|
return false;
|
|
// an identical 'prototype' property.
|
|
if (a.prototype !== b.prototype) return false;
|
|
//~~~I've managed to break Object.keys through screwy arguments passing.
|
|
// Converting to array solves the problem.
|
|
if (isArguments(a)) {
|
|
if (!isArguments(b)) {
|
|
return false;
|
|
}
|
|
a = pSlice.call(a);
|
|
b = pSlice.call(b);
|
|
return _deepEqual(a, b);
|
|
}
|
|
try {
|
|
var ka = objectKeys(a),
|
|
kb = objectKeys(b),
|
|
key, i;
|
|
} catch (e) {//happens when one is a string literal and the other isn't
|
|
return false;
|
|
}
|
|
// having the same number of owned properties (keys incorporates
|
|
// hasOwnProperty)
|
|
if (ka.length != kb.length)
|
|
return false;
|
|
//the same set of keys (although not necessarily the same order),
|
|
ka.sort();
|
|
kb.sort();
|
|
//~~~cheap key test
|
|
for (i = ka.length - 1; i >= 0; i--) {
|
|
if (ka[i] != kb[i])
|
|
return false;
|
|
}
|
|
//equivalent values for every corresponding key, and
|
|
//~~~possibly expensive deep test
|
|
for (i = ka.length - 1; i >= 0; i--) {
|
|
key = ka[i];
|
|
if (!_deepEqual(a[key], b[key])) return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// 8. The non-equivalence assertion tests for any deep inequality.
|
|
// assert.notDeepEqual(actual, expected, message_opt);
|
|
|
|
assert.notDeepEqual = function notDeepEqual(actual, expected, message) {
|
|
if (_deepEqual(actual, expected)) {
|
|
fail(actual, expected, message, 'notDeepEqual', assert.notDeepEqual);
|
|
}
|
|
};
|
|
|
|
// 9. The strict equality assertion tests strict equality, as determined by ===.
|
|
// assert.strictEqual(actual, expected, message_opt);
|
|
|
|
assert.strictEqual = function strictEqual(actual, expected, message) {
|
|
if (actual !== expected) {
|
|
fail(actual, expected, message, '===', assert.strictEqual);
|
|
}
|
|
};
|
|
|
|
// 10. The strict non-equality assertion tests for strict inequality, as
|
|
// determined by !==. assert.notStrictEqual(actual, expected, message_opt);
|
|
|
|
assert.notStrictEqual = function notStrictEqual(actual, expected, message) {
|
|
if (actual === expected) {
|
|
fail(actual, expected, message, '!==', assert.notStrictEqual);
|
|
}
|
|
};
|
|
|
|
function expectedException(actual, expected) {
|
|
if (!actual || !expected) {
|
|
return false;
|
|
}
|
|
|
|
if (expected instanceof RegExp) {
|
|
return expected.test(actual);
|
|
} else if (actual instanceof expected) {
|
|
return true;
|
|
} else if (expected.call({}, actual) === true) {
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
function _throws(shouldThrow, block, expected, message) {
|
|
var actual;
|
|
|
|
if (typeof expected === 'string') {
|
|
message = expected;
|
|
expected = null;
|
|
}
|
|
|
|
try {
|
|
block();
|
|
} catch (e) {
|
|
actual = e;
|
|
}
|
|
|
|
message = (expected && expected.name ? ' (' + expected.name + ').' : '.') +
|
|
(message ? ' ' + message : '.');
|
|
|
|
if (shouldThrow && !actual) {
|
|
fail('Missing expected exception' + message);
|
|
}
|
|
|
|
if (!shouldThrow && expectedException(actual, expected)) {
|
|
fail('Got unwanted exception' + message);
|
|
}
|
|
|
|
if ((shouldThrow && actual && expected &&
|
|
!expectedException(actual, expected)) || (!shouldThrow && actual)) {
|
|
throw actual;
|
|
}
|
|
}
|
|
|
|
// 11. Expected to throw an error:
|
|
// assert.throws(block, Error_opt, message_opt);
|
|
|
|
assert.throws = function(block, /*optional*/error, /*optional*/message) {
|
|
_throws.apply(this, [true].concat(pSlice.call(arguments)));
|
|
};
|
|
|
|
// EXTENSION! This is annoying to write outside this module.
|
|
assert.doesNotThrow = function(block, /*optional*/error, /*optional*/message) {
|
|
_throws.apply(this, [false].concat(pSlice.call(arguments)));
|
|
};
|
|
|
|
assert.ifError = function(err) { if (err) {throw err;}};
|
|
|
|
},{"util":2,"buffer":3}],2:[function(require,module,exports){
|
|
var events = require('events');
|
|
|
|
exports.isArray = isArray;
|
|
exports.isDate = function(obj){return Object.prototype.toString.call(obj) === '[object Date]'};
|
|
exports.isRegExp = function(obj){return Object.prototype.toString.call(obj) === '[object RegExp]'};
|
|
|
|
|
|
exports.print = function () {};
|
|
exports.puts = function () {};
|
|
exports.debug = function() {};
|
|
|
|
exports.inspect = function(obj, showHidden, depth, colors) {
|
|
var seen = [];
|
|
|
|
var stylize = function(str, styleType) {
|
|
// http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
|
|
var styles =
|
|
{ 'bold' : [1, 22],
|
|
'italic' : [3, 23],
|
|
'underline' : [4, 24],
|
|
'inverse' : [7, 27],
|
|
'white' : [37, 39],
|
|
'grey' : [90, 39],
|
|
'black' : [30, 39],
|
|
'blue' : [34, 39],
|
|
'cyan' : [36, 39],
|
|
'green' : [32, 39],
|
|
'magenta' : [35, 39],
|
|
'red' : [31, 39],
|
|
'yellow' : [33, 39] };
|
|
|
|
var style =
|
|
{ 'special': 'cyan',
|
|
'number': 'blue',
|
|
'boolean': 'yellow',
|
|
'undefined': 'grey',
|
|
'null': 'bold',
|
|
'string': 'green',
|
|
'date': 'magenta',
|
|
// "name": intentionally not styling
|
|
'regexp': 'red' }[styleType];
|
|
|
|
if (style) {
|
|
return '\033[' + styles[style][0] + 'm' + str +
|
|
'\033[' + styles[style][1] + 'm';
|
|
} else {
|
|
return str;
|
|
}
|
|
};
|
|
if (! colors) {
|
|
stylize = function(str, styleType) { return str; };
|
|
}
|
|
|
|
function format(value, recurseTimes) {
|
|
// Provide a hook for user-specified inspect functions.
|
|
// Check that value is an object with an inspect function on it
|
|
if (value && typeof value.inspect === 'function' &&
|
|
// Filter out the util module, it's inspect function is special
|
|
value !== exports &&
|
|
// Also filter out any prototype objects using the circular check.
|
|
!(value.constructor && value.constructor.prototype === value)) {
|
|
return value.inspect(recurseTimes);
|
|
}
|
|
|
|
// Primitive types cannot have properties
|
|
switch (typeof value) {
|
|
case 'undefined':
|
|
return stylize('undefined', 'undefined');
|
|
|
|
case 'string':
|
|
var simple = '\'' + JSON.stringify(value).replace(/^"|"$/g, '')
|
|
.replace(/'/g, "\\'")
|
|
.replace(/\\"/g, '"') + '\'';
|
|
return stylize(simple, 'string');
|
|
|
|
case 'number':
|
|
return stylize('' + value, 'number');
|
|
|
|
case 'boolean':
|
|
return stylize('' + value, 'boolean');
|
|
}
|
|
// For some reason typeof null is "object", so special case here.
|
|
if (value === null) {
|
|
return stylize('null', 'null');
|
|
}
|
|
|
|
// Look up the keys of the object.
|
|
var visible_keys = Object_keys(value);
|
|
var keys = showHidden ? Object_getOwnPropertyNames(value) : visible_keys;
|
|
|
|
// Functions without properties can be shortcutted.
|
|
if (typeof value === 'function' && keys.length === 0) {
|
|
if (isRegExp(value)) {
|
|
return stylize('' + value, 'regexp');
|
|
} else {
|
|
var name = value.name ? ': ' + value.name : '';
|
|
return stylize('[Function' + name + ']', 'special');
|
|
}
|
|
}
|
|
|
|
// Dates without properties can be shortcutted
|
|
if (isDate(value) && keys.length === 0) {
|
|
return stylize(value.toUTCString(), 'date');
|
|
}
|
|
|
|
var base, type, braces;
|
|
// Determine the object type
|
|
if (isArray(value)) {
|
|
type = 'Array';
|
|
braces = ['[', ']'];
|
|
} else {
|
|
type = 'Object';
|
|
braces = ['{', '}'];
|
|
}
|
|
|
|
// Make functions say that they are functions
|
|
if (typeof value === 'function') {
|
|
var n = value.name ? ': ' + value.name : '';
|
|
base = (isRegExp(value)) ? ' ' + value : ' [Function' + n + ']';
|
|
} else {
|
|
base = '';
|
|
}
|
|
|
|
// Make dates with properties first say the date
|
|
if (isDate(value)) {
|
|
base = ' ' + value.toUTCString();
|
|
}
|
|
|
|
if (keys.length === 0) {
|
|
return braces[0] + base + braces[1];
|
|
}
|
|
|
|
if (recurseTimes < 0) {
|
|
if (isRegExp(value)) {
|
|
return stylize('' + value, 'regexp');
|
|
} else {
|
|
return stylize('[Object]', 'special');
|
|
}
|
|
}
|
|
|
|
seen.push(value);
|
|
|
|
var output = keys.map(function(key) {
|
|
var name, str;
|
|
if (value.__lookupGetter__) {
|
|
if (value.__lookupGetter__(key)) {
|
|
if (value.__lookupSetter__(key)) {
|
|
str = stylize('[Getter/Setter]', 'special');
|
|
} else {
|
|
str = stylize('[Getter]', 'special');
|
|
}
|
|
} else {
|
|
if (value.__lookupSetter__(key)) {
|
|
str = stylize('[Setter]', 'special');
|
|
}
|
|
}
|
|
}
|
|
if (visible_keys.indexOf(key) < 0) {
|
|
name = '[' + key + ']';
|
|
}
|
|
if (!str) {
|
|
if (seen.indexOf(value[key]) < 0) {
|
|
if (recurseTimes === null) {
|
|
str = format(value[key]);
|
|
} else {
|
|
str = format(value[key], recurseTimes - 1);
|
|
}
|
|
if (str.indexOf('\n') > -1) {
|
|
if (isArray(value)) {
|
|
str = str.split('\n').map(function(line) {
|
|
return ' ' + line;
|
|
}).join('\n').substr(2);
|
|
} else {
|
|
str = '\n' + str.split('\n').map(function(line) {
|
|
return ' ' + line;
|
|
}).join('\n');
|
|
}
|
|
}
|
|
} else {
|
|
str = stylize('[Circular]', 'special');
|
|
}
|
|
}
|
|
if (typeof name === 'undefined') {
|
|
if (type === 'Array' && key.match(/^\d+$/)) {
|
|
return str;
|
|
}
|
|
name = JSON.stringify('' + key);
|
|
if (name.match(/^"([a-zA-Z_][a-zA-Z_0-9]*)"$/)) {
|
|
name = name.substr(1, name.length - 2);
|
|
name = stylize(name, 'name');
|
|
} else {
|
|
name = name.replace(/'/g, "\\'")
|
|
.replace(/\\"/g, '"')
|
|
.replace(/(^"|"$)/g, "'");
|
|
name = stylize(name, 'string');
|
|
}
|
|
}
|
|
|
|
return name + ': ' + str;
|
|
});
|
|
|
|
seen.pop();
|
|
|
|
var numLinesEst = 0;
|
|
var length = output.reduce(function(prev, cur) {
|
|
numLinesEst++;
|
|
if (cur.indexOf('\n') >= 0) numLinesEst++;
|
|
return prev + cur.length + 1;
|
|
}, 0);
|
|
|
|
if (length > 50) {
|
|
output = braces[0] +
|
|
(base === '' ? '' : base + '\n ') +
|
|
' ' +
|
|
output.join(',\n ') +
|
|
' ' +
|
|
braces[1];
|
|
|
|
} else {
|
|
output = braces[0] + base + ' ' + output.join(', ') + ' ' + braces[1];
|
|
}
|
|
|
|
return output;
|
|
}
|
|
return format(obj, (typeof depth === 'undefined' ? 2 : depth));
|
|
};
|
|
|
|
|
|
function isArray(ar) {
|
|
return ar instanceof Array ||
|
|
Array.isArray(ar) ||
|
|
(ar && ar !== Object.prototype && isArray(ar.__proto__));
|
|
}
|
|
|
|
|
|
function isRegExp(re) {
|
|
return re instanceof RegExp ||
|
|
(typeof re === 'object' && Object.prototype.toString.call(re) === '[object RegExp]');
|
|
}
|
|
|
|
|
|
function isDate(d) {
|
|
if (d instanceof Date) return true;
|
|
if (typeof d !== 'object') return false;
|
|
var properties = Date.prototype && Object_getOwnPropertyNames(Date.prototype);
|
|
var proto = d.__proto__ && Object_getOwnPropertyNames(d.__proto__);
|
|
return JSON.stringify(proto) === JSON.stringify(properties);
|
|
}
|
|
|
|
function pad(n) {
|
|
return n < 10 ? '0' + n.toString(10) : n.toString(10);
|
|
}
|
|
|
|
var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep',
|
|
'Oct', 'Nov', 'Dec'];
|
|
|
|
// 26 Feb 16:19:34
|
|
function timestamp() {
|
|
var d = new Date();
|
|
var time = [pad(d.getHours()),
|
|
pad(d.getMinutes()),
|
|
pad(d.getSeconds())].join(':');
|
|
return [d.getDate(), months[d.getMonth()], time].join(' ');
|
|
}
|
|
|
|
exports.log = function (msg) {};
|
|
|
|
exports.pump = null;
|
|
|
|
var Object_keys = Object.keys || function (obj) {
|
|
var res = [];
|
|
for (var key in obj) res.push(key);
|
|
return res;
|
|
};
|
|
|
|
var Object_getOwnPropertyNames = Object.getOwnPropertyNames || function (obj) {
|
|
var res = [];
|
|
for (var key in obj) {
|
|
if (Object.hasOwnProperty.call(obj, key)) res.push(key);
|
|
}
|
|
return res;
|
|
};
|
|
|
|
var Object_create = Object.create || function (prototype, properties) {
|
|
// from es5-shim
|
|
var object;
|
|
if (prototype === null) {
|
|
object = { '__proto__' : null };
|
|
}
|
|
else {
|
|
if (typeof prototype !== 'object') {
|
|
throw new TypeError(
|
|
'typeof prototype[' + (typeof prototype) + '] != \'object\''
|
|
);
|
|
}
|
|
var Type = function () {};
|
|
Type.prototype = prototype;
|
|
object = new Type();
|
|
object.__proto__ = prototype;
|
|
}
|
|
if (typeof properties !== 'undefined' && Object.defineProperties) {
|
|
Object.defineProperties(object, properties);
|
|
}
|
|
return object;
|
|
};
|
|
|
|
exports.inherits = function(ctor, superCtor) {
|
|
ctor.super_ = superCtor;
|
|
ctor.prototype = Object_create(superCtor.prototype, {
|
|
constructor: {
|
|
value: ctor,
|
|
enumerable: false,
|
|
writable: true,
|
|
configurable: true
|
|
}
|
|
});
|
|
};
|
|
|
|
var formatRegExp = /%[sdj%]/g;
|
|
exports.format = function(f) {
|
|
if (typeof f !== 'string') {
|
|
var objects = [];
|
|
for (var i = 0; i < arguments.length; i++) {
|
|
objects.push(exports.inspect(arguments[i]));
|
|
}
|
|
return objects.join(' ');
|
|
}
|
|
|
|
var i = 1;
|
|
var args = arguments;
|
|
var len = args.length;
|
|
var str = String(f).replace(formatRegExp, function(x) {
|
|
if (x === '%%') return '%';
|
|
if (i >= len) return x;
|
|
switch (x) {
|
|
case '%s': return String(args[i++]);
|
|
case '%d': return Number(args[i++]);
|
|
case '%j': return JSON.stringify(args[i++]);
|
|
default:
|
|
return x;
|
|
}
|
|
});
|
|
for(var x = args[i]; i < len; x = args[++i]){
|
|
if (x === null || typeof x !== 'object') {
|
|
str += ' ' + x;
|
|
} else {
|
|
str += ' ' + exports.inspect(x);
|
|
}
|
|
}
|
|
return str;
|
|
};
|
|
|
|
},{"events":4}],5:[function(require,module,exports){
|
|
exports.readIEEE754 = function(buffer, offset, isBE, mLen, nBytes) {
|
|
var e, m,
|
|
eLen = nBytes * 8 - mLen - 1,
|
|
eMax = (1 << eLen) - 1,
|
|
eBias = eMax >> 1,
|
|
nBits = -7,
|
|
i = isBE ? 0 : (nBytes - 1),
|
|
d = isBE ? 1 : -1,
|
|
s = buffer[offset + i];
|
|
|
|
i += d;
|
|
|
|
e = s & ((1 << (-nBits)) - 1);
|
|
s >>= (-nBits);
|
|
nBits += eLen;
|
|
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
|
|
|
|
m = e & ((1 << (-nBits)) - 1);
|
|
e >>= (-nBits);
|
|
nBits += mLen;
|
|
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
|
|
|
|
if (e === 0) {
|
|
e = 1 - eBias;
|
|
} else if (e === eMax) {
|
|
return m ? NaN : ((s ? -1 : 1) * Infinity);
|
|
} else {
|
|
m = m + Math.pow(2, mLen);
|
|
e = e - eBias;
|
|
}
|
|
return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
|
|
};
|
|
|
|
exports.writeIEEE754 = function(buffer, value, offset, isBE, mLen, nBytes) {
|
|
var e, m, c,
|
|
eLen = nBytes * 8 - mLen - 1,
|
|
eMax = (1 << eLen) - 1,
|
|
eBias = eMax >> 1,
|
|
rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0),
|
|
i = isBE ? (nBytes - 1) : 0,
|
|
d = isBE ? -1 : 1,
|
|
s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
|
|
|
|
value = Math.abs(value);
|
|
|
|
if (isNaN(value) || value === Infinity) {
|
|
m = isNaN(value) ? 1 : 0;
|
|
e = eMax;
|
|
} else {
|
|
e = Math.floor(Math.log(value) / Math.LN2);
|
|
if (value * (c = Math.pow(2, -e)) < 1) {
|
|
e--;
|
|
c *= 2;
|
|
}
|
|
if (e + eBias >= 1) {
|
|
value += rt / c;
|
|
} else {
|
|
value += rt * Math.pow(2, 1 - eBias);
|
|
}
|
|
if (value * c >= 2) {
|
|
e++;
|
|
c /= 2;
|
|
}
|
|
|
|
if (e + eBias >= eMax) {
|
|
m = 0;
|
|
e = eMax;
|
|
} else if (e + eBias >= 1) {
|
|
m = (value * c - 1) * Math.pow(2, mLen);
|
|
e = e + eBias;
|
|
} else {
|
|
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
|
|
e = 0;
|
|
}
|
|
}
|
|
|
|
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8);
|
|
|
|
e = (e << mLen) | m;
|
|
eLen += mLen;
|
|
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8);
|
|
|
|
buffer[offset + i - d] |= s * 128;
|
|
};
|
|
|
|
},{}],6:[function(require,module,exports){
|
|
// shim for using process in browser
|
|
|
|
var process = module.exports = {};
|
|
|
|
process.nextTick = (function () {
|
|
var canSetImmediate = typeof window !== 'undefined'
|
|
&& window.setImmediate;
|
|
var canPost = typeof window !== 'undefined'
|
|
&& window.postMessage && window.addEventListener
|
|
;
|
|
|
|
if (canSetImmediate) {
|
|
return function (f) { return window.setImmediate(f) };
|
|
}
|
|
|
|
if (canPost) {
|
|
var queue = [];
|
|
window.addEventListener('message', function (ev) {
|
|
if (ev.source === window && ev.data === 'process-tick') {
|
|
ev.stopPropagation();
|
|
if (queue.length > 0) {
|
|
var fn = queue.shift();
|
|
fn();
|
|
}
|
|
}
|
|
}, true);
|
|
|
|
return function nextTick(fn) {
|
|
queue.push(fn);
|
|
window.postMessage('process-tick', '*');
|
|
};
|
|
}
|
|
|
|
return function nextTick(fn) {
|
|
setTimeout(fn, 0);
|
|
};
|
|
})();
|
|
|
|
process.title = 'browser';
|
|
process.browser = true;
|
|
process.env = {};
|
|
process.argv = [];
|
|
|
|
process.binding = function (name) {
|
|
throw new Error('process.binding is not supported');
|
|
}
|
|
|
|
// TODO(shtylman)
|
|
process.cwd = function () { return '/' };
|
|
process.chdir = function (dir) {
|
|
throw new Error('process.chdir is not supported');
|
|
};
|
|
|
|
},{}],4:[function(require,module,exports){
|
|
(function(process){if (!process.EventEmitter) process.EventEmitter = function () {};
|
|
|
|
var EventEmitter = exports.EventEmitter = process.EventEmitter;
|
|
var isArray = typeof Array.isArray === 'function'
|
|
? Array.isArray
|
|
: function (xs) {
|
|
return Object.prototype.toString.call(xs) === '[object Array]'
|
|
}
|
|
;
|
|
function indexOf (xs, x) {
|
|
if (xs.indexOf) return xs.indexOf(x);
|
|
for (var i = 0; i < xs.length; i++) {
|
|
if (x === xs[i]) return i;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
// By default EventEmitters will print a warning if more than
|
|
// 10 listeners are added to it. This is a useful default which
|
|
// helps finding memory leaks.
|
|
//
|
|
// Obviously not all Emitters should be limited to 10. This function allows
|
|
// that to be increased. Set to zero for unlimited.
|
|
var defaultMaxListeners = 10;
|
|
EventEmitter.prototype.setMaxListeners = function(n) {
|
|
if (!this._events) this._events = {};
|
|
this._events.maxListeners = n;
|
|
};
|
|
|
|
|
|
EventEmitter.prototype.emit = function(type) {
|
|
// If there is no 'error' event listener then throw.
|
|
if (type === 'error') {
|
|
if (!this._events || !this._events.error ||
|
|
(isArray(this._events.error) && !this._events.error.length))
|
|
{
|
|
if (arguments[1] instanceof Error) {
|
|
throw arguments[1]; // Unhandled 'error' event
|
|
} else {
|
|
throw new Error("Uncaught, unspecified 'error' event.");
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!this._events) return false;
|
|
var handler = this._events[type];
|
|
if (!handler) return false;
|
|
|
|
if (typeof handler == 'function') {
|
|
switch (arguments.length) {
|
|
// fast cases
|
|
case 1:
|
|
handler.call(this);
|
|
break;
|
|
case 2:
|
|
handler.call(this, arguments[1]);
|
|
break;
|
|
case 3:
|
|
handler.call(this, arguments[1], arguments[2]);
|
|
break;
|
|
// slower
|
|
default:
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
handler.apply(this, args);
|
|
}
|
|
return true;
|
|
|
|
} else if (isArray(handler)) {
|
|
var args = Array.prototype.slice.call(arguments, 1);
|
|
|
|
var listeners = handler.slice();
|
|
for (var i = 0, l = listeners.length; i < l; i++) {
|
|
listeners[i].apply(this, args);
|
|
}
|
|
return true;
|
|
|
|
} else {
|
|
return false;
|
|
}
|
|
};
|
|
|
|
// EventEmitter is defined in src/node_events.cc
|
|
// EventEmitter.prototype.emit() is also defined there.
|
|
EventEmitter.prototype.addListener = function(type, listener) {
|
|
if ('function' !== typeof listener) {
|
|
throw new Error('addListener only takes instances of Function');
|
|
}
|
|
|
|
if (!this._events) this._events = {};
|
|
|
|
// To avoid recursion in the case that type == "newListeners"! Before
|
|
// adding it to the listeners, first emit "newListeners".
|
|
this.emit('newListener', type, listener);
|
|
|
|
if (!this._events[type]) {
|
|
// Optimize the case of one listener. Don't need the extra array object.
|
|
this._events[type] = listener;
|
|
} else if (isArray(this._events[type])) {
|
|
|
|
// Check for listener leak
|
|
if (!this._events[type].warned) {
|
|
var m;
|
|
if (this._events.maxListeners !== undefined) {
|
|
m = this._events.maxListeners;
|
|
} else {
|
|
m = defaultMaxListeners;
|
|
}
|
|
|
|
if (m && m > 0 && this._events[type].length > m) {
|
|
this._events[type].warned = true;
|
|
console.error('(node) warning: possible EventEmitter memory ' +
|
|
'leak detected. %d listeners added. ' +
|
|
'Use emitter.setMaxListeners() to increase limit.',
|
|
this._events[type].length);
|
|
console.trace();
|
|
}
|
|
}
|
|
|
|
// If we've already got an array, just append.
|
|
this._events[type].push(listener);
|
|
} else {
|
|
// Adding the second element, need to change to array.
|
|
this._events[type] = [this._events[type], listener];
|
|
}
|
|
|
|
return this;
|
|
};
|
|
|
|
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
|
|
|
|
EventEmitter.prototype.once = function(type, listener) {
|
|
var self = this;
|
|
self.on(type, function g() {
|
|
self.removeListener(type, g);
|
|
listener.apply(this, arguments);
|
|
});
|
|
|
|
return this;
|
|
};
|
|
|
|
EventEmitter.prototype.removeListener = function(type, listener) {
|
|
if ('function' !== typeof listener) {
|
|
throw new Error('removeListener only takes instances of Function');
|
|
}
|
|
|
|
// does not use listeners(), so no side effect of creating _events[type]
|
|
if (!this._events || !this._events[type]) return this;
|
|
|
|
var list = this._events[type];
|
|
|
|
if (isArray(list)) {
|
|
var i = indexOf(list, listener);
|
|
if (i < 0) return this;
|
|
list.splice(i, 1);
|
|
if (list.length == 0)
|
|
delete this._events[type];
|
|
} else if (this._events[type] === listener) {
|
|
delete this._events[type];
|
|
}
|
|
|
|
return this;
|
|
};
|
|
|
|
EventEmitter.prototype.removeAllListeners = function(type) {
|
|
if (arguments.length === 0) {
|
|
this._events = {};
|
|
return this;
|
|
}
|
|
|
|
// does not use listeners(), so no side effect of creating _events[type]
|
|
if (type && this._events && this._events[type]) this._events[type] = null;
|
|
return this;
|
|
};
|
|
|
|
EventEmitter.prototype.listeners = function(type) {
|
|
if (!this._events) this._events = {};
|
|
if (!this._events[type]) this._events[type] = [];
|
|
if (!isArray(this._events[type])) {
|
|
this._events[type] = [this._events[type]];
|
|
}
|
|
return this._events[type];
|
|
};
|
|
|
|
})(require("__browserify_process"))
|
|
},{"__browserify_process":6}],"buffer-browserify":[function(require,module,exports){
|
|
module.exports=require('q9TxCC');
|
|
},{}],"q9TxCC":[function(require,module,exports){
|
|
function SlowBuffer (size) {
|
|
this.length = size;
|
|
};
|
|
|
|
var assert = require('assert');
|
|
|
|
exports.INSPECT_MAX_BYTES = 50;
|
|
|
|
|
|
function toHex(n) {
|
|
if (n < 16) return '0' + n.toString(16);
|
|
return n.toString(16);
|
|
}
|
|
|
|
function utf8ToBytes(str) {
|
|
var byteArray = [];
|
|
for (var i = 0; i < str.length; i++)
|
|
if (str.charCodeAt(i) <= 0x7F)
|
|
byteArray.push(str.charCodeAt(i));
|
|
else {
|
|
var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
|
|
for (var j = 0; j < h.length; j++)
|
|
byteArray.push(parseInt(h[j], 16));
|
|
}
|
|
|
|
return byteArray;
|
|
}
|
|
|
|
function asciiToBytes(str) {
|
|
var byteArray = []
|
|
for (var i = 0; i < str.length; i++ )
|
|
// Node's code seems to be doing this and not & 0x7F..
|
|
byteArray.push( str.charCodeAt(i) & 0xFF );
|
|
|
|
return byteArray;
|
|
}
|
|
|
|
function base64ToBytes(str) {
|
|
return require("base64-js").toByteArray(str);
|
|
}
|
|
|
|
SlowBuffer.byteLength = function (str, encoding) {
|
|
switch (encoding || "utf8") {
|
|
case 'hex':
|
|
return str.length / 2;
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
return utf8ToBytes(str).length;
|
|
|
|
case 'ascii':
|
|
case 'binary':
|
|
return str.length;
|
|
|
|
case 'base64':
|
|
return base64ToBytes(str).length;
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
};
|
|
|
|
function blitBuffer(src, dst, offset, length) {
|
|
var pos, i = 0;
|
|
while (i < length) {
|
|
if ((i+offset >= dst.length) || (i >= src.length))
|
|
break;
|
|
|
|
dst[i + offset] = src[i];
|
|
i++;
|
|
}
|
|
return i;
|
|
}
|
|
|
|
SlowBuffer.prototype.utf8Write = function (string, offset, length) {
|
|
var bytes, pos;
|
|
return SlowBuffer._charsWritten = blitBuffer(utf8ToBytes(string), this, offset, length);
|
|
};
|
|
|
|
SlowBuffer.prototype.asciiWrite = function (string, offset, length) {
|
|
var bytes, pos;
|
|
return SlowBuffer._charsWritten = blitBuffer(asciiToBytes(string), this, offset, length);
|
|
};
|
|
|
|
SlowBuffer.prototype.binaryWrite = SlowBuffer.prototype.asciiWrite;
|
|
|
|
SlowBuffer.prototype.base64Write = function (string, offset, length) {
|
|
var bytes, pos;
|
|
return SlowBuffer._charsWritten = blitBuffer(base64ToBytes(string), this, offset, length);
|
|
};
|
|
|
|
SlowBuffer.prototype.base64Slice = function (start, end) {
|
|
var bytes = Array.prototype.slice.apply(this, arguments)
|
|
return require("base64-js").fromByteArray(bytes);
|
|
}
|
|
|
|
function decodeUtf8Char(str) {
|
|
try {
|
|
return decodeURIComponent(str);
|
|
} catch (err) {
|
|
return String.fromCharCode(0xFFFD); // UTF 8 invalid char
|
|
}
|
|
}
|
|
|
|
SlowBuffer.prototype.utf8Slice = function () {
|
|
var bytes = Array.prototype.slice.apply(this, arguments);
|
|
var res = "";
|
|
var tmp = "";
|
|
var i = 0;
|
|
while (i < bytes.length) {
|
|
if (bytes[i] <= 0x7F) {
|
|
res += decodeUtf8Char(tmp) + String.fromCharCode(bytes[i]);
|
|
tmp = "";
|
|
} else
|
|
tmp += "%" + bytes[i].toString(16);
|
|
|
|
i++;
|
|
}
|
|
|
|
return res + decodeUtf8Char(tmp);
|
|
}
|
|
|
|
SlowBuffer.prototype.asciiSlice = function () {
|
|
var bytes = Array.prototype.slice.apply(this, arguments);
|
|
var ret = "";
|
|
for (var i = 0; i < bytes.length; i++)
|
|
ret += String.fromCharCode(bytes[i]);
|
|
return ret;
|
|
}
|
|
|
|
SlowBuffer.prototype.binarySlice = SlowBuffer.prototype.asciiSlice;
|
|
|
|
SlowBuffer.prototype.inspect = function() {
|
|
var out = [],
|
|
len = this.length;
|
|
for (var i = 0; i < len; i++) {
|
|
out[i] = toHex(this[i]);
|
|
if (i == exports.INSPECT_MAX_BYTES) {
|
|
out[i + 1] = '...';
|
|
break;
|
|
}
|
|
}
|
|
return '<SlowBuffer ' + out.join(' ') + '>';
|
|
};
|
|
|
|
|
|
SlowBuffer.prototype.hexSlice = function(start, end) {
|
|
var len = this.length;
|
|
|
|
if (!start || start < 0) start = 0;
|
|
if (!end || end < 0 || end > len) end = len;
|
|
|
|
var out = '';
|
|
for (var i = start; i < end; i++) {
|
|
out += toHex(this[i]);
|
|
}
|
|
return out;
|
|
};
|
|
|
|
|
|
SlowBuffer.prototype.toString = function(encoding, start, end) {
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
|
start = +start || 0;
|
|
if (typeof end == 'undefined') end = this.length;
|
|
|
|
// Fastpath empty strings
|
|
if (+end == start) {
|
|
return '';
|
|
}
|
|
|
|
switch (encoding) {
|
|
case 'hex':
|
|
return this.hexSlice(start, end);
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
return this.utf8Slice(start, end);
|
|
|
|
case 'ascii':
|
|
return this.asciiSlice(start, end);
|
|
|
|
case 'binary':
|
|
return this.binarySlice(start, end);
|
|
|
|
case 'base64':
|
|
return this.base64Slice(start, end);
|
|
|
|
case 'ucs2':
|
|
case 'ucs-2':
|
|
return this.ucs2Slice(start, end);
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
};
|
|
|
|
|
|
SlowBuffer.prototype.hexWrite = function(string, offset, length) {
|
|
offset = +offset || 0;
|
|
var remaining = this.length - offset;
|
|
if (!length) {
|
|
length = remaining;
|
|
} else {
|
|
length = +length;
|
|
if (length > remaining) {
|
|
length = remaining;
|
|
}
|
|
}
|
|
|
|
// must be an even number of digits
|
|
var strLen = string.length;
|
|
if (strLen % 2) {
|
|
throw new Error('Invalid hex string');
|
|
}
|
|
if (length > strLen / 2) {
|
|
length = strLen / 2;
|
|
}
|
|
for (var i = 0; i < length; i++) {
|
|
var byte = parseInt(string.substr(i * 2, 2), 16);
|
|
if (isNaN(byte)) throw new Error('Invalid hex string');
|
|
this[offset + i] = byte;
|
|
}
|
|
SlowBuffer._charsWritten = i * 2;
|
|
return i;
|
|
};
|
|
|
|
|
|
SlowBuffer.prototype.write = function(string, offset, length, encoding) {
|
|
// Support both (string, offset, length, encoding)
|
|
// and the legacy (string, encoding, offset, length)
|
|
if (isFinite(offset)) {
|
|
if (!isFinite(length)) {
|
|
encoding = length;
|
|
length = undefined;
|
|
}
|
|
} else { // legacy
|
|
var swap = encoding;
|
|
encoding = offset;
|
|
offset = length;
|
|
length = swap;
|
|
}
|
|
|
|
offset = +offset || 0;
|
|
var remaining = this.length - offset;
|
|
if (!length) {
|
|
length = remaining;
|
|
} else {
|
|
length = +length;
|
|
if (length > remaining) {
|
|
length = remaining;
|
|
}
|
|
}
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
|
|
|
switch (encoding) {
|
|
case 'hex':
|
|
return this.hexWrite(string, offset, length);
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
return this.utf8Write(string, offset, length);
|
|
|
|
case 'ascii':
|
|
return this.asciiWrite(string, offset, length);
|
|
|
|
case 'binary':
|
|
return this.binaryWrite(string, offset, length);
|
|
|
|
case 'base64':
|
|
return this.base64Write(string, offset, length);
|
|
|
|
case 'ucs2':
|
|
case 'ucs-2':
|
|
return this.ucs2Write(string, offset, length);
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
};
|
|
|
|
|
|
// slice(start, end)
|
|
SlowBuffer.prototype.slice = function(start, end) {
|
|
if (end === undefined) end = this.length;
|
|
|
|
if (end > this.length) {
|
|
throw new Error('oob');
|
|
}
|
|
if (start > end) {
|
|
throw new Error('oob');
|
|
}
|
|
|
|
return new Buffer(this, end - start, +start);
|
|
};
|
|
|
|
SlowBuffer.prototype.copy = function(target, targetstart, sourcestart, sourceend) {
|
|
var temp = [];
|
|
for (var i=sourcestart; i<sourceend; i++) {
|
|
assert.ok(typeof this[i] !== 'undefined', "copying undefined buffer bytes!");
|
|
temp.push(this[i]);
|
|
}
|
|
|
|
for (var i=targetstart; i<targetstart+temp.length; i++) {
|
|
target[i] = temp[i-targetstart];
|
|
}
|
|
};
|
|
|
|
SlowBuffer.prototype.fill = function(value, start, end) {
|
|
if (end > this.length) {
|
|
throw new Error('oob');
|
|
}
|
|
if (start > end) {
|
|
throw new Error('oob');
|
|
}
|
|
|
|
for (var i = start; i < end; i++) {
|
|
this[i] = value;
|
|
}
|
|
}
|
|
|
|
function coerce(length) {
|
|
// Coerce length to a number (possibly NaN), round up
|
|
// in case it's fractional (e.g. 123.456) then do a
|
|
// double negate to coerce a NaN to 0. Easy, right?
|
|
length = ~~Math.ceil(+length);
|
|
return length < 0 ? 0 : length;
|
|
}
|
|
|
|
|
|
// Buffer
|
|
|
|
function Buffer(subject, encoding, offset) {
|
|
if (!(this instanceof Buffer)) {
|
|
return new Buffer(subject, encoding, offset);
|
|
}
|
|
|
|
var type;
|
|
|
|
// Are we slicing?
|
|
if (typeof offset === 'number') {
|
|
this.length = coerce(encoding);
|
|
this.parent = subject;
|
|
this.offset = offset;
|
|
} else {
|
|
// Find the length
|
|
switch (type = typeof subject) {
|
|
case 'number':
|
|
this.length = coerce(subject);
|
|
break;
|
|
|
|
case 'string':
|
|
this.length = Buffer.byteLength(subject, encoding);
|
|
break;
|
|
|
|
case 'object': // Assume object is an array
|
|
this.length = coerce(subject.length);
|
|
break;
|
|
|
|
default:
|
|
throw new Error('First argument needs to be a number, ' +
|
|
'array or string.');
|
|
}
|
|
|
|
if (this.length > Buffer.poolSize) {
|
|
// Big buffer, just alloc one.
|
|
this.parent = new SlowBuffer(this.length);
|
|
this.offset = 0;
|
|
|
|
} else {
|
|
// Small buffer.
|
|
if (!pool || pool.length - pool.used < this.length) allocPool();
|
|
this.parent = pool;
|
|
this.offset = pool.used;
|
|
pool.used += this.length;
|
|
}
|
|
|
|
// Treat array-ish objects as a byte array.
|
|
if (isArrayIsh(subject)) {
|
|
for (var i = 0; i < this.length; i++) {
|
|
if (subject instanceof Buffer) {
|
|
this.parent[i + this.offset] = subject.readUInt8(i);
|
|
}
|
|
else {
|
|
this.parent[i + this.offset] = subject[i];
|
|
}
|
|
}
|
|
} else if (type == 'string') {
|
|
// We are a string
|
|
this.length = this.write(subject, 0, encoding);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
function isArrayIsh(subject) {
|
|
return Array.isArray(subject) || Buffer.isBuffer(subject) ||
|
|
subject && typeof subject === 'object' &&
|
|
typeof subject.length === 'number';
|
|
}
|
|
|
|
exports.SlowBuffer = SlowBuffer;
|
|
exports.Buffer = Buffer;
|
|
|
|
Buffer.poolSize = 8 * 1024;
|
|
var pool;
|
|
|
|
function allocPool() {
|
|
pool = new SlowBuffer(Buffer.poolSize);
|
|
pool.used = 0;
|
|
}
|
|
|
|
|
|
// Static methods
|
|
Buffer.isBuffer = function isBuffer(b) {
|
|
return b instanceof Buffer || b instanceof SlowBuffer;
|
|
};
|
|
|
|
Buffer.concat = function (list, totalLength) {
|
|
if (!Array.isArray(list)) {
|
|
throw new Error("Usage: Buffer.concat(list, [totalLength])\n \
|
|
list should be an Array.");
|
|
}
|
|
|
|
if (list.length === 0) {
|
|
return new Buffer(0);
|
|
} else if (list.length === 1) {
|
|
return list[0];
|
|
}
|
|
|
|
if (typeof totalLength !== 'number') {
|
|
totalLength = 0;
|
|
for (var i = 0; i < list.length; i++) {
|
|
var buf = list[i];
|
|
totalLength += buf.length;
|
|
}
|
|
}
|
|
|
|
var buffer = new Buffer(totalLength);
|
|
var pos = 0;
|
|
for (var i = 0; i < list.length; i++) {
|
|
var buf = list[i];
|
|
buf.copy(buffer, pos);
|
|
pos += buf.length;
|
|
}
|
|
return buffer;
|
|
};
|
|
|
|
// Inspect
|
|
Buffer.prototype.inspect = function inspect() {
|
|
var out = [],
|
|
len = this.length;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
out[i] = toHex(this.parent[i + this.offset]);
|
|
if (i == exports.INSPECT_MAX_BYTES) {
|
|
out[i + 1] = '...';
|
|
break;
|
|
}
|
|
}
|
|
|
|
return '<Buffer ' + out.join(' ') + '>';
|
|
};
|
|
|
|
|
|
Buffer.prototype.get = function get(i) {
|
|
if (i < 0 || i >= this.length) throw new Error('oob');
|
|
return this.parent[this.offset + i];
|
|
};
|
|
|
|
|
|
Buffer.prototype.set = function set(i, v) {
|
|
if (i < 0 || i >= this.length) throw new Error('oob');
|
|
return this.parent[this.offset + i] = v;
|
|
};
|
|
|
|
|
|
// write(string, offset = 0, length = buffer.length-offset, encoding = 'utf8')
|
|
Buffer.prototype.write = function(string, offset, length, encoding) {
|
|
// Support both (string, offset, length, encoding)
|
|
// and the legacy (string, encoding, offset, length)
|
|
if (isFinite(offset)) {
|
|
if (!isFinite(length)) {
|
|
encoding = length;
|
|
length = undefined;
|
|
}
|
|
} else { // legacy
|
|
var swap = encoding;
|
|
encoding = offset;
|
|
offset = length;
|
|
length = swap;
|
|
}
|
|
|
|
offset = +offset || 0;
|
|
var remaining = this.length - offset;
|
|
if (!length) {
|
|
length = remaining;
|
|
} else {
|
|
length = +length;
|
|
if (length > remaining) {
|
|
length = remaining;
|
|
}
|
|
}
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
|
|
|
var ret;
|
|
switch (encoding) {
|
|
case 'hex':
|
|
ret = this.parent.hexWrite(string, this.offset + offset, length);
|
|
break;
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
ret = this.parent.utf8Write(string, this.offset + offset, length);
|
|
break;
|
|
|
|
case 'ascii':
|
|
ret = this.parent.asciiWrite(string, this.offset + offset, length);
|
|
break;
|
|
|
|
case 'binary':
|
|
ret = this.parent.binaryWrite(string, this.offset + offset, length);
|
|
break;
|
|
|
|
case 'base64':
|
|
// Warning: maxLength not taken into account in base64Write
|
|
ret = this.parent.base64Write(string, this.offset + offset, length);
|
|
break;
|
|
|
|
case 'ucs2':
|
|
case 'ucs-2':
|
|
ret = this.parent.ucs2Write(string, this.offset + offset, length);
|
|
break;
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
|
|
Buffer._charsWritten = SlowBuffer._charsWritten;
|
|
|
|
return ret;
|
|
};
|
|
|
|
|
|
// toString(encoding, start=0, end=buffer.length)
|
|
Buffer.prototype.toString = function(encoding, start, end) {
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
|
|
|
if (typeof start == 'undefined' || start < 0) {
|
|
start = 0;
|
|
} else if (start > this.length) {
|
|
start = this.length;
|
|
}
|
|
|
|
if (typeof end == 'undefined' || end > this.length) {
|
|
end = this.length;
|
|
} else if (end < 0) {
|
|
end = 0;
|
|
}
|
|
|
|
start = start + this.offset;
|
|
end = end + this.offset;
|
|
|
|
switch (encoding) {
|
|
case 'hex':
|
|
return this.parent.hexSlice(start, end);
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
return this.parent.utf8Slice(start, end);
|
|
|
|
case 'ascii':
|
|
return this.parent.asciiSlice(start, end);
|
|
|
|
case 'binary':
|
|
return this.parent.binarySlice(start, end);
|
|
|
|
case 'base64':
|
|
return this.parent.base64Slice(start, end);
|
|
|
|
case 'ucs2':
|
|
case 'ucs-2':
|
|
return this.parent.ucs2Slice(start, end);
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
};
|
|
|
|
|
|
// byteLength
|
|
Buffer.byteLength = SlowBuffer.byteLength;
|
|
|
|
|
|
// fill(value, start=0, end=buffer.length)
|
|
Buffer.prototype.fill = function fill(value, start, end) {
|
|
value || (value = 0);
|
|
start || (start = 0);
|
|
end || (end = this.length);
|
|
|
|
if (typeof value === 'string') {
|
|
value = value.charCodeAt(0);
|
|
}
|
|
if (!(typeof value === 'number') || isNaN(value)) {
|
|
throw new Error('value is not a number');
|
|
}
|
|
|
|
if (end < start) throw new Error('end < start');
|
|
|
|
// Fill 0 bytes; we're done
|
|
if (end === start) return 0;
|
|
if (this.length == 0) return 0;
|
|
|
|
if (start < 0 || start >= this.length) {
|
|
throw new Error('start out of bounds');
|
|
}
|
|
|
|
if (end < 0 || end > this.length) {
|
|
throw new Error('end out of bounds');
|
|
}
|
|
|
|
return this.parent.fill(value,
|
|
start + this.offset,
|
|
end + this.offset);
|
|
};
|
|
|
|
|
|
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
|
|
Buffer.prototype.copy = function(target, target_start, start, end) {
|
|
var source = this;
|
|
start || (start = 0);
|
|
end || (end = this.length);
|
|
target_start || (target_start = 0);
|
|
|
|
if (end < start) throw new Error('sourceEnd < sourceStart');
|
|
|
|
// Copy 0 bytes; we're done
|
|
if (end === start) return 0;
|
|
if (target.length == 0 || source.length == 0) return 0;
|
|
|
|
if (target_start < 0 || target_start >= target.length) {
|
|
throw new Error('targetStart out of bounds');
|
|
}
|
|
|
|
if (start < 0 || start >= source.length) {
|
|
throw new Error('sourceStart out of bounds');
|
|
}
|
|
|
|
if (end < 0 || end > source.length) {
|
|
throw new Error('sourceEnd out of bounds');
|
|
}
|
|
|
|
// Are we oob?
|
|
if (end > this.length) {
|
|
end = this.length;
|
|
}
|
|
|
|
if (target.length - target_start < end - start) {
|
|
end = target.length - target_start + start;
|
|
}
|
|
|
|
return this.parent.copy(target.parent,
|
|
target_start + target.offset,
|
|
start + this.offset,
|
|
end + this.offset);
|
|
};
|
|
|
|
|
|
// slice(start, end)
|
|
Buffer.prototype.slice = function(start, end) {
|
|
if (end === undefined) end = this.length;
|
|
if (end > this.length) throw new Error('oob');
|
|
if (start > end) throw new Error('oob');
|
|
|
|
return new Buffer(this.parent, end - start, +start + this.offset);
|
|
};
|
|
|
|
|
|
// Legacy methods for backwards compatibility.
|
|
|
|
Buffer.prototype.utf8Slice = function(start, end) {
|
|
return this.toString('utf8', start, end);
|
|
};
|
|
|
|
Buffer.prototype.binarySlice = function(start, end) {
|
|
return this.toString('binary', start, end);
|
|
};
|
|
|
|
Buffer.prototype.asciiSlice = function(start, end) {
|
|
return this.toString('ascii', start, end);
|
|
};
|
|
|
|
Buffer.prototype.utf8Write = function(string, offset) {
|
|
return this.write(string, offset, 'utf8');
|
|
};
|
|
|
|
Buffer.prototype.binaryWrite = function(string, offset) {
|
|
return this.write(string, offset, 'binary');
|
|
};
|
|
|
|
Buffer.prototype.asciiWrite = function(string, offset) {
|
|
return this.write(string, offset, 'ascii');
|
|
};
|
|
|
|
Buffer.prototype.readUInt8 = function(offset, noAssert) {
|
|
var buffer = this;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
if (offset >= buffer.length) return;
|
|
|
|
return buffer.parent[buffer.offset + offset];
|
|
};
|
|
|
|
function readUInt16(buffer, offset, isBigEndian, noAssert) {
|
|
var val = 0;
|
|
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
if (offset >= buffer.length) return 0;
|
|
|
|
if (isBigEndian) {
|
|
val = buffer.parent[buffer.offset + offset] << 8;
|
|
if (offset + 1 < buffer.length) {
|
|
val |= buffer.parent[buffer.offset + offset + 1];
|
|
}
|
|
} else {
|
|
val = buffer.parent[buffer.offset + offset];
|
|
if (offset + 1 < buffer.length) {
|
|
val |= buffer.parent[buffer.offset + offset + 1] << 8;
|
|
}
|
|
}
|
|
|
|
return val;
|
|
}
|
|
|
|
Buffer.prototype.readUInt16LE = function(offset, noAssert) {
|
|
return readUInt16(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readUInt16BE = function(offset, noAssert) {
|
|
return readUInt16(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readUInt32(buffer, offset, isBigEndian, noAssert) {
|
|
var val = 0;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
if (offset >= buffer.length) return 0;
|
|
|
|
if (isBigEndian) {
|
|
if (offset + 1 < buffer.length)
|
|
val = buffer.parent[buffer.offset + offset + 1] << 16;
|
|
if (offset + 2 < buffer.length)
|
|
val |= buffer.parent[buffer.offset + offset + 2] << 8;
|
|
if (offset + 3 < buffer.length)
|
|
val |= buffer.parent[buffer.offset + offset + 3];
|
|
val = val + (buffer.parent[buffer.offset + offset] << 24 >>> 0);
|
|
} else {
|
|
if (offset + 2 < buffer.length)
|
|
val = buffer.parent[buffer.offset + offset + 2] << 16;
|
|
if (offset + 1 < buffer.length)
|
|
val |= buffer.parent[buffer.offset + offset + 1] << 8;
|
|
val |= buffer.parent[buffer.offset + offset];
|
|
if (offset + 3 < buffer.length)
|
|
val = val + (buffer.parent[buffer.offset + offset + 3] << 24 >>> 0);
|
|
}
|
|
|
|
return val;
|
|
}
|
|
|
|
Buffer.prototype.readUInt32LE = function(offset, noAssert) {
|
|
return readUInt32(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readUInt32BE = function(offset, noAssert) {
|
|
return readUInt32(this, offset, true, noAssert);
|
|
};
|
|
|
|
|
|
/*
|
|
* Signed integer types, yay team! A reminder on how two's complement actually
|
|
* works. The first bit is the signed bit, i.e. tells us whether or not the
|
|
* number should be positive or negative. If the two's complement value is
|
|
* positive, then we're done, as it's equivalent to the unsigned representation.
|
|
*
|
|
* Now if the number is positive, you're pretty much done, you can just leverage
|
|
* the unsigned translations and return those. Unfortunately, negative numbers
|
|
* aren't quite that straightforward.
|
|
*
|
|
* At first glance, one might be inclined to use the traditional formula to
|
|
* translate binary numbers between the positive and negative values in two's
|
|
* complement. (Though it doesn't quite work for the most negative value)
|
|
* Mainly:
|
|
* - invert all the bits
|
|
* - add one to the result
|
|
*
|
|
* Of course, this doesn't quite work in Javascript. Take for example the value
|
|
* of -128. This could be represented in 16 bits (big-endian) as 0xff80. But of
|
|
* course, Javascript will do the following:
|
|
*
|
|
* > ~0xff80
|
|
* -65409
|
|
*
|
|
* Whoh there, Javascript, that's not quite right. But wait, according to
|
|
* Javascript that's perfectly correct. When Javascript ends up seeing the
|
|
* constant 0xff80, it has no notion that it is actually a signed number. It
|
|
* assumes that we've input the unsigned value 0xff80. Thus, when it does the
|
|
* binary negation, it casts it into a signed value, (positive 0xff80). Then
|
|
* when you perform binary negation on that, it turns it into a negative number.
|
|
*
|
|
* Instead, we're going to have to use the following general formula, that works
|
|
* in a rather Javascript friendly way. I'm glad we don't support this kind of
|
|
* weird numbering scheme in the kernel.
|
|
*
|
|
* (BIT-MAX - (unsigned)val + 1) * -1
|
|
*
|
|
* The astute observer, may think that this doesn't make sense for 8-bit numbers
|
|
* (really it isn't necessary for them). However, when you get 16-bit numbers,
|
|
* you do. Let's go back to our prior example and see how this will look:
|
|
*
|
|
* (0xffff - 0xff80 + 1) * -1
|
|
* (0x007f + 1) * -1
|
|
* (0x0080) * -1
|
|
*/
|
|
Buffer.prototype.readInt8 = function(offset, noAssert) {
|
|
var buffer = this;
|
|
var neg;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
if (offset >= buffer.length) return;
|
|
|
|
neg = buffer.parent[buffer.offset + offset] & 0x80;
|
|
if (!neg) {
|
|
return (buffer.parent[buffer.offset + offset]);
|
|
}
|
|
|
|
return ((0xff - buffer.parent[buffer.offset + offset] + 1) * -1);
|
|
};
|
|
|
|
function readInt16(buffer, offset, isBigEndian, noAssert) {
|
|
var neg, val;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
val = readUInt16(buffer, offset, isBigEndian, noAssert);
|
|
neg = val & 0x8000;
|
|
if (!neg) {
|
|
return val;
|
|
}
|
|
|
|
return (0xffff - val + 1) * -1;
|
|
}
|
|
|
|
Buffer.prototype.readInt16LE = function(offset, noAssert) {
|
|
return readInt16(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readInt16BE = function(offset, noAssert) {
|
|
return readInt16(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readInt32(buffer, offset, isBigEndian, noAssert) {
|
|
var neg, val;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
val = readUInt32(buffer, offset, isBigEndian, noAssert);
|
|
neg = val & 0x80000000;
|
|
if (!neg) {
|
|
return (val);
|
|
}
|
|
|
|
return (0xffffffff - val + 1) * -1;
|
|
}
|
|
|
|
Buffer.prototype.readInt32LE = function(offset, noAssert) {
|
|
return readInt32(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readInt32BE = function(offset, noAssert) {
|
|
return readInt32(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readFloat(buffer, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
return require('./buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
|
|
23, 4);
|
|
}
|
|
|
|
Buffer.prototype.readFloatLE = function(offset, noAssert) {
|
|
return readFloat(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readFloatBE = function(offset, noAssert) {
|
|
return readFloat(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readDouble(buffer, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset + 7 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
return require('./buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
|
|
52, 8);
|
|
}
|
|
|
|
Buffer.prototype.readDoubleLE = function(offset, noAssert) {
|
|
return readDouble(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readDoubleBE = function(offset, noAssert) {
|
|
return readDouble(this, offset, true, noAssert);
|
|
};
|
|
|
|
|
|
/*
|
|
* We have to make sure that the value is a valid integer. This means that it is
|
|
* non-negative. It has no fractional component and that it does not exceed the
|
|
* maximum allowed value.
|
|
*
|
|
* value The number to check for validity
|
|
*
|
|
* max The maximum value
|
|
*/
|
|
function verifuint(value, max) {
|
|
assert.ok(typeof (value) == 'number',
|
|
'cannot write a non-number as a number');
|
|
|
|
assert.ok(value >= 0,
|
|
'specified a negative value for writing an unsigned value');
|
|
|
|
assert.ok(value <= max, 'value is larger than maximum value for type');
|
|
|
|
assert.ok(Math.floor(value) === value, 'value has a fractional component');
|
|
}
|
|
|
|
Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
|
|
var buffer = this;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'trying to write beyond buffer length');
|
|
|
|
verifuint(value, 0xff);
|
|
}
|
|
|
|
if (offset < buffer.length) {
|
|
buffer.parent[buffer.offset + offset] = value;
|
|
}
|
|
};
|
|
|
|
function writeUInt16(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'trying to write beyond buffer length');
|
|
|
|
verifuint(value, 0xffff);
|
|
}
|
|
|
|
for (var i = 0; i < Math.min(buffer.length - offset, 2); i++) {
|
|
buffer.parent[buffer.offset + offset + i] =
|
|
(value & (0xff << (8 * (isBigEndian ? 1 - i : i)))) >>>
|
|
(isBigEndian ? 1 - i : i) * 8;
|
|
}
|
|
|
|
}
|
|
|
|
Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
|
|
writeUInt16(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
|
|
writeUInt16(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeUInt32(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'trying to write beyond buffer length');
|
|
|
|
verifuint(value, 0xffffffff);
|
|
}
|
|
|
|
for (var i = 0; i < Math.min(buffer.length - offset, 4); i++) {
|
|
buffer.parent[buffer.offset + offset + i] =
|
|
(value >>> (isBigEndian ? 3 - i : i) * 8) & 0xff;
|
|
}
|
|
}
|
|
|
|
Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
|
|
writeUInt32(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
|
|
writeUInt32(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
|
|
/*
|
|
* We now move onto our friends in the signed number category. Unlike unsigned
|
|
* numbers, we're going to have to worry a bit more about how we put values into
|
|
* arrays. Since we are only worrying about signed 32-bit values, we're in
|
|
* slightly better shape. Unfortunately, we really can't do our favorite binary
|
|
* & in this system. It really seems to do the wrong thing. For example:
|
|
*
|
|
* > -32 & 0xff
|
|
* 224
|
|
*
|
|
* What's happening above is really: 0xe0 & 0xff = 0xe0. However, the results of
|
|
* this aren't treated as a signed number. Ultimately a bad thing.
|
|
*
|
|
* What we're going to want to do is basically create the unsigned equivalent of
|
|
* our representation and pass that off to the wuint* functions. To do that
|
|
* we're going to do the following:
|
|
*
|
|
* - if the value is positive
|
|
* we can pass it directly off to the equivalent wuint
|
|
* - if the value is negative
|
|
* we do the following computation:
|
|
* mb + val + 1, where
|
|
* mb is the maximum unsigned value in that byte size
|
|
* val is the Javascript negative integer
|
|
*
|
|
*
|
|
* As a concrete value, take -128. In signed 16 bits this would be 0xff80. If
|
|
* you do out the computations:
|
|
*
|
|
* 0xffff - 128 + 1
|
|
* 0xffff - 127
|
|
* 0xff80
|
|
*
|
|
* You can then encode this value as the signed version. This is really rather
|
|
* hacky, but it should work and get the job done which is our goal here.
|
|
*/
|
|
|
|
/*
|
|
* A series of checks to make sure we actually have a signed 32-bit number
|
|
*/
|
|
function verifsint(value, max, min) {
|
|
assert.ok(typeof (value) == 'number',
|
|
'cannot write a non-number as a number');
|
|
|
|
assert.ok(value <= max, 'value larger than maximum allowed value');
|
|
|
|
assert.ok(value >= min, 'value smaller than minimum allowed value');
|
|
|
|
assert.ok(Math.floor(value) === value, 'value has a fractional component');
|
|
}
|
|
|
|
function verifIEEE754(value, max, min) {
|
|
assert.ok(typeof (value) == 'number',
|
|
'cannot write a non-number as a number');
|
|
|
|
assert.ok(value <= max, 'value larger than maximum allowed value');
|
|
|
|
assert.ok(value >= min, 'value smaller than minimum allowed value');
|
|
}
|
|
|
|
Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
|
|
var buffer = this;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifsint(value, 0x7f, -0x80);
|
|
}
|
|
|
|
if (value >= 0) {
|
|
buffer.writeUInt8(value, offset, noAssert);
|
|
} else {
|
|
buffer.writeUInt8(0xff + value + 1, offset, noAssert);
|
|
}
|
|
};
|
|
|
|
function writeInt16(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifsint(value, 0x7fff, -0x8000);
|
|
}
|
|
|
|
if (value >= 0) {
|
|
writeUInt16(buffer, value, offset, isBigEndian, noAssert);
|
|
} else {
|
|
writeUInt16(buffer, 0xffff + value + 1, offset, isBigEndian, noAssert);
|
|
}
|
|
}
|
|
|
|
Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
|
|
writeInt16(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
|
|
writeInt16(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeInt32(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifsint(value, 0x7fffffff, -0x80000000);
|
|
}
|
|
|
|
if (value >= 0) {
|
|
writeUInt32(buffer, value, offset, isBigEndian, noAssert);
|
|
} else {
|
|
writeUInt32(buffer, 0xffffffff + value + 1, offset, isBigEndian, noAssert);
|
|
}
|
|
}
|
|
|
|
Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
|
|
writeInt32(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
|
|
writeInt32(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeFloat(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38);
|
|
}
|
|
|
|
require('./buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
|
|
23, 4);
|
|
}
|
|
|
|
Buffer.prototype.writeFloatLE = function(value, offset, noAssert) {
|
|
writeFloat(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeFloatBE = function(value, offset, noAssert) {
|
|
writeFloat(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeDouble(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 7 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308);
|
|
}
|
|
|
|
require('./buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
|
|
52, 8);
|
|
}
|
|
|
|
Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) {
|
|
writeDouble(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) {
|
|
writeDouble(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
SlowBuffer.prototype.readUInt8 = Buffer.prototype.readUInt8;
|
|
SlowBuffer.prototype.readUInt16LE = Buffer.prototype.readUInt16LE;
|
|
SlowBuffer.prototype.readUInt16BE = Buffer.prototype.readUInt16BE;
|
|
SlowBuffer.prototype.readUInt32LE = Buffer.prototype.readUInt32LE;
|
|
SlowBuffer.prototype.readUInt32BE = Buffer.prototype.readUInt32BE;
|
|
SlowBuffer.prototype.readInt8 = Buffer.prototype.readInt8;
|
|
SlowBuffer.prototype.readInt16LE = Buffer.prototype.readInt16LE;
|
|
SlowBuffer.prototype.readInt16BE = Buffer.prototype.readInt16BE;
|
|
SlowBuffer.prototype.readInt32LE = Buffer.prototype.readInt32LE;
|
|
SlowBuffer.prototype.readInt32BE = Buffer.prototype.readInt32BE;
|
|
SlowBuffer.prototype.readFloatLE = Buffer.prototype.readFloatLE;
|
|
SlowBuffer.prototype.readFloatBE = Buffer.prototype.readFloatBE;
|
|
SlowBuffer.prototype.readDoubleLE = Buffer.prototype.readDoubleLE;
|
|
SlowBuffer.prototype.readDoubleBE = Buffer.prototype.readDoubleBE;
|
|
SlowBuffer.prototype.writeUInt8 = Buffer.prototype.writeUInt8;
|
|
SlowBuffer.prototype.writeUInt16LE = Buffer.prototype.writeUInt16LE;
|
|
SlowBuffer.prototype.writeUInt16BE = Buffer.prototype.writeUInt16BE;
|
|
SlowBuffer.prototype.writeUInt32LE = Buffer.prototype.writeUInt32LE;
|
|
SlowBuffer.prototype.writeUInt32BE = Buffer.prototype.writeUInt32BE;
|
|
SlowBuffer.prototype.writeInt8 = Buffer.prototype.writeInt8;
|
|
SlowBuffer.prototype.writeInt16LE = Buffer.prototype.writeInt16LE;
|
|
SlowBuffer.prototype.writeInt16BE = Buffer.prototype.writeInt16BE;
|
|
SlowBuffer.prototype.writeInt32LE = Buffer.prototype.writeInt32LE;
|
|
SlowBuffer.prototype.writeInt32BE = Buffer.prototype.writeInt32BE;
|
|
SlowBuffer.prototype.writeFloatLE = Buffer.prototype.writeFloatLE;
|
|
SlowBuffer.prototype.writeFloatBE = Buffer.prototype.writeFloatBE;
|
|
SlowBuffer.prototype.writeDoubleLE = Buffer.prototype.writeDoubleLE;
|
|
SlowBuffer.prototype.writeDoubleBE = Buffer.prototype.writeDoubleBE;
|
|
|
|
},{"assert":1,"./buffer_ieee754":5,"base64-js":7}],7:[function(require,module,exports){
|
|
(function (exports) {
|
|
'use strict';
|
|
|
|
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
|
|
function b64ToByteArray(b64) {
|
|
var i, j, l, tmp, placeHolders, arr;
|
|
|
|
if (b64.length % 4 > 0) {
|
|
throw 'Invalid string. Length must be a multiple of 4';
|
|
}
|
|
|
|
// the number of equal signs (place holders)
|
|
// if there are two placeholders, than the two characters before it
|
|
// represent one byte
|
|
// if there is only one, then the three characters before it represent 2 bytes
|
|
// this is just a cheap hack to not do indexOf twice
|
|
placeHolders = b64.indexOf('=');
|
|
placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0;
|
|
|
|
// base64 is 4/3 + up to two characters of the original data
|
|
arr = [];//new Uint8Array(b64.length * 3 / 4 - placeHolders);
|
|
|
|
// if there are placeholders, only get up to the last complete 4 chars
|
|
l = placeHolders > 0 ? b64.length - 4 : b64.length;
|
|
|
|
for (i = 0, j = 0; i < l; i += 4, j += 3) {
|
|
tmp = (lookup.indexOf(b64[i]) << 18) | (lookup.indexOf(b64[i + 1]) << 12) | (lookup.indexOf(b64[i + 2]) << 6) | lookup.indexOf(b64[i + 3]);
|
|
arr.push((tmp & 0xFF0000) >> 16);
|
|
arr.push((tmp & 0xFF00) >> 8);
|
|
arr.push(tmp & 0xFF);
|
|
}
|
|
|
|
if (placeHolders === 2) {
|
|
tmp = (lookup.indexOf(b64[i]) << 2) | (lookup.indexOf(b64[i + 1]) >> 4);
|
|
arr.push(tmp & 0xFF);
|
|
} else if (placeHolders === 1) {
|
|
tmp = (lookup.indexOf(b64[i]) << 10) | (lookup.indexOf(b64[i + 1]) << 4) | (lookup.indexOf(b64[i + 2]) >> 2);
|
|
arr.push((tmp >> 8) & 0xFF);
|
|
arr.push(tmp & 0xFF);
|
|
}
|
|
|
|
return arr;
|
|
}
|
|
|
|
function uint8ToBase64(uint8) {
|
|
var i,
|
|
extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
|
|
output = "",
|
|
temp, length;
|
|
|
|
function tripletToBase64 (num) {
|
|
return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F];
|
|
};
|
|
|
|
// go through the array every three bytes, we'll deal with trailing stuff later
|
|
for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
|
|
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
|
|
output += tripletToBase64(temp);
|
|
}
|
|
|
|
// pad the end with zeros, but make sure to not forget the extra bytes
|
|
switch (extraBytes) {
|
|
case 1:
|
|
temp = uint8[uint8.length - 1];
|
|
output += lookup[temp >> 2];
|
|
output += lookup[(temp << 4) & 0x3F];
|
|
output += '==';
|
|
break;
|
|
case 2:
|
|
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]);
|
|
output += lookup[temp >> 10];
|
|
output += lookup[(temp >> 4) & 0x3F];
|
|
output += lookup[(temp << 2) & 0x3F];
|
|
output += '=';
|
|
break;
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
module.exports.toByteArray = b64ToByteArray;
|
|
module.exports.fromByteArray = uint8ToBase64;
|
|
}());
|
|
|
|
},{}],8:[function(require,module,exports){
|
|
exports.readIEEE754 = function(buffer, offset, isBE, mLen, nBytes) {
|
|
var e, m,
|
|
eLen = nBytes * 8 - mLen - 1,
|
|
eMax = (1 << eLen) - 1,
|
|
eBias = eMax >> 1,
|
|
nBits = -7,
|
|
i = isBE ? 0 : (nBytes - 1),
|
|
d = isBE ? 1 : -1,
|
|
s = buffer[offset + i];
|
|
|
|
i += d;
|
|
|
|
e = s & ((1 << (-nBits)) - 1);
|
|
s >>= (-nBits);
|
|
nBits += eLen;
|
|
for (; nBits > 0; e = e * 256 + buffer[offset + i], i += d, nBits -= 8);
|
|
|
|
m = e & ((1 << (-nBits)) - 1);
|
|
e >>= (-nBits);
|
|
nBits += mLen;
|
|
for (; nBits > 0; m = m * 256 + buffer[offset + i], i += d, nBits -= 8);
|
|
|
|
if (e === 0) {
|
|
e = 1 - eBias;
|
|
} else if (e === eMax) {
|
|
return m ? NaN : ((s ? -1 : 1) * Infinity);
|
|
} else {
|
|
m = m + Math.pow(2, mLen);
|
|
e = e - eBias;
|
|
}
|
|
return (s ? -1 : 1) * m * Math.pow(2, e - mLen);
|
|
};
|
|
|
|
exports.writeIEEE754 = function(buffer, value, offset, isBE, mLen, nBytes) {
|
|
var e, m, c,
|
|
eLen = nBytes * 8 - mLen - 1,
|
|
eMax = (1 << eLen) - 1,
|
|
eBias = eMax >> 1,
|
|
rt = (mLen === 23 ? Math.pow(2, -24) - Math.pow(2, -77) : 0),
|
|
i = isBE ? (nBytes - 1) : 0,
|
|
d = isBE ? -1 : 1,
|
|
s = value < 0 || (value === 0 && 1 / value < 0) ? 1 : 0;
|
|
|
|
value = Math.abs(value);
|
|
|
|
if (isNaN(value) || value === Infinity) {
|
|
m = isNaN(value) ? 1 : 0;
|
|
e = eMax;
|
|
} else {
|
|
e = Math.floor(Math.log(value) / Math.LN2);
|
|
if (value * (c = Math.pow(2, -e)) < 1) {
|
|
e--;
|
|
c *= 2;
|
|
}
|
|
if (e + eBias >= 1) {
|
|
value += rt / c;
|
|
} else {
|
|
value += rt * Math.pow(2, 1 - eBias);
|
|
}
|
|
if (value * c >= 2) {
|
|
e++;
|
|
c /= 2;
|
|
}
|
|
|
|
if (e + eBias >= eMax) {
|
|
m = 0;
|
|
e = eMax;
|
|
} else if (e + eBias >= 1) {
|
|
m = (value * c - 1) * Math.pow(2, mLen);
|
|
e = e + eBias;
|
|
} else {
|
|
m = value * Math.pow(2, eBias - 1) * Math.pow(2, mLen);
|
|
e = 0;
|
|
}
|
|
}
|
|
|
|
for (; mLen >= 8; buffer[offset + i] = m & 0xff, i += d, m /= 256, mLen -= 8);
|
|
|
|
e = (e << mLen) | m;
|
|
eLen += mLen;
|
|
for (; eLen > 0; buffer[offset + i] = e & 0xff, i += d, e /= 256, eLen -= 8);
|
|
|
|
buffer[offset + i - d] |= s * 128;
|
|
};
|
|
|
|
},{}],3:[function(require,module,exports){
|
|
function SlowBuffer (size) {
|
|
this.length = size;
|
|
};
|
|
|
|
var assert = require('assert');
|
|
|
|
exports.INSPECT_MAX_BYTES = 50;
|
|
|
|
|
|
function toHex(n) {
|
|
if (n < 16) return '0' + n.toString(16);
|
|
return n.toString(16);
|
|
}
|
|
|
|
function utf8ToBytes(str) {
|
|
var byteArray = [];
|
|
for (var i = 0; i < str.length; i++)
|
|
if (str.charCodeAt(i) <= 0x7F)
|
|
byteArray.push(str.charCodeAt(i));
|
|
else {
|
|
var h = encodeURIComponent(str.charAt(i)).substr(1).split('%');
|
|
for (var j = 0; j < h.length; j++)
|
|
byteArray.push(parseInt(h[j], 16));
|
|
}
|
|
|
|
return byteArray;
|
|
}
|
|
|
|
function asciiToBytes(str) {
|
|
var byteArray = []
|
|
for (var i = 0; i < str.length; i++ )
|
|
// Node's code seems to be doing this and not & 0x7F..
|
|
byteArray.push( str.charCodeAt(i) & 0xFF );
|
|
|
|
return byteArray;
|
|
}
|
|
|
|
function base64ToBytes(str) {
|
|
return require("base64-js").toByteArray(str);
|
|
}
|
|
|
|
SlowBuffer.byteLength = function (str, encoding) {
|
|
switch (encoding || "utf8") {
|
|
case 'hex':
|
|
return str.length / 2;
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
return utf8ToBytes(str).length;
|
|
|
|
case 'ascii':
|
|
return str.length;
|
|
|
|
case 'base64':
|
|
return base64ToBytes(str).length;
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
};
|
|
|
|
function blitBuffer(src, dst, offset, length) {
|
|
var pos, i = 0;
|
|
while (i < length) {
|
|
if ((i+offset >= dst.length) || (i >= src.length))
|
|
break;
|
|
|
|
dst[i + offset] = src[i];
|
|
i++;
|
|
}
|
|
return i;
|
|
}
|
|
|
|
SlowBuffer.prototype.utf8Write = function (string, offset, length) {
|
|
var bytes, pos;
|
|
return SlowBuffer._charsWritten = blitBuffer(utf8ToBytes(string), this, offset, length);
|
|
};
|
|
|
|
SlowBuffer.prototype.asciiWrite = function (string, offset, length) {
|
|
var bytes, pos;
|
|
return SlowBuffer._charsWritten = blitBuffer(asciiToBytes(string), this, offset, length);
|
|
};
|
|
|
|
SlowBuffer.prototype.base64Write = function (string, offset, length) {
|
|
var bytes, pos;
|
|
return SlowBuffer._charsWritten = blitBuffer(base64ToBytes(string), this, offset, length);
|
|
};
|
|
|
|
SlowBuffer.prototype.base64Slice = function (start, end) {
|
|
var bytes = Array.prototype.slice.apply(this, arguments)
|
|
return require("base64-js").fromByteArray(bytes);
|
|
}
|
|
|
|
function decodeUtf8Char(str) {
|
|
try {
|
|
return decodeURIComponent(str);
|
|
} catch (err) {
|
|
return String.fromCharCode(0xFFFD); // UTF 8 invalid char
|
|
}
|
|
}
|
|
|
|
SlowBuffer.prototype.utf8Slice = function () {
|
|
var bytes = Array.prototype.slice.apply(this, arguments);
|
|
var res = "";
|
|
var tmp = "";
|
|
var i = 0;
|
|
while (i < bytes.length) {
|
|
if (bytes[i] <= 0x7F) {
|
|
res += decodeUtf8Char(tmp) + String.fromCharCode(bytes[i]);
|
|
tmp = "";
|
|
} else
|
|
tmp += "%" + bytes[i].toString(16);
|
|
|
|
i++;
|
|
}
|
|
|
|
return res + decodeUtf8Char(tmp);
|
|
}
|
|
|
|
SlowBuffer.prototype.asciiSlice = function () {
|
|
var bytes = Array.prototype.slice.apply(this, arguments);
|
|
var ret = "";
|
|
for (var i = 0; i < bytes.length; i++)
|
|
ret += String.fromCharCode(bytes[i]);
|
|
return ret;
|
|
}
|
|
|
|
SlowBuffer.prototype.inspect = function() {
|
|
var out = [],
|
|
len = this.length;
|
|
for (var i = 0; i < len; i++) {
|
|
out[i] = toHex(this[i]);
|
|
if (i == exports.INSPECT_MAX_BYTES) {
|
|
out[i + 1] = '...';
|
|
break;
|
|
}
|
|
}
|
|
return '<SlowBuffer ' + out.join(' ') + '>';
|
|
};
|
|
|
|
|
|
SlowBuffer.prototype.hexSlice = function(start, end) {
|
|
var len = this.length;
|
|
|
|
if (!start || start < 0) start = 0;
|
|
if (!end || end < 0 || end > len) end = len;
|
|
|
|
var out = '';
|
|
for (var i = start; i < end; i++) {
|
|
out += toHex(this[i]);
|
|
}
|
|
return out;
|
|
};
|
|
|
|
|
|
SlowBuffer.prototype.toString = function(encoding, start, end) {
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
|
start = +start || 0;
|
|
if (typeof end == 'undefined') end = this.length;
|
|
|
|
// Fastpath empty strings
|
|
if (+end == start) {
|
|
return '';
|
|
}
|
|
|
|
switch (encoding) {
|
|
case 'hex':
|
|
return this.hexSlice(start, end);
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
return this.utf8Slice(start, end);
|
|
|
|
case 'ascii':
|
|
return this.asciiSlice(start, end);
|
|
|
|
case 'binary':
|
|
return this.binarySlice(start, end);
|
|
|
|
case 'base64':
|
|
return this.base64Slice(start, end);
|
|
|
|
case 'ucs2':
|
|
case 'ucs-2':
|
|
return this.ucs2Slice(start, end);
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
};
|
|
|
|
|
|
SlowBuffer.prototype.hexWrite = function(string, offset, length) {
|
|
offset = +offset || 0;
|
|
var remaining = this.length - offset;
|
|
if (!length) {
|
|
length = remaining;
|
|
} else {
|
|
length = +length;
|
|
if (length > remaining) {
|
|
length = remaining;
|
|
}
|
|
}
|
|
|
|
// must be an even number of digits
|
|
var strLen = string.length;
|
|
if (strLen % 2) {
|
|
throw new Error('Invalid hex string');
|
|
}
|
|
if (length > strLen / 2) {
|
|
length = strLen / 2;
|
|
}
|
|
for (var i = 0; i < length; i++) {
|
|
var byte = parseInt(string.substr(i * 2, 2), 16);
|
|
if (isNaN(byte)) throw new Error('Invalid hex string');
|
|
this[offset + i] = byte;
|
|
}
|
|
SlowBuffer._charsWritten = i * 2;
|
|
return i;
|
|
};
|
|
|
|
|
|
SlowBuffer.prototype.write = function(string, offset, length, encoding) {
|
|
// Support both (string, offset, length, encoding)
|
|
// and the legacy (string, encoding, offset, length)
|
|
if (isFinite(offset)) {
|
|
if (!isFinite(length)) {
|
|
encoding = length;
|
|
length = undefined;
|
|
}
|
|
} else { // legacy
|
|
var swap = encoding;
|
|
encoding = offset;
|
|
offset = length;
|
|
length = swap;
|
|
}
|
|
|
|
offset = +offset || 0;
|
|
var remaining = this.length - offset;
|
|
if (!length) {
|
|
length = remaining;
|
|
} else {
|
|
length = +length;
|
|
if (length > remaining) {
|
|
length = remaining;
|
|
}
|
|
}
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
|
|
|
switch (encoding) {
|
|
case 'hex':
|
|
return this.hexWrite(string, offset, length);
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
return this.utf8Write(string, offset, length);
|
|
|
|
case 'ascii':
|
|
return this.asciiWrite(string, offset, length);
|
|
|
|
case 'binary':
|
|
return this.binaryWrite(string, offset, length);
|
|
|
|
case 'base64':
|
|
return this.base64Write(string, offset, length);
|
|
|
|
case 'ucs2':
|
|
case 'ucs-2':
|
|
return this.ucs2Write(string, offset, length);
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
};
|
|
|
|
|
|
// slice(start, end)
|
|
SlowBuffer.prototype.slice = function(start, end) {
|
|
if (end === undefined) end = this.length;
|
|
|
|
if (end > this.length) {
|
|
throw new Error('oob');
|
|
}
|
|
if (start > end) {
|
|
throw new Error('oob');
|
|
}
|
|
|
|
return new Buffer(this, end - start, +start);
|
|
};
|
|
|
|
SlowBuffer.prototype.copy = function(target, targetstart, sourcestart, sourceend) {
|
|
var temp = [];
|
|
for (var i=sourcestart; i<sourceend; i++) {
|
|
assert.ok(typeof this[i] !== 'undefined', "copying undefined buffer bytes!");
|
|
temp.push(this[i]);
|
|
}
|
|
|
|
for (var i=targetstart; i<targetstart+temp.length; i++) {
|
|
target[i] = temp[i-targetstart];
|
|
}
|
|
};
|
|
|
|
function coerce(length) {
|
|
// Coerce length to a number (possibly NaN), round up
|
|
// in case it's fractional (e.g. 123.456) then do a
|
|
// double negate to coerce a NaN to 0. Easy, right?
|
|
length = ~~Math.ceil(+length);
|
|
return length < 0 ? 0 : length;
|
|
}
|
|
|
|
|
|
// Buffer
|
|
|
|
function Buffer(subject, encoding, offset) {
|
|
if (!(this instanceof Buffer)) {
|
|
return new Buffer(subject, encoding, offset);
|
|
}
|
|
|
|
var type;
|
|
|
|
// Are we slicing?
|
|
if (typeof offset === 'number') {
|
|
this.length = coerce(encoding);
|
|
this.parent = subject;
|
|
this.offset = offset;
|
|
} else {
|
|
// Find the length
|
|
switch (type = typeof subject) {
|
|
case 'number':
|
|
this.length = coerce(subject);
|
|
break;
|
|
|
|
case 'string':
|
|
this.length = Buffer.byteLength(subject, encoding);
|
|
break;
|
|
|
|
case 'object': // Assume object is an array
|
|
this.length = coerce(subject.length);
|
|
break;
|
|
|
|
default:
|
|
throw new Error('First argument needs to be a number, ' +
|
|
'array or string.');
|
|
}
|
|
|
|
if (this.length > Buffer.poolSize) {
|
|
// Big buffer, just alloc one.
|
|
this.parent = new SlowBuffer(this.length);
|
|
this.offset = 0;
|
|
|
|
} else {
|
|
// Small buffer.
|
|
if (!pool || pool.length - pool.used < this.length) allocPool();
|
|
this.parent = pool;
|
|
this.offset = pool.used;
|
|
pool.used += this.length;
|
|
}
|
|
|
|
// Treat array-ish objects as a byte array.
|
|
if (isArrayIsh(subject)) {
|
|
for (var i = 0; i < this.length; i++) {
|
|
this.parent[i + this.offset] = subject[i];
|
|
}
|
|
} else if (type == 'string') {
|
|
// We are a string
|
|
this.length = this.write(subject, 0, encoding);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
function isArrayIsh(subject) {
|
|
return Array.isArray(subject) || Buffer.isBuffer(subject) ||
|
|
subject && typeof subject === 'object' &&
|
|
typeof subject.length === 'number';
|
|
}
|
|
|
|
exports.SlowBuffer = SlowBuffer;
|
|
exports.Buffer = Buffer;
|
|
|
|
Buffer.poolSize = 8 * 1024;
|
|
var pool;
|
|
|
|
function allocPool() {
|
|
pool = new SlowBuffer(Buffer.poolSize);
|
|
pool.used = 0;
|
|
}
|
|
|
|
|
|
// Static methods
|
|
Buffer.isBuffer = function isBuffer(b) {
|
|
return b instanceof Buffer || b instanceof SlowBuffer;
|
|
};
|
|
|
|
Buffer.concat = function (list, totalLength) {
|
|
if (!Array.isArray(list)) {
|
|
throw new Error("Usage: Buffer.concat(list, [totalLength])\n \
|
|
list should be an Array.");
|
|
}
|
|
|
|
if (list.length === 0) {
|
|
return new Buffer(0);
|
|
} else if (list.length === 1) {
|
|
return list[0];
|
|
}
|
|
|
|
if (typeof totalLength !== 'number') {
|
|
totalLength = 0;
|
|
for (var i = 0; i < list.length; i++) {
|
|
var buf = list[i];
|
|
totalLength += buf.length;
|
|
}
|
|
}
|
|
|
|
var buffer = new Buffer(totalLength);
|
|
var pos = 0;
|
|
for (var i = 0; i < list.length; i++) {
|
|
var buf = list[i];
|
|
buf.copy(buffer, pos);
|
|
pos += buf.length;
|
|
}
|
|
return buffer;
|
|
};
|
|
|
|
// Inspect
|
|
Buffer.prototype.inspect = function inspect() {
|
|
var out = [],
|
|
len = this.length;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
out[i] = toHex(this.parent[i + this.offset]);
|
|
if (i == exports.INSPECT_MAX_BYTES) {
|
|
out[i + 1] = '...';
|
|
break;
|
|
}
|
|
}
|
|
|
|
return '<Buffer ' + out.join(' ') + '>';
|
|
};
|
|
|
|
|
|
Buffer.prototype.get = function get(i) {
|
|
if (i < 0 || i >= this.length) throw new Error('oob');
|
|
return this.parent[this.offset + i];
|
|
};
|
|
|
|
|
|
Buffer.prototype.set = function set(i, v) {
|
|
if (i < 0 || i >= this.length) throw new Error('oob');
|
|
return this.parent[this.offset + i] = v;
|
|
};
|
|
|
|
|
|
// write(string, offset = 0, length = buffer.length-offset, encoding = 'utf8')
|
|
Buffer.prototype.write = function(string, offset, length, encoding) {
|
|
// Support both (string, offset, length, encoding)
|
|
// and the legacy (string, encoding, offset, length)
|
|
if (isFinite(offset)) {
|
|
if (!isFinite(length)) {
|
|
encoding = length;
|
|
length = undefined;
|
|
}
|
|
} else { // legacy
|
|
var swap = encoding;
|
|
encoding = offset;
|
|
offset = length;
|
|
length = swap;
|
|
}
|
|
|
|
offset = +offset || 0;
|
|
var remaining = this.length - offset;
|
|
if (!length) {
|
|
length = remaining;
|
|
} else {
|
|
length = +length;
|
|
if (length > remaining) {
|
|
length = remaining;
|
|
}
|
|
}
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
|
|
|
var ret;
|
|
switch (encoding) {
|
|
case 'hex':
|
|
ret = this.parent.hexWrite(string, this.offset + offset, length);
|
|
break;
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
ret = this.parent.utf8Write(string, this.offset + offset, length);
|
|
break;
|
|
|
|
case 'ascii':
|
|
ret = this.parent.asciiWrite(string, this.offset + offset, length);
|
|
break;
|
|
|
|
case 'binary':
|
|
ret = this.parent.binaryWrite(string, this.offset + offset, length);
|
|
break;
|
|
|
|
case 'base64':
|
|
// Warning: maxLength not taken into account in base64Write
|
|
ret = this.parent.base64Write(string, this.offset + offset, length);
|
|
break;
|
|
|
|
case 'ucs2':
|
|
case 'ucs-2':
|
|
ret = this.parent.ucs2Write(string, this.offset + offset, length);
|
|
break;
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
|
|
Buffer._charsWritten = SlowBuffer._charsWritten;
|
|
|
|
return ret;
|
|
};
|
|
|
|
|
|
// toString(encoding, start=0, end=buffer.length)
|
|
Buffer.prototype.toString = function(encoding, start, end) {
|
|
encoding = String(encoding || 'utf8').toLowerCase();
|
|
|
|
if (typeof start == 'undefined' || start < 0) {
|
|
start = 0;
|
|
} else if (start > this.length) {
|
|
start = this.length;
|
|
}
|
|
|
|
if (typeof end == 'undefined' || end > this.length) {
|
|
end = this.length;
|
|
} else if (end < 0) {
|
|
end = 0;
|
|
}
|
|
|
|
start = start + this.offset;
|
|
end = end + this.offset;
|
|
|
|
switch (encoding) {
|
|
case 'hex':
|
|
return this.parent.hexSlice(start, end);
|
|
|
|
case 'utf8':
|
|
case 'utf-8':
|
|
return this.parent.utf8Slice(start, end);
|
|
|
|
case 'ascii':
|
|
return this.parent.asciiSlice(start, end);
|
|
|
|
case 'binary':
|
|
return this.parent.binarySlice(start, end);
|
|
|
|
case 'base64':
|
|
return this.parent.base64Slice(start, end);
|
|
|
|
case 'ucs2':
|
|
case 'ucs-2':
|
|
return this.parent.ucs2Slice(start, end);
|
|
|
|
default:
|
|
throw new Error('Unknown encoding');
|
|
}
|
|
};
|
|
|
|
|
|
// byteLength
|
|
Buffer.byteLength = SlowBuffer.byteLength;
|
|
|
|
|
|
// fill(value, start=0, end=buffer.length)
|
|
Buffer.prototype.fill = function fill(value, start, end) {
|
|
value || (value = 0);
|
|
start || (start = 0);
|
|
end || (end = this.length);
|
|
|
|
if (typeof value === 'string') {
|
|
value = value.charCodeAt(0);
|
|
}
|
|
if (!(typeof value === 'number') || isNaN(value)) {
|
|
throw new Error('value is not a number');
|
|
}
|
|
|
|
if (end < start) throw new Error('end < start');
|
|
|
|
// Fill 0 bytes; we're done
|
|
if (end === start) return 0;
|
|
if (this.length == 0) return 0;
|
|
|
|
if (start < 0 || start >= this.length) {
|
|
throw new Error('start out of bounds');
|
|
}
|
|
|
|
if (end < 0 || end > this.length) {
|
|
throw new Error('end out of bounds');
|
|
}
|
|
|
|
return this.parent.fill(value,
|
|
start + this.offset,
|
|
end + this.offset);
|
|
};
|
|
|
|
|
|
// copy(targetBuffer, targetStart=0, sourceStart=0, sourceEnd=buffer.length)
|
|
Buffer.prototype.copy = function(target, target_start, start, end) {
|
|
var source = this;
|
|
start || (start = 0);
|
|
end || (end = this.length);
|
|
target_start || (target_start = 0);
|
|
|
|
if (end < start) throw new Error('sourceEnd < sourceStart');
|
|
|
|
// Copy 0 bytes; we're done
|
|
if (end === start) return 0;
|
|
if (target.length == 0 || source.length == 0) return 0;
|
|
|
|
if (target_start < 0 || target_start >= target.length) {
|
|
throw new Error('targetStart out of bounds');
|
|
}
|
|
|
|
if (start < 0 || start >= source.length) {
|
|
throw new Error('sourceStart out of bounds');
|
|
}
|
|
|
|
if (end < 0 || end > source.length) {
|
|
throw new Error('sourceEnd out of bounds');
|
|
}
|
|
|
|
// Are we oob?
|
|
if (end > this.length) {
|
|
end = this.length;
|
|
}
|
|
|
|
if (target.length - target_start < end - start) {
|
|
end = target.length - target_start + start;
|
|
}
|
|
|
|
return this.parent.copy(target.parent,
|
|
target_start + target.offset,
|
|
start + this.offset,
|
|
end + this.offset);
|
|
};
|
|
|
|
|
|
// slice(start, end)
|
|
Buffer.prototype.slice = function(start, end) {
|
|
if (end === undefined) end = this.length;
|
|
if (end > this.length) throw new Error('oob');
|
|
if (start > end) throw new Error('oob');
|
|
|
|
return new Buffer(this.parent, end - start, +start + this.offset);
|
|
};
|
|
|
|
|
|
// Legacy methods for backwards compatibility.
|
|
|
|
Buffer.prototype.utf8Slice = function(start, end) {
|
|
return this.toString('utf8', start, end);
|
|
};
|
|
|
|
Buffer.prototype.binarySlice = function(start, end) {
|
|
return this.toString('binary', start, end);
|
|
};
|
|
|
|
Buffer.prototype.asciiSlice = function(start, end) {
|
|
return this.toString('ascii', start, end);
|
|
};
|
|
|
|
Buffer.prototype.utf8Write = function(string, offset) {
|
|
return this.write(string, offset, 'utf8');
|
|
};
|
|
|
|
Buffer.prototype.binaryWrite = function(string, offset) {
|
|
return this.write(string, offset, 'binary');
|
|
};
|
|
|
|
Buffer.prototype.asciiWrite = function(string, offset) {
|
|
return this.write(string, offset, 'ascii');
|
|
};
|
|
|
|
Buffer.prototype.readUInt8 = function(offset, noAssert) {
|
|
var buffer = this;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
return buffer.parent[buffer.offset + offset];
|
|
};
|
|
|
|
function readUInt16(buffer, offset, isBigEndian, noAssert) {
|
|
var val = 0;
|
|
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
if (isBigEndian) {
|
|
val = buffer.parent[buffer.offset + offset] << 8;
|
|
val |= buffer.parent[buffer.offset + offset + 1];
|
|
} else {
|
|
val = buffer.parent[buffer.offset + offset];
|
|
val |= buffer.parent[buffer.offset + offset + 1] << 8;
|
|
}
|
|
|
|
return val;
|
|
}
|
|
|
|
Buffer.prototype.readUInt16LE = function(offset, noAssert) {
|
|
return readUInt16(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readUInt16BE = function(offset, noAssert) {
|
|
return readUInt16(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readUInt32(buffer, offset, isBigEndian, noAssert) {
|
|
var val = 0;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
if (isBigEndian) {
|
|
val = buffer.parent[buffer.offset + offset + 1] << 16;
|
|
val |= buffer.parent[buffer.offset + offset + 2] << 8;
|
|
val |= buffer.parent[buffer.offset + offset + 3];
|
|
val = val + (buffer.parent[buffer.offset + offset] << 24 >>> 0);
|
|
} else {
|
|
val = buffer.parent[buffer.offset + offset + 2] << 16;
|
|
val |= buffer.parent[buffer.offset + offset + 1] << 8;
|
|
val |= buffer.parent[buffer.offset + offset];
|
|
val = val + (buffer.parent[buffer.offset + offset + 3] << 24 >>> 0);
|
|
}
|
|
|
|
return val;
|
|
}
|
|
|
|
Buffer.prototype.readUInt32LE = function(offset, noAssert) {
|
|
return readUInt32(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readUInt32BE = function(offset, noAssert) {
|
|
return readUInt32(this, offset, true, noAssert);
|
|
};
|
|
|
|
|
|
/*
|
|
* Signed integer types, yay team! A reminder on how two's complement actually
|
|
* works. The first bit is the signed bit, i.e. tells us whether or not the
|
|
* number should be positive or negative. If the two's complement value is
|
|
* positive, then we're done, as it's equivalent to the unsigned representation.
|
|
*
|
|
* Now if the number is positive, you're pretty much done, you can just leverage
|
|
* the unsigned translations and return those. Unfortunately, negative numbers
|
|
* aren't quite that straightforward.
|
|
*
|
|
* At first glance, one might be inclined to use the traditional formula to
|
|
* translate binary numbers between the positive and negative values in two's
|
|
* complement. (Though it doesn't quite work for the most negative value)
|
|
* Mainly:
|
|
* - invert all the bits
|
|
* - add one to the result
|
|
*
|
|
* Of course, this doesn't quite work in Javascript. Take for example the value
|
|
* of -128. This could be represented in 16 bits (big-endian) as 0xff80. But of
|
|
* course, Javascript will do the following:
|
|
*
|
|
* > ~0xff80
|
|
* -65409
|
|
*
|
|
* Whoh there, Javascript, that's not quite right. But wait, according to
|
|
* Javascript that's perfectly correct. When Javascript ends up seeing the
|
|
* constant 0xff80, it has no notion that it is actually a signed number. It
|
|
* assumes that we've input the unsigned value 0xff80. Thus, when it does the
|
|
* binary negation, it casts it into a signed value, (positive 0xff80). Then
|
|
* when you perform binary negation on that, it turns it into a negative number.
|
|
*
|
|
* Instead, we're going to have to use the following general formula, that works
|
|
* in a rather Javascript friendly way. I'm glad we don't support this kind of
|
|
* weird numbering scheme in the kernel.
|
|
*
|
|
* (BIT-MAX - (unsigned)val + 1) * -1
|
|
*
|
|
* The astute observer, may think that this doesn't make sense for 8-bit numbers
|
|
* (really it isn't necessary for them). However, when you get 16-bit numbers,
|
|
* you do. Let's go back to our prior example and see how this will look:
|
|
*
|
|
* (0xffff - 0xff80 + 1) * -1
|
|
* (0x007f + 1) * -1
|
|
* (0x0080) * -1
|
|
*/
|
|
Buffer.prototype.readInt8 = function(offset, noAssert) {
|
|
var buffer = this;
|
|
var neg;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
neg = buffer.parent[buffer.offset + offset] & 0x80;
|
|
if (!neg) {
|
|
return (buffer.parent[buffer.offset + offset]);
|
|
}
|
|
|
|
return ((0xff - buffer.parent[buffer.offset + offset] + 1) * -1);
|
|
};
|
|
|
|
function readInt16(buffer, offset, isBigEndian, noAssert) {
|
|
var neg, val;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
val = readUInt16(buffer, offset, isBigEndian, noAssert);
|
|
neg = val & 0x8000;
|
|
if (!neg) {
|
|
return val;
|
|
}
|
|
|
|
return (0xffff - val + 1) * -1;
|
|
}
|
|
|
|
Buffer.prototype.readInt16LE = function(offset, noAssert) {
|
|
return readInt16(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readInt16BE = function(offset, noAssert) {
|
|
return readInt16(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readInt32(buffer, offset, isBigEndian, noAssert) {
|
|
var neg, val;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
val = readUInt32(buffer, offset, isBigEndian, noAssert);
|
|
neg = val & 0x80000000;
|
|
if (!neg) {
|
|
return (val);
|
|
}
|
|
|
|
return (0xffffffff - val + 1) * -1;
|
|
}
|
|
|
|
Buffer.prototype.readInt32LE = function(offset, noAssert) {
|
|
return readInt32(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readInt32BE = function(offset, noAssert) {
|
|
return readInt32(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readFloat(buffer, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
return require('./buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
|
|
23, 4);
|
|
}
|
|
|
|
Buffer.prototype.readFloatLE = function(offset, noAssert) {
|
|
return readFloat(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readFloatBE = function(offset, noAssert) {
|
|
return readFloat(this, offset, true, noAssert);
|
|
};
|
|
|
|
function readDouble(buffer, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset + 7 < buffer.length,
|
|
'Trying to read beyond buffer length');
|
|
}
|
|
|
|
return require('./buffer_ieee754').readIEEE754(buffer, offset, isBigEndian,
|
|
52, 8);
|
|
}
|
|
|
|
Buffer.prototype.readDoubleLE = function(offset, noAssert) {
|
|
return readDouble(this, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.readDoubleBE = function(offset, noAssert) {
|
|
return readDouble(this, offset, true, noAssert);
|
|
};
|
|
|
|
|
|
/*
|
|
* We have to make sure that the value is a valid integer. This means that it is
|
|
* non-negative. It has no fractional component and that it does not exceed the
|
|
* maximum allowed value.
|
|
*
|
|
* value The number to check for validity
|
|
*
|
|
* max The maximum value
|
|
*/
|
|
function verifuint(value, max) {
|
|
assert.ok(typeof (value) == 'number',
|
|
'cannot write a non-number as a number');
|
|
|
|
assert.ok(value >= 0,
|
|
'specified a negative value for writing an unsigned value');
|
|
|
|
assert.ok(value <= max, 'value is larger than maximum value for type');
|
|
|
|
assert.ok(Math.floor(value) === value, 'value has a fractional component');
|
|
}
|
|
|
|
Buffer.prototype.writeUInt8 = function(value, offset, noAssert) {
|
|
var buffer = this;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'trying to write beyond buffer length');
|
|
|
|
verifuint(value, 0xff);
|
|
}
|
|
|
|
buffer.parent[buffer.offset + offset] = value;
|
|
};
|
|
|
|
function writeUInt16(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'trying to write beyond buffer length');
|
|
|
|
verifuint(value, 0xffff);
|
|
}
|
|
|
|
if (isBigEndian) {
|
|
buffer.parent[buffer.offset + offset] = (value & 0xff00) >>> 8;
|
|
buffer.parent[buffer.offset + offset + 1] = value & 0x00ff;
|
|
} else {
|
|
buffer.parent[buffer.offset + offset + 1] = (value & 0xff00) >>> 8;
|
|
buffer.parent[buffer.offset + offset] = value & 0x00ff;
|
|
}
|
|
}
|
|
|
|
Buffer.prototype.writeUInt16LE = function(value, offset, noAssert) {
|
|
writeUInt16(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeUInt16BE = function(value, offset, noAssert) {
|
|
writeUInt16(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeUInt32(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'trying to write beyond buffer length');
|
|
|
|
verifuint(value, 0xffffffff);
|
|
}
|
|
|
|
if (isBigEndian) {
|
|
buffer.parent[buffer.offset + offset] = (value >>> 24) & 0xff;
|
|
buffer.parent[buffer.offset + offset + 1] = (value >>> 16) & 0xff;
|
|
buffer.parent[buffer.offset + offset + 2] = (value >>> 8) & 0xff;
|
|
buffer.parent[buffer.offset + offset + 3] = value & 0xff;
|
|
} else {
|
|
buffer.parent[buffer.offset + offset + 3] = (value >>> 24) & 0xff;
|
|
buffer.parent[buffer.offset + offset + 2] = (value >>> 16) & 0xff;
|
|
buffer.parent[buffer.offset + offset + 1] = (value >>> 8) & 0xff;
|
|
buffer.parent[buffer.offset + offset] = value & 0xff;
|
|
}
|
|
}
|
|
|
|
Buffer.prototype.writeUInt32LE = function(value, offset, noAssert) {
|
|
writeUInt32(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeUInt32BE = function(value, offset, noAssert) {
|
|
writeUInt32(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
|
|
/*
|
|
* We now move onto our friends in the signed number category. Unlike unsigned
|
|
* numbers, we're going to have to worry a bit more about how we put values into
|
|
* arrays. Since we are only worrying about signed 32-bit values, we're in
|
|
* slightly better shape. Unfortunately, we really can't do our favorite binary
|
|
* & in this system. It really seems to do the wrong thing. For example:
|
|
*
|
|
* > -32 & 0xff
|
|
* 224
|
|
*
|
|
* What's happening above is really: 0xe0 & 0xff = 0xe0. However, the results of
|
|
* this aren't treated as a signed number. Ultimately a bad thing.
|
|
*
|
|
* What we're going to want to do is basically create the unsigned equivalent of
|
|
* our representation and pass that off to the wuint* functions. To do that
|
|
* we're going to do the following:
|
|
*
|
|
* - if the value is positive
|
|
* we can pass it directly off to the equivalent wuint
|
|
* - if the value is negative
|
|
* we do the following computation:
|
|
* mb + val + 1, where
|
|
* mb is the maximum unsigned value in that byte size
|
|
* val is the Javascript negative integer
|
|
*
|
|
*
|
|
* As a concrete value, take -128. In signed 16 bits this would be 0xff80. If
|
|
* you do out the computations:
|
|
*
|
|
* 0xffff - 128 + 1
|
|
* 0xffff - 127
|
|
* 0xff80
|
|
*
|
|
* You can then encode this value as the signed version. This is really rather
|
|
* hacky, but it should work and get the job done which is our goal here.
|
|
*/
|
|
|
|
/*
|
|
* A series of checks to make sure we actually have a signed 32-bit number
|
|
*/
|
|
function verifsint(value, max, min) {
|
|
assert.ok(typeof (value) == 'number',
|
|
'cannot write a non-number as a number');
|
|
|
|
assert.ok(value <= max, 'value larger than maximum allowed value');
|
|
|
|
assert.ok(value >= min, 'value smaller than minimum allowed value');
|
|
|
|
assert.ok(Math.floor(value) === value, 'value has a fractional component');
|
|
}
|
|
|
|
function verifIEEE754(value, max, min) {
|
|
assert.ok(typeof (value) == 'number',
|
|
'cannot write a non-number as a number');
|
|
|
|
assert.ok(value <= max, 'value larger than maximum allowed value');
|
|
|
|
assert.ok(value >= min, 'value smaller than minimum allowed value');
|
|
}
|
|
|
|
Buffer.prototype.writeInt8 = function(value, offset, noAssert) {
|
|
var buffer = this;
|
|
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifsint(value, 0x7f, -0x80);
|
|
}
|
|
|
|
if (value >= 0) {
|
|
buffer.writeUInt8(value, offset, noAssert);
|
|
} else {
|
|
buffer.writeUInt8(0xff + value + 1, offset, noAssert);
|
|
}
|
|
};
|
|
|
|
function writeInt16(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 1 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifsint(value, 0x7fff, -0x8000);
|
|
}
|
|
|
|
if (value >= 0) {
|
|
writeUInt16(buffer, value, offset, isBigEndian, noAssert);
|
|
} else {
|
|
writeUInt16(buffer, 0xffff + value + 1, offset, isBigEndian, noAssert);
|
|
}
|
|
}
|
|
|
|
Buffer.prototype.writeInt16LE = function(value, offset, noAssert) {
|
|
writeInt16(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeInt16BE = function(value, offset, noAssert) {
|
|
writeInt16(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeInt32(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifsint(value, 0x7fffffff, -0x80000000);
|
|
}
|
|
|
|
if (value >= 0) {
|
|
writeUInt32(buffer, value, offset, isBigEndian, noAssert);
|
|
} else {
|
|
writeUInt32(buffer, 0xffffffff + value + 1, offset, isBigEndian, noAssert);
|
|
}
|
|
}
|
|
|
|
Buffer.prototype.writeInt32LE = function(value, offset, noAssert) {
|
|
writeInt32(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeInt32BE = function(value, offset, noAssert) {
|
|
writeInt32(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeFloat(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 3 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifIEEE754(value, 3.4028234663852886e+38, -3.4028234663852886e+38);
|
|
}
|
|
|
|
require('./buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
|
|
23, 4);
|
|
}
|
|
|
|
Buffer.prototype.writeFloatLE = function(value, offset, noAssert) {
|
|
writeFloat(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeFloatBE = function(value, offset, noAssert) {
|
|
writeFloat(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
function writeDouble(buffer, value, offset, isBigEndian, noAssert) {
|
|
if (!noAssert) {
|
|
assert.ok(value !== undefined && value !== null,
|
|
'missing value');
|
|
|
|
assert.ok(typeof (isBigEndian) === 'boolean',
|
|
'missing or invalid endian');
|
|
|
|
assert.ok(offset !== undefined && offset !== null,
|
|
'missing offset');
|
|
|
|
assert.ok(offset + 7 < buffer.length,
|
|
'Trying to write beyond buffer length');
|
|
|
|
verifIEEE754(value, 1.7976931348623157E+308, -1.7976931348623157E+308);
|
|
}
|
|
|
|
require('./buffer_ieee754').writeIEEE754(buffer, value, offset, isBigEndian,
|
|
52, 8);
|
|
}
|
|
|
|
Buffer.prototype.writeDoubleLE = function(value, offset, noAssert) {
|
|
writeDouble(this, value, offset, false, noAssert);
|
|
};
|
|
|
|
Buffer.prototype.writeDoubleBE = function(value, offset, noAssert) {
|
|
writeDouble(this, value, offset, true, noAssert);
|
|
};
|
|
|
|
SlowBuffer.prototype.readUInt8 = Buffer.prototype.readUInt8;
|
|
SlowBuffer.prototype.readUInt16LE = Buffer.prototype.readUInt16LE;
|
|
SlowBuffer.prototype.readUInt16BE = Buffer.prototype.readUInt16BE;
|
|
SlowBuffer.prototype.readUInt32LE = Buffer.prototype.readUInt32LE;
|
|
SlowBuffer.prototype.readUInt32BE = Buffer.prototype.readUInt32BE;
|
|
SlowBuffer.prototype.readInt8 = Buffer.prototype.readInt8;
|
|
SlowBuffer.prototype.readInt16LE = Buffer.prototype.readInt16LE;
|
|
SlowBuffer.prototype.readInt16BE = Buffer.prototype.readInt16BE;
|
|
SlowBuffer.prototype.readInt32LE = Buffer.prototype.readInt32LE;
|
|
SlowBuffer.prototype.readInt32BE = Buffer.prototype.readInt32BE;
|
|
SlowBuffer.prototype.readFloatLE = Buffer.prototype.readFloatLE;
|
|
SlowBuffer.prototype.readFloatBE = Buffer.prototype.readFloatBE;
|
|
SlowBuffer.prototype.readDoubleLE = Buffer.prototype.readDoubleLE;
|
|
SlowBuffer.prototype.readDoubleBE = Buffer.prototype.readDoubleBE;
|
|
SlowBuffer.prototype.writeUInt8 = Buffer.prototype.writeUInt8;
|
|
SlowBuffer.prototype.writeUInt16LE = Buffer.prototype.writeUInt16LE;
|
|
SlowBuffer.prototype.writeUInt16BE = Buffer.prototype.writeUInt16BE;
|
|
SlowBuffer.prototype.writeUInt32LE = Buffer.prototype.writeUInt32LE;
|
|
SlowBuffer.prototype.writeUInt32BE = Buffer.prototype.writeUInt32BE;
|
|
SlowBuffer.prototype.writeInt8 = Buffer.prototype.writeInt8;
|
|
SlowBuffer.prototype.writeInt16LE = Buffer.prototype.writeInt16LE;
|
|
SlowBuffer.prototype.writeInt16BE = Buffer.prototype.writeInt16BE;
|
|
SlowBuffer.prototype.writeInt32LE = Buffer.prototype.writeInt32LE;
|
|
SlowBuffer.prototype.writeInt32BE = Buffer.prototype.writeInt32BE;
|
|
SlowBuffer.prototype.writeFloatLE = Buffer.prototype.writeFloatLE;
|
|
SlowBuffer.prototype.writeFloatBE = Buffer.prototype.writeFloatBE;
|
|
SlowBuffer.prototype.writeDoubleLE = Buffer.prototype.writeDoubleLE;
|
|
SlowBuffer.prototype.writeDoubleBE = Buffer.prototype.writeDoubleBE;
|
|
|
|
},{"assert":1,"./buffer_ieee754":8,"base64-js":9}],9:[function(require,module,exports){
|
|
(function (exports) {
|
|
'use strict';
|
|
|
|
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
|
|
function b64ToByteArray(b64) {
|
|
var i, j, l, tmp, placeHolders, arr;
|
|
|
|
if (b64.length % 4 > 0) {
|
|
throw 'Invalid string. Length must be a multiple of 4';
|
|
}
|
|
|
|
// the number of equal signs (place holders)
|
|
// if there are two placeholders, than the two characters before it
|
|
// represent one byte
|
|
// if there is only one, then the three characters before it represent 2 bytes
|
|
// this is just a cheap hack to not do indexOf twice
|
|
placeHolders = b64.indexOf('=');
|
|
placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0;
|
|
|
|
// base64 is 4/3 + up to two characters of the original data
|
|
arr = [];//new Uint8Array(b64.length * 3 / 4 - placeHolders);
|
|
|
|
// if there are placeholders, only get up to the last complete 4 chars
|
|
l = placeHolders > 0 ? b64.length - 4 : b64.length;
|
|
|
|
for (i = 0, j = 0; i < l; i += 4, j += 3) {
|
|
tmp = (lookup.indexOf(b64[i]) << 18) | (lookup.indexOf(b64[i + 1]) << 12) | (lookup.indexOf(b64[i + 2]) << 6) | lookup.indexOf(b64[i + 3]);
|
|
arr.push((tmp & 0xFF0000) >> 16);
|
|
arr.push((tmp & 0xFF00) >> 8);
|
|
arr.push(tmp & 0xFF);
|
|
}
|
|
|
|
if (placeHolders === 2) {
|
|
tmp = (lookup.indexOf(b64[i]) << 2) | (lookup.indexOf(b64[i + 1]) >> 4);
|
|
arr.push(tmp & 0xFF);
|
|
} else if (placeHolders === 1) {
|
|
tmp = (lookup.indexOf(b64[i]) << 10) | (lookup.indexOf(b64[i + 1]) << 4) | (lookup.indexOf(b64[i + 2]) >> 2);
|
|
arr.push((tmp >> 8) & 0xFF);
|
|
arr.push(tmp & 0xFF);
|
|
}
|
|
|
|
return arr;
|
|
}
|
|
|
|
function uint8ToBase64(uint8) {
|
|
var i,
|
|
extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
|
|
output = "",
|
|
temp, length;
|
|
|
|
function tripletToBase64 (num) {
|
|
return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F];
|
|
};
|
|
|
|
// go through the array every three bytes, we'll deal with trailing stuff later
|
|
for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) {
|
|
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]);
|
|
output += tripletToBase64(temp);
|
|
}
|
|
|
|
// pad the end with zeros, but make sure to not forget the extra bytes
|
|
switch (extraBytes) {
|
|
case 1:
|
|
temp = uint8[uint8.length - 1];
|
|
output += lookup[temp >> 2];
|
|
output += lookup[(temp << 4) & 0x3F];
|
|
output += '==';
|
|
break;
|
|
case 2:
|
|
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]);
|
|
output += lookup[temp >> 10];
|
|
output += lookup[(temp >> 4) & 0x3F];
|
|
output += lookup[(temp << 2) & 0x3F];
|
|
output += '=';
|
|
break;
|
|
}
|
|
|
|
return output;
|
|
}
|
|
|
|
module.exports.toByteArray = b64ToByteArray;
|
|
module.exports.fromByteArray = uint8ToBase64;
|
|
}());
|
|
|
|
},{}]},{},[])
|
|
;;module.exports=require("buffer-browserify")
|
|
|
|
},{}],30:[function(require,module,exports){
|
|
// shim for using process in browser
|
|
|
|
var process = module.exports = {};
|
|
|
|
process.nextTick = (function () {
|
|
var canSetImmediate = typeof window !== 'undefined'
|
|
&& window.setImmediate;
|
|
var canPost = typeof window !== 'undefined'
|
|
&& window.postMessage && window.addEventListener
|
|
;
|
|
|
|
if (canSetImmediate) {
|
|
return function (f) { return window.setImmediate(f) };
|
|
}
|
|
|
|
if (canPost) {
|
|
var queue = [];
|
|
window.addEventListener('message', function (ev) {
|
|
var source = ev.source;
|
|
if ((source === window || source === null) && ev.data === 'process-tick') {
|
|
ev.stopPropagation();
|
|
if (queue.length > 0) {
|
|
var fn = queue.shift();
|
|
fn();
|
|
}
|
|
}
|
|
}, true);
|
|
|
|
return function nextTick(fn) {
|
|
queue.push(fn);
|
|
window.postMessage('process-tick', '*');
|
|
};
|
|
}
|
|
|
|
return function nextTick(fn) {
|
|
setTimeout(fn, 0);
|
|
};
|
|
})();
|
|
|
|
process.title = 'browser';
|
|
process.browser = true;
|
|
process.env = {};
|
|
process.argv = [];
|
|
|
|
process.binding = function (name) {
|
|
throw new Error('process.binding is not supported');
|
|
}
|
|
|
|
// TODO(shtylman)
|
|
process.cwd = function () { return '/' };
|
|
process.chdir = function (dir) {
|
|
throw new Error('process.chdir is not supported');
|
|
};
|
|
|
|
},{}],31:[function(require,module,exports){
|
|
|
|
module.exports = require('./lib/urlsafe-base64');
|
|
},{"./lib/urlsafe-base64":32}],32:[function(require,module,exports){
|
|
var Buffer=require("__browserify_Buffer").Buffer;/*!
|
|
* urlsafe-base64
|
|
*/
|
|
|
|
/**
|
|
* Module Dependencies
|
|
*/
|
|
|
|
// None yet!
|
|
|
|
/**
|
|
* Library version.
|
|
*/
|
|
|
|
exports.version = '1.0.0';
|
|
|
|
/**
|
|
* .encode
|
|
*
|
|
* return an encoded Buffer as URL Safe Base64
|
|
*
|
|
* Note: This function encodes to the RFC 4648 Spec where '+' is encoded
|
|
* as '-' and '/' is encoded as '_'. The padding character '=' is
|
|
* removed.
|
|
*
|
|
* @param {Buffer} buffer
|
|
* @return {String}
|
|
* @api public
|
|
*/
|
|
|
|
exports.encode = function encode(buffer) {
|
|
|
|
return buffer.toString('base64')
|
|
.replace(/\+/g, '-') // Convert '+' to '-'
|
|
.replace(/\//g, '_') // Convert '/' to '_'
|
|
.replace(/=+$/, ''); // Remove ending '='
|
|
|
|
};
|
|
|
|
/**
|
|
* .decode
|
|
*
|
|
* return an decoded URL Safe Base64 as Buffer
|
|
*
|
|
* @param {String}
|
|
* @return {Buffer}
|
|
* @api public
|
|
*/
|
|
|
|
exports.decode = function decode(base64) {
|
|
|
|
// Add removed at end '='
|
|
base64 += Array(5 - base64.length % 4).join('=');
|
|
|
|
base64 = base64
|
|
.replace(/\-/g, '+') // Convert '-' to '+'
|
|
.replace(/\_/g, '/'); // Convert '_' to '/'
|
|
|
|
return new Buffer(base64, 'base64');
|
|
|
|
};
|
|
|
|
/**
|
|
* .validate
|
|
*
|
|
* Validates a string if it is URL Safe Base64 encoded.
|
|
*
|
|
* @param {String}
|
|
* @return {Boolean}
|
|
* @api public
|
|
*/
|
|
|
|
exports.validate = function validate(base64) {
|
|
|
|
return /^[A-Za-z0-9\-_]+$/.test(base64);
|
|
|
|
};
|
|
},{"__browserify_Buffer":29}]},{},[1])
|
|
(1)
|
|
});
|
|
; |