Calculates the Fletcher-16 checkbytes returned as 2 byte binary-string. Including the bytes into the buffer (at the position marked by offset) the # noqa: E501 global Fletcher-16 checksum of the buffer will be 0. Thus it is easy to verify # noqa: E501 the integrity of the buf
(binbuf, offset)
| 629 | |
| 630 | @conf.commands.register |
| 631 | def fletcher16_checkbytes(binbuf, offset): |
| 632 | # type: (bytes, int) -> bytes |
| 633 | """Calculates the Fletcher-16 checkbytes returned as 2 byte binary-string. |
| 634 | |
| 635 | Including the bytes into the buffer (at the position marked by offset) the # noqa: E501 |
| 636 | global Fletcher-16 checksum of the buffer will be 0. Thus it is easy to verify # noqa: E501 |
| 637 | the integrity of the buffer on the receiver side. |
| 638 | |
| 639 | For details on the algorithm, see RFC 2328 chapter 12.1.7 and RFC 905 Annex B. # noqa: E501 |
| 640 | """ |
| 641 | |
| 642 | # This is based on the GPLed C implementation in Zebra <http://www.zebra.org/> # noqa: E501 |
| 643 | if len(binbuf) < offset: |
| 644 | raise Exception("Packet too short for checkbytes %d" % len(binbuf)) |
| 645 | |
| 646 | binbuf = binbuf[:offset] + b"\x00\x00" + binbuf[offset + 2:] |
| 647 | (c0, c1) = _fletcher16(binbuf) |
| 648 | |
| 649 | x = ((len(binbuf) - offset - 1) * c0 - c1) % 255 |
| 650 | |
| 651 | if (x <= 0): |
| 652 | x += 255 |
| 653 | |
| 654 | y = 510 - c0 - x |
| 655 | |
| 656 | if (y > 255): |
| 657 | y -= 255 |
| 658 | return chb(x) + chb(y) |
| 659 | |
| 660 | |
| 661 | def mac2str(mac): |
no test coverage detected