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