* @brief Load file */
| 266 | * @brief Load file |
| 267 | */ |
| 268 | HRESULT CHexMergeView::LoadFile(const tchar_t* path) |
| 269 | { |
| 270 | CHexMergeDoc *pDoc = static_cast<CHexMergeDoc *>(GetDocument()); |
| 271 | String strTempFileName = path; |
| 272 | if (!pDoc->GetUnpacker()->Unpacking(m_nThisPane, &m_unpackerSubcodes, strTempFileName, path, { strTempFileName })) |
| 273 | return E_FAIL; |
| 274 | HANDLE h = CreateFile(strTempFileName.c_str(), GENERIC_READ, |
| 275 | FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 276 | 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); |
| 277 | HRESULT hr = SE(h != INVALID_HANDLE_VALUE); |
| 278 | if (h == INVALID_HANDLE_VALUE) |
| 279 | return hr; |
| 280 | LARGE_INTEGER length64{}; |
| 281 | hr = SE(GetFileSizeEx(h, &length64)); |
| 282 | if (sizeof(size_t) == 4 && length64.HighPart > 0) |
| 283 | hr = E_OUTOFMEMORY; |
| 284 | size_t length = static_cast<size_t>(length64.QuadPart); |
| 285 | if (hr == S_OK) |
| 286 | { |
| 287 | if (void *buffer = GetBuffer(length)) |
| 288 | { |
| 289 | size_t pos = 0; |
| 290 | while (pos < length && SUCCEEDED(hr)) |
| 291 | { |
| 292 | DWORD cb = 0; |
| 293 | hr = SE(ReadFile(h, reinterpret_cast<BYTE *>(buffer) + pos, |
| 294 | (length - pos) < 0x10000000 ? static_cast<DWORD>(length - pos) : 0x10000000, |
| 295 | &cb, 0)); |
| 296 | if (cb == 0) |
| 297 | break; |
| 298 | pos += cb; |
| 299 | } |
| 300 | if (hr != S_OK) |
| 301 | GetBuffer(0); |
| 302 | } |
| 303 | else if (length != 0) |
| 304 | { |
| 305 | hr = E_OUTOFMEMORY; |
| 306 | } |
| 307 | } |
| 308 | CloseHandle(h); |
| 309 | m_fileInfo.Update(path); |
| 310 | return hr; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * @brief Save file |