-
Notifications
You must be signed in to change notification settings - Fork 0
/
103-python.c
104 lines (86 loc) · 2.23 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <Python.h>
void print_python_list(PyObject *p);
void print_python_bytes(PyObject *p);
void print_python_float(PyObject *p);
/**
* print_python_list - Prints basic info about Python lists.
* @p: A PyObject list object.
*/
void print_python_list(PyObject *p)
{
Py_ssize_t size, alloc, i;
const char *type;
PyListObject *list = (PyListObject *)p;
PyVarObject *var = (PyVarObject *)p;
size = var->ob_size;
alloc = list->allocated;
fflush(stdout);
printf("[*] Python list info\n");
if (strcmp(p->ob_type->tp_name, "list") != 0)
{
printf(" [ERROR] Invalid List Object\n");
return;
}
printf("[*] Size of the Python List = %ld\n", size);
printf("[*] Allocated = %ld\n", alloc);
for (i = 0; i < size; i++)
{
type = list->ob_item[i]->ob_type->tp_name;
printf("Element %ld: %s\n", i, type);
if (strcmp(type, "bytes") == 0)
print_python_bytes(list->ob_item[i]);
else if (strcmp(type, "float") == 0)
print_python_float(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)
{
Py_ssize_t size, i;
PyBytesObject *bytes = (PyBytesObject *)p;
fflush(stdout);
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 %ld bytes: ", size);
for (i = 0; i < size; i++)
{
printf("%02hhx", bytes->ob_sval[i]);
if (i == (size - 1))
printf("\n");
else
printf(" ");
}
}
/**
* print_python_float - Prints basic info about Python float objects.
* @p: A PyObject float object.
*/
void print_python_float(PyObject *p)
{
char *buffer = NULL;
PyFloatObject *float_obj = (PyFloatObject *)p;
fflush(stdout);
printf("[.] float object info\n");
if (strcmp(p->ob_type->tp_name, "float") != 0)
{
printf(" [ERROR] Invalid Float Object\n");
return;
}
buffer = PyOS_double_to_string(float_obj->ob_fval, 'r', 0,
Py_DTSF_ADD_DOT_0, NULL);
printf(" value: %s\n", buffer);
PyMem_Free(buffer);
}