« third_party/modest/source/myhtml/mynamespace.c
↑ Summary
third_party/modest/source/myhtml/parser.c »
| 1 | /* | ||||||||||||||
| 2 | Copyright (C) 2015-2017 Alexander Borisov | ||||||||||||||
| 3 | |||||||||||||||
| 4 | This library is free software; you can redistribute it and/or | ||||||||||||||
| 5 | modify it under the terms of the GNU Lesser General Public | ||||||||||||||
| 6 | License as published by the Free Software Foundation; either | ||||||||||||||
| 7 | version 2.1 of the License, or (at your option) any later version. | ||||||||||||||
| 8 | |||||||||||||||
| 9 | This library is distributed in the hope that it will be useful, | ||||||||||||||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||||||||||||
| 12 | Lesser General Public License for more details. | ||||||||||||||
| 13 | |||||||||||||||
| 14 | You should have received a copy of the GNU Lesser General Public | ||||||||||||||
| 15 | License along with this library; if not, write to the Free Software | ||||||||||||||
| 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||||||||||||||
| 17 | |||||||||||||||
| 18 | Author: lex.borisov@gmail.com (Alexander Borisov) | ||||||||||||||
| 19 | */ | ||||||||||||||
| 20 | |||||||||||||||
| 21 | #include "myhtml/mystring.h" | ||||||||||||||
| 22 | #include "mycore/utils/resources.h" | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 0 | size_t myhtml_string_append_with_preprocessing(mycore_string_t* str, const char* buff, size_t length, bool emit_null_chars) | |||||||||||||
| 25 | { | ||||||||||||||
| 26 | 0 | ▶ | MyCORE_STRING_REALLOC_IF_NEED(str, (length + 1), 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 27 | |||||||||||||||
| 28 | 0 | unsigned char *data = (unsigned char*)str->data; | |||||||||||||
| 29 | 0 | const unsigned char *u_buff = (const unsigned char*)buff; | |||||||||||||
| 30 | |||||||||||||||
| 31 | /* 0x0D == \r */ | ||||||||||||||
| 32 | /* 0x0A == \n */ | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 0 | ▶ | for (size_t i = 0; i < length; i++) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 35 | { | ||||||||||||||
| 36 | 0 | ▶ | if(u_buff[i] == 0x0D) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | 0 | data[str->length] = 0x0A; | |||||||||||||
| 38 | |||||||||||||||
| 39 | 0 | ▶ | if((i + 1) < length) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 40 | 0 | ▶ | if(u_buff[(i + 1)] == 0x0A) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 41 | 0 | i++; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | else { | ||||||||||||||
| 44 | 0 | str->length++; | |||||||||||||
| 45 | |||||||||||||||
| 46 | 0 | ▶ | MyCORE_STRING_REALLOC_IF_NEED(str, 0, 2); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 0 | str->data[str->length] = '\0'; | |||||||||||||
| 48 | |||||||||||||||
| 49 | 0 | return str->length; | |||||||||||||
| 50 | } | ||||||||||||||
| 51 | } | ||||||||||||||
| 52 | 0 | ▶ | else if(u_buff[i] == 0x00 && emit_null_chars == false) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 53 | { | ||||||||||||||
| 54 | 0 | mycore_string_realloc(str, (str->size + 5)); | |||||||||||||
| 55 | 0 | data = (unsigned char*)str->data; | |||||||||||||
| 56 | |||||||||||||||
| 57 | // Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD) | ||||||||||||||
| 58 | 0 | data[str->length] = 0xEF; str->length++; | |||||||||||||
| 59 | 0 | data[str->length] = 0xBF; str->length++; | |||||||||||||
| 60 | 0 | data[str->length] = 0xBD; | |||||||||||||
| 61 | } | ||||||||||||||
| 62 | else | ||||||||||||||
| 63 | 0 | data[str->length] = u_buff[i]; | |||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | str->length++; | |||||||||||||
| 66 | } | ||||||||||||||
| 67 | |||||||||||||||
| 68 | 0 | str->data[str->length] = '\0'; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 0 | return 0; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | size_t myhtml_string_append_lowercase_with_preprocessing(mycore_string_t* str, const char* buff, size_t length, bool emit_null_chars) | |||||||||||||
| 74 | { | ||||||||||||||
| 75 | 0 | ▶ | MyCORE_STRING_REALLOC_IF_NEED(str, (length + 1), 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | |||||||||||||||
| 77 | 0 | unsigned char *data = (unsigned char*)str->data; | |||||||||||||
| 78 | 0 | const unsigned char *u_buff = (const unsigned char*)buff; | |||||||||||||
| 79 | |||||||||||||||
| 80 | 0 | ▶ | for (size_t i = 0; i < length; i++) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 81 | { | ||||||||||||||
| 82 | 0 | ▶ | if(u_buff[i] == 0x0D) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | 0 | data[str->length] = 0x0A; | |||||||||||||
| 84 | |||||||||||||||
| 85 | 0 | ▶ | if((i + 1) < length) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | 0 | ▶ | if(u_buff[(i + 1)] == 0x0A) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 0 | i++; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | else { | ||||||||||||||
| 90 | 0 | str->length++; | |||||||||||||
| 91 | |||||||||||||||
| 92 | 0 | ▶ | MyCORE_STRING_REALLOC_IF_NEED(str, 0, 2); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 0 | str->data[str->length] = '\0'; | |||||||||||||
| 94 | |||||||||||||||
| 95 | 0 | return str->length; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | } | ||||||||||||||
| 98 | 0 | ▶ | else if(u_buff[i] == 0x00 && emit_null_chars == false) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 99 | { | ||||||||||||||
| 100 | 0 | mycore_string_realloc(str, (str->size + 5)); | |||||||||||||
| 101 | 0 | data = (unsigned char*)str->data; | |||||||||||||
| 102 | |||||||||||||||
| 103 | // Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD) | ||||||||||||||
| 104 | 0 | data[str->length] = 0xEF; str->length++; | |||||||||||||
| 105 | 0 | data[str->length] = 0xBF; str->length++; | |||||||||||||
| 106 | 0 | data[str->length] = 0xBD; | |||||||||||||
| 107 | } | ||||||||||||||
| 108 | else { | ||||||||||||||
| 109 | 0 | data[str->length] = mycore_string_chars_lowercase_map[ u_buff[i] ]; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 0 | str->length++; | |||||||||||||
| 113 | } | ||||||||||||||
| 114 | |||||||||||||||
| 115 | 0 | str->data[str->length] = '\0'; | |||||||||||||
| 116 | |||||||||||||||
| 117 | 0 | return 0; | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | |||||||||||||||
| 120 | 0 | size_t myhtml_string_before_append_any_preprocessing(mycore_string_t* str, const char* buff, size_t length, size_t last_position) | |||||||||||||
| 121 | { | ||||||||||||||
| 122 | 0 | ▶ | if(last_position == 0 || length == 0) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 123 | 0 | return 0; | |||||||||||||
| 124 | |||||||||||||||
| 125 | 0 | ▶ | if(*buff == '\n') | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | 0 | return 1; | |||||||||||||
| 127 | |||||||||||||||
| 128 | 0 | return 0; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | ///////////////////////////////////////////////////////// | ||||||||||||||
| 132 | //// Append With Convert Encoding and Preprocessing API | ||||||||||||||
| 133 | //// | ||||||||||||||
| 134 | ///////////////////////////////////////////////////////// | ||||||||||||||
| 135 | 0 | size_t myhtml_string_append_with_convert_encoding_with_preprocessing(mycore_string_t* str, const char* buff, size_t length, myencoding_t encoding, bool emit_null_chars) | |||||||||||||
| 136 | { | ||||||||||||||
| 137 | myencoding_result_t res; | ||||||||||||||
| 138 | 0 | myencoding_result_clean(&res); | |||||||||||||
| 139 | |||||||||||||||
| 140 | 0 | return myhtml_string_append_chunk_with_convert_encoding_with_preprocessing(str, &res, buff, length, encoding, emit_null_chars); | |||||||||||||
| 141 | } | ||||||||||||||
| 142 | |||||||||||||||
| 143 | 0 | size_t myhtml_string_append_chunk_with_convert_encoding_with_preprocessing(mycore_string_t* str, myencoding_result_t* res, const char* buff, size_t length, myencoding_t encoding, bool emit_null_chars) | |||||||||||||
| 144 | { | ||||||||||||||
| 145 | 0 | ▶ | MyCORE_STRING_REALLOC_IF_NEED(str, (length + 1), 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 146 | |||||||||||||||
| 147 | 0 | unsigned const char* u_buff = (unsigned const char*)buff; | |||||||||||||
| 148 | 0 | const myencoding_custom_f func = myencoding_get_function_by_id(encoding); | |||||||||||||
| 149 | |||||||||||||||
| 150 | 0 | ▶ | for (size_t i = 0; i < length; i++) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 151 | { | ||||||||||||||
| 152 | 0 | ▶ | if(func(u_buff[i], res) == MyENCODING_STATUS_OK) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | 0 | ▶ | MyCORE_STRING_REALLOC_IF_NEED(str, 5, 1); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 154 | |||||||||||||||
| 155 | 0 | size_t len = myencoding_codepoint_to_ascii_utf_8(res->result, &str->data[str->length]); | |||||||||||||
| 156 | |||||||||||||||
| 157 | 0 | ▶ | if(len == 1) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 158 | 0 | ▶ | if(str->data[str->length] == '\r') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | 0 | str->data[str->length] = '\n'; | |||||||||||||
| 160 | |||||||||||||||
| 161 | 0 | ▶ | if((i + 1) < length) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 162 | 0 | ▶ | if(buff[(i + 1)] == '\n') | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 163 | 0 | i++; | |||||||||||||
| 164 | } | ||||||||||||||
| 165 | else { | ||||||||||||||
| 166 | 0 | str->length++; | |||||||||||||
| 167 | |||||||||||||||
| 168 | 0 | ▶ | MyCORE_STRING_REALLOC_IF_NEED(str, 0, 2); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 169 | 0 | str->data[str->length] = '\0'; | |||||||||||||
| 170 | |||||||||||||||
| 171 | 0 | return str->length; | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | } | ||||||||||||||
| 174 | 0 | ▶ | else if(str->data[str->length] == 0x00 && emit_null_chars == false) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 175 | { | ||||||||||||||
| 176 | 0 | mycore_string_realloc(str, (str->size + 5)); | |||||||||||||
| 177 | |||||||||||||||
| 178 | // Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD) | ||||||||||||||
| 179 | 0 | str->data[str->length] = (char)0xEF; str->length++; | |||||||||||||
| 180 | 0 | str->data[str->length] = (char)0xBF; str->length++; | |||||||||||||
| 181 | 0 | str->data[str->length] = (char)0xBD; | |||||||||||||
| 182 | } | ||||||||||||||
| 183 | } | ||||||||||||||
| 184 | |||||||||||||||
| 185 | 0 | str->length += len; | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | } | ||||||||||||||
| 188 | |||||||||||||||
| 189 | 0 | ▶ | MyCORE_STRING_APPEND_BYTE_WITHOUT_INCREMENT('\0', str, 1); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 190 | |||||||||||||||
| 191 | 0 | return 0; | |||||||||||||
| 192 | } | ||||||||||||||
| 193 | |||||||||||||||
| 194 | 0 | size_t myhtml_string_append_lowercase_with_convert_encoding_with_preprocessing(mycore_string_t* str, const char* buff, size_t length, myencoding_t encoding, bool emit_null_chars) | |||||||||||||
| 195 | { | ||||||||||||||
| 196 | myencoding_result_t res; | ||||||||||||||
| 197 | 0 | myencoding_result_clean(&res); | |||||||||||||
| 198 | |||||||||||||||
| 199 | 0 | return myhtml_string_append_lowercase_chunk_with_convert_encoding_with_preprocessing(str, &res, buff, length, encoding, emit_null_chars); | |||||||||||||
| 200 | } | ||||||||||||||
| 201 | |||||||||||||||
| 202 | 0 | size_t myhtml_string_append_lowercase_chunk_with_convert_encoding_with_preprocessing(mycore_string_t* str, myencoding_result_t* res, const char* buff, size_t length, myencoding_t encoding, bool emit_null_chars) | |||||||||||||
| 203 | { | ||||||||||||||
| 204 | 0 | ▶ | MyCORE_STRING_REALLOC_IF_NEED(str, (length + 1), 0); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 205 | |||||||||||||||
| 206 | 0 | unsigned const char* u_buff = (unsigned const char*)buff; | |||||||||||||
| 207 | 0 | const myencoding_custom_f func = myencoding_get_function_by_id(encoding); | |||||||||||||
| 208 | |||||||||||||||
| 209 | 0 | ▶ | for (size_t i = 0; i < length; i++) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 210 | { | ||||||||||||||
| 211 | 0 | ▶ | if(func(u_buff[i], res) == MyENCODING_STATUS_OK) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 212 | 0 | ▶ | MyCORE_STRING_REALLOC_IF_NEED(str, 5, 1); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 213 | |||||||||||||||
| 214 | 0 | size_t len = myencoding_codepoint_to_lowercase_ascii_utf_8(res->result, &str->data[str->length]); | |||||||||||||
| 215 | |||||||||||||||
| 216 | 0 | ▶ | if(len == 1) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 217 | 0 | ▶ | if(str->data[str->length] == '\r') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 218 | 0 | str->data[str->length] = '\n'; | |||||||||||||
| 219 | |||||||||||||||
| 220 | 0 | ▶ | if((i + 1) < length) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 221 | 0 | ▶ | if(buff[(i + 1)] == '\n') | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 222 | 0 | i++; | |||||||||||||
| 223 | } | ||||||||||||||
| 224 | else { | ||||||||||||||
| 225 | 0 | str->length++; | |||||||||||||
| 226 | |||||||||||||||
| 227 | 0 | ▶ | MyCORE_STRING_REALLOC_IF_NEED(str, 0, 2); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 228 | 0 | str->data[str->length] = '\0'; | |||||||||||||
| 229 | |||||||||||||||
| 230 | 0 | return str->length; | |||||||||||||
| 231 | } | ||||||||||||||
| 232 | } | ||||||||||||||
| 233 | 0 | ▶ | else if(str->data[str->length] == 0x00 && emit_null_chars == false) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 234 | { | ||||||||||||||
| 235 | 0 | mycore_string_realloc(str, (str->size + 5)); | |||||||||||||
| 236 | |||||||||||||||
| 237 | // Unicode Character 'REPLACEMENT CHARACTER' (U+FFFD) | ||||||||||||||
| 238 | 0 | str->data[str->length] = (char)0xEF; str->length++; | |||||||||||||
| 239 | 0 | str->data[str->length] = (char)0xBF; str->length++; | |||||||||||||
| 240 | 0 | str->data[str->length] = (char)0xBD; | |||||||||||||
| 241 | } | ||||||||||||||
| 242 | } | ||||||||||||||
| 243 | |||||||||||||||
| 244 | 0 | str->length += len; | |||||||||||||
| 245 | } | ||||||||||||||
| 246 | } | ||||||||||||||
| 247 | |||||||||||||||
| 248 | 0 | ▶ | MyCORE_STRING_APPEND_BYTE_WITHOUT_INCREMENT('\0', str, 1); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 249 | |||||||||||||||
| 250 | 0 | return 0; | |||||||||||||
| 251 | } | ||||||||||||||