| 16 | } |
| 17 | |
| 18 | uint64_t generic_crc(const struct crcspec& spec, const Bytes& bytes) |
| 19 | { |
| 20 | uint64_t crc = spec.init; |
| 21 | uint64_t top = 1LL << (spec.width - 1); |
| 22 | uint64_t mask = (top << 1) - 1; |
| 23 | |
| 24 | for (uint8_t b : bytes) |
| 25 | { |
| 26 | if (spec.refin) |
| 27 | b = reflect(b); |
| 28 | |
| 29 | for (uint8_t i = 0x80; i != 0; i >>= 1) |
| 30 | { |
| 31 | uint64_t bit = crc & top; |
| 32 | crc <<= 1; |
| 33 | if (b & i) |
| 34 | bit ^= top; |
| 35 | if (bit) |
| 36 | crc ^= spec.poly; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | if (spec.refout) |
| 41 | crc = reflect(crc, spec.width); |
| 42 | crc ^= spec.xorout; |
| 43 | return crc & mask; |
| 44 | } |
| 45 | |
| 46 | uint16_t sumBytes(const Bytes& bytes) |
| 47 | { |