]> git.pond.sub.org Git - empserver/blob - src/lib/common/ef_verify.c
Virtual selectors
[empserver] / src / lib / common / ef_verify.c
1 /*
2  *  Empire - A multi-player, client/server Internet based war game.
3  *  Copyright (C) 1986-2008, 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 <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include "file.h"
40 #include "misc.h"
41 #include "nsc.h"
42 #include "product.h"
43
44 static void verify_fail(int, int, struct castr *, int, char *, ...)
45     ATTRIBUTE((format (printf, 5, 6)));
46
47 static void
48 verify_fail(int type, int row, struct castr *ca, int idx, char *fmt, ...)
49 {
50     int i;
51     va_list ap;
52
53     /* Find base table of view, if any */
54     for (i = 0; empfile[i].cache == empfile[type].cache; i++) ;
55
56     fprintf(stderr, "%s %s uid %d",
57             EF_IS_GAME_STATE(i) ? "File" : "Config",
58             ef_nameof(type), row);
59     if (ca) {
60         fprintf(stderr, " field %s", ca->ca_name);
61         if (ca->ca_type != NSC_STRINGY && ca->ca_len != 0)
62             fprintf(stderr, "(%d)", idx);
63     }
64     fprintf(stderr, ": ");
65     va_start(ap, fmt);
66     vfprintf(stderr, fmt, ap);
67     va_end(ap);
68     putc('\n', stderr);
69 }
70
71 static int
72 verify_row(int type, int row)
73 {
74     struct castr *ca = ef_cadef(type);
75     struct emptypedstr *row_ref;
76     int i, j, k, n;
77     struct castr *ca_sym;
78     struct valstr val;
79     int ret_val = 0;
80     int flags = ef_flags(type);
81  
82     if (flags & EFF_MEM)
83         row_ref = ef_ptr(type, row); 
84     else {
85         row_ref = malloc(empfile[type].size); 
86         ef_read(type, row, row_ref);
87     }
88
89     if ((flags & EFF_TYPED) && !EF_IS_VIEW(type)) {
90         if (row_ref->ef_type != type || row_ref->uid != row) {
91             verify_fail(type, row, NULL, 0, "header corrupt");
92             ret_val = -1;
93         }
94     }
95
96     for (i = 0; ca[i].ca_name; ++i) {
97         if (ca[i].ca_flags & NSC_EXTRA)
98             continue;
99         n = ca[i].ca_type != NSC_STRINGY ? ca[i].ca_len : 0;
100         j = 0;
101         do {
102             if (ca[i].ca_table == EF_BAD)
103                 continue;
104             /* FIXME use xdeval() */
105             val.val_type = ca[i].ca_type;
106             val.val_cat = NSC_OFF;
107             val.val_as.sym.off = ca[i].ca_off;
108             val.val_as.sym.len = ca[i].ca_len;
109             val.val_as.sym.idx = j;
110             val.val_as.sym.get = ca[i].ca_get;
111             nstr_exec_val(&val, 0, row_ref, NSC_NOTYPE);
112             if (val.val_type != NSC_LONG)
113                 continue;
114             ca_sym = ef_cadef(ca[i].ca_table);
115             if (ca[i].ca_flags & NSC_BITS) {
116                 /* symbol set */
117                 if (CANT_HAPPEN(ca_sym != symbol_ca)) {
118                     ret_val = -1;
119                     continue;
120                 }
121                 for (k = 0; k < (int)sizeof(long) * 8; k++) {
122                     if (val.val_as.lng & (1L << k))
123                         if (!symbol_by_value(1L << k,
124                                              ef_ptr(ca[i].ca_table, 0))) {
125                             verify_fail(type, row, &ca[i], j,
126                                         "bit %d is not in symbol table %s",
127                                         k, ef_nameof(ca[i].ca_table));
128                             ret_val = -1;
129                         }
130                 }
131             } else if (ca[i].ca_table == type && i == 0) {
132                 /* uid */
133                 /* Some files contain zeroed records, cope */
134                 /* TODO tighten this check */
135                 if (val.val_as.lng == 0)
136                     continue;
137                 if (val.val_as.lng != row) {
138                     verify_fail(type, row, &ca[i], j,
139                                 "value is %ld instead of %d",
140                                 val.val_as.lng, row);
141                     ret_val = -1;
142                 }
143
144             } else if (ca_sym == symbol_ca) {
145                 /* symbol */
146                 if (!symbol_by_value(val.val_as.lng,
147                                      ef_ptr(ca[i].ca_table, 0))) {
148                     verify_fail(type, row, &ca[i], j,
149                                 "value %ld is not in symbol table %s",
150                                 val.val_as.lng, ef_nameof(ca[i].ca_table));
151                     ret_val = -1;
152                 }
153             } else {
154                 /* table index */
155                 if (val.val_as.lng >= ef_nelem(ca[i].ca_table)
156                     || val.val_as.lng < -1) {
157                     verify_fail(type, row, &ca[i], j,
158                         "value %ld indexes table %s out of bounds 0..%d",
159                         val.val_as.lng, ef_nameof(ca[i].ca_table),
160                         ef_nelem(ca[i].ca_table));
161                     ret_val = -1;
162                 }
163             }
164         } while (++j < n);
165     } 
166     if (!(flags & EFF_MEM))
167         free(row_ref);
168     return ret_val;
169 }
170
171 int
172 ef_verify()
173 {
174     struct empfile *ep;
175     int retval = 0;
176     int i;
177
178     for (ep = empfile; ep->name; ep++) {
179         if (!ef_cadef(ep->uid))
180             continue;
181         for (i = 0; i < ef_nelem(ep->uid); i++) {
182             retval += verify_row(ep->uid, i);
183         }
184     }
185
186     /* Special checks */
187     for (i = 0; pchr[i].p_sname; i++) {
188         if ((pchr[i].p_type >= 0) == (pchr[i].p_level >= 0)) {
189             fprintf(stderr,
190                 "Config %s uid %d field level doesn't match field type\n",
191                 ef_nameof(EF_PRODUCT), i);
192             retval = -1;
193         }
194     }
195
196     return retval;
197 }