int find_first_data_track(const char *cue_path, int32_t *offset, char *track_path, size_t max_len) { int rv, m, s, f; char tmp_token[MAX_TOKEN_LEN]; char cue_dir[PATH_MAX_LENGTH]; RFILE *fd; strlcpy(cue_dir, cue_path, PATH_MAX_LENGTH); path_basedir(cue_dir); fd = retro_fopen(cue_path, RFILE_MODE_READ, -1); if (!fd) { RARCH_LOG("Could not open CUE file '%s': %s\n", cue_path, strerror(errno)); return -errno; } RARCH_LOG("Parsing CUE file '%s'...\n", cue_path); while (get_token(fd, tmp_token, MAX_TOKEN_LEN) > 0) { if (strcmp(tmp_token, "FILE") == 0) { get_token(fd, tmp_token, MAX_TOKEN_LEN); fill_pathname_join(track_path, cue_dir, tmp_token, max_len); } else if (strcasecmp(tmp_token, "TRACK") == 0) { get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, MAX_TOKEN_LEN); if (strcasecmp(tmp_token, "AUDIO") == 0) continue; find_token(fd, "INDEX"); get_token(fd, tmp_token, MAX_TOKEN_LEN); get_token(fd, tmp_token, MAX_TOKEN_LEN); if (sscanf(tmp_token, "%02d:%02d:%02d", &m, &s, &f) < 3) { RARCH_LOG("Error parsing time stamp '%s'\n", tmp_token); return -errno; } *offset = ((m * 60) * (s * 75) * f) * 25; RARCH_LOG("Found 1st data track on file '%s+%d'\n", track_path, *offset); rv = 0; goto clean; } } rv = -EINVAL; clean: retro_fclose(fd); return rv; }
static int create_db(lua_State *L) { int rv; RFILE *dst; const char *db_file = luaL_checkstring(L, -2); if (!lua_isfunction(L, -1)) { lua_pushstring(L, "second argument must be a function"); lua_error(L); } lua_setfield(L, LUA_REGISTRYINDEX, "testlib_get_value"); dst = retro_fopen(db_file, RFILE_MODE_WRITE, -1); if (!dst) { lua_pushstring(L, "Could not open destination file"); lua_error(L); } rv = libretrodb_create(dst, &value_provider, L); retro_fclose(dst); return 0; }
/** * retro_read_file: * @path : path to file. * @buf : buffer to allocate and read the contents of the * file into. Needs to be freed manually. * * Read the contents of a file into @buf. * * Returns: number of items read, -1 on error. */ int retro_read_file(const char *path, void **buf, ssize_t *len) { ssize_t ret = 0; ssize_t content_buf_size = 0; void *content_buf = NULL; RFILE *file = retro_fopen(path, RFILE_MODE_READ, -1); if (!file) goto error; if (retro_fseek(file, 0, SEEK_END) != 0) goto error; content_buf_size = retro_ftell(file); if (content_buf_size < 0) goto error; retro_frewind(file); content_buf = malloc(content_buf_size + 1); if (!content_buf) goto error; if ((ret = retro_fread(file, content_buf, content_buf_size)) < content_buf_size) { #ifdef RARCH_INTERNAL RARCH_WARN("Didn't read whole file: %s.\n", path); #else printf("Didn't read whole file: %s.\n", path); #endif } if (!content_buf) goto error; *buf = content_buf; /* Allow for easy reading of strings to be safe. * Will only work with sane character formatting (Unix). */ ((char*)content_buf)[content_buf_size] = '\0'; if (retro_fclose(file) != 0) printf("Failed to close file stream.\n"); if (len) *len = ret; return 1; error: retro_fclose(file); if (content_buf) free(content_buf); if (len) *len = -1; *buf = NULL; return 0; }
/** * menu_content_playlist_load: * @playlist : Playlist handle. * @idx : Index in playlist. * * Initializes core and loads content based on playlist entry. **/ void menu_content_playlist_load(void *data, unsigned idx) { const char *core_path = NULL; const char *path = NULL; content_playlist_t *playlist = (content_playlist_t*)data; if (!playlist) return; content_playlist_get_index(playlist, idx, &path, NULL, &core_path, NULL, NULL, NULL); if (path && !string_is_empty(path)) { unsigned i; RFILE *fp = NULL; char *path_check = NULL; char *path_tolower = strdup(path); for (i = 0; i < strlen(path_tolower); ++i) path_tolower[i] = tolower(path_tolower[i]); if (strstr(path_tolower, ".zip")) strstr(path_tolower, ".zip")[4] = '\0'; else if (strstr(path_tolower, ".7z")) strstr(path_tolower, ".7z")[3] = '\0'; path_check = (char *)calloc(strlen(path_tolower) + 1, sizeof(char)); strncpy(path_check, path, strlen(path_tolower)); fp = retro_fopen(path_check, RFILE_MODE_READ, -1); if (!fp) { runloop_msg_queue_push("File could not be loaded.\n", 1, 100, true); RARCH_LOG("File at %s failed to load.\n", path_check); free(path_tolower); free(path_check); return; } retro_fclose(fp); free(path_tolower); free(path_check); } runloop_ctl(RUNLOOP_CTL_SET_LIBRETRO_PATH, (void*)core_path); if (path) menu_driver_ctl(RARCH_MENU_CTL_UNSET_LOAD_NO_CONTENT, NULL); else menu_driver_ctl(RARCH_MENU_CTL_SET_LOAD_NO_CONTENT, NULL); rarch_environment_cb(RETRO_ENVIRONMENT_EXEC, (void*)path); event_cmd_ctl(EVENT_CMD_LOAD_CORE, NULL); }
int detect_system(const char *track_path, int32_t offset, const char **system_name) { int rv; char magic[MAGIC_LEN]; int i; RFILE *fd = retro_fopen(track_path, RFILE_MODE_READ, -1); if (!fd) { RARCH_LOG("Could not open data track of file '%s': %s\n", track_path, strerror(errno)); rv = -errno; goto clean; } retro_fseek(fd, offset, SEEK_SET); if (retro_fread(fd, magic, MAGIC_LEN) < MAGIC_LEN) { RARCH_LOG("Could not read data from file '%s' at offset %d: %s\n", track_path, offset, strerror(errno)); rv = -errno; goto clean; } RARCH_LOG("Comparing with known magic numbers...\n"); for (i = 0; MAGIC_NUMBERS[i].system_name != NULL; i++) { if (memcmp(MAGIC_NUMBERS[i].magic, magic, MAGIC_LEN) == 0) { *system_name = MAGIC_NUMBERS[i].system_name; rv = 0; goto clean; } } retro_fseek(fd, 0x8008, SEEK_SET); if (retro_fread(fd, magic, 8) > 0) { magic[8] = '\0'; if (!strcmp(magic, "PSP GAME")) { *system_name = "psp\0"; rv = 0; goto clean; } } RARCH_LOG("Could not find compatible system\n"); rv = -EINVAL; clean: retro_fclose(fd); return rv; }
/** * retro_write_file: * @path : path to file. * @data : contents to write to the file. * @size : size of the contents. * * Writes data to a file. * * Returns: true (1) on success, false (0) otherwise. */ bool retro_write_file(const char *path, const void *data, ssize_t size) { ssize_t ret = 0; RFILE *file = retro_fopen(path, RFILE_MODE_WRITE, -1); if (!file) return false; ret = retro_fwrite(file, data, size); retro_fclose(file); return (ret == size); }
int detect_ps1_game(const char *track_path, char *game_id) { bool rv = false; unsigned pos; RFILE *fd = retro_fopen(track_path, RFILE_MODE_READ, -1); if (!fd) { RARCH_LOG("Could not open data track: %s\n", strerror(errno)); return -errno; } for (pos = 0; pos < 100000; pos++) { retro_fseek(fd, pos, SEEK_SET); if (retro_fread(fd, game_id, 5) > 0) { game_id[5] = '\0'; if (!strcmp(game_id, "SLUS_") || !strcmp(game_id, "SCUS_") || !strcmp(game_id, "SLES_") || !strcmp(game_id, "SCES_") || !strcmp(game_id, "SCED_") || !strcmp(game_id, "SLPS_") || !strcmp(game_id, "SCPS_") || !strcmp(game_id, "SLPM_") ) { retro_fseek(fd, pos, SEEK_SET); if (retro_fread(fd, game_id, 11) > 0) { game_id[4] = '-'; game_id[8] = game_id[9]; game_id[9] = game_id[10]; game_id[10] = '\0'; rv = true; } break; } } else break; } retro_fclose(fd); return rv; }
int main(void) { struct stub_state state; RFILE *fd = retro_fopen("test.msgpack", RFILE_MODE_READ, 0); state.i = 0; state.stack[0] = 0; rmsgpack_read(fd, &stub_callbacks, &state); printf("Test succeeded.\n"); retro_fclose(fd); return 0; }
static bool gfx_ctx_mali_fbdev_set_video_mode(void *data, unsigned width, unsigned height, bool fullscreen) { struct fb_var_screeninfo vinfo; static const EGLint attribs[] = { EGL_CONTEXT_CLIENT_VERSION, 2, /* Use version 2, even for GLES3. */ EGL_NONE }; mali_ctx_data_t *mali = (mali_ctx_data_t*)data; RFILE *fd = retro_fopen("/dev/fb0", RFILE_MODE_READ_WRITE, -1); int fb = retro_get_fd(fd); if (ioctl(fb, FBIOGET_VSCREENINFO, &vinfo) < 0) { RARCH_ERR("Error obtaining framebuffer info.\n"); goto error; } retro_fclose(fd); width = vinfo.xres; height = vinfo.yres; mali->width = width; mali->height = height; mali->native_window.width = vinfo.xres; mali->native_window.height = vinfo.yres; if (!egl_create_context(mali, attribs)) { egl_report_error(); goto error; } if (!egl_create_surface(mali, &mali->native_window)) goto error; return true; error: if (fd) retro_fclose(fd); RARCH_ERR("[Mali fbdev]: EGL error: %d.\n", eglGetError()); gfx_ctx_mali_fbdev_destroy(data); return false; }
int sha1_calculate(const char *path, char *result) { unsigned char buff[4096] = {0}; SHA1Context sha; int rv = 1; RFILE *fd = retro_fopen(path, RFILE_MODE_READ, -1); if (!fd) goto error; SHA1Reset(&sha); do { rv = retro_fread(fd, buff, 4096); if (rv < 0) goto error; SHA1Input(&sha, buff, rv); }while(rv); if (!SHA1Result(&sha)) goto error; sprintf(result, "%08X%08X%08X%08X%08X", sha.Message_Digest[0], sha.Message_Digest[1], sha.Message_Digest[2], sha.Message_Digest[3], sha.Message_Digest[4]); retro_fclose(fd); return 0; error: if (fd) retro_fclose(fd); return -1; }
static void gfx_ctx_mali_fbdev_destroy(void *data) { int fb; RFILE *fd; mali_ctx_data_t *mali = (mali_ctx_data_t*)data; if (mali) { egl_destroy(data); mali->resize = false; free(mali); } /* Clear framebuffer and set cursor on again */ fd = retro_fopen("/dev/tty", RFILE_MODE_READ_WRITE, -1); fb = retro_get_fd(fd); ioctl(fb, VT_ACTIVATE,5); ioctl(fb, VT_ACTIVATE,1); retro_fclose(fd); system("setterm -cursor on"); }
static int detect_ps1_game_sub(const char *track_path, char *game_id, int sub_channel_mixed) { uint8_t* tmp; uint8_t buffer[2048 * 2]; int skip, frame_size, is_mode1, cd_sector; RFILE *fp = retro_fopen(track_path, RFILE_MODE_READ, -1); if (!fp) return 0; is_mode1 = 0; retro_fseek(fp, 0, SEEK_END); if (!sub_channel_mixed) { if (!(retro_ftell(fp) & 0x7FF)) { unsigned int mode_test = 0; retro_fseek(fp, 0, SEEK_SET); retro_fread(fp, &mode_test, 4); #if defined(MSB_FIRST) if (mode_test != 0x00ffffff) #else if (mode_test != 0xffffff00) #endif is_mode1 = 1; } } skip = is_mode1? 0: 24; frame_size = sub_channel_mixed? 2448: is_mode1? 2048: 2352; retro_fseek(fp, 156 + skip + 16 * frame_size, SEEK_SET); retro_fread(fp, buffer, 6); cd_sector = buffer[2] | (buffer[3] << 8) | (buffer[4] << 16); retro_fseek(fp, skip + cd_sector * frame_size, SEEK_SET); retro_fread(fp, buffer, 2048 * 2); tmp = buffer; while (tmp < (buffer + 2048 * 2)) { if (!*tmp) return 0; if (!strncasecmp((const char*)(tmp + 33), "SYSTEM.CNF;1", 12)) break; tmp += *tmp; } if(tmp >= (buffer + 2048 * 2)) return 0; cd_sector = tmp[2] | (tmp[3] << 8) | (tmp[4] << 16); retro_fseek(fp, 13 + skip + cd_sector * frame_size, SEEK_SET); retro_fread(fp, buffer, 256); tmp = (uint8_t*)strrchr((const char*)buffer, '\\'); if(!tmp) tmp = buffer; else tmp++; *game_id++ = toupper(*tmp++); *game_id++ = toupper(*tmp++); *game_id++ = toupper(*tmp++); *game_id++ = toupper(*tmp++); *game_id++ = '-'; tmp++; *game_id++ = *tmp++; *game_id++ = *tmp++; *game_id++ = *tmp++; tmp++; *game_id++ = *tmp++; *game_id++ = *tmp++; *game_id = 0; retro_fclose(fp); return 1; }
/** * menu_content_load_from_playlist: * @playlist : Playlist handle. * @idx : Index in playlist. * * Initializes core and loads content based on playlist entry. **/ static bool menu_content_load_from_playlist(void *data) { unsigned idx; const char *core_path = NULL; const char *path = NULL; menu_content_ctx_playlist_info_t *info = (menu_content_ctx_playlist_info_t *)data; content_playlist_t *playlist = NULL; if (!info) return false; playlist = (content_playlist_t*)info->data; idx = info->idx; if (!playlist) return false; content_playlist_get_index(playlist, idx, &path, NULL, &core_path, NULL, NULL, NULL); if (path && !string_is_empty(path)) { unsigned i; RFILE *fp = NULL; char *path_check = NULL; char *path_tolower = strdup(path); for (i = 0; i < strlen(path_tolower); ++i) path_tolower[i] = tolower(path_tolower[i]); if (strstr(path_tolower, ".zip")) strstr(path_tolower, ".zip")[4] = '\0'; else if (strstr(path_tolower, ".7z")) strstr(path_tolower, ".7z")[3] = '\0'; path_check = (char *) calloc(strlen(path_tolower) + 1, sizeof(char)); strncpy(path_check, path, strlen(path_tolower)); free(path_tolower); fp = retro_fopen(path_check, RFILE_MODE_READ, -1); free(path_check); if (!fp) goto error; retro_fclose(fp); } runloop_ctl(RUNLOOP_CTL_SET_LIBRETRO_PATH, (void*)core_path); if (path) menu_driver_ctl(RARCH_MENU_CTL_UNSET_LOAD_NO_CONTENT, NULL); else menu_driver_ctl(RARCH_MENU_CTL_SET_LOAD_NO_CONTENT, NULL); if (!event_cmd_ctl(EVENT_CMD_EXEC, (void*)path)) return false; event_cmd_ctl(EVENT_CMD_LOAD_CORE, NULL); return true; error: runloop_msg_queue_push("File could not be loaded.\n", 1, 100, true); return false; }
int detect_psp_game(const char *track_path, char *game_id) { bool rv = false; unsigned pos; RFILE *fd = retro_fopen(track_path, RFILE_MODE_READ, -1); if (!fd) { RARCH_LOG("Could not open data track: %s\n", strerror(errno)); return -errno; } for (pos = 0; pos < 100000; pos++) { retro_fseek(fd, pos, SEEK_SET); if (retro_fread(fd, game_id, 5) > 0) { game_id[5] = '\0'; if (!strcmp(game_id, "ULES-") || !strcmp(game_id, "ULUS-") || !strcmp(game_id, "ULJS-") || !strcmp(game_id, "ULEM-") || !strcmp(game_id, "ULUM-") || !strcmp(game_id, "ULJM-") || !strcmp(game_id, "UCES-") || !strcmp(game_id, "UCUS-") || !strcmp(game_id, "UCJS-") || !strcmp(game_id, "UCAS-") || !strcmp(game_id, "NPEH-") || !strcmp(game_id, "NPUH-") || !strcmp(game_id, "NPJH-") || !strcmp(game_id, "NPEG-") || !strcmp(game_id, "NPUG-") || !strcmp(game_id, "NPJG-") || !strcmp(game_id, "NPHG-") || !strcmp(game_id, "NPEZ-") || !strcmp(game_id, "NPUZ-") || !strcmp(game_id, "NPJZ-") ) { retro_fseek(fd, pos, SEEK_SET); if (retro_fread(fd, game_id, 10) > 0) { #if 0 game_id[4] = '-'; game_id[8] = game_id[9]; game_id[9] = game_id[10]; #endif game_id[10] = '\0'; rv = true; } break; } } else break; } retro_fclose(fd); return rv; }
/** * retro_read_file: * @path : path to file. * @buf : buffer to allocate and read the contents of the * file into. Needs to be freed manually. * * Read the contents of a file into @buf. * * Returns: number of items read, -1 on error. */ int retro_read_file(const char *path, void **buf, ssize_t *len) { ssize_t ret = 0; ssize_t content_buf_size = 0; void *content_buf = NULL; RFILE *file = retro_fopen(path, RFILE_MODE_READ, -1); if (!file) { #if __STDC_VERSION__ >= 199901L fprintf(stderr, "%s: Failed to open %s: %s\n", __FUNCTION__, path, strerror(errno)); #else fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno)); #endif goto error; } if (retro_fseek(file, 0, SEEK_END) != 0) goto error; content_buf_size = retro_ftell(file); if (content_buf_size < 0) goto error; retro_frewind(file); content_buf = malloc(content_buf_size + 1); if (!content_buf) goto error; ret = retro_fread(file, content_buf, content_buf_size); if (ret < 0) { #if __STDC_VERSION__ >= 199901L fprintf(stderr, "%s: Failed to read %s: %s\n", __FUNCTION__, path, strerror(errno)); #else fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno)); #endif goto error; } retro_fclose(file); *buf = content_buf; /* Allow for easy reading of strings to be safe. * Will only work with sane character formatting (Unix). */ ((char*)content_buf)[content_buf_size] = '\0'; if (len) *len = ret; return 1; error: if (file) retro_fclose(file); if (content_buf) free(content_buf); if (len) *len = -1; *buf = NULL; return 0; }
static void *gfx_ctx_drm_init(void *video_driver) { int fd, i; unsigned monitor_index; unsigned gpu_index = 0; const char *gpu = NULL; struct string_list *gpu_descriptors = NULL; gfx_ctx_drm_data_t *drm = (gfx_ctx_drm_data_t*) calloc(1, sizeof(gfx_ctx_drm_data_t)); if (!drm) return NULL; fd = -1; gpu_descriptors = dir_list_new("/dev/dri", NULL, false, false); nextgpu: drm_restore_crtc(); free_drm_resources(drm); if (!gpu_descriptors || gpu_index == gpu_descriptors->size) { RARCH_ERR("[KMS]: Couldn't find a suitable DRM device.\n"); goto error; } gpu = gpu_descriptors->elems[gpu_index++].data; drm->drm = retro_fopen(gpu, RFILE_MODE_READ_WRITE, -1); if (!drm->drm) { RARCH_WARN("[KMS]: Couldn't open DRM device.\n"); goto nextgpu; } fd = retro_get_fd(drm->drm); if (!drm_get_resources(fd)) goto nextgpu; if (!drm_get_connector(fd)) goto nextgpu; if (!drm_get_encoder(fd)) goto nextgpu; drm_setup(fd); /* First mode is assumed to be the "optimal" * one for get_video_size() purposes. */ drm->fb_width = g_drm_connector->modes[0].hdisplay; drm->fb_height = g_drm_connector->modes[0].vdisplay; g_gbm_dev = gbm_create_device(fd); if (!g_gbm_dev) { RARCH_WARN("[KMS]: Couldn't create GBM device.\n"); goto nextgpu; } dir_list_free(gpu_descriptors); /* Setup the flip handler. */ g_drm_fds.fd = fd; g_drm_fds.events = POLLIN; g_drm_evctx.version = DRM_EVENT_CONTEXT_VERSION; g_drm_evctx.page_flip_handler = drm_flip_handler; g_drm_fd = fd; return drm; error: dir_list_free(gpu_descriptors); gfx_ctx_drm_destroy_resources(drm); if (drm) free(drm); return NULL; }