mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-08-19 13:00:42 -07:00
add: reflect16
rem: swapbits, reflect
This commit is contained in:
parent
e396575424
commit
cf44d04be1
2 changed files with 22 additions and 29 deletions
|
@ -13,33 +13,28 @@ size_t nbytes(size_t nbits) {
|
||||||
return (nbits >> 3)+((nbits % 8) > 0);
|
return (nbits >> 3)+((nbits % 8) > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Swap bit order on a uint32_t value. Can be limited by 'b' just use say 8 bits reversal
|
|
||||||
// note: function clears the rest of the bits.
|
|
||||||
uint32_t SwapBits(uint32_t v, int b) {
|
|
||||||
uint32_t newvalue = 0;
|
|
||||||
for(int i = 0; i < b; i++) {
|
|
||||||
newvalue ^= ((v >> i) & 1) << (b - 1 - i);
|
|
||||||
}
|
|
||||||
return newvalue;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t reflect8(uint8_t b) {
|
uint8_t reflect8(uint8_t b) {
|
||||||
return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
|
return ((b * 0x80200802ULL) & 0x0884422110ULL) * 0x0101010101ULL >> 32;
|
||||||
}
|
}
|
||||||
/*
|
uint16_t reflect16(uint16_t b) {
|
||||||
ref http://www.csm.ornl.gov/~dunigan/crc.html
|
uint16_t v = 0;
|
||||||
Returns the value v with the bottom b [0,32] bits reflected.
|
v |= (b & 0x8000) >> 15;
|
||||||
Example: reflect(0x3e23L,3) == 0x3e26
|
v |= (b & 0x4000) >> 13;
|
||||||
*/
|
v |= (b & 0x2000) >> 11;
|
||||||
uint32_t reflect(uint32_t v, int b) {
|
v |= (b & 0x1000) >> 9;
|
||||||
uint32_t t = v;
|
v |= (b & 0x0800) >> 7;
|
||||||
for ( int i = 0; i < b; ++i) {
|
v |= (b & 0x0400) >> 5;
|
||||||
if (t & 1)
|
v |= (b & 0x0200) >> 3;
|
||||||
v |= BITMASK((b-1)-i);
|
v |= (b & 0x0100) >> 1;
|
||||||
else
|
|
||||||
v &= ~BITMASK((b-1)-i);
|
v |= (b & 0x0080) << 1;
|
||||||
t >>= 1;
|
v |= (b & 0x0040) << 3;
|
||||||
}
|
v |= (b & 0x0020) << 5;
|
||||||
|
v |= (b & 0x0010) << 7;
|
||||||
|
v |= (b & 0x0008) << 9;
|
||||||
|
v |= (b & 0x0004) << 11;
|
||||||
|
v |= (b & 0x0002) << 13;
|
||||||
|
v |= (b & 0x0001) << 15;
|
||||||
return v;
|
return v;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,11 +40,9 @@
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
size_t nbytes(size_t nbits);
|
size_t nbytes(size_t nbits);
|
||||||
uint32_t SwapBits(uint32_t value, int nrbits);
|
|
||||||
uint32_t reflect(uint32_t v, int b);
|
|
||||||
|
|
||||||
// dedicated 8bit reversal
|
extern uint8_t reflect8(uint8_t b); // dedicated 8bit reversal
|
||||||
uint8_t reflect8(uint8_t b);
|
extern uint16_t reflect16(uint16_t b); // dedicated 16bit reversal
|
||||||
|
|
||||||
void num_to_bytes(uint64_t n, size_t len, uint8_t* dest);
|
void num_to_bytes(uint64_t n, size_t len, uint8_t* dest);
|
||||||
uint64_t bytes_to_num(uint8_t* src, size_t len);
|
uint64_t bytes_to_num(uint8_t* src, size_t len);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue