Skip to content

Instantly share code, notes, and snippets.

@MiSERYYYYY
Forked from OtterHacker/bin2c.py
Created October 30, 2022 00:04
Show Gist options
  • Select an option

  • Save MiSERYYYYY/096240cda0908ed8aa1b44b3b02a05e9 to your computer and use it in GitHub Desktop.

Select an option

Save MiSERYYYYY/096240cda0908ed8aa1b44b3b02a05e9 to your computer and use it in GitHub Desktop.

Revisions

  1. @OtterHacker OtterHacker revised this gist Oct 29, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bin2c.py
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,7 @@ def bin2c(filename, output_filename):
    l = 1
    m = 1
    for i in range(k):
    file.write('CopyMemory(&sc[{}], sc_{}, 16);\n'.format(i*16, i))
    file.write('\tCopyMemory(&sc[{}], sc_{}, 16);\n'.format(i*16, i))
    # file.write('\tfor (int i = 0; i < 16; i++) {sc[' + format(i*16) +' + i] = sc_' + format(i) + '[i];}\n')
    if(l % 200 == 0):
    file.write("}\nvoid buildsc_" + format(m) + "(){\n")
  2. @OtterHacker OtterHacker revised this gist Oct 29, 2022. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion bin2c.py
    Original file line number Diff line number Diff line change
    @@ -38,7 +38,8 @@ def bin2c(filename, output_filename):
    l = 1
    m = 1
    for i in range(k):
    file.write('\tfor (int i = 0; i < 16; i++) {sc[' + format(i*16) +' + i] = sc_' + format(i) + '[i];}\n')
    file.write('CopyMemory(&sc[{}], sc_{}, 16);\n'.format(i*16, i))
    # file.write('\tfor (int i = 0; i < 16; i++) {sc[' + format(i*16) +' + i] = sc_' + format(i) + '[i];}\n')
    if(l % 200 == 0):
    file.write("}\nvoid buildsc_" + format(m) + "(){\n")
    m += 1
  3. @OtterHacker OtterHacker revised this gist Oct 29, 2022. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion bin2c.py
    Original file line number Diff line number Diff line change
    @@ -16,7 +16,7 @@ def bin2c(filename, output_filename):
    file = open(output_filename, 'w')
    i = 1
    k = 1
    file.write('#define _CRT_SECURE_NO_WARNINGS\n#pragma once\n#include <string.h>\n\n')
    file.write('#define _CRT_SECURE_NO_WARNINGS\n\n')
    file.write('char sc_0[16] = {')
    for j in range(len(binary)):
    elt = binary[j]
  4. @OtterHacker OtterHacker created this gist Oct 29, 2022.
    52 changes: 52 additions & 0 deletions bin2c.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    """
    Transform a binary file into a C header file.
    The binary file is splitted into 16 char strings and rebuild at execution time.
    The function buildsc() must be called in your main to rebuild the binary file into the sc C variable.
    The length is set in the sc_length variable.
    Be carefull, try to avoid compiler code optimization as it will remove all these modifications in the final binary.
    """

    def bin2c(filename, output_filename):
    file = open(filename, 'rb')
    binary = file.read()
    file.close()

    file = open(output_filename, 'w')
    i = 1
    k = 1
    file.write('#define _CRT_SECURE_NO_WARNINGS\n#pragma once\n#include <string.h>\n\n')
    file.write('char sc_0[16] = {')
    for j in range(len(binary)):
    elt = binary[j]
    file.write('{}'.format(hex(elt)))
    if(i%16 == 0):
    file.write('};\nconst char sc_' + format(i//16) + '[16] = {')
    k += 1
    elif j != len(binary) - 1:
    file.write(',')
    i += 1

    if(i%16 != 1):
    file.write('};\n')

    file.write('\nchar sc[{}];\n'.format(len(binary)))
    file.write('int sc_length = {};\n'.format(len(binary)))

    file.write("void buildsc_0(){\n")
    l = 1
    m = 1
    for i in range(k):
    file.write('\tfor (int i = 0; i < 16; i++) {sc[' + format(i*16) +' + i] = sc_' + format(i) + '[i];}\n')
    if(l % 200 == 0):
    file.write("}\nvoid buildsc_" + format(m) + "(){\n")
    m += 1
    l += 1
    file.write("}\n")

    file.write("void buildsc(){\n")
    for i in range(m):
    file.write("\tbuildsc_{}();\n".format(i))
    file.write("}")
    file.close()