source: Dev/trunk/src/client/dojox/encoding/easy64.js @ 529

Last change on this file since 529 was 483, checked in by hendrikvanantwerpen, 11 years ago

Added Dojo 1.9.3 release.

File size: 1.5 KB
Line 
1define(["dojo/_base/lang"], function(lang) {
2        var easy64 = lang.getObject("dojox.encoding.easy64", true);
3        var c = function(input, length, result){
4                for(var i = 0; i < length; i += 3){
5                        result.push(
6                                String.fromCharCode((input[i] >>> 2) + 33),
7                                String.fromCharCode(((input[i] & 3) << 4) + (input[i + 1] >>> 4) + 33),
8                                String.fromCharCode(((input[i + 1] & 15) << 2) + (input[i + 2] >>> 6) + 33),
9                                String.fromCharCode((input[i + 2] & 63) + 33)
10                        );
11                }
12        };
13
14        easy64.encode = function(input){
15                // summary:
16                //              encodes input data in easy64 string
17                // input: Array
18                //              an array of numbers (0-255) to encode
19                var result = [], reminder = input.length % 3, length = input.length - reminder;
20                c(input, length, result);
21                if(reminder){
22                        var t = input.slice(length);
23                        while(t.length < 3){ t.push(0); }
24                        c(t, 3, result);
25                        for(var i = 3; i > reminder; result.pop(), --i);
26                }
27                return result.join(""); // String
28        };
29
30        easy64.decode = function(input){
31                // summary:
32                //              decodes the input string back to array of numbers
33                // input: String
34                //              the input string to decode
35                var n = input.length, r = [], b = [0, 0, 0, 0], i, j, d;
36                for(i = 0; i < n; i += 4){
37                        for(j = 0; j < 4; ++j){ b[j] = input.charCodeAt(i + j) - 33; }
38                        d = n - i;
39                        for(j = d; j < 4; b[++j] = 0);
40                        r.push(
41                                (b[0] << 2) + (b[1] >>> 4),
42                                ((b[1] & 15) << 4) + (b[2] >>> 2),
43                                ((b[2] & 3) << 6) + b[3]
44                        );
45                        for(j = d; j < 4; ++j, r.pop());
46                }
47                return r;
48        };
49
50        return easy64;
51});
Note: See TracBrowser for help on using the repository browser.