Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BF: force "little" endianness while xor_byte #102

Merged
merged 2 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/fscacher/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import os
import os.path as op
import shutil
import sys
import time
import joblib
from platformdirs import PlatformDirs
Expand Down Expand Up @@ -247,9 +246,11 @@ def to_tuple(self):

def xor_bytes(b1: bytes, b2: bytes) -> bytes:
length = max(len(b1), len(b2))
i1 = int.from_bytes(b1, sys.byteorder)
i2 = int.from_bytes(b2, sys.byteorder)
return (i1 ^ i2).to_bytes(length, sys.byteorder)
# force 'little' byte order to match our assumptions on how to
# treat bytes of different length.
i1 = int.from_bytes(b1, "little")
i2 = int.from_bytes(b2, "little")
return (i1 ^ i2).to_bytes(length, "little")


def elapsed_since(t: float) -> float:
Expand Down
3 changes: 3 additions & 0 deletions src/fscacher/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from itertools import zip_longest
import pytest
from ..cache import xor_bytes

Expand All @@ -13,4 +14,6 @@
],
)
def test_xor_bytes(b1: bytes, b2: bytes, r: bytes) -> None:
# test the target value assumption
assert r == bytes(x ^ y for x, y in zip_longest(b1, b2, fillvalue=0))
assert xor_bytes(b1, b2) == r
Loading