interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / collab / co0form.om
| 1 | global gcfv uchar 65536 |
| 2 | global gcfvl ulong 1 |
| 3 | note Shared issue and preview form decoding and text validation. |
| 4 | note cobfld t: decode field t into gcfv; its length, -1 when absent, -2 when over 65536 bytes |
| 5 | fn cobfld t view uchar * to long |
| 6 | n = get fbodyl 0 |
| 7 | i = ulong 0 |
| 8 | while lt i n bound 70000 |
| 9 | if cobfmat t i n |
| 10 | ret cobfdec add add i len t 1 n |
| 11 | end |
| 12 | i = cobfnxt i n |
| 13 | end |
| 14 | ret -1 |
| 15 | end |
| 16 | fn cobfmat t view uchar * i ulong n ulong to bool |
| 17 | if gt add add i len t 1 n |
| 18 | ret false |
| 19 | end |
| 20 | for k ulong len t bound 64 |
| 21 | if ne get fbody add i k get t k |
| 22 | ret false |
| 23 | end |
| 24 | end |
| 25 | ret eq get fbody add i len t 61 |
| 26 | end |
| 27 | fn cobfnxt i ulong n ulong to ulong |
| 28 | j = i |
| 29 | while lt j n bound 70000 |
| 30 | if eq get fbody j 38 |
| 31 | ret add j 1 |
| 32 | end |
| 33 | j = add j 1 |
| 34 | end |
| 35 | ret n |
| 36 | end |
| 37 | fn cobfdec i ulong n ulong to long |
| 38 | j = i |
| 39 | o = ulong 0 |
| 40 | while lt j n bound 70000 |
| 41 | c = get fbody j |
| 42 | if eq c 38 |
| 43 | break |
| 44 | end |
| 45 | if ge o 65536 |
| 46 | ret -2 |
| 47 | end |
| 48 | j = add j 1 |
| 49 | if eq c 43 |
| 50 | c = uchar 32 |
| 51 | end |
| 52 | if eq c 37 |
| 53 | c = cobpct j n |
| 54 | if lt add j 1 n |
| 55 | j = add j 2 |
| 56 | end |
| 57 | end |
| 58 | put gcfv o c |
| 59 | o = add o 1 |
| 60 | end |
| 61 | put gcfvl 0 o |
| 62 | ret long o |
| 63 | end |
| 64 | fn cobpct j ulong n ulong to uchar |
| 65 | if ge add j 1 n |
| 66 | ret uchar 37 |
| 67 | end |
| 68 | a = fhexv get fbody j |
| 69 | b = fhexv get fbody add j 1 |
| 70 | if or lt a 0 lt b 0 |
| 71 | ret uchar 37 |
| 72 | end |
| 73 | ret uchar add mul a 16 b |
| 74 | end |
| 75 | note ---- text checks on gcfv[0,n) |
| 76 | note cotxtok n ml: CRLF and lone CR become LF; then the text must be UTF-8 without NUL or control |
| 77 | note bytes (ml: tab and LF allowed; else none); trailing blanks and leading newlines are trimmed |
| 78 | note (single line: leading blanks too). The cleaned length, or -1 when the text is refused. |
| 79 | fn cotxtok n ulong ml bool to long |
| 80 | m = cocrlf n |
| 81 | if eq cowutf8 m ml false |
| 82 | ret -1 |
| 83 | end |
| 84 | e = m |
| 85 | while gt e 0 bound 70000 |
| 86 | c = get gcfv sub e 1 |
| 87 | if and ne c 32 and ne c 9 ne c 10 |
| 88 | break |
| 89 | end |
| 90 | e = sub e 1 |
| 91 | end |
| 92 | s = ulong 0 |
| 93 | while lt s e bound 70000 |
| 94 | c = get gcfv s |
| 95 | if and ne c 10 or ml and ne c 32 ne c 9 |
| 96 | break |
| 97 | end |
| 98 | s = add s 1 |
| 99 | end |
| 100 | if gt s 0 |
| 101 | for k ulong sub e s bound 70000 |
| 102 | put gcfv k get gcfv add s k |
| 103 | end |
| 104 | end |
| 105 | ret long sub e s |
| 106 | end |
| 107 | fn cocrlf n ulong to ulong |
| 108 | o = ulong 0 |
| 109 | for i ulong n bound 70000 |
| 110 | c = get gcfv i |
| 111 | if eq c 13 |
| 112 | c = uchar 10 |
| 113 | if lt add i 1 n |
| 114 | if eq get gcfv add i 1 10 |
| 115 | c = uchar 0 |
| 116 | end |
| 117 | end |
| 118 | end |
| 119 | if ne c 0 |
| 120 | put gcfv o c |
| 121 | o = add o 1 |
| 122 | else |
| 123 | if ne get gcfv i 13 |
| 124 | put gcfv o c |
| 125 | o = add o 1 |
| 126 | end |
| 127 | end |
| 128 | end |
| 129 | ret o |
| 130 | end |
| 131 | note cowutf8 n ml: well-formed UTF-8 (no overlong forms, surrogates or code points over U+10FFFF), no |
| 132 | note NUL, C0 control or DEL byte except tab and LF when ml |
| 133 | fn cowutf8 n ulong ml bool to bool |
| 134 | i = ulong 0 |
| 135 | while lt i n bound 70000 |
| 136 | c = get gcfv i |
| 137 | if lt c 128 |
| 138 | if eq coctlok c ml false |
| 139 | ret false |
| 140 | end |
| 141 | i = add i 1 |
| 142 | else |
| 143 | k = coulen i n |
| 144 | if eq k 0 |
| 145 | ret false |
| 146 | end |
| 147 | i = add i k |
| 148 | end |
| 149 | end |
| 150 | ret true |
| 151 | end |
| 152 | fn coctlok c uchar ml bool to bool |
| 153 | if eq c 127 |
| 154 | ret false |
| 155 | end |
| 156 | if ge c 32 |
| 157 | ret true |
| 158 | end |
| 159 | if and ml or eq c 9 eq c 10 |
| 160 | ret true |
| 161 | end |
| 162 | ret false |
| 163 | end |
| 164 | note coulen i n: byte length of the multi-byte sequence at gcfv[i], 0 when it is malformed |
| 165 | fn coulen i ulong n ulong to ulong |
| 166 | c = get gcfv i |
| 167 | k = ulong 0 |
| 168 | lo = uchar 128 |
| 169 | hi = uchar 191 |
| 170 | if and ge c 194 le c 223 |
| 171 | k = ulong 2 |
| 172 | end |
| 173 | if and ge c 224 le c 239 |
| 174 | k = ulong 3 |
| 175 | if eq c 224 |
| 176 | lo = uchar 160 |
| 177 | end |
| 178 | if eq c 237 |
| 179 | hi = uchar 159 |
| 180 | end |
| 181 | end |
| 182 | if and ge c 240 le c 244 |
| 183 | k = ulong 4 |
| 184 | if eq c 240 |
| 185 | lo = uchar 144 |
| 186 | end |
| 187 | if eq c 244 |
| 188 | hi = uchar 143 |
| 189 | end |
| 190 | end |
| 191 | if or eq k 0 gt add i k n |
| 192 | ret ulong 0 |
| 193 | end |
| 194 | d = get gcfv add i 1 |
| 195 | if or lt d lo gt d hi |
| 196 | ret ulong 0 |
| 197 | end |
| 198 | for j ulong sub k 2 bound 2 |
| 199 | d = get gcfv add add i 2 j |
| 200 | if or lt d 128 gt d 191 |
| 201 | ret ulong 0 |
| 202 | end |
| 203 | end |
| 204 | ret k |
| 205 | end |