« third_party/modest/source/mycss/values/values.c
↑ Summary
third_party/modest/source/myencoding/encoding.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 "myencoding/encoding.h" | ||||||||||||||||||
| 22 | #include "myencoding/detect_resource.h" | ||||||||||||||||||
| 23 | #include "mycore/utils/resources.h" | ||||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | 0 | myencoding_trigram_result_t myencoding_detect_by_trigram(unsigned const char *u_text, size_t length, | |||||||||||||||||
| 26 | const myencoding_trigram_t *list, size_t list_length, | ||||||||||||||||||
| 27 | size_t max_sum_for_break) | ||||||||||||||||||
| 28 | { | ||||||||||||||||||
| 29 | 0 | myencoding_trigram_result_t res = {0, 0}; | |||||||||||||||||
| 30 | |||||||||||||||||||
| 31 | 0 | ▶ | for (size_t i = 0; i < (length - 3); i++) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 32 | 0 | ▶ | if(u_text[i] > 127) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 33 | { | ||||||||||||||||||
| 34 | 0 | ▶ | for (size_t j = 0; j < list_length; j++) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 35 | { | ||||||||||||||||||
| 36 | 0 | ▶ | if(memcmp(list[j].trigram, &u_text[i], 3) == 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 37 | 0 | res.value += list[j].value; | |||||||||||||||||
| 38 | 0 | res.count++; | |||||||||||||||||
| 39 | |||||||||||||||||||
| 40 | 0 | ▶ | if(res.value >= max_sum_for_break) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 41 | 0 | i = length; | |||||||||||||||||
| 42 | |||||||||||||||||||
| 43 | 0 | break; | |||||||||||||||||
| 44 | } | ||||||||||||||||||
| 45 | } | ||||||||||||||||||
| 46 | } | ||||||||||||||||||
| 47 | } | ||||||||||||||||||
| 48 | |||||||||||||||||||
| 49 | 0 | return res; | |||||||||||||||||
| 50 | } | ||||||||||||||||||
| 51 | |||||||||||||||||||
| 52 | 0 | bool myencoding_detect_russian_has_end(myencoding_trigram_result_t *res, size_t min_count, size_t min_value) | |||||||||||||||||
| 53 | { | ||||||||||||||||||
| 54 | 0 | ▶ | if(res->value >= min_value || res->count >= min_count) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 55 | 0 | return true; | |||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 0 | return false; | |||||||||||||||||
| 58 | } | ||||||||||||||||||
| 59 | |||||||||||||||||||
| 60 | 0 | bool myencoding_detect_unicode_has_end(myencoding_unicode_result_t *res, size_t max_bad_percent) | |||||||||||||||||
| 61 | { | ||||||||||||||||||
| 62 | 0 | ▶ | if(res->count_good == 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 63 | 0 | ▶ | if(res->count_bad) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 64 | 0 | return false; | |||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | 0 | return true; | |||||||||||||||||
| 67 | } | ||||||||||||||||||
| 68 | 0 | ▶ | else if(res->count_bad == 0) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 69 | 0 | return true; | |||||||||||||||||
| 70 | |||||||||||||||||||
| 71 | 0 | size_t percent_bad = (res->count_bad * 100) / res->count_good; | |||||||||||||||||
| 72 | 0 | ▶ | if(percent_bad < max_bad_percent) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 73 | 0 | return true; | |||||||||||||||||
| 74 | |||||||||||||||||||
| 75 | 0 | return false; | |||||||||||||||||
| 76 | } | ||||||||||||||||||
| 77 | |||||||||||||||||||
| 78 | 0 | myencoding_unicode_result_t myencoding_detect_utf_8(unsigned const char *u_text, size_t length) | |||||||||||||||||
| 79 | { | ||||||||||||||||||
| 80 | 0 | size_t i = 0; | |||||||||||||||||
| 81 | 0 | myencoding_unicode_result_t res = {0, 0, 0}; | |||||||||||||||||
| 82 | |||||||||||||||||||
| 83 | 0 | ▶ | while(i < length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 84 | { | ||||||||||||||||||
| 85 | 0 | ▶ | if((u_text[i] & 0x80) == 0x00) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 86 | 0 | i++; | |||||||||||||||||
| 87 | 0 | res.count_ascii++; | |||||||||||||||||
| 88 | } | ||||||||||||||||||
| 89 | 0 | ▶ | else if((u_text[i] & 0xE0) == 0xC0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 90 | 0 | i += 2; | |||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | 0 | ▶ | if(i >= length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 93 | 0 | break; | |||||||||||||||||
| 94 | |||||||||||||||||||
| 95 | 0 | ▶ | if(((u_text[i - 1] & 0x80) && (u_text[i - 1] & 0x40) == 0)) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 96 | 0 | res.count_good++; | |||||||||||||||||
| 97 | else | ||||||||||||||||||
| 98 | 0 | res.count_bad++; | |||||||||||||||||
| 99 | } | ||||||||||||||||||
| 100 | 0 | ▶ | else if((u_text[i] & 0xF0) == 0xE0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 101 | 0 | i += 3; | |||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | 0 | ▶ | if(i >= length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 104 | 0 | break; | |||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | 0 | ▶ | if(((u_text[i - 1] & 0x80) && (u_text[i - 1] & 0x40) == 0) && | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 107 | 0 | ▶ | ((u_text[i - 2] & 0x80) && (u_text[i - 2] & 0x40) == 0)) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 108 | 0 | res.count_good++; | |||||||||||||||||
| 109 | } | ||||||||||||||||||
| 110 | else | ||||||||||||||||||
| 111 | 0 | res.count_bad++; | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | 0 | ▶ | else if((u_text[i] & 0xF8) == 0xF0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 114 | 0 | i += 4; | |||||||||||||||||
| 115 | |||||||||||||||||||
| 116 | 0 | ▶ | if(i >= length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 117 | 0 | break; | |||||||||||||||||
| 118 | |||||||||||||||||||
| 119 | 0 | ▶ | if(((u_text[i - 1] & 0x80) && (u_text[i - 1] & 0x40) == 0) && | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 120 | 0 | ▶ | ((u_text[i - 2] & 0x80) && (u_text[i - 2] & 0x40) == 0) && | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 121 | 0 | ▶ | ((u_text[i - 3] & 0x80) && (u_text[i - 3] & 0x40) == 0)) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 122 | 0 | res.count_good++; | |||||||||||||||||
| 123 | } | ||||||||||||||||||
| 124 | else | ||||||||||||||||||
| 125 | 0 | res.count_bad++; | |||||||||||||||||
| 126 | } | ||||||||||||||||||
| 127 | else { | ||||||||||||||||||
| 128 | 0 | i++; | |||||||||||||||||
| 129 | 0 | res.count_bad++; | |||||||||||||||||
| 130 | } | ||||||||||||||||||
| 131 | } | ||||||||||||||||||
| 132 | |||||||||||||||||||
| 133 | 0 | return res; | |||||||||||||||||
| 134 | } | ||||||||||||||||||
| 135 | |||||||||||||||||||
| 136 | 0 | myencoding_unicode_result_t myencoding_detect_utf_16(unsigned const char *u_text, size_t length) | |||||||||||||||||
| 137 | { | ||||||||||||||||||
| 138 | 0 | size_t i = 0; | |||||||||||||||||
| 139 | 0 | myencoding_unicode_result_t res = {0, 0, 0}; | |||||||||||||||||
| 140 | |||||||||||||||||||
| 141 | 0 | ▶ | while(i < length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 142 | { | ||||||||||||||||||
| 143 | 0 | ▶ | if(u_text[i] == 0x00) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 144 | 0 | ▶ | if((i % 2) == 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 145 | 0 | i++; | |||||||||||||||||
| 146 | |||||||||||||||||||
| 147 | 0 | ▶ | if(u_text[i] > 0x1F && u_text[i] < 0x7F) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 148 | 0 | res.count_bad++; | |||||||||||||||||
| 149 | } | ||||||||||||||||||
| 150 | else { | ||||||||||||||||||
| 151 | 0 | ▶ | if(u_text[(i - 1)] > 0x1F && u_text[(i - 1)] < 0x7F) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 152 | 0 | res.count_good++; | |||||||||||||||||
| 153 | |||||||||||||||||||
| 154 | 0 | i++; | |||||||||||||||||
| 155 | } | ||||||||||||||||||
| 156 | } | ||||||||||||||||||
| 157 | else | ||||||||||||||||||
| 158 | 0 | i++; | |||||||||||||||||
| 159 | } | ||||||||||||||||||
| 160 | |||||||||||||||||||
| 161 | 0 | return res; | |||||||||||||||||
| 162 | } | ||||||||||||||||||
| 163 | |||||||||||||||||||
| 164 | 0 | bool myencoding_detect_bom(const char *text, size_t length, myencoding_t *encoding) | |||||||||||||||||
| 165 | { | ||||||||||||||||||
| 166 | 0 | unsigned const char *u_text = (unsigned const char*)text; | |||||||||||||||||
| 167 | |||||||||||||||||||
| 168 | 0 | ▶ | if(length > 2) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 169 | 0 | ▶ | if(u_text[0] == 0xEF && | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 170 | 0 | ▶ | u_text[1] == 0xBB && | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 171 | 0 | ▶ | u_text[2] == 0xBF) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 172 | { | ||||||||||||||||||
| 173 | 0 | *encoding = MyENCODING_UTF_8; | |||||||||||||||||
| 174 | 0 | return true; | |||||||||||||||||
| 175 | } | ||||||||||||||||||
| 176 | } | ||||||||||||||||||
| 177 | |||||||||||||||||||
| 178 | 0 | ▶ | if(length > 1) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 179 | 0 | ▶ | if(u_text[0] == 0xFE && u_text[1] == 0xFF) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 180 | 0 | *encoding = MyENCODING_UTF_16BE; | |||||||||||||||||
| 181 | 0 | return true; | |||||||||||||||||
| 182 | } | ||||||||||||||||||
| 183 | |||||||||||||||||||
| 184 | 0 | ▶ | if(u_text[0] == 0xFF && u_text[1] == 0xFE) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 185 | 0 | *encoding = MyENCODING_UTF_16LE; | |||||||||||||||||
| 186 | 0 | return true; | |||||||||||||||||
| 187 | } | ||||||||||||||||||
| 188 | } | ||||||||||||||||||
| 189 | |||||||||||||||||||
| 190 | // //for UTF-32 | ||||||||||||||||||
| 191 | // if(length > 3) { | ||||||||||||||||||
| 192 | // if(u_text[0] == 0x00 && | ||||||||||||||||||
| 193 | // u_text[1] == 0x00 && | ||||||||||||||||||
| 194 | // u_text[2] == 0xFE && | ||||||||||||||||||
| 195 | // u_text[3] == 0xFF) | ||||||||||||||||||
| 196 | // { | ||||||||||||||||||
| 197 | // *encoding = MyENCODING_UTF_32BE; | ||||||||||||||||||
| 198 | // return true; | ||||||||||||||||||
| 199 | // } | ||||||||||||||||||
| 200 | // | ||||||||||||||||||
| 201 | // if(u_text[0] == 0xFF && | ||||||||||||||||||
| 202 | // u_text[1] == 0xFE && | ||||||||||||||||||
| 203 | // u_text[2] == 0x00 && | ||||||||||||||||||
| 204 | // u_text[3] == 0x00) | ||||||||||||||||||
| 205 | // { | ||||||||||||||||||
| 206 | // *encoding = MyENCODING_UTF_32LE; | ||||||||||||||||||
| 207 | // return true; | ||||||||||||||||||
| 208 | // } | ||||||||||||||||||
| 209 | // } | ||||||||||||||||||
| 210 | |||||||||||||||||||
| 211 | 0 | return false; | |||||||||||||||||
| 212 | } | ||||||||||||||||||
| 213 | |||||||||||||||||||
| 214 | 0 | bool myencoding_detect_and_cut_bom(const char *text, size_t length, myencoding_t *encoding, const char **new_text, size_t *new_size) | |||||||||||||||||
| 215 | { | ||||||||||||||||||
| 216 | 0 | ▶ | if(myencoding_detect_bom(text, length, encoding)) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 217 | { | ||||||||||||||||||
| 218 | 0 | ▶ | if(*encoding == MyENCODING_UTF_8) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 219 | 0 | *new_text = &text[3]; | |||||||||||||||||
| 220 | 0 | *new_size = length - 3; | |||||||||||||||||
| 221 | } | ||||||||||||||||||
| 222 | else { | ||||||||||||||||||
| 223 | 0 | *new_text = &text[2]; | |||||||||||||||||
| 224 | 0 | *new_size = length - 2; | |||||||||||||||||
| 225 | } | ||||||||||||||||||
| 226 | |||||||||||||||||||
| 227 | 0 | return true; | |||||||||||||||||
| 228 | } | ||||||||||||||||||
| 229 | |||||||||||||||||||
| 230 | 0 | return false; | |||||||||||||||||
| 231 | } | ||||||||||||||||||
| 232 | |||||||||||||||||||
| 233 | 0 | bool myencoding_detect_unicode(const char *text, size_t length, myencoding_t *encoding) | |||||||||||||||||
| 234 | { | ||||||||||||||||||
| 235 | 0 | unsigned const char *u_text = (unsigned const char*)text; | |||||||||||||||||
| 236 | 0 | *encoding = MyENCODING_DEFAULT; | |||||||||||||||||
| 237 | |||||||||||||||||||
| 238 | 0 | myencoding_unicode_result_t res = myencoding_detect_utf_16(u_text, length); | |||||||||||||||||
| 239 | |||||||||||||||||||
| 240 | 0 | ▶ | if(res.count_bad == 0 && res.count_good >= 3) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 241 | 0 | *encoding = MyENCODING_UTF_16LE; | |||||||||||||||||
| 242 | 0 | return true; | |||||||||||||||||
| 243 | } | ||||||||||||||||||
| 244 | 0 | ▶ | else if(res.count_bad >= 3 && res.count_good == 0) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 245 | 0 | *encoding = MyENCODING_UTF_16BE; | |||||||||||||||||
| 246 | 0 | return true; | |||||||||||||||||
| 247 | } | ||||||||||||||||||
| 248 | |||||||||||||||||||
| 249 | 0 | res = myencoding_detect_utf_8(u_text, length); | |||||||||||||||||
| 250 | 0 | ▶ | if(myencoding_detect_unicode_has_end(&res, 10)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 251 | 0 | *encoding = MyENCODING_UTF_8; | |||||||||||||||||
| 252 | 0 | return true; | |||||||||||||||||
| 253 | } | ||||||||||||||||||
| 254 | |||||||||||||||||||
| 255 | 0 | return false; | |||||||||||||||||
| 256 | } | ||||||||||||||||||
| 257 | |||||||||||||||||||
| 258 | 0 | bool myencoding_detect_russian(const char *text, size_t length, myencoding_t *encoding) | |||||||||||||||||
| 259 | { | ||||||||||||||||||
| 260 | 0 | unsigned const char *u_text = (unsigned const char*)text; | |||||||||||||||||
| 261 | |||||||||||||||||||
| 262 | 0 | size_t min_count = 50; | |||||||||||||||||
| 263 | 0 | size_t min_value = 100000; | |||||||||||||||||
| 264 | 0 | size_t max_value = 0; | |||||||||||||||||
| 265 | |||||||||||||||||||
| 266 | 0 | *encoding = MyENCODING_DEFAULT; | |||||||||||||||||
| 267 | |||||||||||||||||||
| 268 | myencoding_trigram_result_t | ||||||||||||||||||
| 269 | 0 | res = myencoding_detect_by_trigram(u_text, length, myencoding_detect_trigrams_index_windows_1251, 1000, min_value); | |||||||||||||||||
| 270 | 0 | ▶ | if(myencoding_detect_russian_has_end(&res, min_count, min_value)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 271 | 0 | *encoding = MyENCODING_WINDOWS_1251; | |||||||||||||||||
| 272 | 0 | return true; | |||||||||||||||||
| 273 | } | ||||||||||||||||||
| 274 | |||||||||||||||||||
| 275 | 0 | max_value = res.value; | |||||||||||||||||
| 276 | 0 | ▶ | if(max_value) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 277 | 0 | *encoding = MyENCODING_WINDOWS_1251; | |||||||||||||||||
| 278 | } | ||||||||||||||||||
| 279 | |||||||||||||||||||
| 280 | 0 | res = myencoding_detect_by_trigram(u_text, length, myencoding_detect_trigrams_index_koi8_r, 1000, min_value); | |||||||||||||||||
| 281 | 0 | ▶ | if(myencoding_detect_russian_has_end(&res, min_count, min_value)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 282 | 0 | *encoding = MyENCODING_KOI8_R; | |||||||||||||||||
| 283 | 0 | return true; | |||||||||||||||||
| 284 | } | ||||||||||||||||||
| 285 | |||||||||||||||||||
| 286 | 0 | ▶ | if(max_value < res.value) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 287 | 0 | *encoding = MyENCODING_KOI8_R; | |||||||||||||||||
| 288 | 0 | max_value = res.value; | |||||||||||||||||
| 289 | } | ||||||||||||||||||
| 290 | |||||||||||||||||||
| 291 | 0 | res = myencoding_detect_by_trigram(u_text, length, myencoding_detect_trigrams_index_iso_8859_5, 1000, min_value); | |||||||||||||||||
| 292 | 0 | ▶ | if(myencoding_detect_russian_has_end(&res, min_count, min_value)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 293 | 0 | *encoding = MyENCODING_ISO_8859_5; | |||||||||||||||||
| 294 | 0 | return true; | |||||||||||||||||
| 295 | } | ||||||||||||||||||
| 296 | |||||||||||||||||||
| 297 | 0 | ▶ | if(max_value < res.value) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 298 | 0 | *encoding = MyENCODING_ISO_8859_5; | |||||||||||||||||
| 299 | 0 | max_value = res.value; | |||||||||||||||||
| 300 | } | ||||||||||||||||||
| 301 | |||||||||||||||||||
| 302 | 0 | res = myencoding_detect_by_trigram(u_text, length, myencoding_detect_trigrams_index_x_mac_cyrillic, 1000, min_value); | |||||||||||||||||
| 303 | 0 | ▶ | if(myencoding_detect_russian_has_end(&res, min_count, min_value)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 304 | 0 | *encoding = MyENCODING_X_MAC_CYRILLIC; | |||||||||||||||||
| 305 | 0 | return true; | |||||||||||||||||
| 306 | } | ||||||||||||||||||
| 307 | |||||||||||||||||||
| 308 | 0 | ▶ | if(max_value < res.value) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 309 | 0 | *encoding = MyENCODING_X_MAC_CYRILLIC; | |||||||||||||||||
| 310 | 0 | max_value = res.value; | |||||||||||||||||
| 311 | } | ||||||||||||||||||
| 312 | |||||||||||||||||||
| 313 | 0 | res = myencoding_detect_by_trigram(u_text, length, myencoding_detect_trigrams_index_ibm866, 1000, min_value); | |||||||||||||||||
| 314 | 0 | ▶ | if(myencoding_detect_russian_has_end(&res, min_count, min_value)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 315 | 0 | *encoding = MyENCODING_IBM866; | |||||||||||||||||
| 316 | 0 | return true; | |||||||||||||||||
| 317 | } | ||||||||||||||||||
| 318 | |||||||||||||||||||
| 319 | 0 | ▶ | if(max_value < res.value) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 320 | 0 | *encoding = MyENCODING_IBM866; | |||||||||||||||||
| 321 | } | ||||||||||||||||||
| 322 | |||||||||||||||||||
| 323 | 0 | return false; | |||||||||||||||||
| 324 | } | ||||||||||||||||||
| 325 | |||||||||||||||||||
| 326 | 0 | bool myencoding_detect(const char *text, size_t length, myencoding_t *encoding) | |||||||||||||||||
| 327 | { | ||||||||||||||||||
| 328 | 0 | *encoding = MyENCODING_DEFAULT; | |||||||||||||||||
| 329 | |||||||||||||||||||
| 330 | 0 | ▶ | if(myencoding_detect_unicode(text, length, encoding)) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 331 | 0 | return true; | |||||||||||||||||
| 332 | |||||||||||||||||||
| 333 | 0 | ▶ | if(myencoding_detect_russian(text, length, encoding)) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 334 | 0 | return true; | |||||||||||||||||
| 335 | |||||||||||||||||||
| 336 | 0 | return false; | |||||||||||||||||
| 337 | } | ||||||||||||||||||
| 338 | |||||||||||||||||||
| 339 | 0 | const myencoding_detect_name_entry_t * myencoding_name_entry_by_name(const char* name, size_t length) | |||||||||||||||||
| 340 | { | ||||||||||||||||||
| 341 | 0 | size_t idx = ((mycore_string_chars_lowercase_map[ (const unsigned char)name[0] ] * | |||||||||||||||||
| 342 | 0 | mycore_string_chars_lowercase_map[ (const unsigned char)name[(length - 1)] ] * | |||||||||||||||||
| 343 | length) | ||||||||||||||||||
| 344 | 0 | % MyENCODING_DETECT_NAME_STATIC_SIZE) + 1; | |||||||||||||||||
| 345 | |||||||||||||||||||
| 346 | 0 | ▶ | while (myencoding_detect_name_entry_static_list_index[idx].label) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 347 | { | ||||||||||||||||||
| 348 | 0 | ▶ | if(myencoding_detect_name_entry_static_list_index[idx].label_length == length) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 349 | 0 | ▶ | if(mycore_strncasecmp(myencoding_detect_name_entry_static_list_index[idx].label, name, length) == 0) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 350 | 0 | return &myencoding_detect_name_entry_static_list_index[idx]; | |||||||||||||||||
| 351 | |||||||||||||||||||
| 352 | 0 | ▶ | if(myencoding_detect_name_entry_static_list_index[idx].next) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 353 | 0 | idx = myencoding_detect_name_entry_static_list_index[idx].next; | |||||||||||||||||
| 354 | else | ||||||||||||||||||
| 355 | 0 | return NULL; | |||||||||||||||||
| 356 | } | ||||||||||||||||||
| 357 | 0 | ▶ | else if(myencoding_detect_name_entry_static_list_index[idx].label_length > length) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 358 | 0 | return NULL; | |||||||||||||||||
| 359 | } | ||||||||||||||||||
| 360 | else { | ||||||||||||||||||
| 361 | 0 | idx = myencoding_detect_name_entry_static_list_index[idx].next; | |||||||||||||||||
| 362 | } | ||||||||||||||||||
| 363 | } | ||||||||||||||||||
| 364 | |||||||||||||||||||
| 365 | 0 | return NULL; | |||||||||||||||||
| 366 | } | ||||||||||||||||||
| 367 | |||||||||||||||||||
| 368 | 0 | bool myencoding_by_name(const char *name, size_t length, myencoding_t *encoding) | |||||||||||||||||
| 369 | { | ||||||||||||||||||
| 370 | 0 | const myencoding_detect_name_entry_t *entry = myencoding_name_entry_by_name(name, length); | |||||||||||||||||
| 371 | |||||||||||||||||||
| 372 | 0 | ▶ | if(entry) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 373 | 0 | ▶ | if(encoding) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 374 | 0 | *encoding = entry->encoding; | |||||||||||||||||
| 375 | |||||||||||||||||||
| 376 | 0 | return true; | |||||||||||||||||
| 377 | } | ||||||||||||||||||
| 378 | |||||||||||||||||||
| 379 | 0 | return false; | |||||||||||||||||
| 380 | } | ||||||||||||||||||
| 381 | |||||||||||||||||||
| 382 | 0 | const char * myencoding_name_by_id(myencoding_t encoding, size_t *length) | |||||||||||||||||
| 383 | { | ||||||||||||||||||
| 384 | 0 | ▶ | if(encoding >= MyENCODING_LAST_ENTRY) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 385 | 0 | ▶ | if(length) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 386 | 0 | *length = 0; | |||||||||||||||||
| 387 | } | ||||||||||||||||||
| 388 | |||||||||||||||||||
| 389 | 0 | return NULL; | |||||||||||||||||
| 390 | } | ||||||||||||||||||
| 391 | |||||||||||||||||||
| 392 | 0 | const myencoding_entry_name_index_t *entry = &myencoding_entry_name_index_static_list_index[encoding]; | |||||||||||||||||
| 393 | |||||||||||||||||||
| 394 | 0 | ▶ | if(length) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 395 | 0 | *length = entry->length; | |||||||||||||||||
| 396 | } | ||||||||||||||||||
| 397 | |||||||||||||||||||
| 398 | 0 | return entry->name; | |||||||||||||||||
| 399 | } | ||||||||||||||||||
| 400 | |||||||||||||||||||
| 401 | /* | ||||||||||||||||||
| 402 | When an algorithm requires a user agent to prescan a byte stream to determine its encoding, | ||||||||||||||||||
| 403 | given some defined end condition, then it must run the following steps. | ||||||||||||||||||
| 404 | These steps either abort unsuccessfully or return a character encoding. | ||||||||||||||||||
| 405 | If at any point during these steps (including during instances of the get an attribute algorithm invoked by this one) | ||||||||||||||||||
| 406 | the user agent either runs out of bytes (meaning the position pointer created in the first step below goes beyond the end of the byte stream obtained so far) | ||||||||||||||||||
| 407 | or reaches its end condition, then abort the prescan a byte stream to determine its encoding algorithm unsuccessfully. | ||||||||||||||||||
| 408 | */ | ||||||||||||||||||
| 409 | 0 | bool myencoding_extracting_character_encoding_from_charset(const char *data, size_t data_size, myencoding_t *encoding) | |||||||||||||||||
| 410 | { | ||||||||||||||||||
| 411 | 0 | return myencoding_extracting_character_encoding_from_charset_with_found(data, data_size, encoding, NULL, NULL); | |||||||||||||||||
| 412 | } | ||||||||||||||||||
| 413 | |||||||||||||||||||
| 414 | 0 | bool myencoding_extracting_character_encoding_from_charset_with_found(const char *data, size_t data_size, myencoding_t *encoding, const char **found, size_t *found_length) | |||||||||||||||||
| 415 | { | ||||||||||||||||||
| 416 | 0 | *encoding = MyENCODING_NOT_DETERMINED; | |||||||||||||||||
| 417 | |||||||||||||||||||
| 418 | 0 | ▶ | if(found) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 419 | 0 | *found = NULL; | |||||||||||||||||
| 420 | 0 | ▶ | if(found_length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 421 | 0 | *found_length = 0; | |||||||||||||||||
| 422 | |||||||||||||||||||
| 423 | /* 1 */ | ||||||||||||||||||
| 424 | 0 | size_t length = 0; | |||||||||||||||||
| 425 | 0 | size_t charset_length = strlen("charset"); | |||||||||||||||||
| 426 | |||||||||||||||||||
| 427 | 0 | bool is_get_pos = false; | |||||||||||||||||
| 428 | 0 | const unsigned char *udata = (const unsigned char *)data; | |||||||||||||||||
| 429 | |||||||||||||||||||
| 430 | /* 2 */ | ||||||||||||||||||
| 431 | 0 | ▶ | while((length + charset_length) < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 432 | 0 | ▶ | if(mycore_ustrcasecmp_without_checks_by_secondary((const unsigned char*)"charset", &udata[length])) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 433 | { | ||||||||||||||||||
| 434 | 0 | length += charset_length; | |||||||||||||||||
| 435 | |||||||||||||||||||
| 436 | /* 2 */ | ||||||||||||||||||
| 437 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 438 | 0 | ▶ | if(udata[length] != 0x09 && udata[length] != 0x0A && udata[length] != 0x0C && | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 439 | 0 | ▶ | udata[length] != 0x0D && udata[length] != 0x20) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 440 | { | ||||||||||||||||||
| 441 | 0 | break; | |||||||||||||||||
| 442 | } | ||||||||||||||||||
| 443 | |||||||||||||||||||
| 444 | 0 | length++; | |||||||||||||||||
| 445 | } | ||||||||||||||||||
| 446 | |||||||||||||||||||
| 447 | /* 4 */ | ||||||||||||||||||
| 448 | 0 | ▶ | if(udata[length] == 0x3D) { /* EQUALS SIGN (=) */ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 449 | 0 | is_get_pos = true; | |||||||||||||||||
| 450 | |||||||||||||||||||
| 451 | 0 | length++; | |||||||||||||||||
| 452 | 0 | break; | |||||||||||||||||
| 453 | } | ||||||||||||||||||
| 454 | } | ||||||||||||||||||
| 455 | |||||||||||||||||||
| 456 | 0 | length++; | |||||||||||||||||
| 457 | } | ||||||||||||||||||
| 458 | |||||||||||||||||||
| 459 | 0 | ▶ | if(is_get_pos == false || length >= data_size) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 460 | 0 | return false; | |||||||||||||||||
| 461 | |||||||||||||||||||
| 462 | /* 5 */ | ||||||||||||||||||
| 463 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 464 | 0 | ▶ | if(udata[length] != 0x09 && udata[length] != 0x0A && udata[length] != 0x0C && | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 465 | 0 | ▶ | udata[length] != 0x0D && udata[length] != 0x20) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 466 | { | ||||||||||||||||||
| 467 | 0 | break; | |||||||||||||||||
| 468 | } | ||||||||||||||||||
| 469 | |||||||||||||||||||
| 470 | 0 | length++; | |||||||||||||||||
| 471 | } | ||||||||||||||||||
| 472 | |||||||||||||||||||
| 473 | 0 | ▶ | if(length >= data_size) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 474 | 0 | return false; | |||||||||||||||||
| 475 | |||||||||||||||||||
| 476 | /* 6 */ | ||||||||||||||||||
| 477 | /* " */ | ||||||||||||||||||
| 478 | 0 | ▶ | if(udata[length] == 0x22) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 479 | { | ||||||||||||||||||
| 480 | 0 | length++; | |||||||||||||||||
| 481 | 0 | size_t begin = length; | |||||||||||||||||
| 482 | |||||||||||||||||||
| 483 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 484 | 0 | ▶ | if(udata[length] == 0x22) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 485 | { | ||||||||||||||||||
| 486 | 0 | ▶ | if(found) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 487 | 0 | *found = &data[begin]; | |||||||||||||||||
| 488 | 0 | ▶ | if(found_length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 489 | 0 | *found_length = (length - begin); | |||||||||||||||||
| 490 | |||||||||||||||||||
| 491 | 0 | return myencoding_by_name(&data[begin], (length - begin), encoding); | |||||||||||||||||
| 492 | } | ||||||||||||||||||
| 493 | |||||||||||||||||||
| 494 | 0 | length++; | |||||||||||||||||
| 495 | } | ||||||||||||||||||
| 496 | |||||||||||||||||||
| 497 | 0 | return false; | |||||||||||||||||
| 498 | } | ||||||||||||||||||
| 499 | |||||||||||||||||||
| 500 | /* ' */ | ||||||||||||||||||
| 501 | 0 | ▶ | if(udata[length] == 0x27) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 502 | { | ||||||||||||||||||
| 503 | 0 | length++; | |||||||||||||||||
| 504 | 0 | size_t begin = length; | |||||||||||||||||
| 505 | |||||||||||||||||||
| 506 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 507 | 0 | ▶ | if(udata[length] == 0x27) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 508 | { | ||||||||||||||||||
| 509 | 0 | ▶ | if(found) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 510 | 0 | *found = &data[begin]; | |||||||||||||||||
| 511 | 0 | ▶ | if(found_length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 512 | 0 | *found_length = (length - begin); | |||||||||||||||||
| 513 | |||||||||||||||||||
| 514 | 0 | return myencoding_by_name(&data[begin], (length - begin), encoding); | |||||||||||||||||
| 515 | } | ||||||||||||||||||
| 516 | |||||||||||||||||||
| 517 | 0 | length++; | |||||||||||||||||
| 518 | } | ||||||||||||||||||
| 519 | |||||||||||||||||||
| 520 | 0 | return false; | |||||||||||||||||
| 521 | } | ||||||||||||||||||
| 522 | |||||||||||||||||||
| 523 | /* other */ | ||||||||||||||||||
| 524 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 525 | 0 | ▶ | if(udata[length] != 0x09 && udata[length] != 0x0A && udata[length] != 0x0C && | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 526 | 0 | ▶ | udata[length] != 0x0D && udata[length] != 0x20) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 527 | { | ||||||||||||||||||
| 528 | 0 | size_t begin = length; | |||||||||||||||||
| 529 | |||||||||||||||||||
| 530 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 531 | /* SEMICOLON character (;) */ | ||||||||||||||||||
| 532 | 0 | ▶ | if(udata[length] == 0x3B) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 533 | { | ||||||||||||||||||
| 534 | 0 | ▶ | if(found) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 535 | 0 | *found = &data[begin]; | |||||||||||||||||
| 536 | 0 | ▶ | if(found_length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 537 | 0 | *found_length = (length - begin); | |||||||||||||||||
| 538 | |||||||||||||||||||
| 539 | 0 | return myencoding_by_name(&data[begin], (length - begin), encoding); | |||||||||||||||||
| 540 | } | ||||||||||||||||||
| 541 | |||||||||||||||||||
| 542 | 0 | length++; | |||||||||||||||||
| 543 | } | ||||||||||||||||||
| 544 | |||||||||||||||||||
| 545 | 0 | ▶ | if(found) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 546 | 0 | *found = &data[begin]; | |||||||||||||||||
| 547 | 0 | ▶ | if(found_length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 548 | 0 | *found_length = (length - begin); | |||||||||||||||||
| 549 | |||||||||||||||||||
| 550 | 0 | return myencoding_by_name(&data[begin], (length - begin), encoding); | |||||||||||||||||
| 551 | } | ||||||||||||||||||
| 552 | |||||||||||||||||||
| 553 | 0 | length++; | |||||||||||||||||
| 554 | } | ||||||||||||||||||
| 555 | |||||||||||||||||||
| 556 | 0 | return false; | |||||||||||||||||
| 557 | } | ||||||||||||||||||
| 558 | |||||||||||||||||||
| 559 | 0 | bool myencoding_prescan_stream_to_determine_encoding_get_attr_spaces(const unsigned char *udata, size_t *data_length, size_t data_size, myencoding_detect_attr_t *attr) | |||||||||||||||||
| 560 | { | ||||||||||||||||||
| 561 | 0 | size_t length = *data_length; | |||||||||||||||||
| 562 | |||||||||||||||||||
| 563 | /* set position */ | ||||||||||||||||||
| 564 | 0 | attr->key_length = length - attr->key_begin; | |||||||||||||||||
| 565 | |||||||||||||||||||
| 566 | /* 6 */ | ||||||||||||||||||
| 567 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 568 | 0 | ▶ | if(udata[length] != 0x09 && udata[length] != 0x0A && udata[length] != 0x0C && | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 569 | 0 | ▶ | udata[length] != 0x0D && udata[length] != 0x20 && udata[length] != 0x2F) | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 570 | { | ||||||||||||||||||
| 571 | 0 | break; | |||||||||||||||||
| 572 | } | ||||||||||||||||||
| 573 | |||||||||||||||||||
| 574 | 0 | length++; | |||||||||||||||||
| 575 | } | ||||||||||||||||||
| 576 | |||||||||||||||||||
| 577 | 0 | ▶ | if(length >= data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 578 | 0 | *data_length = length; | |||||||||||||||||
| 579 | 0 | return false; | |||||||||||||||||
| 580 | } | ||||||||||||||||||
| 581 | |||||||||||||||||||
| 582 | /* 7 */ | ||||||||||||||||||
| 583 | 0 | ▶ | if(udata[length] != 0x3D) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 584 | 0 | *data_length = length; | |||||||||||||||||
| 585 | 0 | return false; | |||||||||||||||||
| 586 | } | ||||||||||||||||||
| 587 | |||||||||||||||||||
| 588 | /* 8 */ | ||||||||||||||||||
| 589 | 0 | *data_length = (length + 1); | |||||||||||||||||
| 590 | 0 | return true; | |||||||||||||||||
| 591 | } | ||||||||||||||||||
| 592 | |||||||||||||||||||
| 593 | 0 | size_t myencoding_prescan_stream_to_determine_encoding_get_attr_value(const unsigned char *udata, size_t length, size_t data_size, myencoding_detect_attr_t *attr, bool *it_last) | |||||||||||||||||
| 594 | { | ||||||||||||||||||
| 595 | /* 9 */ | ||||||||||||||||||
| 596 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 597 | 0 | ▶ | if(udata[length] != 0x09 && udata[length] != 0x0A && udata[length] != 0x0C && | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 598 | 0 | ▶ | udata[length] != 0x0D && udata[length] != 0x20) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 599 | { | ||||||||||||||||||
| 600 | 0 | break; | |||||||||||||||||
| 601 | } | ||||||||||||||||||
| 602 | |||||||||||||||||||
| 603 | 0 | length++; | |||||||||||||||||
| 604 | } | ||||||||||||||||||
| 605 | |||||||||||||||||||
| 606 | 0 | ▶ | if(length >= data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 607 | 0 | *it_last = true; | |||||||||||||||||
| 608 | 0 | return length; | |||||||||||||||||
| 609 | } | ||||||||||||||||||
| 610 | |||||||||||||||||||
| 611 | /* 10 */ | ||||||||||||||||||
| 612 | 0 | switch (udata[length]) { | |||||||||||||||||
| 613 | 0 | case 0x22: /* (ASCII ") */ | |||||||||||||||||
| 614 | 0 | length++; | |||||||||||||||||
| 615 | 0 | attr->value_begin = length; | |||||||||||||||||
| 616 | |||||||||||||||||||
| 617 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 618 | 0 | ▶ | if(udata[length] == 0x22) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 619 | { | ||||||||||||||||||
| 620 | 0 | attr->value_length = length - attr->value_begin; | |||||||||||||||||
| 621 | 0 | return (length + 1); | |||||||||||||||||
| 622 | } | ||||||||||||||||||
| 623 | |||||||||||||||||||
| 624 | 0 | length++; | |||||||||||||||||
| 625 | } | ||||||||||||||||||
| 626 | |||||||||||||||||||
| 627 | 0 | break; | |||||||||||||||||
| 628 | |||||||||||||||||||
| 629 | 0 | case 0x27: /* (ASCII ') */ | |||||||||||||||||
| 630 | 0 | length++; | |||||||||||||||||
| 631 | 0 | attr->value_begin = length; | |||||||||||||||||
| 632 | |||||||||||||||||||
| 633 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 634 | 0 | ▶ | if(udata[length] == 0x27) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 635 | { | ||||||||||||||||||
| 636 | 0 | attr->value_length = length - attr->value_begin; | |||||||||||||||||
| 637 | 0 | return (length + 1); | |||||||||||||||||
| 638 | } | ||||||||||||||||||
| 639 | |||||||||||||||||||
| 640 | 0 | length++; | |||||||||||||||||
| 641 | } | ||||||||||||||||||
| 642 | |||||||||||||||||||
| 643 | 0 | break; | |||||||||||||||||
| 644 | |||||||||||||||||||
| 645 | 0 | case 0x3E: /* (ASCII >) */ | |||||||||||||||||
| 646 | 0 | *it_last = true; | |||||||||||||||||
| 647 | 0 | return (length + 1); | |||||||||||||||||
| 648 | |||||||||||||||||||
| 649 | 0 | default: | |||||||||||||||||
| 650 | 0 | attr->value_begin = length; | |||||||||||||||||
| 651 | |||||||||||||||||||
| 652 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 653 | 0 | ▶ | if(udata[length] == 0x09 || udata[length] == 0x0A || udata[length] == 0x0C || | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 654 | 0 | ▶ | udata[length] == 0x0D || udata[length] == 0x20 || udata[length] == 0x3E) | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 655 | { | ||||||||||||||||||
| 656 | 0 | attr->value_length = length - attr->value_begin; | |||||||||||||||||
| 657 | 0 | return (length + 1); | |||||||||||||||||
| 658 | } | ||||||||||||||||||
| 659 | |||||||||||||||||||
| 660 | 0 | length++; | |||||||||||||||||
| 661 | } | ||||||||||||||||||
| 662 | |||||||||||||||||||
| 663 | 0 | break; | |||||||||||||||||
| 664 | } | ||||||||||||||||||
| 665 | |||||||||||||||||||
| 666 | 0 | attr->value_length = length - attr->value_begin; | |||||||||||||||||
| 667 | 0 | return length; | |||||||||||||||||
| 668 | } | ||||||||||||||||||
| 669 | |||||||||||||||||||
| 670 | 0 | size_t myencoding_prescan_stream_to_determine_encoding_get_attr(const unsigned char *udata, size_t length, size_t data_size, myencoding_detect_attr_t *attr, bool *it_last) | |||||||||||||||||
| 671 | { | ||||||||||||||||||
| 672 | 0 | memset(attr, 0, sizeof(myencoding_detect_attr_t)); | |||||||||||||||||
| 673 | |||||||||||||||||||
| 674 | /* | ||||||||||||||||||
| 675 | If the byte at position is one of 0x09 (ASCII TAB), 0x0A (ASCII LF), 0x0C (ASCII FF), 0x0D (ASCII CR), | ||||||||||||||||||
| 676 | 0x20 (ASCII space), or 0x2F (ASCII /) then advance position to the next byte and redo this step. | ||||||||||||||||||
| 677 | */ | ||||||||||||||||||
| 678 | /* 1 */ | ||||||||||||||||||
| 679 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 680 | 0 | ▶ | if(udata[length] != 0x09 && udata[length] != 0x0A && udata[length] != 0x0C && | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 681 | 0 | ▶ | udata[length] != 0x0D && udata[length] != 0x20 && udata[length] != 0x2F) | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 682 | { | ||||||||||||||||||
| 683 | 0 | break; | |||||||||||||||||
| 684 | } | ||||||||||||||||||
| 685 | |||||||||||||||||||
| 686 | 0 | length++; | |||||||||||||||||
| 687 | } | ||||||||||||||||||
| 688 | |||||||||||||||||||
| 689 | 0 | ▶ | if(length >= data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 690 | 0 | *it_last = true; | |||||||||||||||||
| 691 | 0 | return length; | |||||||||||||||||
| 692 | } | ||||||||||||||||||
| 693 | |||||||||||||||||||
| 694 | /* 2 */ | ||||||||||||||||||
| 695 | 0 | ▶ | if(udata[length] == 0x3E) { /* (ASCII >) */ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 696 | 0 | *it_last = true; | |||||||||||||||||
| 697 | 0 | return (length + 1); | |||||||||||||||||
| 698 | } | ||||||||||||||||||
| 699 | |||||||||||||||||||
| 700 | 0 | attr->key_begin = length; | |||||||||||||||||
| 701 | |||||||||||||||||||
| 702 | /* 3, 4 */ | ||||||||||||||||||
| 703 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 704 | 0 | switch (udata[length]) { | |||||||||||||||||
| 705 | 0 | case 0x3D: /* (ASCII =) */ | |||||||||||||||||
| 706 | 0 | ▶ | if(attr->key_begin != (length - 1)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 707 | 0 | attr->key_length = length - attr->key_begin; | |||||||||||||||||
| 708 | |||||||||||||||||||
| 709 | 0 | length++; | |||||||||||||||||
| 710 | 0 | return myencoding_prescan_stream_to_determine_encoding_get_attr_value(udata, length, data_size, attr, it_last); | |||||||||||||||||
| 711 | } | ||||||||||||||||||
| 712 | |||||||||||||||||||
| 713 | /* fall through */ | ||||||||||||||||||
| 714 | |||||||||||||||||||
| 715 | case 0x09: /* (ASCII TAB) */ | ||||||||||||||||||
| 716 | case 0x0A: /* (ASCII LF) */ | ||||||||||||||||||
| 717 | case 0x0C: /* (ASCII FF) */ | ||||||||||||||||||
| 718 | case 0x0D: /* (ASCII CR) */ | ||||||||||||||||||
| 719 | case 0x20: /* (ASCII space) */ | ||||||||||||||||||
| 720 | 0 | length++; | |||||||||||||||||
| 721 | |||||||||||||||||||
| 722 | 0 | ▶ | if(myencoding_prescan_stream_to_determine_encoding_get_attr_spaces(udata, &length, data_size, attr) == false) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 723 | 0 | *it_last = true; | |||||||||||||||||
| 724 | 0 | return length; | |||||||||||||||||
| 725 | } | ||||||||||||||||||
| 726 | |||||||||||||||||||
| 727 | 0 | return myencoding_prescan_stream_to_determine_encoding_get_attr_value(udata, length, data_size, attr, it_last); | |||||||||||||||||
| 728 | |||||||||||||||||||
| 729 | 0 | case 0x2F: /* (ASCII /) */ | |||||||||||||||||
| 730 | case 0x3E: /* (ASCII >) */ | ||||||||||||||||||
| 731 | 0 | *it_last = true; | |||||||||||||||||
| 732 | 0 | attr->key_length = length - attr->key_begin; | |||||||||||||||||
| 733 | |||||||||||||||||||
| 734 | 0 | return (length + 1); | |||||||||||||||||
| 735 | |||||||||||||||||||
| 736 | 0 | default: | |||||||||||||||||
| 737 | 0 | break; | |||||||||||||||||
| 738 | } | ||||||||||||||||||
| 739 | |||||||||||||||||||
| 740 | 0 | length++; | |||||||||||||||||
| 741 | } | ||||||||||||||||||
| 742 | |||||||||||||||||||
| 743 | 0 | ▶ | if(myencoding_prescan_stream_to_determine_encoding_get_attr_spaces(udata, &length, data_size, attr) == false) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 744 | 0 | *it_last = true; | |||||||||||||||||
| 745 | 0 | return length; | |||||||||||||||||
| 746 | } | ||||||||||||||||||
| 747 | |||||||||||||||||||
| 748 | 0 | return myencoding_prescan_stream_to_determine_encoding_get_attr_value(udata, length, data_size, attr, it_last); | |||||||||||||||||
| 749 | } | ||||||||||||||||||
| 750 | |||||||||||||||||||
| 751 | 0 | bool myencoding_prescan_stream_to_determine_encoding_check_meta(const unsigned char *udata, size_t *length, size_t data_size, myencoding_t *encoding, const char **found, size_t *found_length) | |||||||||||||||||
| 752 | { | ||||||||||||||||||
| 753 | myencoding_detect_attr_t attr; | ||||||||||||||||||
| 754 | |||||||||||||||||||
| 755 | 0 | bool got_pragma = false; | |||||||||||||||||
| 756 | 0 | bool it_last = false; | |||||||||||||||||
| 757 | |||||||||||||||||||
| 758 | 0 | unsigned int need_pragma = 0; /* 0 = NULL, 1 = false, 2 = true */ | |||||||||||||||||
| 759 | |||||||||||||||||||
| 760 | /* | ||||||||||||||||||
| 761 | http-equiv = 1 | ||||||||||||||||||
| 762 | content = 2 | ||||||||||||||||||
| 763 | charset = 4 | ||||||||||||||||||
| 764 | */ | ||||||||||||||||||
| 765 | /* If the attribute's name is already in attribute list, then return to the step labeled attributes. */ | ||||||||||||||||||
| 766 | 0 | size_t is_exists = 0; | |||||||||||||||||
| 767 | |||||||||||||||||||
| 768 | 0 | ▶ | while(*length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 769 | 0 | *length = myencoding_prescan_stream_to_determine_encoding_get_attr(udata, *length, data_size, &attr, &it_last); | |||||||||||||||||
| 770 | |||||||||||||||||||
| 771 | /* 9 */ | ||||||||||||||||||
| 772 | 0 | if(attr.key_length == strlen("http-equiv") && | |||||||||||||||||
| 773 | 0 | mycore_ustrcasecmp_without_checks_by_secondary((const unsigned char*)"http-equiv", &udata[ attr.key_begin ])) | |||||||||||||||||
| 774 | { | ||||||||||||||||||
| 775 | 0 | ▶ | if((is_exists & 1) == 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 776 | 0 | is_exists |= 1; | |||||||||||||||||
| 777 | |||||||||||||||||||
| 778 | 0 | if(attr.value_length == strlen("content-type") && | |||||||||||||||||
| 779 | 0 | mycore_ustrcasecmp_without_checks_by_secondary((const unsigned char*)"content-type", &udata[ attr.value_begin ])) | |||||||||||||||||
| 780 | { | ||||||||||||||||||
| 781 | 0 | got_pragma = true; | |||||||||||||||||
| 782 | } | ||||||||||||||||||
| 783 | } | ||||||||||||||||||
| 784 | } | ||||||||||||||||||
| 785 | 0 | else if(attr.key_length == strlen("content") && | |||||||||||||||||
| 786 | 0 | mycore_ustrcasecmp_without_checks_by_secondary((const unsigned char*)"content", &udata[ attr.key_begin ])) | |||||||||||||||||
| 787 | { | ||||||||||||||||||
| 788 | 0 | ▶ | if((is_exists & 2) == 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 789 | 0 | is_exists |= 2; | |||||||||||||||||
| 790 | |||||||||||||||||||
| 791 | 0 | ▶ | if(myencoding_extracting_character_encoding_from_charset_with_found((const char*)(&udata[ attr.value_begin ]), attr.value_length, encoding, found, found_length)) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 792 | 0 | need_pragma = 2; | |||||||||||||||||
| 793 | } | ||||||||||||||||||
| 794 | } | ||||||||||||||||||
| 795 | } | ||||||||||||||||||
| 796 | 0 | else if(attr.key_length == strlen("charset") && | |||||||||||||||||
| 797 | 0 | mycore_ustrcasecmp_without_checks_by_secondary((const unsigned char*)"charset", &udata[ attr.key_begin ])) | |||||||||||||||||
| 798 | { | ||||||||||||||||||
| 799 | 0 | ▶ | if((is_exists & 4) == 0) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 800 | 0 | is_exists |= 4; | |||||||||||||||||
| 801 | |||||||||||||||||||
| 802 | 0 | ▶ | if(found) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 803 | 0 | *found = (const char*)(&udata[ attr.value_begin ]); | |||||||||||||||||
| 804 | 0 | ▶ | if(found_length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 805 | 0 | *found_length = attr.value_length; | |||||||||||||||||
| 806 | |||||||||||||||||||
| 807 | 0 | myencoding_by_name((const char*)(&udata[ attr.value_begin ]), attr.value_length, encoding); | |||||||||||||||||
| 808 | 0 | need_pragma = 1; | |||||||||||||||||
| 809 | } | ||||||||||||||||||
| 810 | } | ||||||||||||||||||
| 811 | |||||||||||||||||||
| 812 | 0 | ▶ | if(it_last) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 813 | 0 | break; | |||||||||||||||||
| 814 | } | ||||||||||||||||||
| 815 | |||||||||||||||||||
| 816 | /* 11, 12, 13 */ | ||||||||||||||||||
| 817 | 0 | ▶ | if(need_pragma == 0 || (need_pragma == 2 && got_pragma == false)) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 818 | 0 | *encoding = MyENCODING_NOT_DETERMINED; | |||||||||||||||||
| 819 | 0 | return false; | |||||||||||||||||
| 820 | } | ||||||||||||||||||
| 821 | |||||||||||||||||||
| 822 | /* 14 */ | ||||||||||||||||||
| 823 | 0 | ▶ | if(*encoding == MyENCODING_UTF_16BE || *encoding == MyENCODING_UTF_16LE) { | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 824 | 0 | *encoding = MyENCODING_UTF_8; | |||||||||||||||||
| 825 | } | ||||||||||||||||||
| 826 | |||||||||||||||||||
| 827 | /* 15 */ | ||||||||||||||||||
| 828 | 0 | ▶ | if(*encoding == MyENCODING_X_USER_DEFINED) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 829 | 0 | *encoding = MyENCODING_WINDOWS_1252; | |||||||||||||||||
| 830 | } | ||||||||||||||||||
| 831 | |||||||||||||||||||
| 832 | /* 16 */ | ||||||||||||||||||
| 833 | 0 | return true; | |||||||||||||||||
| 834 | } | ||||||||||||||||||
| 835 | |||||||||||||||||||
| 836 | 0 | size_t myencoding_prescan_stream_to_determine_encoding_skip_name(const unsigned char *udata, size_t length, size_t data_size) | |||||||||||||||||
| 837 | { | ||||||||||||||||||
| 838 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 839 | 0 | ▶ | if(udata[length] != 0x09 && udata[length] != 0x0A && udata[length] != 0x0C && | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 840 | 0 | ▶ | udata[length] != 0x0D && udata[length] != 0x20) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 841 | { | ||||||||||||||||||
| 842 | 0 | break; | |||||||||||||||||
| 843 | } | ||||||||||||||||||
| 844 | |||||||||||||||||||
| 845 | 0 | length++; | |||||||||||||||||
| 846 | } | ||||||||||||||||||
| 847 | |||||||||||||||||||
| 848 | 0 | ▶ | if(length >= data_size) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 849 | 0 | return length; | |||||||||||||||||
| 850 | |||||||||||||||||||
| 851 | 0 | ▶ | if(udata[length] == 0x3E) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 852 | 0 | return (length + 1); | |||||||||||||||||
| 853 | } | ||||||||||||||||||
| 854 | |||||||||||||||||||
| 855 | myencoding_detect_attr_t attr; | ||||||||||||||||||
| 856 | 0 | bool it_last = false; | |||||||||||||||||
| 857 | |||||||||||||||||||
| 858 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 859 | 0 | length = myencoding_prescan_stream_to_determine_encoding_get_attr(udata, length, data_size, &attr, &it_last); | |||||||||||||||||
| 860 | |||||||||||||||||||
| 861 | 0 | ▶ | if(it_last) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 862 | 0 | return length; | |||||||||||||||||
| 863 | } | ||||||||||||||||||
| 864 | } | ||||||||||||||||||
| 865 | |||||||||||||||||||
| 866 | 0 | return length; | |||||||||||||||||
| 867 | } | ||||||||||||||||||
| 868 | |||||||||||||||||||
| 869 | 0 | size_t myencoding_prescan_stream_to_determine_encoding_skip_other(const unsigned char *udata, size_t length, size_t data_size) | |||||||||||||||||
| 870 | { | ||||||||||||||||||
| 871 | 0 | ▶ | if(udata[length] == 0x2F) { /* / */ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 872 | 0 | length++; | |||||||||||||||||
| 873 | |||||||||||||||||||
| 874 | 0 | ▶ | if(length >= data_size) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 875 | 0 | return length; | |||||||||||||||||
| 876 | |||||||||||||||||||
| 877 | 0 | ▶ | if(mycore_tokenizer_chars_map[ udata[length] ] == MyCORE_STRING_MAP_CHAR_A_Z_a_z) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 878 | 0 | return myencoding_prescan_stream_to_determine_encoding_skip_name(udata, length, data_size); | |||||||||||||||||
| 879 | } | ||||||||||||||||||
| 880 | |||||||||||||||||||
| 881 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 882 | 0 | ▶ | if(udata[length] != 0x3E) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 883 | 0 | return (length + 1); | |||||||||||||||||
| 884 | } | ||||||||||||||||||
| 885 | |||||||||||||||||||
| 886 | 0 | length++; | |||||||||||||||||
| 887 | } | ||||||||||||||||||
| 888 | |||||||||||||||||||
| 889 | 0 | return length; | |||||||||||||||||
| 890 | } | ||||||||||||||||||
| 891 | 0 | ▶ | else if(udata[length] == 0x21) { /* ! */ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 892 | 0 | length++; | |||||||||||||||||
| 893 | |||||||||||||||||||
| 894 | 0 | ▶ | if((length + 2) < data_size && udata[length] == 0x2D && udata[(length+1)] == 0x2D) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 895 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 896 | 0 | ▶ | if(udata[length] != 0x3E) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 897 | 0 | ▶ | if(udata[(length - 1)] == 0x2D && udata[(length - 2)] == 0x2D) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 898 | 0 | return (length + 1); | |||||||||||||||||
| 899 | } | ||||||||||||||||||
| 900 | |||||||||||||||||||
| 901 | 0 | length++; | |||||||||||||||||
| 902 | } | ||||||||||||||||||
| 903 | |||||||||||||||||||
| 904 | 0 | return length; | |||||||||||||||||
| 905 | } | ||||||||||||||||||
| 906 | |||||||||||||||||||
| 907 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 908 | 0 | ▶ | if(udata[length] != 0x3E) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 909 | 0 | return (length + 1); | |||||||||||||||||
| 910 | } | ||||||||||||||||||
| 911 | |||||||||||||||||||
| 912 | 0 | length++; | |||||||||||||||||
| 913 | } | ||||||||||||||||||
| 914 | |||||||||||||||||||
| 915 | 0 | return length; | |||||||||||||||||
| 916 | } | ||||||||||||||||||
| 917 | 0 | ▶ | else if(udata[length] == 0x3F) { /* ? */ | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 918 | 0 | length++; | |||||||||||||||||
| 919 | |||||||||||||||||||
| 920 | 0 | ▶ | while(length < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 921 | 0 | ▶ | if(udata[length] != 0x3E) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 922 | 0 | return (length + 1); | |||||||||||||||||
| 923 | } | ||||||||||||||||||
| 924 | |||||||||||||||||||
| 925 | 0 | length++; | |||||||||||||||||
| 926 | } | ||||||||||||||||||
| 927 | |||||||||||||||||||
| 928 | 0 | return length; | |||||||||||||||||
| 929 | } | ||||||||||||||||||
| 930 | |||||||||||||||||||
| 931 | |||||||||||||||||||
| 932 | 0 | return myencoding_prescan_stream_to_determine_encoding_skip_name(udata, length, data_size); | |||||||||||||||||
| 933 | } | ||||||||||||||||||
| 934 | |||||||||||||||||||
| 935 | 0 | myencoding_t myencoding_prescan_stream_to_determine_encoding(const char *data, size_t data_size) | |||||||||||||||||
| 936 | { | ||||||||||||||||||
| 937 | 0 | return myencoding_prescan_stream_to_determine_encoding_with_found(data, data_size, NULL, NULL); | |||||||||||||||||
| 938 | } | ||||||||||||||||||
| 939 | |||||||||||||||||||
| 940 | 0 | myencoding_t myencoding_prescan_stream_to_determine_encoding_with_found(const char *data, size_t data_size, const char **found, size_t *found_length) | |||||||||||||||||
| 941 | { | ||||||||||||||||||
| 942 | 0 | const unsigned char* udata = (const unsigned char*)data; | |||||||||||||||||
| 943 | 0 | myencoding_t encoding = MyENCODING_NOT_DETERMINED; | |||||||||||||||||
| 944 | |||||||||||||||||||
| 945 | 0 | ▶ | if(found) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 946 | 0 | *found = NULL; | |||||||||||||||||
| 947 | 0 | ▶ | if(found_length) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 948 | 0 | *found_length = 0; | |||||||||||||||||
| 949 | |||||||||||||||||||
| 950 | 0 | size_t i = 0; | |||||||||||||||||
| 951 | 0 | ▶ | while(i < data_size) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 952 | /* 0x3C = '<' */ | ||||||||||||||||||
| 953 | 0 | ▶ | if(data[i] == 0x3C) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 954 | { | ||||||||||||||||||
| 955 | 0 | ▶ | if((i + 5) >= data_size) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 956 | 0 | return encoding; | |||||||||||||||||
| 957 | |||||||||||||||||||
| 958 | 0 | i++; | |||||||||||||||||
| 959 | |||||||||||||||||||
| 960 | 0 | ▶ | switch (data[i]) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 961 | /* | ||||||||||||||||||
| 962 | A sequence of bytes starting with: | ||||||||||||||||||
| 963 | 0x3C, 0x4D or 0x6D, 0x45 or 0x65, 0x54 or 0x74, 0x41 or 0x61, | ||||||||||||||||||
| 964 | and one of 0x09, 0x0A, 0x0C, 0x0D, 0x20, 0x2F | ||||||||||||||||||
| 965 | (case-insensitive ASCII '<meta' followed by a space or slash) | ||||||||||||||||||
| 966 | */ | ||||||||||||||||||
| 967 | 0 | case 0x4D: | |||||||||||||||||
| 968 | case 0x6D: | ||||||||||||||||||
| 969 | 0 | ▶ | if(mycore_ustrcasecmp_without_checks_by_secondary((const unsigned char*)"meta", &udata[i])) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 970 | 0 | i += 4; | |||||||||||||||||
| 971 | |||||||||||||||||||
| 972 | 0 | ▶ | if(udata[i] == 0x09 || udata[i] == 0x0A || udata[i] == 0x0C || | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 973 | 0 | ▶ | udata[i] == 0x0D || udata[i] == 0x20 || udata[i] == 0x2F) | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 974 | { | ||||||||||||||||||
| 975 | 0 | i++; | |||||||||||||||||
| 976 | |||||||||||||||||||
| 977 | 0 | ▶ | if(myencoding_prescan_stream_to_determine_encoding_check_meta(udata, &i, data_size, &encoding, found, found_length)) | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 978 | 0 | return encoding; | |||||||||||||||||
| 979 | } | ||||||||||||||||||
| 980 | } | ||||||||||||||||||
| 981 | |||||||||||||||||||
| 982 | 0 | break; | |||||||||||||||||
| 983 | |||||||||||||||||||
| 984 | 0 | default: | |||||||||||||||||
| 985 | 0 | i = myencoding_prescan_stream_to_determine_encoding_skip_other(udata, i, data_size); | |||||||||||||||||
| 986 | 0 | break; | |||||||||||||||||
| 987 | } | ||||||||||||||||||
| 988 | } | ||||||||||||||||||
| 989 | else { | ||||||||||||||||||
| 990 | 0 | i++; | |||||||||||||||||
| 991 | } | ||||||||||||||||||
| 992 | } | ||||||||||||||||||
| 993 | |||||||||||||||||||
| 994 | 0 | return encoding; | |||||||||||||||||
| 995 | } | ||||||||||||||||||
| 996 | |||||||||||||||||||
| 997 | |||||||||||||||||||