addon-sdk/source/lib/sdk/base64.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 "use strict";
     7 module.metadata = {
     8   "stability": "unstable"
     9 };
    11 const { Cu } = require("chrome");
    13 // Passing an empty object as second argument to avoid scope's pollution
    14 const { atob, btoa } = Cu.import("resource://gre/modules/Services.jsm", {});
    16 function isUTF8(charset) {
    17   let type = typeof charset;
    19   if (type === "undefined")
    20     return false;
    22   if (type === "string" && charset.toLowerCase() === "utf-8")
    23     return true;
    25   throw new Error("The charset argument can be only 'utf-8'");
    26 }
    28 exports.decode = function (data, charset) {
    29   if (isUTF8(charset))
    30 		return decodeURIComponent(escape(atob(data)))
    32 	return atob(data);
    33 }
    35 exports.encode = function (data, charset) {
    36   if (isUTF8(charset))
    37     return btoa(unescape(encodeURIComponent(data)))
    39 	return btoa(data);
    40 }

mercurial