Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/tools/gen/abstract/recheck.c # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / tools / gen / abstract / recheck.c` 515 lines ยท 22884 bytes Raw Blame History 1 /* 2 * recheck.c -- strict validator of GEN records, schema v1 (tools/gen/abstract/RECORD_SCHEMA.md; GEN-3, 3 * aien-spark/omega#478). Format and internal consistency only; it runs no program. `verify` re-runs the 4 * derivation; this tool is the first gate of it and of every consumer. 5 * 6 * recheck [--root ] [--receipts ] ... 7 * prints "OK " per accepted record, or "REFUSED : line : " (first defect only); 8 * with --root, every `source` digest must equal the sha256 of / (a stale digest is refused); 9 * with --receipts, / must exist, start "gen-receipt 1" / "record " and hash 10 * to its name. 11 * exit 0 every record accepted, 1 any refused, 2 usage. 12 */ 13 #include 14 #include 15 #include 16 17 #include "sha256.h" 18 19 #define MAXB 65536 20 #define MAXL 1024 21 #define MAXLINES 2048 22 23 static char buf[MAXB + 2]; 24 static char *ln[MAXLINES]; 25 static int nl; 26 static const char *g_file; 27 static char g_err[512]; 28 static int g_errline; 29 30 static int fail(int line, const char *fmt, const char *a) 31 { 32 if (!g_err[0]) { 33 g_errline = line; 34 snprintf(g_err, sizeof g_err, fmt, a ? a : ""); 35 } 36 return -1; 37 } 38 39 static int is_hex64(const char *s) 40 { 41 for (int i = 0; i < 64; i++) if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) return 0; 42 return s[64] == 0; 43 } 44 45 static int is_taskid(const char *s, size_t n) 46 { 47 if (n < 1 || n > 40) return 0; 48 for (size_t i = 0; i < n; i++) 49 if (!((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9') || s[i] == '_' || s[i] == '-')) return 0; 50 return 1; 51 } 52 53 static const char *const SCALARS[] = {"bool", "uchar", "ushort", "uint", "ulong", "byte", "short", "int", "long"}; 54 static const char *const RESERVED[] = {"fn", "to", "void", "end", "if", "else", "while", "bound", "for", "break", "ret", 55 "put", "free", "trap", "add", "sub", "mul", "div", "rem", "and", "or", "xor", "shl", "shr", "eq", "ne", "lt", "le", 56 "gt", "ge", "neg", "not", "get", "new", "own", "edit", "view", "true", "false", "u8", "u16", "u32", "u64", 57 "i8", "i16", "i32", "i64"}; 58 59 static int is_scalar(const char *w) 60 { 61 for (size_t i = 0; i < sizeof SCALARS / sizeof *SCALARS; i++) if (!strcmp(w, SCALARS[i])) return 1; 62 return 0; 63 } 64 65 static int is_ident(const char *w) 66 { 67 size_t n = strlen(w); 68 if (n < 1 || n > 63) return 0; 69 if (!((w[0] >= 'a' && w[0] <= 'z') || (w[0] >= 'A' && w[0] <= 'Z') || w[0] == '_')) return 0; 70 for (size_t i = 1; i < n; i++) 71 if (!((w[i] >= 'a' && w[i] <= 'z') || (w[i] >= 'A' && w[i] <= 'Z') || (w[i] >= '0' && w[i] <= '9') || w[i] == '_')) return 0; 72 if (is_scalar(w)) return 0; 73 for (size_t i = 0; i < sizeof RESERVED / sizeof *RESERVED; i++) if (!strcmp(w, RESERVED[i])) return 0; 74 return 1; 75 } 76 77 /* split a copy of s on single spaces into w (at most max words); returns the count */ 78 static int words(const char *s, char *store, size_t storen, char **w, int max) 79 { 80 size_t n = strlen(s); 81 if (n + 1 > storen) return -1; 82 memcpy(store, s, n + 1); 83 int k = 0; 84 char *p = store; 85 while (*p) { 86 if (k == max) return -1; 87 w[k++] = p; 88 char *q = strchr(p, ' '); 89 if (!q) break; 90 *q = 0; 91 p = q + 1; 92 } 93 return k; 94 } 95 96 static int is_len(const char *w) 97 { 98 if (!w[0] || (w[0] == '0') || strlen(w) > 2) return 0; 99 for (const char *p = w; *p; p++) if (*p < '0' || *p > '9') return 0; 100 int v = atoi(w); 101 return v >= 1 && v <= 64; 102 } 103 104 /* a type at w[i]: scalar (1 word) or view|edit|own scalar len (3 words); returns words used or 0 */ 105 static int type_at(char **w, int n, int i) 106 { 107 if (i >= n) return 0; 108 if (is_scalar(w[i])) return 1; 109 if ((!strcmp(w[i], "view") || !strcmp(w[i], "edit") || !strcmp(w[i], "own")) && i + 2 < n && is_scalar(w[i + 1]) && is_len(w[i + 2])) 110 return 3; 111 return 0; 112 } 113 114 typedef struct { 115 char name[64]; 116 int np; 117 char pname[6][64]; 118 char ptype[6][32]; /* the type words joined by single spaces */ 119 int plen[6]; /* 0 scalar, else array length */ 120 char ret[32]; 121 } Sig; 122 123 /* parse an Omegese fn line (as in the body) */ 124 static int parse_sig(const char *s, Sig *g, int line) 125 { 126 char st[MAXL + 1], *w[64]; 127 int n = words(s, st, sizeof st, w, 64); 128 memset(g, 0, sizeof *g); 129 if (n < 4 || strcmp(w[0], "fn")) return fail(line, "signature is not 'fn ( )* to '", NULL); 130 if (!is_ident(w[1])) return fail(line, "function name '%s' is not an identifier", w[1]); 131 snprintf(g->name, sizeof g->name, "%s", w[1]); 132 int i = 2; 133 while (i < n && strcmp(w[i], "to")) { 134 if (g->np == 6) return fail(line, "more than 6 parameters", NULL); 135 if (!is_ident(w[i])) return fail(line, "parameter name '%s' is not an identifier (or is reserved)", w[i]); 136 for (int k = 0; k < g->np; k++) if (!strcmp(g->pname[k], w[i])) return fail(line, "parameter name '%s' repeated", w[i]); 137 if (!strcmp(w[i], g->name)) return fail(line, "parameter '%s' has the function's name", w[i]); 138 snprintf(g->pname[g->np], 64, "%s", w[i]); 139 int t = type_at(w, n, i + 1); 140 if (!t) return fail(line, "parameter '%s' has no valid type", w[i]); 141 if (t == 1) snprintf(g->ptype[g->np], 32, "%s", w[i + 1]); 142 else { snprintf(g->ptype[g->np], 32, "%s %s %s", w[i + 1], w[i + 2], w[i + 3]); g->plen[g->np] = atoi(w[i + 3]); } 143 g->np++; 144 i += 1 + t; 145 } 146 if (i >= n) return fail(line, "signature has no 'to '", NULL); 147 i++; 148 if (i < n && !strcmp(w[i], "void") && i + 1 == n) { snprintf(g->ret, sizeof g->ret, "void"); return 0; } 149 int t = type_at(w, n, i); 150 if (t != 1 || i + 1 != n) return fail(line, "return type must be one scalar type or void, and end the line", NULL); 151 snprintf(g->ret, sizeof g->ret, "%s", w[i]); 152 return 0; 153 } 154 155 static int is_int(const char *w) 156 { 157 const char *p = w; 158 if (*p == '-') p++; 159 if (!*p || (p[0] == '0' && p[1]) || strlen(p) > 20) return 0; 160 if (w[0] == '-' && !strcmp(p, "0")) return 0; 161 for (; *p; p++) if (*p < '0' || *p > '9') return 0; 162 return 1; 163 } 164 165 /* " -> " or " -> TRAP "; nargs expected; trap_ok allows TRAP */ 166 static int parse_call(const char *s, int nargs, int trap_ok, int line, const char *what) 167 { 168 char st[MAXL + 1], *w[80]; 169 int n = words(s, st, sizeof st, w, 80); 170 if (n < 0) return fail(line, "%s has too many words", what); 171 int a = 0; 172 while (a < n && strcmp(w[a], "->")) { 173 if (!is_int(w[a])) return fail(line, "%s argument is not a decimal number", what); 174 a++; 175 } 176 if (a != nargs) return fail(line, "%s has the wrong number of arguments for the signature", what); 177 if (a >= n) return fail(line, "%s has no '-> '", what); 178 if (a + 2 == n && is_int(w[a + 1])) return 0; 179 if (trap_ok && a + 3 == n && !strcmp(w[a + 1], "TRAP") && is_int(w[a + 2]) && atoi(w[a + 2]) >= 1 && atoi(w[a + 2]) <= 14 && w[a + 2][0] != '-') 180 return 0; 181 return fail(line, trap_ok ? "%s result must be a decimal number or TRAP <1..14>" : "%s result must be a decimal number", what); 182 } 183 184 static int key(int i, const char *k, const char **val) 185 { 186 size_t n = strlen(k); 187 if (i >= nl || strncmp(ln[i], k, n) || ln[i][n] != ' ') return 0; 188 *val = ln[i] + n + 1; 189 return 1; 190 } 191 192 static int textline(const char *v, int line, const char *what) 193 { 194 if (strlen(v) < 1 || strlen(v) > 160) return fail(line, "%s must be 1..160 bytes", what); 195 return 0; 196 } 197 198 static void hexs(const uint8_t d[32], char out[65]) 199 { 200 static const char h[] = "`0123456789abcdef`"; 201 for (int i = 0; i < 32; i++) { out[2 * i] = h[d[i] >> 4]; out[2 * i + 1] = h[d[i] & 15]; } 202 out[64] = 0; 203 } 204 205 static int file_sha(const char *path, char out[65]) 206 { 207 FILE *f = fopen(path, "rb"); 208 if (!f) return -1; 209 sha256_ctx c; 210 sha256_init(&c); 211 uint8_t b[8192]; 212 size_t r; 213 while ((r = fread(b, 1, sizeof b, f)) > 0) sha256_update(&c, b, r); 214 int bad = ferror(f); 215 fclose(f); 216 if (bad) return -1; 217 uint8_t d[32]; 218 sha256_final(&c, d); 219 hexs(d, out); 220 return 0; 221 } 222 223 static int safe_path(const char *p) 224 { 225 size_t n = strlen(p); 226 if (n < 1 || n > 255 || p[0] == '/') return 0; 227 for (size_t i = 0; i < n; i++) 228 if (!((p[i] >= 'a' && p[i] <= 'z') || (p[i] >= 'A' && p[i] <= 'Z') || (p[i] >= '0' && p[i] <= '9') || strchr("_./-", p[i]))) return 0; 229 const char *s = p; 230 while (s) { 231 const char *e = strchr(s, '/'); 232 size_t m = e ? (size_t)(e - s) : strlen(s); 233 if (m == 0 || (m == 1 && s[0] == '.') || (m == 2 && s[0] == '.' && s[1] == '.')) return 0; 234 s = e ? e + 1 : NULL; 235 } 236 return 1; 237 } 238 239 /* one byte of a name that safe_path or is_hex64 has already accepted; anything else becomes '_' */ 240 static int name_byte(int c) 241 { 242 return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == '.' || c == '/' || c == '-' ? c : '_'; 243 } 244 245 /* out = dir "/" name, the name copied byte by byte through name_byte (the path holds only checked bytes). 246 Returns -1 when it does not fit. */ 247 static int join_path(char *out, size_t cap, const char *dir, const char *name) 248 { 249 int r = snprintf(out, cap, "%s/", dir); 250 if (r < 0 || (size_t)r >= cap) return -1; 251 size_t k = (size_t)r; 252 for (size_t i = 0; name[i]; i++) { 253 if (k + 1 >= cap) return -1; 254 out[k++] = (char)name_byte((unsigned char)name[i]); 255 } 256 out[k] = 0; 257 return 0; 258 } 259 260 static const char *irname(const char *s) 261 { 262 static const char *const ir[] = {"bool", "u8", "u16", "u32", "u64", "i8", "i16", "i32", "i64"}; 263 for (size_t i = 0; i < sizeof SCALARS / sizeof *SCALARS; i++) if (!strcmp(s, SCALARS[i])) return ir[i]; 264 return "?"; 265 } 266 267 /* family line: gs1-<16 hex> ids, single spaces, strictly ascending */ 268 static int famlist(const char *v, int line) 269 { 270 (void)line; 271 char prev[32] = ""; 272 int n = 0; 273 while (*v) { 274 const char *e = strchr(v, ' '); 275 size_t m = e ? (size_t)(e - v) : strlen(v); 276 if (m != 20 || strncmp(v, "gs1-", 4)) return -1; 277 for (size_t k = 4; k < 20; k++) if (!strchr("`0123456789abcdef`", v[k])) return -1; 278 char cur[32]; 279 snprintf(cur, sizeof cur, "%.*s", (int)m, v); 280 if (n && strcmp(prev, cur) >= 0) return -1; 281 memcpy(prev, cur, sizeof cur); 282 n++; 283 if (!e) break; 284 v = e + 1; 285 if (!*v) return -1; 286 } 287 return n ? 0 : -1; 288 } 289 290 static int check(const char *root, const char *rdir) 291 { 292 const char *v; 293 int i = 0; 294 char id[128] = "", kind[16] = "", receipt[65] = ""; 295 Sig sig; 296 297 if (nl < 1 || strcmp(ln[0], "gen-record 1")) return fail(1, "line 1 must be 'gen-record 1'", NULL); 298 i = 1; 299 if (!key(i, "id", &v)) return fail(i + 1, "expected 'id '", NULL); 300 snprintf(id, sizeof id, "%s", v); 301 i++; 302 if (!key(i, "kind", &v) || (strcmp(v, "raw") && strcmp(v, "abstraction"))) return fail(i + 1, "expected 'kind raw' or 'kind abstraction'", NULL); 303 snprintf(kind, sizeof kind, "%s", v); 304 int abs = !strcmp(kind, "abstraction"); 305 i++; 306 if (abs) { if (strncmp(id, "abs-", 4) || strlen(id) != 20) return fail(2, "abstraction id must be abs-<16 hex>", NULL); 307 for (int k = 4; k < 20; k++) if (!strchr("`0123456789abcdef`", id[k])) return fail(2, "abstraction id must be abs-<16 hex>", NULL); } 308 else if (strncmp(id, "raw-", 4) || !is_taskid(id + 4, strlen(id + 4))) return fail(2, "raw id must be raw-", NULL); 309 if (!key(i, "split", &v) || (strcmp(v, "corpus") && strcmp(v, "probe"))) return fail(i + 1, "expected 'split corpus' or 'split probe'", NULL); 310 if (abs && strcmp(v, "corpus")) return fail(i + 1, "an abstraction record is split corpus", NULL); 311 i++; 312 if (!key(i, "family", &v) || famlist(v, i + 1)) return fail(i + 1, "expected 'family +' (strictly ascending)", NULL); 313 if (!abs && strchr(v, ' ')) return fail(i + 1, "a raw record has exactly one family", NULL); 314 i++; 315 const char *ent, *pars, *rt; 316 int entl = i + 1; 317 if (!key(i, "entry", &ent)) return fail(i + 1, "expected 'entry '", NULL); 318 i++; 319 if (i < nl && !strcmp(ln[i], "params")) pars = ""; 320 else if (!key(i, "params", &pars)) return fail(i + 1, "expected 'params ...' (or just 'params')", NULL); 321 i++; 322 if (!key(i, "ret", &rt)) return fail(i + 1, "expected 'ret '", NULL); 323 i++; 324 if (!key(i, "signature", &v)) return fail(i + 1, "expected 'signature '", NULL); 325 const char *sigline = v; 326 int sigl = i + 1; 327 if (parse_sig(v, &sig, i + 1)) return -1; 328 if (strcmp(ent, sig.name)) return fail(entl, "entry differs from the signature's function name", NULL); 329 { 330 char want[512] = "", one[64]; 331 size_t wl = 0; 332 for (int k = 0; k < sig.np; k++) { 333 if (sig.plen[k]) { 334 char a[8], s[8]; 335 int l; 336 if (sscanf(sig.ptype[k], "%7s %7s %d", a, s, &l) != 3) return fail(sigl, "array parameter type is not ' '", NULL); 337 snprintf(one, sizeof one, "%s %s %d", a, irname(s), l); 338 } else snprintf(one, sizeof one, "%s", irname(sig.ptype[k])); 339 int r = snprintf(want + wl, sizeof want - wl, "%s%s", k ? " " : "", one); 340 if (r < 0 || (size_t)r >= sizeof want - wl) return fail(entl + 1, "params line too long", NULL); 341 wl += (size_t)r; 342 } 343 if (strcmp(pars, want)) return fail(entl + 1, "params differ from the signature's parameter types (requirement spelling)", NULL); 344 if (strcmp(rt, strcmp(sig.ret, "void") ? irname(sig.ret) : "void")) return fail(entl + 2, "ret differs from the signature's return type (requirement spelling)", NULL); 345 } 346 i++; 347 if (!key(i, "desc", &v)) return fail(i + 1, "expected 'desc '", NULL); 348 if (textline(v, i + 1, "desc")) return -1; 349 i++; 350 if (!key(i, "receipt", &v) || !is_hex64(v)) return fail(i + 1, "expected 'receipt <64 lowercase hex>'", NULL); 351 memcpy(receipt, v, 65); 352 i++; 353 if (!key(i, "validity", &v) || (strcmp(v, "verified") && strcmp(v, "unverified"))) return fail(i + 1, "expected 'validity verified' or 'validity unverified'", NULL); 354 i++; 355 /* ancestry + */ 356 int na = 0, ndist = 0; 357 char prev[64] = "", dig[16][65]; 358 while (key(i, "ancestry", &v)) { 359 const char *sp = strchr(v, ' '); 360 if (!sp || !is_taskid(v, (size_t)(sp - v)) || !is_hex64(sp + 1)) return fail(i + 1, "expected 'ancestry <64 hex>'", NULL); 361 char t[64]; 362 snprintf(t, sizeof t, "%.*s", (int)(sp - v), v); 363 if (na && strcmp(prev, t) >= 0) return fail(i + 1, "ancestry not strictly ascending by task id (or repeated)", NULL); 364 int seen = 0; 365 for (int k = 0; k < ndist && k < 16; k++) if (!strcmp(dig[k], sp + 1)) seen = 1; 366 if (!seen && ndist < 16) memcpy(dig[ndist++], sp + 1, 65); 367 if (!abs && strcmp(t, id + 4)) return fail(i + 1, "raw record ancestry must be its own task", NULL); 368 snprintf(prev, sizeof prev, "%s", t); 369 na++; 370 i++; 371 } 372 if (na < 1) return fail(i + 1, "expected at least one 'ancestry' line", NULL); 373 if (!abs && na != 1) return fail(i + 1, "raw record has exactly one ancestry line", NULL); 374 if (abs && (na < 2 || ndist < 2)) return fail(i + 1, "abstraction needs ancestry in >= 2 distinct corpus solutions (task ids and digests)", NULL); 375 /* source + */ 376 int ns = 0; 377 char prevp[256] = ""; 378 while (key(i, "source", &v)) { 379 const char *sp = strrchr(v, ' '); 380 char p[256]; 381 if (!sp || (size_t)(sp - v) >= sizeof p) return fail(i + 1, "expected 'source <64 hex>'", NULL); 382 snprintf(p, sizeof p, "%.*s", (int)(sp - v), v); 383 if (!safe_path(p) || !is_hex64(sp + 1)) return fail(i + 1, "expected 'source <64 hex>'", NULL); 384 if (ns && strcmp(prevp, p) >= 0) return fail(i + 1, "source not strictly ascending by path (or repeated)", NULL); 385 if (root) { 386 char full[1024], h[65]; 387 if (join_path(full, sizeof full, root, p) || file_sha(full, h)) return fail(i + 1, "source file %s cannot be read", p); 388 if (strcmp(h, sp + 1)) return fail(i + 1, "stale source digest for %s", p); 389 } 390 snprintf(prevp, sizeof prevp, "%s", p); 391 ns++; 392 i++; 393 } 394 if (ns < 1) return fail(i + 1, "expected at least one 'source' line", NULL); 395 char fpr[65] = ""; 396 if (abs) { 397 int nargs = 0; 398 for (int k = 0; k < sig.np; k++) { 399 if (!strcmp(sig.pname[k], "res")) return fail(sigl, "parameter name res is reserved for the postcondition", NULL); 400 nargs += sig.plen[k] ? sig.plen[k] : 1; 401 } 402 if (!is_scalar(sig.ret)) return fail(sigl, "v1 abstraction helpers return a scalar", NULL); 403 if (!key(i, "pre", &v)) return fail(i + 1, "expected 'pre STATED+TESTED '", NULL); 404 if (strncmp(v, "STATED+TESTED ", 14) || !v[14]) return fail(i + 1, "pre must be tagged STATED+TESTED (v1 proves nothing; PROVED is reserved)", NULL); 405 i++; 406 if (!key(i, "post", &v)) return fail(i + 1, "expected 'post STATED+TESTED '", NULL); 407 if (strncmp(v, "STATED+TESTED ", 14) || !v[14]) return fail(i + 1, "post must be tagged STATED+TESTED (v1 proves nothing; PROVED is reserved)", NULL); 408 i++; 409 if (!key(i, "example", &v)) return fail(i + 1, "expected 'example -> '", NULL); 410 if (parse_call(v, nargs, 0, i + 1, "example")) return -1; 411 i++; 412 int nlim = 0; 413 while (key(i, "limit", &v)) { if (textline(v, i + 1, "limit")) return -1; nlim++; i++; } 414 if (!nlim) return fail(i + 1, "expected at least one 'limit '", NULL); 415 while (key(i, "counter", &v)) { if (parse_call(v, nargs, 1, i + 1, "counter")) return -1; i++; } 416 if (!key(i, "fingerprint", &v) || !is_hex64(v)) return fail(i + 1, "expected 'fingerprint <64 hex>'", NULL); 417 memcpy(fpr, v, 65); 418 if (strncmp(fpr, id + 4, 16)) return fail(i + 1, "id is not abs- + the first 16 hex of the fingerprint", NULL); 419 i++; 420 int nd = 0; 421 char prevd[64] = ""; 422 while (key(i, "differs", &v)) { 423 const char *sp = strchr(v, ' '); 424 if (!sp || !is_taskid(v, (size_t)(sp - v)) || !is_hex64(sp + 1)) return fail(i + 1, "expected 'differs <64 hex>'", NULL); 425 char t[64]; 426 snprintf(t, sizeof t, "%.*s", (int)(sp - v), v); 427 if (nd && strcmp(prevd, t) >= 0) return fail(i + 1, "differs not strictly ascending by task id (or repeated)", NULL); 428 if (!strcmp(sp + 1, fpr)) return fail(i + 1, "helper fingerprint equals the whole corpus solution of %s", t); 429 snprintf(prevd, sizeof prevd, "%s", t); 430 nd++; 431 i++; 432 } 433 if (nd < 2) return fail(i + 1, "expected a 'differs' line for every corpus solution (at least 2)", NULL); 434 } 435 /* body */ 436 if (i >= nl || strcmp(ln[i], "body")) return fail(i + 1, "expected 'body'", NULL); 437 int b0 = i + 1; 438 if (nl - b0 < 1 || nl - b0 > 400) return fail(i + 1, "body must have 1..400 lines", NULL); 439 int nfn = 0, lastfn = -1; 440 for (int k = b0; k < nl; k++) if (!strncmp(ln[k], "fn ", 3)) { nfn++; lastfn = k; } 441 if (!nfn) return fail(b0 + 1, "body has no 'fn' line", NULL); 442 if (strcmp(ln[nl - 1], "end")) return fail(nl, "body must end with 'end'", NULL); 443 if (abs) { 444 if (nfn != 1 || strncmp(ln[b0], "fn ", 3)) return fail(b0 + 1, "abstraction body is exactly one function starting at its first line", NULL); 445 if (strcmp(ln[b0], sigline)) return fail(sigl, "signature differs from the body's fn line", NULL); 446 } else if (strcmp(ln[lastfn], sigline)) return fail(sigl, "signature differs from the body's last fn line (the entry)", NULL); 447 /* receipt file */ 448 if (rdir) { 449 char full[1024], h[65]; 450 if (join_path(full, sizeof full, rdir, receipt) || file_sha(full, h)) return fail(11, "receipt file %s is missing", receipt); 451 if (strcmp(h, receipt)) return fail(11, "receipt file does not hash to its id (fake or edited receipt)", NULL); 452 FILE *f = fopen(full, "rb"); 453 char l1[64] = "", l2[160] = "", want[160]; 454 if (!f || !fgets(l1, sizeof l1, f) || !fgets(l2, sizeof l2, f)) { if (f) fclose(f); return fail(11, "receipt file is too short", NULL); } 455 fclose(f); 456 snprintf(want, sizeof want, "record %s\n", id); 457 if (strcmp(l1, "gen-receipt 1\n") || strcmp(l2, want)) return fail(11, "receipt is not this record's (header or record line)", NULL); 458 } 459 return 0; 460 } 461 462 static int load(const char *path) 463 { 464 FILE *f = fopen(path, "rb"); 465 if (!f) return fail(0, "cannot read the file", NULL); 466 size_t n = fread(buf, 1, MAXB + 1, f); 467 fclose(f); 468 if (n > MAXB) return fail(0, "file over 65536 bytes", NULL); 469 if (n == 0 || buf[n - 1] != '\n') return fail(0, "file is empty or does not end with LF", NULL); 470 buf[n] = 0; 471 nl = 0; 472 int line = 1; 473 char *s = buf; 474 for (size_t k = 0; k < n; k++) { 475 unsigned char c = (unsigned char)buf[k]; 476 if (c == '\n') { 477 buf[k] = 0; 478 size_t len = strlen(s); 479 if (len == 0) return fail(line, "empty line", NULL); 480 if (len > MAXL) return fail(line, "line over 1024 bytes", NULL); 481 if (s[0] == ' ' || s[len - 1] == ' ') return fail(line, "line starts or ends with a space", NULL); 482 if (strstr(s, " ")) return fail(line, "two spaces in a row", NULL); 483 if (nl == MAXLINES) return fail(line, "too many lines", NULL); 484 ln[nl++] = s; 485 s = buf + k + 1; 486 line++; 487 } else if (c < 0x20 || c > 0x7e) return fail(line, "byte outside ASCII 0x20..0x7E (CR, tab or non-ASCII)", NULL); 488 } 489 return 0; 490 } 491 492 int main(int argc, char **argv) 493 { 494 const char *root = NULL, *rdir = NULL; 495 int i = 1, bad = 0; 496 while (i < argc && argv[i][0] == '-' && argv[i][1] == '-') { 497 if (!strcmp(argv[i], "--root") && i + 1 < argc) root = argv[i + 1]; 498 else if (!strcmp(argv[i], "--receipts") && i + 1 < argc) rdir = argv[i + 1]; 499 else { fprintf(stderr, "usage: recheck [--root ] [--receipts ] ...\n"); return 2; } 500 i += 2; 501 } 502 if (i >= argc) { fprintf(stderr, "usage: recheck [--root ] [--receipts ] ...\n"); return 2; } 503 for (; i < argc; i++) { 504 g_file = argv[i]; 505 g_err[0] = 0; 506 g_errline = 0; 507 if (load(argv[i]) == 0 && check(root, rdir) == 0) { 508 printf("OK %s\n", ln[1] + 3); 509 } else { 510 printf("REFUSED %s: line %d: %s\n", g_file, g_errline, g_err); 511 bad = 1; 512 } 513 } 514 return bad; 515 }