diff options
author | Damien Zammit <damien@zamaudio.com> | 2015-08-05 06:53:05 +1000 |
---|---|---|
committer | Damien Zammit <damien@zamaudio.com> | 2015-08-05 06:53:05 +1000 |
commit | 32e11c420ea7569ce0f6600f6d9600f26ca12836 (patch) | |
tree | abf77ce6e6c5ef04eb7c9c09879b129b9ead8c4d | |
parent | 3bed944d7713131da61572e37979872be2804a65 (diff) |
Updated xor magic
-rw-r--r-- | ptfformat.h | 103 |
1 files changed, 23 insertions, 80 deletions
diff --git a/ptfformat.h b/ptfformat.h index 30a82f9..dee8c1c 100644 --- a/ptfformat.h +++ b/ptfformat.h @@ -27,41 +27,18 @@ #include <stdbool.h> #include <assert.h> -static uint32_t baselut[16] = { - 0xaaaaaaaa, - 0xaa955555, - 0xa9554aaa, - 0xa552a955, - 0xb56ad5aa, - 0x95a95a95, - 0x94a5294a, - 0x9696b4b5, - 0xd2d25a5a, - 0xd24b6d25, - 0xdb6db6da, - 0xd9249b6d, - 0xc9b64d92, - 0xcd93264d, - 0xccd99b32, - 0xcccccccd +static const uint32_t baselut[16] = { + 0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955, + 0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5, + 0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d, + 0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd }; -static uint32_t hiseed[8] = { - 0x00000000, - 0x00b00000, - 0x0b001000, - 0x010b0b00, - 0x10b0b010, - 0xb01b01b0, - 0xb101bb10, - 0xbbbb1110 -}; - -static uint32_t hixor4[4] = { // simple bit-map - 0x000, - 0x0a0, - 0xa00, - 0x0aa, +static const uint32_t xorlut[16] = { + 0x00000000, 0x00b00000, 0x0b001000, 0x010b0b00, + 0x10b0b010, 0xb01b01b0, 0xb101bb10, 0xbbbb1110, + 0xbbbb1111, 0xbb01bbb1, 0xb0bb0bb1, 0xbab0b0bb, + 0xab0b0bab, 0xabaaba0b, 0xaabaa0ab, 0xaaaaaaab }; class PTFFormat { @@ -69,7 +46,7 @@ public: PTFFormat(); ~PTFFormat(); - static uint32_t swapbytes32 (uint32_t v) { + static const uint32_t swapbytes32 (const uint32_t v) { uint32_t rv = 0; rv |= ((v >> 0) & 0xf) << 28; rv |= ((v >> 4) & 0xf) << 24; @@ -82,71 +59,37 @@ public: return rv; } - static uint32_t swapbytes16 (uint32_t v) { - uint32_t rv = 0; - rv |= ((v >> 0) & 0xf) << 12; - rv |= ((v >> 4) & 0xf) << 8; - rv |= ((v >> 8) & 0xf) << 4; - rv |= ((v >> 12) & 0xf) << 0; - return rv; - } - - static uint32_t mirror16 (uint32_t v) { - assert (0 == (v & 0xffff0000)); - return (swapbytes16 (v) << 16) | v; - } - - static uint32_t himap(int i) { - if (i & 4) { - return swapbytes16 (hixor4 [3 - (i & 3)] ^ 0xaaaa); - } else { - return hixor4 [i & 3]; - } - } - - static uint64_t gen_secret (int i) { + static const uint64_t gen_secret (int i) { + assert (i > 0 && i < 256); int iwrap = i & 0x7f; // wrap at 0x80; - int idx; // mirror at 0x40; - uint32_t xor_lo = 0; // 0x40 flag - bool xorX = false; // mirror at 0x20 + int idx; // mirror at 0x40; + uint32_t xor_lo = 0; // 0x40 flag + bool xor_20 = false; // mirror at 0x20 if (iwrap & 0x40) { - idx = 0x80 - iwrap; xor_lo = 0x1; + idx = 0x80 - iwrap; } else { - idx = iwrap; + idx = iwrap; } - int i16 = (idx >> 1) & 15; - + int i16 = (idx >> 1) & 0xf; if (idx & 0x20) { i16 = 15 - i16; - xorX = true; + xor_20 = true; } uint32_t lo = baselut [i16]; + uint32_t xk = xorlut [i16]; - int i08 = i16 & 7; - - uint32_t xk; - - if (i16 & 0x08) { - xk = hiseed [7 - i08] ^ 1 ^ mirror16 (himap(i08)); - } else { - xk = hiseed [i08]; - } - - if (xorX) { + if (xor_20) { lo ^= 0xaaaaaaab; xk ^= 1; } - - uint32_t hi = swapbytes32(lo) ^ swapbytes32(xk); - + uint32_t hi = swapbytes32 (lo) ^ swapbytes32 (xk); return ((uint64_t)hi << 32) | (lo ^ xor_lo); } - /* Return values: 0 success -1 could not open file as ptf */ |