Brian Silverman | 70325d6 | 2015-09-20 17:00:43 -0400 | [diff] [blame] | 1 | /* Copyright (c) 2007, Google Inc. |
| 2 | * All rights reserved. |
| 3 | * |
| 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 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | * --- |
| 30 | * |
| 31 | * Author: falmeida@google.com (Filipe Almeida) |
| 32 | */ |
| 33 | |
| 34 | #ifndef SECURITY_STREAMHTMLPARSER_JSPARSER_H |
| 35 | #define SECURITY_STREAMHTMLPARSER_JSPARSER_H |
| 36 | |
| 37 | #include <config.h> |
| 38 | #include "htmlparser/statemachine.h" |
| 39 | |
| 40 | #ifdef __cplusplus |
| 41 | namespace ctemplate_htmlparser { |
| 42 | #endif /* __cplusplus */ |
| 43 | |
| 44 | /* Size of the ring buffer used to lookup the last token in the javascript |
| 45 | * stream. The size is pretty much arbitrary at this point but must be bigger |
| 46 | * than the biggest token we want to lookup plus 3: Two delimiters plus an empty |
| 47 | * ring buffer slot. */ |
| 48 | #define JSPARSER_RING_BUFFER_SIZE 18 |
| 49 | |
| 50 | enum js_state_external_enum { |
| 51 | JSPARSER_STATE_TEXT, |
| 52 | JSPARSER_STATE_Q, |
| 53 | JSPARSER_STATE_DQ, |
| 54 | JSPARSER_STATE_REGEXP, |
| 55 | JSPARSER_STATE_COMMENT |
| 56 | }; |
| 57 | |
| 58 | /* Stores the context of the javascript parser. |
| 59 | * |
| 60 | * If this structure is changed, jsparser_new(), jsparser_copy() and |
| 61 | * jsparser_reset() should be updated accordingly. |
| 62 | */ |
| 63 | typedef struct jsparser_ctx_s { |
| 64 | |
| 65 | /* Reference to the statemachine context. */ |
| 66 | statemachine_ctx *statemachine; |
| 67 | |
| 68 | /* Reference to the statemachine definition. |
| 69 | * |
| 70 | * It should be readonly and contain the same values across jsparser |
| 71 | * instances. |
| 72 | */ |
| 73 | /* TODO(falmeida): Change statemachine_def to const. */ |
| 74 | statemachine_definition *statemachine_def; |
| 75 | |
| 76 | /* Index to the start of the buffer. */ |
| 77 | int buffer_start; |
| 78 | |
| 79 | /* Index the current writing position (end of the buffer plus one). */ |
| 80 | int buffer_end; |
| 81 | |
| 82 | /* Ring buffer used to lookup the last token. */ |
| 83 | char buffer[JSPARSER_RING_BUFFER_SIZE]; |
| 84 | |
| 85 | } jsparser_ctx; |
| 86 | |
| 87 | |
| 88 | void jsparser_reset(jsparser_ctx *ctx); |
| 89 | jsparser_ctx *jsparser_new(void); |
| 90 | |
| 91 | /* Returns a pointer to a context which is a duplicate of the jsparser src. |
| 92 | */ |
| 93 | jsparser_ctx *jsparser_duplicate(jsparser_ctx *src); |
| 94 | |
| 95 | /* Copies the context of the jsparser pointed to by src to the jsparser dst. |
| 96 | */ |
| 97 | void jsparser_copy(jsparser_ctx *dst, jsparser_ctx *src); |
| 98 | int jsparser_state(jsparser_ctx *ctx); |
| 99 | int jsparser_parse(jsparser_ctx *ctx, const char *str, int size); |
| 100 | |
| 101 | void jsparser_delete(jsparser_ctx *ctx); |
| 102 | |
| 103 | /** |
| 104 | * Ring buffer functions. |
| 105 | * |
| 106 | * These functions are only exported for testing and should not be called from |
| 107 | * outside of jsparser.c in production code. |
| 108 | */ |
| 109 | |
| 110 | /* Appends a character to the ring buffer. |
| 111 | * |
| 112 | * Sequences of whitespaces and newlines are folded into one character. |
| 113 | */ |
| 114 | void jsparser_buffer_append_chr(jsparser_ctx *js, char chr); |
| 115 | |
| 116 | /* Appends a string to the ring buffer. |
| 117 | * |
| 118 | * Sequences of whitespaces and newlines are folded into one character. |
| 119 | */ |
| 120 | void jsparser_buffer_append_str(jsparser_ctx *js, const char *str); |
| 121 | |
| 122 | /* Returns the last appended character and removes it from the buffer. If the |
| 123 | * buffer is empty, then it returns ASCII 0 ('\0'). |
| 124 | */ |
| 125 | char jsparser_buffer_pop(jsparser_ctx *js); |
| 126 | |
| 127 | /* Returns the value of the character at a certain index in the buffer or an |
| 128 | * ASCII 0 ('\0') character if the index is extends beyond the size of the |
| 129 | * buffer, either because we don't have as many characters in the buffer, or |
| 130 | * because the index points to a place bigger than the size of the buffer.. |
| 131 | * |
| 132 | * Index positions must be negative, where -1 is the last character appended to |
| 133 | * the buffer. |
| 134 | */ |
| 135 | char jsparser_buffer_get(jsparser_ctx *js, int pos); |
| 136 | |
| 137 | /* Sets the value of the character at a certain index in the buffer. Returns |
| 138 | * true if the write was successful or false if there was an attempt to write |
| 139 | * outside of the buffer boundaries. |
| 140 | * |
| 141 | * Index positions are negative, were -1 is the last character appended to the |
| 142 | * buffer. Using positive integers for the index will result in undefined |
| 143 | * behaviour. |
| 144 | */ |
| 145 | int jsparser_buffer_set(jsparser_ctx *js, int pos, char value); |
| 146 | |
| 147 | /* Copies a slice of the buffer to the string pointed to by output. start and |
| 148 | * end are the indexes of the sliced region. If the start argument extends |
| 149 | * beyond the beginning of the buffer, the slice will only contain characters |
| 150 | * starting from beginning of the buffer. |
| 151 | */ |
| 152 | void jsparser_buffer_slice(jsparser_ctx *js, char *buffer, int start, int end); |
| 153 | |
| 154 | /* Copy the last javascript identifier or keyword found in the buffer to the |
| 155 | * string pointed by identifier. |
| 156 | */ |
| 157 | int jsparser_buffer_last_identifier(jsparser_ctx *js, char *identifier); |
| 158 | |
| 159 | |
| 160 | #define jsparser_parse_chr(a,b) jsparser_parse(a, &(b), 1); |
| 161 | #ifdef __cplusplus |
| 162 | #define jsparser_parse_str(a,b) jsparser_parse(a, b, \ |
| 163 | static_cast<int>(strlen(b))); |
| 164 | #else |
| 165 | #define jsparser_parse_str(a,b) jsparser_parse(a, b, (int)strlen(b)); |
| 166 | #endif |
| 167 | |
| 168 | #ifdef __cplusplus |
| 169 | } /* namespace security_streamhtmlparser */ |
| 170 | #endif /* __cplusplus */ |
| 171 | |
| 172 | #endif /* SECURITY_STREAMHTMLPARSER_JSPARSER_H */ |