int main (int argc, char *argv[]) { Grf *grf; GrfError error; if (!argv[1]) usage (2); grf = grf_open (argv[1], &error); if (!grf) { fprintf (stderr, "Error: %s\n", grf_strerror (error)); return 1; } if (argv[2]) { unsigned long i; char *file = NULL; void *data; if (argv[3] && strcmp (argv[3], "--ends") == 0) { for (i = 0; i < grf->nfiles; i++) { if (str_has_suffix (grf->files[i].name, argv[2])) { file = grf->files[i].name; break; } } if (!file) { fprintf (stderr, "Error: %s\n", grf_strerror (GE_NOTFOUND)); return 1; } } else file = argv[2]; data = grf_get (grf, file, &i, &error); if (data) fwrite (data, 1, i, stdout); else { fprintf (stderr, "Error extracting %s: %s\n", file, grf_strerror (error)); return 1; } } else { unsigned long i; for (i = 0; i < grf->nfiles; i++) { printf ("%s\n", grf->files[i].name); } } grf_free (grf); return 0; }
int main(int argc, char *argv[]) { void *grf; if (argc != 2) { fprintf(stderr, "Arguments: %s out_file\n", argv[0]); return 1; } grf = grf_new(argv[1], true); if (grf == NULL) { fprintf(stderr, "Could not write to %s\n", argv[1]); return 2; } recurse_scan_add(grf, "data"); grf_free(grf); return 0; }
BOOL main_unpack(CHAR *patch_file, struct MemoryStruct *patch, CHAR *grf) { CHAR mes[256]; CMemInStream memory_stream; CLookToRead lookStream; CSzArEx db; SRes res; ISzAlloc allocImp; ISzAlloc allocTempImp; memory_stream.buf = patch->memory; memory_stream.ptr = memory_stream.buf; memory_stream.size = patch->size; memory_stream.s.Read = &lzma_memory_read; memory_stream.s.Seek = &lzma_memory_seek; sprintf_s(mes, sizeof(mes), "Распаковка: %s...", patch_file); dialog_set_status(mes); LookToRead_CreateVTable(&lookStream, False); lookStream.realStream = &memory_stream.s; LookToRead_Init(&lookStream); allocImp.Alloc = SzAlloc; allocImp.Free = SzFree; allocTempImp.Alloc = SzAllocTemp; allocTempImp.Free = SzFreeTemp; SzArEx_Init(&db); res = SzArEx_Open(&db, &lookStream.s, &allocImp, &allocTempImp); if (res == SZ_OK) { UInt32 i; UInt32 blockIndex = 0xFFFFFFFF; Byte *outBuffer = 0; size_t outBufferSize = 0; void *grf_handle; // if we are working with grf we should // open grf-file first if (grf != NULL) { grf_handle = grf_load(grf, TRUE); if (grf_handle == NULL) grf_handle = grf_new(grf, TRUE); if (grf_handle == NULL) { SzArEx_Free(&db, &allocImp); sprintf_s(mes, sizeof(mes), "Невозможно ни открыть, ни создать GRF-файл: %s", grf); dialog_set_status(mes); return RES_FAIL; } } for (i = 0; i < db.db.NumFiles; i++) { BOOL res; size_t offset; size_t outSizeProcessed; CSzFileItem *f = db.db.Files + i; if (!f->IsDir) { res = SzAr_Extract(&db, &lookStream.s, i, &blockIndex, &outBuffer, &outBufferSize, &offset, &outSizeProcessed, &allocImp, &allocTempImp); if (res != SZ_OK) break; } res = (grf != NULL) ? main_target_grf(grf_handle, (WCHAR *)f->Name, f->IsDir, outBuffer + offset, outSizeProcessed) : main_target_fs((WCHAR *)f->Name, f->IsDir, outBuffer + offset, outSizeProcessed); if (res) { IAlloc_Free(&allocImp, outBuffer); SzArEx_Free(&db, &allocImp); return RES_FAIL; } } // if we're working with grf // close it if (grf != NULL) grf_free(grf_handle); IAlloc_Free(&allocImp, outBuffer); } SzArEx_Free(&db, &allocImp); if (res != SZ_OK) { if (res == SZ_ERROR_UNSUPPORTED) sprintf_s(mes, sizeof(mes), "Ошибка распаковки %s: Архив не поддерживается", patch_file); else if (res == SZ_ERROR_MEM) sprintf_s(mes, sizeof(mes), "Ошибка распаковки %s: Невозможно зарезервировать память", patch_file); else if (res == SZ_ERROR_CRC) sprintf_s(mes, sizeof(mes), "Ошибка распаковки %s: ошибка CRC", patch_file); else sprintf_s(mes, sizeof(mes), "Ошибка распаковки %s: ошибка #%d", patch_file, res); dialog_set_status(mes); return RES_FAIL; } return RES_OK; }
void open_grf_file (const char *fname) { char *title, *tmp; GrfError err; Grf *newgrf; GList *list, *cols; if (!g_file_test (fname, G_FILE_TEST_EXISTS)) { show_error (_("File %s does not exist."), fname); return; } mainWin.busy (true); mainWin.status (_("Loading...")); while (gtk_events_pending ()) gtk_main_iteration (); newgrf = grf_open (fname, "r", &err); if (!newgrf) { char *base; base = g_path_get_basename (fname); mainWin.status (""); gdk_window_set_cursor (W(main)->window, NULL); show_error (_("Error while opening %s:\n%s"), base, grf_strerror (err)); g_free (base); return; } if (document.grf) grf_free (document.grf); document.grf = newgrf; document.filename = fname; title = g_strdup_printf (_("%s - GRF Tool"), fname); gtk_window_set_title (GTK_WINDOW (W(main)), title); g_free (title); gtk_list_store_clear (GTK_LIST_STORE (filelist)); cols = gtk_tree_view_get_columns (GTK_TREE_VIEW (W(filelist))); for (list = cols; list; list = list->next) { GtkTreeViewColumn *col = (GtkTreeViewColumn *) list->data; gtk_tree_view_column_set_sort_indicator (col, FALSE); } gtk_tree_sortable_set_sort_column_id (GTK_TREE_SORTABLE (filelist), GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID, GTK_SORT_ASCENDING); g_list_free (cols); fill_filelist (); tmp = g_path_get_basename (fname); title = g_strdup_printf (_("%s: %ld files"), tmp, (long int)document.grf->nfiles); mainWin.status (title); g_free (tmp); g_free (title); gtk_widget_set_sensitive (W(extract), TRUE); mainWin.busy (false); }