Project

General

Profile

Statistics
| Branch: | Revision:

colonymech / docs / www / colonyscout / internal / includes / uploadify / com / adobe / net / URIEncodingBitmap.as @ f59acf11

History | View | Annotate | Download (4.13 KB)

1
/*
2
  Copyright (c) 2008, Adobe Systems Incorporated
3
  All rights reserved.
4

    
5
  Redistribution and use in source and binary forms, with or without 
6
  modification, are permitted provided that the following conditions are
7
  met:
8

    
9
  * Redistributions of source code must retain the above copyright notice, 
10
    this list of conditions and the following disclaimer.
11
  
12
  * Redistributions in binary form must reproduce the above copyright
13
    notice, this list of conditions and the following disclaimer in the 
14
    documentation and/or other materials provided with the distribution.
15
  
16
  * Neither the name of Adobe Systems Incorporated nor the names of its 
17
    contributors may be used to endorse or promote products derived from 
18
    this software without specific prior written permission.
19

    
20
  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23
  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
24
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
*/
32

    
33
package com.adobe.net
34
{
35
	import flash.utils.ByteArray;
36
	
37
	/**
38
	 * This class implements an efficient lookup table for URI
39
	 * character escaping.  This class is only needed if you
40
	 * create a derived class of URI to handle custom URI
41
	 * syntax.  This class is used internally by URI.
42
	 * 
43
	 * @langversion ActionScript 3.0
44
	 * @playerversion Flash 9.0* 
45
	 */
46
	public class URIEncodingBitmap extends ByteArray
47
	{
48
		/**
49
		 * Constructor.  Creates an encoding bitmap using the given
50
		 * string of characters as the set of characters that need
51
		 * to be URI escaped.
52
		 * 
53
		 * @langversion ActionScript 3.0
54
		 * @playerversion Flash 9.0
55
		 */
56
		public function URIEncodingBitmap(charsToEscape:String) : void
57
		{
58
			var i:int;
59
			var data:ByteArray = new ByteArray();
60
			
61
			// Initialize our 128 bits (16 bytes) to zero
62
			for (i = 0; i < 16; i++)
63
				this.writeByte(0);
64
				
65
			data.writeUTFBytes(charsToEscape);
66
			data.position = 0;
67
			
68
			while (data.bytesAvailable)
69
			{
70
				var c:int = data.readByte();
71
				
72
				if (c > 0x7f)
73
					continue;  // only escape low bytes
74
					
75
				var enc:int;
76
				this.position = (c >> 3);
77
				enc = this.readByte();
78
				enc |= 1 << (c & 0x7);
79
				this.position = (c >> 3);
80
				this.writeByte(enc);
81
			}
82
		}
83
		
84
		/**
85
		 * Based on the data table contained in this object, check
86
		 * if the given character should be escaped.
87
		 * 
88
		 * @param char	the character to be escaped.  Only the first
89
		 * character in the string is used.  Any other characters
90
		 * are ignored.
91
		 * 
92
		 * @return	the integer value of the raw UTF8 character.  For
93
		 * example, if '%' is given, the return value is 37 (0x25).
94
		 * If the character given does not need to be escaped, the
95
		 * return value is zero.
96
		 * 
97
		 * @langversion ActionScript 3.0
98
		 * @playerversion Flash 9.0 
99
		 */
100
		public function ShouldEscape(char:String) : int
101
		{
102
			var data:ByteArray = new ByteArray();
103
			var c:int, mask:int;
104
			
105
			// write the character into a ByteArray so
106
			// we can pull it out as a raw byte value.
107
			data.writeUTFBytes(char);
108
			data.position = 0;
109
			c = data.readByte();
110
			
111
			if (c & 0x80)
112
			{
113
				// don't escape high byte characters.  It can make international
114
				// URI's unreadable.  We just want to escape characters that would
115
				// make URI syntax ambiguous.
116
				return 0;
117
			}
118
			else if ((c < 0x1f) || (c == 0x7f))
119
			{
120
				// control characters must be escaped.
121
				return c;
122
			}
123
			
124
			this.position = (c >> 3);
125
			mask = this.readByte();
126
			
127
			if (mask & (1 << (c & 0x7)))
128
			{
129
				// we need to escape this, return the numeric value
130
				// of the character
131
				return c;
132
			}
133
			else
134
			{
135
				return 0;
136
			}
137
		}
138
	}
139
}