3
3
#include " ArchiveStorageDecryptor.hpp"
4
4
#include < Python.h>
5
5
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)
7
7
{
8
8
unsigned char count_byte = substitute_data[((index >> 2 ) & 3 ) + 4 ]
9
9
+ substitute_data[index & 3 ]
@@ -15,7 +15,7 @@ inline unsigned char decrypt_byte(unsigned char* bytes, uint64_t& offset, uint64
15
15
return count_byte;
16
16
}
17
17
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)
19
19
{
20
20
uint64_t offset = 0 ;
21
21
@@ -52,10 +52,10 @@ inline uint64_t decrypt(unsigned char* bytes, uint64_t index, uint64_t remaining
52
52
return offset;
53
53
}
54
54
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;
59
59
uint64_t index;
60
60
61
61
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) {
73
73
return NULL ;
74
74
}
75
75
76
- unsigned char * data = (unsigned char *)view.buf ;
76
+ const unsigned char * data = (unsigned char *)view.buf ;
77
77
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);
80
84
81
85
uint64_t offset = 0 ;
82
86
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);
84
88
}
85
89
90
+ PyObject* result = PyBytes_FromStringAndSize ((const char *)decrypted_data, size);
91
+ PyMem_Free (decrypted_data);
86
92
PyBuffer_Release (&view);
87
- Py_RETURN_NONE;
93
+
94
+ return result;
88
95
}
89
96
0 commit comments