| 1 | // Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer in the |
| 10 | // documentation and/or other materials provided with the distribution. |
| 11 | // * Neither the name of the Andrey N. Sabelnikov nor the |
| 12 | // names of its contributors may be used to endorse or promote products |
| 13 | // derived from this software without specific prior written permission. |
| 14 | // |
| 15 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 16 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 17 | // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 18 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY |
| 19 | // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 20 | // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 21 | // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 22 | // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 23 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 24 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | // |
| 26 | |
| 27 | #include "file_io_utils.h" |
| 28 | |
| 29 | #include <fstream> |
| 30 | #include <boost/filesystem/path.hpp> |
| 31 | #include <boost/filesystem/operations.hpp> |
| 32 | #ifdef _WIN32 |
| 33 | #include <windows.h> |
| 34 | #include "string_tools.h" |
| 35 | #endif |
| 36 | |
| 37 | // On Windows there is a problem with non-ASCII characters in path and file names |
| 38 | // as far as support by the standard components used is concerned: |
| 39 | |
| 40 | // The various file stream classes, e.g. std::ifstream and std::ofstream, are |
| 41 | // part of the GNU C++ Library / libstdc++. On the most basic level they use the |
| 42 | // fopen() call as defined / made accessible to programs compiled within MSYS2 |
| 43 | // by the stdio.h header file maintained by the MinGW project. |
| 44 | |
| 45 | // The critical point: The implementation of fopen() is part of MSVCRT, the |
| 46 | // Microsoft Visual C/C++ Runtime Library, and this method does NOT offer any |
| 47 | // Unicode support. |
| 48 | |
| 49 | // Monero code that would want to continue to use the normal file stream classes |
| 50 | // but WITH Unicode support could therefore not solve this problem on its own, |
| 51 | // but 2 different projects from 2 different maintaining groups would need changes |
| 52 | // in this particular direction - something probably difficult to achieve and |
| 53 | // with a long time to wait until all new versions / releases arrive. |
| 54 | |
| 55 | // Implemented solution approach: Circumvent the problem by stopping to use std |
| 56 | // file stream classes on Windows and directly use Unicode-capable WIN32 API |
| 57 | // calls. Most of the code doing so is concentrated in this header file here. |
| 58 | |
| 59 | namespace epee |
| 60 | { |
| 61 | namespace file_io_utils |
| 62 | { |
| 63 | |
| 64 | bool is_file_exist(const std::string& path) |
| 65 | { |
| 66 | boost::filesystem::path p(path); |
| 67 | return boost::filesystem::exists(p); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | bool save_string_to_file(const std::string& path_to_file, const std::string& str) |
| 72 | { |
| 73 | #ifdef _WIN32 |
| 74 | std::wstring wide_path; |
| 75 | try { wide_path = string_tools::utf8_to_utf16(path_to_file); } catch (...) { return false; } |
| 76 | HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 77 | if (file_handle == INVALID_HANDLE_VALUE) |
| 78 | return false; |
| 79 | DWORD bytes_written; |
| 80 | DWORD bytes_to_write = (DWORD)str.size(); |
| 81 | BOOL result = WriteFile(file_handle, str.data(), bytes_to_write, &bytes_written, NULL); |
| 82 | CloseHandle(file_handle); |
| 83 | if (bytes_written != bytes_to_write) |
| 84 | result = FALSE; |
| 85 | return result; |
| 86 | #else |
| 87 | try |
| 88 | { |
| 89 | std::ofstream fstream; |
| 90 | fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 91 | fstream.open(path_to_file, std::ios_base::binary | std::ios_base::out | std::ios_base::trunc); |
| 92 | fstream << str; |
| 93 | fstream.close(); |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | catch(...) |
| 98 | { |
| 99 | return false; |
| 100 | } |
| 101 | #endif |
| 102 | } |
| 103 | |
| 104 | |
| 105 | bool load_file_to_string(const std::string& path_to_file, std::string& target_str, size_t max_size) |
| 106 | { |
| 107 | #ifdef _WIN32 |
| 108 | std::wstring wide_path; |
| 109 | try { wide_path = string_tools::utf8_to_utf16(path_to_file); } catch (...) { return false; } |
| 110 | HANDLE file_handle = CreateFileW(wide_path.c_str(), GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 111 | if (file_handle == INVALID_HANDLE_VALUE) |
| 112 | return false; |
| 113 | DWORD file_size = GetFileSize(file_handle, NULL); |
| 114 | if ((file_size == INVALID_FILE_SIZE) || (uint64_t)file_size > (uint64_t)max_size) { |
| 115 | CloseHandle(file_handle); |
| 116 | return false; |
| 117 | } |
| 118 | target_str.resize(file_size); |
| 119 | DWORD bytes_read; |
| 120 | BOOL result = ReadFile(file_handle, &target_str[0], file_size, &bytes_read, NULL); |
| 121 | CloseHandle(file_handle); |
| 122 | if (bytes_read != file_size) |
| 123 | result = FALSE; |
| 124 | return result; |
| 125 | #else |
| 126 | try |
| 127 | { |
| 128 | std::ifstream fstream; |
| 129 | fstream.exceptions(std::ifstream::failbit | std::ifstream::badbit); |
| 130 | fstream.open(path_to_file, std::ios_base::binary | std::ios_base::in | std::ios::ate); |
| 131 | |
| 132 | std::ifstream::pos_type file_size = fstream.tellg(); |
| 133 | |
| 134 | if((uint64_t)file_size > (uint64_t)max_size) // ensure a large domain for comparison, and negative -> too large |
| 135 | return false;//don't go crazy |
| 136 | size_t file_size_t = static_cast<size_t>(file_size); |
| 137 | |
| 138 | target_str.resize(file_size_t); |
| 139 | |
| 140 | fstream.seekg (0, std::ios::beg); |
| 141 | fstream.read((char*)target_str.data(), target_str.size()); |
| 142 | fstream.close(); |
| 143 | return true; |
| 144 | } |
| 145 | |
| 146 | catch(...) |
| 147 | { |
| 148 | return false; |
| 149 | } |
| 150 | #endif |
| 151 | } |
| 152 | } |
| 153 | } |