PageRenderTime 39ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/dynamictable/public/phpolait/jsolait/lib/codecs.js

http://pyjamas.googlecode.com/
JavaScript | 103 lines | 103 code | 0 blank | 0 comment | 25 complexity | 14b6a710c54b8369b79f456e4e28d155 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. Module("codecs", "0.1.2", function(mod){
  2. mod.listEncoders=function(){
  3. var c=[];
  4. for(var attr in String.prototype){
  5. if(attr.slice(0, 7) == "encode_"){
  6. c.push(attr.slice(7));
  7. }
  8. }
  9. return c;
  10. }
  11. mod.listDecoders=function(){
  12. var c=[];
  13. for(var attr in String.prototype){
  14. if(attr.slice(0, 7) == "decode_"){
  15. c.push(attr.slice(7));
  16. }
  17. }
  18. return c;
  19. }
  20. String.prototype.decode = function(codec){
  21. var n ="decode_" + codec;
  22. if(String.prototype[n]){
  23. var args=[];
  24. for(var i=1;i<arguments.length;i++){
  25. args[i-1] = arguments[i];
  26. }
  27. return String.prototype[n].apply(this, args);
  28. }else{
  29. throw new mod.Exception("Decoder '%s' not found.".format(codec));
  30. }
  31. }
  32. String.prototype.encode = function(codec){
  33. var n ="encode_" + codec;
  34. if(String.prototype[n]){
  35. var args=[];
  36. for(var i=1;i<arguments.length;i++){
  37. args[i-1] = arguments[i];
  38. }
  39. return String.prototype[n].apply(this, args);
  40. }else{
  41. throw new mod.Exception("Ecnoder '%s' not found.".format(codec));
  42. }
  43. }
  44. String.prototype.decode_base64=function(){
  45. if((this.length % 4) == 0){
  46. if(typeof(atob) != "undefined"){
  47. return atob(this);
  48. }else{
  49. var nBits;
  50. var sDecoded = new Array(this.length /4);
  51. var base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  52. for(var i=0; i < this.length; i+=4){
  53. nBits = (base64.indexOf(this.charAt(i)) & 0xff) << 18 |
  54. (base64.indexOf(this.charAt(i+1)) & 0xff) << 12 |
  55. (base64.indexOf(this.charAt(i+2)) & 0xff) << 6 |
  56. base64.indexOf(this.charAt(i+3)) & 0xff;
  57. sDecoded[i] = String.fromCharCode((nBits & 0xff0000) >> 16, (nBits & 0xff00) >> 8, nBits & 0xff);
  58. }
  59. sDecoded[sDecoded.length-1] = sDecoded[sDecoded.length-1].substring(0, 3 - ((this.charCodeAt(i - 2) == 61) ? 2 : (this.charCodeAt(i - 1) == 61 ? 1 : 0)));
  60. return sDecoded.join("");
  61. }
  62. }else{
  63. throw new mod.Exception("String length must be divisible by 4.");
  64. }
  65. }
  66. String.prototype.encode_base64=function(){
  67. if(typeof(btoa) != "undefined"){
  68. return btoa(this);
  69. }else{
  70. var base64 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
  71. 'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
  72. '0','1','2','3','4','5','6','7','8','9','+','/'];
  73. var sbin;
  74. var pad=0;
  75. var s="" + this;
  76. if((s.length % 3) == 1){
  77. s+=String.fromCharCode(0);
  78. s+=String.fromCharCode(0);
  79. pad=2;
  80. }else if((s.length % 3) == 2){
  81. s+=String.fromCharCode(0);
  82. pad=1;
  83. }
  84. var rslt=new Array(s.length / 3);
  85. var ri=0;
  86. for(var i=0;i<s.length; i+=3){
  87. sbin=((s.charCodeAt(i) & 0xff) << 16) | ((s.charCodeAt(i+1) & 0xff ) << 8) | (s.charCodeAt(i+2) & 0xff);
  88. rslt[ri] = (base64[(sbin >> 18) & 0x3f] + base64[(sbin >> 12) & 0x3f] + base64[(sbin >>6) & 0x3f] + base64[sbin & 0x3f]);
  89. ri++;
  90. }
  91. if(pad>0){
  92. rslt[rslt.length-1] = rslt[rslt.length-1].substr(0, 4-pad) + ((pad==2) ? "==" : (pad==1) ? "=" : "");
  93. }
  94. return rslt.join("");
  95. }
  96. }
  97. String.prototype.decode_uri=function(){
  98. return decodeURI(this);
  99. }
  100. String.prototype.encode_uri=function(){
  101. return encodeURI(this);
  102. }
  103. })