From bf16930c70ed9e2985dd10b352609ba1e41de189 Mon Sep 17 00:00:00 2001 From: helloplhm-qwq Date: Sat, 20 Apr 2024 23:12:12 +0800 Subject: [PATCH] =?UTF-8?q?chore=20(pyqdes):=20=E5=90=88=E5=B9=B6=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E4=BB=93=E5=BA=93=20x2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- deps/pyqdes/main.cpp | 51 +++++++++++++++++--------------------------- 1 file changed, 20 insertions(+), 31 deletions(-) diff --git a/deps/pyqdes/main.cpp b/deps/pyqdes/main.cpp index 4025bf2..6a0ac6f 100644 --- a/deps/pyqdes/main.cpp +++ b/deps/pyqdes/main.cpp @@ -1,6 +1,6 @@ #include "des.cpp" -#include -#define PY_SSIZE_T_CLEAN +#include + unsigned char KEY1[] = "!@#)(NHLiuy*$%^&"; unsigned char KEY2[] = "123ZXC!@#)(*$%^&"; unsigned char KEY3[] = "!@#)(*$%^&abcDEF"; @@ -30,38 +30,27 @@ void LyricDecode_(unsigned char *content, int len) func_ddes(content, KEY3, len); } -static PyObject* lyric_decode(PyObject* self, PyObject* args) { - const char* content; - int len; +namespace py = pybind11; - // Parse arguments - if (!PyArg_ParseTuple(args, "s#", &content, &len)) { - return NULL; - } +// 修改 LyricDecode 函数,接受和返回字节数组 +py::bytes LyricDecode(py::bytes input) +{ + // 获取输入字节数组的指针和长度 + const char *input_ptr = PyBytes_AsString(input.ptr()); + Py_ssize_t input_len = PyBytes_Size(input.ptr()); - // Call the C function - LyricDecode_((unsigned char*)content, len); + // 复制输入数据以便修改 + std::vector data(input_ptr, input_ptr + input_len); - // Return the result as bytes - return Py_BuildValue("y#", content, len); + // 调用 LyricDecode 函数进行解密 + LyricDecode_(data.data(), data.size()); + + // 创建输出字节数组 + py::bytes output(reinterpret_cast(data.data()), data.size()); + + return output; } -// Method table -static PyMethodDef module_methods[] = { - {"LyricDecode", lyric_decode, METH_VARARGS, "QRC Decrypt."}, - {NULL, NULL, 0, NULL} /* Sentinel */ -}; - -// Module initialization function -static struct PyModuleDef lyric_decoder_module = { - PyModuleDef_HEAD_INIT, - "qdes", - NULL, - -1, - module_methods -}; - -// Module initialization -PyMODINIT_FUNC PyInit_qdes(void) { - return PyModule_Create(&lyric_decoder_module); +PYBIND11_MODULE(qdes, m) { + m.def("LyricDecode", &LyricDecode, "Decrypt a string"); }