Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 31 additions & 5 deletions src/io.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,39 @@ avifIO * avifIOCreateFileReader(const char * filename)
return NULL;
}

fseek(f, 0, SEEK_END);
long fileSize = ftell(f);
if (fileSize < 0) {
#if defined(_WIN32)
// Windows uses _fseeki64 / _ftelli64 for large file support
if (_fseeki64(f, 0, SEEK_END) != 0) {
fclose(f);
return NULL;
}
fseek(f, 0, SEEK_SET);
__int64 fileSizeSigned = _ftelli64(f);
if (fileSizeSigned < 0) {
fclose(f);
return NULL;
}
if (_fseeki64(f, 0, SEEK_SET) != 0) {
fclose(f);
return NULL;
}
uint64_t fileSize = (uint64_t)fileSizeSigned;
#else
// POSIX large file support
if (fseeko(f, 0, SEEK_END) != 0) {
fclose(f);
return NULL;
}
off_t fileSizeSigned = ftello(f);
if (fileSizeSigned < 0) {
fclose(f);
return NULL;
}
if (fseeko(f, 0, SEEK_SET) != 0) {
fclose(f);
return NULL;
}
uint64_t fileSize = (uint64_t)fileSizeSigned;
#endif
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NiDU-NINJA: Thank you for the pull request. We are in the middle of creating the libavif v1.4.0 release. I will review this PR next week.

Some preliminary comments.

  1. It is disappointing that C23 did not address the use of the long type in fseek() and ftell().
  2. I think your PR is correct. Since the fseek() call at line 113 in avifIOFileReaderRead() needs the same treatment, it would be better to create libavif's own wrapper functions for the fseek() and ftell() variants that support large files.
    • We can be POSIX-centric and provide fseeko() and ftello() wrappers for Windows, or
    • we can be neutral and provide avifFseek64() and avifFtell64() for POSIX and Windows.
    • I can take care of the wrappers in a follow-up PR.


avifIOFileReader * reader = (avifIOFileReader *)avifAlloc(sizeof(avifIOFileReader));
if (!reader) {
Expand All @@ -159,7 +185,7 @@ avifIO * avifIOCreateFileReader(const char * filename)
reader->f = f;
reader->io.destroy = avifIOFileReaderDestroy;
reader->io.read = avifIOFileReaderRead;
reader->io.sizeHint = (uint64_t)fileSize;
reader->io.sizeHint = fileSize;
reader->io.persistent = AVIF_FALSE;
if (avifRWDataRealloc(&reader->buffer, 1024) != AVIF_RESULT_OK) {
avifFree(reader);
Expand Down
Loading