UNPKG

4.08 kBJavaScriptView Raw
1/*
2 * Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 */
22
23/*
24 * Base 64 implementation in JavaScript
25 * Original source available at https://raw.github.com/nzakas/computer-science-in-javascript/02a2745b4aa8214f2cae1bf0b15b447ca1a91b23/encodings/base64/base64.js
26 *
27 * Converted to AMD and linter refinement by Scott Andrews
28 */
29
30(function (define) {
31 'use strict';
32
33 /*jshint bitwise: false */
34 define(function (/* require */) {
35
36 var digits = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
37
38 /**
39 * Base64-encodes a string of text.
40 *
41 * @param {string} text The text to encode.
42 * @return {string} The base64-encoded string.
43 */
44 function base64Encode(text) {
45
46 if (/([^\u0000-\u00ff])/.test(text)) {
47 throw new Error('Can\'t base64 encode non-ASCII characters.');
48 }
49
50 var i = 0,
51 cur, prev, byteNum,
52 result = [];
53
54 while (i < text.length) {
55
56 cur = text.charCodeAt(i);
57 byteNum = i % 3;
58
59 switch (byteNum) {
60 case 0: //first byte
61 result.push(digits.charAt(cur >> 2));
62 break;
63
64 case 1: //second byte
65 result.push(digits.charAt((prev & 3) << 4 | (cur >> 4)));
66 break;
67
68 case 2: //third byte
69 result.push(digits.charAt((prev & 0x0f) << 2 | (cur >> 6)));
70 result.push(digits.charAt(cur & 0x3f));
71 break;
72 }
73
74 prev = cur;
75 i += 1;
76 }
77
78 if (byteNum === 0) {
79 result.push(digits.charAt((prev & 3) << 4));
80 result.push('==');
81 } else if (byteNum === 1) {
82 result.push(digits.charAt((prev & 0x0f) << 2));
83 result.push('=');
84 }
85
86 return result.join('');
87 }
88
89 /**
90 * Base64-decodes a string of text.
91 *
92 * @param {string} text The text to decode.
93 * @return {string} The base64-decoded string.
94 */
95 function base64Decode(text) {
96
97 //ignore white space
98 text = text.replace(/\s/g, '');
99
100 //first check for any unexpected input
101 if (!(/^[a-z0-9\+\/\s]+\={0,2}$/i.test(text)) || text.length % 4 > 0) {
102 throw new Error('Not a base64-encoded string.');
103 }
104
105 //local variables
106 var cur, prev, digitNum,
107 i = 0,
108 result = [];
109
110 //remove any equals signs
111 text = text.replace(/\=/g, '');
112
113 //loop over each character
114 while (i < text.length) {
115
116 cur = digits.indexOf(text.charAt(i));
117 digitNum = i % 4;
118
119 switch (digitNum) {
120
121 //case 0: first digit - do nothing, not enough info to work with
122
123 case 1: //second digit
124 result.push(String.fromCharCode(prev << 2 | cur >> 4));
125 break;
126
127 case 2: //third digit
128 result.push(String.fromCharCode((prev & 0x0f) << 4 | cur >> 2));
129 break;
130
131 case 3: //fourth digit
132 result.push(String.fromCharCode((prev & 3) << 6 | cur));
133 break;
134 }
135
136 prev = cur;
137 i += 1;
138 }
139
140 //return a string
141 return result.join('');
142
143 }
144
145 return {
146 encode: base64Encode,
147 decode: base64Decode
148 };
149
150 });
151
152}(
153 typeof define === 'function' && define.amd ? define : function (factory) { module.exports = factory(require); }
154 // Boilerplate for AMD and Node
155));