* xmlDetectEncoding: * @ctxt: the parser context * * Handle optional BOM, detect and switch to encoding. * * Assumes that there are at least four bytes in the input buffer. */
| 1273 | * Assumes that there are at least four bytes in the input buffer. |
| 1274 | */ |
| 1275 | void |
| 1276 | xmlDetectEncoding(xmlParserCtxtPtr ctxt) { |
| 1277 | const xmlChar *in; |
| 1278 | xmlCharEncoding enc; |
| 1279 | int bomSize; |
| 1280 | int autoFlag = 0; |
| 1281 | |
| 1282 | if (xmlParserGrow(ctxt) < 0) |
| 1283 | return; |
| 1284 | in = ctxt->input->cur; |
| 1285 | if (ctxt->input->end - in < 4) |
| 1286 | return; |
| 1287 | |
| 1288 | if (ctxt->input->flags & XML_INPUT_HAS_ENCODING) { |
| 1289 | /* |
| 1290 | * If the encoding was already set, only skip the BOM which was |
| 1291 | * possibly decoded to UTF-8. |
| 1292 | */ |
| 1293 | if ((in[0] == 0xEF) && (in[1] == 0xBB) && (in[2] == 0xBF)) { |
| 1294 | ctxt->input->cur += 3; |
| 1295 | } |
| 1296 | |
| 1297 | return; |
| 1298 | } |
| 1299 | |
| 1300 | enc = XML_CHAR_ENCODING_NONE; |
| 1301 | bomSize = 0; |
| 1302 | |
| 1303 | switch (in[0]) { |
| 1304 | case 0x00: |
| 1305 | if ((in[1] == 0x00) && (in[2] == 0x00) && (in[3] == 0x3C)) { |
| 1306 | enc = XML_CHAR_ENCODING_UCS4BE; |
| 1307 | autoFlag = XML_INPUT_AUTO_OTHER; |
| 1308 | } else if ((in[1] == 0x3C) && (in[2] == 0x00) && (in[3] == 0x3F)) { |
| 1309 | enc = XML_CHAR_ENCODING_UTF16BE; |
| 1310 | autoFlag = XML_INPUT_AUTO_UTF16BE; |
| 1311 | } |
| 1312 | break; |
| 1313 | |
| 1314 | case 0x3C: |
| 1315 | if (in[1] == 0x00) { |
| 1316 | if ((in[2] == 0x00) && (in[3] == 0x00)) { |
| 1317 | enc = XML_CHAR_ENCODING_UCS4LE; |
| 1318 | autoFlag = XML_INPUT_AUTO_OTHER; |
| 1319 | } else if ((in[2] == 0x3F) && (in[3] == 0x00)) { |
| 1320 | enc = XML_CHAR_ENCODING_UTF16LE; |
| 1321 | autoFlag = XML_INPUT_AUTO_UTF16LE; |
| 1322 | } |
| 1323 | } |
| 1324 | break; |
| 1325 | |
| 1326 | case 0x4C: |
| 1327 | if ((in[1] == 0x6F) && (in[2] == 0xA7) && (in[3] == 0x94)) { |
| 1328 | enc = XML_CHAR_ENCODING_EBCDIC; |
| 1329 | autoFlag = XML_INPUT_AUTO_OTHER; |
| 1330 | } |
| 1331 | break; |
| 1332 |
no test coverage detected