int64_t File::seek(int64_t offset, int whence) { if(!is_multipart) return seek_file(fd, offset, whence); index = (int)(offset / part_size); return seek_file(fp[index], (offset % part_size), whence); }
static int parse_param_sfo(file_t fd, const char *field, char *title_name) { if (FD_OK(fd)) { unsigned len, pos, str; unsigned char *mem = NULL; len = seek_file(fd, 0, SEEK_END); mem = (unsigned char *) malloc(len + 16); if (!mem) { close_file(fd); return -2; } memset(mem, 0, len + 16); seek_file(fd, 0, SEEK_SET); read_file(fd, mem, len); close_file(fd); str = (mem[8] + (mem[9] << 8)); pos = (mem[0xc] + (mem[0xd] << 8)); int indx = 0; while (str < len) { if (mem[str] == 0) break; if (!strcmp((char *) &mem[str], field)) { strncpy(title_name, (char *) &mem[pos], 63); free(mem); return 0; } while (mem[str]) str++; str++; pos += (mem[0x1c + indx] + (mem[0x1d + indx] << 8)); indx += 16; } if (mem) free(mem); } return -1; }
int zip_load (zip *z, const char *name, char *buf) { struct zipfile *zfile; zfile = find_file (z, name); if (!zfile) return ZIP_NOFILE; seek_file (z, zfile); if (zfile->zip_size < zfile->real_size) { char *zip_buf; z_stream zs; zs.zalloc = NULL; zs.zfree = NULL; zs.opaque = NULL; zip_buf = malloc (zfile->zip_size); fread (zip_buf, zfile->zip_size, 1, z->f); zs.next_in = zip_buf; zs.avail_in = zfile->zip_size; zs.next_out = buf; zs.avail_out = zfile->real_size; inflateInit2 (&zs, -MAX_WBITS); inflate (&zs, Z_FINISH); inflateEnd (&zs); free (zip_buf); } else { fread (buf, zfile->real_size, 1, z->f); } return ZIP_OK; }
void image_send_start(char *imageName) { currentState = IMAGE_SENDING; FIL file; uint16_t imageBuffer[BUFFSIZE/2]; uint16_t bytesRead; // Open image open_file(&file, imageName); while (currentState == IMAGE_SENDING) { read_file(&file, imageBuffer, BUFFSIZE, &bytesRead); if (bytesRead == 0) { // Seek file back to start or to next frame seek_file(&file, 0); } else { // Send image buffer over EBI ebi_write_buffer(EBI_IMAGE_STREAM_START, imageBuffer, bytesRead/2); } } // Finished sending image, close file close_file(&file); currentState = IMAGE_IDLE; }
/* Parse / process non-empty the LINE from an strace output. */ static void parse_line(svn_stringbuf_t *line) { /* determine function name, first parameter and return value */ char *func_end = strchr(line->data, '('); char *return_value = strrchr(line->data, ' '); char *first_param_end; apr_int64_t func_return = 0; char *func_start = strchr(line->data, ' '); if (func_end == NULL || return_value == NULL) return; if (func_start == NULL || func_start > func_end) func_start = line->data; else while(*func_start == ' ') func_start++; first_param_end = strchr(func_end, ','); if (first_param_end == NULL) first_param_end = strchr(func_end, ')'); if (first_param_end == NULL) return; *func_end++ = 0; *first_param_end = 0; ++return_value; /* (try to) convert the return value into an integer. * If that fails, continue anyway as defaulting to 0 will be safe for us. */ svn_error_clear(svn_cstring_atoi64(&func_return, return_value)); /* process those operations that we care about */ if (strcmp(func_start, "open") == 0) { /* remove double quotes from file name parameter */ *func_end++ = 0; *--first_param_end = 0; open_file(func_end, (int)func_return); } else if (strcmp(func_start, "read") == 0) read_file(atoi(func_end), func_return); else if (strcmp(func_start, "lseek") == 0) seek_file(atoi(func_end), func_return); else if (strcmp(func_start, "close") == 0) close_file(atoi(func_end)); }
int main() { BLOCK_DEVICE *device; FSFILE *file; unsigned char i; unsigned long filepos; /* avoid issues with the interrupt routine hijacking the IY register - not sure how big a problem this really is, but can't hurt... */ #asm di #endasm buffer_emptybuffers(); // TODO: Can this be sensibly incorporated into fatfs_init? printf("Initializing fatfs library\n"); fatfs_init(); printf("opening DivIDE master drive\n"); device = divide_open_drive(DIVIDE_DRIVE_MASTER); printf("opening FATfs filesystem on it\n"); fatfs_fsopen(device, &fs); printf("fatfs filesystem at %04x\n", &fs); open_root_dir(&fs, &dir); printf("dir handle at %04x\n", &dir); for (i = 0; i < 13; i++) { read_dir(&dir, &entry); /* printf("%s\n", entry.filename); */ } printf("dirent at %04x\n", &entry); file = open_dirent(&entry, FILE_MODE_EXC_READ); printf("file handle at %04x\n", file); read_file(file, buffer1, 5); printf("first 5 chars stored at %04x\n", buffer1); seek_file(file, 2L); read_file(file, buffer2, 5); printf("chars 2-6 stored at %04x\n", buffer2); i = read_byte(file); printf("char 7 is %02x\n", i); filepos = get_file_pos(file); printf("file position is now %08lx\n", filepos); close_file(file); #asm ei #endasm return 0; }
iks * zip_load_xml (zip *z, const char *name, int *err) { iksparser *prs; char *real_buf; iks *x; struct zipfile *zfile; *err = ZIP_OK; zfile = find_file (z, name); if (!zfile) { *err = ZIP_NOFILE; return NULL; } seek_file (z, zfile); real_buf = malloc (zfile->real_size + 1); if (zfile->zip_size < zfile->real_size) { char *zip_buf; z_stream zs; zs.zalloc = NULL; zs.zfree = NULL; zs.opaque = NULL; zip_buf = malloc (zfile->zip_size); fread (zip_buf, zfile->zip_size, 1, z->f); zs.next_in = zip_buf; zs.avail_in = zfile->zip_size; zs.next_out = real_buf; zs.avail_out = zfile->real_size; inflateInit2 (&zs, -MAX_WBITS); inflate (&zs, Z_FINISH); inflateEnd (&zs); free (zip_buf); } else { fread (real_buf, zfile->real_size, 1, z->f); } real_buf[zfile->real_size] = '\0'; prs = iks_dom_new (&x); iks_parse (prs, real_buf, zfile->real_size, 1); iks_parser_delete (prs); free (real_buf); return x; }
ssize_t write_file(files_struct * fsp, char *data, SMB_OFF_T pos, size_t n) { if (!fsp->can_write) { errno = EPERM; return (0); } if (!fsp->modified) { fsp->modified = True; } if ((pos != -1) && (seek_file(fsp, pos) == -1)) return -1; return vfs_write_data(fsp, data, n); }
int main(int argc, char *argv[]) { oss_media_init(AOS_LOG_INFO); if (argc < 2) { usage(); return -1; } // example of oss media file functions if (strcmp("write", argv[1]) == 0) { write_file(); } else if (strcmp("append", argv[1]) == 0) { append_file(); } else if (strcmp("read", argv[1]) == 0) { read_file(); } else if (strcmp("seek", argv[1]) == 0) { seek_file(); } else if (strcmp("error_code", argv[1]) ==0) { error_code(); } else if (strcmp("idr", argv[1]) == 0) { if (argc < 3) { usage(); return -1; } idr(argv[2]); } else if (strcmp("perf", argv[1]) == 0) { int loop = (argc == 3) ? atoi(argv[2]) : 1000; perf(loop); } else if (strcmp("app", argv[1]) == 0) { if (argc < 3) { usage(); return -1; } camera_app(argv[2]); } else { printf("Unsupport operation:%s\n", argv[1]); usage(); } oss_media_destroy(); return 0; }
ssize_t read_file(files_struct * fsp, char *data, SMB_OFF_T pos, size_t n) { ssize_t ret = 0, readret; if (seek_file(fsp, pos) == -1) { DEBUG(3, ("read_file: Failed to seek to %.0f\n", (double)pos)); return (ret); } if (n > 0) { readret = fsp->conn->vfs_ops.read(fsp->fd, data, n); if (readret == -1) return -1; if (readret > 0) ret += readret; } return (ret); }
ssize_t VIsoFile::read(void *buf, size_t nbyte) { DirList *dirList; uint64_t remaining, to_read; uint64_t r; uint8_t *p; if (!fsBuf) return -1; remaining = nbyte; r = 0; p = (uint8_t *)buf; if (vFilePtr >= totalSize || remaining == 0) { return 0; } else if (vFilePtr < 0) { return -1; } if (vFilePtr < fsBufSize) { // Read FS structure from RAM to_read = MIN(fsBufSize-vFilePtr, remaining); memcpy(p, fsBuf+vFilePtr, to_read); remaining -= to_read; r += to_read; p += to_read; vFilePtr += to_read; } if (remaining == 0 || vFilePtr >= totalSize) return r; if (vFilePtr < padAreaStart) { // Read from file(s) dirList = rootList; while (dirList) { FileList *fileList = dirList->fileList; while (fileList) { uint64_t fStart = (uint64_t)fsBufSize + (uint64_t)fileList->rlba * 0x800; uint64_t fEnd = fStart + fileList->size; uint64_t fEndSector = ((fEnd+0x7ffULL)&~0x7ffULL); if (vFilePtr >= fStart && vFilePtr < fEndSector) { if (fileList->multipart) { fprintf(stderr, "Sorry no support for 666 files. I have the feeling that your game is about to crash ^_^\n"); return r; } if (vFilePtr < fEnd) { file_t fd; ssize_t this_r; to_read = MIN(fileList->size-(vFilePtr-fStart), remaining); fd = open_file(fileList->path, O_RDONLY); if (!FD_OK(fd)) { fprintf(stderr, "VISO: file %s cannot be opened!\n", fileList->path); return r; } seek_file(fd, vFilePtr-fStart, SEEK_SET); this_r = read_file(fd, p, to_read); close_file(fd); if (this_r < 0) { fprintf(stderr, "VISO: read_file failed on %s\n", fileList->path); return r; } if (this_r != to_read) { fprintf(stderr, "VISO: read on file %s returned less data than expected (file modified?)\n", fileList->path); return r; } remaining -= to_read; r += to_read; p += to_read; vFilePtr += to_read; } if (remaining > 0 && fEnd != fEndSector) { // This is a zero area after the file to fill the sector to_read = MIN((fEndSector-fEnd)-(vFilePtr-fEnd), remaining); memset(p, 0, to_read); remaining -= to_read; r += to_read; p += to_read; vFilePtr += to_read; } if (remaining == 0) return r; } fileList = fileList->next; } dirList = dirList->next; } } if (vFilePtr >= padAreaStart && vFilePtr < totalSize) { // Pad at the end to_read = MIN(padAreaSize-(vFilePtr-padAreaStart), remaining); memset(p, 0, to_read); remaining -= to_read; r += to_read; p += to_read; vFilePtr += to_read; } return r; }
/* * Look at the start of our "elementary" stream, and try to determine * its actual type. * * - `input` is the input stream to inspect * - if `verbose` is true, the caller wants details of how the decision * is being made * - `decided` is returned TRUE if the function believes it has identified * the stream type, in which case: * - `result` will an appropriate value indicating what we've decided * * Note that this function reads into the stream, and may attempt to * rewind it. * * Returns 0 if nothing went wrong, 1 if an error occurred */ static int determine_packet_type(int input, int verbose, int *decided, int *result) { int err; #ifdef _WIN32 int length; #else ssize_t length; #endif byte first_byte; int video_type; length = read(input,&first_byte,1); if (length == 0) { fprintf(stderr,"### EOF reading first byte\n"); return 1; } else if (length == -1) { fprintf(stderr,"### Error reading first byte: %s\n",strerror(errno)); return 1; } // Does it look like transport stream? err = check_if_TS(input,first_byte,verbose,decided,result); if (err) { close_file(input); return 1; } if (*decided) { close_file(input); return 0; } seek_file(input,0); // Does it look like program stream? err = check_if_PS(input,verbose,decided,result); if (err) { close_file(input); return 1; } if (*decided) { close_file(input); return 0; } seek_file(input,0); // Does it look like one of the types of ES we recognise? if (verbose) printf("Is it an Elementary Stream we recognise?\n"); err = decide_ES_file_video_type(input,!verbose,verbose,&video_type); if (err) { close_file(input); return 1; } switch (video_type) { case VIDEO_H264: *result = STREAM_IS_H264; *decided = TRUE; break; case VIDEO_H262: *result = STREAM_IS_H262; *decided = TRUE; break; case VIDEO_AVS: *result = STREAM_IS_AVS; *decided = TRUE; break; case VIDEO_UNKNOWN: *result = STREAM_IS_UNSURE; *decided = FALSE; if (verbose) printf("Still not sure\n"); break; default: printf("### stream_type: Unexpected decision from" " decide_ES_file_video_type: %d\n",video_type); close_file(input); return 1; } close_file(input); return 0; }
off_t syscall_lseek(int fildes, off_t offset, int whence) { if (fildes >= 0 && fildes < 20 && fileinf->fhandles[fildes] >= 0 && (whence == SEEK_SET || whence == SEEK_CUR || whence == SEEK_END)) return seek_file(fileinf->fhandles[fildes], offset, whence); else return EINVALID_SEEK; }