Skip to content

Commit

Permalink
feat: deprecate TBC_NO_CHECK
Browse files Browse the repository at this point in the history
  • Loading branch information
Starry-OvO committed Feb 23, 2024
1 parent d9ef48a commit 00a74df
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 45 deletions.
2 changes: 1 addition & 1 deletion aiotieba/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "4.0.4a0"
__version__ = "4.0.4a1"
12 changes: 0 additions & 12 deletions aiotieba/helper/crypto/src/tbcrypto/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ PyObject* cuid_galaxy2(PyObject* Py_UNUSED(self), PyObject* args)
const unsigned char* androidID;
Py_ssize_t androidIDSize;

#ifdef TBC_NO_CHECK
PyArg_ParseTuple(args, "s#", &androidID, &androidIDSize);
#else
if (!PyArg_ParseTuple(args, "s#", &androidID, &androidIDSize)) {
PyErr_SetString(PyExc_TypeError, "Failed to parse args");
return NULL;
Expand All @@ -24,7 +21,6 @@ PyObject* cuid_galaxy2(PyObject* Py_UNUSED(self), PyObject* args)
PyErr_Format(PyExc_ValueError, "Invalid size of android_id. Expect 16, got %zu", androidIDSize);
return NULL;
}
#endif

tbc_cuid_galaxy2(androidID, dst);

Expand All @@ -39,9 +35,6 @@ PyObject* c3_aid(PyObject* Py_UNUSED(self), PyObject* args)
const unsigned char* uuid;
Py_ssize_t uuidSize;

#ifdef TBC_NO_CHECK
PyArg_ParseTuple(args, "s#s#", &androidID, &androidIDSize, &uuid, &uuidSize);
#else
if (!PyArg_ParseTuple(args, "s#s#", &androidID, &androidIDSize, &uuid, &uuidSize)) {
PyErr_SetString(PyExc_TypeError, "Failed to parse args");
return NULL;
Expand All @@ -55,7 +48,6 @@ PyObject* c3_aid(PyObject* Py_UNUSED(self), PyObject* args)
PyErr_Format(PyExc_ValueError, "Invalid size of uuid. Expect 36, got %zu", androidIDSize);
return NULL;
}
#endif

tbc_c3_aid(androidID, uuid, dst);

Expand All @@ -70,9 +62,6 @@ PyObject* rc4_42(PyObject* Py_UNUSED(self), PyObject* args)
const unsigned char* cbcSecKey;
Py_ssize_t cbcSecKeySize;

#ifdef TBC_NO_CHECK
PyArg_ParseTuple(args, "s#y#", &xyusMd5Str, &xyusMd5Size, &cbcSecKey, &cbcSecKeySize);
#else
if (!PyArg_ParseTuple(args, "s#y#", &xyusMd5Str, &xyusMd5Size, &cbcSecKey, &cbcSecKeySize)) {
PyErr_SetString(PyExc_TypeError, "Failed to parse args");
return NULL;
Expand All @@ -86,7 +75,6 @@ PyObject* rc4_42(PyObject* Py_UNUSED(self), PyObject* args)
PyErr_Format(PyExc_ValueError, "Invalid size of cbc_sec_key. Expect 16, got %zu", cbcSecKeySize);
return NULL;
}
#endif

tbc_rc4_42(xyusMd5Str, cbcSecKey, dst);

Expand Down
40 changes: 9 additions & 31 deletions aiotieba/helper/crypto/src/tbcrypto/sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ PyObject* sign(PyObject* Py_UNUSED(self), PyObject* args)
{
PyObject* items;

#ifdef TBC_NO_CHECK
PyArg_ParseTuple(args, "O", &items);
#else
if (!PyArg_ParseTuple(args, "O", &items)) {
PyErr_SetString(PyExc_TypeError, "Failed to parse args");
return NULL;
Expand All @@ -36,7 +33,6 @@ PyObject* sign(PyObject* Py_UNUSED(self), PyObject* args)
PyErr_SetString(PyExc_TypeError, "Input should be List[Tuple[str, str | int]]]");
return NULL;
}
#endif

Py_ssize_t listSize = PyList_GET_SIZE(items);

Expand All @@ -48,21 +44,15 @@ PyObject* sign(PyObject* Py_UNUSED(self), PyObject* args)
for (Py_ssize_t iList = 0; iList < listSize; iList++) {
PyObject* item = PyList_GET_ITEM(items, iList);

#ifndef TBC_NO_CHECK
if (!PyTuple_Check(item)) {
PyErr_SetString(PyExc_TypeError, "List item should be Tuple[str, str | int]");
return NULL;
}
#endif

#ifdef TBC_NO_CHECK
PyObject* pyoKey = PyTuple_GET_ITEM(item, 0);
#else
PyObject* pyoKey = PyTuple_GetItem(item, 0);
if (!pyoKey) {
return NULL; // IndexError
}
#endif

char* key;
size_t keySize;
Expand All @@ -74,37 +64,25 @@ PyObject* sign(PyObject* Py_UNUSED(self), PyObject* args)

mbedtls_md5_update(&md5Ctx, (unsigned char*)key, keySize);

#ifdef TBC_NO_CHECK
PyObject* pyoVal = PyTuple_GET_ITEM(item, 1);
#else
PyObject* pyoVal = PyTuple_GetItem(item, 1);
if (!pyoVal) {
return NULL; // IndexError
}
#endif

if (PyUnicode_Check(pyoVal)) {
const char* val;
size_t valSize;
__tbc_pyStr2UTF8(&val, &valSize, pyoVal);
mbedtls_md5_update(&md5Ctx, (unsigned char*)val, valSize);
} else if (PyLong_Check(pyoVal)) {
int64_t ival = PyLong_AsLongLong(pyoVal);
char* val = itoaBuffer;
char* valEnd = i64toa(ival, val);
size_t valSize = valEnd - val;
mbedtls_md5_update(&md5Ctx, (unsigned char*)val, valSize);
} else {

#ifndef TBC_NO_CHECK
if (PyLong_Check(pyoVal)) {
#endif

int64_t ival = PyLong_AsLongLong(pyoVal);
char* val = itoaBuffer;
char* valEnd = i64toa(ival, val);
size_t valSize = valEnd - val;
mbedtls_md5_update(&md5Ctx, (unsigned char*)val, valSize);

#ifndef TBC_NO_CHECK
} else {
PyErr_SetString(PyExc_TypeError, "item[1] should be str or int");
return NULL;
}
#endif
PyErr_SetString(PyExc_TypeError, "item[1] should be str or int");
return NULL;
}
}

Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ def build_extensions(self):
"aiotieba.helper.crypto.crypto",
sources=source_files,
include_dirs=[str(include_dir)],
define_macros=[('TBC_NO_CHECK', None)],
language='c',
)

Expand Down

0 comments on commit 00a74df

Please sign in to comment.