+
+/*-------------------------------------------------
+ core_stdio_fopen - core_file wrapper over fopen
+-------------------------------------------------*/
+static core_file *core_stdio_fopen(char const *path) {
+ core_file *file = malloc(sizeof(core_file));
+ if (!file)
+ return NULL;
+ if (!(file->argp = fopen(path, "rb"))) {
+ free(file);
+ return NULL;
+ }
+ file->fsize = core_stdio_fsize;
+ file->fread = core_stdio_fread;
+ file->fclose = core_stdio_fclose;
+ file->fseek = core_stdio_fseek;
+ return file;
+}
+
+/*-------------------------------------------------
+ core_stdio_fsize - core_file function for
+ getting file size with stdio
+-------------------------------------------------*/
+static UINT64 core_stdio_fsize(core_file *file) {
+#if defined USE_LIBRETRO_VFS
+ #define core_stdio_fseek_impl fseek
+ #define core_stdio_ftell_impl ftell
+#elif defined(__WIN32__) || defined(_WIN32) || defined(WIN32) || defined(__WIN64__)
+ #define core_stdio_fseek_impl _fseeki64
+ #define core_stdio_ftell_impl _ftelli64
+#elif defined(_LARGEFILE_SOURCE) && defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64
+ #define core_stdio_fseek_impl fseeko64
+ #define core_stdio_ftell_impl ftello64
+#elif defined(__PS3__) && !defined(__PSL1GHT__) || defined(__SWITCH__) || defined(__vita__)
+ #define core_stdio_fseek_impl(x,y,z) fseek(x,(off_t)y,z)
+ #define core_stdio_ftell_impl(x) (off_t)ftell(x)
+#else
+ #define core_stdio_fseek_impl fseeko
+ #define core_stdio_ftell_impl ftello
+#endif
+ FILE *fp;
+ UINT64 p, rv;
+ fp = (FILE*)file->argp;
+
+ p = core_stdio_ftell_impl(fp);
+ core_stdio_fseek_impl(fp, 0, SEEK_END);
+ rv = core_stdio_ftell_impl(fp);
+ core_stdio_fseek_impl(fp, p, SEEK_SET);
+ return rv;
+}
+
+/*-------------------------------------------------
+ core_stdio_fread - core_file wrapper over fread
+-------------------------------------------------*/
+static size_t core_stdio_fread(void *ptr, size_t size, size_t nmemb, core_file *file) {
+ return fread(ptr, size, nmemb, (FILE*)file->argp);
+}
+
+/*-------------------------------------------------
+ core_stdio_fclose - core_file wrapper over fclose
+-------------------------------------------------*/
+static int core_stdio_fclose(core_file *file) {
+ int err = fclose((FILE*)file->argp);
+ if (err == 0)
+ free(file);
+ return err;
+}
+
+/*-------------------------------------------------
+ core_stdio_fclose_nonowner - don't call fclose because
+ we don't own the underlying file, but do free the
+ core_file because libchdr did allocate that itself.
+-------------------------------------------------*/
+static int core_stdio_fclose_nonowner(core_file *file) {
+ free(file);
+ return 0;
+}
+
+/*-------------------------------------------------
+ core_stdio_fseek - core_file wrapper over fclose
+-------------------------------------------------*/
+static int core_stdio_fseek(core_file* file, INT64 offset, int whence) {
+ return core_stdio_fseek_impl((FILE*)file->argp, offset, whence);
+}