PageRenderTime 33ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/socket.io-client/lib/vendor/web-socket-js/flash-src/com/hurlant/util/Base64.as

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
ActionScript | 189 lines | 123 code | 27 blank | 39 comment | 32 complexity | 36e59a468d4441f82b325e48c052069e MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* Base64 library for ActionScript 3.0.
  2. * Based on: Ma Bingyao code.
  3. * Optimized by: Jean-Philippe Auclair / jpauclair.wordpress.com
  4. * Copyright (C) 2007 Ma Bingyao <andot@ujn.edu.cn>
  5. * LastModified: Oct 26, 2009
  6. * This library is free. You can redistribute it and/or modify it.
  7. */
  8. package com.hurlant.util{
  9. import flash.utils.ByteArray;
  10. public class Base64
  11. {
  12. private static const _encodeChars : Vector.<int> = InitEncoreChar();
  13. private static const _decodeChars : Vector.<int> = InitDecodeChar();
  14. public static function encodeByteArray(data : ByteArray) : String
  15. {
  16. var out : ByteArray = new ByteArray();
  17. //Presetting the length keep the memory smaller and optimize speed since there is no "grow" needed
  18. out.length = (2 + data.length - ((data.length + 2) % 3)) * 4 / 3; //Preset length //1.6 to 1.5 ms
  19. var i : int = 0;
  20. var r : int = data.length % 3;
  21. var len : int = data.length - r;
  22. var c : int; //read (3) character AND write (4) characters
  23. while (i < len)
  24. {
  25. //Read 3 Characters (8bit * 3 = 24 bits)
  26. c = data[i++] << 16 | data[i++] << 8 | data[i++];
  27. //Cannot optimize this to read int because of the positioning overhead. (as3 bytearray seek is slow)
  28. //Convert to 4 Characters (6 bit * 4 = 24 bits)
  29. c = (_encodeChars[c >>> 18] << 24) | (_encodeChars[c >>> 12 & 0x3f] << 16) | (_encodeChars[c >>> 6 & 0x3f] << 8) | _encodeChars[c & 0x3f];
  30. //Optimization: On older and slower computer, do one write Int instead of 4 write byte: 1.5 to 0.71 ms
  31. out.writeInt(c);
  32. /*
  33. out.writeByte(_encodeChars[c >> 18] );
  34. out.writeByte(_encodeChars[c >> 12 & 0x3f]);
  35. out.writeByte(_encodeChars[c >> 6 & 0x3f]);
  36. out.writeByte(_encodeChars[c & 0x3f]);
  37. */
  38. }
  39. if (r == 1) //Need two "=" padding
  40. {
  41. //Read one char, write two chars, write padding
  42. c = data[i];
  43. c = (_encodeChars[c >>> 2] << 24) | (_encodeChars[(c & 0x03) << 4] << 16) | 61 << 8 | 61;
  44. out.writeInt(c);
  45. }
  46. else if (r == 2) //Need one "=" padding
  47. {
  48. c = data[i++] << 8 | data[i];
  49. c = (_encodeChars[c >>> 10] << 24) | (_encodeChars[c >>> 4 & 0x3f] << 16) | (_encodeChars[(c & 0x0f) << 2] << 8) | 61;
  50. out.writeInt(c);
  51. }
  52. out.position = 0;
  53. return out.readUTFBytes(out.length);
  54. }
  55. public static function decodeToByteArray(str : String) : ByteArray
  56. {
  57. var c1 : int;
  58. var c2 : int;
  59. var c3 : int;
  60. var c4 : int;
  61. var i : int;
  62. var len : int;
  63. var out : ByteArray;
  64. len = str.length;
  65. i = 0;
  66. out = new ByteArray();
  67. var byteString : ByteArray = new ByteArray();
  68. byteString.writeUTFBytes(str);
  69. while (i < len)
  70. {
  71. //c1
  72. do
  73. {
  74. c1 = _decodeChars[byteString[i++]];
  75. } while (i < len && c1 == -1);
  76. if (c1 == -1) break;
  77. //c2
  78. do
  79. {
  80. c2 = _decodeChars[byteString[i++]];
  81. } while (i < len && c2 == -1);
  82. if (c2 == -1) break;
  83. out.writeByte((c1 << 2) | ((c2 & 0x30) >> 4));
  84. //c3
  85. do
  86. {
  87. c3 = byteString[i++];
  88. if (c3 == 61) return out;
  89. c3 = _decodeChars[c3];
  90. } while (i < len && c3 == -1);
  91. if (c3 == -1) break;
  92. out.writeByte(((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2));
  93. //c4
  94. do {
  95. c4 = byteString[i++];
  96. if (c4 == 61) return out;
  97. c4 = _decodeChars[c4];
  98. } while (i < len && c4 == -1);
  99. if (c4 == -1) break;
  100. out.writeByte(((c3 & 0x03) << 6) | c4);
  101. }
  102. out.position = 0;
  103. return out;
  104. }
  105. public static function encode(data : String) : String {
  106. // Convert string to ByteArray
  107. var bytes : ByteArray = new ByteArray();
  108. bytes.writeUTFBytes(data);
  109. // Return encoded ByteArray
  110. return encodeByteArray(bytes);
  111. }
  112. public static function decode(data : String) : String {
  113. // Decode data to ByteArray
  114. var bytes : ByteArray = decodeToByteArray(data);
  115. // Convert to string and return
  116. return bytes.readUTFBytes(bytes.length);
  117. }
  118. public static function InitEncoreChar() : Vector.<int>
  119. {
  120. var encodeChars : Vector.<int> = new Vector.<int>();
  121. // We could push the number directly, but i think it's nice to see the characters (with no overhead on encode/decode)
  122. var chars : String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  123. for (var i : int = 0; i < 64; i++)
  124. {
  125. encodeChars.push(chars.charCodeAt(i));
  126. }
  127. /*
  128. encodeChars.push(
  129. 65, 66, 67, 68, 69, 70, 71, 72,
  130. 73, 74, 75, 76, 77, 78, 79, 80,
  131. 81, 82, 83, 84, 85, 86, 87, 88,
  132. 89, 90, 97, 98, 99, 100, 101, 102,
  133. 103, 104, 105, 106, 107, 108, 109, 110,
  134. 111, 112, 113, 114, 115, 116, 117, 118,
  135. 119, 120, 121, 122, 48, 49, 50, 51,
  136. 52, 53, 54, 55, 56, 57, 43, 47);
  137. */
  138. return encodeChars;
  139. }
  140. public static function InitDecodeChar() : Vector.<int>
  141. {
  142. var decodeChars : Vector.<int> = new Vector.<int>();
  143. decodeChars.push(-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  144. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  145. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  146. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
  147. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  148. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  149. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  150. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
  151. - 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  152. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  153. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  154. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  155. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  156. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  157. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  158. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
  159. return decodeChars;
  160. }
  161. }
  162. }