]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
Don't misinterpret blank configuration entries as sentinels
[empserver] / src / lib / common / ef_verify.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2011, Dave Pare, Jeff Bailey, Thomas Ruschak,
4  *                Ken Stevens, Steve McClure, Markus Armbruster
5  *
6  *  Empire 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 3 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, see <http://www.gnu.org/licenses/>.
18  *
19  *  ---
20  *
21  *  See files README, COPYING and CREDITS in the root of the source
22  *  tree for related information and legal notices.  It is expected
23  *  that future projects/authors will amend these files as needed.
24  *
25  *  ---
26  *
27  *  ef_verify.c: Verify game configuration
28  *
29  *  Known contributors to this file:
30  *     Ron Koenderink, 2005
31  *     Markus Armbruster, 2006-2010
32  */
33
34 #include <config.h>
35
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "file.h"
40 #include "misc.h"
41 #include "nsc.h"
42 #include "plane.h"
43 #include "product.h"
44
45 static void verify_fail(int, int, struct castr *, int, char *, ...)
46     ATTRIBUTE((format (printf, 5, 6)));
47
48 static void
49 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
50 {
51     int base = empfile[type].base < 0 ? type : empfile[type].base;
52     va_list ap;
53
54     fprintf(stderr, "%s %s uid %d",
55             EF_IS_GAME_STATE(base) ? "File" : "Config",
56             ef_nameof(type), row);
57     if (ca) {
58         fprintf(stderr, " field %s", ca->ca_name);
59         if (ca->ca_type != NSC_STRINGY && ca->ca_len != 0)
60             fprintf(stderr, "(%d)", idx);
61     }
62     fprintf(stderr, ": ");
63     va_start(ap, fmt);
64     vfprintf(stderr, fmt, ap);
65     va_end(ap);
66     putc('\n', stderr);
67 }
68
69 static int
70 verify_ca(int type)
71 {
72     struct castr *ca = ef_cadef(type);
73     int i;
74
75     for (i = 0; ca[i].ca_name; i++) {
76         /*
77          * Virtual selectors must be NSC_EXTRA, because xundump can't
78          * cope with them without setter methods.  Exception: if
79          * EFF_MEM is not set, xundump doesn't touch the table.
80          */
81         if (CANT_HAPPEN((ef_flags(type) & EFF_MEM)
82                         && ca[i].ca_get && !(ca[i].ca_flags & NSC_EXTRA)))
83             ca[i].ca_flags |= NSC_EXTRA;
84     }
85     return 0;
86 }
87
88 static int
89 verify_row(int type, int row)
90 {
91     struct castr *ca = ef_cadef(type);
92     struct emptypedstr *row_ref;
93     int i, j, k, n;
94     struct castr *ca_sym;
95     struct valstr val;
96     int ret_val = 0;
97     int flags = ef_flags(type);
98
99     if (flags & EFF_MEM)
100         row_ref = ef_ptr(type, row);
101     else {
102         row_ref = malloc(empfile[type].size);
103         ef_read(type, row, row_ref);
104     }
105
106     if ((flags & EFF_TYPED) && !EF_IS_VIEW(type)) {
107         if (row_ref->ef_type != type || row_ref->uid != row) {
108             verify_fail(type, row, NULL, 0, "header corrupt");
109             ret_val = -1;
110         }
111     }
112
113     for (i = 0; ca[i].ca_name; ++i) {
114         if (ca[i].ca_get)
115             continue;           /* virtual */
116         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
117         j = 0;
118         do {
119             if (ca[i].ca_table == EF_BAD)
120                 continue;
121             nstr_mksymval(&val, &ca[i], j);
122             nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE);
123             if (CANT_HAPPEN(val.val_type != NSC_LONG)) {
124                 ret_val = -1;
125                 continue;
126             }
127             ca_sym = ef_cadef(ca[i].ca_table);
128             if (ca[i].ca_flags & NSC_BITS) {
129                 /* symbol set */
130                 if (CANT_HAPPEN(ca_sym != symbol_ca)) {
131                     ret_val = -1;
132                     continue;
133                 }
134                 for (k = 0; k < (int)sizeof(long) * 8; k++) {
135                     if (val.val_as.lng & (1L << k))
136                         if (!symbol_by_value(1L << k,
137                                              ef_ptr(ca[i].ca_table, 0))) {
138                             verify_fail(type, row, &ca[i], j,
139                                         "bit %d is not in symbol table %s",
140                                         k, ef_nameof(ca[i].ca_table));
141                             ret_val = -1;
142                         }
143                 }
144             } else if (ca[i].ca_table == type && i == 0) {
145                 /* uid */
146                 /* Some files contain zeroed records, cope */
147                 /* TODO tighten this check */
148                 if (val.val_as.lng == 0)
149                     continue;
150                 if (val.val_as.lng != row) {
151                     verify_fail(type, row, &ca[i], j,
152                                 "value is %ld instead of %d",
153                                 val.val_as.lng, row);
154                     ret_val = -1;
155                 }
156
157             } else if (ca_sym == symbol_ca) {
158                 /* symbol */
159                 if (!symbol_by_value(val.val_as.lng,
160                                      ef_ptr(ca[i].ca_table, 0))) {
161                     verify_fail(type, row, &ca[i], j,
162                                 "value %ld is not in symbol table %s",
163                                 val.val_as.lng, ef_nameof(ca[i].ca_table));
164                     ret_val = -1;
165                 }
166             } else {
167                 /* table index */
168                 if (val.val_as.lng >= ef_nelem(ca[i].ca_table)
169                     || val.val_as.lng < -1) {
170                     verify_fail(type, row, &ca[i], j,
171                         "value %ld indexes table %s out of bounds 0..%d",
172                         val.val_as.lng, ef_nameof(ca[i].ca_table),
173                         ef_nelem(ca[i].ca_table));
174                     ret_val = -1;
175                 }
176             }
177         } while (++j < n);
178     }
179     if (!(flags & EFF_MEM))
180         free(row_ref);
181     return ret_val;
182 }
183
184 static void
185 pln_zap_transient_flags(void)
186 {
187     int i;
188     struct plnstr *pp;
189     int dirty = 0;
190
191     /* laziness: assumes plane file is EFF_MEM */
192     for (i = 0; (pp = getplanep(i)) != NULL; i++) {
193         if (!pp->pln_own)
194             continue;
195         if (pp->pln_flags & PLN_LAUNCHED
196             && (plchr[pp->pln_type].pl_flags & (P_M | P_O)) != P_O) {
197             pp->pln_flags &= ~PLN_LAUNCHED;
198             /* FIXME missile should be destroyed instead */
199             dirty = 1;
200             verify_fail(EF_PLANE, i, NULL, 0, "stuck in the air (fixed)");
201             /*
202              * Can't putplane() here, because pln_prewrite() crashes
203              * without a valid player.
204              */
205         }
206     }
207     if (dirty)
208         ef_flush(EF_PLANE);     /* pretty wasteful */
209 }
210
211 int
212 ef_verify(void)
213 {
214     struct empfile *ep;
215     int retval = 0;
216     int i;
217
218     for (ep = empfile; ep->name; ep++) {
219         if (!ef_cadef(ep->uid))
220             continue;
221         verify_ca(ep->uid);
222         for (i = 0; i < ef_nelem(ep->uid); i++) {
223             retval += verify_row(ep->uid, i);
224         }
225     }
226
227     /* Special checks */
228     for (i = 0; pchr[i].p_sname; i++) {
229         if (!pchr[i].p_sname[0])
230             continue;
231         if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
232             fprintf(stderr,
233                 "Config %s uid %d field level doesn't match field type\n",
234                 ef_nameof(EF_PRODUCT), i);
235             retval = -1;
236         }
237     }
238
239     pln_zap_transient_flags();
240     return retval;
241 }