react / wstein / node_modules / jest-cli / node_modules / istanbul / node_modules / js-yaml / lib / js-yaml / type / binary.js
81149 views'use strict';12/*eslint-disable no-bitwise*/34// A trick for browserified version.5// Since we make browserifier to ignore `buffer` module, NodeBuffer will be undefined6var NodeBuffer = require('buffer').Buffer;7var Type = require('../type');8910// [ 64, 65, 66 ] -> [ padding, CR, LF ]11var BASE64_MAP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=\n\r';121314function resolveYamlBinary(data) {15if (null === data) {16return false;17}1819var code, idx, bitlen = 0, len = 0, max = data.length, map = BASE64_MAP;2021// Convert one by one.22for (idx = 0; idx < max; idx++) {23code = map.indexOf(data.charAt(idx));2425// Skip CR/LF26if (code > 64) { continue; }2728// Fail on illegal characters29if (code < 0) { return false; }3031bitlen += 6;32}3334// If there are any bits left, source was corrupted35return (bitlen % 8) === 0;36}3738function constructYamlBinary(data) {39var code, idx, tailbits,40input = data.replace(/[\r\n=]/g, ''), // remove CR/LF & padding to simplify scan41max = input.length,42map = BASE64_MAP,43bits = 0,44result = [];4546// Collect by 6*4 bits (3 bytes)4748for (idx = 0; idx < max; idx++) {49if ((idx % 4 === 0) && idx) {50result.push((bits >> 16) & 0xFF);51result.push((bits >> 8) & 0xFF);52result.push(bits & 0xFF);53}5455bits = (bits << 6) | map.indexOf(input.charAt(idx));56}5758// Dump tail5960tailbits = (max % 4) * 6;6162if (tailbits === 0) {63result.push((bits >> 16) & 0xFF);64result.push((bits >> 8) & 0xFF);65result.push(bits & 0xFF);66} else if (tailbits === 18) {67result.push((bits >> 10) & 0xFF);68result.push((bits >> 2) & 0xFF);69} else if (tailbits === 12) {70result.push((bits >> 4) & 0xFF);71}7273// Wrap into Buffer for NodeJS and leave Array for browser74if (NodeBuffer) {75return new NodeBuffer(result);76}7778return result;79}8081function representYamlBinary(object /*, style*/) {82var result = '', bits = 0, idx, tail,83max = object.length,84map = BASE64_MAP;8586// Convert every three bytes to 4 ASCII characters.8788for (idx = 0; idx < max; idx++) {89if ((idx % 3 === 0) && idx) {90result += map[(bits >> 18) & 0x3F];91result += map[(bits >> 12) & 0x3F];92result += map[(bits >> 6) & 0x3F];93result += map[bits & 0x3F];94}9596bits = (bits << 8) + object[idx];97}9899// Dump tail100101tail = max % 3;102103if (tail === 0) {104result += map[(bits >> 18) & 0x3F];105result += map[(bits >> 12) & 0x3F];106result += map[(bits >> 6) & 0x3F];107result += map[bits & 0x3F];108} else if (tail === 2) {109result += map[(bits >> 10) & 0x3F];110result += map[(bits >> 4) & 0x3F];111result += map[(bits << 2) & 0x3F];112result += map[64];113} else if (tail === 1) {114result += map[(bits >> 2) & 0x3F];115result += map[(bits << 4) & 0x3F];116result += map[64];117result += map[64];118}119120return result;121}122123function isBinary(object) {124return NodeBuffer && NodeBuffer.isBuffer(object);125}126127module.exports = new Type('tag:yaml.org,2002:binary', {128kind: 'scalar',129resolve: resolveYamlBinary,130construct: constructYamlBinary,131predicate: isBinary,132represent: representYamlBinary133});134135136