Skip to content

Commit a3a5cd5

Browse files
AXiX-officialK0lb3
authored andcommitted
copy bytes by PyBytes_FromObject
1 parent f510d24 commit a3a5cd5

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

UnityPy/UnityPyBoost.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,4 @@ def decrypt_block(
4343
substitute_bytes: bytes,
4444
data: Union[bytes, bytearray],
4545
index: int,
46-
) -> bytes: ...
46+
) -> bytearray: ...

UnityPyBoost/ArchiveStorageDecryptor.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ PyObject *decrypt_block(PyObject *self, PyObject *args) {
6262
return NULL;
6363
}
6464

65+
PyObject *result = PyBytes_FromObject(py_data);
66+
6567
Py_buffer view;
66-
if (PyObject_GetBuffer(py_data, &view, PyBUF_SIMPLE) != 0) {
68+
if (PyObject_GetBuffer(result, &view, PyBUF_SIMPLE) != 0) {
6769
return NULL;
6870
}
6971

@@ -73,22 +75,16 @@ PyObject *decrypt_block(PyObject *self, PyObject *args) {
7375
return NULL;
7476
}
7577

76-
const unsigned char *data = (unsigned char *)view.buf;
78+
unsigned char *data = (unsigned char *)view.buf;
7779
uint64_t size = (uint64_t)view.len;
7880
unsigned char *index_data = (unsigned char *)PyBytes_AS_STRING(py_index_bytes);
7981
unsigned char *substitute_data = (unsigned char *)PyBytes_AS_STRING(py_substitute_bytes);
8082

81-
unsigned char *decrypted_data = (unsigned char *)PyMem_Malloc(size + 1);
82-
decrypted_data[size] = 0;
83-
memcpy(decrypted_data, data, size);
84-
8583
uint64_t offset = 0;
8684
while (offset < size) {
87-
offset += decrypt(decrypted_data + offset, index++, size - offset, index_data, substitute_data);
85+
offset += decrypt(data + offset, index++, size - offset, index_data, substitute_data);
8886
}
8987

90-
PyObject* result = PyBytes_FromStringAndSize((const char*)decrypted_data, size);
91-
PyMem_Free(decrypted_data);
9288
PyBuffer_Release(&view);
9389

9490
return result;

0 commit comments

Comments
 (0)