Moved web root into root of project; this makes deployment easier.

Also deleted 'docs', which is now redundant.
This commit is contained in:
Simon Brooke 2020-02-27 14:18:29 +00:00
parent a5204c66b9
commit 743d8a1740
No known key found for this signature in database
GPG key ID: A7A4F18D1D4DF987
1592 changed files with 53626 additions and 139250 deletions

View file

@ -0,0 +1,359 @@
// Copyright 2007 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Base64 en/decoding. Not much to say here except that we
* work with decoded values in arrays of bytes. By "byte" I mean a number
* in [0, 255].
*
* @author doughtie@google.com (Gavin Doughtie)
*/
goog.provide('goog.crypt.base64');
goog.require('goog.asserts');
goog.require('goog.crypt');
goog.require('goog.string');
goog.require('goog.userAgent');
goog.require('goog.userAgent.product');
// Static lookup maps, lazily populated by init_()
/**
* Maps bytes to characters.
* @type {Object}
* @private
*/
goog.crypt.base64.byteToCharMap_ = null;
/**
* Maps characters to bytes. Used for normal and websafe characters.
* @type {Object}
* @private
*/
goog.crypt.base64.charToByteMap_ = null;
/**
* Maps bytes to websafe characters.
* @type {Object}
* @private
*/
goog.crypt.base64.byteToCharMapWebSafe_ = null;
/**
* Our default alphabet, shared between
* ENCODED_VALS and ENCODED_VALS_WEBSAFE
* @type {string}
*/
goog.crypt.base64.ENCODED_VALS_BASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' +
'abcdefghijklmnopqrstuvwxyz' +
'0123456789';
/**
* Our default alphabet. Value 64 (=) is special; it means "nothing."
* @type {string}
*/
goog.crypt.base64.ENCODED_VALS = goog.crypt.base64.ENCODED_VALS_BASE + '+/=';
/**
* Our websafe alphabet.
* @type {string}
*/
goog.crypt.base64.ENCODED_VALS_WEBSAFE =
goog.crypt.base64.ENCODED_VALS_BASE + '-_.';
/**
* White list of implementations with known-good native atob and btoa functions.
* Listing these explicitly (via the ASSUME_* wrappers) benefits dead-code
* removal in per-browser compilations.
* @private {boolean}
*/
goog.crypt.base64.ASSUME_NATIVE_SUPPORT_ = goog.userAgent.GECKO ||
(goog.userAgent.WEBKIT && !goog.userAgent.product.SAFARI) ||
goog.userAgent.OPERA;
/**
* Does this browser have a working btoa function?
* @private {boolean}
*/
goog.crypt.base64.HAS_NATIVE_ENCODE_ =
goog.crypt.base64.ASSUME_NATIVE_SUPPORT_ ||
typeof(goog.global.btoa) == 'function';
/**
* Does this browser have a working atob function?
* We blacklist known-bad implementations:
* - IE (10+) added atob() but it does not tolerate whitespace on the input.
* @private {boolean}
*/
goog.crypt.base64.HAS_NATIVE_DECODE_ =
goog.crypt.base64.ASSUME_NATIVE_SUPPORT_ ||
(!goog.userAgent.product.SAFARI && !goog.userAgent.IE &&
typeof(goog.global.atob) == 'function');
/**
* Base64-encode an array of bytes.
*
* @param {Array<number>|Uint8Array} input An array of bytes (numbers with
* value in [0, 255]) to encode.
* @param {boolean=} opt_webSafe True indicates we should use the alternative
* alphabet, which does not require escaping for use in URLs.
* @return {string} The base64 encoded string.
*/
goog.crypt.base64.encodeByteArray = function(input, opt_webSafe) {
// Assert avoids runtime dependency on goog.isArrayLike, which helps reduce
// size of jscompiler output, and which yields slight performance increase.
goog.asserts.assert(
goog.isArrayLike(input), 'encodeByteArray takes an array as a parameter');
goog.crypt.base64.init_();
var byteToCharMap = opt_webSafe ? goog.crypt.base64.byteToCharMapWebSafe_ :
goog.crypt.base64.byteToCharMap_;
var output = [];
for (var i = 0; i < input.length; i += 3) {
var byte1 = input[i];
var haveByte2 = i + 1 < input.length;
var byte2 = haveByte2 ? input[i + 1] : 0;
var haveByte3 = i + 2 < input.length;
var byte3 = haveByte3 ? input[i + 2] : 0;
var outByte1 = byte1 >> 2;
var outByte2 = ((byte1 & 0x03) << 4) | (byte2 >> 4);
var outByte3 = ((byte2 & 0x0F) << 2) | (byte3 >> 6);
var outByte4 = byte3 & 0x3F;
if (!haveByte3) {
outByte4 = 64;
if (!haveByte2) {
outByte3 = 64;
}
}
output.push(
byteToCharMap[outByte1], byteToCharMap[outByte2],
byteToCharMap[outByte3], byteToCharMap[outByte4]);
}
return output.join('');
};
/**
* Base64-encode a string.
*
* @param {string} input A string to encode.
* @param {boolean=} opt_webSafe True indicates we should use the alternative
* alphabet, which does not require escaping for use in URLs.
* @return {string} The base64 encoded string.
*/
goog.crypt.base64.encodeString = function(input, opt_webSafe) {
// Shortcut for browsers that implement
// a native base64 encoder in the form of "btoa/atob"
if (goog.crypt.base64.HAS_NATIVE_ENCODE_ && !opt_webSafe) {
return goog.global.btoa(input);
}
return goog.crypt.base64.encodeByteArray(
goog.crypt.stringToByteArray(input), opt_webSafe);
};
/**
* Base64-decode a string.
*
* @param {string} input Input to decode. Any whitespace is ignored, and the
* input maybe encoded with either supported alphabet (or a mix thereof).
* @param {boolean=} opt_webSafe True indicates we should use the alternative
* alphabet, which does not require escaping for use in URLs. Note that
* passing false may also still allow webSafe input decoding, when the
* fallback decoder is used on browsers without native support.
* @return {string} string representing the decoded value.
*/
goog.crypt.base64.decodeString = function(input, opt_webSafe) {
// Shortcut for browsers that implement
// a native base64 encoder in the form of "btoa/atob"
if (goog.crypt.base64.HAS_NATIVE_DECODE_ && !opt_webSafe) {
return goog.global.atob(input);
}
var output = '';
function pushByte(b) { output += String.fromCharCode(b); }
goog.crypt.base64.decodeStringInternal_(input, pushByte);
return output;
};
/**
* Base64-decode a string to an Array of numbers.
*
* In base-64 decoding, groups of four characters are converted into three
* bytes. If the encoder did not apply padding, the input length may not
* be a multiple of 4.
*
* In this case, the last group will have fewer than 4 characters, and
* padding will be inferred. If the group has one or two characters, it decodes
* to one byte. If the group has three characters, it decodes to two bytes.
*
* @param {string} input Input to decode. Any whitespace is ignored, and the
* input maybe encoded with either supported alphabet (or a mix thereof).
* @param {boolean=} opt_ignored Unused parameter, retained for compatibility.
* @return {!Array<number>} bytes representing the decoded value.
*/
goog.crypt.base64.decodeStringToByteArray = function(input, opt_ignored) {
var output = [];
function pushByte(b) { output.push(b); }
goog.crypt.base64.decodeStringInternal_(input, pushByte);
return output;
};
/**
* Base64-decode a string to a Uint8Array.
*
* Note that Uint8Array is not supported on older browsers, e.g. IE < 10.
* @see http://caniuse.com/uint8array
*
* In base-64 decoding, groups of four characters are converted into three
* bytes. If the encoder did not apply padding, the input length may not
* be a multiple of 4.
*
* In this case, the last group will have fewer than 4 characters, and
* padding will be inferred. If the group has one or two characters, it decodes
* to one byte. If the group has three characters, it decodes to two bytes.
*
* @param {string} input Input to decode. Any whitespace is ignored, and the
* input maybe encoded with either supported alphabet (or a mix thereof).
* @return {!Uint8Array} bytes representing the decoded value.
*/
goog.crypt.base64.decodeStringToUint8Array = function(input) {
goog.asserts.assert(
!goog.userAgent.IE || goog.userAgent.isVersionOrHigher('10'),
'Browser does not support typed arrays');
var output = new Uint8Array(Math.ceil(input.length * 3 / 4));
var outLen = 0;
function pushByte(b) { output[outLen++] = b; }
goog.crypt.base64.decodeStringInternal_(input, pushByte);
return output.subarray(0, outLen);
};
/**
* @param {string} input Input to decode.
* @param {function(number):void} pushByte result accumulator.
* @private
*/
goog.crypt.base64.decodeStringInternal_ = function(input, pushByte) {
goog.crypt.base64.init_();
var nextCharIndex = 0;
/**
* @param {number} default_val Used for end-of-input.
* @return {number} The next 6-bit value, or the default for end-of-input.
*/
function getByte(default_val) {
while (nextCharIndex < input.length) {
var ch = input.charAt(nextCharIndex++);
var b = goog.crypt.base64.charToByteMap_[ch];
if (b != null) {
return b; // Common case: decoded the char.
}
if (!goog.string.isEmptyOrWhitespace(ch)) {
throw Error('Unknown base64 encoding at char: ' + ch);
}
// We encountered whitespace: loop around to the next input char.
}
return default_val; // No more input remaining.
}
while (true) {
var byte1 = getByte(-1);
var byte2 = getByte(0);
var byte3 = getByte(64);
var byte4 = getByte(64);
// The common case is that all four bytes are present, so if we have byte4
// we can skip over the truncated input special case handling.
if (byte4 === 64) {
if (byte1 === -1) {
return; // Terminal case: no input left to decode.
}
// Here we know an intermediate number of bytes are missing.
// The defaults for byte2, byte3 and byte4 apply the inferred padding
// rules per the public API documentation. i.e: 1 byte
// missing should yield 2 bytes of output, but 2 or 3 missing bytes yield
// a single byte of output. (Recall that 64 corresponds the padding char).
}
var outByte1 = (byte1 << 2) | (byte2 >> 4);
pushByte(outByte1);
if (byte3 != 64) {
var outByte2 = ((byte2 << 4) & 0xF0) | (byte3 >> 2);
pushByte(outByte2);
if (byte4 != 64) {
var outByte3 = ((byte3 << 6) & 0xC0) | byte4;
pushByte(outByte3);
}
}
}
};
/**
* Lazy static initialization function. Called before
* accessing any of the static map variables.
* @private
*/
goog.crypt.base64.init_ = function() {
if (!goog.crypt.base64.byteToCharMap_) {
goog.crypt.base64.byteToCharMap_ = {};
goog.crypt.base64.charToByteMap_ = {};
goog.crypt.base64.byteToCharMapWebSafe_ = {};
// We want quick mappings back and forth, so we precompute two maps.
for (var i = 0; i < goog.crypt.base64.ENCODED_VALS.length; i++) {
goog.crypt.base64.byteToCharMap_[i] =
goog.crypt.base64.ENCODED_VALS.charAt(i);
goog.crypt.base64.charToByteMap_[goog.crypt.base64.byteToCharMap_[i]] = i;
goog.crypt.base64.byteToCharMapWebSafe_[i] =
goog.crypt.base64.ENCODED_VALS_WEBSAFE.charAt(i);
// Be forgiving when decoding and correctly decode both encodings.
if (i >= goog.crypt.base64.ENCODED_VALS_BASE.length) {
goog.crypt.base64
.charToByteMap_[goog.crypt.base64.ENCODED_VALS_WEBSAFE.charAt(i)] =
i;
}
}
}
};

View file

@ -0,0 +1,195 @@
// Copyright 2008 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Namespace with crypto related helper functions.
*/
goog.provide('goog.crypt');
goog.require('goog.array');
goog.require('goog.asserts');
/**
* Turns a string into an array of bytes; a "byte" being a JS number in the
* range 0-255. Multi-byte characters are written as little-endian.
* @param {string} str String value to arrify.
* @return {!Array<number>} Array of numbers corresponding to the
* UCS character codes of each character in str.
*/
goog.crypt.stringToByteArray = function(str) {
var output = [], p = 0;
for (var i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
// NOTE: c <= 0xffff since JavaScript strings are UTF-16.
if (c > 0xff) {
output[p++] = c & 0xff;
c >>= 8;
}
output[p++] = c;
}
return output;
};
/**
* Turns an array of numbers into the string given by the concatenation of the
* characters to which the numbers correspond.
* @param {!Uint8Array|!Array<number>} bytes Array of numbers representing
* characters.
* @return {string} Stringification of the array.
*/
goog.crypt.byteArrayToString = function(bytes) {
var CHUNK_SIZE = 8192;
// Special-case the simple case for speed's sake.
if (bytes.length <= CHUNK_SIZE) {
return String.fromCharCode.apply(null, bytes);
}
// The remaining logic splits conversion by chunks since
// Function#apply() has a maximum parameter count.
// See discussion: http://goo.gl/LrWmZ9
var str = '';
for (var i = 0; i < bytes.length; i += CHUNK_SIZE) {
var chunk = goog.array.slice(bytes, i, i + CHUNK_SIZE);
str += String.fromCharCode.apply(null, chunk);
}
return str;
};
/**
* Turns an array of numbers into the hex string given by the concatenation of
* the hex values to which the numbers correspond.
* @param {Uint8Array|Array<number>} array Array of numbers representing
* characters.
* @return {string} Hex string.
*/
goog.crypt.byteArrayToHex = function(array) {
return goog.array
.map(
array,
function(numByte) {
var hexByte = numByte.toString(16);
return hexByte.length > 1 ? hexByte : '0' + hexByte;
})
.join('');
};
/**
* Converts a hex string into an integer array.
* @param {string} hexString Hex string of 16-bit integers (two characters
* per integer).
* @return {!Array<number>} Array of {0,255} integers for the given string.
*/
goog.crypt.hexToByteArray = function(hexString) {
goog.asserts.assert(
hexString.length % 2 == 0, 'Key string length must be multiple of 2');
var arr = [];
for (var i = 0; i < hexString.length; i += 2) {
arr.push(parseInt(hexString.substring(i, i + 2), 16));
}
return arr;
};
/**
* Converts a JS string to a UTF-8 "byte" array.
* @param {string} str 16-bit unicode string.
* @return {!Array<number>} UTF-8 byte array.
*/
goog.crypt.stringToUtf8ByteArray = function(str) {
// TODO(user): Use native implementations if/when available
var out = [], p = 0;
for (var i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
if (c < 128) {
out[p++] = c;
} else if (c < 2048) {
out[p++] = (c >> 6) | 192;
out[p++] = (c & 63) | 128;
} else if (
((c & 0xFC00) == 0xD800) && (i + 1) < str.length &&
((str.charCodeAt(i + 1) & 0xFC00) == 0xDC00)) {
// Surrogate Pair
c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) & 0x03FF);
out[p++] = (c >> 18) | 240;
out[p++] = ((c >> 12) & 63) | 128;
out[p++] = ((c >> 6) & 63) | 128;
out[p++] = (c & 63) | 128;
} else {
out[p++] = (c >> 12) | 224;
out[p++] = ((c >> 6) & 63) | 128;
out[p++] = (c & 63) | 128;
}
}
return out;
};
/**
* Converts a UTF-8 byte array to JavaScript's 16-bit Unicode.
* @param {Uint8Array|Array<number>} bytes UTF-8 byte array.
* @return {string} 16-bit Unicode string.
*/
goog.crypt.utf8ByteArrayToString = function(bytes) {
// TODO(user): Use native implementations if/when available
var out = [], pos = 0, c = 0;
while (pos < bytes.length) {
var c1 = bytes[pos++];
if (c1 < 128) {
out[c++] = String.fromCharCode(c1);
} else if (c1 > 191 && c1 < 224) {
var c2 = bytes[pos++];
out[c++] = String.fromCharCode((c1 & 31) << 6 | c2 & 63);
} else if (c1 > 239 && c1 < 365) {
// Surrogate Pair
var c2 = bytes[pos++];
var c3 = bytes[pos++];
var c4 = bytes[pos++];
var u = ((c1 & 7) << 18 | (c2 & 63) << 12 | (c3 & 63) << 6 | c4 & 63) -
0x10000;
out[c++] = String.fromCharCode(0xD800 + (u >> 10));
out[c++] = String.fromCharCode(0xDC00 + (u & 1023));
} else {
var c2 = bytes[pos++];
var c3 = bytes[pos++];
out[c++] =
String.fromCharCode((c1 & 15) << 12 | (c2 & 63) << 6 | c3 & 63);
}
}
return out.join('');
};
/**
* XOR two byte arrays.
* @param {!Uint8Array|!Int8Array|!Array<number>} bytes1 Byte array 1.
* @param {!Uint8Array|!Int8Array|!Array<number>} bytes2 Byte array 2.
* @return {!Array<number>} Resulting XOR of the two byte arrays.
*/
goog.crypt.xorByteArray = function(bytes1, bytes2) {
goog.asserts.assert(
bytes1.length == bytes2.length, 'XOR array lengths must match');
var result = [];
for (var i = 0; i < bytes1.length; i++) {
result.push(bytes1[i] ^ bytes2[i]);
}
return result;
};