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