]> git.pond.sub.org Git - eow/blob - static/dojo-release-1.1.1/dojox/encoding/easy64.js
add Dojo 1.1.1
[eow] / static / dojo-release-1.1.1 / dojox / encoding / easy64.js
1 if(!dojo._hasResource["dojox.encoding.easy64"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
2 dojo._hasResource["dojox.encoding.easy64"] = true;
3 dojo.provide("dojox.encoding.easy64");
4
5 (function(){
6         var c = function(input, length, result){
7                 for(var i = 0; i < length; i += 3){
8                         result.push(
9                                 String.fromCharCode((input[i] >>> 2) + 33),
10                                 String.fromCharCode(((input[i] & 3) << 4) + (input[i + 1] >>> 4) + 33),
11                                 String.fromCharCode(((input[i + 1] & 15) << 2) + (input[i + 2] >>> 6) + 33),
12                                 String.fromCharCode((input[i + 2] & 63) + 33)
13                         );
14                 }
15         };
16         
17         dojox.encoding.easy64.encode = function(input){
18                 // summary: encodes input data in easy64 string
19                 // input: Array: an array of numbers (0-255) to encode
20                 var result = [], reminder = input.length % 3, length = input.length - reminder;
21                 c(input, length, result);
22                 if(reminder){
23                         var t = input.slice(length);
24                         while(t.length < 3){ t.push(0); }
25                         c(t, 3, result);
26                         for(var i = 3; i > reminder; result.pop(), --i);
27                 }
28                 return result.join(""); // String
29         };
30
31         dojox.encoding.easy64.decode = function(input){
32                 // summary: decodes the input string back to array of numbers
33                 // input: String: the input string to decode
34                 var n = input.length, r = [], b = [0, 0, 0, 0], i, j, d;
35                 for(i = 0; i < n; i += 4){
36                         for(j = 0; j < 4; ++j){ b[j] = input.charCodeAt(i + j) - 33; }
37                         d = n - i;
38                         for(j = d; j < 4; b[++j] = 0);
39                         r.push(
40                                 (b[0] << 2) + (b[1] >>> 4),
41                                 ((b[1] & 15) << 4) + (b[2] >>> 2),
42                                 ((b[2] & 3) << 6) + b[3]
43                         );
44                         for(j = d; j < 4; ++j, r.pop());
45                 }
46                 return r;
47         };
48 })();
49
50 }