@@ -5,38 +5,35 @@ from libc.string cimport strlen
55
66
77cdef extern from " crypt_wrapper.h" :
8- void _encrypt_into(const char * unencrypted, char * encrypted)
9- void _decrypt_into(const char * encrypted, char * unencrypted)
8+ void _encrypt_into(const char * unencrypted, char * encrypted, unsigned long length )
9+ void _decrypt_into(const char * encrypted, char * unencrypted, unsigned long length )
1010
11- cdef void _decrypt(const char * encrypted, char ** unencrypted, Py_ssize_t * length):
12- cdef Py_ssize_t n = strlen(encrypted)
13- unencrypted[0 ] = < char * > malloc((n + 1 ) * sizeof(char ))
11+ cdef void _decrypt(const char * encrypted, char ** unencrypted, Py_ssize_t length):
12+ unencrypted[0 ] = < char * > malloc((length + 1 ) * sizeof(char ))
1413 if not unencrypted[0 ]:
1514 return # malloc failed
16- _decrypt_into(encrypted, unencrypted[0 ] )
17- length[0 ] = n
15+ _decrypt_into(encrypted, unencrypted[0 ], length)
1816
19- cdef void _encrypt(const char * unencrypted, char ** encrypted, Py_ssize_t * length):
20- cdef Py_ssize_t n = strlen(unencrypted)
21- encrypted[0 ] = < char * > malloc((n + 1 ) * sizeof(char ))
17+ cdef void _encrypt(const char * unencrypted, char ** encrypted, Py_ssize_t length):
18+ encrypted[0 ] = < char * > malloc((length + 1 ) * sizeof(char ))
2219 if not encrypted[0 ]:
2320 return # malloc failed
24- _encrypt_into(unencrypted, encrypted[0 ])
25- length[0 ] = n
21+ _encrypt_into(unencrypted, encrypted[0 ], length)
2622
2723def encrypt (string: str ) -> bytes:
2824 cdef char* encrypted = NULL
29- cdef Py_ssize_t length = 0
30- _encrypt(string.encode('utf-8'), &encrypted , &length )
25+ py_byte_string = string.encode(' utf-8' )
26+ cdef Py_ssize_t length = len (py_byte_string)
27+ _encrypt(py_byte_string , &encrypted , length )
3128 try:
3229 return encrypted[:length]
3330 finally:
3431 free(encrypted )
3532
3633def decrypt(string: bytes ) -> str:
3734 cdef char* unencrypted = NULL
38- cdef Py_ssize_t length = 0
39- _decrypt(string , &unencrypted , & length )
35+ cdef Py_ssize_t length = len (string)
36+ _decrypt(string , &unencrypted , length )
4037 try:
4138 return unencrypted[:length].decode('utf-8')
4239 finally:
0 commit comments