aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / svc / allen_code.c
| 1 | /* allen_code.c -- Code from the Spark, the kernel side; see allen_code.h. */ |
| 2 | #include "allen_code.h" |
| 3 | |
| 4 | static int digit(char c) { return c >= '0' && c <= '9'; } |
| 5 | |
| 6 | /* a decimal number that fits 32 bits (at most 10 digits), then p past it */ |
| 7 | static int num(const char **p, uint32_t *v) |
| 8 | { |
| 9 | const char *s = *p; |
| 10 | uint64_t x = 0; |
| 11 | unsigned k = 0; |
| 12 | while (digit(*s) && k < 11) |
| 13 | x = x * 10u + (uint64_t)(*s++ - '0'), k++; |
| 14 | if (!k || k > 10 || x > 0xffffffffu) |
| 15 | return 0; |
| 16 | *v = (uint32_t)x, *p = s; |
| 17 | return 1; |
| 18 | } |
| 19 | |
| 20 | static int hex8(const char **p, uint32_t *v) |
| 21 | { |
| 22 | const char *s = *p; |
| 23 | uint32_t x = 0; |
| 24 | for (unsigned k = 0; k < 8; k++, s++) { |
| 25 | char c = *s; |
| 26 | unsigned d = digit(c) ? (unsigned)(c - '0') : c >= 'a' && c <= 'f' ? (unsigned)(c - 'a' + 10) : 16u; |
| 27 | if (d > 15) |
| 28 | return 0; |
| 29 | x = x << 4 | d; |
| 30 | } |
| 31 | *v = x, *p = s; |
| 32 | return 1; |
| 33 | } |
| 34 | |
| 35 | uint32_t ck_code_crc32(const void *p, size_t n) |
| 36 | { |
| 37 | const uint8_t *b = p; |
| 38 | uint32_t c = 0xffffffffu; |
| 39 | for (size_t i = 0; i < n; i++) { |
| 40 | c ^= b[i]; |
| 41 | for (int k = 0; k < 8; k++) |
| 42 | c = c & 1u ? (c >> 1) ^ 0xedb88320u : c >> 1; |
| 43 | } |
| 44 | return ~c; |
| 45 | } |
| 46 | |
| 47 | void ck_code_begin(ck_code_rx *rx, uint32_t seq) |
| 48 | { |
| 49 | rx->seq = seq; |
| 50 | rx->n = rx->bytes = rx->crc = rx->got = rx->refused = 0; |
| 51 | rx->lang[0] = 0; |
| 52 | for (unsigned i = 0; i < CK_CODE_PIECES; i++) |
| 53 | rx->have[i] = 0; |
| 54 | rx->text[0] = 0; |
| 55 | } |
| 56 | |
| 57 | static int refuse(ck_code_rx *rx) |
| 58 | { |
| 59 | rx->refused++; |
| 60 | return -1; |
| 61 | } |
| 62 | |
| 63 | int ck_code_take(ck_code_rx *rx, const char *line) |
| 64 | { |
| 65 | if (!line || line[0] != 'C' || line[1] != 'O' || line[2] != 'D' || line[3] != 'E' || line[4] != ' ') |
| 66 | return 0; |
| 67 | const char *p = line + 5; |
| 68 | uint32_t seq, i, n, bytes, crc; |
| 69 | if (!num(&p, &seq) || *p++ != ' ' || !num(&p, &i) || *p++ != '/' || !num(&p, &n) || *p++ != ' ' || |
| 70 | !num(&p, &bytes) || *p++ != ' ' || !hex8(&p, &crc) || *p++ != ' ') |
| 71 | return refuse(rx); |
| 72 | if (seq != rx->seq || !bytes || bytes > CK_CODE_MAX || n != (bytes + CK_CODE_PIECE - 1) / CK_CODE_PIECE || |
| 73 | i < 1 || i > n) |
| 74 | return refuse(rx); |
| 75 | if (rx->n && (n != rx->n || bytes != rx->bytes || crc != rx->crc)) |
| 76 | return refuse(rx); /* the head must agree with the pieces before it */ |
| 77 | /* the language: one word of [a-z0-9+#._-], or "-" for none */ |
| 78 | char lang[CK_CODE_LANG]; |
| 79 | unsigned ln = 0; |
| 80 | while (*p && *p != ' ') { |
| 81 | char c = *p++; |
| 82 | int ok = (c >= 'a' && c <= 'z') || digit(c) || c == '+' || c == '#' || c == '.' || c == '_' || c == '-'; |
| 83 | if (!ok || ln + 1 >= sizeof lang) |
| 84 | return refuse(rx); |
| 85 | lang[ln++] = c; |
| 86 | } |
| 87 | lang[ln] = 0; |
| 88 | if (!ln || *p++ != ' ') |
| 89 | return refuse(rx); |
| 90 | if (ln == 1 && lang[0] == '-') |
| 91 | lang[0] = 0; |
| 92 | /* the piece, unescaped into place */ |
| 93 | uint32_t want = i < n ? CK_CODE_PIECE : bytes - (n - 1) * CK_CODE_PIECE; |
| 94 | char piece[CK_CODE_PIECE]; |
| 95 | uint32_t k = 0; |
| 96 | while (*p) { |
| 97 | char c = *p++; |
| 98 | if (c == '\\') { |
| 99 | c = *p++; |
| 100 | if (c == 'n') |
| 101 | c = '\n'; |
| 102 | else if (c != '\\') |
| 103 | return refuse(rx); |
| 104 | } else if ((unsigned char)c < 32 || (unsigned char)c > 126) { |
| 105 | return refuse(rx); |
| 106 | } |
| 107 | if (k >= want) |
| 108 | return refuse(rx); |
| 109 | piece[k++] = c; |
| 110 | } |
| 111 | if (k != want) |
| 112 | return refuse(rx); |
| 113 | if (!rx->n) { |
| 114 | rx->n = n, rx->bytes = bytes, rx->crc = crc; |
| 115 | for (unsigned j = 0; j <= ln; j++) |
| 116 | rx->lang[j] = lang[j]; |
| 117 | } |
| 118 | char *at = rx->text + (i - 1) * CK_CODE_PIECE; |
| 119 | for (uint32_t j = 0; j < k; j++) |
| 120 | at[j] = piece[j]; |
| 121 | if (!rx->have[i - 1]) { |
| 122 | rx->have[i - 1] = 1; |
| 123 | rx->got++; |
| 124 | } |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | uint32_t ck_code_missing(const ck_code_rx *rx) |
| 129 | { |
| 130 | if (!rx->n) |
| 131 | return 1; |
| 132 | for (uint32_t i = 0; i < rx->n; i++) |
| 133 | if (!rx->have[i]) |
| 134 | return i + 1; |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int ck_code_state(ck_code_rx *rx) |
| 139 | { |
| 140 | if (!rx->n) |
| 141 | return CK_CODE_NONE; |
| 142 | if (rx->got < rx->n) |
| 143 | return CK_CODE_PART; |
| 144 | rx->text[rx->bytes] = 0; |
| 145 | if (ck_code_crc32(rx->text, rx->bytes) != rx->crc) |
| 146 | return CK_CODE_BAD; |
| 147 | for (uint32_t i = 0; i < rx->bytes; i++) |
| 148 | if (!rx->text[i]) /* a NUL can never be sent (the pieces are printable), but never trust the sum alone */ |
| 149 | return CK_CODE_BAD; |
| 150 | return ck_code_lines(rx->text, rx->bytes) <= CK_CODE_LINES ? CK_CODE_WHOLE : CK_CODE_BAD; |
| 151 | } |
| 152 | |
| 153 | uint32_t ck_code_lines(const char *text, uint32_t bytes) |
| 154 | { |
| 155 | uint32_t n = 0; |
| 156 | for (uint32_t i = 0; i < bytes; i++) |
| 157 | n += text[i] == '\n'; |
| 158 | return n + (bytes && text[bytes - 1] != '\n'); |
| 159 | } |
| 160 | |
| 161 | uint32_t ck_code_rows(const char *text, uint32_t bytes, uint32_t cols, uint32_t *starts, uint16_t *lens, |
| 162 | uint16_t *line_of, uint32_t max) |
| 163 | { |
| 164 | uint32_t rows = 0, line = 1, i = 0; |
| 165 | if (!cols) |
| 166 | return 0; |
| 167 | while (i < bytes && rows < max) { |
| 168 | uint32_t e = i; |
| 169 | while (e < bytes && text[e] != '\n') |
| 170 | e++; |
| 171 | /* line [i, e) in rows of at most cols */ |
| 172 | uint32_t s = i; |
| 173 | int first = 1; |
| 174 | do { |
| 175 | uint32_t take = e - s; |
| 176 | if (take > cols) { |
| 177 | take = cols; |
| 178 | for (uint32_t j = cols; j > cols / 2; j--) |
| 179 | if (text[s + j - 1] == ' ') { |
| 180 | take = j; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | starts[rows] = s; |
| 185 | lens[rows] = (uint16_t)take; |
| 186 | line_of[rows] = first ? (uint16_t)(line < 65535u ? line : 65535u) : 0; |
| 187 | rows++; |
| 188 | first = 0; |
| 189 | s += take; |
| 190 | } while (s < e && rows < max); |
| 191 | i = e + 1; |
| 192 | line++; |
| 193 | } |
| 194 | return rows; |
| 195 | } |
| 196 | |
| 197 | uint32_t ck_code_scroll(uint32_t top, int32_t delta, uint32_t rows, uint32_t page) |
| 198 | { |
| 199 | int64_t t = (int64_t)top + delta, last = rows > page ? (int64_t)(rows - page) : 0; |
| 200 | return (uint32_t)(t < 0 ? 0 : t > last ? last : t); |
| 201 | } |
| 202 | |
| 203 | void ck_code_wait_begin(ck_code_wait *w, uint64_t now_ms) |
| 204 | { |
| 205 | w->asked_ms = w->heard_ms = now_ms; |
| 206 | w->slow_said = 0; |
| 207 | } |
| 208 | |
| 209 | void ck_code_wait_heard(ck_code_wait *w, uint64_t now_ms) { w->heard_ms = now_ms; } |
| 210 | |
| 211 | int ck_code_wait_tick(ck_code_wait *w, uint64_t now_ms) |
| 212 | { |
| 213 | if (now_ms - w->heard_ms >= CK_CODE_GONE_MS) |
| 214 | return CK_CODE_WAIT_GONE; |
| 215 | if (!w->slow_said && now_ms - w->asked_ms >= CK_CODE_SLOW_MS) { |
| 216 | w->slow_said = 1; |
| 217 | return CK_CODE_WAIT_SLOW; |
| 218 | } |
| 219 | return CK_CODE_WAIT_NOTHING; |
| 220 | } |