Code Obfuscate

This is a simple python script to obfuscate all the.c files in a folder
Author:MY

Usage:
Obfuscation (remove comments manually)
python Obfuscate.py "c:\souce_folder_path"

Obfuscation (automatic removal of comments)
python Obfuscate.py "c:\souce_folder_path" -r

Help document
python Obfuscate.py -h

Note:
1.The first character in a String cannot be a space.
For example :" Hello!". You need to change it to "Hello!" "Or "-Hello!".

2.I recommend that you remove comments manually, because automatically removing comments automatically removes all text on the line following //
This can lead to the error of removing anything after the // path symbol, or after https://

# This is a simple python script to obfuscate all the.c files in a folder
# Author:MY
# BUG Report: Discord:https://discord.gg/BqgkQ3wxZr

import os
import random
import re
import string
import argparse

def generate_random_word():
    letters = string.ascii_uppercase
    numbers = string.digits
    random_char = random.choice(letters)
    random_chars = [random_char] + [random.choice(letters + numbers) for _ in range(9)]
    return "".join(random_chars)

def generate_random_ifdef_else(string_part):
    random_word1 = generate_random_word()
    random_word2 = generate_random_word()
    return f'#ifdef {random_word1} {random_word2} #else {string_part} #endif'

def generate_random_ifndef_else(string_part):
    random_word1 = generate_random_word()
    random_word2 = generate_random_word()
    return f'#ifndef {random_word1} {string_part} #else {random_word2} #endif'

def replace_line(line):
    if any(keyword in line for keyword in ['#ifdef', '#ifndef', '#else', '#endif', '#define']):
        return line
    parts = line.split()
    new_parts = []
    for part in parts:
        if random.random() < 0.5:
            new_part = generate_random_ifdef_else(part)
        else:
            new_part = generate_random_ifndef_else(part)
        new_parts.append(new_part)
    return " ".join(new_parts)

def process_file(file_path, remove_comments_flag):
    with open(file_path, 'r') as input_file:
        original_code = input_file.read()

    if remove_comments_flag:
        code_no_comments = remove_comments(original_code)
    else:
        code_no_comments = original_code

    lines = code_no_comments.split('\n')
    confused_code_lines = []
    for line in lines:
        confused_line = replace_line(line.strip())
        confused_code_lines.append(confused_line)

    confused_code = "\n".join(confused_code_lines)

    with open(file_path, 'w') as output_file:
        output_file.write(confused_code)

def remove_comments(code):
    code = re.sub(r'//.*', '', code)
    code = re.sub(r'/\*.*?\*/', '', code, flags=re.DOTALL)
    return code

def process_folder(folder_path, remove_comments_flag):
    for root, dirs, files in os.walk(folder_path):
        for file_name in files:
            if file_name.endswith('.c'):
                file_path = os.path.join(root, file_name)
                process_file(file_path, remove_comments_flag)

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Confuse C files in a folder.')
    parser.add_argument('folder_path', type=str, help='Path to the folder containing C files.')
    parser.add_argument('-r', action='store_true', help='Remove comments if set.')
    args = parser.parse_args()

    process_folder(args.folder_path, args.r)