33#include " ArchiveStorageDecryptor.hpp"
44#include < Python.h>
55
6- inline unsigned char decrypt_byte (unsigned char * bytes, uint64_t & offset, uint64_t & index, unsigned char * index_data, unsigned char * substitute_data)
6+ inline unsigned char decrypt_byte (unsigned char * bytes, uint64_t & offset, uint64_t & index, unsigned char * index_data, unsigned char * substitute_data)
77{
88 unsigned char count_byte = substitute_data[((index >> 2 ) & 3 ) + 4 ]
99 + substitute_data[index & 3 ]
@@ -15,7 +15,7 @@ inline unsigned char decrypt_byte(unsigned char* bytes, uint64_t& offset, uint64
1515 return count_byte;
1616}
1717
18- inline uint64_t decrypt (unsigned char * bytes, uint64_t index, uint64_t remaining, unsigned char * index_data, unsigned char * substitute_data)
18+ inline uint64_t decrypt (unsigned char * bytes, uint64_t index, uint64_t remaining, unsigned char * index_data, unsigned char * substitute_data)
1919{
2020 uint64_t offset = 0 ;
2121
@@ -52,10 +52,10 @@ inline uint64_t decrypt(unsigned char* bytes, uint64_t index, uint64_t remaining
5252 return offset;
5353}
5454
55- PyObject* decrypt_block (PyObject* self, PyObject* args) {
56- PyObject* py_index_bytes;
57- PyObject* py_substitute_bytes;
58- PyObject* py_data;
55+ PyObject * decrypt_block (PyObject * self, PyObject * args) {
56+ PyObject * py_index_bytes;
57+ PyObject * py_substitute_bytes;
58+ PyObject * py_data;
5959 uint64_t index;
6060
6161 if (!PyArg_ParseTuple (args, " OOOi" , &py_index_bytes, &py_substitute_bytes, &py_data, &index)) {
@@ -73,17 +73,24 @@ PyObject* decrypt_block(PyObject* self, PyObject* args) {
7373 return NULL ;
7474 }
7575
76- unsigned char * data = (unsigned char *)view.buf ;
76+ const unsigned char * data = (unsigned char *)view.buf ;
7777 uint64_t size = (uint64_t )view.len ;
78- unsigned char * index_data = (unsigned char *)PyBytes_AS_STRING (py_index_bytes);
79- unsigned char * substitute_data = (unsigned char *)PyBytes_AS_STRING (py_substitute_bytes);
78+ unsigned char *index_data = (unsigned char *)PyBytes_AS_STRING (py_index_bytes);
79+ unsigned char *substitute_data = (unsigned char *)PyBytes_AS_STRING (py_substitute_bytes);
80+
81+ unsigned char *decrypted_data = (unsigned char *)PyMem_Malloc (size + 1 );
82+ decrypted_data[size] = 0 ;
83+ memcpy (decrypted_data, data, size);
8084
8185 uint64_t offset = 0 ;
8286 while (offset < size) {
83- offset += decrypt (data + offset, index++, size - offset, index_data, substitute_data);
87+ offset += decrypt (decrypted_data + offset, index++, size - offset, index_data, substitute_data);
8488 }
8589
90+ PyObject* result = PyBytes_FromStringAndSize ((const char *)decrypted_data, size);
91+ PyMem_Free (decrypted_data);
8692 PyBuffer_Release (&view);
87- Py_RETURN_NONE;
93+
94+ return result;
8895}
8996
0 commit comments