-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
111 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
SHELL := bash | ||
|
||
CC = gcc | ||
CFLAGS = -std=c99 \ | ||
-pedantic \ | ||
-Wall \ | ||
-Wextra \ | ||
-Werror \ | ||
-fPIC \ | ||
-fstack-protector-all \ | ||
-D_FORTIFY_SOURCE=2 \ | ||
-shared \ | ||
-nostdlib \ | ||
-O3 | ||
LDLIBS = -lm # Link the math library | ||
|
||
# Use this .so.1 extension because otherwise the dependency injector will | ||
# erroneously try to import entropy.so, which will fail. | ||
entropy.so.1: entropy.c | ||
$(CC) \ | ||
$(CFLAGS) \ | ||
$(filter %.c, $^) \ | ||
$(LDLIBS) \ | ||
-o $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import ctypes | ||
import os | ||
from typing import Callable, Optional | ||
import sysconfig | ||
|
||
C_LOG_TYPE = ctypes.CFUNCTYPE(None, ctypes.c_uint8) | ||
|
||
ext_suffix = sysconfig.get_config_var("EXT_SUFFIX") | ||
if not isinstance(ext_suffix, str): | ||
raise RuntimeError("Could not find compiled C library, no EXT_SUFFIX sysconfig var") | ||
|
||
_lib_entropy = ctypes.cdll.LoadLibrary( | ||
os.path.abspath(os.path.join(os.path.dirname(__file__), "entropy_c" + ext_suffix + ".1")) | ||
) | ||
C_ENTROPY_FUNC = _lib_entropy.entropy | ||
|
||
C_ENTROPY_FUNC.argtypes = ( | ||
ctypes.c_char_p, | ||
ctypes.c_size_t, | ||
ctypes.c_char_p, | ||
ctypes.c_size_t, | ||
C_LOG_TYPE, | ||
) | ||
C_ENTROPY_FUNC.restype = ctypes.c_int | ||
|
||
|
||
def entropy_c( | ||
data: bytes, window_size: int, log_percent: Optional[Callable[[int], None]] = None | ||
) -> bytes: | ||
if log_percent is None: | ||
log_percent = lambda x: None | ||
|
||
if len(data) <= window_size: | ||
return b"" | ||
entropy = ctypes.create_string_buffer(len(data) - window_size) | ||
errval = C_ENTROPY_FUNC(data, len(data), entropy, window_size, C_LOG_TYPE(log_percent)) | ||
if errval != 0: | ||
raise ValueError("Bad input to entropy function.") | ||
return bytes(entropy.raw) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters