|
| 1 | +/* |
| 2 | + * atma.h -- ATMA parser (see: https://web.archive.org/web/20190112072924/http://www.broadband-forum.org/ftp/pub/approved-specs/af-dans-0152.000.pdf ) |
| 3 | + * |
| 4 | + * Copyright (c) 2025, NLnet Labs. All rights reserved. |
| 5 | + * |
| 6 | + * SPDX-License-Identifier: BSD-3-Clause |
| 7 | + * |
| 8 | + */ |
| 9 | +#ifndef ATMA_H |
| 10 | +#define ATMA_H |
| 11 | + |
| 12 | +static const uint8_t bad_atma_chars[256] = { |
| 13 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x00 - 0x07 |
| 14 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x08 - 0x0f |
| 15 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x10 - 0x17 |
| 16 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x18 - 0x1f |
| 17 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x20 - 0x27 |
| 18 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x01, // 0x28 - 0x2f |
| 19 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x30 - 0x37 |
| 20 | + 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x38 - 0x3f |
| 21 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x40 - 0x47 |
| 22 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x48 - 0x4f |
| 23 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x50 - 0x57 |
| 24 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x58 - 0x5f |
| 25 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x60 - 0x67 |
| 26 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x68 - 0x6f |
| 27 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x70 - 0x77 |
| 28 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x78 - 0x7f |
| 29 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x80 - 0x87 |
| 30 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x88 - 0x8f |
| 31 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x90 - 0x97 |
| 32 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x98 - 0x9f |
| 33 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xa0 - 0xa7 |
| 34 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xa8 - 0xaf |
| 35 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xb0 - 0xb7 |
| 36 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xb8 - 0xbf |
| 37 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xc0 - 0xc7 |
| 38 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xc8 - 0xcf |
| 39 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xd0 - 0xd7 |
| 40 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xd8 - 0xdf |
| 41 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xe0 - 0xe7 |
| 42 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xe0 - 0xe7 |
| 43 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xf8 - 0xff |
| 44 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0xf8 - 0xff |
| 45 | +}; |
| 46 | + |
| 47 | +static const uint8_t atma_increment[256] = { |
| 48 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x01 - 0x07 |
| 49 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x08 - 0x0f |
| 50 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x10 - 0x17 |
| 51 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x18 - 0x1f |
| 52 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x20 - 0x27 |
| 53 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x28 - 0x2f |
| 54 | + 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, // 0x30 - 0x37 |
| 55 | + 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x38 - 0x3f |
| 56 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x40 - 0x47 |
| 57 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x48 - 0x4f |
| 58 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x50 - 0x57 |
| 59 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x58 - 0x5f |
| 60 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x60 - 0x67 |
| 61 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x68 - 0x6f |
| 62 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x70 - 0x77 |
| 63 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x78 - 0x7f |
| 64 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80 - 0x87 |
| 65 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x88 - 0x8f |
| 66 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x90 - 0x97 |
| 67 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x98 - 0x9f |
| 68 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xa0 - 0xa7 |
| 69 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xa8 - 0xaf |
| 70 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xb0 - 0xb7 |
| 71 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xb8 - 0xbf |
| 72 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xc0 - 0xc7 |
| 73 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xc8 - 0xcf |
| 74 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xd0 - 0xd7 |
| 75 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xd8 - 0xdf |
| 76 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xe0 - 0xe7 |
| 77 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xe0 - 0xe7 |
| 78 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xf8 - 0x00 |
| 79 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xf8 - 0x00 |
| 80 | +}; |
| 81 | + |
| 82 | + |
| 83 | +nonnull_all |
| 84 | +static really_inline int32_t parse_atma_e164( |
| 85 | + parser_t *parser, |
| 86 | + const type_info_t *type, |
| 87 | + const rdata_info_t *field, |
| 88 | + rdata_t *rdata, |
| 89 | + const token_t *token) |
| 90 | +{ |
| 91 | + uint32_t bad_chars = 0; |
| 92 | + for (size_t count=1; count < token->length; count++) { |
| 93 | + const uint8_t octet = (uint8_t)token->data[count]; |
| 94 | + *rdata->octets = octet; |
| 95 | + rdata->octets += atma_increment[octet]; |
| 96 | + bad_chars |= bad_atma_chars[octet]; |
| 97 | + } |
| 98 | + if (bad_chars) |
| 99 | + SEMANTIC_ERROR(parser, "Invalid %s in %s", NAME(field), NAME(type)); |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
| 103 | + |
| 104 | +nonnull((1,2,4,5)) |
| 105 | +static really_inline int atma_stream_decode( |
| 106 | + struct base16_state *state, |
| 107 | + const char *src, |
| 108 | + size_t srclen, |
| 109 | + uint8_t *out, |
| 110 | + size_t *outlen) |
| 111 | +{ |
| 112 | + int ret = 0; |
| 113 | + const uint8_t *s = (const uint8_t *) src; |
| 114 | + uint8_t *o = (uint8_t *) out; |
| 115 | + uint32_t q; |
| 116 | + |
| 117 | + // Use local temporaries to avoid cache thrashing: |
| 118 | + size_t olen = 0; |
| 119 | + size_t slen = srclen; |
| 120 | + struct base16_state st; |
| 121 | + st.eof = state->eof; |
| 122 | + st.bytes = state->bytes; |
| 123 | + st.carry = state->carry; |
| 124 | + |
| 125 | + if (st.eof) { |
| 126 | + *outlen = 0; |
| 127 | + return ret; |
| 128 | + } |
| 129 | + |
| 130 | + // Duff's device again: |
| 131 | + switch (st.bytes) |
| 132 | + { |
| 133 | +#if defined(__SUNPRO_C) |
| 134 | +#pragma error_messages(off, E_STATEMENT_NOT_REACHED) |
| 135 | +#endif |
| 136 | + for (;;) |
| 137 | +#if defined(__SUNPRO_C) |
| 138 | +#pragma error_messages(default, E_STATEMENT_NOT_REACHED) |
| 139 | +#endif |
| 140 | + { |
| 141 | + case 0: |
| 142 | + base16_dec_loop_generic_32(&s, &slen, &o, &olen); |
| 143 | + if (slen-- == 0) { |
| 144 | + ret = 1; |
| 145 | + break; |
| 146 | + } |
| 147 | + if ((q = base16_table_dec_32bit_d0[*s++]) == 256) { |
| 148 | + st.eof = BASE16_EOF; |
| 149 | + break; |
| 150 | + } else if (q == 257) { |
| 151 | + continue; |
| 152 | + } |
| 153 | + st.carry = (uint8_t)q; |
| 154 | + st.bytes = 1; |
| 155 | + |
| 156 | + // fallthrough |
| 157 | + |
| 158 | + case 1: |
| 159 | + if (slen-- == 0) { |
| 160 | + ret = 1; |
| 161 | + break; |
| 162 | + } |
| 163 | + if ((q = base16_table_dec_32bit_d1[*s++]) == 256) { |
| 164 | + st.eof = BASE16_EOF; |
| 165 | + break; |
| 166 | + } else if (q == 257) { |
| 167 | + continue; |
| 168 | + } |
| 169 | + *o++ = st.carry | (uint8_t)q; |
| 170 | + st.carry = 0; |
| 171 | + st.bytes = 0; |
| 172 | + olen++; |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + state->eof = st.eof; |
| 177 | + state->bytes = st.bytes; |
| 178 | + state->carry = st.carry; |
| 179 | + *outlen = olen; |
| 180 | + return ret; |
| 181 | +} |
| 182 | + |
| 183 | +nonnull((1,3,4)) |
| 184 | +static really_inline int atma_decode( |
| 185 | + const char *src, size_t srclen, uint8_t *out, size_t *outlen) |
| 186 | +{ |
| 187 | + struct base16_state state = { .eof = 0, .bytes = 0, .carry = 0 }; |
| 188 | + return atma_stream_decode(&state, src, srclen, out, outlen) & !state.bytes; |
| 189 | +} |
| 190 | + |
| 191 | +nonnull_all |
| 192 | +static really_inline int32_t parse_atma( |
| 193 | + parser_t *parser, |
| 194 | + const type_info_t *type, |
| 195 | + const rdata_info_t *field, |
| 196 | + rdata_t *rdata, |
| 197 | + const token_t *token) |
| 198 | +{ |
| 199 | + if (token->length && (char)*token->data == '+') { |
| 200 | + *rdata->octets++ = 1; |
| 201 | + if ((uintptr_t)rdata->limit - (uintptr_t)rdata->octets < token->length) |
| 202 | + SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), NAME(type)); |
| 203 | + return parse_atma_e164(parser, type, field, rdata, token); |
| 204 | + } |
| 205 | + size_t length = token->length / 2; |
| 206 | + if ((uintptr_t)rdata->limit - (uintptr_t)rdata->octets < length) |
| 207 | + SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), NAME(type)); |
| 208 | + *rdata->octets++ = 0; |
| 209 | + if (!atma_decode(token->data, token->length, rdata->octets, &length)) |
| 210 | + SYNTAX_ERROR(parser, "Invalid %s in %s", NAME(field), NAME(type)); |
| 211 | + rdata->octets += length; |
| 212 | + return 0; |
| 213 | +} |
| 214 | + |
| 215 | +#endif // ATMA_H |
0 commit comments