2
0

URIEncodingBitmap.as 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Copyright (c) 2008, Adobe Systems Incorporated
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are
  6. met:
  7. * Redistributions of source code must retain the above copyright notice,
  8. this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * Neither the name of Adobe Systems Incorporated nor the names of its
  13. contributors may be used to endorse or promote products derived from
  14. this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  16. IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  17. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  18. PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  19. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  20. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  22. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. package com.adobe.net
  28. {
  29. import flash.utils.ByteArray;
  30. /**
  31. * This class implements an efficient lookup table for URI
  32. * character escaping. This class is only needed if you
  33. * create a derived class of URI to handle custom URI
  34. * syntax. This class is used internally by URI.
  35. *
  36. * @langversion ActionScript 3.0
  37. * @playerversion Flash 9.0*
  38. */
  39. public class URIEncodingBitmap extends ByteArray
  40. {
  41. /**
  42. * Constructor. Creates an encoding bitmap using the given
  43. * string of characters as the set of characters that need
  44. * to be URI escaped.
  45. *
  46. * @langversion ActionScript 3.0
  47. * @playerversion Flash 9.0
  48. */
  49. public function URIEncodingBitmap(charsToEscape:String) : void
  50. {
  51. var i:int;
  52. var data:ByteArray = new ByteArray();
  53. // Initialize our 128 bits (16 bytes) to zero
  54. for (i = 0; i < 16; i++)
  55. this.writeByte(0);
  56. data.writeUTFBytes(charsToEscape);
  57. data.position = 0;
  58. while (data.bytesAvailable)
  59. {
  60. var c:int = data.readByte();
  61. if (c > 0x7f)
  62. continue; // only escape low bytes
  63. var enc:int;
  64. this.position = (c >> 3);
  65. enc = this.readByte();
  66. enc |= 1 << (c & 0x7);
  67. this.position = (c >> 3);
  68. this.writeByte(enc);
  69. }
  70. }
  71. /**
  72. * Based on the data table contained in this object, check
  73. * if the given character should be escaped.
  74. *
  75. * @param char the character to be escaped. Only the first
  76. * character in the string is used. Any other characters
  77. * are ignored.
  78. *
  79. * @return the integer value of the raw UTF8 character. For
  80. * example, if '%' is given, the return value is 37 (0x25).
  81. * If the character given does not need to be escaped, the
  82. * return value is zero.
  83. *
  84. * @langversion ActionScript 3.0
  85. * @playerversion Flash 9.0
  86. */
  87. public function ShouldEscape(char:String) : int
  88. {
  89. var data:ByteArray = new ByteArray();
  90. var c:int, mask:int;
  91. // write the character into a ByteArray so
  92. // we can pull it out as a raw byte value.
  93. data.writeUTFBytes(char);
  94. data.position = 0;
  95. c = data.readByte();
  96. if (c & 0x80)
  97. {
  98. // don't escape high byte characters. It can make international
  99. // URI's unreadable. We just want to escape characters that would
  100. // make URI syntax ambiguous.
  101. return 0;
  102. }
  103. else if ((c < 0x1f) || (c == 0x7f))
  104. {
  105. // control characters must be escaped.
  106. return c;
  107. }
  108. this.position = (c >> 3);
  109. mask = this.readByte();
  110. if (mask & (1 << (c & 0x7)))
  111. {
  112. // we need to escape this, return the numeric value
  113. // of the character
  114. return c;
  115. }
  116. else
  117. {
  118. return 0;
  119. }
  120. }
  121. }
  122. }