-
Notifications
You must be signed in to change notification settings - Fork 34
/
generateUnameOffsets.py
132 lines (121 loc) · 4.77 KB
/
generateUnameOffsets.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python3
# SysinternalsEBPF
#
# Copyright (c) Microsoft Corporation
#
# All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import sys
import json
import re
if len(sys.argv) < 2 or len(sys.argv) > 3:
print(f'Usage: {sys.argv[0]} <reqd offsets json file> [[PUBLIC_]HEADER]')
sys.exit(1)
header = False
public_header = False
if len(sys.argv) == 3 and sys.argv[2] == 'HEADER':
header = True
elif len(sys.argv) == 3 and sys.argv[2] == 'PUBLIC_HEADER':
public_header = True
else:
data = []
with open(sys.argv[1]) as offsetsReqd:
reqd = json.load(offsetsReqd)
params = reqd["params"]
params_opt = reqd["params_opt"]
num_redirects = reqd["num_redirects"]
deref_end = reqd["deref_end"]
print('/*')
print('SysinternalsEBPF')
print('')
print('Copyright (c) Microsoft Corporation')
print('')
print('All rights reserved.')
print('')
print('This library is free software; you can redistribute it and/or')
print('modify it under the terms of the GNU Lesser General Public')
print('License as published by the Free Software Foundation; either')
print('version 2.1 of the License, or (at your option) any later version.')
print('')
print('This library is distributed in the hope that it will be useful,')
print('but WITHOUT ANY WARRANTY; without even the implied warranty of')
print('MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU')
print('Lesser General Public License for more details.')
print('')
print('You should have received a copy of the GNU Lesser General Public')
print('License along with this library; if not, write to the Free Software')
print('Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA')
print('*/\n')
if public_header:
print('#ifndef SYSINTERNALS_EBPF_OFFSETS_H')
print('#define SYSINTERNALS_EBPF_OFFSETS_H\n')
print('#include <stdbool.h>\n')
print(f'#define NUM_REDIRECTS {num_redirects}')
print(f'#define DEREF_END {deref_end}\n')
print('// offsets')
print('typedef struct {')
for param in params:
print(f' unsigned int {param}[NUM_REDIRECTS];')
for param in params_opt:
print(f' unsigned int {param}[NUM_REDIRECTS];')
print('} Offsets;\n')
print('#endif\n')
sys.exit(0)
if header:
print('#ifndef UNAMEOFFSETS_H')
print('#define UNAMEOFFSETS_H\n')
print('#include <sysinternalsEBPFoffsets.h>\n')
print('typedef struct {')
print(' char *uname;')
print(' Offsets offsets;')
print('} unameOffsets;\n')
print('unsigned int *findConfigItem(Offsets *o, const char *param);')
print('#endif\n')
sys.exit(0)
count = 0
print('#include <string.h>')
print('#include <stdbool.h>')
print('#include <glib-object.h>')
print('#include <json-glib/json-glib.h>')
print('#include "unameOffsets.h"')
print('#include "searchOffsets.h"\n')
print('unsigned int *findConfigItem(Offsets *o, const char *param)')
print('{')
first = True
for param in params:
output = ' '
if not first:
output += 'else '
output += f'if (strcmp(param, "{param}") == 0)\n'
output += f' return o->{param};'
print(output)
first = False
for param in params_opt:
print(f' else if (strcmp(param, "{param}") == 0)')
print(f' return o->{param};')
print(' return NULL;')
print('}\n')
print('bool copyOffsets(Offsets *offsets, JsonReader *reader)')
print('{')
print(' memset(offsets, -1, sizeof(Offsets));')
for param in params:
print(f' if (!copyOffsetsList("{param}", offsets->{param}, reader))')
print(' return false;')
for param in params_opt:
print(f' if (!copyOffsetsList("{param}", offsets->{param}, reader))')
print(f' memset(offsets->{param}, -1, sizeof(offsets->{param}));')
print(' return true;')
print('}\n')