XRootD
XrdOucCRC32C.hh File Reference
#include <cstddef>
#include <cstdint>
+ Include dependency graph for XrdOucCRC32C.hh:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

uint32_t crc32c (uint32_t crc, void const *buf, size_t len)
 
uint32_t crc32c_combine (uint32_t crcA, uint32_t crcB, size_t lengthB)
 merge two CRC32 such that result = crc32(dataB, lengthB, crc32(dataA, lengthA)) More...
 
uint32_t crc32c_sw (uint32_t crc, void const *buf, size_t len)
 

Function Documentation

◆ crc32c()

uint32_t crc32c ( uint32_t  crc,
void const *  buf,
size_t  len 
)

Definition at line 421 of file XrdOucCRC32C.cc.

421  {
422  return crc32c_sw(crc, buf, len);
423 }
uint32_t crc32c_sw(uint32_t crc, void const *buf, size_t len)

References crc32c_sw().

Referenced by XrdEc::ObjCfg::ObjCfg(), XrdOucCRC::Calc32C(), XrdPfc::Info::CalcCksumStore(), XrdPfc::Info::CalcCksumSyncedAndAStats(), XrdOssCsiPages::FetchRangeUnaligned_postblock(), XrdOssCsiPages::FetchRangeUnaligned_preblock(), XrdOssCsiPages::StoreRangeUnaligned_postblock(), XrdOssCsiPages::StoreRangeUnaligned_preblock(), XrdOssCsiPages::truncate(), XrdOssCsiPages::UpdateRangeHoleUntilPage(), and XrdOucCRC::Ver32C().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ crc32c_combine()

uint32_t crc32c_combine ( uint32_t  crcA,
uint32_t  crcB,
size_t  lengthB 
)

merge two CRC32 such that result = crc32(dataB, lengthB, crc32(dataA, lengthA))

CRC32 => 32 bits

Definition at line 581 of file XrdOucCRC32C.cc.

582 {
583  // Original source from Stephan Brumme
584  // https://github.com/stbrumme/crc32
585  // https://create.stephan-brumme.com/crc32/
586  // based on Mark Adler's crc_combine from
587  // https://github.com/madler/pigz/blob/master/pigz.c
588 
589  // main idea:
590  // - if you have two equally-sized blocks A and B,
591  // then you can create a block C = A ^ B
592  // which has the property crc(C) = crc(A) ^ crc(B)
593  // - if you append length(B) zeros to A and call it A' (think of it as AAAA000)
594  // and prepend length(A) zeros to B and call it B' (think of it as 0000BBB)
595  // then exists a C' = A' ^ B'
596  // - remember: if you XOR someting with zero, it remains unchanged: X ^ 0 = X
597  // - that means C' = A concat B so that crc(A concat B) = crc(C') = crc(A') ^ crc(B')
598  // - the trick is to compute crc(A') based on crc(A)
599  // and crc(B') based on crc(B)
600  // - since B' starts with many zeros, the crc of those initial zeros is still zero
601  // - that means crc(B') = crc(B)
602  // - unfortunately the trailing zeros of A' change the crc, so usually crc(A') != crc(A)
603  // - the following code is a fast algorithm to compute crc(A')
604  // - starting with crc(A) and appending length(B) zeros, needing just log2(length(B)) iterations
605  // - the details are explained by the original author at
606  // https://stackoverflow.com/questions/23122312/crc-calculation-of-a-mostly-static-data-stream/23126768
607  //
608  // notes:
609  // - I squeezed everything into one function to keep global namespace clean (original code two helper functions)
610  // - most original comments are still in place, I added comments where these helper functions where made inline code
611  // - performance-wise there isn't any differenze to the original zlib/pigz code
612 
613  // degenerated case
614  if (lengthB == 0)
615  return crcA;
616 
618  const uint32_t CrcBits = 32;
619 
620  uint32_t odd [CrcBits]; // odd-power-of-two zeros operator
621  uint32_t even[CrcBits]; // even-power-of-two zeros operator
622 
623  // put operator for one zero bit in odd
624  odd[0] = POLY; // CRC-32 polynomial
625  for (int i = 1; i < (int)CrcBits; i++)
626  odd[i] = 1 << (i - 1);
627 
628  // put operator for two zero bits in even
629  // same as gf2_matrix_square(even, odd);
630  for (int i = 0; i < (int)CrcBits; i++)
631  {
632  uint32_t vec = odd[i];
633  even[i] = 0;
634  for (int j = 0; vec != 0; j++, vec >>= 1)
635  if (vec & 1)
636  even[i] ^= odd[j];
637  }
638  // put operator for four zero bits in odd
639  // same as gf2_matrix_square(odd, even);
640  for (int i = 0; i < (int)CrcBits; i++)
641  {
642  uint32_t vec = even[i];
643  odd[i] = 0;
644  for (int j = 0; vec != 0; j++, vec >>= 1)
645  if (vec & 1)
646  odd[i] ^= even[j];
647  }
648 
649  // the following loop becomes much shorter if I keep swapping even and odd
650  uint32_t* a = even;
651  uint32_t* b = odd;
652  // apply secondLength zeros to firstCrc32
653  for (; lengthB > 0; lengthB >>= 1)
654  {
655  // same as gf2_matrix_square(a, b);
656  for (int i = 0; i < (int)CrcBits; i++)
657  {
658  uint32_t vec = b[i];
659  a[i] = 0;
660  for (int j = 0; vec != 0; j++, vec >>= 1)
661  if (vec & 1)
662  a[i] ^= b[j];
663  }
664 
665  // apply zeros operator for this bit
666  if (lengthB & 1)
667  {
668  // same as firstCrc32 = gf2_matrix_times(a, firstCrc32);
669  uint32_t sum = 0;
670  for (int i = 0; crcA != 0; i++, crcA >>= 1)
671  if (crcA & 1)
672  sum ^= a[i];
673  crcA = sum;
674  }
675 
676  // switch even and odd
677  uint32_t* t = a; a = b; b = t;
678  }
679 
680  // return combined crc
681  return crcA ^ crcB;
682 }
#define POLY
Definition: XrdOucCRC32C.cc:67

References POLY.

Referenced by XrdOucCRC::Combine32C().

+ Here is the caller graph for this function:

◆ crc32c_sw()

uint32_t crc32c_sw ( uint32_t  crc,
void const *  buf,
size_t  len 
)

Definition at line 568 of file XrdOucCRC32C.cc.

568  {
569  static int const little = 1;
570  if (*(char const *)&little)
571  return crc32c_sw_little(crc, buf, len);
572  else
573  return crc32c_sw_big(crc, buf, len);
574 }
uint32_t crc32c_sw_big(uint32_t crc, void const *buf, size_t len)
uint32_t crc32c_sw_little(uint32_t crc, void const *buf, size_t len)

References crc32c_sw_big(), and crc32c_sw_little().

Referenced by crc32c().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: