(PS3) ps3py/crypt.c - style nits

This commit is contained in:
twinaphex 2015-10-27 02:02:01 +01:00
parent 0447c1b442
commit a1749c3d1b
1 changed files with 82 additions and 74 deletions

View File

@ -2,86 +2,93 @@
static PyObject *sha1_callback = NULL; static PyObject *sha1_callback = NULL;
static void manipulate(uint8_t *key) { static void manipulate(uint8_t *key)
uint64_t temp = key[0x38] << 56| {
key[0x39] << 48| uint64_t temp = key[0x38] << 56|
key[0x3a] << 40| key[0x39] << 48|
key[0x3b] << 32| key[0x3a] << 40|
key[0x3c] << 24| key[0x3b] << 32|
key[0x3d] << 16| key[0x3c] << 24|
key[0x3e] << 8| key[0x3d] << 16|
key[0x3f]; key[0x3e] << 8|
temp++; key[0x3f];
key[0x38] = (temp >> 56) & 0xff; temp++;
key[0x39] = (temp >> 48) & 0xff; key[0x38] = (temp >> 56) & 0xff;
key[0x3a] = (temp >> 40) & 0xff; key[0x39] = (temp >> 48) & 0xff;
key[0x3b] = (temp >> 32) & 0xff; key[0x3a] = (temp >> 40) & 0xff;
key[0x3c] = (temp >> 24) & 0xff; key[0x3b] = (temp >> 32) & 0xff;
key[0x3d] = (temp >> 16) & 0xff; key[0x3c] = (temp >> 24) & 0xff;
key[0x3e] = (temp >> 8) & 0xff; key[0x3d] = (temp >> 16) & 0xff;
key[0x3f] = (temp >> 0) & 0xff; key[0x3e] = (temp >> 8) & 0xff;
key[0x3f] = (temp >> 0) & 0xff;
} }
static PyObject* pkg_crypt(PyObject *self, PyObject *args) { static PyObject* pkg_crypt(PyObject *self, PyObject *args)
uint8_t *key, *input, *ret; {
int key_length, input_length, length; uint8_t *key, *input, *ret;
int remaining, i, offset=0; int key_length, input_length, length;
int remaining, i, offset=0;
PyObject *arglist; PyObject *arglist;
PyObject *result; PyObject *result;
if (!PyArg_ParseTuple(args, "s#s#i", &key, &key_length, &input, &input_length, &length)) if (!PyArg_ParseTuple(args, "s#s#i", &key, &key_length, &input, &input_length, &length))
return NULL; return NULL;
ret = malloc(length); ret = malloc(length);
remaining = length; remaining = length;
while (remaining > 0) {
int bytes_to_dump = remaining;
if (bytes_to_dump > 0x10)
bytes_to_dump = 0x10;
// outhash = SHA1(listToString(key)[0:0x40]) while (remaining > 0)
uint8_t *outHash; {
{ int outHash_length;
arglist = Py_BuildValue("(s#)", key, 0x40); uint8_t *outHash;
result = PyObject_CallObject(sha1_callback, arglist); int bytes_to_dump = remaining;
Py_DECREF(arglist); if (bytes_to_dump > 0x10)
if (!result) return NULL; bytes_to_dump = 0x10;
int outHash_length;
if (!PyArg_Parse(result, "s#", &outHash, &outHash_length)) return NULL; arglist = Py_BuildValue("(s#)", key, 0x40);
} result = PyObject_CallObject(sha1_callback, arglist);
for(i = 0; i < bytes_to_dump; i++) { Py_DECREF(arglist);
ret[offset] = outHash[i] ^ input[offset]; if (!result)
offset++; return NULL;
} if (!PyArg_Parse(result, "s#", &outHash, &outHash_length))
Py_DECREF(result); return NULL;
manipulate(key);
remaining -= bytes_to_dump; for(i = 0; i < bytes_to_dump; i++)
} {
ret[offset] = outHash[i] ^ input[offset];
// Return the encrypted data offset++;
PyObject *py_ret = Py_BuildValue("s#", ret, length); }
free(ret); Py_DECREF(result);
return py_ret; manipulate(key);
remaining -= bytes_to_dump;
}
/* Return the encrypted data */
PyObject *py_ret = Py_BuildValue("s#", ret, length);
free(ret);
return py_ret;
} }
static PyObject *register_sha1_callback(PyObject *self, PyObject *args) { static PyObject *register_sha1_callback(PyObject *self, PyObject *args)
PyObject *result = NULL; {
PyObject *temp; PyObject *result = NULL;
PyObject *temp;
if (PyArg_ParseTuple(args, "O:set_callback", &temp)) { if (PyArg_ParseTuple(args, "O:set_callback", &temp))
if (!PyCallable_Check(temp)) { {
PyErr_SetString(PyExc_TypeError, "parameter must be callable"); if (!PyCallable_Check(temp))
return NULL; {
} PyErr_SetString(PyExc_TypeError, "parameter must be callable");
Py_XINCREF(temp); /* Add a reference to new callback */ return NULL;
Py_XDECREF(sha1_callback); /* Dispose of previous callback */ }
sha1_callback = temp; /* Remember new callback */ Py_XINCREF(temp); /* Add a reference to new callback */
/* Boilerplate to return "None" */ Py_XDECREF(sha1_callback); /* Dispose of previous callback */
Py_INCREF(Py_None); sha1_callback = temp; /* Remember new callback */
result = Py_None; /* Boilerplate to return "None" */
} Py_INCREF(Py_None);
return result; result = Py_None;
}
return result;
} }
static PyMethodDef cryptMethods[] = { static PyMethodDef cryptMethods[] = {
@ -90,8 +97,9 @@ static PyMethodDef cryptMethods[] = {
{NULL, NULL, 0, NULL} {NULL, NULL, 0, NULL}
}; };
PyMODINIT_FUNC initpkgcrypt(void) { PyMODINIT_FUNC initpkgcrypt(void)
(void) Py_InitModule("pkgcrypt", cryptMethods); {
(void) Py_InitModule("pkgcrypt", cryptMethods);
} }