| 1519 | } |
| 1520 | |
| 1521 | static int toml_parse_header(const char *data, const toml_line_t *line, const char *target_name, |
| 1522 | toml_header_t *header) { |
| 1523 | memset(header, 0, sizeof(*header)); |
| 1524 | size_t start = line->start; |
| 1525 | size_t end = line->content_end; |
| 1526 | header->edit_start = line->start; |
| 1527 | if (line->start == 0 && end - start >= 3 && (unsigned char)data[start] == 0xef && |
| 1528 | (unsigned char)data[start + 1] == 0xbb && (unsigned char)data[start + 2] == 0xbf) { |
| 1529 | start += 3; |
| 1530 | header->edit_start = start; |
| 1531 | } |
| 1532 | while (start < end && (data[start] == ' ' || data[start] == '\t')) { |
| 1533 | ++start; |
| 1534 | } |
| 1535 | if (start >= end || data[start] != '[') { |
| 1536 | return TOML_EDIT_OK; |
| 1537 | } |
| 1538 | |
| 1539 | int array = start + 1 < end && data[start + 1] == '['; |
| 1540 | size_t inner_start = start + (array ? 2u : 1u); |
| 1541 | size_t pos = inner_start; |
| 1542 | char quote = '\0'; |
| 1543 | int escaped = 0; |
| 1544 | size_t close_start = SIZE_MAX; |
| 1545 | size_t close_end = SIZE_MAX; |
| 1546 | while (pos < end) { |
| 1547 | char ch = data[pos]; |
| 1548 | if (quote) { |
| 1549 | if (quote == '"' && !escaped && ch == '\\') { |
| 1550 | escaped = 1; |
| 1551 | } else { |
| 1552 | if (!escaped && ch == quote) { |
| 1553 | quote = '\0'; |
| 1554 | } |
| 1555 | escaped = 0; |
| 1556 | } |
| 1557 | ++pos; |
| 1558 | continue; |
| 1559 | } |
| 1560 | if (ch == '"' || ch == '\'') { |
| 1561 | quote = ch; |
| 1562 | ++pos; |
| 1563 | continue; |
| 1564 | } |
| 1565 | if (ch == ']' && (!array || (pos + 1 < end && data[pos + 1] == ']'))) { |
| 1566 | close_start = pos; |
| 1567 | close_end = pos + (array ? 2u : 1u); |
| 1568 | break; |
| 1569 | } |
| 1570 | ++pos; |
| 1571 | } |
| 1572 | if (quote || close_start == SIZE_MAX) { |
| 1573 | return TOML_EDIT_ERR; |
| 1574 | } |
| 1575 | size_t inner_end = close_start; |
| 1576 | toml_trim(data, &inner_start, &inner_end); |
| 1577 | if (inner_start == inner_end) { |
| 1578 | return TOML_EDIT_ERR; |
no test coverage detected