LCOV - code coverage report
Current view: top level - legacy/ascend950/common - checkcrc.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 95.8 % 72 69
Test Date: 2026-08-04 10:52:23 Functions: 90.9 % 11 10

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "checkcrc.h"
      12              : #include "log.h"
      13              : #include <string>
      14              : #include <cstdlib>
      15              : #include <iostream>
      16              : #include <ios>
      17              : #include <fstream>
      18              : #include "const_val.h"
      19              : 
      20              : namespace Hccl {
      21              : constexpr s32 FILE_MAX_LENGTH = 40 * 1024 * 1024; // file max length 40*1024*1024=40M.
      22           38 : CheckCrc::CheckCrc() : initFlag_(0), crcCalcTable{0}, crcTable_(0)
      23              : {
      24           19 : }
      25              : 
      26           19 : CheckCrc::~CheckCrc()
      27              : {
      28           19 : }
      29              : 
      30           19 : HcclResult CheckCrc::AddCrc(u32 crcValue)
      31              : {
      32           57 :     HCCL_INFO("crcValue[%u]", crcValue);
      33           19 :     crcTable_.push_back(crcValue);
      34           57 :     HCCL_INFO("num[%llu]", crcTable_.size());
      35           19 :     return HCCL_SUCCESS;
      36              : }
      37              : 
      38            5 : HcclResult CheckCrc::GetCrcNum(u32 *num)
      39              : {
      40            5 :     CHK_PTR_NULL(num);
      41           15 :     HCCL_INFO("num[%u]", *num);
      42            5 :     *num = crcTable_.size();
      43            5 :     return HCCL_SUCCESS;
      44              : }
      45              : 
      46            4 : HcclResult CheckCrc::GetCrc(u32 num, u32 *crcAddr)
      47              : {
      48            4 :     CHK_PTR_NULL(crcAddr);
      49           12 :     HCCL_INFO("num[%u], crc[%u]", num, *crcAddr);
      50              : 
      51            4 :     if (num == 0) {
      52            3 :         HCCL_ERROR("[Get][Crc]errNo[0x%016llx] In get crc the value of num is 0", HCCL_ERROR_CODE(HCCL_E_PARA));
      53            1 :         return HCCL_E_PARA;
      54              :     }
      55              : 
      56            3 :     if (num != crcTable_.size()) {
      57            3 :         HCCL_ERROR("[Get][Crc]errNo[0x%016llx] num error inputNum[%u], localNum[%llu]",
      58              :             HCCL_ERROR_CODE(HCCL_E_INTERNAL), num, crcTable_.size());
      59            1 :         return HCCL_E_INTERNAL;
      60              :     }
      61              : 
      62            8 :     for (u32 i = 0; i < num; i++) {
      63            6 :         crcAddr[i] = crcTable_[i];
      64              :     }
      65            2 :     return HCCL_SUCCESS;
      66              : }
      67              : 
      68           11 : void CheckCrc::InitTable(void)
      69              : {
      70         2827 :     for (u32 i = 0; i < CRC_TABLE_LENTH; i++) {
      71         2816 :         u32 crc = i;
      72        25344 :         for (u32 j = 0; j < CRC_CALC_8; j++) {
      73        22528 :             if ((crc & 1) != 0) {
      74        11264 :                 crc = (crc >> 1) ^ CRC_DEFAULT_VALUE;
      75              :             } else {
      76        11264 :                 crc = crc >> 1;
      77              :             }
      78              :         }
      79         2816 :         crcCalcTable[i] = crc;
      80              :     }
      81           11 : }
      82              : 
      83              : // 构造CRC字符串,按照CRC_NUM CRC1 CRC2 CRC3....,每个数字中间使用空格隔开
      84            5 : std::string CheckCrc::GetString(void)
      85              : {
      86            5 :     u32 num = crcTable_.size();
      87            5 :     std::string str;
      88            5 :     str += std::to_string(num);
      89            5 :     str += " ";
      90            5 :     if (num != 0) {
      91           17 :         for (u32 i = 0; i < num; i++) {
      92           13 :             str += std::to_string(crcTable_[i]);
      93           13 :             str += " ";
      94              :         }
      95              :     }
      96           15 :     HCCL_INFO("str[%s]", str.c_str());
      97            5 :     return str;
      98            0 : }
      99              : 
     100           16 : HcclResult CheckCrc::Calc32Crc(const char *data, u64 length, u32 *crcValue)
     101              : {
     102           16 :     CHK_PTR_NULL(data);
     103           16 :     CHK_PTR_NULL(crcValue);
     104           48 :     HCCL_INFO("length[%llu], crcValue[%u]", length, *crcValue);
     105           16 :     u32 ret = INVALID_U32;
     106           16 :     if (initFlag_ == 0) {
     107           11 :         InitTable();
     108           11 :         initFlag_ = 1;
     109              :     }
     110        27150 :     for (u64 i = 0; i < length; i++) {
     111        27134 :         ret = crcCalcTable[((ret & 0xFF) ^ static_cast<u8>(data[i]))] ^ (ret >> CRC_CALC_8);
     112              :     }
     113              : 
     114           16 :     *crcValue = ~ret;
     115           16 :     return HCCL_SUCCESS;
     116              : }
     117              : 
     118            6 : HcclResult CheckCrc::CalcStringCrc(const char *str, u32 *crcValue)
     119              : {
     120            6 :     CHK_PTR_NULL(str);
     121            6 :     CHK_PTR_NULL(crcValue);
     122              :     s32 strLength;
     123              : 
     124            6 :     strLength = strlen(str);
     125            6 :     CHK_PRT_RET(strLength <= 0, \
     126              :         HCCL_ERROR("[Calc][StringCrc]String is empty, String length[%d].", strLength), HCCL_E_PARA);
     127              : 
     128              :     // 计算并设置CRC值
     129            6 :     CHK_RET(this->Calc32Crc(str, static_cast<u64>(strLength), crcValue));
     130              : 
     131            6 :     return HCCL_SUCCESS;
     132              : }
     133              : 
     134            1 : HcclResult CheckCrc::ClearCrcInfo(void)
     135              : {
     136            1 :     this->crcTable_.clear();
     137            1 :     if ((this->crcTable_.size()) != 0) {
     138            0 :         HCCL_ERROR("[Clear][CrcInfo]errNo[0x%016llx] clear crcTable_ is failed", HCCL_ERROR_CODE(HCCL_E_INTERNAL));
     139            0 :         return HCCL_E_INTERNAL;
     140              :     }
     141            1 :     return HCCL_SUCCESS;
     142              : }
     143              : }  // namespace hccl
        

Generated by: LCOV version 2.0-1