* xmlSetDeclaredEncoding: * @ctxt: the parser context * @encoding: declared encoding * * Set the encoding from a declaration in the document. * * If no encoding was set yet, switch the encoding. Otherwise, only warn * about encoding mismatches. * * Takes ownership of 'encoding'. */
| 1378 | * Takes ownership of 'encoding'. |
| 1379 | */ |
| 1380 | void |
| 1381 | xmlSetDeclaredEncoding(xmlParserCtxtPtr ctxt, xmlChar *encoding) { |
| 1382 | if (((ctxt->input->flags & XML_INPUT_HAS_ENCODING) == 0) && |
| 1383 | ((ctxt->options & XML_PARSE_IGNORE_ENC) == 0)) { |
| 1384 | xmlCharEncodingHandlerPtr handler; |
| 1385 | int res; |
| 1386 | |
| 1387 | /* |
| 1388 | * xmlSwitchInputEncodingName treats unsupported encodings as |
| 1389 | * warnings, but we want it to be an error in an encoding |
| 1390 | * declaration. |
| 1391 | */ |
| 1392 | res = xmlOpenCharEncodingHandler((const char *) encoding, |
| 1393 | /* output */ 0, &handler); |
| 1394 | if (res != XML_ERR_OK) { |
| 1395 | xmlFatalErr(ctxt, res, (const char *) encoding); |
| 1396 | xmlFree(encoding); |
| 1397 | return; |
| 1398 | } |
| 1399 | |
| 1400 | res = xmlSwitchInputEncoding(ctxt, ctxt->input, handler); |
| 1401 | if (res != XML_ERR_OK) { |
| 1402 | xmlFree(encoding); |
| 1403 | return; |
| 1404 | } |
| 1405 | |
| 1406 | ctxt->input->flags |= XML_INPUT_USES_ENC_DECL; |
| 1407 | } else if (ctxt->input->flags & XML_INPUT_AUTO_ENCODING) { |
| 1408 | static const char *allowedUTF8[] = { |
| 1409 | "UTF-8", "UTF8", NULL |
| 1410 | }; |
| 1411 | static const char *allowedUTF16LE[] = { |
| 1412 | "UTF-16", "UTF-16LE", "UTF16", NULL |
| 1413 | }; |
| 1414 | static const char *allowedUTF16BE[] = { |
| 1415 | "UTF-16", "UTF-16BE", "UTF16", NULL |
| 1416 | }; |
| 1417 | const char **allowed = NULL; |
| 1418 | const char *autoEnc = NULL; |
| 1419 | |
| 1420 | switch (ctxt->input->flags & XML_INPUT_AUTO_ENCODING) { |
| 1421 | case XML_INPUT_AUTO_UTF8: |
| 1422 | allowed = allowedUTF8; |
| 1423 | autoEnc = "UTF-8"; |
| 1424 | break; |
| 1425 | case XML_INPUT_AUTO_UTF16LE: |
| 1426 | allowed = allowedUTF16LE; |
| 1427 | autoEnc = "UTF-16LE"; |
| 1428 | break; |
| 1429 | case XML_INPUT_AUTO_UTF16BE: |
| 1430 | allowed = allowedUTF16BE; |
| 1431 | autoEnc = "UTF-16BE"; |
| 1432 | break; |
| 1433 | } |
| 1434 | |
| 1435 | if (allowed != NULL) { |
| 1436 | const char **p; |
| 1437 | int match = 0; |
no test coverage detected