summaryrefslogtreecommitdiffstats
path: root/comm/mail/extensions/openpgp/content/modules/data.jsm
blob: 0dd5cf451fd7847dc6022d50a930ead227bf8b12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

"use strict";

var EXPORTED_SYMBOLS = ["EnigmailData"];

const SCRIPTABLEUNICODECONVERTER_CONTRACTID =
  "@mozilla.org/intl/scriptableunicodeconverter";

const HEX_TABLE = "0123456789abcdef";

function converter(charset) {
  let unicodeConv = Cc[SCRIPTABLEUNICODECONVERTER_CONTRACTID].getService(
    Ci.nsIScriptableUnicodeConverter
  );
  unicodeConv.charset = charset || "utf-8";
  return unicodeConv;
}

var EnigmailData = {
  getUnicodeData(data) {
    if (!data) {
      throw new Error("EnigmailData.getUnicodeData invalid parameter");
    }
    // convert output to Unicode
    var tmpStream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
      Ci.nsIStringInputStream
    );
    tmpStream.setData(data, data.length);
    var inStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
      Ci.nsIScriptableInputStream
    );
    inStream.init(tmpStream);
    return inStream.read(tmpStream.available());
  },

  decodeQuotedPrintable(str) {
    return unescape(
      str.replace(/%/g, "=25").replace(new RegExp("=", "g"), "%")
    );
  },

  decodeBase64(str) {
    return atob(str.replace(/[\s\r\n]*/g, ""));
  },

  /***
   * Encode a string in base64, with a max. line length of 72 characters
   */
  encodeBase64(str) {
    return btoa(str).replace(/(.{72})/g, "$1\r\n");
  },

  convertToUnicode(text, charset) {
    if (!text || (charset && charset.toLowerCase() == "iso-8859-1")) {
      return text;
    }

    // Encode plaintext
    let buffer = Uint8Array.from(text, c => c.charCodeAt(0));
    return new TextDecoder(charset).decode(buffer);
  },

  convertFromUnicode(text, charset) {
    if (!text) {
      return "";
    }

    let conv = converter(charset);
    let result = conv.ConvertFromUnicode(text);
    result += conv.Finish();
    return result;
  },

  convertGpgToUnicode(text) {
    if (typeof text === "string") {
      text = text.replace(/\\x3a/gi, "\\e3A");
      var a = text.search(/\\x[0-9a-fA-F]{2}/);
      while (a >= 0) {
        var ch = unescape("%" + text.substr(a + 2, 2));
        var r = new RegExp("\\" + text.substr(a, 4));
        text = text.replace(r, ch);

        a = text.search(/\\x[0-9a-fA-F]{2}/);
      }

      text = EnigmailData.convertToUnicode(text, "utf-8").replace(
        /\\e3A/g,
        ":"
      );
    }

    return text;
  },

  pack(value, bytes) {
    let str = "";
    let mask = 0xff;
    for (let j = 0; j < bytes; j++) {
      str = String.fromCharCode((value & mask) >> (j * 8)) + str;
      mask <<= 8;
    }

    return str;
  },

  unpack(str) {
    let len = str.length;
    let value = 0;

    for (let j = 0; j < len; j++) {
      value <<= 8;
      value |= str.charCodeAt(j);
    }

    return value;
  },

  bytesToHex(str) {
    let len = str.length;

    let hex = "";
    for (let j = 0; j < len; j++) {
      let charCode = str.charCodeAt(j);
      hex +=
        HEX_TABLE.charAt((charCode & 0xf0) >> 4) +
        HEX_TABLE.charAt(charCode & 0x0f);
    }

    return hex;
  },

  /**
   * Convert an ArrayBuffer (or Uint8Array) object into a string
   */
  arrayBufferToString(buffer) {
    const MAXLEN = 102400;

    let uArr = new Uint8Array(buffer);
    let ret = "";
    let len = buffer.byteLength;

    for (let j = 0; j < Math.floor(len / MAXLEN) + 1; j++) {
      ret += String.fromCharCode.apply(
        null,
        uArr.subarray(j * MAXLEN, (j + 1) * MAXLEN)
      );
    }

    return ret;
  },
};