Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate ps3py to Python 3 #128

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions tools/ps3py/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ endif
all: pkgcrypt.so

pkgcrypt.so: crypt.c setup.py
@`./find-python2` setup.py build_ext $(COMPILER)
@python3 setup.py build_ext $(COMPILER)
@cp build/lib.*/pkgcrypt.* .

clean:
Expand All @@ -27,5 +27,5 @@ clean:
install: all
@[ -d $(PS3DEV)/bin ] || mkdir -p $(PS3DEV)/bin
@echo Installing ICON0.PNG sfo.xml pkgcrypt.* fself.py Struct.py sfo.py pkg.py
@install -m 644 ICON0.PNG sfo.xml pkgcrypt.* $(PS3DEV)/bin
@./install-scripts $(PS3DEV)/bin/ fself.py Struct.py sfo.py pkg.py
@install -m 644 ICON0.PNG sfo.xml pkgcrypt.* Struct.py $(PS3DEV)/bin
@install -m 755 fself.py sfo.py pkg.py $(PS3DEV)/bin/
6 changes: 3 additions & 3 deletions tools/ps3py/Struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class StructException(Exception):
pass

class Struct(object):
__slots__ = ('__attrs__', '__baked__', '__defs__', '__endian__', '__next__', '__sizes__', '__values__')
__slots__ = ('__attrs__', '__baked__', '__defs__', '__next__', '__sizes__', '__values__')
int8 = StructType(('b', 1))
uint8 = StructType(('B', 1))

Expand Down Expand Up @@ -49,7 +49,7 @@ def __init__(self, func=None, unpack=None, **kwargs):
else:
sys.settrace(self.__trace__)
func()
for name in func.func_code.co_varnames:
for name in func.__code__.co_varnames:
value = self.__frame__.f_locals[name]
self.__setattr__(name, value)

Expand Down Expand Up @@ -231,7 +231,7 @@ def unpack(self, data, pos=0):
def pack(self):
arraypos, arrayname = None, None

ret = ''
ret = b''
for i in range(len(self.__defs__)):
sdef, size, attrs = self.__defs__[i], self.__sizes__[i], self.__attrs__[i]

Expand Down
55 changes: 42 additions & 13 deletions tools/ps3py/crypt.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>

static PyObject *sha1_callback = NULL;
Expand Down Expand Up @@ -25,32 +26,46 @@ static void manipulate(uint8_t *key) {
}

static PyObject* pkg_crypt(PyObject *self, PyObject *args) {
uint8_t *key, *input, *ret;
int key_length, input_length, length;
int remaining, i, offset=0;
const uint8_t *rokey, *input;
uint8_t *ret;
uint8_t key[0x40];
Py_ssize_t key_length, input_length, length;
Py_ssize_t remaining, i, offset=0;

PyObject *arglist;
PyObject *result;

if (!PyArg_ParseTuple(args, "s#s#i", &key, &key_length, &input, &input_length, &length))
(void) self;

if (!PyArg_ParseTuple(args, "y#y#n", &rokey, &key_length, &input, &input_length, &length))
return NULL;

if (key_length != 0x40)
return NULL;

if (input_length < length)
return NULL;

memcpy(key, rokey, sizeof(key));

ret = malloc(length);
remaining = length;

while (remaining > 0) {
int bytes_to_dump = remaining;
Py_ssize_t bytes_to_dump = remaining;
if (bytes_to_dump > 0x10)
bytes_to_dump = 0x10;

// outhash = SHA1(listToString(key)[0:0x40])
uint8_t *outHash;
{
arglist = Py_BuildValue("(s#)", key, 0x40);
arglist = Py_BuildValue("(y#)", key, 0x40);
result = PyObject_CallObject(sha1_callback, arglist);
Py_DECREF(arglist);
if (!result) return NULL;
int outHash_length;
if (!PyArg_Parse(result, "s#", &outHash, &outHash_length)) return NULL;
Py_ssize_t outHash_length;
if (!PyArg_Parse(result, "y#", &outHash, &outHash_length)) return NULL;
if (outHash_length < 0x10) return NULL;
}
for(i = 0; i < bytes_to_dump; i++) {
ret[offset] = outHash[i] ^ input[offset];
Expand All @@ -60,9 +75,9 @@ static PyObject* pkg_crypt(PyObject *self, PyObject *args) {
manipulate(key);
remaining -= bytes_to_dump;
}

// Return the encrypted data
PyObject *py_ret = Py_BuildValue("s#", ret, length);
PyObject *py_ret = Py_BuildValue("y#y#", ret, length, key, sizeof(key));
free(ret);
return py_ret;
}
Expand All @@ -71,6 +86,8 @@ static PyObject *register_sha1_callback(PyObject *self, PyObject *args) {
PyObject *result = NULL;
PyObject *temp;

(void) self;

if (PyArg_ParseTuple(args, "O:set_callback", &temp)) {
if (!PyCallable_Check(temp)) {
PyErr_SetString(PyExc_TypeError, "parameter must be callable");
Expand All @@ -92,8 +109,20 @@ static PyMethodDef cryptMethods[] = {
{NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initpkgcrypt(void) {
(void) Py_InitModule("pkgcrypt", cryptMethods);
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"pkgcrypt", /* m_name */
NULL, /* m_doc */
-1, /* m_size */
cryptMethods, /* m_methods */
NULL, /* m_reload */
NULL, /* m_traverse */
NULL, /* m_clear */
NULL, /* m_free */
};

PyMODINIT_FUNC PyInit_pkgcrypt(void) {
return PyModule_Create(&moduledef);
}


5 changes: 0 additions & 5 deletions tools/ps3py/find-python2

This file was deleted.

9 changes: 4 additions & 5 deletions tools/ps3py/fself.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python
from __future__ import with_statement
#!/usr/bin/env python3
from Struct import Struct
import struct
import getopt
Expand Down Expand Up @@ -115,7 +114,7 @@ def align(address, alignment):

def padding(address, alignment):
padding = alignment - (address % alignment)
return "\0" * padding
return b"\0" * padding

def readElf(infile):
with open(infile, 'rb') as fp:
Expand Down Expand Up @@ -243,11 +242,11 @@ def createFself(npdrm, infile, outfile="EBOOT.BIN"):


def usage():
print """fself.py usage:
print("""fself.py usage:
fself.py [options] input.elf output.self
If output file is not specified, fself.py will default to EBOOT.BIN
Options:
--npdrm: will output a file for use with pkg.py."""
--npdrm: will output a file for use with pkg.py.""")
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "hn", ["help", "npdrm"])
Expand Down
10 changes: 0 additions & 10 deletions tools/ps3py/install-scripts

This file was deleted.

Loading