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, const unsigned char *index_data, const 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, const unsigned char *index_data, const unsigned char *substitute_data)
1919{
2020 uint64_t offset = 0 ;
2121
@@ -53,39 +53,37 @@ inline uint64_t decrypt(unsigned char *bytes, uint64_t index, uint64_t remaining
5353}
5454
5555PyObject *decrypt_block (PyObject *self, PyObject *args) {
56- PyObject *py_index_bytes ;
57- PyObject *py_substitute_bytes ;
58- PyObject *py_data ;
56+ Py_buffer index_data ;
57+ Py_buffer substitute_data ;
58+ Py_buffer data ;
5959 uint64_t index;
6060
61- if (!PyArg_ParseTuple (args, " OOOi" , &py_index_bytes, &py_substitute_bytes, &py_data, &index)) {
61+ if (!PyArg_ParseTuple (args, " y*y*y*K" , &index_data, &substitute_data, &data, &index)) {
62+ if (index_data.buf ) PyBuffer_Release (&index_data);
63+ if (substitute_data.buf ) PyBuffer_Release (&substitute_data);
64+ if (data.buf ) PyBuffer_Release (&data);
6265 return NULL ;
6366 }
6467
65- PyObject *result = PyBytes_FromObject (py_data);
66-
67- Py_buffer view;
68- if (PyObject_GetBuffer (result, &view, PyBUF_SIMPLE) != 0 ) {
69- return NULL ;
70- }
71-
72- if (!PyBytes_Check (py_index_bytes) || !PyBytes_Check (py_substitute_bytes)) {
73- PyBuffer_Release (&view);
74- PyErr_SetString (PyExc_TypeError, " Attributes 'index' and 'substitute' must be bytes" );
68+ PyObject *result = PyBytes_FromStringAndSize (NULL , data.len );
69+ if (result == NULL ) {
70+ PyBuffer_Release (&index_data);
71+ PyBuffer_Release (&substitute_data);
72+ PyBuffer_Release (&data);
7573 return NULL ;
7674 }
7775
78- unsigned char *data = (unsigned char *)view.buf ;
79- uint64_t size = (uint64_t )view.len ;
80- unsigned char *index_data = (unsigned char *)PyBytes_AS_STRING (py_index_bytes);
81- unsigned char *substitute_data = (unsigned char *)PyBytes_AS_STRING (py_substitute_bytes);
76+ unsigned char *result_raw = (unsigned char *)PyBytes_AS_STRING (result);
77+ memcpy (result_raw, data.buf , data.len );
8278
8379 uint64_t offset = 0 ;
84- while (offset < size ) {
85- offset += decrypt (data + offset, index++, size - offset, index_data, substitute_data);
80+ while (offset < data. len ) {
81+ offset += decrypt (result_raw + offset, index++, data. len - offset, ( unsigned char *) index_data. buf , ( unsigned char *) substitute_data. buf );
8682 }
8783
88- PyBuffer_Release (&view);
84+ PyBuffer_Release (&index_data);
85+ PyBuffer_Release (&substitute_data);
86+ PyBuffer_Release (&data);
8987
9088 return result;
9189}
0 commit comments