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 / omega_codec.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 | /* Binary graph codec backed by pure seeded OM-2 (om/visor/codec_om2.om & canonical_om2.om). */ |
| 18 | #include "omega_codec.h" |
| 19 | #include "omega_canonical.h" |
| 20 | #include "omega_core.h" |
| 21 | #include "omega_validate.h" |
| 22 | #include "omega_native.h" |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <ctype.h> |
| 27 | |
| 28 | static uint16_t rd_u16_be(const uint8_t *buf, size_t buf_len, size_t pos) { |
| 29 | uint64_t r = 0; |
| 30 | if (omega2_om_codec_u16_rd(&r, buf, buf_len, pos) != OMEGA_OK) abort(); |
| 31 | return (uint16_t)r; |
| 32 | } |
| 33 | |
| 34 | static uint32_t rd_u32_be(const uint8_t *buf, size_t buf_len, size_t pos) { |
| 35 | uint64_t r = 0; |
| 36 | if (omega2_om_codec_u32_rd(&r, buf, buf_len, pos) != OMEGA_OK) abort(); |
| 37 | return (uint32_t)r; |
| 38 | } |
| 39 | |
| 40 | int omega_graph_serialize_binary(const OmegaGraph *graph, uint8_t *out_buf, size_t max_len, size_t *out_len) { |
| 41 | if (!graph || !out_buf || !out_len || max_len == 0) return -1; |
| 42 | uint64_t r = 0; |
| 43 | if (omega2_om_codec_hdr_enc(&r, graph->object_count, out_buf, max_len, max_len) != OMEGA_OK) abort(); |
| 44 | if ((int64_t)r < 0) return -1; |
| 45 | size_t pos = (size_t)r; |
| 46 | |
| 47 | for (uint16_t i = 0; i < graph->object_count; ++i) { |
| 48 | uint8_t cbuf[4096]; |
| 49 | size_t clen = 0; |
| 50 | if (omega_canonical_encode(&graph->objects[i], cbuf, sizeof(cbuf), &clen) != 0) { |
| 51 | return -1; |
| 52 | } |
| 53 | if (pos + 4 + clen > max_len) return -1; |
| 54 | if (omega2_om_canon_write_u32be(&r, (uint64_t)clen, out_buf, max_len, pos, max_len) != OMEGA_OK) abort(); |
| 55 | if ((int64_t)r < 0) return -1; |
| 56 | pos = (size_t)r; |
| 57 | memcpy(&out_buf[pos], cbuf, clen); |
| 58 | pos += clen; |
| 59 | } |
| 60 | |
| 61 | *out_len = pos; |
| 62 | return 0; |
| 63 | } |
| 64 | |
| 65 | int omega_graph_deserialize_binary(const uint8_t *in_buf, size_t in_len, OmegaGraph *out_graph) { |
| 66 | if (!in_buf || !out_graph || in_len < 6) return -1; |
| 67 | uint64_t r = 0; |
| 68 | if (omega2_om_codec_hdr_dec(&r, in_buf, in_len, in_len, OMEGA_MAX_GRAPH_OBJECTS) != OMEGA_OK) abort(); |
| 69 | if ((int64_t)r < 0) return -1; |
| 70 | uint16_t count = (uint16_t)r; |
| 71 | size_t pos = 6; |
| 72 | |
| 73 | memset(out_graph, 0, sizeof(OmegaGraph)); |
| 74 | |
| 75 | for (uint16_t i = 0; i < count; ++i) { |
| 76 | if (pos + 4 > in_len) return -1; |
| 77 | uint32_t clen = rd_u32_be(in_buf, in_len, pos); |
| 78 | pos += 4; |
| 79 | if (pos + clen > in_len || clen < 6) return -1; |
| 80 | |
| 81 | if (omega2_om_codec_obj_hdr_chk(&r, in_buf, in_len, pos, clen) != OMEGA_OK) abort(); |
| 82 | if ((int64_t)r < 0) return -1; |
| 83 | |
| 84 | const uint8_t *cbuf = &in_buf[pos]; |
| 85 | OmegaObject *obj = &out_graph->objects[out_graph->object_count++]; |
| 86 | obj->kind = (SemanticKind)r; |
| 87 | size_t cpos = 6; |
| 88 | |
| 89 | if (cpos + 2 > clen) return -1; |
| 90 | obj->attr_count = rd_u16_be(cbuf, clen, cpos); |
| 91 | cpos += 2; |
| 92 | if (obj->attr_count > OMEGA_MAX_ATTRIBUTES) return -1; |
| 93 | for (uint16_t a = 0; a < obj->attr_count; ++a) { |
| 94 | if (cpos + 1 > clen) return -1; |
| 95 | uint8_t klen = cbuf[cpos++]; |
| 96 | if (klen >= OMEGA_MAX_KEY_LEN) return -1; |
| 97 | if (cpos + klen + 2 > clen) return -1; |
| 98 | memcpy(obj->attributes[a].key, &cbuf[cpos], klen); |
| 99 | obj->attributes[a].key[klen] = '\0'; |
| 100 | cpos += klen; |
| 101 | obj->attributes[a].val_len = rd_u16_be(cbuf, clen, cpos); |
| 102 | cpos += 2; |
| 103 | if (obj->attributes[a].val_len > OMEGA_MAX_VAL_LEN) return -1; |
| 104 | if (cpos + obj->attributes[a].val_len > clen) return -1; |
| 105 | memcpy(obj->attributes[a].value, &cbuf[cpos], obj->attributes[a].val_len); |
| 106 | cpos += obj->attributes[a].val_len; |
| 107 | } |
| 108 | |
| 109 | if (cpos + 2 > clen) return -1; |
| 110 | obj->rel_count = rd_u16_be(cbuf, clen, cpos); |
| 111 | cpos += 2; |
| 112 | if (obj->rel_count > OMEGA_MAX_RELATIONS) return -1; |
| 113 | for (uint16_t ri = 0; ri < obj->rel_count; ++ri) { |
| 114 | if (cpos + 2 + OMEGA_ID_BYTES > clen) return -1; |
| 115 | obj->relations[ri].kind = rd_u16_be(cbuf, clen, cpos); |
| 116 | cpos += 2; |
| 117 | memcpy(obj->relations[ri].target_id.bytes, &cbuf[cpos], OMEGA_ID_BYTES); |
| 118 | cpos += OMEGA_ID_BYTES; |
| 119 | } |
| 120 | |
| 121 | if (cpos + 2 > clen) return -1; |
| 122 | obj->const_count = rd_u16_be(cbuf, clen, cpos); |
| 123 | cpos += 2; |
| 124 | if (obj->const_count > OMEGA_MAX_CONSTRAINTS) return -1; |
| 125 | for (uint16_t c = 0; c < obj->const_count; ++c) { |
| 126 | if (cpos + 4 > clen) return -1; |
| 127 | obj->constraints[c].kind = rd_u16_be(cbuf, clen, cpos); |
| 128 | cpos += 2; |
| 129 | obj->constraints[c].payload_len = rd_u16_be(cbuf, clen, cpos); |
| 130 | cpos += 2; |
| 131 | if (obj->constraints[c].payload_len > sizeof obj->constraints[c].payload) return -1; |
| 132 | if (cpos + obj->constraints[c].payload_len > clen) return -1; |
| 133 | memcpy(obj->constraints[c].payload, &cbuf[cpos], obj->constraints[c].payload_len); |
| 134 | cpos += obj->constraints[c].payload_len; |
| 135 | } |
| 136 | |
| 137 | if (cpos + 4 > clen) return -1; |
| 138 | obj->payload_len = rd_u32_be(cbuf, clen, cpos); |
| 139 | cpos += 4; |
| 140 | if (obj->payload_len > OMEGA_MAX_PAYLOAD_LEN) return -1; |
| 141 | if (cpos + obj->payload_len > clen) return -1; |
| 142 | memcpy(obj->payload, &cbuf[cpos], obj->payload_len); |
| 143 | cpos += obj->payload_len; |
| 144 | if (obj->kind == KIND_EFFECT) { |
| 145 | EffectPayload eff; |
| 146 | if (omega_effect_read(obj, &eff) != OMEGA_EFFECT_OK) return -1; |
| 147 | } |
| 148 | |
| 149 | omega_compute_semantic_id(obj); |
| 150 | pos += clen; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | int omega_graph_format_text(const OmegaGraph *graph, char *out_str, size_t max_len) { |
| 157 | if (!graph || !out_str || max_len == 0) return -1; |
| 158 | size_t pos = 0; |
| 159 | |
| 160 | int w = snprintf(out_str + pos, max_len - pos, "// OMEGA NON-CANONICAL TEXTUAL FORM\n// Objects: %u\n\n", graph->object_count); |
| 161 | if (w < 0 || (size_t)w >= max_len - pos) return -1; |
| 162 | pos += (size_t)w; |
| 163 | |
| 164 | for (uint16_t i = 0; i < graph->object_count; ++i) { |
| 165 | const OmegaObject *obj = &graph->objects[i]; |
| 166 | char hex[65]; |
| 167 | omega_hex_semantic_id(&obj->id, hex); |
| 168 | |
| 169 | switch (obj->kind) { |
| 170 | case KIND_TYPE: { |
| 171 | const TypePayload *tp = (const TypePayload*)obj->payload; |
| 172 | if (tp->tag == TYPE_UNSIGNED_INT) { |
| 173 | w = snprintf(out_str + pos, max_len - pos, "type U%u = UNSIGNED_INTEGER(%u); // id=%s\n", tp->width, tp->width, hex); |
| 174 | } else if (tp->tag == TYPE_BOOL) { |
| 175 | w = snprintf(out_str + pos, max_len - pos, "type Bool = BOOL; // id=%s\n", hex); |
| 176 | } else { |
| 177 | w = snprintf(out_str + pos, max_len - pos, "type Tag%u; // id=%s\n", tp->tag, hex); |
| 178 | } |
| 179 | break; |
| 180 | } |
| 181 | case KIND_VALUE: { |
| 182 | const ValuePayload *vp = (const ValuePayload*)obj->payload; |
| 183 | uint64_t v = 0; |
| 184 | for (int b = 0; b < vp->byte_len; ++b) { |
| 185 | v = (v << 8) | vp->bytes[b]; |
| 186 | } |
| 187 | w = snprintf(out_str + pos, max_len - pos, "val v_%u = %lu; // id=%s\n", i, (unsigned long)v, hex); |
| 188 | break; |
| 189 | } |
| 190 | case KIND_OPERATION: { |
| 191 | if (obj->payload_len >= sizeof(OperationPayload)) { |
| 192 | const OperationPayload *op = (const OperationPayload*)obj->payload; |
| 193 | const char *opname = (op->opcode == OP_ADD) ? "ADD" : (op->opcode == OP_SUB ? "SUB" : "OP"); |
| 194 | w = snprintf(out_str + pos, max_len - pos, "op %s = %s[WRAP]; // id=%s\n", opname, opname, hex); |
| 195 | } else if (obj->payload_len >= sizeof(ApplyPayload)) { |
| 196 | w = snprintf(out_str + pos, max_len - pos, "apply app_%u; // id=%s\n", i, hex); |
| 197 | } |
| 198 | break; |
| 199 | } |
| 200 | default: |
| 201 | w = snprintf(out_str + pos, max_len - pos, "object kind=%u; // id=%s\n", obj->kind, hex); |
| 202 | break; |
| 203 | } |
| 204 | if (w < 0 || (size_t)w >= max_len - pos) return -1; |
| 205 | pos += (size_t)w; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | int omega_graph_parse_text(const char *in_str, OmegaGraph *out_graph, char *err_msg, size_t err_msg_len) { |
| 212 | if (!in_str || !out_graph) { |
| 213 | snprintf(err_msg, err_msg_len, "Null arguments to parse_text"); |
| 214 | return -1; |
| 215 | } |
| 216 | memset(out_graph, 0, sizeof(OmegaGraph)); |
| 217 | |
| 218 | const char *p = in_str; |
| 219 | OmegaObject *u32_type = NULL; |
| 220 | OmegaObject *val_a = NULL; |
| 221 | OmegaObject *val_b = NULL; |
| 222 | OmegaObject *op_add = NULL; |
| 223 | OmegaObject *op_sub = NULL; |
| 224 | |
| 225 | while (*p) { |
| 226 | while (*p && isspace((unsigned char)*p)) p++; |
| 227 | if (!*p) break; |
| 228 | if (*p == '/' && *(p + 1) == '/') { |
| 229 | while (*p && *p != '\n') p++; |
| 230 | continue; |
| 231 | } |
| 232 | |
| 233 | if (strncmp(p, "type", 4) == 0) { |
| 234 | if (strstr(p, "UNSIGNED_INTEGER(32)") != NULL) { |
| 235 | u32_type = omega_build_type_uint(out_graph, 32); |
| 236 | } |
| 237 | while (*p && *p != '\n') p++; |
| 238 | } else if (strncmp(p, "val", 3) == 0) { |
| 239 | const char *eq = strchr(p, '='); |
| 240 | if (eq && u32_type) { |
| 241 | unsigned long v = strtoul(eq + 1, NULL, 0); |
| 242 | if (val_a == NULL) { |
| 243 | val_a = omega_build_val_uint(out_graph, &u32_type->id, 32, (uint64_t)v); |
| 244 | } else if (val_b == NULL) { |
| 245 | val_b = omega_build_val_uint(out_graph, &u32_type->id, 32, (uint64_t)v); |
| 246 | } |
| 247 | } |
| 248 | while (*p && *p != '\n') p++; |
| 249 | } else if (strncmp(p, "op", 2) == 0) { |
| 250 | if (strstr(p, "ADD") != NULL && u32_type) { |
| 251 | op_add = omega_build_op_binary(out_graph, OP_ADD, OVERFLOW_WRAP, &u32_type->id); |
| 252 | } else if (strstr(p, "SUB") != NULL && u32_type) { |
| 253 | op_sub = omega_build_op_binary(out_graph, OP_SUB, OVERFLOW_WRAP, &u32_type->id); |
| 254 | } |
| 255 | while (*p && *p != '\n') p++; |
| 256 | } else if (strncmp(p, "apply", 5) == 0) { |
| 257 | if (op_add && val_a && val_b) { |
| 258 | omega_build_apply(out_graph, &op_add->id, &val_a->id, &val_b->id); |
| 259 | } else if (op_sub && val_a && val_b) { |
| 260 | omega_build_apply(out_graph, &op_sub->id, &val_a->id, &val_b->id); |
| 261 | } |
| 262 | while (*p && *p != '\n') p++; |
| 263 | } else { |
| 264 | while (*p && *p != '\n') p++; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |