source: Dev/branches/rest-dojo-ui/client/dojox/encoding/ascii85.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.7 KB
Line 
1define(["dojo/_base/lang"], function(lang) {
2
3        var ascii85 = lang.getObject("dojox.encoding.ascii85", true);
4        /*=====
5                ascii85 = dojox.encoding.ascii85;
6        =====*/
7
8        var c = function(input, length, result){
9                var i, j, n, b = [0, 0, 0, 0, 0];
10                for(i = 0; i < length; i += 4){
11                        n = ((input[i] * 256 + input[i+1]) * 256 + input[i+2]) * 256 + input[i+3];
12                        if(!n){
13                                result.push("z");
14                        }else{
15                                for(j = 0; j < 5; b[j++] = n % 85 + 33, n = Math.floor(n / 85));
16                        }
17                        result.push(String.fromCharCode(b[4], b[3], b[2], b[1], b[0]));
18                }
19        };
20
21        ascii85.encode = function(input){
22                // summary: encodes input data in ascii85 string
23                // input: Array: an array of numbers (0-255) to encode
24                var result = [], reminder = input.length % 4, length = input.length - reminder;
25                c(input, length, result);
26                if(reminder){
27                        var t = input.slice(length);
28                        while(t.length < 4){ t.push(0); }
29                        c(t, 4, result);
30                        var x = result.pop();
31                        if(x == "z"){ x = "!!!!!"; }
32                        result.push(x.substr(0, reminder + 1));
33                }
34                return result.join(""); // String
35        };
36
37        ascii85.decode = function(input){
38                // summary: decodes the input string back to array of numbers
39                // input: String: the input string to decode
40                var n = input.length, r = [], b = [0, 0, 0, 0, 0], i, j, t, x, y, d;
41                for(i = 0; i < n; ++i){
42                        if(input.charAt(i) == "z"){
43                                r.push(0, 0, 0, 0);
44                                continue;
45                        }
46                        for(j = 0; j < 5; ++j){ b[j] = input.charCodeAt(i + j) - 33; }
47                        d = n - i;
48                        if(d < 5){
49                                for(j = d; j < 4; b[++j] = 0);
50                                b[d] = 85;
51                        }
52                        t = (((b[0] * 85 + b[1]) * 85 + b[2]) * 85 + b[3]) * 85 + b[4];
53                        x = t & 255;
54                        t >>>= 8;
55                        y = t & 255;
56                        t >>>= 8;
57                        r.push(t >>> 8, t & 255, y, x);
58                        for(j = d; j < 5; ++j, r.pop());
59                        i += 4;
60                }
61                return r;
62        };
63
64        return ascii85;
65});
Note: See TracBrowser for help on using the repository browser.