2 Copyright (C) 2015 Damien Zammit
3 Copyright (C) 2015 Robin Gareus
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
17 #include "ptfformat.h"
25 static const uint32_t baselut[16] = {
26 0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955,
27 0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5,
28 0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d,
29 0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd
32 static const uint32_t xorlut[16] = {
33 0x00000000, 0x00000b00, 0x000100b0, 0x00b0b010,
34 0x010b0b01, 0x0b10b10b, 0x01bb101b, 0x0111bbbb,
35 0x1111bbbb, 0x1bbb10bb, 0x1bb0bb0b, 0xbb0b0bab,
36 0xbab0b0ba, 0xb0abaaba, 0xba0aabaa, 0xbaaaaaaa
39 static const uint32_t swapbytes32 (const uint32_t v) {
41 rv |= ((v >> 0) & 0xf) << 28;
42 rv |= ((v >> 4) & 0xf) << 24;
43 rv |= ((v >> 8) & 0xf) << 20;
44 rv |= ((v >> 12) & 0xf) << 16;
45 rv |= ((v >> 16) & 0xf) << 12;
46 rv |= ((v >> 20) & 0xf) << 8;
47 rv |= ((v >> 24) & 0xf) << 4;
48 rv |= ((v >> 28) & 0xf) << 0;
52 static const uint64_t gen_secret (int i) {
53 assert (i > 0 && i < 256);
54 int iwrap = i & 0x7f; // wrap at 0x80;
55 uint32_t xor_lo = 0; // 0x40 flag
56 int idx; // mirror at 0x40;
65 int i16 = (idx >> 1) & 0xf;
70 uint32_t lo = baselut [i16];
71 uint32_t xk = xorlut [i16];
77 uint32_t hi = swapbytes32 (lo) ^ xk;
78 return ((uint64_t)hi << 32) | (lo ^ xor_lo);
81 PTFFormat::PTFFormat() {
84 PTFFormat::~PTFFormat() {
91 PTFFormat::foundin(std::string haystack, std::string needle) {
92 size_t found = haystack.find(needle);
93 if (found != std::string::npos) {
100 /* Return values: 0 success
101 0x01 to 0xff value of missing lut
102 -1 could not open file as ptf
105 PTFFormat::load(std::string path) {
107 unsigned char xxor[256];
114 unsigned char message;
116 if (! (fp = fopen(path.c_str(), "rb"))) {
120 fseek(fp, 0, SEEK_END);
126 fseek(fp, 0x40, SEEK_SET);
127 fread(&c0, 1, 1, fp);
128 fread(&c1, 1, 1, fp);
130 if (! (ptfunxored = (unsigned char*) malloc(len * sizeof(unsigned char)))) {
131 /* Silently fail -- out of memory*/
139 fseek(fp, 0x0, SEEK_SET);
146 for (i = 2; i < 64; i++) {
147 xxor[i] = (xxor[i-1] + c1 - c0) & 0xff;
150 fread(&ct, 1, 1, fp);
152 ptfunxored[0] = message;
154 fread(&ct, 1, 1, fp);
156 ptfunxored[1] = message;
159 while (fread(&ct, 1, 1, fp) != 0) {
163 message = xxor[i] ^ ct;
164 ptfunxored[j] = message;
173 for (i = 2; i < 256; i++) {
177 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
180 for (i = 0; i < 64; i++) {
183 for (i = 128; i < 192; i++) {
187 fread(&ct, 1, 1, fp);
189 ptfunxored[0] = message;
191 fread(&ct, 1, 1, fp);
193 ptfunxored[1] = message;
196 while (fread(&ct, 1, 1, fp) != 0) {
200 message = xxor[i] ^ ct;
201 ptfunxored[j] = message;
210 for (i = 2; i < 256; i++) {
214 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
218 key = gen_secret(c1);
219 for (i = 0; i < 64; i++) {
220 xxor[i] ^= (((key >> i) & 1) * 2 * 0x40) + 0x40;
222 for (i = 128; i < 192; i++) {
223 inv = (((key >> (i-128)) & 1) == 1) ? 1 : 3;
224 xxor[i] ^= (inv * 0x40);
227 for (i = 192; i < 256; i++) {
231 fread(&ct, 1, 1, fp);
233 ptfunxored[0] = message;
235 fread(&ct, 1, 1, fp);
237 ptfunxored[1] = message;
240 while (fread(&ct, 1, 1, fp) != 0) {
244 message = xxor[i] ^ ct;
245 ptfunxored[j] = message;
252 //Should not happen, failed c[0] c[1]
262 PTFFormat::parse(void) {
263 this->version = ptfunxored[61];
265 if (this->version == 8) {
268 } else if (this->version == 9) {
277 PTFFormat::parse8header(void) {
280 // Find session sample rate
283 if ( (ptfunxored[k ] == 0x5a) &&
284 (ptfunxored[k+1] == 0x05)) {
290 this->sessionrate = 0;
291 this->sessionrate |= ptfunxored[k+11];
292 this->sessionrate |= ptfunxored[k+12] << 8;
293 this->sessionrate |= ptfunxored[k+13] << 16;
297 PTFFormat::parse9header(void) {
300 // Find session sample rate
303 if ( (ptfunxored[k ] == 0x5a) &&
304 (ptfunxored[k+1] == 0x06)) {
310 this->sessionrate = 0;
311 this->sessionrate |= ptfunxored[k+11];
312 this->sessionrate |= ptfunxored[k+12] << 8;
313 this->sessionrate |= ptfunxored[k+13] << 16;
317 PTFFormat::parserest(void) {
320 // Find end of wav file list
323 if ( (ptfunxored[k ] == 0xff) &&
324 (ptfunxored[k+1] == 0xff) &&
325 (ptfunxored[k+2] == 0xff) &&
326 (ptfunxored[k+3] == 0xff)) {
335 // Find actual wav names
337 uint16_t numberofwavs;
339 for (i = k; i > 4; i--) {
340 if ( (ptfunxored[i ] == 'W') &&
341 (ptfunxored[i-1] == 'A') &&
342 (ptfunxored[i-2] == 'V') &&
343 (ptfunxored[i-3] == 'E')) {
346 while (ptfunxored[j] != '\0') {
347 wavname[l] = ptfunxored[j];
352 //uint8_t playlist = ptfunxored[j-8];
356 for (j = k; j > 4; j--) {
357 if ( (ptfunxored[j ] == 0x01) &&
358 (ptfunxored[j-1] == 0x5a)) {
361 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
362 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
363 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
364 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
365 //printf("%d wavs\n", numberofwavs);
372 std::string wave = string(wavname);
373 std::reverse(wave.begin(), wave.end());
374 wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0 };
376 if (foundin(wave, string(".grp"))) {
380 actualwavs.push_back(f);
383 if (numberofwavs <= 0)
389 uint8_t startbytes = 0;
390 uint8_t lengthbytes = 0;
391 uint8_t offsetbytes = 0;
392 uint8_t somethingbytes = 0;
393 uint8_t skipbytes = 0;
396 if ( (ptfunxored[k ] == 'S') &&
397 (ptfunxored[k+1] == 'n') &&
398 (ptfunxored[k+2] == 'a') &&
399 (ptfunxored[k+3] == 'p')) {
407 for (i = k; i < len-70; i++) {
408 if ( (ptfunxored[i ] == 0x5a) &&
409 (ptfunxored[i+1] == 0x0a)) {
412 if ( (ptfunxored[i ] == 0x5a) &&
413 (ptfunxored[i+1] == 0x0c)) {
415 uint8_t lengthofname = ptfunxored[i+9];
417 char name[256] = {0};
418 for (j = 0; j < lengthofname; j++) {
419 name[j] = ptfunxored[i+13+j];
423 //uint8_t disabled = ptfunxored[j];
425 offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
426 lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
427 startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
428 somethingbytes = (ptfunxored[j+3] & 0xf);
429 skipbytes = ptfunxored[j+4];
430 findex = ptfunxored[j+5
437 /*rindex = ptfunxored[j+5
445 uint32_t sampleoffset = 0;
446 switch (offsetbytes) {
448 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
450 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
452 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
454 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
460 switch (lengthbytes) {
462 length |= (uint32_t)(ptfunxored[j+8] << 24);
464 length |= (uint32_t)(ptfunxored[j+7] << 16);
466 length |= (uint32_t)(ptfunxored[j+6] << 8);
468 length |= (uint32_t)(ptfunxored[j+5]);
474 switch (startbytes) {
476 start |= (uint32_t)(ptfunxored[j+8] << 24);
478 start |= (uint32_t)(ptfunxored[j+7] << 16);
480 start |= (uint32_t)(ptfunxored[j+6] << 8);
482 start |= (uint32_t)(ptfunxored[j+5]);
487 uint32_t something = 0;
488 switch (somethingbytes) {
490 something |= (uint32_t)(ptfunxored[j+8] << 24);
492 something |= (uint32_t)(ptfunxored[j+7] << 16);
494 something |= (uint32_t)(ptfunxored[j+6] << 8);
496 something |= (uint32_t)(ptfunxored[j+5]);
501 std::string filename = string(name) + ".wav";
510 //printf("something=%d\n", something);
512 vector<wav_t>::iterator begin = this->actualwavs.begin();
513 vector<wav_t>::iterator finish = this->actualwavs.end();
514 vector<wav_t>::iterator found;
515 // Add file to list only if it is an actual wav
516 if ((found = std::find(begin, finish, f)) != finish) {
517 this->audiofiles.push_back(f);
518 // Also add plain wav as region
527 this->regions.push_back(r);
530 if (foundin(filename, string(".grp"))) {
541 this->regions.push_back(r);
548 if ( (ptfunxored[k ] == 0x5a) &&
549 (ptfunxored[k+1] == 0x03)) {
555 if ( (ptfunxored[k ] == 0x5a) &&
556 (ptfunxored[k+1] == 0x02)) {
565 uint32_t tracknumber = 0;
566 uint32_t regionspertrack = 0;
567 for (;k < len; k++) {
568 if ( (ptfunxored[k ] == 0x5a) &&
569 (ptfunxored[k+1] == 0x04)) {
572 if ( (ptfunxored[k ] == 0x5a) &&
573 (ptfunxored[k+1] == 0x02)) {
575 uint8_t lengthofname = 0;
576 lengthofname = ptfunxored[k+9];
577 if (lengthofname == 0x5a) {
582 regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
584 //printf("regions/track=%d\n", regionspertrack);
585 char name[256] = {0};
586 for (j = 0; j < lengthofname; j++) {
587 name[j] = ptfunxored[j+k+13];
590 tr.name = string(name);
591 tr.index = tracknumber++;
593 for (j = k; regionspertrack > 0 && j < len; j++) {
594 for (l = j; l < len; l++) {
595 if ( (ptfunxored[l ] == 0x5a) &&
596 (ptfunxored[l+1] == 0x07)) {
603 if (regionspertrack == 0) {
604 // tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
608 tr.reg.index = (uint8_t)(ptfunxored[l+11]);
609 vector<region_t>::iterator begin = this->regions.begin();
610 vector<region_t>::iterator finish = this->regions.end();
611 vector<region_t>::iterator found;
612 if ((found = std::find(begin, finish, tr.reg)) != finish) {
615 startbytes = (ptfunxored[l+3] & 0xf0) >> 4;
619 switch (startbytes) {
621 offset |= (uint32_t)(ptfunxored[i+3] << 24);
623 offset |= (uint32_t)(ptfunxored[i+2] << 16);
625 offset |= (uint32_t)(ptfunxored[i+1] << 8);
627 offset |= (uint32_t)(ptfunxored[i]);
631 tr.reg.startpos = (int64_t)offset;
632 if (tr.reg.length > 0) {
633 this->tracks.push_back(tr);