#!/usr/bin/python3
# -*- coding: utf-8 -*-
# 2025/10/22
# Author: DarkRanger <darkranger.no-ip.org>
import os, sys
import io

def bin2c():
    if len(sys.argv) != 4:
        return False
    if not os.path.isfile(sys.argv[1]):
        return False
    
    input_file = open(sys.argv[1], "rb")
    output_file = open(sys.argv[2], "w")
    symbol = sys.argv[3]
    input_file.seek(0, io.SEEK_END)
    sz = input_file.tell()
    input_file.seek(0, io.SEEK_SET)
    output_file.write("static size_t size_{0} = {1};\n".format(symbol, sz))
    output_file.write("static unsigned char {0}[] = {{\n".format(symbol))
    n = 0

    while True:
        ch = input_file.read(1)

        if not ch:
            break
        
        if (n & 0xf) == 0xf:
            output_file.write("\n")
        
        output_file.write(" 0x{0},".format(ch.hex()))
        n = n + 1
    
    output_file.write("\n};\n")
    input_file.close()
    output_file.close()
    return True

if __name__ == "__main__":
    appdir = os.path.abspath(os.path.dirname(sys.argv[0]))
    os.chdir(appdir)
    bin2c()
