llvm.org GIT mirror llvm / a3ee530
[Support] Add zlib independent CRC32 Differential revision: https://reviews.llvm.org/D59816 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@357901 91177308-0d34-0410-b5e6-96231b3b80d8 Eugene Leviant 7 months ago
6 changed file(s) with 126 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/Support/CRC.h - Cyclic Redundancy Check-------------*- C++ -*-===//
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file contains basic functions for calculating Cyclic Redundancy Check
9 // or CRC.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_SUPPORT_CRC_H
14 #define LLVM_SUPPORT_CRC_H
15
16 #include "llvm/ADT/StringRef.h"
17 #include "llvm/Support/DataTypes.h"
18
19 namespace llvm {
20 /// zlib independent CRC32 calculation.
21 uint32_t crc32(uint32_t CRC, StringRef S);
22 } // end namespace llvm
23
24 #endif
2222 #include "llvm/Object/COFF.h"
2323 #include "llvm/Object/MachO.h"
2424 #include "llvm/Object/MachOUniversal.h"
25 #include "llvm/Support/CRC.h"
2526 #include "llvm/Support/Casting.h"
2627 #include "llvm/Support/Compression.h"
2728 #include "llvm/Support/DataExtractor.h"
162163 MemoryBuffer::getFileOrSTDIN(Path);
163164 if (!MB)
164165 return false;
165 return !zlib::isAvailable() || CRCHash == zlib::crc32(MB.get()->getBuffer());
166 return CRCHash == crc32(0, MB.get()->getBuffer());
166167 }
167168
168169 bool findDebugBinary(const std::string &OrigPath,
7575 CodeGenCoverage.cpp
7676 CommandLine.cpp
7777 Compression.cpp
78 CRC.cpp
7879 ConvertUTF.cpp
7980 ConvertUTFWrapper.cpp
8081 CrashRecoveryContext.cpp
0 //===--- CRC.cpp - Cyclic Redundancy Check implementation -----------------===//
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file implements llvm::crc32 function.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "llvm/Support/CRC.h"
13 #include "llvm/Config/config.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/Threading.h"
16 #include
17
18 using namespace llvm;
19
20 #if LLVM_ENABLE_ZLIB == 0 || !HAVE_ZLIB_H
21 using CRC32Table = std::array;
22
23 static void initCRC32Table(CRC32Table &Tbl) {
24 auto Shuffle = [](uint32_t V) {
25 return (V & 1) ? (V >> 1) ^ 0xEDB88320U : V >> 1;
26 };
27
28 for (size_t I = 0; I < Tbl.size(); ++I) {
29 uint32_t V = Shuffle(I);
30 V = Shuffle(V);
31 V = Shuffle(V);
32 V = Shuffle(V);
33 V = Shuffle(V);
34 V = Shuffle(V);
35 V = Shuffle(V);
36 Tbl[I] = Shuffle(V);
37 }
38 }
39
40 uint32_t llvm::crc32(uint32_t CRC, StringRef S) {
41 static llvm::once_flag InitFlag;
42 static CRC32Table Tbl;
43 llvm::call_once(InitFlag, initCRC32Table, Tbl);
44
45 const uint8_t *P = reinterpret_cast(S.data());
46 size_t Len = S.size();
47 CRC ^= 0xFFFFFFFFU;
48 for (; Len >= 8; Len -= 8) {
49 CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
50 CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
51 CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
52 CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
53 CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
54 CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
55 CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
56 CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
57 }
58 while (Len--)
59 CRC = Tbl[(CRC ^ *P++) & 0xFF] ^ (CRC >> 8);
60 return CRC ^ 0xFFFFFFFFU;
61 }
62 #else
63 #include
64 uint32_t llvm::crc32(uint32_t CRC, StringRef S) {
65 return ::crc32(CRC, (const Bytef *)S.data(), S.size());
66 }
67 #endif
1717 CommandLineTest.cpp
1818 CompressionTest.cpp
1919 ConvertUTFTest.cpp
20 CRCTest.cpp
2021 DataExtractorTest.cpp
2122 DebugTest.cpp
2223 DebugCounterTest.cpp
0 //===- llvm/unittest/Support/CRCTest.cpp - CRC tests ----------------------===//
1 //
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file implements unit tests for CRC calculation functions.
9 //
10 //===----------------------------------------------------------------------===//
11
12 #include "llvm/Support/CRC.h"
13 #include "gtest/gtest.h"
14
15 using namespace llvm;
16
17 namespace {
18
19 TEST(CRCTest, CRC32) {
20 EXPECT_EQ(0x414FA339U,
21 llvm::crc32(
22 0, StringRef("The quick brown fox jumps over the lazy dog")));
23 // CRC-32/ISO-HDLC test vector
24 // http://reveng.sourceforge.net/crc-catalogue/17plus.htm#crc.cat.crc-32c
25 EXPECT_EQ(0xCBF43926U, llvm::crc32(0, StringRef("123456789")));
26 }
27
28 } // end anonymous namespace