1/*
2 * Modified from:
3 * http://lxr.mozilla.org/mozilla/source/extensions/xml-rpc/src/nsXmlRpcClient.js#956
4 */
5
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
44/*jslint white: false, bitwise: false, plusplus: false */
45/*global console */
46
47var Base64 = {
48
49/* Convert data (an array of integers) to a Base64 string. */
50toBase64Table : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
51base64Pad     : '=',
52
53encode: function (data) {
54    "use strict";
55    var result = '',
56        chrTable = Base64.toBase64Table.split(''),
57        pad = Base64.base64Pad,
58        length = data.length,
59        i;
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];
72        if ((length%3) === 2) {
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;
79        }
80    }
81
82    return result;
83},
84
85/* Convert Base64 data to a string */
86toBinaryTable : [
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
97decode: function (data, offset) {
98    "use strict";
99    offset = typeof(offset) !== 'undefined' ? offset : 0;
100    var binTable = Base64.toBinaryTable,
101        pad = Base64.base64Pad,
102        result, result_length, idx, i, c, padding,
103        leftbits = 0, // number of bits decoded, but yet to be appended
104        leftdata = 0, // bits decoded, but yet to be appended
105        data_length = data.indexOf('=') - offset;
106
107    if (data_length < 0) { data_length = data.length - offset; }
108
109    /* Every four characters is 3 resulting numbers */
110    result_length = (data_length >> 2) * 3 + Math.floor((data_length%4)/1.5);
111    result = new Array(result_length);
112
113    // Convert one by one.
114    for (idx = 0, i = offset; i < data.length; i++) {
115        c = binTable[data.charCodeAt(i) & 0x7f];
116        padding = (data.charAt(i) === pad);
117        // Skip illegal characters and whitespace
118        if (c === -1) {
119            console.error("Illegal character code " + data.charCodeAt(i) + " at position " + i);
120            continue;
121        }
122
123        // Collect data into leftdata, update bitcount
124        leftdata = (leftdata << 6) | c;
125        leftbits += 6;
126
127        // If we have 8 or more bits, append 8 bits to the result
128        if (leftbits >= 8) {
129            leftbits -= 8;
130            // Append if not padding.
131            if (!padding) {
132                result[idx++] = (leftdata >> leftbits) & 0xff;
133            }
134            leftdata &= (1 << leftbits) - 1;
135        }
136    }
137
138    // If there are any bits left, the base64 string was corrupted
139    if (leftbits) {
140        throw {name: 'Base64-Error',
141               message: 'Corrupted base64 string'};
142    }
143
144    return result;
145}
146
147}; /* End of Base64 namespace */
148