Path: blob/master/node_modules/@protobufjs/base64/index.js
2591 views
"use strict";12/**3* A minimal base64 implementation for number arrays.4* @memberof util5* @namespace6*/7var base64 = exports;89/**10* Calculates the byte length of a base64 encoded string.11* @param {string} string Base64 encoded string12* @returns {number} Byte length13*/14base64.length = function length(string) {15var p = string.length;16if (!p)17return 0;18var n = 0;19while (--p % 4 > 1 && string.charAt(p) === "=")20++n;21return Math.ceil(string.length * 3) / 4 - n;22};2324// Base64 encoding table25var b64 = new Array(64);2627// Base64 decoding table28var s64 = new Array(123);2930// 65..90, 97..122, 48..57, 43, 4731for (var i = 0; i < 64;)32s64[b64[i] = i < 26 ? i + 65 : i < 52 ? i + 71 : i < 62 ? i - 4 : i - 59 | 43] = i++;3334/**35* Encodes a buffer to a base64 encoded string.36* @param {Uint8Array} buffer Source buffer37* @param {number} start Source start38* @param {number} end Source end39* @returns {string} Base64 encoded string40*/41base64.encode = function encode(buffer, start, end) {42var parts = null,43chunk = [];44var i = 0, // output index45j = 0, // goto index46t; // temporary47while (start < end) {48var b = buffer[start++];49switch (j) {50case 0:51chunk[i++] = b64[b >> 2];52t = (b & 3) << 4;53j = 1;54break;55case 1:56chunk[i++] = b64[t | b >> 4];57t = (b & 15) << 2;58j = 2;59break;60case 2:61chunk[i++] = b64[t | b >> 6];62chunk[i++] = b64[b & 63];63j = 0;64break;65}66if (i > 8191) {67(parts || (parts = [])).push(String.fromCharCode.apply(String, chunk));68i = 0;69}70}71if (j) {72chunk[i++] = b64[t];73chunk[i++] = 61;74if (j === 1)75chunk[i++] = 61;76}77if (parts) {78if (i)79parts.push(String.fromCharCode.apply(String, chunk.slice(0, i)));80return parts.join("");81}82return String.fromCharCode.apply(String, chunk.slice(0, i));83};8485var invalidEncoding = "invalid encoding";8687/**88* Decodes a base64 encoded string to a buffer.89* @param {string} string Source string90* @param {Uint8Array} buffer Destination buffer91* @param {number} offset Destination offset92* @returns {number} Number of bytes written93* @throws {Error} If encoding is invalid94*/95base64.decode = function decode(string, buffer, offset) {96var start = offset;97var j = 0, // goto index98t; // temporary99for (var i = 0; i < string.length;) {100var c = string.charCodeAt(i++);101if (c === 61 && j > 1)102break;103if ((c = s64[c]) === undefined)104throw Error(invalidEncoding);105switch (j) {106case 0:107t = c;108j = 1;109break;110case 1:111buffer[offset++] = t << 2 | (c & 48) >> 4;112t = c;113j = 2;114break;115case 2:116buffer[offset++] = (t & 15) << 4 | (c & 60) >> 2;117t = c;118j = 3;119break;120case 3:121buffer[offset++] = (t & 3) << 6 | c;122j = 0;123break;124}125}126if (j === 1)127throw Error(invalidEncoding);128return offset - start;129};130131/**132* Tests if the specified string appears to be base64 encoded.133* @param {string} string String to test134* @returns {boolean} `true` if probably base64 encoded, otherwise false135*/136base64.test = function test(string) {137return /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/.test(string);138};139140141