2015-12-15 16:34:55 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
2022-01-07 08:58:03 +08:00
|
|
|
// Copyright (C) Proxmark3 contributors. See AUTHORS.md for details.
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// See LICENSE.txt for the text of the license.
|
2015-12-15 16:34:55 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-07-14 21:51:12 +08:00
|
|
|
// Parity functions
|
2015-12-15 16:34:55 +08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2019-03-09 15:59:13 +08:00
|
|
|
// all functions defined in header file by purpose. Allows compiler optimizations.
|
2017-07-14 21:51:12 +08:00
|
|
|
|
2015-12-15 16:34:55 +08:00
|
|
|
#ifndef __PARITY_H
|
|
|
|
#define __PARITY_H
|
|
|
|
|
2019-08-08 22:57:33 +08:00
|
|
|
#include "common.h"
|
2015-12-15 16:34:55 +08:00
|
|
|
|
2022-01-09 20:34:54 +08:00
|
|
|
static const uint8_t g_odd_byte_parity[256] = {
|
|
|
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
|
|
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
|
|
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
|
|
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
|
|
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
|
|
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
|
|
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
|
|
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
|
|
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
|
|
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
|
|
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
|
|
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
|
|
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1,
|
|
|
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
|
|
|
0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 0,
|
|
|
|
1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 0, 1
|
|
|
|
};
|
|
|
|
|
|
|
|
//extern const uint8_t OddByteParity[256];
|
|
|
|
|
|
|
|
#define ODD_PARITY8(x) { g_odd_byte_parity[x] }
|
|
|
|
#define EVEN_PARITY8(x) { !g_odd_byte_parity[x] }
|
2015-12-15 16:34:55 +08:00
|
|
|
|
2021-02-24 06:00:29 +08:00
|
|
|
static inline uint8_t oddparity8(const uint8_t x) {
|
2022-01-09 20:34:54 +08:00
|
|
|
return g_odd_byte_parity[x];
|
2017-07-14 21:51:12 +08:00
|
|
|
}
|
|
|
|
|
2021-02-24 06:00:29 +08:00
|
|
|
static inline uint8_t evenparity8(const uint8_t x) {
|
2022-01-09 20:34:54 +08:00
|
|
|
return !g_odd_byte_parity[x];
|
2016-01-17 06:05:21 +08:00
|
|
|
}
|
2015-12-15 16:34:55 +08:00
|
|
|
|
2021-07-14 21:01:01 +08:00
|
|
|
static inline uint8_t evenparity16(uint16_t x) {
|
|
|
|
#if !defined __GNUC__
|
|
|
|
x ^= x >> 8;
|
2022-01-09 20:34:54 +08:00
|
|
|
return EVEN_PARITY8(x) ;
|
2021-07-14 21:01:01 +08:00
|
|
|
#else
|
|
|
|
return (__builtin_parity(x) & 0xFF);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint8_t oddparity16(uint16_t x) {
|
|
|
|
#if !defined __GNUC__
|
|
|
|
x ^= x >> 8;
|
2022-01-09 20:34:54 +08:00
|
|
|
return ODD_PARITY8(x);
|
2021-07-14 21:01:01 +08:00
|
|
|
#else
|
|
|
|
return !__builtin_parity(x);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-02-24 06:00:29 +08:00
|
|
|
static inline uint8_t evenparity32(uint32_t x) {
|
2017-07-14 21:51:12 +08:00
|
|
|
#if !defined __GNUC__
|
2019-03-10 02:19:50 +08:00
|
|
|
x ^= x >> 16;
|
|
|
|
x ^= x >> 8;
|
2022-01-09 20:34:54 +08:00
|
|
|
return EVEN_PARITY8(x);
|
2017-07-14 21:51:12 +08:00
|
|
|
#else
|
2021-04-17 19:16:39 +08:00
|
|
|
return (__builtin_parity(x) & 0xFF);
|
2017-07-14 21:51:12 +08:00
|
|
|
#endif
|
2015-12-15 16:34:55 +08:00
|
|
|
}
|
|
|
|
|
2021-02-24 06:00:29 +08:00
|
|
|
static inline uint8_t oddparity32(uint32_t x) {
|
2017-07-14 21:51:12 +08:00
|
|
|
#if !defined __GNUC__
|
2019-03-10 02:19:50 +08:00
|
|
|
x ^= x >> 16;
|
|
|
|
x ^= x >> 8;
|
2022-01-09 20:34:54 +08:00
|
|
|
return ODD_PARITY8(x);
|
2017-07-14 21:51:12 +08:00
|
|
|
#else
|
2019-03-10 02:19:50 +08:00
|
|
|
return !__builtin_parity(x);
|
2017-07-14 21:51:12 +08:00
|
|
|
#endif
|
2015-12-15 16:34:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __PARITY_H */
|