]>
Commit | Line | Data |
---|---|---|
b5537b60 JM |
1 | /* |
2 | * Modified from: | |
3 | * http://lxr.mozilla.org/mozilla/source/extensions/xml-rpc/src/nsXmlRpcClient.js#956 | |
4 | */ | |
cc0410a3 | 5 | |
b5537b60 JM |
6 | /* ***** BEGIN LICENSE BLOCK ***** |
7 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 | |
8 | * | |
9 | * The contents of this file are subject to the Mozilla Public License Version | |
10 | * 1.1 (the "License"); you may not use this file except in compliance with | |
11 | * the License. You may obtain a copy of the License at | |
12 | * http://www.mozilla.org/MPL/ | |
13 | * | |
14 | * Software distributed under the License is distributed on an "AS IS" basis, | |
15 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License | |
16 | * for the specific language governing rights and limitations under the | |
17 | * License. | |
18 | * | |
19 | * The Original Code is Mozilla XML-RPC Client component. | |
20 | * | |
21 | * The Initial Developer of the Original Code is | |
22 | * Digital Creations 2, Inc. | |
23 | * Portions created by the Initial Developer are Copyright (C) 2000 | |
24 | * the Initial Developer. All Rights Reserved. | |
25 | * | |
26 | * Contributor(s): | |
27 | * Martijn Pieters <mj@digicool.com> (original author) | |
28 | * Samuel Sieb <samuel@sieb.net> | |
29 | * | |
30 | * Alternatively, the contents of this file may be used under the terms of | |
31 | * either the GNU General Public License Version 2 or later (the "GPL"), or | |
32 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), | |
33 | * in which case the provisions of the GPL or the LGPL are applicable instead | |
34 | * of those above. If you wish to allow use of your version of this file only | |
35 | * under the terms of either the GPL or the LGPL, and not to allow others to | |
36 | * use your version of this file under the terms of the MPL, indicate your | |
37 | * decision by deleting the provisions above and replace them with the notice | |
38 | * and other provisions required by the GPL or the LGPL. If you do not delete | |
39 | * the provisions above, a recipient may use your version of this file under | |
40 | * the terms of any one of the MPL, the GPL or the LGPL. | |
41 | * | |
42 | * ***** END LICENSE BLOCK ***** */ | |
43 | ||
d595e656 JM |
44 | "use strict"; |
45 | /*jslint white: false, bitwise: false, plusplus: false */ | |
46 | /*global console */ | |
47 | ||
48 | var Base64 = { | |
b5537b60 JM |
49 | |
50 | /* Convert data (an array of integers) to a Base64 string. */ | |
51 | toBase64Table : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', | |
52 | base64Pad : '=', | |
53 | ||
54 | encode: function (data) { | |
d595e656 JM |
55 | var result = '', |
56 | chrTable = Base64.toBase64Table.split(''), | |
57 | pad = Base64.base64Pad, | |
58 | length = data.length, | |
59 | i; | |
b5537b60 JM |
60 | // Convert every three bytes to 4 ascii characters. |
61 | for (i = 0; i < (length - 2); i += 3) { | |
62 | result += chrTable[data[i] >> 2]; | |
63 | result += chrTable[((data[i] & 0x03) << 4) + (data[i+1] >> 4)]; | |
64 | result += chrTable[((data[i+1] & 0x0f) << 2) + (data[i+2] >> 6)]; | |
65 | result += chrTable[data[i+2] & 0x3f]; | |
66 | } | |
67 | ||
68 | // Convert the remaining 1 or 2 bytes, pad out to 4 characters. | |
69 | if (length%3) { | |
70 | i = length - (length%3); | |
71 | result += chrTable[data[i] >> 2]; | |
d595e656 | 72 | if ((length%3) === 2) { |
b5537b60 JM |
73 | result += chrTable[((data[i] & 0x03) << 4) + (data[i+1] >> 4)]; |
74 | result += chrTable[(data[i+1] & 0x0f) << 2]; | |
75 | result += pad; | |
76 | } else { | |
77 | result += chrTable[(data[i] & 0x03) << 4]; | |
78 | result += pad + pad; | |
cc0410a3 | 79 | } |
b5537b60 JM |
80 | } |
81 | ||
82 | return result; | |
83 | }, | |
84 | ||
85 | /* Convert Base64 data to a string */ | |
86 | toBinaryTable : [ | |
87 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | |
88 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, | |
89 | -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63, | |
90 | 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1, | |
91 | -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14, | |
92 | 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1, | |
93 | -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40, | |
94 | 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1 | |
95 | ], | |
96 | ||
5d8e7ec0 | 97 | decode: function (data, offset) { |
d595e656 JM |
98 | offset = typeof(offset) !== 'undefined' ? offset : 0; |
99 | var binTable = Base64.toBinaryTable, | |
100 | pad = Base64.base64Pad, | |
101 | result, result_length, idx, i, c, padding, | |
102 | leftbits = 0, // number of bits decoded, but yet to be appended | |
103 | leftdata = 0, // bits decoded, but yet to be appended | |
104 | data_length = data.indexOf('=') - offset; | |
b5537b60 | 105 | |
d595e656 | 106 | if (data_length < 0) { data_length = data.length - offset; } |
5d8e7ec0 | 107 | |
d595e656 JM |
108 | /* Every four characters is 3 resulting numbers */ |
109 | result_length = (data_length >> 2) * 3 + Math.floor((data_length%4)/1.5); | |
110 | result = new Array(result_length); | |
b5537b60 JM |
111 | |
112 | // Convert one by one. | |
d595e656 JM |
113 | for (idx = 0, i = offset; i < data.length; i++) { |
114 | c = binTable[data.charCodeAt(i) & 0x7f]; | |
115 | padding = (data.charAt(i) === pad); | |
b5537b60 | 116 | // Skip illegal characters and whitespace |
d595e656 | 117 | if (c === -1) { |
81e5adaf | 118 | console.error("Illegal character '" + data.charCodeAt(i) + "'"); |
5d8e7ec0 JM |
119 | continue; |
120 | } | |
b5537b60 JM |
121 | |
122 | // Collect data into leftdata, update bitcount | |
123 | leftdata = (leftdata << 6) | c; | |
124 | leftbits += 6; | |
125 | ||
126 | // If we have 8 or more bits, append 8 bits to the result | |
127 | if (leftbits >= 8) { | |
128 | leftbits -= 8; | |
129 | // Append if not padding. | |
d595e656 | 130 | if (!padding) { |
b5537b60 | 131 | result[idx++] = (leftdata >> leftbits) & 0xff; |
d595e656 | 132 | } |
b5537b60 | 133 | leftdata &= (1 << leftbits) - 1; |
cc0410a3 | 134 | } |
cc0410a3 | 135 | } |
b5537b60 JM |
136 | |
137 | // If there are any bits left, the base64 string was corrupted | |
d595e656 | 138 | if (leftbits) { |
db504ade JM |
139 | throw {name: 'Base64-Error', |
140 | message: 'Corrupted base64 string'}; | |
d595e656 | 141 | } |
b5537b60 JM |
142 | |
143 | return result; | |
cc0410a3 | 144 | } |
b5537b60 JM |
145 | |
146 | }; /* End of Base64 namespace */ |