]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
(verify_row): NSC_BITS implies symbol_ca. Oops if violated.
[empserver] / src / lib / common / ef_verify.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2006, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                           Ken Stevens, Steve McClure
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  *  ---
21  *
22  *  See files README, COPYING and CREDITS in the root of the source
23  *  tree for related information and legal notices.  It is expected
24  *  that future projects/authors will amend these files as needed.
25  *
26  *  ---
27  *
28  *  ef_verify.c: Verify game configuration
29  * 
30  *  Known contributors to this file:
31  *     Ron Koenderink, 2005
32  */
33
34 #include <config.h>
35
36 #include <stdio.h>
37 #include <time.h>
38
39 #include "prototypes.h"
40 #include "file.h"
41 #include "nsc.h"
42
43 static int
44 verify_row(int type, int row)
45 {
46     struct castr *ca = ef_cadef(type);
47     void *row_ref;
48     int i, j, k, n;
49     struct castr *ca_sym;
50     struct valstr val;
51     int ret_val = 0; 
52     int in_mem = (ef_flags(type) & EFF_MEM) != 0; 
53  
54     if (in_mem)
55         row_ref = ef_ptr(type, row); 
56     else {
57         row_ref = malloc(empfile[type].size); 
58         ef_read(type, row, row_ref);
59     }
60
61     for (i = 0; ca[i].ca_name; ++i) {
62         if (ca[i].ca_flags & NSC_EXTRA)
63             continue;
64         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
65         j = 0;
66         do {
67             if (ca[i].ca_table == EF_BAD || ca[i].ca_table == type)
68                 continue;
69             val.val_type = ca[i].ca_type;
70             val.val_cat = NSC_OFF;
71             val.val_as.sym.off = ca[i].ca_off;
72             val.val_as.sym.idx = j;
73             nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE);
74             if (val.val_type != NSC_LONG && val.val_type != NSC_TYPEID)
75                 continue;
76             ca_sym = ef_cadef(ca[i].ca_table);
77             if (ca[i].ca_flags & NSC_BITS) {
78                 /* symbol set */
79                 if (CANT_HAPPEN(ca_sym != symbol_ca)) {
80                     ret_val = -1;
81                     continue;
82                 }
83                 for (k = 0; k < (int)sizeof(long) * 8; k++) {
84                     if (val.val_as.lng & (1L << k))
85                         if (!symbol_by_value(1L << k, ef_ptr(ca[i].ca_table, 0))) {
86                             fprintf(stderr,
87                                     "bit %d not found in symbol table %s "
88                                     "when verify table %s row %d field %s\n",
89                                     k, ef_nameof(ca[i].ca_table),
90                                     ef_nameof(type), row + 1, ca[i].ca_name);
91                             ret_val = -1;
92                         }
93                 }
94             } else if (ca_sym == symbol_ca) {
95                 /* symbol */
96                 if (val.val_as.lng > -1) {
97                     if (!symbol_by_value(val.val_as.lng,
98                                          ef_ptr(ca[i].ca_table,0))) {
99                         fprintf(stderr, "value %ld not found in "
100                                 "symbol table %s when verify table %s "
101                                 "row %d field %s\n",
102                                 val.val_as.lng,
103                                 ef_nameof(ca[i].ca_table),
104                                 ef_nameof(type), row + 1,
105                                 ca[i].ca_name);
106                         ret_val = -1;
107                     }
108                 }
109             } else {
110                 /* table index */
111                 if (val.val_as.lng >= ef_nelem(ca[i].ca_table) ||
112                     val.val_as.lng < -2) {
113                     fprintf(stderr, "Table index %ld to table %s "
114                             "out of range, nelements %d for table %s "
115                             "row %d field %s\n",
116                             val.val_as.lng, ef_nameof(ca[i].ca_table),
117                             ef_nelem(ca[i].ca_table), ef_nameof(type), 
118                             row + 1, ca[i].ca_name);
119                     ret_val = -1;
120                 }
121             }
122         } while (++j < n);
123     } 
124     if (!in_mem)
125         free(row_ref);
126     return ret_val;
127 }
128
129 int
130 ef_verify()
131 {
132     struct empfile *ep;
133     int retval = 0;
134     int i;
135
136     for (ep = empfile; ep->name; ep++) {
137         if (!ef_cadef(ep->uid))
138             continue;
139         for (i = 0; i < ef_nelem(ep->uid); i++) {
140             retval += verify_row(ep->uid, i);
141         }
142     }
143     return retval;
144 }