import sys
import binascii

# Fonction pour extraire le MCUID à partir d'une position donnée dans le fichier
def extract_mcuid_at_position(file_path, position, mcuid_length=8):
    with open(file_path, 'rb') as file:
        file.seek(position)
        mcuid_bytes = file.read(mcuid_length)
    mcuid_hex = binascii.hexlify(mcuid_bytes).decode('utf-8').lower()
    return mcuid_hex

# Fonction pour calculer les positions de modification en fonction des valeurs du MCUID
def calculate_modification_positions(mcuid_values, base_positions):
    calculated_positions = []
    for base_pos in base_positions:
        for value in mcuid_values:
            calculated_positions.append(base_pos + value)
    return calculated_positions

# Fonction pour vérifier les modifications par rapport aux positions calculées
def check_modifications(input_file, calculated_positions, modifications):
    with open(input_file, 'rb') as file:
        file_content = file.read()
    
    results = []
    for pos in calculated_positions:
        if pos < len(file_content):
            actual_value = file_content[pos]
            expected_value = modifications.get(pos)
            if expected_value is not None:
                results.append((pos, actual_value, expected_value, actual_value == expected_value))
            else:
                results.append((pos, actual_value, None, False))
        else:
            results.append((pos, None, None, False))
    
    return results

# Fonction principale pour extraire le MCUID et calculer les positions de modification
def main(input_file, mcuid_position, base_positions, modifications):
    # Extraire le MCUID
    mcuid_hex = extract_mcuid_at_position(input_file, mcuid_position)
    mcuid_values = [int(byte) for byte in bytes.fromhex(mcuid_hex)]
    
    # Calculer les positions des modifications
    calculated_positions = calculate_modification_positions(mcuid_values, base_positions)
    
    # Vérifier les modifications
    modifications_dict = {pos: val for pos, val in modifications}
    results = check_modifications(input_file, calculated_positions, modifications_dict)
    
    print("Vérification des modifications aux positions calculées :")
    for pos, actual_value, expected_value, matches in results:
        print(f"Position: {pos}, Valeur actuelle: {actual_value}, Valeur attendue: {expected_value}, Correspondance: {matches}")

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("Usage: python3 calculate_positions.py <input_file>")
        sys.exit(1)

    input_file = sys.argv[1]

    # Position du MCUID dans le fichier
    mcuid_position = 82432
    
    # Positions de base pour les modifications
    base_positions = [
        131908, 131909, 131910, 131911,
        262740, 262741, 262742, 262743,
        1395070, 1395071,
        1395072, 1395073, 1395074, 1395075,
        1399726, 1399727,
        1571584, 1571585, 1571586, 1571588,
        1571589, 1571590, 1571591, 1571592,
        1571593, 1571594, 1571595, 1571596,
        1571597, 1571598, 1571599, 1571600,
        1571601, 1571602, 1571603, 1571604,
        1571605, 1571606, 1571607, 1571608,
        1571609, 1571610, 1571611, 1571612,
        1571613, 1571614, 1571615, 1571616,
        1571617, 1571619
    ]

    # Modifications attendues
    modifications = [
        (131908, 0xae),
        (131909, 0x3a),
        (131910, 0x23),
        (131911, 0xbb),
        (262740, 0x9d),
        (262741, 0x8d),
        (262742, 0x82),
        (262743, 0x4b),
        (1395070, 0x6d),
        (1395071, 0x01),
        (1395072, 0xc1),
        (1395073, 0x58),
        (1395074, 0x00),
        (1395075, 0x90),
        (1399726, 0x00),
        (1399727, 0x90),
        (1571584, 0x85),
        (1571585, 0xd2),
        (1571586, 0x04),
        (1571588, 0x7b),
        (1571589, 0x50),
        (1571590, 0xb7),
        (1571591, 0xfa),
        (1571592, 0x1b),
        (1571593, 0xff),
        (1571594, 0xfb),
        (1571595, 0xfe),
        (1571596, 0xc6),
        (1571597, 0xf2),
        (1571598, 0x7b),
        (1571599, 0x90),
        (1571600, 0x3c),
        (1571601, 0xfa),
        (1571602, 0x1b),
        (1571603, 0x8f),
        (1571604, 0xac),
        (1571605, 0xf8),
        (1571606, 0xc6),
        (1571607, 0xf2),
        (1571608, 0x7b),
        (1571609, 0x20),
        (1571610, 0xd3),
        (1571611, 0xf6),
        (1571612, 0x1b),
        (1571613, 0x0f),
        (1571614, 0x5a),
        (1571615, 0xf6),
        (1571616, 0xc6),
        (1571617, 0xf2),
        (1571619, 0x90),
    ]

    main(input_file, mcuid_position, base_positions, modifications)
