source: Dev/branches/rest-dojo-ui/client/dojox/encoding/easy64.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 1.5 KB
Line 
1define(["dojo/_base/lang"], function(lang) {
2        var easy64 = lang.getObject("dojox.encoding.easy64", true);
3        /*=====
4                easy64 = dojox.encoding.easy64;
5        =====*/
6
7        var c = function(input, length, result){
8                for(var i = 0; i < length; i += 3){
9                        result.push(
10                                String.fromCharCode((input[i] >>> 2) + 33),
11                                String.fromCharCode(((input[i] & 3) << 4) + (input[i + 1] >>> 4) + 33),
12                                String.fromCharCode(((input[i + 1] & 15) << 2) + (input[i + 2] >>> 6) + 33),
13                                String.fromCharCode((input[i + 2] & 63) + 33)
14                        );
15                }
16        };
17
18        easy64.encode = function(input){
19                // summary: encodes input data in easy64 string
20                // input: Array: an array of numbers (0-255) to encode
21                var result = [], reminder = input.length % 3, length = input.length - reminder;
22                c(input, length, result);
23                if(reminder){
24                        var t = input.slice(length);
25                        while(t.length < 3){ t.push(0); }
26                        c(t, 3, result);
27                        for(var i = 3; i > reminder; result.pop(), --i);
28                }
29                return result.join(""); // String
30        };
31
32        easy64.decode = function(input){
33                // summary: decodes the input string back to array of numbers
34                // input: String: 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.