forked from softwaresecured/FaceBroke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
smapper.py
161 lines (130 loc) · 5.12 KB
/
smapper.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/bin/env python3
import os
import sys
import re
from pathlib import Path
SEP = "------------------------------------------------------------------"
# Retrieve list of complete absolute paths of files given extension, ignoring
# out directory
def getFilesofType(rootPath, extension):
results = list()
jsps = sorted(Path(sys.argv[1]).rglob("*"+extension))
for j in jsps:
results.append(j.resolve())
return results
# Now have two dicts, one of JSP names and paths and another
# of smap names and paths
# for name in smaps.keys():
# with smaps[name].open() as file:
# for line in file:
# print(line.strip())
def load_jsp(filename, jsps):
result = list()
with jsps[filename].open() as file:
for line in file:
if ".jsp" in line and "include" in line:
result.append((list(), ""))
matches = re.search(r"[\'\"](.*.jsp)", line)
if matches:
chunk = load_jsp(matches.group(1), jsps)
result = result + chunk
else:
result.append((list(), line.strip()))
return result
def printVirtFile(virtFile):
lineCount = 0
for (index, line) in enumerate(virtFile):
if len(line[0]) > lineCount:
lineCount = len(line[0])
print(lineCount)
print(" JAVA | JSP | Line")
print(SEP)
for (index, line) in enumerate(virtFile):
print((" {:>"+str(4*lineCount)+"s} : {:4d} : {}").format(",".join(line[0]), index+1, line[1]))
def load_smap_jsp(jsp_name, virt_jsp, smaps, jsps):
pattern = r''
for piece in jsp_name.split("."):
if "_" in piece:
for chunk in piece.split("_"):
pattern += chunk + ".*"
else:
pattern += piece + ".*"
for name in smaps:
if re.search(pattern, name):
print("JSP: {} SMAP: {}".format(jsp_name, name))
with smaps[name].open() as smap:
files = list()
context = 0
for line in smap:
if "+" in line:
chunks = line.split()
num_lines = 0
with jsps[chunks[2]].open() as file:
for line in file:
num_lines += 1
files.append(num_lines)
elif ":" in line:
input_start_line = None
line_file_id = None
repeat_count = None
output_start_line = None
output_line_increment = None
chunks = line.split(":")
jsp_piece = chunks[0].split(",")
java_piece = chunks[1].split(",")
if "#" in jsp_piece[0]:
input_start_line = int(jsp_piece[0].split("#")[0])
line_file_id = int(jsp_piece[0].split("#")[1])
else:
input_start_line = int(jsp_piece[0])
if len(jsp_piece) > 1:
repeat_count = int(jsp_piece[1])
output_start_line = int(java_piece[0])
if len(java_piece) > 1:
output_line_increment = int(java_piece[1])
if line_file_id is not None:
context = line_file_id
if repeat_count is None:
repeat_count = 1
if output_line_increment is None:
output_line_increment = 1
offset = 0
for x in range(0, context):
offset += files[x]
index = input_start_line + offset - 1
out = output_start_line
#print("{}#{},{}:{},{} --> {},{},{}".format(input_start_line, line_file_id, repeat_count, output_start_line, output_line_increment, offset, index, out))
if index >= len(virt_jsp):
print("Invalid index: "+str(index))
continue
virt_jsp[index][0].append(str(index))
break
return virt_jsp
def main():
root = sys.argv[1]
# Get JSPs
jsps = getFilesofType(root, ".jsp")
# Only want JSPs not in target/
tmp = dict()
for j in jsps:
if "target/" not in str(j):
tmp[str(j.name)] = j
jsps = tmp
# Get our smap files
smaps = getFilesofType(root, ".smap")
tmp = dict()
for f in smaps:
tmp[str(f.name)] = f
smaps = tmp
# Now have two dicts, one of JSP names and paths and another
# of smap names and paths
for jsp_name in ["wall.jsp"]:
print(SEP)
print("File: {}".format(jsp_name))
print(SEP)
virt_jsp = load_jsp(jsp_name, jsps)
mapped_virt_jsp = load_smap_jsp(jsp_name, virt_jsp, smaps, jsps)
printVirtFile(mapped_virt_jsp)
print("\n\n")
if __name__ == '__main__':
main()