Show differences between 2 binary strings, Packets... Available algorithms: - wagnerfischer: Use the Wagner and Fischer algorithm to compute the Levenstein distance between the strings then backtrack. - difflib: Use the difflib.SequenceMatcher implementation. This
(
a: Union['Packet', AnyStr],
b: Union['Packet', AnyStr],
algo: Optional[str] = None,
autojunk: bool = False,
)
| 399 | |
| 400 | @conf.commands.register |
| 401 | def hexdiff( |
| 402 | a: Union['Packet', AnyStr], |
| 403 | b: Union['Packet', AnyStr], |
| 404 | algo: Optional[str] = None, |
| 405 | autojunk: bool = False, |
| 406 | ) -> None: |
| 407 | """ |
| 408 | Show differences between 2 binary strings, Packets... |
| 409 | |
| 410 | Available algorithms: |
| 411 | - wagnerfischer: Use the Wagner and Fischer algorithm to compute the |
| 412 | Levenstein distance between the strings then backtrack. |
| 413 | - difflib: Use the difflib.SequenceMatcher implementation. This based on a |
| 414 | modified version of the Ratcliff and Obershelp algorithm. |
| 415 | This is much faster, but far less accurate. |
| 416 | https://docs.python.org/3.8/library/difflib.html#difflib.SequenceMatcher |
| 417 | |
| 418 | :param a: |
| 419 | :param b: The binary strings, packets... to compare |
| 420 | :param algo: Force the algo to be 'wagnerfischer' or 'difflib'. |
| 421 | By default, this is chosen depending on the complexity, optimistically |
| 422 | preferring wagnerfischer unless really necessary. |
| 423 | :param autojunk: (difflib only) See difflib documentation. |
| 424 | """ |
| 425 | xb = bytes_encode(a) |
| 426 | yb = bytes_encode(b) |
| 427 | |
| 428 | if algo is None: |
| 429 | # Choose the best algorithm |
| 430 | complexity = len(xb) * len(yb) |
| 431 | if complexity < 1e7: |
| 432 | # Comparing two (non-jumbos) Ethernet packets is ~2e6 which is manageable. |
| 433 | # Anything much larger than this shouldn't be attempted by default. |
| 434 | algo = "wagnerfischer" |
| 435 | if complexity > 1e6: |
| 436 | log_interactive.info( |
| 437 | "Complexity is a bit high. hexdiff will take a few seconds." |
| 438 | ) |
| 439 | else: |
| 440 | algo = "difflib" |
| 441 | |
| 442 | backtrackx = [] |
| 443 | backtracky = [] |
| 444 | |
| 445 | if algo == "wagnerfischer": |
| 446 | xb = xb[::-1] |
| 447 | yb = yb[::-1] |
| 448 | |
| 449 | # costs for the 3 operations |
| 450 | INSERT = 1 |
| 451 | DELETE = 1 |
| 452 | SUBST = 1 |
| 453 | |
| 454 | # Typically, d[i,j] will hold the distance between |
| 455 | # the first i characters of xb and the first j characters of yb. |
| 456 | # We change the Wagner Fischer to also store pointers to all |
| 457 | # the intermediate steps taken while calculating the Levenstein distance. |
| 458 | d = {(-1, -1): (0, (-1, -1))} |
no test coverage detected