<commit_before>"""
Realtek Semiconductor Corp.

RTL8195A elf2bin script
"""

import sys, array, struct, os, re, subprocess
import hashlib
import shutil
import time
import binascii
import elftools

from tools.paths import TOOLS_BOOTLOADERS
from tools.toolchains import TOOLCHAIN_PATHS

# Constant Variables
TAG = 0x81950001
VER = 0x81950001
CAMPAIGN = binascii.hexlify('FFFFFFFFFFFFFFFF')

RAM2_HEADER = {
    'tag': 0,
    'ver': 0,
    'timestamp': 0,
    'size': 72,
    'hash': 'FF',
    'campaign': 'FF',
    'crc32': 0xFFFFFFFF,
}

def format_number(number, width):
    # convert to string
    line = format(number, '0%dx' % (width))
    if len(line) > width:
        print "[ERROR] 0x%s cannot fit in width %d" % (line, width)
        sys.exit(-1)
    # cut string to list & reverse
    line = [line[i:i+2] for i in range(0, len(line), 2)]
    line.reverse()
    return binascii.a2b_hex("".join(line))

def format_string(string):
    return binascii.a2b_hex(string)

def write_number(value, width, output):
    output.write(format_number(value, width))

def write_string(value, width, output):
    output.write(format_string(value))

def append_image_file(image, output):
    input = open(image, "rb")
    output.write(input.read())
    input.close()

def write_padding_bytes(output_name, size):
    current_size = os.stat(output_name).st_size
    padcount = size - current_size
    if padcount < 0:
        print "[ERROR] image is larger than expected size"
        sys.exit(-1)
    output = open(output_name, "ab")
    output.write('\377' * padcount)
    output.close()

def crc32_checksum(string):
    return binascii.crc32(string) & 0xFFFFFFFF

def sha256_checksum(filename, block_size=65536):
    sha256 = hashlib.sha256()
    with open(filename, 'rb') as f:
        for block in iter(lambda: f.read(block_size), b''):
            sha256.update(block)
    return sha256.hexdigest()

def epoch_timestamp():
    epoch = int(time.time())
    return epoch

def find_symbol(toolchain, mapfile, symbol):
    ret = None

    HEX = '0x0{,8}(?P<addr>[0-9A-Fa-f]{8})'
    if toolchain == "GCC_ARM":
        SYM = re.compile(r'^\s+' + HEX + r'\s+' + symbol + '\r?$')
    elif toolchain in ["ARM_STD", "ARM", "ARM_MICRO"]:
        SYM = re.compile(r'^\s+' + HEX + r'\s+0x[0-9A-Fa-f]{8}\s+Code.*\s+i\.' + symbol + r'\s+.*$')
    elif toolchain == "IAR":
        SYM = re.compile(r'^' + symbol + r'\s+' + HEX + '\s+.*$')

    with open(mapfile, 'r') as infile:
        for line in infile:
            match = re.match(SYM, line)<commit_msg>RealTek post script fix for ARMC6<commit_after>"""
Realtek Semiconductor Corp.

RTL8195A elf2bin script
"""

import sys, array, struct, os, re, subprocess
import hashlib
import shutil
import time
import binascii
import elftools

from tools.paths import TOOLS_BOOTLOADERS
from tools.toolchains import TOOLCHAIN_PATHS

# Constant Variables
TAG = 0x81950001
VER = 0x81950001
CAMPAIGN = binascii.hexlify('FFFFFFFFFFFFFFFF')

RAM2_HEADER = {
    'tag': 0,
    'ver': 0,
    'timestamp': 0,
    'size': 72,
    'hash': 'FF',
    'campaign': 'FF',
    'crc32': 0xFFFFFFFF,
}

def format_number(number, width):
    # convert to string
    line = format(number, '0%dx' % (width))
    if len(line) > width:
        print "[ERROR] 0x%s cannot fit in width %d" % (line, width)
        sys.exit(-1)
    # cut string to list & reverse
    line = [line[i:i+2] for i in range(0, len(line), 2)]
    line.reverse()
    return binascii.a2b_hex("".join(line))

def format_string(string):
    return binascii.a2b_hex(string)

def write_number(value, width, output):
    output.write(format_number(value, width))

def write_string(value, width, output):
    output.write(format_string(value))

def append_image_file(image, output):
    input = open(image, "rb")
    output.write(input.read())
    input.close()

def write_padding_bytes(output_name, size):
    current_size = os.stat(output_name).st_size
    padcount = size - current_size
    if padcount < 0:
        print "[ERROR] image is larger than expected size"
        sys.exit(-1)
    output = open(output_name, "ab")
    output.write('\377' * padcount)
    output.close()

def crc32_checksum(string):
    return binascii.crc32(string) & 0xFFFFFFFF

def sha256_checksum(filename, block_size=65536):
    sha256 = hashlib.sha256()
    with open(filename, 'rb') as f:
        for block in iter(lambda: f.read(block_size), b''):
            sha256.update(block)
    return sha256.hexdigest()

def epoch_timestamp():
    epoch = int(time.time())
    return epoch

def find_symbol(toolchain, mapfile, symbol):
    ret = None

    HEX = '0x0{,8}(?P<addr>[0-9A-Fa-f]{8})'
    if toolchain == "GCC_ARM":
        SYM = re.compile(r'^\s+' + HEX + r'\s+' + symbol + '\r?$')
    elif toolchain in ["ARM_STD", "ARM", "ARM_MICRO", "ARMC6"]:
        SYM = re.compile(r'^\s+' + HEX + r'\s+0x[0-9A-Fa-f]{8}\s+Code.*\s+i\.' + symbol + r'\s+.*$')
    elif toolchain == "IAR":
        SYM = re.compile(r'^' + symbol + r'\s+' + HEX + '\s+.*$')

    with open(mapfile, 'r') as infile:
        for line in infile:
            match = re.match(SYM, line)
{'fraction_non_alphanumeric': 0.15343915343915343, 'fraction_numerical': 0.031746031746031744, 'mean_word_length': 3.3144547759932377, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5918167', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1865, 'n_words': 455}