-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcore.py
More file actions
413 lines (356 loc) · 15.5 KB
/
core.py
File metadata and controls
413 lines (356 loc) · 15.5 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import skia
import os
from thefuzz import fuzz as tf
from rapidfuzz import fuzz as rf
from aiohttp import web
from glob import glob
from copydetect import CopyDetector
import numpy as np
import json
from jinja2 import Template
import pdfkit
import shutil
UPLOAD_FOLDER = 'uploads'
OUTPUT_FOLDER = 'output'
STATIC_FOLDER = 'static'
ALLOWED_EXTENSIONS = {'c'}
# Value for `transformation_factor`
# 0 < k <= 1: Compression factor, diluting the 40-50% and maintains for higher percentages.
# k > 1: Expansion factor, creating a higher amount of percentage values.
# k <= 0: Inverts the percentage values, NOT SUITABLE!
# Recommended ranges: 0.7 up to 0.9
# Recommended value for `least_plagiarism`: 30%
TF_FACTOR = 1
LEAST_PLG = 30.0
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# For beginners learning C, there are many features in the standard libraries that they
# might not encounter or fully appreciate until they have more experience. Below is a
# categorized list of functions, macros, and tokens from the C Standard Library that are
# less commonly used by beginners:
CItems: dict[str, list[str]] = {
"Preprocessor Macros": [
"__LINE__", "__FILE__", "__DATE__", "__TIME__", "MAX", "MIN",
"CHAR_BIT", "MB_CUR_MAX", "FLT_EPSILON", "DBL_MAX", "LDBL_MIN"
],
"Preprocessor Directives": [
"#define", "#undef", "#include",
"#if", "#elif", "#else", "#endif", "#ifdef", "#ifndef",
"#pragma", "#error", "#line"
],
"Keywords": [
"register", "extern", "static", "volatile", "restrict", "_Bool", "_Complex",
"_Imaginary", "goto", "continue", "alignas", "alignof", "_Atomic",
"thread_local", "_Noreturn", "typedef", "enum", "union", "sizeof",
"inline", "_Static_assert", "_Generic"
],
"Type-Related Features": [
"alignof", "alignas", "atomic_load", "atomic_store", "static_assert"
],
"Input/Output Functions": [
"tmpfile", "setbuf", "setvbuf", "vprintf", "vsprintf", "sprintf", "snprintf", "freopen"
],
"String and Character Functions": [
"strtok", "strxfrm", "strcoll", "memmove", "memset",
"isalnum", "ispunct", "isgraph", "strncpy", "strncat",
"memcpy", "strstr", "memchr", "memcmp", "strpbrk",
"strspn", "strcspn", "strtok_r", "strrev"
],
"Wide Characters": [
"wprintf", "fwprintf", "wcscmp", "wcslen", "wmemcpy", "iswalpha", "iswdigit"
],
"Math Functions": [
"fmod", "modf", "hypot", "lgamma", "nan", "isnan", "isinf"
],
"Complex Numbers": [
"cabs", "carg", "creal", "cimag", "cpow"
],
"Time Functions": [
"difftime", "strftime", "clock", "mktime"
],
"Memory Management": [
"aligned_alloc", "reallocarray", "bsearch", "qsort", "memset"
],
"Localization": [
"setlocale", "localeconv"
],
"Signals and Error Handling": [
"signal", "raise", "strerror", "exit", "_Exit"
],
"Multithreading": [
"thrd_create", "mtx_lock", "mtx_unlock", "cnd_wait", "cnd_signal"
],
"Windows.h": [
"SetConsoleTextAttribute", "GetStdHandle", "CONSOLE_SCREEN_BUFFER_INFO",
"FillConsoleOutputCharacter", "FillConsoleOutputAttribute", "SetConsoleCursorPosition",
"SetConsoleMode", "GetConsoleMode", "system"
],
}
# Helper function to process similarity
def process_similarity(Programs, TransformationFactor=1, LeastPlagiarism=30.0):
TotalCodePrograms = len(Programs)
Caches = []
Tokenizes = []
Scores = [
[
[
[0 for _ in range(6)] for _ in range(TotalCodePrograms - 1)
] for _ in range(TotalCodePrograms)
] for _ in range(TotalCodePrograms)
]
for program in Programs:
with open(program, 'r', encoding='utf-8') as f:
Tokenizes.append(f.readlines())
with open(program, 'r', encoding='utf-8') as f:
Caches.append("".join(f.readlines()))
for i in range(TotalCodePrograms):
for j in range(TotalCodePrograms):
for k in range(TotalCodePrograms - 1):
try:
Scores[i][j][k][0] = sum([
tf.token_set_ratio(Caches[i], Caches[j]),
rf.token_set_ratio(Caches[i], Caches[j])
]) / 2
Scores[i][j][k][1] = sum([
tf.ratio(Caches[i], Caches[j]),
rf.ratio(Caches[i], Caches[j])
]) / 2
Scores[i][j][k][2] = sum([
tf.token_sort_ratio(Caches[i], Caches[j]),
rf.token_sort_ratio(Caches[i], Caches[j])
]) / 2
Scores[i][j][k][3] = sum([
tf.QRatio(Caches[i], Caches[j]),
rf.QRatio(Caches[i], Caches[j])
]) / 2
Scores[i][j][k][4] = tf.UQRatio(Caches[i], Caches[j])
Scores[i][j][k][5] = pow(
(
(
(Scores[i][j][k][1] + Scores[i][j][k][2] +
((Scores[i][j][k][3] + Scores[i][j][k][4]) / 2)) / 3 +
Scores[i][j][k][0]
) / 2
), TransformationFactor
)
except IndexError:
continue
results = []
seen_pairs = set() # Set to track unique pairs of programs
for i in range(TotalCodePrograms):
for j in range(i + 1, TotalCodePrograms):
for k in range(TotalCodePrograms - 1):
if (i == j or j == k or i == k) and (TotalCodePrograms > 2):
continue
# Create a sorted tuple to represent the unique pair
pair = tuple(sorted([Programs[i], Programs[j]]))
if pair in seen_pairs:
continue # Skip duplicates
seen_pairs.add(pair) # Add pair to the set
results.append({
"score": Scores[i][j][k][5],
"program1": Programs[i].replace(UPLOAD_FOLDER+'/', ''),
"program2": Programs[j].replace(UPLOAD_FOLDER+'/', ''),
})
sorted_results = sorted(results, key=lambda x: x['score'], reverse=True)
return {
"total_programs": TotalCodePrograms,
"results": sorted_results
}
def process_ai_detection(request):
os.makedirs(UPLOAD_FOLDER, exist_ok=True)
Programs = glob(UPLOAD_FOLDER + '/*.c')
TotalCodePrograms: int = len(Programs)
Caches: list[str] = []
Tokenizes: list[list] = []
# Load programs and tokenize
for program in Programs:
with open(program, 'r', encoding='utf-8') as f:
Tokenizes.append(f.readlines())
with open(program, 'r', encoding='utf-8') as f:
Caches.append("".join(f.readlines()))
payload = {"programs": []}
for item in range(TotalCodePrograms):
program_data = {
"filename": Programs[item].replace(UPLOAD_FOLDER + '/', ''),
"lines": len(Tokenizes[item]),
"categories": []
}
def generate_section_data(title, key):
count_founded = 0
found_keys = set()
found_items = []
for line, code_snippet in enumerate(Tokenizes[item]):
for key_item in CItems[key]:
if key_item in code_snippet:
found_items.append({
"line": line,
"snippet": code_snippet.strip(),
"highlight": key_item
})
count_founded += 1
found_keys.add(key_item)
if count_founded > 0:
return {
"title": title,
"found_count": count_founded,
"targets": list(CItems[key]),
"found_targets": list(found_keys),
"found_items": found_items
}
return None
# Add categories
categories = [
("Preprocessor Macros", "Preprocessor Macros"),
("Preprocessor Directives", "Preprocessor Directives"),
("Keywords", "Keywords"),
("Type-Related Features", "Type-Related Features"),
("Input/Output Functions", "Input/Output Functions"),
("String and Character Functions", "String and Character Functions"),
("Wide Characters", "Wide Characters"),
("Math Functions", "Math Functions"),
("Complex Numbers", "Complex Numbers"),
("Time Functions", "Time Functions"),
("Memory Management", "Memory Management"),
("Localization", "Localization"),
("Signals and Error Handling", "Signals and Error Handling"),
("Multithreading", "Multithreading"),
("Windows.h", "Windows.h"),
]
for title, key in categories:
section_data = generate_section_data(title, key)
if section_data:
program_data["categories"].append(section_data)
payload["programs"].append(program_data)
return web.json_response(payload)
class CustomDetector(CopyDetector):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def my_html(self, output_mode="save"):
if len(self.similarity_matrix) == 0:
return
code_list = self.get_copied_code_list()
scores=self.similarity_matrix[:,:,0][self.similarity_matrix[:,:,0]!=-1]
# render template with jinja and save as html
with open("templates/report.html", encoding="utf-8") as template_fp:
template = Template(template_fp.read())
flagged = self.similarity_matrix[:,:,0] > self.conf.display_t
flagged_file_count = np.sum(np.any(flagged, axis=1))
formatted_conf = json.dumps(self.conf.to_json(), indent=4)
output = template.render(config_params=formatted_conf,
version=1.0,
test_count=len(self.test_files),
test_files=self.test_files,
compare_count=len(self.ref_files),
compare_files=self.ref_files,
flagged_file_count=flagged_file_count,
code_list=code_list)
with open('static/result.html', "w", encoding="utf-8") as report_f:
report_f.write(output)
def generate_pdf_file(pdf_file, config_params, version, test_count, compare_count, code_list):
stream = skia.FILEWStream(pdf_file)
with skia.PDF.MakeDocument(stream) as document:
# A4 page size
A4_WIDTH = 595
A4_HEIGHT = 842
# Function to draw text with wrapping
def draw_wrapped_text(canvas, text, x, y, max_width, paint, font):
lines = []
current_line = ""
words = text.split()
for word in words:
test_line = f"{current_line} {word}".strip()
if font.measureText(test_line) <= max_width:
current_line = test_line
else:
lines.append(current_line)
current_line = word
lines.append(current_line)
# Draw each line
line_height = font.getSpacing()
for line in lines:
canvas.drawString(line, x, y, font, paint)
y += line_height
# Render each page
with document.page(A4_WIDTH, A4_HEIGHT) as canvas:
# Setup font and paint
header_font = skia.Font(skia.Typeface('Arial'), 16)
text_font = skia.Font(skia.Typeface('Arial'), 12)
text_paint = skia.Paint(AntiAlias=True, Color=skia.ColorBLACK)
# Margins and positions
margin = 50
line_y = margin
# Render header
canvas.drawString("Matched Code Report", margin, line_y, header_font, text_paint)
line_y += 30
# Render configuration and version
draw_wrapped_text(
canvas,
f"Configuration Parameters: {config_params}",
margin,
line_y,
A4_WIDTH - 2 * margin,
text_paint,
text_font,
)
line_y += 20
canvas.drawString(f"Version: {version}", margin, line_y, text_font, text_paint)
line_y += 20
canvas.drawString(f"Test Files Count: {test_count}", margin, line_y, text_font, text_paint)
line_y += 20
canvas.drawString(f"Comparison Files Count: {compare_count}", margin, line_y, text_font, text_paint)
line_y += 30
# Render code list
for index, code in enumerate(code_list, start=1):
if line_y > A4_HEIGHT - margin: # Start a new page if out of space
line_y = margin
with document.page(A4_WIDTH, A4_HEIGHT) as canvas:
pass
# Render each code comparison
canvas.drawString(
f"Test File: {code[2]} ({code[0]*100:.2f}%)", margin, line_y, text_font, text_paint
)
line_y += 20
canvas.drawString(
f"Reference File: {code[3]} ({code[1]*100:.2f}%)", margin, line_y, text_font, text_paint
)
line_y += 20
canvas.drawString(f"Token Overlap: {code[6]}", margin, line_y, text_font, text_paint)
line_y += 20
# Matched code columns
canvas.drawString("Matched Code (Test):", margin, line_y, text_font, text_paint)
canvas.drawString("Matched Code (Reference):", A4_WIDTH // 2, line_y, text_font, text_paint)
line_y += 20
draw_wrapped_text(
canvas, code[4], margin, line_y, (A4_WIDTH // 2) - margin, text_paint, text_font
)
draw_wrapped_text(
canvas, code[5], A4_WIDTH // 2, line_y, (A4_WIDTH // 2) - margin, text_paint, text_font
)
line_y += 50
print(f"PDF saved to {pdf_file}")
def compare(program1, program2):
pdf_path = 'static' + '/result.pdf'
html_path = 'static' + '/result.html'
if os.path.exists(pdf_path):
os.remove(pdf_path)
if os.path.exists(html_path):
os.remove(html_path)
os.makedirs(OUTPUT_FOLDER, exist_ok=True)
# Remove any existing .c files in the OUTPUT_FOLDER
for file in glob(OUTPUT_FOLDER + '/*.c'):
os.remove(file)
program1_path = UPLOAD_FOLDER + '/' + program1
program2_path = UPLOAD_FOLDER + '/' + program2
os.system(f'cp "{program1_path}" "{OUTPUT_FOLDER}"')
os.system(f'cp "{program2_path}" "{OUTPUT_FOLDER}"')
# Initialize the detector
detector = CustomDetector(test_dirs=[OUTPUT_FOLDER], extensions=['c'], out_file=html_path, autoopen=False)
detector.run()
detector.my_html()
# code_list = detector.get_copied_code_list()
# formatted_conf = json.dumps(detector.conf.to_json(), indent=4)
# generate_pdf_file('static/result.pdf', formatted_conf, 1.0, len(detector.test_files), len(detector.ref_files), code_list)
pdfkit.from_file(html_path, pdf_path)
if os.path.exists(OUTPUT_FOLDER):
shutil.rmtree(OUTPUT_FOLDER)