For SNAT, don't store the pre-fragment L2 header before actions are applied.
[sliver-openvswitch.git] / datapath / crc32.c
1 /*
2  * Distributed under the terms of the GNU GPL version 2.
3  * Copyright (c) 2007, 2008 The Board of Trustees of The Leland 
4  * Stanford Junior University
5  */
6
7 #include "crc32.h"
8
9 void crc32_init(struct crc32 *crc, unsigned int polynomial)
10 {
11         int i;
12
13         for (i = 0; i < CRC32_TABLE_SIZE; ++i) {
14                 unsigned int reg = i << 24;
15                 int j;
16                 for (j = 0; j < CRC32_TABLE_BITS; j++) {
17                         int topBit = (reg & 0x80000000) != 0;
18                         reg <<= 1;
19                         if (topBit)
20                                 reg ^= polynomial;
21                         }
22                         crc->table[i] = reg;
23         }
24 }
25
26 unsigned int crc32_calculate(const struct crc32 *crc,
27                         const void *data_, size_t n_bytes)
28 {
29         // FIXME: this can be optimized by unrolling, see linux-2.6/lib/crc32.c.
30         const uint8_t *data = data_;
31         unsigned int result = 0;
32         size_t i;
33
34         for (i = 0; i < n_bytes; i++) {
35                 unsigned int top = result >> 24;
36                 top ^= data[i];
37                 result = (result << 8) ^ crc->table[top];
38         }
39         return result;
40 }