int fclose(FILE *f) { long errorCode = sysfile_close(f); if (errorCode != 0) { post("sysfile_close error %d - aren't you glad you asked?", errorCode); } return errorCode; }
static bool checkCrc32(const unsigned id) { int bytesRead; U8 tempBuffer[1024]; U32 expectedCrc32, calculatedCrc32 = MZ_CRC32_INIT; file_t fp = sysfile_open(resourceFiles[id]); if (fp == NULL) { sys_error("(resources) unable to open \"%s\"", resourceFiles[id]); return false; } bytesRead = sysfile_read(fp, tempBuffer, sizeof(U32), 1); /* prepare beginning of buffer for the following loop */ if (bytesRead != 1) { sys_error("(resources) not enough data for \"%s\"", resourceFiles[id]); sysfile_close(fp); return false; } do { bytesRead = sysfile_read(fp, tempBuffer + sizeof(U32), sizeof(U8), sizeof(tempBuffer) - sizeof(U32)); calculatedCrc32 = mz_crc32(calculatedCrc32, tempBuffer, bytesRead); memcpy(tempBuffer, tempBuffer + bytesRead, sizeof(U32)); } while (bytesRead == sizeof(tempBuffer) - sizeof(U32)); sysfile_close(fp); memcpy(&expectedCrc32, tempBuffer, sizeof(U32)); expectedCrc32 = letoh32(expectedCrc32); if (expectedCrc32 != calculatedCrc32) { sys_error("(resources) crc check failed for \"%s\"", resourceFiles[id]); return false; } return true; }
long jsusfx_edsave(t_jsusfx *x, char **ht, long size) { t_filehandle fh_write; if ( path_opensysfile(x->scriptname, x->path, &fh_write, WRITE_PERM) ) { error("jsusfx~: unable to save script file"); return 1; } sysfile_writetextfile(fh_write, ht, TEXT_LB_NATIVE); sysfile_close(fh_write); jsusfx_compile(x, NULL, 0, NULL); return 0; }
void filein_close(t_filein *x) { if (x->f_open) { sysfile_close(x->f_fh); x->f_fh = 0; x->f_open = FALSE; } if (x->f_data) { sysmem_lockhandle((t_handle)x->f_data,0); sysmem_freehandle((t_handle)x->f_data); x->f_data = 0; } }
int lua_importfile(lua_State *L) { const char *filename; long outtype; short file_volume, err; if(!lua_isstring(L, 1)) luaL_error(L, "argument 1 for outlet must be a string"); filename = lua_tostring(L, 1); err = locatefile_extended(filename, &file_volume, &outtype, 0, 0); if(err) { error("jit.gl.lua: can't find file %s", filename); } else { // read file from disk long count; char **texthandle; t_filehandle fh; err = path_opensysfile(filename, file_volume, &fh, READ_PERM); if (err) { error("jit.gl.lua: %s: error %d opening file", filename, err); return 0; } texthandle = sysmem_newhandle(0); sysfile_readtextfile(fh, texthandle, 0, TEXT_LB_NATIVE); sysfile_close(fh); count = sysmem_handlesize(texthandle); sysmem_resizehandle(texthandle, count + 1); (*texthandle)[count] = 0; //NULL terminate, '\0'? //run file in Lua err = luaL_loadstring(L, *texthandle); err = lua_pcall(L, 0, LUA_MULTRET, 0); if (err) { post("lua pcall error %s", lua_tostring(L, -1)); lua_pop(L, 1); } sysmem_lockhandle(texthandle, false); sysmem_freehandle(texthandle); } return 0; }
void filein_open(t_filein *x, char *name) { long size; if (x->f_spool) x->f_open = TRUE; else { sysfile_geteof(x->f_fh,&size); if (!(x->f_data = (Byte **)sysmem_newhandle(size))) object_error((t_object *)x, "%s too big to read",name); else { sysmem_lockhandle((t_handle)x->f_data,1); sysfile_read(x->f_fh,&size,*x->f_data); x->f_size = size; } sysfile_close(x->f_fh); } x->f_spool = FALSE; }
void jsusfx_dblclick(t_jsusfx *x) { if (!x->m_editor) { t_filehandle fh_read; if ( path_opensysfile(x->scriptname, x->path, &fh_read, READ_PERM) ) { error("jsusfx~: unable to script file"); return; } t_handle texthandle; texthandle = sysmem_newhandle(0); sysfile_readtextfile(fh_read, texthandle, 0, TEXT_NULL_TERMINATE); x->m_editor = reinterpret_cast<t_object *>(object_new(CLASS_NOBOX, gensym("jed"), (t_object *)x, 0)); object_attr_setchar(x->m_editor, gensym("scratch"), 1); object_method(x->m_editor, gensym("settext"), *texthandle, gensym("utf-8")); object_method(x->m_editor, gensym("filename"), x->scriptname, x->path); sysmem_freehandle(texthandle); sysfile_close(fh_read); } else { object_attr_setchar(x->m_editor, gensym("visible"), 1); } }
void filecontainer_doopen(t_filecontainer *x, t_symbol *arg) { t_atom a[4]; int err = 0; char filename[256]; short path; t_fourcc outtype = 0; t_fourcc type = 'cO0p'; #ifdef MAC_VERSION char *temppath; FSRef ref; Boolean isDir; FSCatalogInfo catalogInfo; #else // WIN_VERSION char temppath[512]; #endif char fullpath[512]; t_object *result = NULL; t_object *result2 = NULL; char **record = NULL; // sqlite records char **record2 = NULL; // sqlite records t_filehandle file_handle; t_ptr_size len = 0; char *blob; char sql[512]; if(!arg || !arg->s_name[0]) { if(open_dialog(filename, &path, &outtype, NULL, -1)) // Returns 0 if successful return; } else { t_fourcc typelist[1]; typelist[0] = 'cO0p'; strcpy(filename, arg->s_name); path = 0; locatefile_extended(filename, &path, &type, typelist, 0); } path_topotentialname(path, filename, fullpath, 0); #ifdef MAC_VERSION temppath = strchr(fullpath, ':'); temppath += 1; #else // WIN_VERSION path_nameconform(fullpath, temppath, PATH_STYLE_NATIVE_WIN, PATH_TYPE_ABSOLUTE); #endif x->name = gensym(temppath); // Create our temp folder for extracted files filecontainer_gettemppath(x); // Create the SQLite instance atom_setsym(&a[0], gensym("@rambased")); atom_setlong(&a[1], 0); atom_setsym(&a[2], gensym("@db")); atom_setsym(&a[3], x->name); x->sqlite = object_new_typed(CLASS_NOBOX, _sym_sqlite, 4, a); // Operate on the open DB if(x->sqlite) { object_method(x->sqlite, ps_starttransaction); object_method(x->sqlite, _sym_execstring, TABLEDEF_FILES, NULL); object_method(x->sqlite, _sym_execstring, TABLEDEF_ATTRS, NULL); object_method(x->sqlite, _sym_execstring, "UPDATE files SET valid = 1", NULL); object_method(x->sqlite, _sym_execstring, "SELECT file_id, filename, moddate FROM files", &result); while(record = (char **)object_method(result, _sym_nextrecord)) { // Here we check for the optional 'platform' attr for this file. // If a flag exists for the other platform, but not for the current platform, then we ignore this file. #ifdef MAC_VERSION sprintf(sql, "SELECT file_id_ext FROM attrs \ WHERE attr_name = 'platform' AND attr_value = 'windows' AND file_id_ext = %s ", record[0]); object_method(x->sqlite, _sym_execstring, sql, &result2); record2 = (char **)object_method(result2, _sym_nextrecord); if(record2) { sprintf(sql, "SELECT file_id_ext FROM attrs \ WHERE attr_name = 'platform' AND attr_value = 'mac' AND file_id_ext = %s ", record[0]); object_method(x->sqlite, _sym_execstring, sql, &result2); record2 = (char **)object_method(result2, _sym_nextrecord); if(!record2) { sprintf(sql, "UPDATE files SET valid = 0 WHERE file_id = %s ", record[0]); object_method(x->sqlite, _sym_execstring, sql, NULL); continue; } } #else // WIN_VERSION snprintf(sql, 512, "SELECT file_id_ext FROM attrs \ WHERE attr_name = 'platform' AND attr_value = 'mac' AND file_id_ext = %s ", record[0]); object_method(x->sqlite, _sym_execstring, sql, &result2); record2 = (char **)object_method(result2, _sym_nextrecord); if(record2) { snprintf(sql, 512, "SELECT file_id_ext FROM attrs \ WHERE attr_name = 'platform' AND attr_value = 'windows' AND file_id_ext = %s ", record[0]); object_method(x->sqlite, _sym_execstring, sql, &result2); record2 = (char **)object_method(result2, _sym_nextrecord); if(!record2) { snprintf(sql, 512, "UPDATE files SET valid = 0 WHERE file_id = %s ", record[0]); object_method(x->sqlite, _sym_execstring, sql, NULL); continue; } } #endif // At this point we have a file (record[0]), and we have determined that it is indeed a file we want to cache // So cache it to a new file in our temp path err = path_createsysfile(record[1], x->temp_path, type, &file_handle); if(err) { // Handle any errors that occur object_error((t_object *)x, "%s - error %d creating file", filename, err); } else { snprintf(sql, 512, "SELECT content FROM files WHERE file_id = %s", record[0]); object_method(x->sqlite, ps_getblob, sql, &blob, &len); err = sysfile_write(file_handle, &len, blob); if(err) { object_error((t_object *)x, "sysfile_write error (%d)", err); } } err = sysfile_seteof(file_handle, len); if(err) { object_error((t_object *)x, "%s - error %d setting EOF", filename, err); } sysfile_close(file_handle); // close file reference sysmem_freeptr(blob); blob = NULL; // Set the moddate #ifdef MAC_VERSION // FSCatalogInfo catalogInfo; // Boolean status; CFGregorianDate gdate; CFAbsoluteTime abstime; CFTimeZoneRef tz; UTCDateTime utc; sscanf(record[2], "%4ld-%02hd-%02hd %02hd:%02hd:%02lf", &gdate.year, (signed short*)&gdate.month, (signed short*)&gdate.day, (signed short*)&gdate.hour, (signed short*)&gdate.minute, &gdate.second); tz = CFTimeZoneCopySystem(); abstime = CFGregorianDateGetAbsoluteTime(gdate, tz); UCConvertCFAbsoluteTimeToUTCDateTime(abstime, &utc); catalogInfo.contentModDate = utc; strcpy(s_tempstr, x->temp_fullpath->s_name); temppath = strchr(s_tempstr, ':'); temppath++; strcat(temppath, "/"); strcat(temppath, record[1]); FSPathMakeRef((UInt8*)temppath, &ref, &isDir); err = FSSetCatalogInfo(&ref, kFSCatInfoContentMod, &catalogInfo); #else // WIN_VERSION char winpath[512]; HANDLE hFile; FILETIME fileTime; SYSTEMTIME systemTime; sscanf(record[2], "%4lu-%02lu-%02lu %02lu:%02lu:%02lu", &systemTime.wYear, &systemTime.wMonth, &systemTime.wDay, &systemTime.wHour, &systemTime.wMinute, &systemTime.wSecond); err = SystemTimeToFileTime(&systemTime, &fileTime); strcpy(s_tempstr, x->temp_fullpath->s_name); path_nameconform(s_tempstr, winpath, PATH_STYLE_NATIVE_WIN, PATH_TYPE_ABSOLUTE); strcat(winpath, "\\"); strcat(winpath, record[1]); hFile = CreateFile((LPCSTR)winpath , GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); if(hFile == INVALID_HANDLE_VALUE) { object_error((t_object *)x, "invalid handle value"); goto out; } err = SetFileTime(hFile, &fileTime, &fileTime, &fileTime); if(err == 0) { err = GetLastError(); object_error((t_object *)x, "Error setting date: %i", err); } CloseHandle(hFile); out: ; #endif } object_method(x->sqlite, ps_endtransaction); }
static uint32_t sys_close(uint32_t arg[]) { int fd = (int)arg[0]; return sysfile_close(fd); }
void *jsusfx_new(t_symbol *notused, long argc, t_atom *argv) { if ( argc < 1 || atom_gettype(argv) != A_SYM ) { error("jsusfx~: missing script name"); return NULL; } t_jsusfx *x = reinterpret_cast<t_jsusfx *>(object_alloc(jsusfx_class)); t_symbol *s = atom_getsym(argv); t_fourcc filetype = 'TEXT', outtype; short path; char filename[MAX_PATH_CHARS]; strcpy(filename, s->s_name); if (locatefile_extended(filename, &path, &outtype, &filetype, 1)) { t_object *mypatcher; object_obex_lookup(x, gensym("#P"), &mypatcher); t_symbol *checkExists = object_attr_getsym(mypatcher, gensym("filepath")); if ( checkExists->s_name[0] == 0 ) { error("jsusfx~: patch needs to be saved in order to create new jsusfx script file"); return NULL; } path = path_getdefault(); t_fourcc type = 'TEXT'; t_filehandle ref; if ( path_createsysfile(filename, path, type, &ref) ) { error("jsusfx~: unable to create file"); return NULL; } char initText[] = "@sample\nspl0=1\nspl1=-1\n"; t_handle h = sysmem_newhandle(0); sysmem_ptrandhand(initText,h,strlen(initText)); if ( sysfile_writetextfile(ref, h, TEXT_LB_NATIVE) ) { error("jsusfx~: unable to write file"); return NULL; } sysfile_close(ref); sysmem_freehandle(h); } strcpy(x->scriptname, filename); x->path = path; char fullpath[MAX_PATH_CHARS]; path_toabsolutesystempath(path, filename, fullpath); std::ifstream is(fullpath); if ( ! is.is_open() ) { error("jsusfx~: error opening file %s", fullpath); return NULL; } x->bypass = false; dsp_setup((t_pxobject *)x, 2); x->outlet1 = outlet_new((t_object *)x, NULL); outlet_new((t_object *)x, "signal"); outlet_new((t_object *)x, "signal"); critical_new(&(x->critical)); x->m_editor = NULL; JsusFxMax *fx = new JsusFxMax(); fx->compile(is); x->fx = fx; /*if ( argc >= 2 && atom_gettype(argv+1) == A_LONG ) { x->fx->normalizeSliders = atom_getlong(argv+1); } else { x->fx->normalizeSliders = 1; } post("normalizer sl %x", x->fx->normalizeSliders);*/ return (x); }
static int load_icode(int fd, int argc, char **kargv, int envc, char **kenvp) { assert(argc >= 0 && argc <= EXEC_MAX_ARG_NUM); assert(envc >= 0 && envc <= EXEC_MAX_ENV_NUM); if (current->mm != NULL) { panic("load_icode: current->mm must be empty.\n"); } int ret = -E_NO_MEM; struct mm_struct *mm; if ((mm = mm_create()) == NULL) { goto bad_mm; } if (setup_pgdir(mm) != 0) { goto bad_pgdir_cleanup_mm; } mm->brk_start = 0; struct Page *page; struct elfhdr __elf, *elf = &__elf; if ((ret = load_icode_read(fd, elf, sizeof(struct elfhdr), 0)) != 0) { goto bad_elf_cleanup_pgdir; } if (elf->e_magic != ELF_MAGIC) { ret = -E_INVAL_ELF; goto bad_elf_cleanup_pgdir; } struct proghdr __ph, *ph = &__ph; uint32_t vm_flags, phnum; pte_perm_t perm = 0; for (phnum = 0; phnum < elf->e_phnum; phnum ++) { off_t phoff = elf->e_phoff + sizeof(struct proghdr) * phnum; if ((ret = load_icode_read(fd, ph, sizeof(struct proghdr), phoff)) != 0) { goto bad_cleanup_mmap; } if (ph->p_type != ELF_PT_LOAD) { continue ; } if (ph->p_filesz > ph->p_memsz) { ret = -E_INVAL_ELF; goto bad_cleanup_mmap; } vm_flags = 0; ptep_set_u_read(&perm); if (ph->p_flags & ELF_PF_X) vm_flags |= VM_EXEC; if (ph->p_flags & ELF_PF_W) vm_flags |= VM_WRITE; if (ph->p_flags & ELF_PF_R) vm_flags |= VM_READ; if (vm_flags & VM_WRITE) ptep_set_u_write(&perm); if ((ret = mm_map(mm, ph->p_va, ph->p_memsz, vm_flags, NULL)) != 0) { goto bad_cleanup_mmap; } if (mm->brk_start < ph->p_va + ph->p_memsz) { mm->brk_start = ph->p_va + ph->p_memsz; } off_t offset = ph->p_offset; size_t off, size; uintptr_t start = ph->p_va, end, la = ROUNDDOWN(start, PGSIZE); end = ph->p_va + ph->p_filesz; while (start < end) { if ((page = pgdir_alloc_page(mm->pgdir, la, perm)) == NULL) { ret = -E_NO_MEM; goto bad_cleanup_mmap; } off = start - la, size = PGSIZE - off, la += PGSIZE; if (end < la) { size -= la - end; } if ((ret = load_icode_read(fd, page2kva(page) + off, size, offset)) != 0) { goto bad_cleanup_mmap; } start += size, offset += size; } end = ph->p_va + ph->p_memsz; if (start < la) { /* ph->p_memsz == ph->p_filesz */ if (start == end) { continue ; } off = start + PGSIZE - la, size = PGSIZE - off; if (end < la) { size -= la - end; } memset(page2kva(page) + off, 0, size); start += size; assert((end < la && start == end) || (end >= la && start == la)); } while (start < end) { if ((page = pgdir_alloc_page(mm->pgdir, la, perm)) == NULL) { ret = -E_NO_MEM; goto bad_cleanup_mmap; } off = start - la, size = PGSIZE - off, la += PGSIZE; if (end < la) { size -= la - end; } memset(page2kva(page) + off, 0, size); start += size; } } sysfile_close(fd); mm->brk_start = mm->brk = ROUNDUP(mm->brk_start, PGSIZE); /* setup user stack */ vm_flags = VM_READ | VM_WRITE | VM_STACK; if ((ret = mm_map(mm, USTACKTOP - USTACKSIZE, USTACKSIZE, vm_flags, NULL)) != 0) { goto bad_cleanup_mmap; } bool intr_flag; local_intr_save(intr_flag); { list_add(&(proc_mm_list), &(mm->proc_mm_link)); } local_intr_restore(intr_flag); mm_count_inc(mm); current->mm = mm; set_pgdir(current, mm->pgdir); mm->lapic = pls_read(lapic_id); mp_set_mm_pagetable(mm); if (init_new_context (current, elf, argc, kargv, envc, kenvp) < 0) goto bad_cleanup_mmap; ret = 0; out: return ret; bad_cleanup_mmap: exit_mmap(mm); bad_elf_cleanup_pgdir: put_pgdir(mm); bad_pgdir_cleanup_mm: mm_destroy(mm); bad_mm: goto out; }
static bool readFile(const unsigned id) { bool success; file_t fp; void * vp; switch (id) { #ifndef GFXST case Resource_PICHAF: /* fallthrough */ case Resource_PICCONGRATS: /* fallthrough */ case Resource_PICSPLASH: return true; #endif /* ndef GFXST */ #ifndef GFXPC case Resource_IMAINHOFT: /* fallthrough */ case Resource_IMAINRDT: /* fallthrough */ case Resource_IMAINCDC: /* fallthrough */ case Resource_SCREENCONGRATS: return true; #endif /* ndef GFXPC */ default: break; } if (resourceFiles[id] == NULL) { sys_error("(resources) resource ID %d not available", id); return false; } if (!checkCrc32(id)) { return false; } fp = sysfile_open(resourceFiles[id]); if (fp == NULL) { sys_error("(resources) unable to open \"%s\"", resourceFiles[id]); return false; } success = readHeader(fp, id); if (success) { switch (id) { case Resource_FILELIST: success = loadResourceFilelist(fp); break; case Resource_PALETTE: { vp = game_colors; success = loadRawData(fp, &vp, sizeof(*game_colors), &game_color_count); game_colors = vp; break; } case Resource_ENTDATA: success = loadResourceEntdata(fp); break; case Resource_SPRSEQ: { vp = ent_sprseq; success = loadRawData(fp, &vp, sizeof(*ent_sprseq), &ent_nbr_sprseq); ent_sprseq = vp; break; } case Resource_MVSTEP: { vp = ent_mvstep; success = loadRawData(fp, &vp, sizeof(*ent_mvstep), &ent_nbr_mvstep); ent_mvstep = vp; break; } case Resource_MAPS: success = loadResourceMaps(fp); break; case Resource_SUBMAPS: success = loadResourceSubmaps(fp); break; case Resource_CONNECT: { vp = map_connect; success = loadRawData(fp, &vp, sizeof(*map_connect), &map_nbr_connect); map_connect = vp; break; } case Resource_BNUMS: { vp = map_bnums; success = loadRawData(fp, &vp, sizeof(*map_bnums), &map_nbr_bnums); map_bnums = vp; break; } case Resource_BLOCKS: { vp = map_blocks; success = loadRawData(fp, &vp, sizeof(*map_blocks), &map_nbr_blocks); map_blocks = vp; break; } case Resource_MARKS: { vp = map_marks; success = loadRawData(fp, &vp, sizeof(*map_marks), &map_nbr_marks); map_marks = vp; break; } case Resource_EFLGC: { vp = map_eflg_c; success = loadRawData(fp, &vp, sizeof(*map_eflg_c), &map_nbr_eflgc); map_eflg_c = vp; break; } case Resource_IMAPSL: { vp = screen_imapsl; success = loadRawData(fp, &vp, sizeof(*screen_imapsl), &screen_nbr_imapsl); screen_imapsl = vp; break; } case Resource_IMAPSTEPS: success = loadResourceImapsteps(fp); break; case Resource_IMAPSOFS: { vp = screen_imapsofs; success = loadRawData(fp, &vp, sizeof(*screen_imapsofs), &screen_nbr_imapsofs); screen_imapsofs = vp; break; } case Resource_IMAPTEXT: success = loadResourceImaptext(fp); break; case Resource_GAMEOVERTXT: success = loadString(fp, (char **)(&screen_gameovertxt), 0xFE); break; case Resource_PAUSEDTXT: success = loadString(fp, (char **)(&screen_pausedtxt), 0xFE); break; case Resource_SPRITESDATA: success = loadResourceSpritesData(fp); break; case Resource_TILESDATA: success = loadResourceTilesData(fp); break; case Resource_HIGHSCORES: success = loadResourceHighScores(fp); break; case Resource_IMGSPLASH: success = loadImage(fp, &img_splash); break; #ifdef GFXST case Resource_PICHAF: success = loadPicture(fp, &pic_haf); break; case Resource_PICCONGRATS: success = loadPicture(fp, &pic_congrats); break; case Resource_PICSPLASH: success = loadPicture(fp, &pic_splash); break; #endif /* GFXST */ #ifdef GFXPC case Resource_IMAINHOFT: success = loadString(fp, (char **)(&screen_imainhoft), 0xFE); break; case Resource_IMAINRDT: success = loadString(fp, (char **)(&screen_imainrdt), 0xFE); break; case Resource_IMAINCDC: success = loadString(fp, (char **)(&screen_imaincdc), 0xFE); break; case Resource_SCREENCONGRATS: success = loadString(fp, (char **)(&screen_congrats), 0xFE); break; #endif /* GFXPC */ default: success = false; break; } } if (!success) { sys_error("(resources) error when parsing \"%s\"", resourceFiles[id]); } sysfile_close(fp); return success; }
static bool loadSound(const unsigned id) { sound_t ** sound; file_t fp; wave_header_t header; U16 u16Temp; U32 u32Temp; int bytesRead; bool isHeaderValid; if (!fromResourceIdToSound(id, &sound)) { return false; } *sound = sysmem_push(sizeof(**sound)); if (!*sound) { return false; } (*sound)->buf = NULL; (*sound)->dispose = true; /* sounds are "fire and forget" by default */ (*sound)->name = u_strdup(resourceFiles[id]); if (!(*sound)->name) { return false; } fp = sysfile_open(resourceFiles[id]); if (!fp) { sys_error("(resources) unable to open \"%s\"", resourceFiles[id]); return false; } bytesRead = sysfile_read(fp, &header, sizeof(header), 1); sysfile_close(fp); if (bytesRead != 1) { sys_error("(resources) unable to read WAVE header from \"%s\"", resourceFiles[id]); return false; } isHeaderValid = false; for (;;) { if (memcmp(header.riffChunkId, "RIFF", 4) || memcmp(header.riffType, "WAVE", 4) || memcmp(header.formatChunkId, "fmt ", 4) || memcmp(header.dataChunkId, "data", 4)) { break; } memcpy(&u16Temp, header.audioFormat, sizeof(u16Temp)); if (letoh16(u16Temp) != Wave_AUDIO_FORMAT) { break; } memcpy(&u16Temp, header.channelCount, sizeof(u16Temp)); if (letoh16(u16Temp) != Wave_CHANNEL_COUNT) { break; } memcpy(&u32Temp, header.sampleRate, sizeof(u32Temp)); if (letoh32(u32Temp) != Wave_SAMPLE_RATE) { isHeaderValid = false; break; } memcpy(&u16Temp, header.bitsPerSample, sizeof(u16Temp)); if (letoh16(u16Temp) != Wave_BITS_PER_SAMPLE) { isHeaderValid = false; break; } memcpy(&u32Temp, header.dataChunkSize, sizeof(u32Temp)); (*sound)->len = letoh32(u32Temp); isHeaderValid = true; break; } if (!isHeaderValid) { sys_error("(resources) incompatible WAVE header for \"%s\"", resourceFiles[id]); return false; } return true; }
static int load_icode(int fd, int argc, char **kargv, int envc, char **kenvp) { assert(argc >= 0 && argc <= EXEC_MAX_ARG_NUM); assert(envc >= 0 && envc <= EXEC_MAX_ENV_NUM); if (current->mm != NULL) { panic("load_icode: current->mm must be empty.\n"); } int ret = -E_NO_MEM; //#ifdef UCONFIG_BIONIC_LIBC uint32_t real_entry; //#endif //UCONFIG_BIONIC_LIBC struct mm_struct *mm; if ((mm = mm_create()) == NULL) { goto bad_mm; } if (setup_pgdir(mm) != 0) { goto bad_pgdir_cleanup_mm; } mm->brk_start = 0; struct Page *page; struct elfhdr __elf, *elf = &__elf; if ((ret = load_icode_read(fd, elf, sizeof(struct elfhdr), 0)) != 0) { goto bad_elf_cleanup_pgdir; } if (elf->e_magic != ELF_MAGIC) { ret = -E_INVAL_ELF; goto bad_elf_cleanup_pgdir; } //#ifdef UCONFIG_BIONIC_LIBC real_entry = elf->e_entry; uint32_t load_address, load_address_flag = 0; //#endif //UCONFIG_BIONIC_LIBC struct proghdr __ph, *ph = &__ph; uint32_t vm_flags, phnum; pte_perm_t perm = 0; //#ifdef UCONFIG_BIONIC_LIBC uint32_t is_dynamic = 0, interp_idx; uint32_t bias = 0; //#endif //UCONFIG_BIONIC_LIBC for (phnum = 0; phnum < elf->e_phnum; phnum++) { off_t phoff = elf->e_phoff + sizeof(struct proghdr) * phnum; if ((ret = load_icode_read(fd, ph, sizeof(struct proghdr), phoff)) != 0) { goto bad_cleanup_mmap; } if (ph->p_type == ELF_PT_INTERP) { is_dynamic = 1; interp_idx = phnum; continue; } if (ph->p_type != ELF_PT_LOAD) { continue; } if (ph->p_filesz > ph->p_memsz) { ret = -E_INVAL_ELF; goto bad_cleanup_mmap; } if (ph->p_va == 0 && !bias) { bias = 0x00008000; } if ((ret = map_ph(fd, ph, mm, &bias, 0)) != 0) { kprintf("load address: 0x%08x size: %d\n", ph->p_va, ph->p_memsz); goto bad_cleanup_mmap; } if (load_address_flag == 0) load_address = ph->p_va + bias; ++load_address_flag; /*********************************************/ /* vm_flags = 0; ptep_set_u_read(&perm); if (ph->p_flags & ELF_PF_X) vm_flags |= VM_EXEC; if (ph->p_flags & ELF_PF_W) vm_flags |= VM_WRITE; if (ph->p_flags & ELF_PF_R) vm_flags |= VM_READ; if (vm_flags & VM_WRITE) ptep_set_u_write(&perm); if ((ret = mm_map(mm, ph->p_va, ph->p_memsz, vm_flags, NULL)) != 0) { goto bad_cleanup_mmap; } if (mm->brk_start < ph->p_va + ph->p_memsz) { mm->brk_start = ph->p_va + ph->p_memsz; } off_t offset = ph->p_offset; size_t off, size; uintptr_t start = ph->p_va, end, la = ROUNDDOWN(start, PGSIZE); end = ph->p_va + ph->p_filesz; while (start < end) { if ((page = pgdir_alloc_page(mm->pgdir, la, perm)) == NULL) { ret = -E_NO_MEM; goto bad_cleanup_mmap; } off = start - la, size = PGSIZE - off, la += PGSIZE; if (end < la) { size -= la - end; } if ((ret = load_icode_read(fd, page2kva(page) + off, size, offset)) != 0) { goto bad_cleanup_mmap; } start += size, offset += size; } end = ph->p_va + ph->p_memsz; if (start < la) { // ph->p_memsz == ph->p_filesz if (start == end) { continue ; } off = start + PGSIZE - la, size = PGSIZE - off; if (end < la) { size -= la - end; } memset(page2kva(page) + off, 0, size); start += size; assert((end < la && start == end) || (end >= la && start == la)); } while (start < end) { if ((page = pgdir_alloc_page(mm->pgdir, la, perm)) == NULL) { ret = -E_NO_MEM; goto bad_cleanup_mmap; } off = start - la, size = PGSIZE - off, la += PGSIZE; if (end < la) { size -= la - end; } memset(page2kva(page) + off, 0, size); start += size; } */ /**************************************/ } mm->brk_start = mm->brk = ROUNDUP(mm->brk_start, PGSIZE); /* setup user stack */ vm_flags = VM_READ | VM_WRITE | VM_STACK; if ((ret = mm_map(mm, USTACKTOP - USTACKSIZE, USTACKSIZE, vm_flags, NULL)) != 0) { goto bad_cleanup_mmap; } if (is_dynamic) { elf->e_entry += bias; bias = 0; off_t phoff = elf->e_phoff + sizeof(struct proghdr) * interp_idx; if ((ret = load_icode_read(fd, ph, sizeof(struct proghdr), phoff)) != 0) { goto bad_cleanup_mmap; } char *interp_path = (char *)kmalloc(ph->p_filesz); load_icode_read(fd, interp_path, ph->p_filesz, ph->p_offset); int interp_fd = sysfile_open(interp_path, O_RDONLY); assert(interp_fd >= 0); struct elfhdr interp___elf, *interp_elf = &interp___elf; assert((ret = load_icode_read(interp_fd, interp_elf, sizeof(struct elfhdr), 0)) == 0); assert(interp_elf->e_magic == ELF_MAGIC); struct proghdr interp___ph, *interp_ph = &interp___ph; uint32_t interp_phnum; uint32_t va_min = 0xffffffff, va_max = 0; for (interp_phnum = 0; interp_phnum < interp_elf->e_phnum; ++interp_phnum) { off_t interp_phoff = interp_elf->e_phoff + sizeof(struct proghdr) * interp_phnum; assert((ret = load_icode_read(interp_fd, interp_ph, sizeof(struct proghdr), interp_phoff)) == 0); if (interp_ph->p_type != ELF_PT_LOAD) { continue; } if (va_min > interp_ph->p_va) va_min = interp_ph->p_va; if (va_max < interp_ph->p_va + interp_ph->p_memsz) va_max = interp_ph->p_va + interp_ph->p_memsz; } bias = get_unmapped_area(mm, va_max - va_min + 1 + PGSIZE); bias = ROUNDUP(bias, PGSIZE); for (interp_phnum = 0; interp_phnum < interp_elf->e_phnum; ++interp_phnum) { off_t interp_phoff = interp_elf->e_phoff + sizeof(struct proghdr) * interp_phnum; assert((ret = load_icode_read(interp_fd, interp_ph, sizeof(struct proghdr), interp_phoff)) == 0); if (interp_ph->p_type != ELF_PT_LOAD) { continue; } assert((ret = map_ph(interp_fd, interp_ph, mm, &bias, 1)) == 0); } real_entry = interp_elf->e_entry + bias; sysfile_close(interp_fd); kfree(interp_path); } sysfile_close(fd); bool intr_flag; local_intr_save(intr_flag); { list_add(&(proc_mm_list), &(mm->proc_mm_link)); } local_intr_restore(intr_flag); mm_count_inc(mm); current->mm = mm; set_pgdir(current, mm->pgdir); mm->cpuid = myid(); mp_set_mm_pagetable(mm); if (!is_dynamic) { real_entry += bias; } #ifdef UCONFIG_BIONIC_LIBC if (init_new_context_dynamic(current, elf, argc, kargv, envc, kenvp, is_dynamic, real_entry, load_address, bias) < 0) goto bad_cleanup_mmap; #else if (init_new_context(current, elf, argc, kargv, envc, kenvp) < 0) goto bad_cleanup_mmap; #endif //UCONFIG_BIONIC_LIBC ret = 0; out: return ret; bad_cleanup_mmap: exit_mmap(mm); bad_elf_cleanup_pgdir: put_pgdir(mm); bad_pgdir_cleanup_mm: mm_destroy(mm); bad_mm: goto out; }