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/src/omega_vcstore.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 / src / omega_vcstore.c` 723 lines ยท 30640 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev) 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #include "omega_vcstore.h" 18 #include "omega_vcstore_priv.h" 19 #include "omega_genesis.h" 20 #include "sha256.h" 21 #include 22 #include 23 #include 24 #include 25 26 /* Every refusal guard sits on one line carrying a VC1S: marker. The mutation test 27 * (Makefile test-vcstore) copies this file, deletes or weakens exactly that line, and 28 * requires tests/test_omega_vcstore.c to fail. A comment-only marker line is a hook where a 29 * mutant injects a forbidden behaviour. Keep one guard per line when editing. */ 30 31 #define TAG "AIEN_VERIFIED_CRUMB_V1" 32 #define TAG_LEN 22 33 #define FILE_MAGIC "AIEN_VCSTORE_V1" 34 #define FILE_MAGIC_LEN 15 35 #define MAX_NAME 128 36 37 static const uint8_t ZERO32[32]; 38 39 const char *omega_vcstore_code_name(int code) { 40 switch (code) { 41 case OMEGA_VCS_OK: return "OK"; 42 case OMEGA_VCS_UNVERIFIED_DEPENDENCY: return "UNVERIFIED_DEPENDENCY"; 43 case OMEGA_VCS_MISSING_RECEIPT: return "MISSING_RECEIPT"; 44 case OMEGA_VCS_RECEIPT_HASH_MISMATCH: return "RECEIPT_HASH_MISMATCH"; 45 case OMEGA_VCS_DEPENDENCY_NOT_PINNED: return "DEPENDENCY_NOT_PINNED"; 46 case OMEGA_VCS_DEPENDENCY_CYCLE: return "DEPENDENCY_CYCLE"; 47 case OMEGA_VCS_STALE_RECEIPT: return "STALE_RECEIPT"; 48 case OMEGA_VCS_UNDECLARED_IMPORT: return "UNDECLARED_IMPORT"; 49 case OMEGA_VCS_TAINTED_ARTIFACT: return "TAINTED_ARTIFACT"; 50 case OMEGA_VCS_UNKNOWN_VERIFIER_PROFILE: return "UNKNOWN_VERIFIER_PROFILE"; 51 case OMEGA_VCS_BAD_DOMAIN_TAG: return "BAD_DOMAIN_TAG"; 52 case OMEGA_VCS_UNKNOWN_FORMAT_VERSION: return "UNKNOWN_FORMAT_VERSION"; 53 case OMEGA_VCS_TRUNCATED: return "TRUNCATED"; 54 case OMEGA_VCS_TRAILING_BYTES: return "TRAILING_BYTES"; 55 case OMEGA_VCS_DUPLICATE_DEPENDENCY: return "DUPLICATE_DEPENDENCY"; 56 case OMEGA_VCS_UNSORTED_DEPENDENCIES: return "UNSORTED_DEPENDENCIES"; 57 case OMEGA_VCS_NONCANONICAL_SET: return "NONCANONICAL_SET"; 58 case OMEGA_VCS_BAD_STRING: return "BAD_STRING"; 59 case OMEGA_VCS_ZERO_ID: return "ZERO_ID"; 60 case OMEGA_VCS_TOO_MANY_ENTRIES: return "TOO_MANY_ENTRIES"; 61 case OMEGA_VCS_BAD_DIGEST_KIND: return "BAD_DIGEST_KIND"; 62 case OMEGA_VCS_VCSTORE_ID_MISMATCH: return "VCSTORE_ID_MISMATCH"; 63 case OMEGA_VCS_VCSTORE_IMMUTABLE_CONFLICT: return "VCSTORE_IMMUTABLE_CONFLICT"; 64 case OMEGA_VCS_VCSTORE_MALFORMED: return "VCSTORE_MALFORMED"; 65 case OMEGA_VCS_VCSTORE_NOT_FOUND: return "VCSTORE_NOT_FOUND"; 66 case OMEGA_VCS_VCSTORE_NAME_EXISTS: return "VCSTORE_NAME_EXISTS"; 67 case OMEGA_VCS_VCSTORE_CAPACITY: return "VCSTORE_CAPACITY"; 68 case OMEGA_VCS_VCSTORE_NOMEM: return "VCSTORE_NOMEM"; 69 case OMEGA_VCS_VCSTORE_IO: return "VCSTORE_IO"; 70 case OMEGA_VCS_GENESIS_NOT_LISTED: return "GENESIS_NOT_LISTED"; 71 } 72 return "UNKNOWN_CODE"; 73 } 74 75 /* ---- decoder (SPEC 3; refusal order is field order, the first failure wins) ---- */ 76 77 typedef struct { const uint8_t *b; size_t n, pos; int err; } cur_t; 78 79 static void cfail(cur_t *c, int code) { if (!c->err) c->err = code; } 80 81 static int need(cur_t *c, size_t k) { 82 if (c->err) return 0; 83 if (c->n - c->pos < k) { cfail(c, OMEGA_VCS_TRUNCATED); return 0; } /* VC1S:dec-truncated */ 84 return 1; 85 } 86 static uint32_t rd32(cur_t *c) { 87 if (!need(c, 4)) return 0; 88 const uint8_t *p = c->b + c->pos; c->pos += 4; 89 return (uint32_t)p[0] << 24 | (uint32_t)p[1] << 16 | (uint32_t)p[2] << 8 | p[3]; 90 } 91 static uint64_t rd64(cur_t *c) { 92 uint64_t hi = rd32(c), lo = rd32(c); 93 return hi << 32 | lo; 94 } 95 static const uint8_t *rd_idp(cur_t *c) { 96 if (!need(c, 32)) return ZERO32; 97 const uint8_t *p = c->b + c->pos; c->pos += 32; 98 return p; 99 } 100 static int is_zero32(const uint8_t *p) { 101 uint8_t acc = 0; 102 for (int i = 0; i < 32; i++) acc |= p[i]; 103 return acc == 0; 104 } 105 static uint32_t rd_count(cur_t *c) { 106 uint32_t n = rd32(c); 107 if (!c->err && n > OMEGA_VC_MAX_ENTRIES) { cfail(c, OMEGA_VCS_TOO_MANY_ENTRIES); return 0; } /* VC1S:dec-count */ 108 return n; 109 } 110 static void rd_str(cur_t *c, OmegaVcStr *out) { 111 uint64_t len = rd64(c); 112 out->p = (const uint8_t *)""; out->len = 0; 113 if (c->err) return; 114 if (len == 0 || len > OMEGA_VC_MAX_STRING) { cfail(c, OMEGA_VCS_BAD_STRING); return; } /* VC1S:dec-string-len */ 115 if (!need(c, (size_t)len)) return; 116 for (uint64_t i = 0; i < len; i++) { 117 uint8_t ch = c->b[c->pos + i]; 118 if (ch < 0x21 || ch > 0x7e) { cfail(c, OMEGA_VCS_BAD_STRING); return; } /* VC1S:dec-string-char */ 119 } 120 out->p = c->b + c->pos; out->len = (size_t)len; c->pos += (size_t)len; 121 } 122 static int str_cmp(const OmegaVcStr *a, const OmegaVcStr *b) { 123 size_t m = a->len < b->len ? a->len : b->len; 124 int r = memcmp(a->p, b->p, m); 125 if (r) return r; 126 return a->len < b->len ? -1 : a->len > b->len ? 1 : 0; 127 } 128 129 int omega_vc_decode(const uint8_t *b, size_t n, OmegaVcView *v) { 130 if (!v || (!b && n)) return OMEGA_VCS_VCSTORE_MALFORMED; 131 cur_t c = { b ? b : ZERO32, n, 0, 0 }; 132 memset(v, 0, sizeof *v); 133 if (!need(&c, TAG_LEN)) return c.err; 134 if (memcmp(b, TAG, TAG_LEN) != 0) return OMEGA_VCS_BAD_DOMAIN_TAG; /* VC1S:dec-tag */ 135 c.pos = TAG_LEN; 136 v->format_version = rd32(&c); 137 if (!c.err && v->format_version != OMEGA_VC_FORMAT_VERSION) cfail(&c, OMEGA_VCS_UNKNOWN_FORMAT_VERSION); /* VC1S:dec-version */ 138 v->semantic_id = rd_idp(&c); 139 v->contract_id = rd_idp(&c); 140 if (need(&c, 1)) { 141 v->digest_kind = c.b[c.pos++]; 142 if (v->digest_kind != OMEGA_VC_DIGEST_SOURCE && v->digest_kind != OMEGA_VC_DIGEST_IR) cfail(&c, OMEGA_VCS_BAD_DIGEST_KIND); /* VC1S:dec-kind */ 143 } 144 v->source_or_ir_digest = rd_idp(&c); 145 if (!c.err && (is_zero32(v->semantic_id) || is_zero32(v->contract_id) || is_zero32(v->source_or_ir_digest))) cfail(&c, OMEGA_VCS_ZERO_ID); /* VC1S:dec-zero-ids */ 146 v->n_realizations = rd_count(&c); 147 v->realization_ids = c.b + c.pos; 148 const uint8_t *prev = NULL; 149 for (uint32_t i = 0; i < v->n_realizations && !c.err; i++) { 150 const uint8_t *id = rd_idp(&c); 151 if (!c.err && prev && memcmp(prev, id, 32) >= 0) cfail(&c, OMEGA_VCS_NONCANONICAL_SET); /* VC1S:dec-real-order */ 152 prev = id; 153 } 154 v->n_dependencies = rd_count(&c); 155 v->dependencies = c.b + c.pos; 156 prev = NULL; 157 for (uint32_t i = 0; i < v->n_dependencies && !c.err; i++) { 158 const uint8_t *sid = rd_idp(&c); 159 const uint8_t *req = rd_idp(&c); 160 if (c.err) break; 161 if (memcmp(sid, v->semantic_id, 32) == 0) { cfail(&c, OMEGA_VCS_DEPENDENCY_CYCLE); break; } /* VC1S:dec-dep-self */ 162 if (is_zero32(sid) || is_zero32(req)) { cfail(&c, OMEGA_VCS_ZERO_ID); break; } /* VC1S:dec-dep-zero */ 163 if (prev) { 164 int cmp = memcmp(prev, sid, 32); 165 if (cmp == 0) { cfail(&c, OMEGA_VCS_DUPLICATE_DEPENDENCY); break; } /* VC1S:dec-dep-dup */ 166 if (cmp > 0) { cfail(&c, OMEGA_VCS_UNSORTED_DEPENDENCIES); break; } /* VC1S:dec-dep-order */ 167 } 168 prev = sid; 169 } 170 v->receipt_id = rd_idp(&c); 171 if (!c.err && is_zero32(v->receipt_id)) cfail(&c, OMEGA_VCS_MISSING_RECEIPT); /* VC1S:dec-receipt */ 172 rd_str(&c, &v->verifier_profile); 173 rd_str(&c, &v->verifier_version); 174 v->evidence_root = rd_idp(&c); 175 if (!c.err && is_zero32(v->evidence_root)) cfail(&c, OMEGA_VCS_ZERO_ID); /* VC1S:dec-evroot */ 176 v->n_exports = rd_count(&c); 177 for (uint32_t i = 0; i < v->n_exports && !c.err; i++) { 178 rd_str(&c, &v->exports[i]); 179 if (!c.err && i > 0 && str_cmp(&v->exports[i - 1], &v->exports[i]) >= 0) cfail(&c, OMEGA_VCS_NONCANONICAL_SET); /* VC1S:dec-exports-order */ 180 } 181 v->n_capabilities = rd_count(&c); 182 for (uint32_t i = 0; i < v->n_capabilities && !c.err; i++) { 183 rd_str(&c, &v->capabilities[i]); 184 if (!c.err && i > 0 && str_cmp(&v->capabilities[i - 1], &v->capabilities[i]) >= 0) cfail(&c, OMEGA_VCS_NONCANONICAL_SET); /* VC1S:dec-caps-order */ 185 } 186 if (!c.err && c.pos != c.n) cfail(&c, OMEGA_VCS_TRAILING_BYTES); /* VC1S:dec-trailing */ 187 return c.err; 188 } 189 190 void omega_vc_compute_id(const uint8_t *bytes, size_t len, uint8_t out[OMEGA_VC_ID_BYTES]) { 191 sha256_hash(bytes, len, out); /* VC1S:id-hash */ 192 } 193 194 /* ---- store ---- */ 195 196 int omega_vcstore_init(OmegaVcStore *s) { 197 if (!s) return OMEGA_VCS_VCSTORE_MALFORMED; 198 memset(s, 0, sizeof *s); 199 return OMEGA_VCS_OK; 200 } 201 202 void omega_vcstore_destroy(OmegaVcStore *s) { 203 if (!s) return; 204 for (size_t i = 0; i < s->count; i++) free(s->objs[i].bytes); 205 for (size_t i = 0; i < s->n_names; i++) free(s->names[i].name); 206 free(s->objs); 207 free(s->names); 208 memset(s, 0, sizeof *s); 209 } 210 211 size_t omega_vcstore_count(const OmegaVcStore *s) { return s ? s->count : 0; } 212 213 /* lower bound by semantic_id; returns 1 if found at *pos, else 0 with *pos the insert point */ 214 static int vcs_find(const OmegaVcStore *s, const uint8_t *id, size_t *pos) { 215 size_t lo = 0, hi = s->count; 216 while (lo < hi) { 217 size_t mid = lo + (hi - lo) / 2; 218 int c = memcmp(s->objs[mid].semantic_id, id, 32); 219 if (c == 0) { *pos = mid; return 1; } 220 if (c < 0) lo = mid + 1; else hi = mid; 221 } 222 *pos = lo; 223 return 0; 224 } 225 226 /* Fetch object idx with every integrity check: the VC id is recomputed from the stored bytes, 227 * the bytes are decoded again, and the decoded semantic_id must be the key. */ 228 static int vcs_fetch(const OmegaVcStore *s, size_t idx, OmegaVcRecord *out) { 229 const OmegaVcObject *o = &s->objs[idx]; 230 uint8_t id[32]; 231 omega_vc_compute_id(o->bytes, o->len, id); 232 if (memcmp(id, o->vc_id, 32) != 0) return OMEGA_VCS_VCSTORE_ID_MISMATCH; /* VC1S:get-recompute */ 233 int rc = omega_vc_decode(o->bytes, o->len, &out->vc); 234 if (rc) return rc; 235 if (memcmp(out->vc.semantic_id, o->semantic_id, 32) != 0) return OMEGA_VCS_VCSTORE_ID_MISMATCH; /* VC1S:get-key */ 236 out->canonical = o->bytes; 237 out->canonical_len = o->len; 238 memcpy(out->vc_id, o->vc_id, 32); 239 out->admission_kind = o->admission_kind; 240 return OMEGA_VCS_OK; 241 } 242 243 static int vcs_insert(OmegaVcStore *s, const uint8_t *canon, size_t len, const uint8_t *claimed, uint8_t kind) { 244 if (!s || !canon || len == 0) return OMEGA_VCS_VCSTORE_MALFORMED; 245 if (!claimed) return OMEGA_VCS_VCSTORE_MALFORMED; /* VC1S:claimed-required */ 246 /* VC1S:no-cap (the store has no fixed capacity: it grows) */ 247 OmegaVcRecord *rec = malloc(sizeof *rec); 248 if (!rec) return OMEGA_VCS_VCSTORE_NOMEM; 249 int rc = omega_vc_decode(canon, len, &rec->vc); 250 if (rc) { free(rec); return rc; } 251 uint8_t id[32]; 252 omega_vc_compute_id(canon, len, id); 253 if (memcmp(id, claimed, 32) != 0) { free(rec); return OMEGA_VCS_VCSTORE_ID_MISMATCH; } /* VC1S:insert-idcheck */ 254 if (kind == OMEGA_VCS_ADMISSION_BOOTSTRAP && !omega_genesis_contains(rec->vc.semantic_id)) { free(rec); return OMEGA_VCS_GENESIS_NOT_LISTED; } /* VC1S:insert-genesis */ 255 size_t pos; 256 if (vcs_find(s, rec->vc.semantic_id, &pos)) { 257 const OmegaVcObject *o = &s->objs[pos]; 258 free(rec); 259 if (o->len == len && o->admission_kind == kind && memcmp(o->bytes, canon, len) == 0) return OMEGA_VCS_OK; /* VC1S:idempotent */ 260 return OMEGA_VCS_VCSTORE_IMMUTABLE_CONFLICT; /* VC1S:immutable */ 261 } 262 /* every dependency must already be stored, under the contract this record requires */ 263 OmegaVcRecord *dr = malloc(sizeof *dr); 264 if (!dr) { free(rec); return OMEGA_VCS_VCSTORE_NOMEM; } 265 for (uint32_t i = 0; i < rec->vc.n_dependencies; i++) { 266 const uint8_t *dep = rec->vc.dependencies + 64 * (size_t)i; 267 size_t di; 268 if (!vcs_find(s, dep, &di)) { rc = OMEGA_VCS_UNVERIFIED_DEPENDENCY; break; } /* VC1S:dep-exists */ 269 rc = vcs_fetch(s, di, dr); 270 if (rc) break; 271 if (memcmp(dr->vc.contract_id, dep + 32, 32) != 0) { rc = OMEGA_VCS_UNVERIFIED_DEPENDENCY; break; } /* VC1S:dep-contract */ 272 } 273 free(dr); 274 if (rc) { free(rec); return rc; } 275 uint8_t sid[32]; 276 memcpy(sid, rec->vc.semantic_id, 32); 277 free(rec); 278 279 uint8_t *cp = malloc(len); 280 if (!cp) return OMEGA_VCS_VCSTORE_NOMEM; 281 memcpy(cp, canon, len); 282 if (s->count == s->cap) { 283 size_t nc = s->cap ? s->cap * 2 : 16; 284 if (nc > SIZE_MAX / sizeof *s->objs) { free(cp); return OMEGA_VCS_VCSTORE_NOMEM; } 285 OmegaVcObject *n = realloc(s->objs, nc * sizeof *s->objs); 286 if (!n) { free(cp); return OMEGA_VCS_VCSTORE_NOMEM; } 287 s->objs = n; s->cap = nc; 288 } 289 size_t at = pos; /* VC1S:sorted-insert */ 290 memmove(&s->objs[at + 1], &s->objs[at], (s->count - at) * sizeof *s->objs); 291 OmegaVcObject *o = &s->objs[at]; 292 memcpy(o->semantic_id, sid, 32); 293 memcpy(o->vc_id, id, 32); 294 o->admission_kind = kind; 295 o->bytes = cp; 296 o->len = len; 297 s->count++; 298 return OMEGA_VCS_OK; 299 } 300 301 int omega_vcstore_insert(OmegaVcStore *s, const uint8_t *canonical, size_t len, const uint8_t claimed_vc_id[32]) { 302 return vcs_insert(s, canonical, len, claimed_vc_id, OMEGA_VCS_ADMISSION_VERIFIED); 303 } 304 305 int omega_vcstore_insert_bootstrap(OmegaVcStore *s, const uint8_t *canonical, size_t len, const uint8_t claimed_vc_id[32]) { 306 return vcs_insert(s, canonical, len, claimed_vc_id, OMEGA_VCS_ADMISSION_BOOTSTRAP); /* VC1S:kind-bootstrap */ 307 } 308 309 int omega_vcstore_get(const OmegaVcStore *s, const uint8_t semantic_id[32], OmegaVcRecord *out) { 310 if (!s || !semantic_id || !out) return OMEGA_VCS_VCSTORE_MALFORMED; 311 size_t idx; 312 if (!vcs_find(s, semantic_id, &idx)) return OMEGA_VCS_UNVERIFIED_DEPENDENCY; 313 return vcs_fetch(s, idx, out); 314 } 315 316 int omega_vcstore_receipt_of(const OmegaVcStore *s, const uint8_t semantic_id[32], uint8_t out_receipt[32]) { 317 if (!out_receipt) return OMEGA_VCS_VCSTORE_MALFORMED; 318 OmegaVcRecord *rec = malloc(sizeof *rec); 319 if (!rec) return OMEGA_VCS_VCSTORE_NOMEM; 320 int rc = omega_vcstore_get(s, semantic_id, rec); 321 if (rc == OMEGA_VCS_OK) memcpy(out_receipt, rec->vc.receipt_id, 32); /* VC1S:receipt-of */ 322 free(rec); 323 return rc; 324 } 325 326 int omega_vcstore_admission_kind(const OmegaVcStore *s, const uint8_t semantic_id[32], uint8_t *out_kind) { 327 if (!out_kind) return OMEGA_VCS_VCSTORE_MALFORMED; 328 OmegaVcRecord *rec = malloc(sizeof *rec); 329 if (!rec) return OMEGA_VCS_VCSTORE_NOMEM; 330 int rc = omega_vcstore_get(s, semantic_id, rec); 331 if (rc == OMEGA_VCS_OK) *out_kind = rec->admission_kind; 332 free(rec); 333 return rc; 334 } 335 336 /* ---- dependency walk: post-order, shared by closure and the load-time graph check ---- */ 337 338 typedef struct { size_t idx; uint32_t n_dep, next; const uint8_t *deps; } frame_t; 339 340 /* state: 0 unseen, 1 on the current path, 2 done. Visits root and everything it needs that is 341 * still unseen. Emits newly finished ids into out_ids while *emitted < cap, always counts. */ 342 static int vcs_walk(const OmegaVcStore *s, size_t root, uint8_t *state, frame_t *stack, OmegaVcRecord *rec, 343 uint8_t *out_ids, size_t cap, size_t *emitted) { 344 /* VC1S:walk-names (no resolution step may read the name index) */ 345 if (state[root] == 2) return OMEGA_VCS_OK; 346 int rc = vcs_fetch(s, root, rec); 347 if (rc) return rc; 348 size_t sp = 0; 349 state[root] = 1; 350 stack[sp++] = (frame_t){ root, rec->vc.n_dependencies, 0, rec->vc.dependencies }; 351 while (sp) { 352 frame_t *f = &stack[sp - 1]; 353 if (f->next < f->n_dep) { 354 const uint8_t *dep = f->deps + 64 * (size_t)f->next++; /* VC1S:walk-order */ 355 size_t di; 356 if (!vcs_find(s, dep, &di)) return OMEGA_VCS_UNVERIFIED_DEPENDENCY; /* VC1S:walk-dep-exists */ 357 rc = vcs_fetch(s, di, rec); 358 if (rc) return rc; 359 if (memcmp(rec->vc.contract_id, dep + 32, 32) != 0) return OMEGA_VCS_UNVERIFIED_DEPENDENCY; /* VC1S:walk-contract */ 360 if (state[di] == 1) return OMEGA_VCS_DEPENDENCY_CYCLE; /* VC1S:walk-cycle */ 361 if (state[di] == 2) continue; /* VC1S:walk-visited */ 362 state[di] = 1; 363 stack[sp++] = (frame_t){ di, rec->vc.n_dependencies, 0, rec->vc.dependencies }; 364 } else { 365 state[f->idx] = 2; 366 if (*emitted < cap) memcpy(out_ids + 32 * *emitted, s->objs[f->idx].semantic_id, 32); 367 (*emitted)++; 368 sp--; 369 } 370 } 371 return OMEGA_VCS_OK; 372 } 373 374 int omega_vcstore_closure(const OmegaVcStore *s, const uint8_t id[32], uint8_t *out_ids, size_t cap, size_t *out_n) { 375 if (!out_n) return OMEGA_VCS_VCSTORE_MALFORMED; 376 *out_n = 0; 377 if (!s || !id || (cap && !out_ids)) return OMEGA_VCS_VCSTORE_MALFORMED; 378 size_t root; 379 if (!vcs_find(s, id, &root)) return OMEGA_VCS_UNVERIFIED_DEPENDENCY; 380 uint8_t *state = calloc(s->count, 1); 381 frame_t *stack = malloc(s->count * sizeof *stack); 382 OmegaVcRecord *rec = malloc(sizeof *rec); 383 int rc; 384 size_t emitted = 0; 385 if (!state || !stack || !rec) { rc = OMEGA_VCS_VCSTORE_NOMEM; goto out; } 386 rc = vcs_walk(s, root, state, stack, rec, out_ids, cap, &emitted); 387 if (rc) goto out; 388 *out_n = emitted; 389 if (emitted > cap) rc = OMEGA_VCS_VCSTORE_CAPACITY; /* VC1S:walk-capacity */ 390 out: 391 free(state); free(stack); free(rec); 392 return rc; 393 } 394 395 /* Every dependency present with its contract, no cycle, over the whole store. */ 396 static int vcs_graph_check(const OmegaVcStore *s) { 397 if (s->count == 0) return OMEGA_VCS_OK; 398 uint8_t *state = calloc(s->count, 1); 399 frame_t *stack = malloc(s->count * sizeof *stack); 400 OmegaVcRecord *rec = malloc(sizeof *rec); 401 int rc = OMEGA_VCS_OK; 402 size_t emitted = 0; 403 if (!state || !stack || !rec) rc = OMEGA_VCS_VCSTORE_NOMEM; 404 for (size_t i = 0; !rc && i < s->count; i++) rc = vcs_walk(s, i, state, stack, rec, NULL, 0, &emitted); 405 free(state); free(stack); free(rec); 406 return rc; 407 } 408 409 /* ---- digests ---- */ 410 411 static void u32be(sha256_ctx *c, uint32_t v) { 412 uint8_t b[4] = { (uint8_t)(v >> 24), (uint8_t)(v >> 16), (uint8_t)(v >> 8), (uint8_t)v }; 413 sha256_update(c, b, 4); 414 } 415 static void u64be(sha256_ctx *c, uint64_t v) { u32be(c, (uint32_t)(v >> 32)); u32be(c, (uint32_t)v); } 416 417 int omega_vcstore_digest(const OmegaVcStore *s, uint8_t out[32]) { 418 if (!s || !out) return OMEGA_VCS_VCSTORE_MALFORMED; 419 sha256_ctx ctx; 420 sha256_init(&ctx); 421 sha256_update(&ctx, (const uint8_t *)"VCS1", 4); /* VC1S:digest-magic */ 422 u64be(&ctx, s->count); 423 for (size_t i = 0; i < s->count; i++) { 424 const OmegaVcObject *o = &s->objs[i]; 425 sha256_update(&ctx, o->semantic_id, 32); 426 uint8_t kind = o->admission_kind; /* VC1S:digest-kind */ 427 sha256_update(&ctx, &kind, 1); 428 u64be(&ctx, o->len); 429 sha256_update(&ctx, o->bytes, o->len); /* VC1S:digest-bytes */ 430 } 431 /* VC1S:digest-names (the name index is never part of the object digest) */ 432 sha256_final(&ctx, out); 433 return OMEGA_VCS_OK; 434 } 435 436 int omega_vcstore_name_index_digest(const OmegaVcStore *s, uint8_t out[32]) { 437 if (!s || !out) return OMEGA_VCS_VCSTORE_MALFORMED; 438 sha256_ctx ctx; 439 sha256_init(&ctx); 440 sha256_update(&ctx, (const uint8_t *)"VCN1", 4); 441 u64be(&ctx, s->n_names); 442 for (size_t i = 0; i < s->n_names; i++) { 443 const OmegaVcName *nm = &s->names[i]; 444 size_t l = strlen(nm->name); 445 u64be(&ctx, l); 446 sha256_update(&ctx, (const uint8_t *)nm->name, l); 447 sha256_update(&ctx, nm->id, 32); /* VC1S:namedigest-id */ 448 } 449 sha256_final(&ctx, out); 450 return OMEGA_VCS_OK; 451 } 452 453 /* ---- name index ---- */ 454 455 static int name_valid(const char *name) { 456 size_t n = strlen(name); 457 if (n == 0 || n > MAX_NAME) return 0; 458 for (size_t i = 0; i < n; i++) { 459 char ch = name[i]; 460 int alpha = (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == '_'; 461 int rest = alpha || (ch >= '0' && ch <= '9') || ch == '.' || ch == '-'; 462 if (i == 0 ? !alpha : !rest) return 0; 463 } 464 return 1; 465 } 466 467 static int name_find(const OmegaVcStore *s, const char *name, size_t *pos) { 468 size_t lo = 0, hi = s->n_names; 469 while (lo < hi) { 470 size_t mid = lo + (hi - lo) / 2; 471 int c = strcmp(s->names[mid].name, name); 472 if (c == 0) { *pos = mid; return 1; } 473 if (c < 0) lo = mid + 1; else hi = mid; 474 } 475 *pos = lo; 476 return 0; 477 } 478 479 static int name_insert_at(OmegaVcStore *s, size_t pos, const char *name, const uint8_t *id) { 480 size_t l = strlen(name); 481 char *cp = malloc(l + 1); 482 if (!cp) return OMEGA_VCS_VCSTORE_NOMEM; 483 memcpy(cp, name, l + 1); 484 if (s->n_names == s->cap_names) { 485 size_t nc = s->cap_names ? s->cap_names * 2 : 8; 486 if (nc > SIZE_MAX / sizeof *s->names) { free(cp); return OMEGA_VCS_VCSTORE_NOMEM; } 487 OmegaVcName *n = realloc(s->names, nc * sizeof *s->names); 488 if (!n) { free(cp); return OMEGA_VCS_VCSTORE_NOMEM; } 489 s->names = n; s->cap_names = nc; 490 } 491 memmove(&s->names[pos + 1], &s->names[pos], (s->n_names - pos) * sizeof *s->names); 492 s->names[pos].name = cp; 493 memcpy(s->names[pos].id, id, 32); 494 s->n_names++; 495 return OMEGA_VCS_OK; 496 } 497 498 int omega_vcstore_name_bind(OmegaVcStore *s, const char *name, const uint8_t id[32]) { 499 if (!s || !name || !id) return OMEGA_VCS_VCSTORE_MALFORMED; 500 if (!name_valid(name)) return OMEGA_VCS_VCSTORE_MALFORMED; /* VC1S:name-valid */ 501 size_t di; 502 if (!vcs_find(s, id, &di)) return OMEGA_VCS_UNVERIFIED_DEPENDENCY; /* VC1S:name-id-exists */ 503 /* VC1S:name-many (many names may point at one id) */ 504 size_t np; 505 if (name_find(s, name, &np)) { 506 if (memcmp(s->names[np].id, id, 32) == 0) return OMEGA_VCS_OK; 507 return OMEGA_VCS_VCSTORE_NAME_EXISTS; /* VC1S:name-no-silent-rebind */ 508 } 509 return name_insert_at(s, np, name, id); /* VC1S:name-sorted */ 510 } 511 512 int omega_vcstore_name_rebind(OmegaVcStore *s, const char *name, const uint8_t id[32]) { 513 if (!s || !name || !id) return OMEGA_VCS_VCSTORE_MALFORMED; 514 size_t di, np; 515 if (!vcs_find(s, id, &di)) return OMEGA_VCS_UNVERIFIED_DEPENDENCY; /* VC1S:rebind-id-exists */ 516 if (!name_find(s, name, &np)) return OMEGA_VCS_VCSTORE_NOT_FOUND; /* VC1S:rebind-exists */ 517 memcpy(s->names[np].id, id, 32); /* VC1S:rebind-write */ 518 return OMEGA_VCS_OK; 519 } 520 521 int omega_vcstore_resolve_name(const OmegaVcStore *s, const char *name, uint8_t out_id[32]) { 522 if (!s || !name || !out_id) return OMEGA_VCS_VCSTORE_MALFORMED; 523 size_t np; 524 if (!name_find(s, name, &np)) return OMEGA_VCS_VCSTORE_NOT_FOUND; 525 memcpy(out_id, s->names[np].id, 32); /* VC1S:resolve */ 526 return OMEGA_VCS_OK; 527 } 528 529 /* ---- persistence ---- */ 530 531 typedef struct { uint8_t *p; size_t n, cap; int err; } wbuf_t; 532 533 static void w_put(wbuf_t *w, const void *d, size_t k) { 534 if (w->err) return; 535 if (w->n + k > w->cap) { 536 size_t nc = (w->n + k) * 2 + 256; 537 uint8_t *np = realloc(w->p, nc); 538 if (!np) { w->err = OMEGA_VCS_VCSTORE_NOMEM; return; } 539 w->p = np; w->cap = nc; 540 } 541 memcpy(w->p + w->n, d, k); 542 w->n += k; 543 } 544 static void w_u64(wbuf_t *w, uint64_t v) { 545 uint8_t b[8]; 546 for (int i = 0; i < 8; i++) b[i] = (uint8_t)(v >> (56 - 8 * i)); 547 w_put(w, b, 8); 548 } 549 550 int omega_vcstore_save(const OmegaVcStore *s, const char *path) { 551 if (!s || !path) return OMEGA_VCS_VCSTORE_MALFORMED; 552 OmegaVcRecord *rec = malloc(sizeof *rec); 553 if (!rec) return OMEGA_VCS_VCSTORE_NOMEM; 554 for (size_t i = 0; i < s->count; i++) { 555 int rc = vcs_fetch(s, i, rec); /* VC1S:save-verify */ 556 if (rc) { free(rec); return rc; } 557 } 558 free(rec); 559 wbuf_t w = { 0, 0, 0, 0 }; 560 w_put(&w, FILE_MAGIC, FILE_MAGIC_LEN); 561 w_u64(&w, s->count); 562 for (size_t i = 0; i < s->count; i++) { 563 w_put(&w, &s->objs[i].admission_kind, 1); 564 w_u64(&w, s->objs[i].len); 565 w_put(&w, s->objs[i].bytes, s->objs[i].len); 566 } 567 w_u64(&w, s->n_names); 568 for (size_t i = 0; i < s->n_names; i++) { 569 size_t l = strlen(s->names[i].name); 570 w_u64(&w, l); 571 w_put(&w, s->names[i].name, l); 572 w_put(&w, s->names[i].id, 32); 573 } 574 uint8_t d1[32], d2[32]; 575 omega_vcstore_digest(s, d1); 576 omega_vcstore_name_index_digest(s, d2); 577 w_put(&w, d1, 32); 578 w_put(&w, d2, 32); 579 if (w.err) { free(w.p); return w.err; } 580 581 size_t pl = strlen(path); 582 char *tmp = malloc(pl + 5); 583 if (!tmp) { free(w.p); return OMEGA_VCS_VCSTORE_NOMEM; } 584 memcpy(tmp, path, pl); memcpy(tmp + pl, ".tmp", 5); 585 int rc = OMEGA_VCS_VCSTORE_IO; 586 FILE *f = fopen(tmp, "wb"); 587 if (f) { 588 int ok = fwrite(w.p, 1, w.n, f) == w.n && fflush(f) == 0 && fsync(fileno(f)) == 0; 589 ok = (fclose(f) == 0) && ok; 590 if (ok && rename(tmp, path) == 0) rc = OMEGA_VCS_OK; 591 else remove(tmp); 592 } 593 free(tmp); free(w.p); 594 return rc; 595 } 596 597 typedef struct { const uint8_t *b; size_t n, pos; int bad; } rdr_t; 598 599 static const uint8_t *r_take(rdr_t *r, size_t k) { 600 if (r->bad || r->n - r->pos < k) { r->bad = 1; return NULL; } 601 const uint8_t *p = r->b + r->pos; r->pos += k; 602 return p; 603 } 604 static uint64_t r_u64(rdr_t *r) { 605 const uint8_t *p = r_take(r, 8); 606 if (!p) return 0; 607 uint64_t v = 0; 608 for (int i = 0; i < 8; i++) v = v << 8 | p[i]; 609 return v; 610 } 611 612 /* Append an object that is already known to sort after every stored one. */ 613 static int vcs_append(OmegaVcStore *s, const OmegaVcRecord *rec, uint8_t kind, const uint8_t id[32]) { 614 uint8_t *cp = malloc(rec->canonical_len); 615 if (!cp) return OMEGA_VCS_VCSTORE_NOMEM; 616 memcpy(cp, rec->canonical, rec->canonical_len); 617 if (s->count == s->cap) { 618 size_t nc = s->cap ? s->cap * 2 : 16; 619 if (nc > SIZE_MAX / sizeof *s->objs) { free(cp); return OMEGA_VCS_VCSTORE_NOMEM; } 620 OmegaVcObject *n = realloc(s->objs, nc * sizeof *s->objs); 621 if (!n) { free(cp); return OMEGA_VCS_VCSTORE_NOMEM; } 622 s->objs = n; s->cap = nc; 623 } 624 OmegaVcObject *o = &s->objs[s->count++]; 625 memcpy(o->semantic_id, rec->vc.semantic_id, 32); 626 memcpy(o->vc_id, id, 32); 627 o->admission_kind = kind; 628 o->bytes = cp; 629 o->len = rec->canonical_len; 630 return OMEGA_VCS_OK; 631 } 632 633 static int parse_store(OmegaVcStore *t, const uint8_t *b, size_t n) { 634 rdr_t r = { b, n, 0, 0 }; 635 const uint8_t *m = r_take(&r, FILE_MAGIC_LEN); 636 if (!m || memcmp(m, FILE_MAGIC, FILE_MAGIC_LEN) != 0) return OMEGA_VCS_VCSTORE_MALFORMED; /* VC1S:load-magic */ 637 uint64_t count = r_u64(&r); 638 if (r.bad || count > (n - r.pos) / 9) return OMEGA_VCS_VCSTORE_MALFORMED; 639 OmegaVcRecord *rec = malloc(sizeof *rec); 640 if (!rec) return OMEGA_VCS_VCSTORE_NOMEM; 641 int rc = OMEGA_VCS_OK; 642 for (uint64_t i = 0; i < count && !rc; i++) { 643 const uint8_t *kp = r_take(&r, 1); 644 uint64_t len = r_u64(&r); 645 if (r.bad || len > n - r.pos) { rc = OMEGA_VCS_VCSTORE_MALFORMED; break; } 646 uint8_t kind = *kp; 647 if (kind != OMEGA_VCS_ADMISSION_VERIFIED && kind != OMEGA_VCS_ADMISSION_BOOTSTRAP) { rc = OMEGA_VCS_VCSTORE_MALFORMED; break; } /* VC1S:load-kind */ 648 const uint8_t *ob = r_take(&r, (size_t)len); 649 rc = omega_vc_decode(ob, (size_t)len, &rec->vc); 650 if (rc) break; 651 if (kind == OMEGA_VCS_ADMISSION_BOOTSTRAP && !omega_genesis_contains(rec->vc.semantic_id)) { rc = OMEGA_VCS_GENESIS_NOT_LISTED; break; } /* VC1S:load-genesis */ 652 if (t->count > 0 && memcmp(t->objs[t->count - 1].semantic_id, rec->vc.semantic_id, 32) >= 0) { rc = OMEGA_VCS_VCSTORE_MALFORMED; break; } /* VC1S:load-order */ 653 rec->canonical = ob; 654 rec->canonical_len = (size_t)len; 655 uint8_t id[32]; 656 omega_vc_compute_id(ob, (size_t)len, id); 657 rc = vcs_append(t, rec, kind, id); 658 } 659 free(rec); 660 if (rc) return rc; 661 uint64_t nn = r_u64(&r); 662 if (r.bad || nn > (n - r.pos) / 41) return OMEGA_VCS_VCSTORE_MALFORMED; 663 for (uint64_t i = 0; i < nn && !rc; i++) { 664 uint64_t l = r_u64(&r); 665 if (r.bad || l == 0 || l > MAX_NAME) return OMEGA_VCS_VCSTORE_MALFORMED; 666 const uint8_t *nb = r_take(&r, (size_t)l); 667 const uint8_t *id = r_take(&r, 32); 668 if (!nb || !id) return OMEGA_VCS_VCSTORE_MALFORMED; 669 char name[MAX_NAME + 1]; 670 memcpy(name, nb, (size_t)l); 671 name[l] = 0; 672 if (strlen(name) != l || !name_valid(name)) return OMEGA_VCS_VCSTORE_MALFORMED; 673 size_t di; 674 if (!vcs_find(t, id, &di)) return OMEGA_VCS_UNVERIFIED_DEPENDENCY; /* VC1S:load-name-id */ 675 if (t->n_names > 0 && strcmp(t->names[t->n_names - 1].name, name) >= 0) return OMEGA_VCS_VCSTORE_MALFORMED; 676 rc = name_insert_at(t, t->n_names, name, id); /* VC1S:load-names */ 677 } 678 if (rc) return rc; 679 const uint8_t *tr = r_take(&r, 64); 680 if (!tr) return OMEGA_VCS_VCSTORE_MALFORMED; 681 if (r.pos != r.n) return OMEGA_VCS_VCSTORE_MALFORMED; /* VC1S:load-trailing */ 682 rc = vcs_graph_check(t); /* VC1S:load-graph */ 683 if (rc) return rc; 684 uint8_t d1[32], d2[32]; 685 omega_vcstore_digest(t, d1); 686 omega_vcstore_name_index_digest(t, d2); 687 if (memcmp(d1, tr, 32) != 0) return OMEGA_VCS_VCSTORE_ID_MISMATCH; /* VC1S:load-digest-obj */ 688 if (memcmp(d2, tr + 32, 32) != 0) return OMEGA_VCS_VCSTORE_ID_MISMATCH; /* VC1S:load-digest-names */ 689 return OMEGA_VCS_OK; 690 } 691 692 int omega_vcstore_load(OmegaVcStore *s, const char *path) { 693 if (!s || !path) return OMEGA_VCS_VCSTORE_MALFORMED; 694 FILE *f = fopen(path, "rb"); 695 if (!f) return OMEGA_VCS_VCSTORE_IO; 696 uint8_t *buf = NULL; 697 size_t n = 0, cap = 0; 698 int rc = OMEGA_VCS_OK; 699 for (;;) { 700 if (n == cap) { 701 size_t nc = cap ? cap * 2 : 4096; 702 uint8_t *nb = realloc(buf, nc); 703 if (!nb) { rc = OMEGA_VCS_VCSTORE_NOMEM; break; } 704 buf = nb; cap = nc; 705 } 706 size_t got = fread(buf + n, 1, cap - n, f); 707 n += got; 708 if (got == 0) { if (ferror(f)) rc = OMEGA_VCS_VCSTORE_IO; break; } 709 } 710 fclose(f); 711 if (rc) { free(buf); return rc; } 712 OmegaVcStore t; 713 omega_vcstore_init(&t); 714 rc = parse_store(&t, buf ? buf : ZERO32, n); 715 free(buf); 716 if (rc) { 717 omega_vcstore_destroy(&t); /* VC1S:load-fail */ 718 return rc; 719 } 720 omega_vcstore_destroy(s); 721 *s = t; 722 return OMEGA_VCS_OK; 723 }