leveldb
Classes | Functions | Variables
leveldb::crc32c Namespace Reference

Classes

class  CRC
 

Functions

static uint32_t LE_LOAD32 (const uint8_t *p)
 
uint32_t Extend (uint32_t crc, const char *buf, size_t size)
 
uint32_t Value (const char *data, size_t n)
 
uint32_t Mask (uint32_t crc)
 
uint32_t Unmask (uint32_t masked_crc)
 
 TEST (CRC, StandardResults)
 
 TEST (CRC, Values)
 
 TEST (CRC, Extend)
 
 TEST (CRC, Mask)
 

Variables

static const uint32_t table0_ [256]
 
static const uint32_t table1_ [256]
 
static const uint32_t table2_ [256]
 
static const uint32_t table3_ [256]
 
static const uint32_t kMaskDelta = 0xa282ead8ul
 

Class Documentation

§ leveldb::crc32c::CRC

class leveldb::crc32c::CRC

Definition at line 11 of file crc32c_test.cc.

Function Documentation

§ Extend()

uint32_t leveldb::crc32c::Extend ( uint32_t  crc,
const char *  buf,
size_t  size 
)

Definition at line 286 of file crc32c.cc.

286  {
287  const uint8_t *p = reinterpret_cast<const uint8_t *>(buf);
288  const uint8_t *e = p + size;
289  uint32_t l = crc ^ 0xffffffffu;
290 
291 #define STEP1 do { \
292  int c = (l & 0xff) ^ *p++; \
293  l = table0_[c] ^ (l >> 8); \
294 } while (0)
295 #define STEP4 do { \
296  uint32_t c = l ^ LE_LOAD32(p); \
297  p += 4; \
298  l = table3_[c & 0xff] ^ \
299  table2_[(c >> 8) & 0xff] ^ \
300  table1_[(c >> 16) & 0xff] ^ \
301  table0_[c >> 24]; \
302 } while (0)
303 
304  // Point x at first 4-byte aligned byte in string. This might be
305  // just past the end of the string.
306  const uintptr_t pval = reinterpret_cast<uintptr_t>(p);
307  const uint8_t* x = reinterpret_cast<const uint8_t*>(((pval + 3) >> 2) << 2);
308  if (x <= e) {
309  // Process bytes until finished or p is 4-byte aligned
310  while (p != x) {
311  STEP1;
312  }
313  }
314  // Process bytes 16 at a time
315  while ((e-p) >= 16) {
316  STEP4; STEP4; STEP4; STEP4;
317  }
318  // Process bytes 4 at a time
319  while ((e-p) >= 4) {
320  STEP4;
321  }
322  // Process the last few bytes
323  while (p != e) {
324  STEP1;
325  }
326 #undef STEP4
327 #undef STEP1
328  return l ^ 0xffffffffu;
329 }
#define STEP1
#define STEP4
Here is the caller graph for this function:

§ LE_LOAD32()

static uint32_t leveldb::crc32c::LE_LOAD32 ( const uint8_t *  p)
inlinestatic

Definition at line 282 of file crc32c.cc.

282  {
283  return DecodeFixed32(reinterpret_cast<const char*>(p));
284 }
uint32_t DecodeFixed32(const char *ptr)
Definition: coding.h:58
Here is the call graph for this function:

§ Mask()

uint32_t leveldb::crc32c::Mask ( uint32_t  crc)
inline

Definition at line 31 of file crc32c.h.

31  {
32  // Rotate right by 15 bits and add a constant.
33  return ((crc >> 15) | (crc << 17)) + kMaskDelta;
34 }
static const uint32_t kMaskDelta
Definition: crc32c.h:24
Here is the caller graph for this function:

§ TEST() [1/4]

leveldb::crc32c::TEST ( CRC  ,
StandardResults   
)

Definition at line 13 of file crc32c_test.cc.

13  {
14  // From rfc3720 section B.4.
15  char buf[32];
16 
17  memset(buf, 0, sizeof(buf));
18  ASSERT_EQ(0x8a9136aa, Value(buf, sizeof(buf)));
19 
20  memset(buf, 0xff, sizeof(buf));
21  ASSERT_EQ(0x62a8ab43, Value(buf, sizeof(buf)));
22 
23  for (int i = 0; i < 32; i++) {
24  buf[i] = i;
25  }
26  ASSERT_EQ(0x46dd794e, Value(buf, sizeof(buf)));
27 
28  for (int i = 0; i < 32; i++) {
29  buf[i] = 31 - i;
30  }
31  ASSERT_EQ(0x113fdb5c, Value(buf, sizeof(buf)));
32 
33  unsigned char data[48] = {
34  0x01, 0xc0, 0x00, 0x00,
35  0x00, 0x00, 0x00, 0x00,
36  0x00, 0x00, 0x00, 0x00,
37  0x00, 0x00, 0x00, 0x00,
38  0x14, 0x00, 0x00, 0x00,
39  0x00, 0x00, 0x04, 0x00,
40  0x00, 0x00, 0x00, 0x14,
41  0x00, 0x00, 0x00, 0x18,
42  0x28, 0x00, 0x00, 0x00,
43  0x00, 0x00, 0x00, 0x00,
44  0x02, 0x00, 0x00, 0x00,
45  0x00, 0x00, 0x00, 0x00,
46  };
47  ASSERT_EQ(0xd9963a56, Value(reinterpret_cast<char*>(data), sizeof(data)));
48 }
#define ASSERT_EQ(a, b)
Definition: testharness.h:107
uint32_t Value(const char *data, size_t n)
Definition: crc32c.h:20
Here is the call graph for this function:

§ TEST() [2/4]

leveldb::crc32c::TEST ( CRC  ,
Values   
)

Definition at line 50 of file crc32c_test.cc.

50  {
51  ASSERT_NE(Value("a", 1), Value("foo", 3));
52 }
#define ASSERT_NE(a, b)
Definition: testharness.h:108
uint32_t Value(const char *data, size_t n)
Definition: crc32c.h:20
Here is the call graph for this function:

§ TEST() [3/4]

leveldb::crc32c::TEST ( CRC  ,
Extend   
)

Definition at line 54 of file crc32c_test.cc.

54  {
55  ASSERT_EQ(Value("hello world", 11),
56  Extend(Value("hello ", 6), "world", 5));
57 }
#define ASSERT_EQ(a, b)
Definition: testharness.h:107
uint32_t Value(const char *data, size_t n)
Definition: crc32c.h:20
uint32_t Extend(uint32_t crc, const char *buf, size_t size)
Definition: crc32c.cc:286
Here is the call graph for this function:

§ TEST() [4/4]

leveldb::crc32c::TEST ( CRC  ,
Mask   
)

Definition at line 59 of file crc32c_test.cc.

59  {
60  uint32_t crc = Value("foo", 3);
61  ASSERT_NE(crc, Mask(crc));
62  ASSERT_NE(crc, Mask(Mask(crc)));
63  ASSERT_EQ(crc, Unmask(Mask(crc)));
64  ASSERT_EQ(crc, Unmask(Unmask(Mask(Mask(crc)))));
65 }
uint32_t Unmask(uint32_t masked_crc)
Definition: crc32c.h:37
uint32_t Mask(uint32_t crc)
Definition: crc32c.h:31
#define ASSERT_EQ(a, b)
Definition: testharness.h:107
#define ASSERT_NE(a, b)
Definition: testharness.h:108
uint32_t Value(const char *data, size_t n)
Definition: crc32c.h:20
Here is the call graph for this function:

§ Unmask()

uint32_t leveldb::crc32c::Unmask ( uint32_t  masked_crc)
inline

Definition at line 37 of file crc32c.h.

37  {
38  uint32_t rot = masked_crc - kMaskDelta;
39  return ((rot >> 17) | (rot << 15));
40 }
static const uint32_t kMaskDelta
Definition: crc32c.h:24
Here is the caller graph for this function:

§ Value()

uint32_t leveldb::crc32c::Value ( const char *  data,
size_t  n 
)
inline

Definition at line 20 of file crc32c.h.

20  {
21  return Extend(0, data, n);
22 }
uint32_t Extend(uint32_t crc, const char *buf, size_t size)
Definition: crc32c.cc:286
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

§ kMaskDelta

const uint32_t leveldb::crc32c::kMaskDelta = 0xa282ead8ul
static

Definition at line 24 of file crc32c.h.

§ table0_

const uint32_t leveldb::crc32c::table0_[256]
static

Definition at line 16 of file crc32c.cc.

§ table1_

const uint32_t leveldb::crc32c::table1_[256]
static

Definition at line 82 of file crc32c.cc.

§ table2_

const uint32_t leveldb::crc32c::table2_[256]
static

Definition at line 148 of file crc32c.cc.

§ table3_

const uint32_t leveldb::crc32c::table3_[256]
static

Definition at line 214 of file crc32c.cc.