VAGSuite.EDC15VM_checksum.tdi41v2_checksum_search C# (CSharp) Метод

tdi41v2_checksum_search() публичный Метод

public tdi41v2_checksum_search ( byte file_buffer, UInt32 file_size, bool debug ) : ChecksumResult
file_buffer byte
file_size System.UInt32
debug bool
Результат ChecksumResult
        public ChecksumResult tdi41v2_checksum_search(byte[] file_buffer, UInt32 file_size, bool debug)
        {
            bool first_pass = true;
            UInt32 chk_oldvalue, chk_value, chk_start_addr, chk_end_addr;
            //UInt32[] chk_array = new UInt32[8] { 0x10000, 0x14000, 0x4C000, 0x50000, 0x50B80, 0x58000, 0x58B80, 0x5C000, 0x60000, 0x64000, 0x68000, 0x70000, 0x70B80, 0x7C000 };
            UInt32[] chk_array = new UInt32[8] { 0x10000, 0x14000, 0x58000, 0x58B80, 0x64000, 0x70000, 0x70B80, 0x7C000 };
            UInt16 seed_a = 0, seed_b = 0;

            chk_found = 0;
            chk_fixed = 0;
            chk_match = 0;

            for (; chk_found < chk_array.Length - 1; chk_found++)
            {
                chk_start_addr = chk_array[chk_found];
                chk_end_addr = chk_array[chk_found + 1];
                // if array is empty, don't check
                //DumpChecksum("chks " + chk_start_addr.ToString("X8") + "-" + chk_end_addr.ToString("X8"), file_buffer, debug);

                if (!first_pass)
                {
                    seed_a |= 0x8631;
                    seed_b |= 0xEFCD;
                }

                if (CheckEmpty(file_buffer, chk_start_addr, chk_end_addr)) continue;

                chk_oldvalue = ((UInt32)file_buffer[chk_end_addr - 1] << 24)
                             + ((UInt32)file_buffer[chk_end_addr - 2] << 16)
                             + ((UInt32)file_buffer[chk_end_addr - 3] << 8)
                             + (UInt32)file_buffer[chk_end_addr - 4];

                chk_value = tdi41_checksum_calculate(file_buffer, chk_start_addr, chk_end_addr - 4, seed_a, seed_b);

                if (chk_oldvalue != chk_value && chk_oldvalue != 0xC3C3C3C3)
                {
                    file_buffer[chk_end_addr - 4] = Convert.ToByte(chk_value & 0x000000ff);
                    file_buffer[chk_end_addr - 3] = Convert.ToByte((chk_value >> 8) & 0x000000ff);
                    file_buffer[chk_end_addr - 2] = Convert.ToByte((chk_value >> 16) & 0x000000ff);
                    file_buffer[chk_end_addr - 1] = Convert.ToByte((chk_value >> 24) & 0x000000ff);
                    Console.WriteLine("Checksum at address " + chk_end_addr.ToString("X8") + " failed");
                    chk_fixed++;
                }
                else if (chk_oldvalue == chk_value) chk_match++;
                Console.WriteLine("Checking " + chk_start_addr.ToString("X8") + " - " + chk_end_addr.ToString("X8") + " file " + chk_oldvalue.ToString("X8") + " calc " + chk_value.ToString("X8"));
                first_pass = false;
            }

            Console.WriteLine("edc15v41_512kB_chkfixed: " + chk_fixed.ToString() + " / " + chk_found.ToString());
            Console.WriteLine("edc15v41_512kB_chkmatch: " + chk_match.ToString() + " / " + chk_found.ToString());
            if (chk_fixed == 0) return ChecksumResult.ChecksumOK;
            else if (chk_match > 3) return ChecksumResult.ChecksumFail;
            else if (chk_fixed >= 4) return ChecksumResult.ChecksumTypeError;
            return ChecksumResult.ChecksumFail;
        }

Usage Example

Пример #1
0
        private ChecksumResult CalculateEDC15VMChecksum(string filename, byte[] allBytes, bool verifyOnly, out ChecksumResultDetails result)
        {
            ChecksumResult res = new ChecksumResult();

            result = new ChecksumResultDetails();
            result.CalculationResult = ChecksumResult.ChecksumFail; // default = failed
            result.TypeResult        = ChecksumType.VAG_EDC15VM_V41;
            if (/*allBytes.Length != 0x40000 && */ allBytes.Length != 0x80000 && allBytes.Length != 0x100000)
            {
                return(res);
            }
            if (allBytes.Length >= 0x80000)
            {
                if (allBytes[0x50008] == 'V' && allBytes[0x50009] == '4' && allBytes[0x5000A] == '.' && allBytes[0x5000B] == '1')
                {
                    // checksum V4.1 rev.1
                    result.TypeResult = ChecksumType.VAG_EDC15VM_V41;
                }
                else if (allBytes[0x58008] == 'V' && allBytes[0x58009] == '4' && allBytes[0x5800A] == '.' && allBytes[0x5800B] == '1')
                {
                    // checksum V4.1 rev.2
                    result.TypeResult = ChecksumType.VAG_EDC15VM_V41V2;
                }
            }
            //allBytes = reverseEndian(allBytes);
            EDC15VM_checksum chks = new EDC15VM_checksum();

            if (result.TypeResult == ChecksumType.VAG_EDC15VM_V41)
            {
                res = chks.tdi41_checksum_search(allBytes, (uint)allBytes.Length, false);
            }
            else
            {
                res = chks.tdi41v2_checksum_search(allBytes, (uint)allBytes.Length, false);
            }
            result.NumberChecksumsTotal = chks.ChecksumsFound;
            result.NumberChecksumsFail  = chks.ChecksumsIncorrect;
            result.NumberChecksumsOk    = chks.ChecksumsFound - chks.ChecksumsIncorrect;
            if (res == ChecksumResult.ChecksumOK)
            {
                Console.WriteLine("Checksum V4.1 matched");
                result.CalculationOk = true;
            }
            else if (res == ChecksumResult.ChecksumFail)
            {
                Console.WriteLine("UpdateChecksum: Checksum failed " + filename);
                if (!verifyOnly)
                {
                    File.WriteAllBytes(filename, allBytes);
                    result.CalculationOk = true;
                    Console.WriteLine("UpdateChecksum: Checksum fixed");
                }
            }
            else if (res == ChecksumResult.ChecksumTypeError)
            {
                result.TypeResult = ChecksumType.VAG_EDC15VM_V41_2002;
                EDC15VM_checksum chks2002 = new EDC15VM_checksum();
                allBytes = File.ReadAllBytes(filename);
                ChecksumResult res2002 = chks2002.tdi41_2002_checksum_search(allBytes, (uint)allBytes.Length, false);
                result.NumberChecksumsTotal = chks2002.ChecksumsFound;
                result.NumberChecksumsFail  = chks2002.ChecksumsIncorrect;
                result.NumberChecksumsOk    = chks2002.ChecksumsFound - chks2002.ChecksumsIncorrect;

                if (res2002 == ChecksumResult.ChecksumOK)
                {
                    Console.WriteLine("Checksum 2002 matched " + filename);
                    result.CalculationOk = true;
                }
                else if (res2002 == ChecksumResult.ChecksumFail)
                {
                    Console.WriteLine("UpdateChecksum: Checksum 2002 failed " + filename);
                    if (!verifyOnly)
                    {
                        File.WriteAllBytes(filename, allBytes);
                        result.CalculationOk = true;
                        Console.WriteLine("UpdateChecksum: Checksum fixed");
                    }
                }
                else if (res2002 == ChecksumResult.ChecksumTypeError)
                {
                    // unknown checksum type
                    result.CalculationOk     = false;
                    result.CalculationResult = ChecksumResult.ChecksumTypeError;
                    result.TypeResult        = ChecksumType.Unknown;
                }
            }
            return(res);
        }
All Usage Examples Of VAGSuite.EDC15VM_checksum::tdi41v2_checksum_search