bool NDS2SF::exe2sf_file(const std::string& nds_path, const std::string& nds2sf_path, std::map<std::string, std::string>& tags) { off_t rom_size_off = path_getfilesize(nds_path.c_str()); if (rom_size_off == -1) { return false; } if (rom_size_off > MAX_NDS_ROM_SIZE) { return false; } uint32_t rom_size = (uint32_t)rom_size_off; FILE * rom_file = fopen(nds_path.c_str(), "rb"); if (rom_file == NULL) { return false; } uint8_t * exe = new uint8_t[NDS2SF_EXE_HEADER_SIZE + rom_size]; if (exe == NULL) { fclose(rom_file); return false; } put_2sf_exe_header(exe, 0, rom_size); if (fread(&exe[NDS2SF_EXE_HEADER_SIZE], 1, rom_size, rom_file) != rom_size) { delete[] exe; fclose(rom_file); return false; } if (!exe2sf(nds2sf_path, exe, NDS2SF_EXE_HEADER_SIZE + rom_size, tags)) { delete[] exe; fclose(rom_file); return false; } delete[] exe; fclose(rom_file); return true; }
bool brr2wav(const std::string & brr_filename, uint16_t pitch) { char path_c[PATH_MAX]; off_t brr_filesize = path_getfilesize(brr_filename.c_str()); if (brr_filesize == -1) { fprintf(stderr, "Error: %s: Unable to open\n", brr_filename.c_str()); return false; } if (brr_filesize == 0) { fprintf(stderr, "Error: %s: File is empty\n", brr_filename.c_str()); return false; } if (brr_filesize > 0x800000) { fprintf(stderr, "Error: %s: File too large\n", brr_filename.c_str()); return false; } uint8_t * data = readfile(brr_filename); if (data == NULL) { return false; } uint8_t * brr = data; size_t brr_size = brr_filesize; int32_t loop_sample = 0; bool has_header = false; switch (brr_size % 9) { case 0: break; case 2: { uint16_t loop_offset = data[0] | (data[1] << 8); loop_sample = loop_offset / 9 * 16; if (loop_offset % 9 == 0) { printf("%s: addmusicM header detected, loop offset = $%04x (sample #%d).\n", brr_filename.c_str(), loop_offset, loop_sample); has_header = true; } else { fprintf(stderr, "%s: warning: illegal length, skip first %d bytes.\n", brr_filename.c_str(), (int)(brr_filesize % 9)); } brr += brr_filesize % 9; brr_size -= brr_filesize % 9; break; } default: fprintf(stderr, "%s: warning: illegal length, skip first %d bytes.\n", brr_filename.c_str(), (int)(brr_filesize % 9)); brr += brr_filesize % 9; brr_size -= brr_filesize % 9; break; } strcpy(path_c, brr_filename.c_str()); path_basename(path_c); path_stripext(path_c); strcat(path_c, ".wav"); std::string wav_filename(path_c); bool looped = false; WavWriter wave(SPCSampDir::decode_brr(brr, brr_size, &looped)); wave.samplerate = pitch * 32000 / 0x1000; wave.bitwidth = 16; wave.channels = 1; if (has_header && looped) { wave.SetLoopSample(loop_sample); } if (!wave.WriteFile(wav_filename)) { fprintf(stderr, "Error: %s: %s\n", wav_filename.c_str(), wave.message().c_str()); delete[] data; return false; } delete[] data; return true; }
bool procyon_ripper::load_rom(const std::string & nds_filename) { if (verbose) { printf("Input NDS ROM \"%s\"\n", nds_filename.c_str()); printf("\n"); } off_t rom_size_off = path_getfilesize(nds_filename.c_str()); if (rom_size_off < 0x180) { return false; } size_t rom_size = (size_t)rom_size_off; if (!is_valid_nds_rom(nds_filename)) { fprintf(stderr, "Error: Invalid NDS ROM.\n"); return false; } FILE * fp = fopen(nds_filename.c_str(), "rb"); if (fp == NULL) { fprintf(stderr, "Error: Unable to open file.\n"); return false; } uint8_t header[0x180]; if (fread(header, 1, 0x180, fp) != 0x180) { fprintf(stderr, "Error: Unable to read header.\n"); fclose(fp); return false; } uint32_t arm9_rom_offset = mget4l(&header[0x20]); uint32_t arm9_load_address = mget4l(&header[0x28]); uint32_t arm9_size = mget4l(&header[0x2c]); if (exe != NULL) { delete[] exe; exe = NULL; rom = NULL; arm9 = NULL; } exe = new uint8_t[NDS2SF_EXE_HEADER_SIZE + rom_size]; if (exe == NULL) { fprintf(stderr, "Error: Unable to allocate memory.\n"); fclose(fp); return false; } NDS2SF::put_2sf_exe_header(exe, 0, rom_size); rewind(fp); if (fread(&exe[NDS2SF_EXE_HEADER_SIZE], 1, rom_size, fp) != rom_size) { fprintf(stderr, "Error: Unable to read ROM.\n"); delete[] exe; exe = NULL; fclose(fp); return false; } this->rom = &exe[NDS2SF_EXE_HEADER_SIZE]; this->rom_size = rom_size; this->arm9 = &rom[arm9_rom_offset]; this->arm9_rom_offset = arm9_rom_offset; this->arm9_load_address = arm9_load_address; this->arm9_size = arm9_size; char abspath[PATH_MAX]; path_getabspath(nds_filename.c_str(), abspath); this->nds_path.assign(abspath); nopRegions.clear(); driver_offset = 0; bgm_play_function_offset = 0; fclose(fp); return true; }