interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / research / atemporal / at0 / evaluator / src / at0e_text.c

157 lines · 6070 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/* Byte rules, tokens and digests for the AT-0 evaluator (AT0_CASE_V1 sections 1, 2, 5). */
18#include "at0e.h"
19#include "sha256.h"
20#include <stdarg.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25int textfile_from_bytes(const uint8_t *b, size_t len, textfile *tf, const char **why) {
26 memset(tf, 0, sizeof *tf);
27 *why = NULL;
28 if (len == 0) { *why = "empty file"; return 1; }
29 if (b[len - 1] != '\n') { *why = "last line not LF-terminated"; return 2; }
30 size_t nl = 0;
31 for (size_t i = 0; i < len; i++) {
32 uint8_t c = b[i];
33 if (c == '\n') { nl++; continue; }
34 if (c < 0x20 || c > 0x7e) { *why = c == '\r' ? "CR byte" : c == '\t' ? "tab byte" : "byte outside 0x20..0x7E"; return 3; }
35 }
36 tf->bytes = malloc(len); memcpy(tf->bytes, b, len); tf->len = len;
37 tf->lines = calloc(nl, sizeof(char *)); tf->n = nl;
38 size_t start = 0, li = 0;
39 for (size_t i = 0; i < len; i++) {
40 if (b[i] != '\n') continue;
41 size_t l = i - start;
42 if (l == 0) { *why = "blank line"; return 4; }
43 if (b[start] == ' ' || b[i - 1] == ' ') { *why = "leading or trailing space"; return 5; }
44 for (size_t j = start + 1; j < i; j++) if (b[j] == ' ' && b[j - 1] == ' ') { *why = "double space"; return 6; }
45 if (l >= AT0E_MAX_LINE) { *why = "line too long"; return 7; }
46 tf->lines[li] = malloc(l + 1); memcpy(tf->lines[li], b + start, l); tf->lines[li][l] = 0;
47 li++; start = i + 1;
48 }
49 return 0;
50}
51int textfile_read(const char *path, textfile *tf, const char **why) {
52 memset(tf, 0, sizeof *tf);
53 FILE *f = fopen(path, "rb");
54 if (!f) { *why = "cannot open"; return 100; }
55 uint8_t *buf = NULL; size_t cap = 0, len = 0;
56 for (;;) {
57 if (len == cap) { cap = cap ? cap * 2 : 65536; buf = realloc(buf, cap); }
58 size_t got = fread(buf + len, 1, cap - len, f);
59 if (got == 0) break;
60 len += got;
61 if (len > (1u << 26)) { fclose(f); free(buf); *why = "file too large"; return 101; }
62 }
63 fclose(f);
64 int rc = textfile_from_bytes(buf, len, tf, why);
65 free(buf);
66 return rc;
67}
68void textfile_free(textfile *tf) {
69 if (tf->lines) { for (size_t i = 0; i < tf->n; i++) free(tf->lines[i]); free(tf->lines); }
70 free(tf->bytes);
71 memset(tf, 0, sizeof *tf);
72}
73
74/* Split on single spaces. The byte rules already forbid double/leading/trailing spaces. */
75int split_tokens(const char *line, char **tok, int max) {
76 static char buf[AT0E_MAX_LINE];
77 size_t l = strlen(line);
78 if (l >= sizeof buf) return -1;
79 memcpy(buf, line, l + 1);
80 int n = 0; char *p = buf;
81 while (*p) {
82 if (n >= max) return -1;
83 tok[n++] = p;
84 char *sp = strchr(p, ' ');
85 if (!sp) break;
86 *sp = 0; p = sp + 1;
87 if (*p == 0) return -1;
88 }
89 return n;
90}
91
92static int in(char c, const char *set) { return strchr(set, c) != NULL && c != 0; }
93int is_label(const char *s) {
94 size_t l = strlen(s);
95 if (l < 1 || l > 32) return 0;
96 if (!(s[0] >= 'a' && s[0] <= 'z')) return 0;
97 for (size_t i = 1; i < l; i++) if (!((s[i] >= 'a' && s[i] <= 'z') || (s[i] >= '0' && s[i] <= '9') || s[i] == '_')) return 0;
98 return 1;
99}
100int is_name(const char *s) {
101 size_t l = strlen(s);
102 if (l < 1 || l > 64) return 0;
103 if (!((s[0] >= 'A' && s[0] <= 'Z') || (s[0] >= 'a' && s[0] <= 'z') || (s[0] >= '0' && s[0] <= '9'))) return 0;
104 for (size_t i = 1; i < l; i++) {
105 char c = s[i];
106 if (!((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || in(c, "_.-"))) return 0;
107 }
108 return 1;
109}
110static int is_lhex(const char *s, size_t want) {
111 if (strlen(s) != want) return 0;
112 for (size_t i = 0; i < want; i++) if (!((s[i] >= '0' && s[i] <= '9') || (s[i] >= 'a' && s[i] <= 'f'))) return 0;
113 return 1;
114}
115int is_digest(const char *s) { return is_lhex(s, 64); }
116int is_sha1hex(const char *s) { return is_lhex(s, 40); }
117int is_text200(const char *s) {
118 size_t l = strlen(s);
119 if (l < 1 || l > 200) return 0;
120 if (s[0] == ' ' || s[l - 1] == ' ') return 0;
121 for (size_t i = 0; i < l; i++) if (s[i] < 0x20 || s[i] > 0x7e) return 0;
122 return 1;
123}
124int hex_to_u64(const char *s16, uint64_t *out) {
125 if (!is_lhex(s16, 16)) return 0;
126 uint64_t v = 0;
127 for (int i = 0; i < 16; i++) { char c = s16[i]; v = (v << 4) | (uint64_t)(c <= '9' ? c - '0' : c - 'a' + 10); }
128 *out = v;
129 return 1;
130}
131
132static void to_hex(const uint8_t d[32], char out[65]) {
133 static const char *h = "0123456789abcdef";
134 for (int i = 0; i < 32; i++) { out[2 * i] = h[d[i] >> 4]; out[2 * i + 1] = h[d[i] & 15]; }
135 out[64] = 0;
136}
137void sha256_hex(const uint8_t *data, size_t len, char out[65]) {
138 uint8_t d[32]; sha256_hash(data, len, d); to_hex(d, out);
139}
140void sha256_tagged_hex(const char *domain, const uint8_t *data, size_t len, char out[65]) {
141 sha256_ctx c; uint8_t d[32]; uint8_t z = 0;
142 sha256_init(&c);
143 sha256_update(&c, (const uint8_t *)domain, strlen(domain));
144 sha256_update(&c, &z, 1);
145 sha256_update(&c, data, len);
146 sha256_final(&c, d);
147 to_hex(d, out);
148}
149
150void findings_add(findings *fs, const char *code, const char *fmt, ...) {
151 if (fs->n >= 256) return;
152 finding *f = &fs->f[fs->n++];
153 snprintf(f->code, sizeof f->code, "%s", code);
154 va_list ap; va_start(ap, fmt);
155 vsnprintf(f->detail, sizeof f->detail, fmt, ap);
156 va_end(ap);
157}