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 57 : CheckCrc::CheckCrc() : initFlag_(0), crcCalcTable{0}, crcTable_(0) {}
23 :
24 19 : CheckCrc::~CheckCrc() {}
25 :
26 19 : HcclResult CheckCrc::AddCrc(u32 crcValue)
27 : {
28 57 : HCCL_INFO("crcValue[%u]", crcValue);
29 19 : crcTable_.push_back(crcValue);
30 57 : HCCL_INFO("num[%llu]", crcTable_.size());
31 19 : return HCCL_SUCCESS;
32 : }
33 :
34 5 : HcclResult CheckCrc::GetCrcNum(u32* num)
35 : {
36 5 : CHK_PTR_NULL(num);
37 15 : HCCL_INFO("num[%u]", *num);
38 5 : *num = crcTable_.size();
39 5 : return HCCL_SUCCESS;
40 : }
41 :
42 4 : HcclResult CheckCrc::GetCrc(u32 num, u32* crcAddr)
43 : {
44 4 : CHK_PTR_NULL(crcAddr);
45 12 : HCCL_INFO("num[%u], crc[%u]", num, *crcAddr);
46 :
47 4 : if (num == 0) {
48 3 : HCCL_ERROR("[Get][Crc]errNo[0x%016llx] In get crc the value of num is 0", HCCL_ERROR_CODE(HCCL_E_PARA));
49 1 : return HCCL_E_PARA;
50 : }
51 :
52 3 : if (num != crcTable_.size()) {
53 3 : HCCL_ERROR(
54 : "[Get][Crc]errNo[0x%016llx] num error inputNum[%u], localNum[%llu]", HCCL_ERROR_CODE(HCCL_E_INTERNAL), num,
55 : crcTable_.size());
56 1 : return HCCL_E_INTERNAL;
57 : }
58 :
59 8 : for (u32 i = 0; i < num; i++) {
60 6 : crcAddr[i] = crcTable_[i];
61 : }
62 2 : return HCCL_SUCCESS;
63 : }
64 :
65 11 : void CheckCrc::InitTable(void)
66 : {
67 2827 : for (u32 i = 0; i < CRC_TABLE_LENTH; i++) {
68 2816 : u32 crc = i;
69 25344 : for (u32 j = 0; j < CRC_CALC_8; j++) {
70 22528 : if ((crc & 1) != 0) {
71 11264 : crc = (crc >> 1) ^ CRC_DEFAULT_VALUE;
72 : } else {
73 11264 : crc = crc >> 1;
74 : }
75 : }
76 2816 : crcCalcTable[i] = crc;
77 : }
78 11 : }
79 :
80 : // 构造CRC字符串,按照CRC_NUM CRC1 CRC2 CRC3....,每个数字中间使用空格隔开
81 5 : std::string CheckCrc::GetString(void)
82 : {
83 5 : u32 num = crcTable_.size();
84 5 : std::string str;
85 5 : str += std::to_string(num);
86 5 : str += " ";
87 5 : if (num != 0) {
88 17 : for (u32 i = 0; i < num; i++) {
89 13 : str += std::to_string(crcTable_[i]);
90 13 : str += " ";
91 : }
92 : }
93 15 : HCCL_INFO("str[%s]", str.c_str());
94 5 : return str;
95 0 : }
96 :
97 16 : HcclResult CheckCrc::Calc32Crc(const char* data, u64 length, u32* crcValue)
98 : {
99 16 : CHK_PTR_NULL(data);
100 16 : CHK_PTR_NULL(crcValue);
101 48 : HCCL_INFO("length[%llu], crcValue[%u]", length, *crcValue);
102 16 : u32 ret = INVALID_U32;
103 16 : if (initFlag_ == 0) {
104 11 : InitTable();
105 11 : initFlag_ = 1;
106 : }
107 27150 : for (u64 i = 0; i < length; i++) {
108 27134 : ret = crcCalcTable[((ret & 0xFF) ^ static_cast<u8>(data[i]))] ^ (ret >> CRC_CALC_8);
109 : }
110 :
111 16 : *crcValue = ~ret;
112 16 : return HCCL_SUCCESS;
113 : }
114 :
115 6 : HcclResult CheckCrc::CalcStringCrc(const char* str, u32* crcValue)
116 : {
117 6 : CHK_PTR_NULL(str);
118 6 : CHK_PTR_NULL(crcValue);
119 : s32 strLength;
120 :
121 6 : strLength = strlen(str);
122 6 : CHK_PRT_RET(
123 : strLength <= 0, HCCL_ERROR("[Calc][StringCrc]String is empty, String length[%d].", strLength), HCCL_E_PARA);
124 :
125 : // 计算并设置CRC值
126 6 : CHK_RET(this->Calc32Crc(str, static_cast<u64>(strLength), crcValue));
127 :
128 6 : return HCCL_SUCCESS;
129 : }
130 :
131 1 : HcclResult CheckCrc::ClearCrcInfo(void)
132 : {
133 1 : this->crcTable_.clear();
134 1 : if ((this->crcTable_.size()) != 0) {
135 0 : HCCL_ERROR("[Clear][CrcInfo]errNo[0x%016llx] clear crcTable_ is failed", HCCL_ERROR_CODE(HCCL_E_INTERNAL));
136 0 : return HCCL_E_INTERNAL;
137 : }
138 1 : return HCCL_SUCCESS;
139 : }
140 : } // namespace Hccl
|