| 517 | } |
| 518 | |
| 519 | static bool ReadName(const uint8_t* data, uint32_t size, uint32_t* offset, char* out, uint32_t out_size) |
| 520 | { |
| 521 | // RFC 1035 section 4.1.4 name parser with support for compression |
| 522 | // pointers. The jump limit keeps malformed packets from looping |
| 523 | // forever when they contain cyclic pointers. |
| 524 | uint32_t pos = *offset; |
| 525 | uint32_t out_len = 0; |
| 526 | bool jumped = false; |
| 527 | uint32_t jump_count = 0; |
| 528 | |
| 529 | while (true) |
| 530 | { |
| 531 | if (pos >= size) |
| 532 | return false; |
| 533 | |
| 534 | uint8_t len = data[pos]; |
| 535 | |
| 536 | if ((len & 0xC0) == 0xC0) |
| 537 | { |
| 538 | if (pos + 1 >= size) |
| 539 | return false; |
| 540 | |
| 541 | uint16_t pointer = (uint16_t) (((len & 0x3F) << 8) | data[pos + 1]); |
| 542 | if (pointer >= size) |
| 543 | return false; |
| 544 | |
| 545 | if (!jumped) |
| 546 | { |
| 547 | *offset = pos + 2; |
| 548 | jumped = true; |
| 549 | } |
| 550 | |
| 551 | pos = pointer; |
| 552 | ++jump_count; |
| 553 | if (jump_count > 16) |
| 554 | return false; |
| 555 | continue; |
| 556 | } |
| 557 | |
| 558 | ++pos; |
| 559 | |
| 560 | if (len == 0) |
| 561 | { |
| 562 | if (!jumped) |
| 563 | *offset = pos; |
| 564 | |
| 565 | if (out_len == 0) |
| 566 | { |
| 567 | if (out_size < 1) |
| 568 | return false; |
| 569 | out[0] = 0; |
| 570 | } |
| 571 | else |
| 572 | { |
| 573 | out[out_len - 1] = 0; |
| 574 | } |
| 575 | return true; |
| 576 | } |
no outgoing calls
no test coverage detected