rxml_document_t *rxml_load_document(const char *path) { #ifndef RXML_TEST RARCH_WARN("Using RXML as drop in for libxml2. Behavior might be very buggy.\n"); #endif char *memory_buffer = NULL; char *new_memory_buffer = NULL; const char *mem_ptr = NULL; long len = 0; RFILE *file = filestream_open(path, RFILE_MODE_READ, -1); if (!file) return NULL; rxml_document_t *doc = (rxml_document_t*)calloc(1, sizeof(*doc)); if (!doc) goto error; filestream_seek(file, 0, SEEK_END); len = filestream_tell(file); filestream_rewind(file); memory_buffer = (char*)malloc(len + 1); if (!memory_buffer) goto error; memory_buffer[len] = '\0'; if (filestream_read(file, memory_buffer, len) != (size_t)len) goto error; filestream_close(file); file = NULL; mem_ptr = memory_buffer; if (!validate_header(&mem_ptr)) goto error; new_memory_buffer = purge_xml_comments(mem_ptr); if (!new_memory_buffer) goto error; free(memory_buffer); mem_ptr = memory_buffer = new_memory_buffer; doc->root_node = rxml_parse_node(&mem_ptr); if (!doc->root_node) goto error; free(memory_buffer); return doc; error: free(memory_buffer); filestream_close(file); rxml_free_document(doc); return NULL; }
/** * filestream_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 filestream_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 = filestream_open(path, RFILE_MODE_READ, -1); if (!file) { fprintf(stderr, "Failed to open %s: %s\n", path, strerror(errno)); goto error; } if (filestream_seek(file, 0, SEEK_END) != 0) goto error; content_buf_size = filestream_tell(file); if (content_buf_size < 0) goto error; filestream_rewind(file); content_buf = malloc(content_buf_size + 1); if (!content_buf) goto error; ret = filestream_read(file, content_buf, content_buf_size); if (ret < 0) { fprintf(stderr, "Failed to read %s: %s\n", path, strerror(errno)); goto error; } filestream_close(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) filestream_close(file); if (content_buf) free(content_buf); if (len) *len = -1; *buf = NULL; return 0; }
int filestream_eof(RFILE *stream) { size_t current_position = filestream_tell(stream); size_t end_position = filestream_seek(stream, 0, SEEK_END); filestream_seek(stream, current_position, SEEK_SET); if (current_position >= end_position) return 1; return 0; }
void filestream_set_size(RFILE *stream) { if (!stream) return; filestream_seek(stream, 0, SEEK_SET); filestream_seek(stream, 0, SEEK_END); stream->size = filestream_tell(stream); filestream_seek(stream, 0, SEEK_SET); }
int intfstream_tell(intfstream_internal_t *intf) { if (!intf) return -1; switch (intf->type) { case INTFSTREAM_FILE: return filestream_tell(intf->file.fp); case INTFSTREAM_MEMORY: return memstream_pos(intf->memory.fp); } return -1; }
int64_t intfstream_tell(intfstream_internal_t *intf) { if (!intf) return -1; switch (intf->type) { case INTFSTREAM_FILE: return (int64_t)filestream_tell(intf->file.fp); case INTFSTREAM_MEMORY: return (int64_t)memstream_pos(intf->memory.fp); case INTFSTREAM_CHD: #ifdef HAVE_CHD return (int64_t)chdstream_tell(intf->chd.fp); #else break; #endif } return -1; }
int64_t rftell(RFILE* stream) { return filestream_tell(stream); }
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 = filestream_open(track_path, RFILE_MODE_READ, -1); if (!fp) return 0; is_mode1 = 0; filestream_seek(fp, 0, SEEK_END); if (!sub_channel_mixed) { if (!(filestream_tell(fp) & 0x7FF)) { unsigned int mode_test = 0; filestream_seek(fp, 0, SEEK_SET); filestream_read(fp, &mode_test, 4); if (mode_test != MODETEST_VAL) is_mode1 = 1; } } skip = is_mode1? 0: 24; frame_size = sub_channel_mixed? 2448: is_mode1? 2048: 2352; filestream_seek(fp, 156 + skip + 16 * frame_size, SEEK_SET); filestream_read(fp, buffer, 6); cd_sector = buffer[2] | (buffer[3] << 8) | (buffer[4] << 16); filestream_seek(fp, skip + cd_sector * frame_size, SEEK_SET); filestream_read(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); filestream_seek(fp, 13 + skip + cd_sector * frame_size, SEEK_SET); filestream_read(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; filestream_close(fp); return 1; }
uint64_t FileStream::tell(void) { if (!fp) return -1; return filestream_tell(fp); }
static int detect_ps1_game_sub(const char *track_path, char *game_id, int sub_channel_mixed) { uint8_t* tmp; uint8_t* boot_file; int skip, frame_size, is_mode1, cd_sector; uint8_t buffer[2048 * 2]; RFILE *fp = filestream_open(track_path, RFILE_MODE_READ, -1); if (!fp) return 0; buffer[0] = '\0'; is_mode1 = 0; filestream_seek(fp, 0, SEEK_END); if (!sub_channel_mixed) { if (!(filestream_tell(fp) & 0x7FF)) { unsigned int mode_test = 0; filestream_seek(fp, 0, SEEK_SET); filestream_read(fp, &mode_test, 4); if (mode_test != MODETEST_VAL) is_mode1 = 1; } } skip = is_mode1? 0: 24; frame_size = sub_channel_mixed? 2448: is_mode1? 2048: 2352; filestream_seek(fp, 156 + skip + 16 * frame_size, SEEK_SET); filestream_read(fp, buffer, 6); cd_sector = buffer[2] | (buffer[3] << 8) | (buffer[4] << 16); filestream_seek(fp, skip + cd_sector * frame_size, SEEK_SET); filestream_read(fp, buffer, 2048 * 2); tmp = buffer; while (tmp < (buffer + 2048 * 2)) { if (!*tmp) goto error; if (!strncasecmp((const char*)(tmp + 33), "SYSTEM.CNF;1", 12)) break; tmp += *tmp; } if(tmp >= (buffer + 2048 * 2)) goto error; cd_sector = tmp[2] | (tmp[3] << 8) | (tmp[4] << 16); filestream_seek(fp, skip + cd_sector * frame_size, SEEK_SET); filestream_read(fp, buffer, 256); buffer[256] = '\0'; tmp = buffer; while(*tmp && strncasecmp((const char*)tmp, "boot", 4)) tmp++; if(!*tmp) goto error; boot_file = tmp; while(*tmp && *tmp != '\n') { if((*tmp == '\\') || (*tmp == ':')) boot_file = tmp + 1; tmp++; } tmp = boot_file; *game_id++ = toupper(*tmp++); *game_id++ = toupper(*tmp++); *game_id++ = toupper(*tmp++); *game_id++ = toupper(*tmp++); *game_id++ = '-'; if(!isalnum(*tmp)) tmp++; while(isalnum(*tmp)) { *game_id++ = *tmp++; if(*tmp == '.') tmp++; } *game_id = 0; filestream_close(fp); return 1; error: filestream_close(fp); return 0; }
long rftell(RFILE* stream) { return filestream_tell(stream); }