


class Get_files():
    def __init__(self):
        self.password1 = b''
    def get_password(self, password_file):
        flag1 = 0
        flag2 = 0
        self.password1 = b''
        with open(password_file) as file_object:
            lines = file_object.readlines()
            #    print(lines)
        for line in lines:
            line = line.rstrip()
            #        print(line[0:5])
            if line[0:9] == '@password':
                flag1 = 1
                continue
            if flag1 == 1:
                flag2 += 1
                #            print(bytes(line, encoding="utf8"))
                #            print(line)
                self.password1 += bytes.fromhex(line)
            if flag2 == 2:
                #            print(password1)
                break
        if len(self.password1)!=32:
            self.password1 = b''
        return self.password1
    def get_boot_vector(self, firmware_file):
        flag1 = 0
        vector1 = []
        with open(firmware_file) as file_object:
            lines = file_object.readlines()
            #    print(lines)
        for line in lines:
            flag1 += 1
            if flag1 > 1 and flag1 < 6:
                vector1.append(line)
            if flag1==9 :
                vector1.append(line)
                break
            if flag1 == 1 and line[0:5] != '@0000':
                break
        return vector1

    def get_firmware(self, firmware_file):
        buff_count = 0
        buff_count2 = 0
        buff_count3 = 0
        data_array1 = []
        data_array2 = []
        app_addr_start_str = ''
        app_addr_start = 0
        with open(firmware_file) as file_object:
            lines = file_object.readlines()
        for line in lines:
            buff_count2 = len(line)
            if line[0] != '@' and line[0] != 'q':
                if buff_count2 != 49:
                    while buff_count < 48:
                        line = line.rstrip() + ' FF\n'
                        buff_count = len(line)
            buff_count3 += 1
            buff_count = 0
            data_array1.append(line)
        buff_count = 0
        for line in data_array1:
            if line[0] == '@' and buff_count == 0:
                app_addr_start_str = line.rstrip()
                app_addr_start_str = "0x" + app_addr_start_str[1:]
                app_addr_start = int(app_addr_start_str, 16)
                app_addr_start = app_addr_start + 16
                app_addr_start_str = hex(app_addr_start)
                app_addr_start_str = '@' + app_addr_start_str[2:] + '\n'
            if buff_count == 2:
                data_array2.append(app_addr_start_str)
            buff_count += 1
            data_array2.append(line)
        # with open('test0.txt', 'w+') as file_write:
        #     for line in data_array2:
        #         file_write.write(line)

        return data_array2
    def get_app_start(self, firmware_file):
        app_addr_s = ''
        buff_count3 = 0
        for line in firmware_file:
            if line[0] == '@' and buff_count3 == 0:
                line = line.rstrip()
                app_addr_s = "0x" + line[1:]
                break
            buff_count3 += 1
        return app_addr_s
    def get_app_end(self, firmware_file):
        app_addr_e = 0
        app_addr_e_str = ''
        buff_count1 = 0
        buff_count2 = 0
        buff_count3 = 0
        for line in firmware_file:
            buff_count1 = len(line)
            if line[0] == '@':
                line = line.rstrip()
                line = "0x" + line[1:]
                app_addr_e = int(line, 16)
                buff_count2 = 0
            if line[0] != '@' and buff_count1 == 49:
                buff_count2 += 16
            if line[0] == 'q':
                app_addr_e += buff_count2
                app_addr_e = hex(app_addr_e)
                app_addr_e_str = str(app_addr_e)
        return app_addr_e_str