|
| 1 | +#include <Python.h> |
| 2 | +#include <stdio.h> |
| 3 | +#include <string.h> |
| 4 | + |
| 5 | +void print_python_list(PyObject *p); |
| 6 | +void print_python_bytes(PyObject *p); |
| 7 | + |
| 8 | +/** |
| 9 | +* print_python_bytes - print some basic info about Python bytes objects |
| 10 | +* @p: python object |
| 11 | +* Return: nothing |
| 12 | +**/ |
| 13 | +void print_python_bytes(PyObject *p) |
| 14 | +{ |
| 15 | + char *s; |
| 16 | + Py_ssize_t l, i; |
| 17 | + |
| 18 | + printf("[.] bytes object info\n"); |
| 19 | + if (!PyBytes_Check(p)) |
| 20 | + printf(" [ERROR] Invalid Bytes Object\n"); |
| 21 | + else |
| 22 | + { |
| 23 | + l = ((PyVarObject *) p)->ob_size; |
| 24 | + printf(" size: %lu\n", l); |
| 25 | + s = ((PyBytesObject *) p)->ob_sval; |
| 26 | + printf(" trying string: %s\n", s); |
| 27 | + if (l > 10) |
| 28 | + l = 10; |
| 29 | + else |
| 30 | + l++; |
| 31 | + printf(" first %lu bytes: ", l); |
| 32 | + for (i = 0; i < l - 1; i++) |
| 33 | + printf("%02x ", s[i] & 0xff); |
| 34 | + printf("%02x\n", s[l - 1] & 0xff); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | +* print_python_list - print some basic info about Python lists |
| 40 | +* @p: python object |
| 41 | +**/ |
| 42 | +void print_python_list(PyObject *p) |
| 43 | +{ |
| 44 | + Py_ssize_t listsize, i; |
| 45 | + PyObject *item; |
| 46 | + |
| 47 | + printf("[*] Python list info\n"); |
| 48 | + if (!PyList_Check(p)) |
| 49 | + printf(" [ERROR] Invalid List Object\n"); |
| 50 | + else |
| 51 | + { |
| 52 | + listsize = ((PyVarObject *) p)->ob_size; |
| 53 | + printf("[*] Size of the Python List = %lu\n", listsize); |
| 54 | + printf("[*] Allocated = %lu\n", |
| 55 | + ((PyListObject *) p)->allocated); |
| 56 | + for (i = 0; i < listsize; i++) |
| 57 | + { |
| 58 | + item = ((PyListObject *) p)->ob_item[i]; |
| 59 | + printf("Element %lu: %s\n", i, item->ob_type->tp_name); |
| 60 | + if (strcmp(item->ob_type->tp_name, "bytes") == 0) |
| 61 | + print_python_bytes(item); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments