-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path103-python.c
More file actions
84 lines (72 loc) · 1.69 KB
/
Copy path103-python.c
File metadata and controls
84 lines (72 loc) · 1.69 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
#include <Python.h>
#include <stdio.h>
/**
* print_python_bytes - Prints information about Python bytes.
* @p: Python object.
* Return: Void.
*/
void print_python_bytes(PyObject *p)
{
long int size;
int i;
char *str;
printf("[.] bytes object info\n");
if (!PyBytes_Check(p))
{
printf(" [ERROR] Invalid Bytes Object\n");
}
else
{
size = ((PyVarObject *)(p))->ob_size;
str = ((PyBytesObject *)(p))->ob_sval;
printf(" size: %ld\n", size);
printf(" trying string: %s\n", str);
size++;
if (size >= 10)
{
size = 10;
}
printf(" first %ld bytes:", size);
for (i = 0; i < size; i++)
{
if (str[i] < 0)
{
printf(" %02x", 256 + str[i]);
}
else
{
printf(" %02x", str[i]);
}
}
printf("\n");
}
}
/**
* print_python_list - Prints information about Python lists.
* @p: Python object.
* Return: Void.
*/
void print_python_list(PyObject *p)
{
int size;
int alloc;
int i;
const char *type_name;
PyObject *item;
size = (((PyVarObject *)(p))->ob_size);
alloc = ((PyListObject *)(p))->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++)
{
item = ((PyListObject *)p)->ob_item[i];
type_name = (item->ob_type)->tp_name;
printf("Element %d: ", i);
printf("%s\n", type_name);
if (PyBytes_Check(item))
{
print_python_bytes(item);
}
}
}