-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ensure bytes with null chars are returned
- Loading branch information
Showing
2 changed files
with
28 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,43 @@ | ||
import cython | ||
|
||
from libc.stdlib cimport malloc | ||
from libc.stdlib cimport free, malloc | ||
from libc.string cimport strlen | ||
|
||
|
||
cdef extern from "crypt_wrapper.h": | ||
void _encrypt_into(const char * unencrypted, char * encrypted) | ||
void _decrypt_into(const char * encrypted, char * unencrypted) | ||
|
||
cdef char* _decrypt(const char *encrypted): | ||
cdef void _decrypt(const char *encrypted, char **unencrypted, Py_ssize_t *length): | ||
cdef Py_ssize_t n = strlen(encrypted) | ||
cdef char* unencrypted = <char *> malloc((n + 1) * sizeof(char)) | ||
if not unencrypted: | ||
return NULL # malloc failed | ||
_decrypt_into(encrypted, unencrypted) | ||
return unencrypted[:n] | ||
unencrypted[0] = <char *> malloc((n + 1) * sizeof(char)) | ||
if not unencrypted[0]: | ||
return # malloc failed | ||
_decrypt_into(encrypted, unencrypted[0] ) | ||
length[0] = n | ||
|
||
cdef char* _encrypt(const char *unencrypted): | ||
cdef void _encrypt(const char *unencrypted, char** encrypted, Py_ssize_t *length): | ||
cdef Py_ssize_t n = strlen(unencrypted) | ||
cdef char* encrypted = <char *> malloc((n + 1) * sizeof(char)) | ||
if not encrypted: | ||
return NULL # malloc failed | ||
_encrypt_into(unencrypted, encrypted) | ||
return encrypted[:n] | ||
encrypted[0] = <char *> malloc((n + 1) * sizeof(char)) | ||
if not encrypted[0]: | ||
return # malloc failed | ||
_encrypt_into(unencrypted, encrypted[0]) | ||
length[0] = n | ||
|
||
def encrypt(string: str) -> bytes: | ||
return _encrypt(string.encode('utf-8')) | ||
|
||
cdef char* encrypted = NULL | ||
cdef Py_ssize_t length = 0 | ||
_encrypt(string.encode('utf-8'), &encrypted, &length) | ||
try: | ||
return encrypted[:length] | ||
finally: | ||
free(encrypted) | ||
|
||
def decrypt(string: bytes) -> str: | ||
return _decrypt(string).decode('utf-8') | ||
cdef char* unencrypted = NULL | ||
cdef Py_ssize_t length = 0 | ||
_decrypt(string, &unencrypted, &length) | ||
try: | ||
return unencrypted[:length].decode('utf-8') | ||
finally: | ||
free(unencrypted) |
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