]> cvs.zerfleddert.de Git - proxmark3-svn/blame - common/parity.h
Adding support for standard USB Smartcard Readers (#769)
[proxmark3-svn] / common / parity.h
CommitLineData
1f065e1d 1//-----------------------------------------------------------------------------
2// This code is licensed to you under the terms of the GNU GPL, version 2 or,
3// at your option, any later version. See the LICENSE.txt file for the text of
4// the license.
5//-----------------------------------------------------------------------------
6// Parity functions
7//-----------------------------------------------------------------------------
8
9// all functions defined in header file by purpose. Allows compiler optimizations.
10
11#ifndef __PARITY_H
12#define __PARITY_H
13
14#include <stdint.h>
15#include <stdbool.h>
a37725fa 16#include "string.h"
1f065e1d 17
18extern const uint8_t OddByteParity[256];
19
20
21static inline bool oddparity8(const uint8_t x) {
22 return OddByteParity[x];
23}
24
a37725fa
OM
25static inline void oddparitybuf(const uint8_t *x, size_t len, uint8_t *parity) {
26 memset(parity, 0x00, (len - 1) / 8 + 1);
27 for (int i = 0; i < len; i++)
28 parity[i / 8] |= oddparity8(x[i]) << (7 - (i % 8));
29}
1f065e1d 30
31static inline bool evenparity8(const uint8_t x) {
32 return !OddByteParity[x];
33}
34
35
36static inline bool evenparity32(uint32_t x)
37{
38#if !defined __GNUC__
39 x ^= x >> 16;
40 x ^= x >> 8;
41 return evenparity8(x);
42#else
43 return __builtin_parity(x);
44#endif
45}
46
47
48static inline bool oddparity32(uint32_t x)
49{
50#if !defined __GNUC__
51 x ^= x >> 16;
52 x ^= x >> 8;
53 return oddparity8(x);
54#else
55 return !__builtin_parity(x);
56#endif
57}
58
59#endif /* __PARITY_H */
Impressum, Datenschutz