diff options
author | Damien Zammit <damien@zamaudio.com> | 2015-08-05 07:09:29 +1000 |
---|---|---|
committer | Damien Zammit <damien@zamaudio.com> | 2015-08-05 07:09:29 +1000 |
commit | 077da383a58c4201132e1895fa408fc170072ebf (patch) | |
tree | 81ba3113356620dcd576a4d103c035cf01e4a5d0 | |
parent | 32e11c420ea7569ce0f6600f6d9600f26ca12836 (diff) |
Updated XOR magic from Robin
Signed-off-by: Damien Zammit <damien@zamaudio.com>
-rw-r--r-- | ptfformat.cc | 70 | ||||
-rw-r--r-- | ptfformat.h | 196 |
2 files changed, 63 insertions, 203 deletions
diff --git a/ptfformat.cc b/ptfformat.cc index 5899298..87bc33f 100644 --- a/ptfformat.cc +++ b/ptfformat.cc @@ -1,5 +1,6 @@ /* Copyright (C) 2015 Damien Zammit + Copyright (C) 2015 Robin Gareus 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 @@ -16,6 +17,62 @@ #include "ptfformat.h" using namespace std; +static const uint32_t baselut[16] = { + 0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955, + 0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5, + 0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d, + 0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd +}; + +static const uint32_t xorlut[16] = { + 0x00000000, 0x00000b00, 0x000100b0, 0x00b0b010, + 0x010b0b01, 0x0b10b10b, 0x01bb101b, 0x0111bbbb, + 0x1111bbbb, 0x1bbb10bb, 0x1bb0bb0b, 0xbb0b0bab, + 0xbab0b0ba, 0xb0abaaba, 0xba0aabaa, 0xbaaaaaaa +}; + +static const uint32_t swapbytes32 (const uint32_t v) { + uint32_t rv = 0; + rv |= ((v >> 0) & 0xf) << 28; + rv |= ((v >> 4) & 0xf) << 24; + rv |= ((v >> 8) & 0xf) << 20; + rv |= ((v >> 12) & 0xf) << 16; + rv |= ((v >> 16) & 0xf) << 12; + rv |= ((v >> 20) & 0xf) << 8; + rv |= ((v >> 24) & 0xf) << 4; + rv |= ((v >> 28) & 0xf) << 0; + return rv; +} + +static const uint64_t gen_secret (int i) { + assert (i > 0 && i < 256); + int iwrap = i & 0x7f; // wrap at 0x80; + uint32_t xor_lo = 0; // 0x40 flag + int idx; // mirror at 0x40; + + if (iwrap & 0x40) { + xor_lo = 0x1; + idx = 0x80 - iwrap; + } else { + idx = iwrap; + } + + int i16 = (idx >> 1) & 0xf; + if (idx & 0x20) { + i16 = 15 - i16; + } + + uint32_t lo = baselut [i16]; + uint32_t xk = xorlut [i16]; + + if (idx & 0x20) { + lo ^= 0xaaaaaaab; + xk ^= 0x10000000; + } + uint32_t hi = swapbytes32 (lo) ^ xk; + return ((uint64_t)hi << 32) | (lo ^ xor_lo); +} + PTFFormat::PTFFormat() { } @@ -35,8 +92,6 @@ PTFFormat::foundin(std::string haystack, std::string needle) { } } - - /* Return values: 0 success 0x01 to 0xff value of missing lut -1 could not open file as ptf @@ -226,14 +281,15 @@ PTFFormat::parse8header(void) { } k++; } + this->sessionrate = 0; this->sessionrate |= ptfunxored[k+11]; this->sessionrate |= ptfunxored[k+12] << 8; this->sessionrate |= ptfunxored[k+13] << 16; } + void -PTFFormat::parse9header(void) -{ +PTFFormat::parse9header(void) { int k; // Find session sample rate @@ -253,11 +309,11 @@ PTFFormat::parse9header(void) } void -PTFFormat::parserest(void) -{ +PTFFormat::parserest(void) { int i,j,k,l; - + // Find end of wav file list + k = 0; while (k < len) { if ( (ptfunxored[k ] == 0xff) && (ptfunxored[k+1] == 0xff) && diff --git a/ptfformat.h b/ptfformat.h index dee8c1c..3e21b8c 100644 --- a/ptfformat.h +++ b/ptfformat.h @@ -1,196 +0,0 @@ -/* - Copyright (C) 2015 Damien Zammit - Copyright (C) 2015 Robin Gareus - - 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. - -*/ -#ifndef PTFFORMAT_H -#define PTFFORMAT_H - -#include <stdlib.h> -#include <string.h> -#include <string> -#include <cstring> -#include <algorithm> -#include <vector> -#include <cstdio> -#include <stdint.h> -#include <stdbool.h> -#include <assert.h> - -static const uint32_t baselut[16] = { - 0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955, - 0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5, - 0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d, - 0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd -}; - -static const uint32_t xorlut[16] = { - 0x00000000, 0x00b00000, 0x0b001000, 0x010b0b00, - 0x10b0b010, 0xb01b01b0, 0xb101bb10, 0xbbbb1110, - 0xbbbb1111, 0xbb01bbb1, 0xb0bb0bb1, 0xbab0b0bb, - 0xab0b0bab, 0xabaaba0b, 0xaabaa0ab, 0xaaaaaaab -}; - -class PTFFormat { -public: - PTFFormat(); - ~PTFFormat(); - - static const uint32_t swapbytes32 (const uint32_t v) { - uint32_t rv = 0; - rv |= ((v >> 0) & 0xf) << 28; - rv |= ((v >> 4) & 0xf) << 24; - rv |= ((v >> 8) & 0xf) << 20; - rv |= ((v >> 12) & 0xf) << 16; - rv |= ((v >> 16) & 0xf) << 12; - rv |= ((v >> 20) & 0xf) << 8; - rv |= ((v >> 24) & 0xf) << 4; - rv |= ((v >> 28) & 0xf) << 0; - return rv; - } - - 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 xor_20 = false; // mirror at 0x20 - - if (iwrap & 0x40) { - xor_lo = 0x1; - idx = 0x80 - iwrap; - } else { - idx = iwrap; - } - - int i16 = (idx >> 1) & 0xf; - if (idx & 0x20) { - i16 = 15 - i16; - xor_20 = true; - } - - uint32_t lo = baselut [i16]; - uint32_t xk = xorlut [i16]; - - if (xor_20) { - lo ^= 0xaaaaaaab; - xk ^= 1; - } - 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 - */ - int load(std::string path); - - typedef struct wav { - std::string filename; - uint16_t index; - - int64_t posabsolute; - int64_t length; - - bool operator ==(const struct wav& other) { - return (this->index == other.index); - } - - } wav_t; - - typedef struct region { - std::string name; - uint16_t index; - int64_t startpos; - int64_t sampleoffset; - int64_t length; - wav_t wave; - - bool operator ==(const struct region& other) { - return (this->index == other.index); - } - } region_t; - - typedef struct track { - std::string name; - uint16_t index; - uint8_t playlist; - region_t reg; - - bool operator ==(const struct track& other) { - return (this->index == other.index); - } - } track_t; - - std::vector<wav_t> audiofiles; - std::vector<region_t> regions; - std::vector<track_t> tracks; - - static bool trackexistsin(std::vector<track_t> tr, uint16_t index) { - std::vector<track_t>::iterator begin = tr.begin(); - std::vector<track_t>::iterator finish = tr.end(); - std::vector<track_t>::iterator found; - - track_t f = { std::string(""), index }; - - if ((found = std::find(begin, finish, f)) != finish) { - return true; - } - return false; - } - - static bool regionexistsin(std::vector<region_t> reg, uint16_t index) { - std::vector<region_t>::iterator begin = reg.begin(); - std::vector<region_t>::iterator finish = reg.end(); - std::vector<region_t>::iterator found; - - region_t r = { std::string(""), index }; - - if ((found = std::find(begin, finish, r)) != finish) { - return true; - } - return false; - } - - static bool wavexistsin(std::vector<wav_t> wv, uint16_t index) { - std::vector<wav_t>::iterator begin = wv.begin(); - std::vector<wav_t>::iterator finish = wv.end(); - std::vector<wav_t>::iterator found; - - wav_t w = { std::string(""), index }; - - if ((found = std::find(begin, finish, w)) != finish) { - return true; - } - return false; - } - - uint32_t sessionrate; - uint8_t version; - - unsigned char c0; - unsigned char c1; - unsigned char *ptfunxored; - int len; - -private: - bool foundin(std::string haystack, std::string needle); - void parse(void); - void parse8header(void); - void parse9header(void); - void parserest(void); - std::vector<wav_t> actualwavs; -}; - - -#endif |