| 1 |
|
| /* mds_linkref.c — link reference definition table. |
| 2 |
|
| * |
| 3 |
|
| * Simple linear-scan table backed by arena memory. CommonMark §4.7 |
| 4 |
|
| * specifies first-definition-wins; we honour that via mds_linkref_add() |
| 5 |
|
| * returning 0 for duplicates. |
| 6 |
|
| * |
| 7 |
|
| * Label normalisation (per spec): trim, lowercase ASCII, collapse runs |
| 8 |
|
| * of whitespace to a single space. Unicode case-folding deferred. |
| 9 |
|
| */ |
| 10 |
|
| |
| 11 |
|
| #include "mds_linkref.h" |
| 12 |
|
| #include <string.h> |
| 13 |
|
| #include <stdlib.h> |
| 14 |
|
| #include <ctype.h> |
| 15 |
|
| |
| 16 |
|
| /* Decode a single UTF-8 codepoint starting at s[i] (i < n). Returns the |
| 17 |
|
| * codepoint, sets *adv to the byte count consumed. On invalid bytes, |
| 18 |
|
| * returns the byte as a codepoint and adv=1 (lenient). */ |
| 19 |
1618003 |
| static unsigned cf_decode(const char* s, size_t n, size_t i, int* adv) { |
| 20 |
1618003 |
| unsigned char c = (unsigned char)s[i]; |
| 21 |
1618003 |
▶ | if (c < 0x80) { *adv = 1; return c; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0x80) { *adv = 1; return c; } |
1617982 |
21 |
1618003 |
|
| 22 |
21 |
▶ | if ((c & 0xE0) == 0xC0 && i + 1 < n && |
Branch: 2 branches
| Branch | True | False | Total |
| if ((c & 0xE0) == 0xC0 && i + 1 < n && |
18 |
3 |
21 |
| if ((c & 0xE0) == 0xC0 && i + 1 < n && |
18 |
0 |
18 |
|
| 23 |
18 |
▶ | ((unsigned char)s[i+1] & 0xC0) == 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+1] & 0xC0) == 0x80) { |
18 |
0 |
18 |
|
| 24 |
18 |
| *adv = 2; |
| 25 |
18 |
| return ((unsigned)(c & 0x1F) << 6) | ((unsigned char)s[i+1] & 0x3F); |
| 26 |
|
| } |
| 27 |
3 |
▶ | if ((c & 0xF0) == 0xE0 && i + 2 < n && |
Branch: 2 branches
| Branch | True | False | Total |
| if ((c & 0xF0) == 0xE0 && i + 2 < n && |
3 |
0 |
3 |
| if ((c & 0xF0) == 0xE0 && i + 2 < n && |
3 |
0 |
3 |
|
| 28 |
3 |
▶ | ((unsigned char)s[i+1] & 0xC0) == 0x80 && |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+1] & 0xC0) == 0x80 && |
3 |
0 |
3 |
|
| 29 |
3 |
▶ | ((unsigned char)s[i+2] & 0xC0) == 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+2] & 0xC0) == 0x80) { |
3 |
0 |
3 |
|
| 30 |
3 |
| *adv = 3; |
| 31 |
3 |
| return ((unsigned)(c & 0x0F) << 12) |
| 32 |
3 |
| | (((unsigned char)s[i+1] & 0x3F) << 6) |
| 33 |
3 |
| | ((unsigned char)s[i+2] & 0x3F); |
| 34 |
|
| } |
| 35 |
0 |
▶ | if ((c & 0xF8) == 0xF0 && i + 3 < n && |
Branch: 2 branches
| Branch | True | False | Total |
| if ((c & 0xF8) == 0xF0 && i + 3 < n && |
0 |
0 |
0 |
| if ((c & 0xF8) == 0xF0 && i + 3 < n && |
0 |
0 |
0 |
|
| 36 |
0 |
▶ | ((unsigned char)s[i+1] & 0xC0) == 0x80 && |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+1] & 0xC0) == 0x80 && |
0 |
0 |
0 |
|
| 37 |
0 |
▶ | ((unsigned char)s[i+2] & 0xC0) == 0x80 && |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+2] & 0xC0) == 0x80 && |
0 |
0 |
0 |
|
| 38 |
0 |
▶ | ((unsigned char)s[i+3] & 0xC0) == 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| ((unsigned char)s[i+3] & 0xC0) == 0x80) { |
0 |
0 |
0 |
|
| 39 |
0 |
| *adv = 4; |
| 40 |
0 |
| return ((unsigned)(c & 0x07) << 18) |
| 41 |
0 |
| | (((unsigned char)s[i+1] & 0x3F) << 12) |
| 42 |
0 |
| | (((unsigned char)s[i+2] & 0x3F) << 6) |
| 43 |
0 |
| | ((unsigned char)s[i+3] & 0x3F); |
| 44 |
|
| } |
| 45 |
0 |
| *adv = 1; |
| 46 |
0 |
| return c; |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
|
| /* Encode codepoint as UTF-8 into out; returns bytes written (0..4). */ |
| 50 |
1618000 |
| static int cf_encode(unsigned cp, char* out) { |
| 51 |
1618000 |
▶ | if (cp < 0x80) { out[0] = (char)cp; return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp < 0x80) { out[0] = (char)cp; return 1; } |
1617982 |
18 |
1618000 |
|
| 52 |
18 |
▶ | if (cp < 0x800) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp < 0x800) { |
18 |
0 |
18 |
|
| 53 |
18 |
| out[0] = (char)(0xC0 | (cp >> 6)); |
| 54 |
18 |
| out[1] = (char)(0x80 | (cp & 0x3F)); |
| 55 |
18 |
| return 2; |
| 56 |
|
| } |
| 57 |
0 |
▶ | if (cp < 0x10000) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp < 0x10000) { |
0 |
0 |
0 |
|
| 58 |
0 |
| out[0] = (char)(0xE0 | (cp >> 12)); |
| 59 |
0 |
| out[1] = (char)(0x80 | ((cp >> 6) & 0x3F)); |
| 60 |
0 |
| out[2] = (char)(0x80 | (cp & 0x3F)); |
| 61 |
0 |
| return 3; |
| 62 |
|
| } |
| 63 |
0 |
| out[0] = (char)(0xF0 | (cp >> 18)); |
| 64 |
0 |
| out[1] = (char)(0x80 | ((cp >> 12) & 0x3F)); |
| 65 |
0 |
| out[2] = (char)(0x80 | ((cp >> 6) & 0x3F)); |
| 66 |
0 |
| out[3] = (char)(0x80 | (cp & 0x3F)); |
| 67 |
0 |
| return 4; |
| 68 |
|
| } |
| 69 |
|
| |
| 70 |
|
| /* Case-fold a single codepoint to its lowercase form, writing 1..4 |
| 71 |
|
| * bytes to out. Covers ASCII, Latin-1 supplement, Latin Extended-A |
| 72 |
|
| * (selected), Greek, Cyrillic, plus a tiny exception list (ẞ → ss, |
| 73 |
|
| * µ → μ). Returns bytes written. This is the subset needed by |
| 74 |
|
| * CommonMark spec examples; full CaseFolding.txt is overkill. */ |
| 75 |
1618003 |
| static int cf_fold(unsigned cp, char* out) { |
| 76 |
|
| /* Exceptions that expand (1 codepoint -> 2 codepoints). */ |
| 77 |
1618003 |
▶ | if (cp == 0x1E9E) { out[0] = 's'; out[1] = 's'; return 2; } /* ẞ */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (cp == 0x1E9E) { out[0] = 's'; out[1] = 's'; return 2; } /* ẞ */ |
3 |
1618000 |
1618003 |
|
| 78 |
|
| |
| 79 |
|
| /* Single-codepoint mappings. */ |
| 80 |
1618000 |
▶ | if (cp >= 'A' && cp <= 'Z') cp = cp + 0x20; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cp >= 'A' && cp <= 'Z') cp = cp + 0x20; |
1617875 |
125 |
1618000 |
| if (cp >= 'A' && cp <= 'Z') cp = cp + 0x20; |
108 |
1617767 |
1617875 |
|
| 81 |
1617892 |
▶ | else if (cp == 0xB5) cp = 0x3BC; /* µ -> μ */ |
Branch: 1 branch
| Branch | True | False | Total |
| else if (cp == 0xB5) cp = 0x3BC; /* µ -> μ */ |
0 |
1617892 |
1617892 |
|
| 82 |
1617892 |
▶ | else if (cp >= 0xC0 && cp <= 0xDE && cp != 0xD7) cp += 0x20; |
Branch: 3 branches
| Branch | True | False | Total |
| else if (cp >= 0xC0 && cp <= 0xDE && cp != 0xD7) cp += 0x20; |
18 |
1617874 |
1617892 |
| else if (cp >= 0xC0 && cp <= 0xDE && cp != 0xD7) cp += 0x20; |
0 |
18 |
18 |
| else if (cp >= 0xC0 && cp <= 0xDE && cp != 0xD7) cp += 0x20; |
0 |
0 |
0 |
|
| 83 |
1617892 |
▶ | else if (cp >= 0x391 && cp <= 0x3A1) cp += 0x20; /* Α..Ρ */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (cp >= 0x391 && cp <= 0x3A1) cp += 0x20; /* Α..Ρ */ |
18 |
1617874 |
1617892 |
| else if (cp >= 0x391 && cp <= 0x3A1) cp += 0x20; /* Α..Ρ */ |
6 |
12 |
18 |
|
| 84 |
1617886 |
▶ | else if (cp >= 0x3A3 && cp <= 0x3AB) cp += 0x20; /* Σ..Ϋ */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (cp >= 0x3A3 && cp <= 0x3AB) cp += 0x20; /* Σ..Ϋ */ |
12 |
1617874 |
1617886 |
| else if (cp >= 0x3A3 && cp <= 0x3AB) cp += 0x20; /* Σ..Ϋ */ |
3 |
9 |
12 |
|
| 85 |
1617883 |
▶ | else if (cp == 0x3A2) { /* reserved, leave */ } |
Branch: 1 branch
| Branch | True | False | Total |
| else if (cp == 0x3A2) { /* reserved, leave */ } |
1617883 |
0 |
1617883 |
|
| 86 |
1617883 |
▶ | else if (cp >= 0x400 && cp <= 0x40F) cp += 0x50; /* Cyrillic Ѐ..Џ */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (cp >= 0x400 && cp <= 0x40F) cp += 0x50; /* Cyrillic Ѐ..Џ */ |
0 |
1617883 |
1617883 |
| else if (cp >= 0x400 && cp <= 0x40F) cp += 0x50; /* Cyrillic Ѐ..Џ */ |
0 |
0 |
0 |
|
| 87 |
1617883 |
▶ | else if (cp >= 0x410 && cp <= 0x42F) cp += 0x20; /* Cyrillic А..Я */ |
Branch: 2 branches
| Branch | True | False | Total |
| else if (cp >= 0x410 && cp <= 0x42F) cp += 0x20; /* Cyrillic А..Я */ |
0 |
1617883 |
1617883 |
| else if (cp >= 0x410 && cp <= 0x42F) cp += 0x20; /* Cyrillic А..Я */ |
0 |
0 |
0 |
|
| 88 |
|
| /* Else: leave codepoint as-is (no fold). */ |
| 89 |
|
| |
| 90 |
1618000 |
| return cf_encode(cp, out); |
| 91 |
|
| } |
| 92 |
|
| |
| 93 |
|
| /* Normalise label per CommonMark spec: trim outer whitespace, collapse |
| 94 |
|
| * inner whitespace runs to a single space, and case-fold codepoints. |
| 95 |
|
| * Writes at most 4 bytes per input byte plus 4 trailing bytes; caller |
| 96 |
|
| * must provide an output buffer of size >= n*4 + 4. Returns output |
| 97 |
|
| * length. */ |
| 98 |
1616518 |
| static size_t cf_normalise(const char* s, size_t n, char* out) { |
| 99 |
1616518 |
| size_t a = 0, b = n; |
| 100 |
1616518 |
| size_t j = 0; |
| 101 |
|
| size_t i; |
| 102 |
1616518 |
| int in_ws = 0; |
| 103 |
|
| /* Trim ends */ |
| 104 |
1616521 |
▶ | while (a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\n')) a++; |
Branch: 4 branches
| Branch | True | False | Total |
| while (a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\n')) a++; |
1616515 |
6 |
1616521 |
| while (a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\n')) a++; |
0 |
1616515 |
1616515 |
| while (a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\n')) a++; |
0 |
1616515 |
1616515 |
| while (a < b && (s[a] == ' ' || s[a] == '\t' || s[a] == '\n')) a++; |
3 |
1616512 |
1616515 |
|
| 105 |
1616521 |
▶ | while (b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\n')) b--; |
Branch: 4 branches
| Branch | True | False | Total |
| while (b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\n')) b--; |
1616515 |
6 |
1616521 |
| while (b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\n')) b--; |
0 |
1616515 |
1616515 |
| while (b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\n')) b--; |
0 |
1616515 |
1616515 |
| while (b > a && (s[b-1] == ' ' || s[b-1] == '\t' || s[b-1] == '\n')) b--; |
3 |
1616512 |
1616515 |
|
| 106 |
1616518 |
| i = a; |
| 107 |
3234602 |
▶ | while (i < b) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (i < b) { |
1618084 |
1616518 |
3234602 |
|
| 108 |
1618084 |
| unsigned char c = (unsigned char)s[i]; |
| 109 |
1618084 |
▶ | if (c == ' ' || c == '\t' || c == '\n') { |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == ' ' || c == '\t' || c == '\n') { |
1618006 |
78 |
1618084 |
| if (c == ' ' || c == '\t' || c == '\n') { |
1618006 |
0 |
1618006 |
| if (c == ' ' || c == '\t' || c == '\n') { |
3 |
1618003 |
1618006 |
|
| 110 |
81 |
▶ | if (!in_ws) { out[j++] = ' '; in_ws = 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!in_ws) { out[j++] = ' '; in_ws = 1; } |
81 |
0 |
81 |
|
| 111 |
81 |
| i++; |
| 112 |
|
| } else { |
| 113 |
|
| int adv; |
| 114 |
1618003 |
| unsigned cp = cf_decode(s, b, i, &adv); |
| 115 |
1618003 |
| j += cf_fold(cp, out + j); |
| 116 |
1618003 |
| i += adv; |
| 117 |
1618003 |
| in_ws = 0; |
| 118 |
|
| } |
| 119 |
|
| } |
| 120 |
1616518 |
| return j; |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
808262 |
| static char* normalise_label(mds_arena* a, const char* s, size_t n, size_t* nlen) { |
| 124 |
|
| char* out; |
| 125 |
|
| size_t j; |
| 126 |
808262 |
| out = (char*)mds_arena_alloc(a, n * 4 + 4); |
| 127 |
808262 |
| j = cf_normalise(s, n, out); |
| 128 |
808262 |
| out[j] = '\0'; |
| 129 |
808262 |
| *nlen = j; |
| 130 |
808262 |
| return out; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
40762 |
| static char* arena_dup(mds_arena* a, const char* s, size_t n) { |
| 134 |
|
| char* d; |
| 135 |
40762 |
| d = (char*)mds_arena_alloc(a, n + 1); |
| 136 |
40762 |
▶ | if (n) memcpy(d, s, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) memcpy(d, s, n); |
40759 |
3 |
40762 |
|
| 137 |
40762 |
| d[n] = '\0'; |
| 138 |
40762 |
| return d; |
| 139 |
|
| } |
| 140 |
|
| |
| 141 |
40641 |
| void mds_linkref_init(struct mds_linkref_tab* t, mds_arena* a) { |
| 142 |
40641 |
| t->entries = NULL; |
| 143 |
40641 |
| t->len = 0; |
| 144 |
40641 |
| t->cap = 0; |
| 145 |
40641 |
| t->arena = a; |
| 146 |
40641 |
| } |
| 147 |
|
| |
| 148 |
|
| /* The entry strings are arena copies and go with the arena, but the entry |
| 149 |
|
| * array itself is grown with realloc off the general heap, so it needs an |
| 150 |
|
| * explicit release. Call this before the arena is reset or freed: the table |
| 151 |
|
| * struct is itself arena-allocated. */ |
| 152 |
164107 |
| void mds_linkref_free(struct mds_linkref_tab* t) { |
| 153 |
164107 |
▶ | if (!t) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!t) return; |
123466 |
40641 |
164107 |
|
| 154 |
40641 |
| free(t->entries); |
| 155 |
40641 |
| t->entries = NULL; |
| 156 |
40641 |
| t->len = 0; |
| 157 |
40641 |
| t->cap = 0; |
| 158 |
|
| } |
| 159 |
|
| |
| 160 |
808256 |
| const mds_linkref* mds_linkref_get(const struct mds_linkref_tab* t, |
| 161 |
|
| const char* label, size_t llen) { |
| 162 |
|
| /* Normalise into a stack buffer. Max expansion is 4x (4-byte UTF-8 |
| 163 |
|
| * input expanding to 4-byte fold). Cap at 4 KiB; pathologically long |
| 164 |
|
| * labels can't match a stored entry anyway. */ |
| 165 |
|
| char buf[4096]; |
| 166 |
|
| size_t nlen; |
| 167 |
|
| size_t i; |
| 168 |
808256 |
▶ | if (llen > sizeof buf / 4 - 4) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (llen > sizeof buf / 4 - 4) return NULL; |
0 |
808256 |
808256 |
|
| 169 |
808256 |
| nlen = cf_normalise(label, llen, buf); |
| 170 |
808304 |
▶ | for (i = 0; i < t->len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->len; i++) { |
808274 |
30 |
808304 |
|
| 171 |
808274 |
▶ | if (t->entries[i].klen == nlen && |
Branch: 2 branches
| Branch | True | False | Total |
| if (t->entries[i].klen == nlen && |
808259 |
15 |
808274 |
| if (t->entries[i].klen == nlen && |
808259 |
0 |
808259 |
|
| 172 |
808259 |
▶ | (nlen == 0 || memcmp(t->entries[i].key, buf, nlen) == 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| (nlen == 0 || memcmp(t->entries[i].key, buf, nlen) == 0)) |
808226 |
33 |
808259 |
|
| 173 |
808226 |
| return &t->entries[i]; |
| 174 |
|
| } |
| 175 |
30 |
| return NULL; |
| 176 |
|
| } |
| 177 |
|
| |
| 178 |
808262 |
| int mds_linkref_add(struct mds_linkref_tab* t, |
| 179 |
|
| const char* label, size_t llen, |
| 180 |
|
| const char* url, size_t ulen, |
| 181 |
|
| const char* title, size_t tlen) { |
| 182 |
|
| size_t nlen; |
| 183 |
|
| char* key; |
| 184 |
|
| size_t i; |
| 185 |
|
| mds_linkref* e; |
| 186 |
808262 |
| key = normalise_label(t->arena, label, llen, &nlen); |
| 187 |
|
| /* dup check */ |
| 188 |
808280 |
▶ | for (i = 0; i < t->len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < t->len; i++) { |
767624 |
40656 |
808280 |
|
| 189 |
767624 |
▶ | if (t->entries[i].klen == nlen && |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->entries[i].klen == nlen && |
767624 |
0 |
767624 |
|
| 190 |
767624 |
▶ | (nlen == 0 || memcmp(t->entries[i].key, key, nlen) == 0)) |
Branch: 2 branches
| Branch | True | False | Total |
| (nlen == 0 || memcmp(t->entries[i].key, key, nlen) == 0)) |
767624 |
0 |
767624 |
| (nlen == 0 || memcmp(t->entries[i].key, key, nlen) == 0)) |
767606 |
18 |
767624 |
|
| 191 |
767606 |
| return 0; |
| 192 |
|
| } |
| 193 |
40656 |
▶ | if (t->len == t->cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (t->len == t->cap) { |
40641 |
15 |
40656 |
|
| 194 |
40641 |
▶ | size_t nc = t->cap ? t->cap * 2 : 8; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t nc = t->cap ? t->cap * 2 : 8; |
0 |
40641 |
40641 |
|
| 195 |
40641 |
| t->entries = (mds_linkref*)realloc(t->entries, nc * sizeof(mds_linkref)); |
| 196 |
40641 |
| t->cap = nc; |
| 197 |
|
| } |
| 198 |
40656 |
| e = &t->entries[t->len++]; |
| 199 |
40656 |
| e->key = key; e->klen = nlen; |
| 200 |
40656 |
| e->url = arena_dup(t->arena, url, ulen); e->ulen = ulen; |
| 201 |
40656 |
▶ | e->title = tlen ? arena_dup(t->arena, title, tlen) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| e->title = tlen ? arena_dup(t->arena, title, tlen) : NULL; |
106 |
40550 |
40656 |
|
| 202 |
40656 |
| e->tlen = tlen; |
| 203 |
40656 |
| return 1; |
| 204 |
|
| } |