]> git.r.bdr.sh - rbdr/dotfiles/blob
863cc4650ce884dda6caa626d1e039ac535fbea6
[rbdr/dotfiles] /
1 /* -*- Mode: js; js-indent-level: 2; -*- */
2 /*
3 * Copyright 2011 Mozilla Foundation and contributors
4 * Licensed under the New BSD license. See LICENSE or:
5 * http://opensource.org/licenses/BSD-3-Clause
6 */
7 if (typeof define !== 'function') {
8 var define = require('amdefine')(module, require);
9 }
10 define(function (require, exports, module) {
11
12 var charToIntMap = {};
13 var intToCharMap = {};
14
15 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
16 .split('')
17 .forEach(function (ch, index) {
18 charToIntMap[ch] = index;
19 intToCharMap[index] = ch;
20 });
21
22 /**
23 * Encode an integer in the range of 0 to 63 to a single base 64 digit.
24 */
25 exports.encode = function base64_encode(aNumber) {
26 if (aNumber in intToCharMap) {
27 return intToCharMap[aNumber];
28 }
29 throw new TypeError("Must be between 0 and 63: " + aNumber);
30 };
31
32 /**
33 * Decode a single base 64 digit to an integer.
34 */
35 exports.decode = function base64_decode(aChar) {
36 if (aChar in charToIntMap) {
37 return charToIntMap[aChar];
38 }
39 throw new TypeError("Not a valid base 64 digit: " + aChar);
40 };
41
42 });