-
Notifications
You must be signed in to change notification settings - Fork 0
/
103-python.c
66 lines (56 loc) · 1.44 KB
/
103-python.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <Python.h>
void print_python_list(PyObject *p);
void print_python_bytes(PyObject *p);
/**
* print_python_list - Prints basic info about Python lists.
* @p: A PyObject list object.
*/
void print_python_list(PyObject *p)
{
int size, alloc, i;
const char *type;
PyListObject *list = (PyListObject *)p;
PyVarObject *var = (PyVarObject *)p;
size = var->ob_size;
alloc = list->allocated;
printf("[*] Python list info\n");
printf("[*] Size of the Python List = %d\n", size);
printf("[*] Allocated = %d\n", alloc);
for (i = 0; i < size; i++)
{
type = list->ob_item[i]->ob_type->tp_name;
printf("Element %d: %s\n", i, type);
if (strcmp(type, "bytes") == 0)
print_python_bytes(list->ob_item[i]);
}
}
/**
* print_python_bytes - Prints basic info about Python byte objects.
* @p: A PyObject byte object.
*/
void print_python_bytes(PyObject *p)
{
unsigned char i, size;
PyBytesObject *bytes = (PyBytesObject *)p;
printf("[.] bytes object info\n");
if (strcmp(p->ob_type->tp_name, "bytes") != 0)
{
printf(" [ERROR] Invalid Bytes Object\n");
return;
}
printf(" size: %ld\n", ((PyVarObject *)p)->ob_size);
printf(" trying string: %s\n", bytes->ob_sval);
if (((PyVarObject *)p)->ob_size > 10)
size = 10;
else
size = ((PyVarObject *)p)->ob_size + 1;
printf(" first %d bytes: ", size);
for (i = 0; i < size; i++)
{
printf("%02hhx", bytes->ob_sval[i]);
if (i == (size - 1))
printf("\n");
else
printf(" ");
}
}