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

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     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