| 141 | } |
| 142 | |
| 143 | bool fromwire_tlv(const u8 **cursor, size_t *max, |
| 144 | const struct tlv_record_type *types, size_t num_types, |
| 145 | void *record, struct tlv_field **fields, |
| 146 | const u64 *extra_types, |
| 147 | size_t *err_off, u64 *err_type) |
| 148 | { |
| 149 | bool first = true; |
| 150 | u64 prev_type = 0; |
| 151 | size_t initial_len = *max; |
| 152 | |
| 153 | update_err_off(err_off, initial_len, *max); |
| 154 | while (*max > 0) { |
| 155 | struct tlv_field field; |
| 156 | |
| 157 | /* BOLT #1: |
| 158 | * |
| 159 | * The `type` is encoded using the BigSize format. |
| 160 | */ |
| 161 | field.numtype = fromwire_bigsize(cursor, max); |
| 162 | |
| 163 | /* BOLT #1: |
| 164 | * - if a `type` or `length` is not minimally encoded: |
| 165 | * - MUST fail to parse the `tlv_stream`. |
| 166 | */ |
| 167 | if (!*cursor) { |
| 168 | SUPERVERBOSE("type"); |
| 169 | if (err_type) |
| 170 | *err_type = 0; |
| 171 | goto fail; |
| 172 | } |
| 173 | |
| 174 | /* BOLT #1: |
| 175 | * - if decoded `type`s are not strictly-increasing |
| 176 | * (including situations when two or more occurrences |
| 177 | * of the same `type` are met): |
| 178 | * - MUST fail to parse the `tlv_stream`. |
| 179 | */ |
| 180 | if (!first && field.numtype <= prev_type) { |
| 181 | if (field.numtype == prev_type) |
| 182 | SUPERVERBOSE("duplicate tlv type"); |
| 183 | else |
| 184 | SUPERVERBOSE("invalid ordering"); |
| 185 | if (err_type) |
| 186 | *err_type = field.numtype; |
| 187 | goto fail; |
| 188 | } |
| 189 | first = false; |
| 190 | prev_type = field.numtype; |
| 191 | |
| 192 | /* BOLT #1: |
| 193 | * - if `type` is known: |
| 194 | * - MUST decode the next `length` bytes using the known |
| 195 | * encoding for `type`. |
| 196 | */ |
| 197 | field.meta = NULL; |
| 198 | for (size_t i = 0; i < num_types; i++) { |
| 199 | if (types[i].type == field.numtype) { |
| 200 | field.meta = &types[i]; |
no test coverage detected