Skip to content

Commit

Permalink
util: introduce libscrc as optional dependency
Browse files Browse the repository at this point in the history
As discussed in #85 libscrc can
increase the speed of CRC calculation. However, as it is C-based module,
the usage of it is made optional to cope with the environment without
Python development package.
  • Loading branch information
peremen committed Sep 9, 2023
1 parent 6c43856 commit 9e4c126
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ page](https://github.com/fgsect/scat/wiki/Baseband-Dumps) for details.
Install SCAT through pip using:

```
# If you want fast CRC calculation (for Qualcomm and HiSilicon)
$ pip install "scat[fastcrc] @ git+https://github.com/fgsect/scat"
# If you don't want or can't build libscrc
$ pip install https://github.com/fgsect/scat
```

Please note that the name SCAT is taken in the PyPI, I will find further solution.

For development purposes, please use `pip install -e .` on your checkout directory.
For development purposes, please use `pip install -e .[fastcrc]` or `pip install
-e .` on your checkout directory.
The older `scat.py` is moved to `src/scat/main.py`.

## Usage
Expand Down Expand Up @@ -76,7 +81,7 @@ please file an issue with the debug output (`--debug`) attached:

SCAT version up to 1.1.0 required specifying the Samsung baseband type manually
using `-m`. As SCAT now autodetects the Samsung baseband type, This option will
be deprecated in future releases.
be deprecated in SCAT 1.2.0, and may be removed in SCAT >= 2.0.0.

### USB
Accessing the baseband diagnostics via USB:
Expand Down
6 changes: 5 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "scat"
version = "1.0.0"
version = "1.1.0"
authors = [
{ name="Shinjo Park", email="[email protected]" },
]
Expand All @@ -16,6 +16,10 @@ dependencies = [
"pyusb>=1.0.2",
"pyserial>=3.3"
]
[project.optional-dependencies]
fastcrc = [
"libscrc>=1.8.0",
]

[project.urls]
"Homepage" = "https://github.com/fgsect/scat"
Expand Down
16 changes: 12 additions & 4 deletions src/scat/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import sys
import string
from enum import IntEnum, unique
try:
import libscrc
has_libscrc = True
except ModuleNotFoundError:
has_libscrc = False

XXD_SET = string.ascii_letters + string.digits + string.punctuation

Expand Down Expand Up @@ -45,10 +50,13 @@
]

def dm_crc16(arr):
ret = 0xffff
for b in arr:
ret = (ret >> 8) ^ crc_table[(ret ^ b) & 0xff]
return ret ^ 0xffff
if has_libscrc:
return libscrc.x25(arr)
else:
ret = 0xffff
for b in arr:
ret = (ret >> 8) ^ crc_table[(ret ^ b) & 0xff]
return ret ^ 0xffff

def wrap(arr):
t = arr.replace(b'\x7d', b'\x7d\x5d')
Expand Down

0 comments on commit 9e4c126

Please sign in to comment.