| 1423 | } |
| 1424 | |
| 1425 | static int toml_parse_header(const char *data, const toml_line_t *line, const char *target_name, |
| 1426 | toml_header_t *header) { |
| 1427 | memset(header, 0, sizeof(*header)); |
| 1428 | size_t start = line->start; |
| 1429 | size_t end = line->content_end; |
| 1430 | header->edit_start = line->start; |
| 1431 | if (line->start == 0 && end - start >= 3 && (unsigned char)data[start] == 0xef && |
| 1432 | (unsigned char)data[start + 1] == 0xbb && (unsigned char)data[start + 2] == 0xbf) { |
| 1433 | start += 3; |
| 1434 | header->edit_start = start; |
| 1435 | } |
| 1436 | while (start < end && (data[start] == ' ' || data[start] == '\t')) { |
| 1437 | ++start; |
| 1438 | } |
| 1439 | if (start >= end || data[start] != '[') { |
| 1440 | return TOML_EDIT_OK; |
| 1441 | } |
| 1442 | |
| 1443 | int array = start + 1 < end && data[start + 1] == '['; |
| 1444 | size_t inner_start = start + (array ? 2u : 1u); |
| 1445 | size_t pos = inner_start; |
| 1446 | char quote = '\0'; |
| 1447 | int escaped = 0; |
| 1448 | size_t close_start = SIZE_MAX; |
| 1449 | size_t close_end = SIZE_MAX; |
| 1450 | while (pos < end) { |
| 1451 | char ch = data[pos]; |
| 1452 | if (quote) { |
| 1453 | if (quote == '"' && !escaped && ch == '\\') { |
| 1454 | escaped = 1; |
| 1455 | } else { |
| 1456 | if (!escaped && ch == quote) { |
| 1457 | quote = '\0'; |
| 1458 | } |
| 1459 | escaped = 0; |
| 1460 | } |
| 1461 | ++pos; |
| 1462 | continue; |
| 1463 | } |
| 1464 | if (ch == '"' || ch == '\'') { |
| 1465 | quote = ch; |
| 1466 | ++pos; |
| 1467 | continue; |
| 1468 | } |
| 1469 | if (ch == ']' && (!array || (pos + 1 < end && data[pos + 1] == ']'))) { |
| 1470 | close_start = pos; |
| 1471 | close_end = pos + (array ? 2u : 1u); |
| 1472 | break; |
| 1473 | } |
| 1474 | ++pos; |
| 1475 | } |
| 1476 | if (quote || close_start == SIZE_MAX) { |
| 1477 | return TOML_EDIT_ERR; |
| 1478 | } |
| 1479 | size_t inner_end = close_start; |
| 1480 | toml_trim(data, &inner_start, &inner_end); |
| 1481 | if (inner_start == inner_end) { |
| 1482 | return TOML_EDIT_ERR; |
no test coverage detected