Ejemplo n.º 1
0
        private void button_CalcCrcStream_Click(object sender, EventArgs e)
        {
            if (null == VBFFilePath)
            {
                MessageBox.Show("Error: 未找到VBF文件");

                return;
            }

            byte[]   buf;
            CAN_File canFile = new CAN_File();

            canFile.name        = new byte[200];
            canFile.block_array = new CAN_Block[10];
            canFile.block_array[0].flash_addr = new byte[4];
            canFile.block_array[0].size       = new byte[4];
            canFile.block_array[0].checksum   = new byte[2];
            canFile.erase_size = new byte[4];
            canFile.crc32      = new byte[4];

            myVBF.ReadFile(VBFFilePath, out buf);
            string strLog;

            myVBF.nswCANGetVbfInfo(1, buf, buf.Length, canFile, out strLog);

            textBox_CrcResult.Text       = String.Format("0x{0:X}", myVBF.Crc32);
            textBox_CrcResultStream.Text = String.Format("0x{0:X}", myVBF.Crc32Stream);

            richTextBox_Log.AppendText(strLog);
        }
Ejemplo n.º 2
0
        /*!
         *  @brief description for nswCANGetVbfInfo()
         *  @param[in]     None
         *  @param[out]    None
         *  @return        None
         *  @author        Name
         *  @Date          20180108
         */
        public int nswCANGetVbfInfo(byte type, byte[] data, int data_len, CAN_File f, out string strLog)
        {
            int ret        = -1;
            int chk_offset = 0;
            int i          = 0;
            int crc_offset = 0;
            int crc_size   = 0;
            int erase_size = 0;

            strLog = String.Format("Get vbf info,datalen=[0x{0:X}]\n", data_len);

            try
            {
                string strData = System.Text.Encoding.Default.GetString(data);
                chk_offset = strData.IndexOf("file_checksum");

                if (data_len < chk_offset)
                {
                    strLog += String.Format("data exceed![0x{0:X}][0x{1:X}]\n", data_len, chk_offset);

                    return(ret);
                }


                // 1st block
                for (i = chk_offset; i < data_len; i++)
                {
                    if (data[i] == '}')
                    {
                        strLog += String.Format("find file_checksum @[0x{0:X}],find }} @[0x{1:X}],block 0 flash addr & block size & data0-1:\n", chk_offset, i);

                        i = i + 0x01;
                        //NEU_LOG_BUFF(&data[i], 10);

                        f.block_count = 0x01;

                        f.block_array[0].type = type;

                        Array.Copy(data, i, f.block_array[0].flash_addr, 0, 4);
                        i = i + 4;

                        Array.Copy(data, i, f.block_array[0].size, 0, 4);
                        i = i + 4;

                        //memcpy(&block->checksum[0], &data[i], BLOCK_CHECKSUM_SIZE);
                        //i = i + BLOCK_CHECKSUM_SIZE;

                        f.block_array[0].start_offset = i;
                        int blocksize = ((((int)(f.block_array[0].size)[0]) * 256 * 256 * 256) + (((int)(f.block_array[0].size)[1]) * 256 * 256) + (((int)(f.block_array[0].size)[2]) * 256) + (f.block_array[0].size)[3]);
                        i = i + blocksize;
                        f.block_array[0].end_offset = i - 0x01;
                        if (i > (data_len - 0x02))
                        {
                            strLog += String.Format("i error!=[0x{0:X}],block size=[0x{1:X}],start offset=[0x{2:X}],end offset=[0x{3:X}]\n", i, blocksize, f.block_array[0].start_offset, f.block_array[0].end_offset);

                            i = (data_len - 0x02);
                        }

                        Array.Copy(data, i, f.block_array[0].checksum, 0, 2);
                        i = i + 2;

                        if (f.block_array[0].end_offset > (data_len - 0x01))
                        {
                            strLog += String.Format("end offset err=[0x{0:X}],datalen=[0x{1:X}],i=[0x{2:X}]\n", f.block_array[0].end_offset, data_len, i);

                            f.block_array[0].end_offset = (data_len - 0x01);
                        }

                        ret = 0;

                        chk_offset = f.block_array[0].end_offset + 4 + 4 + 2 + 0x01;

                        break;
                    }
                }

                if (0 != ret)
                {
                    strLog += String.Format("find file_checksum @[0x{0:X}], can NOT find },end @[0x{1:X}]\n", chk_offset, i);

                    return(ret);
                }

                if (chk_offset >= data_len)
                {
                    strLog += String.Format("no more block,next block data @[0x{0:X}],but totalsize=[0x{1:X}]\n", chk_offset, data_len);
                }
                else
                {
                    for (i = (f.block_array[f.block_count - 0x01].end_offset + 0x01 + 2); i < data_len;)
                    {
                        strLog += String.Format("i=[0x{0:X}],block %d flash addr & block size & data0-1:\n", i, f.block_count);
                        //NEU_LOG_BUFF(&data[i], 10);

                        f.block_array[f.block_count].type = type;
                        Array.Copy(data, i, f.block_array[f.block_count].flash_addr, 0, 4);
                        i = i + 4;

                        Array.Copy(data, i, f.block_array[f.block_count].size, 0, 4);
                        i = i + 4;

                        f.block_array[f.block_count].start_offset = i;

                        int blocksize = ((((int)(f.block_array[f.block_count].size)[0]) * 256 * 256 * 256) + (((int)(f.block_array[f.block_count].size)[1]) * 256 * 256) + (((int)(f.block_array[f.block_count].size)[2]) * 256) + (f.block_array[f.block_count].size)[3]);
                        i = i + blocksize;

                        f.block_array[f.block_count].end_offset = i - 0x01;

                        if (i > (data_len - 0x02))
                        {
                            strLog += String.Format("i error!=[0x{0:X}],block size=[0x{1:X}],start offset=[0x{2:X}],end offset=[0x{3:X}]\n", i, blocksize, f.block_array[f.block_count].start_offset, f.block_array[f.block_count].end_offset);

                            i = (data_len - 0x02);
                        }
                        Array.Copy(data, i, f.block_array[f.block_count].checksum, 0, 2);
                        i = i + 2;

                        f.block_count++;
                        if (f.block_count >= 100)
                        {
                            f.block_count = 100;

                            break;
                        }
                    }
                }

                f.block_array[f.block_count - 0x01].is_last = 0x01;
                strLog += String.Format("block=[%d] is the last block\n", f.block_count - 0x01);


                // calc crc32
                Crc32 = 0xFFFFFFFF;
                //memset(&nubCANCrcDataBuf[0], 0, sizeof(nubCANCrcDataBuf));
                nubCANCrcDataSize = 0;
                nubCANCrcDataBuf  = new byte[data.Length];
                for (i = 0; i < f.block_count; i++)
                {
                    crc_offset = f.block_array[i].start_offset;
                    if (crc_offset > data_len - 1)
                    {
                        strLog    += String.Format("crc offset error=[0x{0:X}]\n", crc_offset);
                        crc_offset = data_len - 1;
                    }
                    crc_size = ((((int)(f.block_array[i].size)[0]) * 256 * 256 * 256) + (((int)(f.block_array[i].size)[1]) * 256 * 256) + (((int)(f.block_array[i].size)[2]) * 256) + (f.block_array[i].size)[3]);
                    if ((nubCANCrcDataSize + crc_size) > data.Length)
                    {
                        strLog += String.Format("crc size error=[0x{0:X}],[0x{1:X}]\n", crc_size, nubCANCrcDataSize);

                        break;
                    }

                    strLog += String.Format("crc offset=[0x{0:X}],size=[0x{1:X}],\n", crc_offset, crc_size);
                    Array.Copy(data, crc_offset, nubCANCrcDataBuf, nubCANCrcDataSize, crc_size);
                    nubCANCrcDataSize = nubCANCrcDataSize + crc_size;
                }

                string str;
                nuwCANCalcChecksum(nubCANCrcDataBuf, nubCANCrcDataSize, out Crc32, out str);
                strLog += str;

                (f.crc32)[0] = (byte)((Crc32) / 256 / 256 / 256);
                (f.crc32)[1] = (byte)((Crc32) / 256 / 256);
                (f.crc32)[2] = (byte)((Crc32) / 256);
                (f.crc32)[3] = (byte)((Crc32) & 0xFF);

                // erase size
                //if (f.block_count > 0) {
                //    erase_size = FLIPW4(f->block_array[f->block_count - 1].flash_addr);
                //    if (erase_size >= FLIPW4(f->block_array[0].flash_addr)) {
                //        erase_size = erase_size - FLIPW4(f->block_array[0].flash_addr);
                //        erase_size = erase_size + FLIPW4(f->block_array[f->block_count - 1].size);
                //    }
                //    else {
                //        erase_size = 0;
                //        CAN_LOG_CRIT("erase size error!\n");
                //    }

                //    FLOPW4(f->erase_size, erase_size);
                //}

                // calc crc32 in stream
                strLog           += "calculating crc in stream......";
                Crc32Stream       = 0xFFFFFFFF;
                nubCANCrcDataSize = 0;
                string str2;
                UInt32 Crc32In = 0xFFFFFFFF;

                for (i = 0; i < f.block_count; i++)
                {
                    crc_offset = f.block_array[i].start_offset;
                    if (crc_offset > data_len - 1)
                    {
                        strLog    += String.Format("crc offset error=[0x{0:X}]\n", crc_offset);
                        crc_offset = data_len - 1;
                    }
                    crc_size = ((((int)(f.block_array[i].size)[0]) * 256 * 256 * 256) + (((int)(f.block_array[i].size)[1]) * 256 * 256) + (((int)(f.block_array[i].size)[2]) * 256) + (f.block_array[i].size)[3]);
                    if ((nubCANCrcDataSize + crc_size) > data.Length)
                    {
                        strLog += String.Format("crc size error=[0x{0:X}],[0x{1:X}]\n", crc_size, nubCANCrcDataSize);

                        break;
                    }

                    strLog += String.Format("crc offset=[0x{0:X}],size=[0x{1:X}],\n", crc_offset, crc_size);
                    byte[] buf = new byte[crc_size];
                    Array.Copy(data, crc_offset, buf, 0, crc_size);
                    nubCANCrcDataSize = nubCANCrcDataSize + crc_size;
                    nuwCANCalcChecksum(Crc32In, buf, buf.Length, out Crc32Stream, out str2);
                    Crc32In = Crc32Stream;
                    strLog += str2;
                }

                UInt32 crctemp = Crc32Stream;
                Crc32Stream = Crc32Stream ^ 0xFFFFFFFF;

                strLog += String.Format("before xor=[0x{0:X}],after xor=[0x{1:X}]\n", crctemp, Crc32Stream);
            }
            catch (Exception ex)
            {
                string err = ex.ToString();
                strLog += err;
                //throw;
            }

            return(ret);
        }