ascii85.js 2.0 KB

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