int fileio_load_archive(const char *filename, unsigned char **dataout, int *datasize, int *dataoffset, const char *filetoload, char *outname) { FILE *f; unsigned char idbuf[4]; int filesFound = 0; std::vector<char *> filelist; // list of files we can load in this archive // default case: outname is filename if (outname) { strcpy(outname, filename); } f = fopen(filename, "rb"); if (!f) { return 0; // no good } fread(idbuf, 4, 1, f); fclose(f); // printf("ID bytes %c %c %x %x\n", idbuf[0], idbuf[1], idbuf[2], idbuf[3]); // Handle all archives with common libarchive code if ((idbuf[0] == 'P') && (idbuf[1] == 'K') && (idbuf[2] == 0x03) && (idbuf[3] == 0x04) || // zip ((idbuf[0] == '7') && (idbuf[1] == 'z') && (idbuf[2] == 0xbc) && (idbuf[3] == 0xaf)) || // 7zip ((idbuf[0] == 0xfd) && (idbuf[1] == 0x37) && (idbuf[2] == 0x7a) && (idbuf[3] == 0x58)) || // txz ((idbuf[0] == 0x1f) && (idbuf[1] == 0x8b) && (idbuf[2] == 0x08) && (idbuf[3] == 0x00)) || // tgz ((idbuf[0] == 0x42) && (idbuf[1] == 0x5a) && (idbuf[2] == 0x68) && (idbuf[3] == 0x39)) // tbz ) { a = archive_read_new(); archive_read_support_filter_all(a); archive_read_support_format_all(a); r = archive_read_open_filename(a, filename, 10240); int64_t entry_size; if (r != ARCHIVE_OK) { print_message("Archive failed to open."); } while (archive_read_next_header(a, &entry) == ARCHIVE_OK) { const char *currentFile = archive_entry_pathname(entry); unsigned char *fileContents; entry_size = archive_entry_size(entry); fileContents = (unsigned char *)malloc(entry_size); if (filetoload != NULL) { if (!strcmp(currentFile, filetoload)) { archive_read_data(a, fileContents, entry_size); archive_read_data_skip(a); r = archive_read_free(a); *datasize = entry_size; *dataout = fileContents; *dataoffset = 0; return 1; } } else { if (checkExtension(currentFile)) { char *tmpstr; tmpstr = (char *)malloc(strlen(currentFile)+1); strcpy(tmpstr, currentFile); // add to the file list filelist.push_back(tmpstr); filesFound++; } } } } else if ((idbuf[0] == 'R') && (idbuf[1] == 'a') && (idbuf[2] == 'r') && (idbuf[3] == '!')) { // it's rar print_message("Rar files are not supported."); } // if we found any files and weren't forced to load them, handle accordingly if (filesFound) { // only 1 file found, just run it if (filesFound == 1) { char fname[512]; strcpy(fname, filelist[0]); free(filelist[0]); filelist.clear(); strcpy(outname, fname); return fileio_load_archive(filename, dataout, datasize, dataoffset, fname, NULL); } else // multiple files we can handle found, give the user a choice { int sel; char fname[512]; GtkWidget *archselect = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW (archselect), "Pick game in archive"); gtk_window_set_modal(GTK_WINDOW (archselect), TRUE); GtkWidget *archbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0); gtk_container_add(GTK_CONTAINER(archselect), archbox); gtk_widget_show(archbox); GtkWidget *scrolledwindow = gtk_scrolled_window_new(NULL, NULL); gtk_box_pack_start(GTK_BOX(archbox), scrolledwindow, TRUE, TRUE, 0); gtk_widget_set_size_request(scrolledwindow, 340, 340); gtk_widget_show(scrolledwindow); GtkWidget *buttonbox = gtk_widget_new(GTK_TYPE_BOX, "halign", GTK_ALIGN_END, NULL); gtk_box_pack_start(GTK_BOX(archbox), buttonbox, FALSE, TRUE, 0); gtk_widget_show(buttonbox); GtkWidget *archtree = gtk_tree_view_new(); gtk_container_add(GTK_CONTAINER (scrolledwindow), archtree); gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW (archtree), FALSE); g_signal_connect(G_OBJECT(archtree), "button_press_event", G_CALLBACK(check_list_double), NULL); gtk_widget_show(archtree); // set up our tree store treestore = gtk_tree_store_new(1, G_TYPE_STRING); // attach the store to the tree gtk_tree_view_set_model(GTK_TREE_VIEW(archtree), GTK_TREE_MODEL(treestore)); for (int fn = 0; fn < filelist.size(); fn++) { gtk_tree_store_insert(treestore, &treeiters[fn], NULL, 999999); gtk_tree_store_set(treestore, &treeiters[fn], 0, filelist[fn], -1); } // create a cell renderer using the stock text one renderer = gtk_cell_renderer_text_new(); // create a display column using the renderer column = gtk_tree_view_column_new_with_attributes ("NES file", renderer, "text", 0, NULL); // add the display column and renderer to the tree view gtk_tree_view_append_column(GTK_TREE_VIEW (archtree), column); GtkWidget *archcancel = gtk_widget_new(GTK_TYPE_BUTTON, "label", GTK_STOCK_CANCEL, "halign", GTK_ALIGN_END, "margin-top", 8, "margin-bottom", 8, "margin-right", 8, NULL); gtk_button_set_use_stock(GTK_BUTTON(archcancel), TRUE); gtk_box_pack_start(GTK_BOX(buttonbox), archcancel, FALSE, FALSE, 0); gtk_widget_show(archcancel); GtkWidget *archok = gtk_widget_new(GTK_TYPE_BUTTON, "label", GTK_STOCK_OK, "halign", GTK_ALIGN_END, "margin-top", 8, "margin-bottom", 8, "margin-right", 8, NULL); gtk_button_set_use_stock(GTK_BUTTON(archok), TRUE); gtk_box_pack_start(GTK_BOX(buttonbox), archok, FALSE, FALSE, 0); gtk_widget_show(archok); // get the selection object too selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(archtree)); g_signal_connect(G_OBJECT(archcancel), "clicked", G_CALLBACK(on_archcancel_clicked), NULL); g_signal_connect(G_OBJECT(archok), "clicked", G_CALLBACK(on_archok_clicked), NULL); g_signal_connect(G_OBJECT(archselect), "destroy", G_CALLBACK(on_archselect_destroyed), NULL); gtk_widget_show(archselect); run_picker = true; cancelled = false; while (run_picker) { gtk_main_iteration_do(TRUE); } sel = find_current_selection(); gtk_widget_destroy(archselect); // was something picked? if ((sel != -1) && (!cancelled)) { strcpy(fname, filelist[sel]); } // free all the temp filenames for (int fn = 0; fn < filelist.size(); fn++) { free(filelist[fn]); } // and wipe the vector filelist.clear(); if ((sel != -1) && (!cancelled)) { if (outname) { strcpy(outname, fname); } return fileio_load_archive(filename, dataout, datasize, dataoffset, fname, NULL); } } } return 0; }
int auxio_load_archive(const char *filename, unsigned char **dataout, int *datasize, int *dataoffset, const char *filetoload, char *outname) { FILE *f; unsigned char idbuf[4]; int filesFound = 0; std::vector<char *> filelist; // list of files we can load in this archive // default case: outname is filename if (outname) { strcpy(outname, filename); } f = fopen(filename, "rb"); if (!f) { return 0; // no good } fread(idbuf, 4, 1, f); fclose(f); // printf("ID bytes %c %c %x %x\n", idbuf[0], idbuf[1], idbuf[2], idbuf[3]); if ((idbuf[0] == 'P') && (idbuf[1] == 'K') && (idbuf[2] == 0x03) && (idbuf[3] == 0x04)) { // it's zip unzFile zipArchive; char file_name[1024]; unz_file_info info; int nlen; int ret = 0; zipArchive = unzOpen(filename); if (!zipArchive) { return 0; } unzGoToFirstFile(zipArchive); for (;;) { if (unzGetCurrentFileInfo(zipArchive, &info, file_name, 1024, NULL, 0, NULL, 0) == UNZ_OK) { if (filetoload != NULL) { if (!strcasecmp(filetoload, file_name)) { int length; unsigned char *buffer; unzOpenCurrentFile(zipArchive); length = info.uncompressed_size; buffer = (unsigned char *)malloc(length); ret = unzReadCurrentFile(zipArchive, buffer, length); if (ret != length) { free(buffer); return 0; } unzCloseCurrentFile(zipArchive); unzClose(zipArchive); *datasize = length; *dataout = buffer; *dataoffset = 0; return 1; } } else { if (checkExtension(file_name)) { char *tmpstr; tmpstr = (char *)malloc(strlen(file_name)+1); strcpy(tmpstr, file_name); // add to the file list filelist.push_back(tmpstr); filesFound++; } } } else { break; } ret = unzGoToNextFile(zipArchive); if (ret == UNZ_END_OF_LIST_OF_FILE) { break; } if (ret != UNZ_OK) { unzClose(zipArchive); return 0; } } unzClose(zipArchive); } else if ((idbuf[0] == '7') && (idbuf[1] == 'z') && (idbuf[2] == 0xbc) && (idbuf[3] == 0xaf)) { // it's 7zip CFileInStream archiveStream; SRes res; CSzArEx db; /* 7z archive database structure */ ISzAlloc allocImp; /* memory functions for main pool */ ISzAlloc allocTempImp; /* memory functions for temporary pool */ archiveStream.File = fopen(filename, "rb"); if (!archiveStream.File) { return 0; } archiveStream.InStream.Read = SzFileReadImp; archiveStream.InStream.Seek = SzFileSeekImp; allocImp.Alloc = SzAlloc; allocImp.Free = SzFree; allocTempImp.Alloc = SzAllocTemp; allocTempImp.Free = SzFreeTemp; // init 7zip internals CrcGenerateTable(); SzArEx_Init(&db); res = SzArEx_Open(&db, &archiveStream.InStream, &allocImp, &allocTempImp); if (res == SZ_OK) { int i; for (i = 0; i < db.db.NumFiles; i++) { CSzFileItem *item = db.db.Files + i; if (!item->IsDirectory) { if (filetoload != NULL) { if (!strcasecmp(filetoload, item->Name)) { UInt32 blockIndex = 0xFFFFFFFF; /* it can have any value before first call (if outBuffer = 0) */ Byte *outBuffer = 0; /* it must be 0 before first call for each new archive. */ size_t outBufferSize = 0; /* it can have any value before first call (if outBuffer = 0) */ size_t offset; size_t outSizeProcessed; res = SzAr_Extract(&db, &archiveStream.InStream, i, &blockIndex, &outBuffer, &outBufferSize, &offset, &outSizeProcessed, &allocImp, &allocTempImp); if (res == SZ_OK) { SzArEx_Free(&db, &allocImp); fclose(archiveStream.File); *datasize = (int)outBufferSize; *dataout = (unsigned char *)outBuffer; *dataoffset = (int)offset; return 1; } else { std::cout << "Error extracting 7zip!\n"; } } } if (checkExtension(item->Name)) { char *tmpstr; tmpstr = (char *)malloc(strlen(item->Name)+1); strcpy(tmpstr, item->Name); // add to the file list filelist.push_back(tmpstr); filesFound++; } } } SzArEx_Free(&db, &allocImp); fclose(archiveStream.File); } } else if ((idbuf[0] == 'R') && (idbuf[1] == 'a') && (idbuf[2] == 'r') && (idbuf[3] == '!')) { // it's rar } // if we found any files and weren't forced to load them, handle accordingly if (filesFound) { // only 1 file found, just run it if (filesFound == 1) { char fname[512]; strcpy(fname, filelist[0]); free(filelist[0]); filelist.clear(); strcpy(outname, fname); return auxio_load_archive(filename, dataout, datasize, dataoffset, fname, NULL); } else // multiple files we can handle found, give the user a choice { int sel; char fname[512]; filepicker = create_archselect(); tree = lookup_widget(filepicker, "archtree"); gtk_tree_view_set_fixed_height_mode(GTK_TREE_VIEW (tree), FALSE); g_signal_connect(G_OBJECT(tree), "button_press_event", G_CALLBACK(check_list_double), NULL); // set up our tree store treestore = gtk_tree_store_new(1, G_TYPE_STRING); // attach the store to the tree gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(treestore)); // create a cell renderer using the stock text one renderer = gtk_cell_renderer_text_new(); // create a display column using the renderer column = gtk_tree_view_column_new_with_attributes ("NES file", renderer, "text", 0, NULL); // add the display column and renderer to the tree view gtk_tree_view_append_column (GTK_TREE_VIEW (tree), column); // get the selection object too selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree)); // add the filenames for (int fn = 0; fn < filelist.size(); fn++) { gtk_tree_store_insert(treestore, &treeiters[fn], NULL, 999999); gtk_tree_store_set(treestore, &treeiters[fn], 0, filelist[fn], -1); } gtk_widget_show(filepicker); run_picker = true; cancelled = false; while (run_picker) { gtk_main_iteration_do(FALSE); } sel = find_current_selection(); gtk_widget_destroy(filepicker); // was something picked? if ((sel != -1) && (!cancelled)) { strcpy(fname, filelist[sel]); } // free all the temp filenames for (int fn = 0; fn < filelist.size(); fn++) { free(filelist[fn]); } // and wipe the vector filelist.clear(); if ((sel != -1) && (!cancelled)) { if (outname) { strcpy(outname, fname); } return auxio_load_archive(filename, dataout, datasize, dataoffset, fname, NULL); } } } return 0; }