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
omega-mv / src / turing / ty_prd.c
| 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 "turing/ty_prd.h" |
| 18 | |
| 19 | #include <math.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #define PRD_HDR 8u |
| 25 | #define PRD_REC 24u /* index, family, loc, scale */ |
| 26 | |
| 27 | static uint32_t rd32(const uint8_t *b) |
| 28 | { |
| 29 | return (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24; |
| 30 | } |
| 31 | static double rdf(const uint8_t *b) |
| 32 | { |
| 33 | uint64_t u = 0; |
| 34 | double d; |
| 35 | for (int i = 7; i >= 0; i--) |
| 36 | u = (u << 8) | b[i]; |
| 37 | memcpy(&d, &u, 8); |
| 38 | return d; |
| 39 | } |
| 40 | static void wr32(uint8_t *b, uint32_t v) |
| 41 | { |
| 42 | for (int i = 0; i < 4; i++) |
| 43 | b[i] = (uint8_t)(v >> (8 * i)); |
| 44 | } |
| 45 | static void wrf(uint8_t *b, double d) |
| 46 | { |
| 47 | uint64_t u; |
| 48 | memcpy(&u, &d, 8); |
| 49 | for (int i = 0; i < 8; i++) |
| 50 | b[i] = (uint8_t)(u >> (8 * i)); |
| 51 | } |
| 52 | |
| 53 | int ty_prd_validate(const tyq_pred *p) |
| 54 | { |
| 55 | if (!p) |
| 56 | return TYQ_E_ARG; |
| 57 | if (p->family != TYQ_FAM_GAUSS) |
| 58 | return TYQ_FAIL_PROTOCOL; |
| 59 | if (!isfinite(p->loc) || !isfinite(p->scale) || !(p->scale > 0.0)) |
| 60 | return TYQ_FAIL_PROTOCOL; |
| 61 | return TYQ_OK; |
| 62 | } |
| 63 | |
| 64 | void ty_prd_free(ty_prd *p) |
| 65 | { |
| 66 | if (!p) |
| 67 | return; |
| 68 | free(p->rec); |
| 69 | p->rec = NULL; |
| 70 | p->n = 0; |
| 71 | } |
| 72 | |
| 73 | int ty_prd_parse(const uint8_t *buf, size_t len, uint32_t first_index, uint32_t count, ty_prd *out) |
| 74 | { |
| 75 | if (!buf || !out || count == 0) |
| 76 | return TYQ_E_ARG; |
| 77 | out->n = 0; |
| 78 | out->first_index = first_index; |
| 79 | out->rec = NULL; |
| 80 | if ((uint64_t)first_index + count > UINT32_MAX) |
| 81 | return TYQ_E_ARG; |
| 82 | if (len < PRD_HDR || memcmp(buf, "PRD1", 4) != 0 || rd32(buf + 4) != 1u) |
| 83 | return TYQ_FAIL_PROTOCOL; |
| 84 | /* Every accepted record is exactly PRD_REC bytes, so the size is exact. */ |
| 85 | if (len - PRD_HDR != (size_t)count * PRD_REC) { |
| 86 | /* Still a refusal whatever the cause: truncated, trailing or missing. */ |
| 87 | return TYQ_FAIL_PROTOCOL; |
| 88 | } |
| 89 | tyq_pred *rec = calloc(count, sizeof *rec); |
| 90 | if (!rec) |
| 91 | return TYQ_E_IO; |
| 92 | const uint8_t *q = buf + PRD_HDR; |
| 93 | for (uint32_t i = 0; i < count; i++, q += PRD_REC) { |
| 94 | tyq_pred *r = &rec[i]; |
| 95 | r->index = rd32(q); |
| 96 | r->family = rd32(q + 4); |
| 97 | r->loc = rdf(q + 8); |
| 98 | r->scale = rdf(q + 16); |
| 99 | if (r->index != first_index + i || ty_prd_validate(r) != TYQ_OK) { |
| 100 | free(rec); |
| 101 | return TYQ_FAIL_PROTOCOL; |
| 102 | } |
| 103 | } |
| 104 | out->n = count; |
| 105 | out->rec = rec; |
| 106 | return TYQ_OK; |
| 107 | } |
| 108 | |
| 109 | int ty_prd_read(const char *path, uint32_t first_index, uint32_t count, ty_prd *out) |
| 110 | { |
| 111 | if (!path || !out) |
| 112 | return TYQ_E_ARG; |
| 113 | FILE *f = fopen(path, "rb"); |
| 114 | if (!f) |
| 115 | return TYQ_E_IO; |
| 116 | size_t cap = 4096, len = 0; |
| 117 | uint8_t *buf = malloc(cap); |
| 118 | if (!buf) { |
| 119 | fclose(f); |
| 120 | return TYQ_E_IO; |
| 121 | } |
| 122 | for (;;) { |
| 123 | if (len == cap) { |
| 124 | if (cap > ((size_t)1 << 30)) { /* far beyond any legal file */ |
| 125 | free(buf); |
| 126 | fclose(f); |
| 127 | return TYQ_FAIL_PROTOCOL; |
| 128 | } |
| 129 | uint8_t *nb = realloc(buf, cap * 2); |
| 130 | if (!nb) { |
| 131 | free(buf); |
| 132 | fclose(f); |
| 133 | return TYQ_E_IO; |
| 134 | } |
| 135 | buf = nb; |
| 136 | cap *= 2; |
| 137 | } |
| 138 | size_t r = fread(buf + len, 1, cap - len, f); |
| 139 | len += r; |
| 140 | if (r == 0) |
| 141 | break; |
| 142 | } |
| 143 | int err = ferror(f); |
| 144 | fclose(f); |
| 145 | int rc = err ? TYQ_E_IO : ty_prd_parse(buf, len, first_index, count, out); |
| 146 | free(buf); |
| 147 | return rc; |
| 148 | } |
| 149 | |
| 150 | int ty_prd_encode(const ty_prd *p, uint8_t **buf, size_t *len) |
| 151 | { |
| 152 | if (!p || !buf || !len || (p->n && !p->rec)) |
| 153 | return TYQ_E_ARG; |
| 154 | *buf = NULL; |
| 155 | *len = 0; |
| 156 | for (uint32_t i = 0; i < p->n; i++) { |
| 157 | if (ty_prd_validate(&p->rec[i]) != TYQ_OK || p->rec[i].index != p->first_index + i) |
| 158 | return TYQ_FAIL_PROTOCOL; |
| 159 | } |
| 160 | size_t total = PRD_HDR + (size_t)p->n * PRD_REC; |
| 161 | uint8_t *b = malloc(total); |
| 162 | if (!b) |
| 163 | return TYQ_E_IO; |
| 164 | memcpy(b, "PRD1", 4); |
| 165 | wr32(b + 4, 1u); |
| 166 | uint8_t *q = b + PRD_HDR; |
| 167 | for (uint32_t i = 0; i < p->n; i++, q += PRD_REC) { |
| 168 | wr32(q, p->rec[i].index); |
| 169 | wr32(q + 4, p->rec[i].family); |
| 170 | wrf(q + 8, p->rec[i].loc); |
| 171 | wrf(q + 16, p->rec[i].scale); |
| 172 | } |
| 173 | *buf = b; |
| 174 | *len = total; |
| 175 | return TYQ_OK; |
| 176 | } |