#include "ndstool.h" #include "overlay.h" #include /* * MkDir */ void MkDir(char *name) { #ifdef __MINGW32__ if (mkdir(name)) #else if (mkdir(name, S_IRWXU)) #endif { if (errno != EEXIST) { fprintf(stderr, "Cannot create directory '%s'.\n", name); exit(1); } } } /* * ExtractFile * if rootdir==0 nothing will be written */ void ExtractFile(char *rootdir, char *prefix, char *entry_name, unsigned int file_id) { unsigned int save_filepos = ftell(fNDS); // read FAT data fseek(fNDS, header.fat_offset + 8*file_id, SEEK_SET); unsigned_int top; fread(&top, 1, sizeof(top), fNDS); unsigned_int bottom; fread(&bottom, 1, sizeof(bottom), fNDS); unsigned int size = bottom - top; if (size > (1U << (17 + header.devicecap))) { fprintf(stderr, "File %u: Size is too big. FAT offset 0x%X contains invalid data.\n", file_id, header.fat_offset + 8*file_id); exit(1); } // print file info if (!rootdir || verbose) { printf("%5u 0x%08X 0x%08X %9u %s%s\n", file_id, (int)top, (int)bottom, size, prefix, entry_name); } // extract file if (rootdir) { // make filename char filename[MAXPATHLEN]; strcpy(filename, rootdir); strcat(filename, prefix); strcat(filename, entry_name); fseek(fNDS, top, SEEK_SET); FILE *fo = fopen(filename, "wb"); if (!fo) { fprintf(stderr, "Cannot create file '%s'.\n", filename); exit(1); } while (size > 0) { unsigned char copybuf[1024]; unsigned int size2 = (size >= sizeof(copybuf)) ? sizeof(copybuf) : size; fread(copybuf, 1, size2, fNDS); fwrite(copybuf, 1, size2, fo); size -= size2; } fclose(fo); } fseek(fNDS, save_filepos, SEEK_SET); } /* * MatchName */ bool MatchName(char *name, char *mask, int level=0) { char *a = name; char *b = mask; //for (int i=0; i 0) { unsigned int size2 = (size >= sizeof(copybuf)) ? sizeof(copybuf) : size; fread(copybuf, 1, size2, fNDS); fwrite(copybuf, 1, size2, fo); size -= size2; } if (with_footer) { unsigned_int nitrocode; fread(&nitrocode, sizeof(nitrocode), 1, fNDS); if (nitrocode == 0xDEC00621) { // 0x2106C0DE, version info, reserved? for (int i=0; i<3; i++) // write additional 3 words { fwrite(&nitrocode, sizeof(nitrocode), 1, fo); fread(&nitrocode, sizeof(nitrocode), 1, fNDS); // next field } } } fclose(fo); fclose(fNDS); }