fix windows pread and pwrite bug

reading without a byte offset specified can affect absolute reads afterward, this is a workaround (thanks windows...)
This commit is contained in:
Kae 2024-09-12 23:06:13 +10:00
parent 7852ad9cf2
commit e8d59f9c2b
2 changed files with 13 additions and 4 deletions

View File

@ -377,7 +377,12 @@ size_t File::pread(void* f, char* data, size_t len, StreamOffset position) {
HANDLE file = (HANDLE)f;
DWORD numRead = 0;
OVERLAPPED overlapped = makeOverlapped(position);
StreamOffset pos = ftell(f);
if (pos != 0) fseek(f, 0, IOSeek::Absolute);
int ret = ReadFile(file, data, len, &numRead, &overlapped);
if (pos != 0) fseek(f, pos, IOSeek::Absolute);
if (ret == 0) {
auto err = GetLastError();
if (err != ERROR_IO_PENDING)
@ -391,7 +396,12 @@ size_t File::pwrite(void* f, char const* data, size_t len, StreamOffset position
HANDLE file = (HANDLE)f;
DWORD numWritten = 0;
OVERLAPPED overlapped = makeOverlapped(position);
StreamOffset pos = ftell(f);
if (pos != 0) fseek(f, 0, IOSeek::Absolute);
int ret = WriteFile(file, data, len, &numWritten, &overlapped);
if (pos != 0) fseek(f, pos, IOSeek::Absolute);
if (ret == 0) {
auto err = GetLastError();
if (err != ERROR_IO_PENDING)

View File

@ -18,13 +18,12 @@ void readPngData(png_structp pngPtr, png_bytep data, png_size_t length) {
};
bool Image::isPng(IODevicePtr device) {
png_byte header[8];
device->readAbsolute(0, (char*)header, sizeof(header));
return !png_sig_cmp(header, 0, sizeof(header));
png_byte header[8]{};
return !png_sig_cmp(header, 0, device->readAbsolute(0, (char*)header, sizeof(header)));
}
Image Image::readPng(IODevicePtr device) {
png_byte header[8];
png_byte header[8]{};
device->readFull((char*)header, sizeof(header));
if (png_sig_cmp(header, 0, sizeof(header)))