diff --git a/include/file.h b/include/file.h index 42c979dd..ff409e3b 100644 --- a/include/file.h +++ b/include/file.h @@ -163,6 +163,7 @@ extern int ef_byname(char *); extern int ef_byname_from(char *, int *); extern void ef_init(void); extern int ef_load(void); +extern int ef_verify(void); extern struct empfile empfile[EF_MAX + 1]; diff --git a/include/prototypes.h b/include/prototypes.h index 2d93d1af..d6dcedc1 100644 --- a/include/prototypes.h +++ b/include/prototypes.h @@ -285,6 +285,8 @@ extern int check_loan_ok(struct lonstr *); extern int check_trade_ok(struct trdstr *); /* ef_load.c */ /* in file.h */ +/* ef_verify.c */ +/* in file.h */ /* fsize.c */ extern int fsize(int); extern int blksize(int); diff --git a/src/lib/common/Makefile b/src/lib/common/Makefile index 912482ed..c6802c2a 100644 --- a/src/lib/common/Makefile +++ b/src/lib/common/Makefile @@ -35,16 +35,17 @@ include ../../make.defs LIB = $(SRCDIR)/lib/libcommon.a NTLIB = $(SRCDIR)\lib\libcommon.lib -OBJS = bestpath.o bridgefall.o check.o damage.o file.o fsize.o \ - hap_fact.o hours.o land.o log.o mailbox.o maps.o move.o \ - nat.o path.o res_pop.o sectdamage.o \ - stmtch.o tfact.o type.o wantupd.o xy.o ef_load.o xundump.o +OBJS = bestpath.o bridgefall.o check.o damage.o ef_load.o ef_verify.o \ + file.o fsize.o hap_fact.o hours.o land.o log.o mailbox.o \ + maps.o move.o nat.o path.o res_pop.o sectdamage.o \ + stmtch.o tfact.o type.o wantupd.o xundump.o xy.o -NTOBJS = bestpath.obj bridgefall.obj check.obj damage.obj file.obj fsize.obj \ +NTOBJS = bestpath.obj bridgefall.obj check.obj damage.obj \ + ef_load.obj ef_verify.obj file.obj fsize.obj \ hap_fact.obj hours.obj land.obj log.obj \ mailbox.obj maps.obj move.obj nat.obj path.obj res_pop.obj \ sectdamage.obj stmtch.obj tfact.obj \ - type.obj wantupd.obj xy.obj ef_load.obj xundump.obj + type.obj wantupd.obj xundump.obj xy.obj all: $(LIB) diff --git a/src/lib/common/ef_verify.c b/src/lib/common/ef_verify.c new file mode 100644 index 00000000..ab1e54d2 --- /dev/null +++ b/src/lib/common/ef_verify.c @@ -0,0 +1,150 @@ +/* + * Empire - A multi-player, client/server Internet based war game. + * Copyright (C) 1986-2005, Dave Pare, Jeff Bailey, Thomas Ruschak, + * Ken Stevens, Steve McClure + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * --- + * + * See the "LEGAL", "LICENSE", "CREDITS" and "README" files for all the + * related information and legal notices. It is expected that any future + * projects/authors will amend these files as needed. + * + * --- + * + * ef_verify.c: Verify game configuration + * + * Known contributors to this file: + * Ron Koenderink, 2005 + * + */ + +#include +#include + +#include "prototypes.h" +#include "file.h" +#include "nsc.h" + +static int +verify_row(int type, int row) +{ + struct castr *ca = ef_cadef(type); + void *row_ref = malloc(empfile[type].size); + int i, j, k, n; + struct castr *ca_sym; + struct valstr val; + int ret_val = 0; + int in_mem = (ef_flags(type) & EFF_MEM) != 0; + + + if (!in_mem) { + row_ref = malloc(empfile[type].size); + ef_read(type, row, row_ref); + } else + row_ref = ef_ptr(type, row); + +// if (ef_flags(type) & EFF_OWNER) != 0 + for (i = 0; ca[i].ca_name; ++i) { + if (ca[i].ca_flags & NSC_EXTRA) + continue; + n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0; + j = 0; + do { + if (ca[i].ca_table != EF_BAD && ca[i].ca_table != type) { + val.val_type = ca[i].ca_type; + val.val_cat = NSC_OFF; + val.val_as.sym.off = ca[i].ca_off; + val.val_as.sym.idx = j; + nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE); + if (val.val_type != NSC_LONG && val.val_type != NSC_TYPEID) + continue; + + ca_sym = ef_cadef(ca[i].ca_table); + if (ca[i].ca_flags & NSC_BITS) { + if (ca_sym != symbol_ca) { + fprintf(stderr, + "Unable to verify symbol set as the " + "table %s is not created as symbol table " + "for table %s row %d field %s\n", + ef_nameof(ca[i].ca_table), ef_nameof(type), + row + 1, ca[i].ca_name + ); + continue; + } + for (k = 0; k < sizeof(long) * 8; k++) { + if (val.val_as.lng & (1 << k)) + if (!symbol_by_value(1 << k, ef_ptr(ca[i].ca_table, 0))) { + fprintf(stderr, + "bit %d not found in symbol table %s " + "when verify table %s row %d field %s\n", + k, ef_nameof(ca[i].ca_table), + ef_nameof(type), row + 1, ca[i].ca_name); + ret_val = -1; + } + } + } else { + if (ca_sym != symbol_ca) { + if (val.val_as.lng >= ef_nelem(ca[i].ca_table) || + val.val_as.lng < -2) { + fprintf(stderr, "Table index %d to table %s " + "out of range, nelements %d for table %s " + "row %d field %s\n", + val.val_as.lng, ef_nameof(ca[i].ca_table), + ef_nelem(ca[i].ca_table), ef_nameof(type), + row + 1, ca[i].ca_name); + ret_val = -1; + } + } else { + if (val.val_as.lng > -1) { + if (!symbol_by_value(val.val_as.lng, + ef_ptr(ca[i].ca_table,0))) { + fprintf(stderr, "value %d not found in " + "symbol table %s when verify table %s " + "row %d field %s\n", + val.val_as.lng, + ef_nameof(ca[i].ca_table), + ef_nameof(type), row + 1, + ca[i].ca_name); + ret_val = -1; + } + } + } + } + } + } while (++j < n); + } + if (!in_mem) + free(row_ref); + return(ret_val); +} + +int +ef_verify() +{ + struct empfile *ep; + int retval = 0; + int i; + + for (ep = empfile; ep->name; ep++) { + if (!ef_cadef(ep->uid)) + continue; + for (i = 0; i < ef_nelem(ep->uid); i++) { + retval += verify_row(ep->uid, i); + } + } + return retval; +} diff --git a/src/lib/subs/fileinit.c b/src/lib/subs/fileinit.c index e34dfd75..7f1dc2b0 100644 --- a/src/lib/subs/fileinit.c +++ b/src/lib/subs/fileinit.c @@ -96,6 +96,8 @@ ef_init_srv(void) } ef_init(); ef_open_srv(); + if (ef_verify() < 0) + exit(EXIT_FAILURE); global_init(); }