Skip to content

Commit

Permalink
Returns a new bytes object instead of modifying it in place.
Browse files Browse the repository at this point in the history
  • Loading branch information
AXiX-official committed Oct 25, 2024
1 parent a29975c commit adcb96a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
2 changes: 1 addition & 1 deletion UnityPy/UnityPyBoost.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ def decrypt_block(
substitute_bytes: bytes,
data: Union[bytes, bytearray],
index: int,
) -> None: ...
) -> bytes: ...
3 changes: 1 addition & 2 deletions UnityPy/helpers/ArchiveStorageManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ def __init__(self, reader: EndianBinaryReader) -> None:
def decrypt_block(self, data: bytes, index: int):

if UnityPyBoost:
UnityPyBoost.decrypt_block(bytes(self.index), bytes(self.substitute), data, index)
return data
return UnityPyBoost.decrypt_block(bytes(self.index), bytes(self.substitute), data, index)

offset = 0
size = len(data)
Expand Down
29 changes: 18 additions & 11 deletions UnityPyBoost/ArchiveStorageDecryptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "ArchiveStorageDecryptor.hpp"
#include <Python.h>

inline unsigned char decrypt_byte(unsigned char* bytes, uint64_t& offset, uint64_t& index, unsigned char* index_data, unsigned char* substitute_data)
inline unsigned char decrypt_byte(unsigned char *bytes, uint64_t& offset, uint64_t& index, unsigned char *index_data, unsigned char *substitute_data)
{
unsigned char count_byte = substitute_data[((index >> 2) & 3) + 4]
+ substitute_data[index & 3]
Expand All @@ -15,7 +15,7 @@ inline unsigned char decrypt_byte(unsigned char* bytes, uint64_t& offset, uint64
return count_byte;
}

inline uint64_t decrypt(unsigned char* bytes, uint64_t index, uint64_t remaining, unsigned char* index_data, unsigned char* substitute_data)
inline uint64_t decrypt(unsigned char *bytes, uint64_t index, uint64_t remaining, unsigned char *index_data, unsigned char *substitute_data)
{
uint64_t offset = 0;

Expand Down Expand Up @@ -52,10 +52,10 @@ inline uint64_t decrypt(unsigned char* bytes, uint64_t index, uint64_t remaining
return offset;
}

PyObject* decrypt_block(PyObject* self, PyObject* args) {
PyObject* py_index_bytes;
PyObject* py_substitute_bytes;
PyObject* py_data;
PyObject *decrypt_block(PyObject *self, PyObject *args) {
PyObject *py_index_bytes;
PyObject *py_substitute_bytes;
PyObject *py_data;
uint64_t index;

if (!PyArg_ParseTuple(args, "OOOi", &py_index_bytes, &py_substitute_bytes, &py_data, &index)) {
Expand All @@ -73,17 +73,24 @@ PyObject* decrypt_block(PyObject* self, PyObject* args) {
return NULL;
}

unsigned char* data = (unsigned char*)view.buf;
const unsigned char *data = (unsigned char *)view.buf;
uint64_t size = (uint64_t)view.len;
unsigned char* index_data = (unsigned char*)PyBytes_AS_STRING(py_index_bytes);
unsigned char* substitute_data = (unsigned char*)PyBytes_AS_STRING(py_substitute_bytes);
unsigned char *index_data = (unsigned char *)PyBytes_AS_STRING(py_index_bytes);
unsigned char *substitute_data = (unsigned char *)PyBytes_AS_STRING(py_substitute_bytes);

unsigned char *decrypted_data = (unsigned char *)PyMem_Malloc(size + 1);
decrypted_data[size] = 0;
memcpy(decrypted_data, data, size);

uint64_t offset = 0;
while (offset < size) {
offset += decrypt(data + offset, index++, size - offset, index_data, substitute_data);
offset += decrypt(decrypted_data + offset, index++, size - offset, index_data, substitute_data);
}

PyObject* result = PyBytes_FromStringAndSize((const char*)decrypted_data, size);
PyMem_Free(decrypted_data);
PyBuffer_Release(&view);
Py_RETURN_NONE;

return result;
}

2 changes: 1 addition & 1 deletion UnityPyBoost/ArchiveStorageDecryptor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
#pragma once
#include <Python.h>

PyObject* decrypt_block(PyObject* self, PyObject* args);
PyObject *decrypt_block(PyObject *self, PyObject *args);

0 comments on commit adcb96a

Please sign in to comment.