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, const unsigned char *index_data, const 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, const unsigned char *index_data, const unsigned char *substitute_data)
19
19
{
20
20
uint64_t offset = 0 ;
21
21
@@ -53,39 +53,37 @@ inline uint64_t decrypt(unsigned char *bytes, uint64_t index, uint64_t remaining
53
53
}
54
54
55
55
PyObject *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 ;
59
59
uint64_t index;
60
60
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);
62
65
return NULL ;
63
66
}
64
67
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);
75
73
return NULL ;
76
74
}
77
75
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 );
82
78
83
79
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 );
86
82
}
87
83
88
- PyBuffer_Release (&view);
84
+ PyBuffer_Release (&index_data);
85
+ PyBuffer_Release (&substitute_data);
86
+ PyBuffer_Release (&data);
89
87
90
88
return result;
91
89
}
0 commit comments