XOR bytearray の高速処理が必要です。Python のバリアントで
for i in range(len(str1)): str1[i]=str1[i] ^ 55
動作が非常に遅い
このモジュールは C で書きました。C 言語の知識はほとんどありませんが、何も書いていませんでした。
バリアントで
PyArg_ParseTuple (args, "s", &str))
すべてが期待どおりに機能しますが、要素にnullが含まれている可能性があるため、ss *の代わりに使用する必要がありますが、pythonクラッシュを呼び出すときにsをs *に変更すると
PyArg_ParseTuple (args, "s*", &str)) // crash
私のような初心者は、私の例を使って自分で何かを書きたいと思うかもしれません。そのため、Windows でこの例で使用するすべての情報を持ってきてください。
ページの引数の解析と値の構築http://docs.python.org/dev/c-api/arg.html
test_xor.c
#include <Python.h>
static PyObject* fast_xor(PyObject* self, PyObject* args)
{
const char* str ;
int i;
if (!PyArg_ParseTuple(args, "s", &str))
return NULL;
for(i=0;i<sizeof(str);i++) {str[i]^=55;};
return Py_BuildValue("s", str);
}
static PyMethodDef fastxorMethods[] =
{
{"fast_xor", fast_xor, METH_VARARGS, "fast_xor desc"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initfastxor(void)
{
(void) Py_InitModule("fastxor", fastxorMethods);
}
test_xor.py
import fastxor
a=fastxor.fast_xor("World") # it works with s instead s*
print a
a=fastxor.fast_xor("Wo\0rld") # It does not work with s instead s*
コンパイル.バット
rem use http://bellard.org/tcc/
tiny_impdef.exe C:\Python26\python26.dll
tcc -shared test_xor.c python26.def -IC:\Python26\include -LC:\Python26\libs -ofastxor.pyd
test_xor.py