* xmlFileOpenSafe: * @filename: the URI for matching * @out: pointer to resulting context * * input from FILE * * * Returns an I/O context or NULL in case of error */
| 679 | * Returns an I/O context or NULL in case of error |
| 680 | */ |
| 681 | static int |
| 682 | xmlFileOpenSafe(const char *filename, int write, void **out) { |
| 683 | char *fromUri = NULL; |
| 684 | FILE *fd; |
| 685 | int ret = XML_ERR_OK; |
| 686 | |
| 687 | *out = NULL; |
| 688 | if (filename == NULL) |
| 689 | return(XML_ERR_ARGUMENT); |
| 690 | |
| 691 | if (xmlConvertUriToPath(filename, &fromUri) < 0) |
| 692 | return(XML_ERR_NO_MEMORY); |
| 693 | |
| 694 | if (fromUri != NULL) |
| 695 | filename = fromUri; |
| 696 | |
| 697 | #if defined(_WIN32) |
| 698 | { |
| 699 | wchar_t *wpath; |
| 700 | |
| 701 | wpath = __xmlIOWin32UTF8ToWChar(filename); |
| 702 | if (wpath == NULL) { |
| 703 | xmlFree(fromUri); |
| 704 | return(XML_ERR_NO_MEMORY); |
| 705 | } |
| 706 | fd = _wfopen(wpath, write ? L"wb" : L"rb"); |
| 707 | xmlFree(wpath); |
| 708 | } |
| 709 | #else |
| 710 | fd = fopen(filename, write ? "wb" : "rb"); |
| 711 | #endif /* WIN32 */ |
| 712 | |
| 713 | if (fd == NULL) { |
| 714 | /* |
| 715 | * Windows and possibly other platforms return EINVAL |
| 716 | * for invalid filenames. |
| 717 | */ |
| 718 | if ((errno == ENOENT) || (errno == EINVAL)) { |
| 719 | ret = XML_IO_ENOENT; |
| 720 | } else { |
| 721 | /* |
| 722 | * This error won't be forwarded to the parser context |
| 723 | * which will report it a second time. |
| 724 | */ |
| 725 | ret = xmlIOErr(0, filename); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | *out = fd; |
| 730 | xmlFree(fromUri); |
| 731 | return(ret); |
| 732 | } |
| 733 | |
| 734 | /** |
| 735 | * xmlFileOpen: |
no test coverage detected