1 /* -*- Mode: js; js-indent-level: 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
7 if (typeof define !== 'function') {
8 var define = require('amdefine')(module, require);
10 define(function (require, exports, module) {
12 var charToIntMap = {};
13 var intToCharMap = {};
15 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
17 .forEach(function (ch, index) {
18 charToIntMap[ch] = index;
19 intToCharMap[index] = ch;
23 * Encode an integer in the range of 0 to 63 to a single base 64 digit.
25 exports.encode = function base64_encode(aNumber) {
26 if (aNumber in intToCharMap) {
27 return intToCharMap[aNumber];
29 throw new TypeError("Must be between 0 and 63: " + aNumber);
33 * Decode a single base 64 digit to an integer.
35 exports.decode = function base64_decode(aChar) {
36 if (aChar in charToIntMap) {
37 return charToIntMap[aChar];
39 throw new TypeError("Not a valid base 64 digit: " + aChar);