Skip to content

Commit f510d24

Browse files
AXiX-officialK0lb3
authored andcommitted
Returns a new bytes object instead of modifying it in place.
1 parent ec74e4b commit f510d24

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
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-
) -> None: ...
46+
) -> bytes: ...

UnityPy/helpers/ArchiveStorageManager.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ def __init__(self, reader: EndianBinaryReader) -> None:
108108
def decrypt_block(self, data: bytes, index: int):
109109

110110
if UnityPyBoost:
111-
UnityPyBoost.decrypt_block(bytes(self.index), bytes(self.substitute), data, index)
112-
return data
111+
return UnityPyBoost.decrypt_block(bytes(self.index), bytes(self.substitute), data, index)
113112

114113
offset = 0
115114
size = len(data)

UnityPyBoost/ArchiveStorageDecryptor.cpp

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
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

UnityPyBoost/ArchiveStorageDecryptor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
#pragma once
33
#include <Python.h>
44

5-
PyObject* decrypt_block(PyObject* self, PyObject* args);
5+
PyObject *decrypt_block(PyObject *self, PyObject *args);

0 commit comments

Comments
 (0)