-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path103-python.c
More file actions
88 lines (78 loc) · 1.81 KB
/
103-python.c
File metadata and controls
88 lines (78 loc) · 1.81 KB
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
#include <Python.h>
#include <stdio.h>
/**
* print_python_float - gives data of the PyFloatObject
* @p: the PyObject
*/
void print_python_float(PyObject *p)
{
double value = 0;
char *string = NULL;
fflush(stdout);
printf("[.] float object info\n");
if (!PyFloat_CheckExact(p))
{
printf(" [ERROR] Invalid Float Object\n");
return;
}
value = ((PyFloatObject *)p)->ob_fval;
string = PyOS_double_to_string(value, 'r', 0, Py_DTSF_ADD_DOT_0, NULL);
printf(" value: %s\n", string);
}
/**
* print_python_bytes - gives data of the PyBytesObject
* @p: the PyObject
*/
void print_python_bytes(PyObject *p)
{
Py_ssize_t size = 0, i = 0;
char *string = NULL;
fflush(stdout);
printf("[.] bytes object info\n");
if (!PyBytes_CheckExact(p))
{
printf(" [ERROR] Invalid Bytes Object\n");
return;
}
size = PyBytes_Size(p);
printf(" size: %zd\n", size);
string = (assert(PyBytes_Check(p)), (((PyBytesObject *)(p))->ob_sval));
printf(" trying string: %s\n", string);
printf(" first %zd bytes:", size < 10 ? size + 1 : 10);
while (i < size + 1 && i < 10)
{
printf(" %02hhx", string[i]);
i++;
}
printf("\n");
}
/**
* print_python_list - gives data of the PyListObject
* @p: the PyObject
*/
void print_python_list(PyObject *p)
{
Py_ssize_t size = 0;
PyObject *item;
int i = 0;
fflush(stdout);
printf("[*] Python list info\n");
if (PyList_CheckExact(p))
{
size = PyList_GET_SIZE(p);
printf("[*] Size of the Python List = %zd\n", size);
printf("[*] Allocated = %lu\n", ((PyListObject *)p)->allocated);
while (i < size)
{
item = PyList_GET_ITEM(p, i);
printf("Element %d: %s\n", i, item->ob_type->tp_name);
if (PyBytes_Check(item))
print_python_bytes(item);
else if (PyFloat_Check(item))
print_python_float(item);
i++;
}
}
else
printf(" [ERROR] Invalid List Object\n");
}