node-query/node_modules/grunt-istanbul/node_modules/istanbul/node_modules/js-yaml/benchmark/implementations/js-yaml-3.2.1/lib/js-yaml/type/binary.js

142 lines
4.1 KiB
JavaScript

// Modified from:
// https://raw.github.com/kanaka/noVNC/d890e8640f20fba3215ba7be8e0ff145aeb8c17c/include/base64.js
'use strict';
// A trick for browserified version.
// Since we make browserifier to ignore `buffer` module, NodeBuffer will be undefined
var NodeBuffer = require('buffer').Buffer;
var Type = require('../type');
var BASE64_PADDING = '=';
var BASE64_BINTABLE = [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, 0, -1, -1,
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
];
var BASE64_CHARTABLE =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
function resolveYamlBinary(data) {
var code, idx = 0, len = data.length, leftbits;
leftbits = 0; // number of bits decoded, but yet to be appended
// Convert one by one.
for (idx = 0; idx < len; idx += 1) {
code = data.charCodeAt(idx);
// Skip LF(NL) || CR
if (0x0A === code || 0x0D === code) { continue; }
// Fail on illegal characters
if (-1 === BASE64_BINTABLE[code & 0x7F]) {
return false;
}
// update bitcount
leftbits += 6;
// If we have 8 or more bits, append 8 bits to the result
if (leftbits >= 8) {
leftbits -= 8;
}
}
// If there are any bits left, the base64 string was corrupted
if (leftbits) {
return false;
} else {
return true;
}
}
function constructYamlBinary(data) {
var value, code, idx = 0, len = data.length, result = [], leftbits, leftdata;
leftbits = 0; // number of bits decoded, but yet to be appended
leftdata = 0; // bits decoded, but yet to be appended
// Convert one by one.
for (idx = 0; idx < len; idx += 1) {
code = data.charCodeAt(idx);
value = BASE64_BINTABLE[code & 0x7F];
// Skip LF(NL) || CR
if (0x0A === code || 0x0D === code) { continue; }
// Collect data into leftdata, update bitcount
leftdata = (leftdata << 6) | value;
leftbits += 6;
// If we have 8 or more bits, append 8 bits to the result
if (leftbits >= 8) {
leftbits -= 8;
// Append if not padding.
if (BASE64_PADDING !== data.charAt(idx)) {
result.push((leftdata >> leftbits) & 0xFF);
}
leftdata &= (1 << leftbits) - 1;
}
}
// Wrap into Buffer for NodeJS and leave Array for browser
if (NodeBuffer) {
return new NodeBuffer(result);
}
return result;
}
function representYamlBinary(object /*, style*/) {
var result = '', index, length, rest;
// Convert every three bytes to 4 ASCII characters.
for (index = 0, length = object.length - 2; index < length; index += 3) {
result += BASE64_CHARTABLE[object[index + 0] >> 2];
result += BASE64_CHARTABLE[((object[index + 0] & 0x03) << 4) + (object[index + 1] >> 4)];
result += BASE64_CHARTABLE[((object[index + 1] & 0x0F) << 2) + (object[index + 2] >> 6)];
result += BASE64_CHARTABLE[object[index + 2] & 0x3F];
}
rest = object.length % 3;
// Convert the remaining 1 or 2 bytes, padding out to 4 characters.
if (0 !== rest) {
index = object.length - rest;
result += BASE64_CHARTABLE[object[index + 0] >> 2];
if (2 === rest) {
result += BASE64_CHARTABLE[((object[index + 0] & 0x03) << 4) + (object[index + 1] >> 4)];
result += BASE64_CHARTABLE[(object[index + 1] & 0x0F) << 2];
result += BASE64_PADDING;
} else {
result += BASE64_CHARTABLE[(object[index + 0] & 0x03) << 4];
result += BASE64_PADDING + BASE64_PADDING;
}
}
return result;
}
function isBinary(object) {
return NodeBuffer && NodeBuffer.isBuffer(object);
}
module.exports = new Type('tag:yaml.org,2002:binary', {
kind: 'scalar',
resolve: resolveYamlBinary,
construct: constructYamlBinary,
predicate: isBinary,
represent: representYamlBinary
});