ascii85.js 1.7 KB

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