interplane.Get Omega
Read in your language

CUDApp

CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.

Brought by drakestapleton

branch main Branches Tags History

CUDApp / src / omega_path_check.h

86 lines · 3460 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * omega_path_check.h: a checked copy of a file system path that came from outside the program (an
5 * environment variable, a command's output, a caller's argument) before anything opens it.
6 *
7 * A path is accepted only when it is 1..cap-1 bytes long, every byte is in the POSIX portable file name set
8 * [A-Za-z0-9._-] or one of / + , = @ : ~ (no space, quote, control, shell or non-ASCII byte), and no
9 * component is "..". Absolute and relative paths are both accepted. The default path asks Omega for the
10 * index of the first 0 inside the destination capacity, then asks Omega to classify and store that bounded
11 * view. Neither call walks the pointer in C. A refused path leaves out as the empty string.
12 */
13#ifndef OMEGA_PATH_CHECK_H
14#define OMEGA_PATH_CHECK_H
15
16#include <stddef.h>
17#include "cudapp_native.h"
18
19#if defined(CUDAPP_MIGRATION_PENDING_LEFTOVERS) && CUDAPP_MIGRATION_PENDING_LEFTOVERS
20
21/* c when it may appear in a checked path, -1 otherwise */
22static inline int omega_path_byte(int c) {
23 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) return c;
24 switch (c) {
25 case '.': case '_': case '-': case '/': case '+': case ',': case '=': case '@': case ':': case '~':
26 return c;
27 default:
28 return -1;
29 }
30}
31
32/* 0 with out = in when in is an acceptable path shorter than cap; -1 with out = "" otherwise */
33static inline int omega_path_copy(char *out, size_t cap, const char *in) {
34 if (!out || cap == 0) return -1;
35 out[0] = '\0';
36 if (!in || !in[0]) return -1;
37 size_t k = 0;
38 for (; in[k]; k++) {
39 int b = omega_path_byte((unsigned char)in[k]);
40 if (b < 0 || k + 1 >= cap) {
41 out[0] = '\0';
42 return -1;
43 }
44 out[k] = (char)b;
45 }
46 out[k] = '\0';
47 for (size_t i = 0; i < k;) {
48 size_t j = i;
49 while (j < k && out[j] != '/') j++;
50 if (j - i == 2 && out[i] == '.' && out[i + 1] == '.') {
51 out[0] = '\0';
52 return -1;
53 }
54 i = j + 1;
55 }
56 return 0;
57}
58#else
59/* Default path. Length, classification and stores are Omega (src/cudapp_freest_pathcopy.c).
60 * omega2_pathlen stops at the first 0 inside cap and never reads past that bound, so a NUL-terminated
61 * source is not read past its allocation. omega2_pathcopy then receives exactly the bytes through that 0.
62 * Empty NULL is a refusal here; a nonempty NULL is refused by the binding. */
63uint32_t omega2_pathlen(uint64_t *result, const uint8_t *a, uint64_t a_n);
64uint32_t omega2_pathcopy(uint64_t *result, const uint8_t *src, uint64_t src_n, uint8_t *dst, uint64_t dst_n);
65static inline int omega_path_byte(int c) {
66 uint64_t value = 256;
67 if (omega_leftovers(&value, 0, (uint64_t)(int64_t)c, 0, 0, 0) != OMEGA_OK)
68 return -1;
69 return value == 256 ? -1 : (int)value;
70}
71static inline int omega_path_copy(char *out, size_t cap, const char *in) {
72 uint64_t n = 0;
73 uint64_t wrote = 0;
74 if (!out || cap == 0) return -1;
75 out[0] = '\0';
76 if (!in) return -1;
77 if (omega2_pathlen(&n, (const uint8_t *)in, (uint64_t)cap) != 0 || n == cap) return -1;
78 if (omega2_pathcopy(&wrote, (const uint8_t *)in, n + 1, (uint8_t *)out, (uint64_t)cap) != 0 || wrote != 1) {
79 out[0] = '\0';
80 return -1;
81 }
82 return 0;
83}
84#endif
85
86#endif /* OMEGA_PATH_CHECK_H */