Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright (C) 2011 Apple Inc. All rights reserved. |
michael@0 | 3 | * Copyright (C) 2011 Mozilla Corporation. All rights reserved. |
michael@0 | 4 | * |
michael@0 | 5 | * Redistribution and use in source and binary forms, with or without |
michael@0 | 6 | * modification, are permitted provided that the following conditions |
michael@0 | 7 | * are met: |
michael@0 | 8 | * 1. Redistributions of source code must retain the above copyright |
michael@0 | 9 | * notice, this list of conditions and the following disclaimer. |
michael@0 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
michael@0 | 11 | * notice, this list of conditions and the following disclaimer in the |
michael@0 | 12 | * documentation and/or other materials provided with the distribution. |
michael@0 | 13 | * |
michael@0 | 14 | * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
michael@0 | 15 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
michael@0 | 16 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
michael@0 | 17 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
michael@0 | 18 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
michael@0 | 19 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
michael@0 | 20 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
michael@0 | 21 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
michael@0 | 22 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
michael@0 | 23 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
michael@0 | 24 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
michael@0 | 25 | */ |
michael@0 | 26 | |
michael@0 | 27 | #ifndef WEBGLVALIDATESTRINGS_H_ |
michael@0 | 28 | #define WEBGLVALIDATESTRINGS_H_ |
michael@0 | 29 | |
michael@0 | 30 | #include "WebGLContext.h" |
michael@0 | 31 | |
michael@0 | 32 | namespace mozilla { |
michael@0 | 33 | |
michael@0 | 34 | // The following code was taken from the WebKit WebGL implementation, |
michael@0 | 35 | // which can be found here: |
michael@0 | 36 | // http://trac.webkit.org/browser/trunk/Source/WebCore/html/canvas/WebGLRenderingContext.cpp?rev=93625#L121 |
michael@0 | 37 | // Note that some modifications were done to adapt it to Mozilla. |
michael@0 | 38 | /****** BEGIN CODE TAKEN FROM WEBKIT ******/ |
michael@0 | 39 | bool WebGLContext::ValidateGLSLCharacter(char16_t c) |
michael@0 | 40 | { |
michael@0 | 41 | // Printing characters are valid except " $ ` @ \ ' DEL. |
michael@0 | 42 | if (c >= 32 && c <= 126 && |
michael@0 | 43 | c != '"' && c != '$' && c != '`' && c != '@' && c != '\\' && c != '\'') |
michael@0 | 44 | { |
michael@0 | 45 | return true; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | // Horizontal tab, line feed, vertical tab, form feed, carriage return are also valid. |
michael@0 | 49 | if (c >= 9 && c <= 13) { |
michael@0 | 50 | return true; |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | return false; |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | // Strips comments from shader text. This allows non-ASCII characters |
michael@0 | 57 | // to be used in comments without potentially breaking OpenGL |
michael@0 | 58 | // implementations not expecting characters outside the GLSL ES set. |
michael@0 | 59 | class StripComments { |
michael@0 | 60 | public: |
michael@0 | 61 | StripComments(const nsAString& str) |
michael@0 | 62 | : m_parseState(BeginningOfLine) |
michael@0 | 63 | , m_end(str.EndReading()) |
michael@0 | 64 | , m_current(str.BeginReading()) |
michael@0 | 65 | , m_position(0) |
michael@0 | 66 | { |
michael@0 | 67 | m_result.SetLength(str.Length()); |
michael@0 | 68 | parse(); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | const nsTArray<char16_t>& result() |
michael@0 | 72 | { |
michael@0 | 73 | return m_result; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | size_t length() |
michael@0 | 77 | { |
michael@0 | 78 | return m_position; |
michael@0 | 79 | } |
michael@0 | 80 | |
michael@0 | 81 | private: |
michael@0 | 82 | bool hasMoreCharacters() |
michael@0 | 83 | { |
michael@0 | 84 | return (m_current < m_end); |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | void parse() |
michael@0 | 88 | { |
michael@0 | 89 | while (hasMoreCharacters()) { |
michael@0 | 90 | process(current()); |
michael@0 | 91 | // process() might advance the position. |
michael@0 | 92 | if (hasMoreCharacters()) |
michael@0 | 93 | advance(); |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | void process(char16_t); |
michael@0 | 98 | |
michael@0 | 99 | bool peek(char16_t& character) |
michael@0 | 100 | { |
michael@0 | 101 | if (m_current + 1 >= m_end) |
michael@0 | 102 | return false; |
michael@0 | 103 | character = *(m_current + 1); |
michael@0 | 104 | return true; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | char16_t current() |
michael@0 | 108 | { |
michael@0 | 109 | //ASSERT(m_position < m_length); |
michael@0 | 110 | return *m_current; |
michael@0 | 111 | } |
michael@0 | 112 | |
michael@0 | 113 | void advance() |
michael@0 | 114 | { |
michael@0 | 115 | ++m_current; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | bool isNewline(char16_t character) |
michael@0 | 119 | { |
michael@0 | 120 | // Don't attempt to canonicalize newline related characters. |
michael@0 | 121 | return (character == '\n' || character == '\r'); |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | void emit(char16_t character) |
michael@0 | 125 | { |
michael@0 | 126 | m_result[m_position++] = character; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | enum ParseState { |
michael@0 | 130 | // Have not seen an ASCII non-whitespace character yet on |
michael@0 | 131 | // this line. Possible that we might see a preprocessor |
michael@0 | 132 | // directive. |
michael@0 | 133 | BeginningOfLine, |
michael@0 | 134 | |
michael@0 | 135 | // Have seen at least one ASCII non-whitespace character |
michael@0 | 136 | // on this line. |
michael@0 | 137 | MiddleOfLine, |
michael@0 | 138 | |
michael@0 | 139 | // Handling a preprocessor directive. Passes through all |
michael@0 | 140 | // characters up to the end of the line. Disables comment |
michael@0 | 141 | // processing. |
michael@0 | 142 | InPreprocessorDirective, |
michael@0 | 143 | |
michael@0 | 144 | // Handling a single-line comment. The comment text is |
michael@0 | 145 | // replaced with a single space. |
michael@0 | 146 | InSingleLineComment, |
michael@0 | 147 | |
michael@0 | 148 | // Handling a multi-line comment. Newlines are passed |
michael@0 | 149 | // through to preserve line numbers. |
michael@0 | 150 | InMultiLineComment |
michael@0 | 151 | }; |
michael@0 | 152 | |
michael@0 | 153 | ParseState m_parseState; |
michael@0 | 154 | const char16_t* m_end; |
michael@0 | 155 | const char16_t* m_current; |
michael@0 | 156 | size_t m_position; |
michael@0 | 157 | nsTArray<char16_t> m_result; |
michael@0 | 158 | }; |
michael@0 | 159 | |
michael@0 | 160 | void StripComments::process(char16_t c) |
michael@0 | 161 | { |
michael@0 | 162 | if (isNewline(c)) { |
michael@0 | 163 | // No matter what state we are in, pass through newlines |
michael@0 | 164 | // so we preserve line numbers. |
michael@0 | 165 | emit(c); |
michael@0 | 166 | |
michael@0 | 167 | if (m_parseState != InMultiLineComment) |
michael@0 | 168 | m_parseState = BeginningOfLine; |
michael@0 | 169 | |
michael@0 | 170 | return; |
michael@0 | 171 | } |
michael@0 | 172 | |
michael@0 | 173 | char16_t temp = 0; |
michael@0 | 174 | switch (m_parseState) { |
michael@0 | 175 | case BeginningOfLine: |
michael@0 | 176 | // If it's an ASCII space. |
michael@0 | 177 | if (c <= ' ' && (c == ' ' || (c <= 0xD && c >= 0x9))) { |
michael@0 | 178 | emit(c); |
michael@0 | 179 | break; |
michael@0 | 180 | } |
michael@0 | 181 | |
michael@0 | 182 | if (c == '#') { |
michael@0 | 183 | m_parseState = InPreprocessorDirective; |
michael@0 | 184 | emit(c); |
michael@0 | 185 | break; |
michael@0 | 186 | } |
michael@0 | 187 | |
michael@0 | 188 | // Transition to normal state and re-handle character. |
michael@0 | 189 | m_parseState = MiddleOfLine; |
michael@0 | 190 | process(c); |
michael@0 | 191 | break; |
michael@0 | 192 | |
michael@0 | 193 | case MiddleOfLine: |
michael@0 | 194 | if (c == '/' && peek(temp)) { |
michael@0 | 195 | if (temp == '/') { |
michael@0 | 196 | m_parseState = InSingleLineComment; |
michael@0 | 197 | emit(' '); |
michael@0 | 198 | advance(); |
michael@0 | 199 | break; |
michael@0 | 200 | } |
michael@0 | 201 | |
michael@0 | 202 | if (temp == '*') { |
michael@0 | 203 | m_parseState = InMultiLineComment; |
michael@0 | 204 | // Emit the comment start in case the user has |
michael@0 | 205 | // an unclosed comment and we want to later |
michael@0 | 206 | // signal an error. |
michael@0 | 207 | emit('/'); |
michael@0 | 208 | emit('*'); |
michael@0 | 209 | advance(); |
michael@0 | 210 | break; |
michael@0 | 211 | } |
michael@0 | 212 | } |
michael@0 | 213 | |
michael@0 | 214 | emit(c); |
michael@0 | 215 | break; |
michael@0 | 216 | |
michael@0 | 217 | case InPreprocessorDirective: |
michael@0 | 218 | // No matter what the character is, just pass it |
michael@0 | 219 | // through. Do not parse comments in this state. This |
michael@0 | 220 | // might not be the right thing to do long term, but it |
michael@0 | 221 | // should handle the #error preprocessor directive. |
michael@0 | 222 | emit(c); |
michael@0 | 223 | break; |
michael@0 | 224 | |
michael@0 | 225 | case InSingleLineComment: |
michael@0 | 226 | // The newline code at the top of this function takes care |
michael@0 | 227 | // of resetting our state when we get out of the |
michael@0 | 228 | // single-line comment. Swallow all other characters. |
michael@0 | 229 | break; |
michael@0 | 230 | |
michael@0 | 231 | case InMultiLineComment: |
michael@0 | 232 | if (c == '*' && peek(temp) && temp == '/') { |
michael@0 | 233 | emit('*'); |
michael@0 | 234 | emit('/'); |
michael@0 | 235 | m_parseState = MiddleOfLine; |
michael@0 | 236 | advance(); |
michael@0 | 237 | break; |
michael@0 | 238 | } |
michael@0 | 239 | |
michael@0 | 240 | // Swallow all other characters. Unclear whether we may |
michael@0 | 241 | // want or need to just emit a space per character to try |
michael@0 | 242 | // to preserve column numbers for debugging purposes. |
michael@0 | 243 | break; |
michael@0 | 244 | } |
michael@0 | 245 | } |
michael@0 | 246 | |
michael@0 | 247 | /****** END CODE TAKEN FROM WEBKIT ******/ |
michael@0 | 248 | |
michael@0 | 249 | } // end namespace mozilla |
michael@0 | 250 | |
michael@0 | 251 | #endif // WEBGLVALIDATESTRINGS_H_ |