ptformat: Decrypt ptf files exported from ptx sessions
[ardour.git] / libs / ptformat / ptfformat.cc
1 /*
2     Copyright (C) 2015  Damien Zammit
3     Copyright (C) 2015  Robin Gareus
4
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.
9
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.
14
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string>
20 #include <string.h>
21 #include <assert.h>
22
23 #include <glib/gstdio.h>
24
25 #include "ptfformat.h"
26
27 using namespace std;
28
29 static const uint32_t baselut[16] = {
30         0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955,
31         0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5,
32         0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d,
33         0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd
34 };
35
36 static const uint32_t xorlut[16] = {
37         0x00000000, 0x00000b00, 0x000100b0, 0x00b0b010,
38         0x010b0b01, 0x0b10b10b, 0x01bb101b, 0x0111bbbb,
39         0x1111bbbb, 0x1bbb10bb, 0x1bb0bb0b, 0xbb0b0bab,
40         0xbab0b0ba, 0xb0abaaba, 0xba0aabaa, 0xbaaaaaaa
41 };
42
43 static uint32_t swapbytes32 (const uint32_t v) {
44         uint32_t rv = 0;
45         rv |= ((v >>  0) & 0xf) << 28;
46         rv |= ((v >>  4) & 0xf) << 24;
47         rv |= ((v >>  8) & 0xf) << 20;
48         rv |= ((v >> 12) & 0xf) << 16;
49         rv |= ((v >> 16) & 0xf) << 12;
50         rv |= ((v >> 20) & 0xf) <<  8;
51         rv |= ((v >> 24) & 0xf) <<  4;
52         rv |= ((v >> 28) & 0xf) <<  0;
53         return rv;
54 }
55
56 static uint64_t gen_secret (int i) {
57         assert (i > 0 && i < 256);
58         int iwrap = i & 0x7f; // wrap at 0x80;
59         uint32_t xor_lo = 0;  // 0x40 flag
60         int idx;              // mirror at 0x40;
61
62         if (iwrap & 0x40) {
63                 xor_lo = 0x1;
64                 idx    = 0x80 - iwrap;
65         } else {
66                 idx    = iwrap;
67         }
68
69         int i16 = (idx >> 1) & 0xf;
70         if (idx & 0x20) {
71                 i16 = 15 - i16;
72         }
73
74         uint32_t lo = baselut [i16];
75         uint32_t xk = xorlut  [i16];
76
77         if (idx & 0x20) {
78                 lo ^= 0xaaaaaaab;
79                 xk ^= 0x10000000;
80         }
81         uint32_t hi = swapbytes32 (lo) ^ xk;
82         return  ((uint64_t)hi << 32) | (lo ^ xor_lo);
83 }
84
85 PTFFormat::PTFFormat() {
86 }
87
88 PTFFormat::~PTFFormat() {
89         if (ptfunxored) {
90                 free(ptfunxored);
91         }
92 }
93
94 bool
95 PTFFormat::foundin(std::string haystack, std::string needle) {
96         size_t found = haystack.find(needle);
97         if (found != std::string::npos) {
98                 return true;
99         } else {
100                 return false;
101         }
102 }
103
104 /* Return values:       0            success
105                         0x01 to 0xff value of missing lut
106                         -1           could not open file as ptf
107 */
108 int
109 PTFFormat::load(std::string path, int64_t targetsr) {
110         FILE *fp;
111         unsigned char xxor[256];
112         unsigned char ct;
113         unsigned char v;
114         unsigned char voff;
115         uint64_t key;
116         uint64_t i;
117         uint64_t j;
118         int inv;
119         int err;
120
121         if (! (fp = g_fopen(path.c_str(), "rb"))) {
122                 return -1;
123         }
124
125         fseek(fp, 0, SEEK_END);
126         len = ftell(fp);
127         if (len < 0x40) {
128                 fclose(fp);
129                 return -1;
130         }
131         fseek(fp, 0x40, SEEK_SET);
132         fread(&c0, 1, 1, fp);
133         fread(&c1, 1, 1, fp);
134
135         // For version <= 7 support:
136         version = c0 & 0x0f;
137         c0 = c0 & 0xc0;
138
139         if (! (ptfunxored = (unsigned char*) malloc(len * sizeof(unsigned char)))) {
140                 /* Silently fail -- out of memory*/
141                 fclose(fp);
142                 ptfunxored = 0;
143                 return -1;
144         }
145
146         switch (c0) {
147         case 0x00:
148                 // Success! easy one
149                 xxor[0] = c0;
150                 xxor[1] = c1;
151                 //fprintf(stderr, "0 %02x\n1 %02x\n", c0, c1);
152
153                 for (i = 2; i < 256; i++) {
154                         if (i%64 == 0) {
155                                 xxor[i] = c0;
156                         } else {
157                                 xxor[i] = (xxor[i-1] + c1 - c0) & 0xff;
158                                 //fprintf(stderr, "%x %02x\n", i, xxor[i]);
159                         }
160                 }
161                 break;
162         case 0x80:
163                 //Success! easy two
164                 xxor[0] = c0;
165                 xxor[1] = c1;
166                 for (i = 2; i < 256; i++) {
167                         if (i%64 == 0) {
168                                 xxor[i] = c0;
169                         } else {
170                                 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
171                         }
172                 }
173                 for (i = 0; i < 64; i++) {
174                         xxor[i] ^= 0x80;
175                 }
176                 for (i = 128; i < 192; i++) {
177                         xxor[i] ^= 0x80;
178                 }
179                 break;
180         case 0x40:
181         case 0xc0:
182                 xxor[0] = c0;
183                 xxor[1] = c1;
184                 for (i = 2; i < 256; i++) {
185                         if (i%64 == 0) {
186                                 xxor[i] = c0;
187                         } else {
188                                 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
189                         }
190                 }
191
192                 key = gen_secret(c1);
193
194                 for (i = 0; i < 64; i++) {
195                         xxor[i] ^= (((key >> i) & 1) * 2 * 0x40) + 0x40;
196                 }
197                 for (i = 128; i < 192; i++) {
198                         inv = (((key >> (i-128)) & 1) == 1) ? 1 : 3;
199                         xxor[i] ^= (inv * 0x40);
200                 }
201
202                 for (i = 192; i < 256; i++) {
203                         xxor[i] ^= 0x80;
204                 }
205                 break;
206                 break;
207         default:
208                 //Should not happen, failed c[0] c[1]
209                 return -1;
210                 break;
211         }
212
213         /* Read file */
214         i = 0;
215         fseek(fp, 0, SEEK_SET);
216         while (fread(&ct, 1, 1, fp) != 0) {
217                 ptfunxored[i++] = ct;
218         }
219         fclose(fp);
220
221         /* version detection */
222         voff = 0x36;
223         v = ptfunxored[voff];
224         if (v == 0x20) {
225                 voff += 7;
226         } else if (v == 0x03) {
227                 voff += 4;
228         } else {
229                 voff = 0;
230         }
231         v = ptfunxored[voff];
232         if (v == 10 || v == 11 || v == 12) {
233                 version = v;
234                 unxor10();
235         }
236
237         // Special case when ptx is exported to ptf in PT
238         if (v == 3) {
239                 version = 11;
240                 unxor_ptx_to_ptf();
241         }
242
243         if (version == 0 || version == 5 || version == 7) {
244                 /* Haven't detected version yet so decipher */
245                 j = 0;
246                 for (i = 0; i < len; i++) {
247                         if (j%256 == 0) {
248                                 j = 0;
249                         }
250                         ptfunxored[i] ^= xxor[j];
251                         j++;
252                 }
253
254                 /* version detection */
255                 voff = 0x36;
256                 v = ptfunxored[voff];
257                 if (v == 0x20) {
258                         voff += 7;
259                 } else if (v == 0x03) {
260                         voff += 4;
261                 } else {
262                         voff = 0;
263                 }
264                 v = ptfunxored[voff];
265                 if (v == 5 || v == 7 || v == 8 || v == 9) {
266                         version = v;
267                 }
268         }
269
270         if (version < 5 || version > 12)
271                 return -1;
272         targetrate = targetsr;
273         err = parse();
274         if (err)
275                 return -1;
276         return 0;
277 }
278
279 uint8_t
280 PTFFormat::mostfrequent(uint32_t start, uint32_t stop)
281 {
282         uint32_t counts[256] = {0};
283         uint64_t i;
284         uint32_t max = 0;
285         uint8_t maxi = 0;
286
287         for (i = start; i < stop; i++) {
288                 counts[ptfunxored[i]]++;
289         }
290
291         for (i = 0; i < 256; i++) {
292                 if (counts[i] > max) {
293                         maxi = i;
294                         max = counts[i];
295                 }
296         }
297         return maxi;
298 }
299
300 void
301 PTFFormat::unxor10(void)
302 {
303         uint64_t j;
304         uint8_t x = mostfrequent(0x1000, 0x2000);
305         uint8_t dx = 0x100-x;
306
307         for (j = 0x1000; j < len; j++) {
308                 if(j % 0x1000 == 0xfff) {
309                         x = (x - dx) & 0xff;
310                 }
311                 ptfunxored[j] ^= x;
312         }
313 }
314
315 void
316 PTFFormat::unxor_ptx_to_ptf(void)
317 {
318         unsigned char keyy[16] = {      0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,
319                                         0x80,0x90,0xa0,0xb0,0xc0,0xd0,0xe0,0xf0
320         };
321         uint64_t j;
322         uint8_t i;
323
324         for (i = 0, j = 0x10; j < len; j++,i++) {
325                 ptfunxored[j] ^= keyy[i];
326                 if ((j % 16) == 0) {
327                         i = 0;
328                         if (ptfunxored[j] % 2 == 0) {
329                                 ptfunxored[j]++;
330                         } else {
331                                 ptfunxored[j]--;
332                         }
333                 }
334         }
335 }
336
337 int
338 PTFFormat::parse(void) {
339         if (version == 5) {
340                 parse5header();
341                 setrates();
342                 if (sessionrate < 44100 || sessionrate > 192000)
343                   return -1;
344                 parseaudio5();
345                 parserest5();
346         } else if (version == 7) {
347                 parse7header();
348                 setrates();
349                 if (sessionrate < 44100 || sessionrate > 192000)
350                   return -1;
351                 parseaudio();
352                 parserest89();
353         } else if (version == 8) {
354                 parse8header();
355                 setrates();
356                 if (sessionrate < 44100 || sessionrate > 192000)
357                   return -1;
358                 parseaudio();
359                 parserest89();
360         } else if (version == 9) {
361                 parse9header();
362                 setrates();
363                 if (sessionrate < 44100 || sessionrate > 192000)
364                   return -1;
365                 parseaudio();
366                 parserest89();
367         } else if (version == 10 || version == 11 || version == 12) {
368                 parse10header();
369                 setrates();
370                 if (sessionrate < 44100 || sessionrate > 192000)
371                   return -1;
372                 parseaudio();
373                 parserest10();
374         } else {
375                 // Should not occur
376                 return -1;
377         }
378         return 0;
379 }
380
381 void
382 PTFFormat::setrates(void) {
383         ratefactor = 1.f;
384         if (sessionrate != 0) {
385                 ratefactor = (float)targetrate / sessionrate;
386         }
387 }
388
389 void
390 PTFFormat::parse5header(void) {
391         uint32_t k;
392
393         // Find session sample rate
394         k = 0x100;
395         while (k < len) {
396                 if (            (ptfunxored[k  ] == 0x5a) &&
397                                 (ptfunxored[k+1] == 0x00) &&
398                                 (ptfunxored[k+2] == 0x02)) {
399                         break;
400                 }
401                 k++;
402         }
403
404         sessionrate = 0;
405         sessionrate |= ptfunxored[k+12] << 16;
406         sessionrate |= ptfunxored[k+13] << 8;
407         sessionrate |= ptfunxored[k+14];
408 }
409
410 void
411 PTFFormat::parse7header(void) {
412         uint64_t k;
413
414         // Find session sample rate
415         k = 0x100;
416         while (k < len) {
417                 if (            (ptfunxored[k  ] == 0x5a) &&
418                                 (ptfunxored[k+1] == 0x00) &&
419                                 (ptfunxored[k+2] == 0x05)) {
420                         break;
421                 }
422                 k++;
423         }
424
425         sessionrate = 0;
426         sessionrate |= ptfunxored[k+12] << 16;
427         sessionrate |= ptfunxored[k+13] << 8;
428         sessionrate |= ptfunxored[k+14];
429 }
430
431 void
432 PTFFormat::parse8header(void) {
433         uint64_t k;
434
435         // Find session sample rate
436         k = 0;
437         while (k < len) {
438                 if (            (ptfunxored[k  ] == 0x5a) &&
439                                 (ptfunxored[k+1] == 0x05)) {
440                         break;
441                 }
442                 k++;
443         }
444
445         sessionrate = 0;
446         sessionrate |= ptfunxored[k+11];
447         sessionrate |= ptfunxored[k+12] << 8;
448         sessionrate |= ptfunxored[k+13] << 16;
449 }
450
451 void
452 PTFFormat::parse9header(void) {
453         uint64_t k;
454
455         // Find session sample rate
456         k = 0x100;
457         while (k < len) {
458                 if (            (ptfunxored[k  ] == 0x5a) &&
459                                 (ptfunxored[k+1] == 0x06)) {
460                         break;
461                 }
462                 k++;
463         }
464
465         sessionrate = 0;
466         sessionrate |= ptfunxored[k+11];
467         sessionrate |= ptfunxored[k+12] << 8;
468         sessionrate |= ptfunxored[k+13] << 16;
469 }
470
471 void
472 PTFFormat::parse10header(void) {
473         uint64_t k;
474
475         // Find session sample rate
476         k = 0x100;
477         while (k < len) {
478                 if (            (ptfunxored[k  ] == 0x5a) &&
479                                 (ptfunxored[k+1] == 0x09)) {
480                         break;
481                 }
482                 k++;
483         }
484
485         sessionrate = 0;
486         sessionrate |= ptfunxored[k+11];
487         sessionrate |= ptfunxored[k+12] << 8;
488         sessionrate |= ptfunxored[k+13] << 16;
489 }
490
491 void
492 PTFFormat::parserest5(void) {
493         uint64_t i, j, k;
494         uint64_t regionspertrack, lengthofname;
495         uint64_t startbytes, lengthbytes, offsetbytes;
496         uint16_t tracknumber = 0;
497         uint16_t findex;
498         uint16_t rindex;
499
500         k = 0;
501         for (i = 0; i < 5; i++) {
502                 while (k < len) {
503                         if (            (ptfunxored[k  ] == 0x5a) &&
504                                         (ptfunxored[k+1] == 0x00) &&
505                                         (ptfunxored[k+2] == 0x03)) {
506                                 break;
507                         }
508                         k++;
509                 }
510                 k++;
511         }
512         k--;
513
514         for (i = 0; i < 2; i++) {
515                 while (k) {
516                         if (            (ptfunxored[k  ] == 0x5a) &&
517                                         (ptfunxored[k+1] == 0x00) &&
518                                         (ptfunxored[k+2] == 0x01)) {
519                                 break;
520                         }
521                         k--;
522                 }
523                 if (k)
524                         k--;
525         }
526         k++;
527
528         rindex = 0;
529         while (k < len) {
530                 if (            (ptfunxored[k  ] == 0xff) &&
531                                 (ptfunxored[k+1] == 0xff)) {
532                         break;
533                 }
534                 while (k < len) {
535                         if (            (ptfunxored[k  ] == 0x5a) &&
536                                         (ptfunxored[k+1] == 0x00) &&
537                                         (ptfunxored[k+2] == 0x01)) {
538                                 break;
539                         }
540                         k++;
541                 }
542
543                 lengthofname = ptfunxored[k+12];
544                 if (ptfunxored[k+13] == 0x5a) {
545                         k++;
546                         break;
547                 }
548                 char name[256] = {0};
549                 for (j = 0; j < lengthofname; j++) {
550                         name[j] = ptfunxored[k+13+j];
551                 }
552                 name[j] = '\0';
553                 regionspertrack = ptfunxored[k+13+j+3];
554                 for (i = 0; i < regionspertrack; i++) {
555                         while (k < len) {
556                                 if (            (ptfunxored[k  ] == 0x5a) &&
557                                                 (ptfunxored[k+1] == 0x00) &&
558                                                 (ptfunxored[k+2] == 0x03)) {
559                                         break;
560                                 }
561                                 k++;
562                         }
563                         j = k+16;
564                         startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
565                         lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
566                         offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
567                         //somethingbytes = (ptfunxored[j+1] & 0xf);
568                         findex = ptfunxored[k+14];
569                         j--;
570                         uint32_t start = 0;
571                         switch (startbytes) {
572                         case 4:
573                                 start |= (uint32_t)(ptfunxored[j+8] << 24);
574                         case 3:
575                                 start |= (uint32_t)(ptfunxored[j+7] << 16);
576                         case 2:
577                                 start |= (uint32_t)(ptfunxored[j+6] << 8);
578                         case 1:
579                                 start |= (uint32_t)(ptfunxored[j+5]);
580                         default:
581                                 break;
582                         }
583                         j+=startbytes;
584                         uint32_t length = 0;
585                         switch (lengthbytes) {
586                         case 4:
587                                 length |= (uint32_t)(ptfunxored[j+8] << 24);
588                         case 3:
589                                 length |= (uint32_t)(ptfunxored[j+7] << 16);
590                         case 2:
591                                 length |= (uint32_t)(ptfunxored[j+6] << 8);
592                         case 1:
593                                 length |= (uint32_t)(ptfunxored[j+5]);
594                         default:
595                                 break;
596                         }
597                         j+=lengthbytes;
598                         uint32_t sampleoffset = 0;
599                         switch (offsetbytes) {
600                         case 4:
601                                 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
602                         case 3:
603                                 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
604                         case 2:
605                                 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
606                         case 1:
607                                 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
608                         default:
609                                 break;
610                         }
611                         j+=offsetbytes;
612
613                         //printf("name=`%s` start=%04x length=%04x offset=%04x findex=%d\n", name,start,length,sampleoffset,findex);
614
615                         std::string filename = string(name) + extension;
616                         wav_t f = {
617                                 filename,
618                                 findex,
619                                 (int64_t)(start*ratefactor),
620                                 (int64_t)(length*ratefactor),
621                         };
622
623                         vector<wav_t>::iterator begin = audiofiles.begin();
624                         vector<wav_t>::iterator finish = audiofiles.end();
625                         vector<wav_t>::iterator found;
626                         // Add file to lists
627                         if ((found = std::find(begin, finish, f)) != finish) {
628                                 region_t r = {
629                                         name,
630                                         rindex,
631                                         (int64_t)(start*ratefactor),
632                                         (int64_t)(sampleoffset*ratefactor),
633                                         (int64_t)(length*ratefactor),
634                                         *found,
635                                 };
636                                 regions.push_back(r);
637                                 vector<track_t>::iterator ti;
638                                 vector<track_t>::iterator bt = tracks.begin();
639                                 vector<track_t>::iterator et = tracks.end();
640                                 track_t tr = { name, 0, 0, r };
641                                 if ((ti = std::find(bt, et, tr)) != et) {
642                                         tracknumber = (*ti).index;
643                                 } else {
644                                         tracknumber = tracks.size() + 1;
645                                 }
646                                 track_t t = {
647                                         name,
648                                         (uint16_t)tracknumber,
649                                         uint8_t(0),
650                                         r
651                                 };
652                                 tracks.push_back(t);
653                         } else {
654                                 region_t r = {
655                                         name,
656                                         rindex,
657                                         (int64_t)(start*ratefactor),
658                                         (int64_t)(sampleoffset*ratefactor),
659                                         (int64_t)(length*ratefactor),
660                                         f,
661                                 };
662                                 regions.push_back(r);
663                                 vector<track_t>::iterator ti;
664                                 vector<track_t>::iterator bt = tracks.begin();
665                                 vector<track_t>::iterator et = tracks.end();
666                                 track_t tr = { name, 0, 0, r };
667                                 if ((ti = std::find(bt, et, tr)) != et) {
668                                         tracknumber = (*ti).index;
669                                 } else {
670                                         tracknumber = tracks.size() + 1;
671                                 }
672                                 track_t t = {
673                                         name,
674                                         (uint16_t)tracknumber,
675                                         uint8_t(0),
676                                         r
677                                 };
678                                 tracks.push_back(t);
679                         }
680                         rindex++;
681                         k++;
682                 }
683                 k++;
684         }
685 }
686
687 void
688 PTFFormat::resort(std::vector<wav_t>& ws) {
689         int j = 0;
690         std::sort(ws.begin(), ws.end());
691         for (std::vector<wav_t>::iterator i = ws.begin(); i != ws.end(); ++i) {
692                 (*i).index = j;
693                 j++;
694         }
695 }
696
697 void
698 PTFFormat::parseaudio5(void) {
699         uint64_t i,k,l;
700         uint64_t lengthofname, wavnumber;
701
702         // Find end of wav file list
703         k = 0;
704         while (k < len) {
705                 if (            (ptfunxored[k  ] == 0x5f) &&
706                                 (ptfunxored[k+1] == 0x50) &&
707                                 (ptfunxored[k+2] == 0x35)) {
708                         break;
709                 }
710                 k++;
711         }
712         k++;
713         while (k < len) {
714                 if (            (ptfunxored[k  ] == 0x5f) &&
715                                 (ptfunxored[k+1] == 0x50) &&
716                                 (ptfunxored[k+2] == 0x35)) {
717                         break;
718                 }
719                 k++;
720         }
721
722         // Find actual wav names
723         uint16_t numberofwavs = ptfunxored[k-23];
724         char wavname[256];
725         for (i = k; i < len; i++) {
726                 if (            (ptfunxored[i  ] == 'F') &&
727                                 (ptfunxored[i+1] == 'i') &&
728                                 (ptfunxored[i+2] == 'l') &&
729                                 (ptfunxored[i+3] == 'e') &&
730                                 (ptfunxored[i+4] == 's')) {
731                         break;
732                 }
733         }
734
735         wavnumber = 0;
736         i+=16;
737         char ext[5];
738         while (i < len && numberofwavs > 0) {
739                 i++;
740                 if (            (ptfunxored[i  ] == 0x5a) &&
741                                 (ptfunxored[i+1] == 0x00) &&
742                                 (ptfunxored[i+2] == 0x05)) {
743                         break;
744                 }
745                 lengthofname = ptfunxored[i];
746                 i++;
747                 l = 0;
748                 while (l < lengthofname) {
749                         wavname[l] = ptfunxored[i+l];
750                         l++;
751                 }
752                 i+=lengthofname;
753                 ext[0] = ptfunxored[i++];
754                 ext[1] = ptfunxored[i++];
755                 ext[2] = ptfunxored[i++];
756                 ext[3] = ptfunxored[i++];
757                 ext[4] = '\0';
758
759                 wavname[l] = 0;
760                 if (foundin(wavname, ".L") || foundin(wavname, ".R")) {
761                         extension = string("");
762                 } else if (foundin(wavname, ".wav") || foundin(ext, "WAVE")) {
763                         extension = string(".wav");
764                 } else if (foundin(wavname, ".aif") || foundin(ext, "AIFF")) {
765                         extension = string(".aif");
766                 } else {
767                         extension = string("");
768                 }
769
770                 std::string wave = string(wavname);
771                 wav_t f = { wave, (uint16_t)(wavnumber++), 0, 0 };
772
773                 if (foundin(wave, string(".grp"))) {
774                         continue;
775                 }
776
777                 actualwavs.push_back(f);
778                 audiofiles.push_back(f);
779                 //printf("done\n");
780                 numberofwavs--;
781                 i += 7;
782         }
783         resort(actualwavs);
784         resort(audiofiles);
785 }
786
787 void
788 PTFFormat::parseaudio(void) {
789         uint64_t i,j,k,l;
790
791         // Find end of wav file list
792         k = 0;
793         while (k < len) {
794                 if (            (ptfunxored[k  ] == 0xff) &&
795                                 (ptfunxored[k+1] == 0xff) &&
796                                 (ptfunxored[k+2] == 0xff) &&
797                                 (ptfunxored[k+3] == 0xff)) {
798                         break;
799                 }
800                 k++;
801         }
802
803         // Find actual wav names
804         bool first = true;
805         uint16_t numberofwavs;
806         char wavname[256];
807         for (i = k; i > 4; i--) {
808                 if (            ((ptfunxored[i  ] == 'W') || (ptfunxored[i  ] == 'A')) &&
809                                 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
810                                 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
811                                 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
812                         j = i-4;
813                         l = 0;
814                         while (ptfunxored[j] != '\0') {
815                                 wavname[l] = ptfunxored[j];
816                                 l++;
817                                 j--;
818                         }
819                         wavname[l] = 0;
820                         if (ptfunxored[i] == 'W') {
821                                 extension = string(".wav");
822                         } else {
823                                 extension = string(".aif");
824                         }
825                         //uint8_t playlist = ptfunxored[j-8];
826
827                         if (first) {
828                                 first = false;
829                                 for (j = k; j > 4; j--) {
830                                         if (    (ptfunxored[j  ] == 0x01) &&
831                                                 (ptfunxored[j-1] == 0x5a)) {
832
833                                                 numberofwavs = 0;
834                                                 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
835                                                 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
836                                                 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
837                                                 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
838                                                 //printf("%d wavs\n", numberofwavs);
839                                                 break;
840                                         }
841                                 k--;
842                                 }
843                         }
844
845                         std::string wave = string(wavname);
846                         std::reverse(wave.begin(), wave.end());
847                         wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
848
849                         if (foundin(wave, string(".grp"))) {
850                                 continue;
851                         }
852
853                         actualwavs.push_back(f);
854
855                         numberofwavs--;
856                         if (numberofwavs <= 0)
857                                 break;
858                 }
859         }
860 }
861
862 void
863 PTFFormat::parserest89(void) {
864         uint64_t i,j,k,l;
865         // Find Regions
866         uint8_t startbytes = 0;
867         uint8_t lengthbytes = 0;
868         uint8_t offsetbytes = 0;
869         uint8_t somethingbytes = 0;
870         uint8_t skipbytes = 0;
871
872         k = 0;
873         while (k < len) {
874                 if (            (ptfunxored[k  ] == 'S') &&
875                                 (ptfunxored[k+1] == 'n') &&
876                                 (ptfunxored[k+2] == 'a') &&
877                                 (ptfunxored[k+3] == 'p')) {
878                         break;
879                 }
880                 k++;
881         }
882         uint16_t rindex = 0;
883         uint32_t findex = 0;
884         for (i = k; i < len-70; i++) {
885                 if (            (ptfunxored[i  ] == 0x5a) &&
886                                 (ptfunxored[i+1] == 0x0a)) {
887                                 break;
888                 }
889                 if (            (ptfunxored[i  ] == 0x5a) &&
890                                 (ptfunxored[i+1] == 0x0c)) {
891
892                         uint8_t lengthofname = ptfunxored[i+9];
893
894                         char name[256] = {0};
895                         for (j = 0; j < lengthofname; j++) {
896                                 name[j] = ptfunxored[i+13+j];
897                         }
898                         name[j] = '\0';
899                         j += i+13;
900                         //uint8_t disabled = ptfunxored[j];
901
902                         offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
903                         lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
904                         startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
905                         somethingbytes = (ptfunxored[j+3] & 0xf);
906                         skipbytes = ptfunxored[j+4];
907                         findex = ptfunxored[j+5
908                                         +startbytes
909                                         +lengthbytes
910                                         +offsetbytes
911                                         +somethingbytes
912                                         +skipbytes
913                                         +40];
914                         /*rindex = ptfunxored[j+5
915                                         +startbytes
916                                         +lengthbytes
917                                         +offsetbytes
918                                         +somethingbytes
919                                         +skipbytes
920                                         +24];
921                         */
922                         uint32_t sampleoffset = 0;
923                         switch (offsetbytes) {
924                         case 4:
925                                 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
926                         case 3:
927                                 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
928                         case 2:
929                                 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
930                         case 1:
931                                 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
932                         default:
933                                 break;
934                         }
935                         j+=offsetbytes;
936                         uint32_t length = 0;
937                         switch (lengthbytes) {
938                         case 4:
939                                 length |= (uint32_t)(ptfunxored[j+8] << 24);
940                         case 3:
941                                 length |= (uint32_t)(ptfunxored[j+7] << 16);
942                         case 2:
943                                 length |= (uint32_t)(ptfunxored[j+6] << 8);
944                         case 1:
945                                 length |= (uint32_t)(ptfunxored[j+5]);
946                         default:
947                                 break;
948                         }
949                         j+=lengthbytes;
950                         uint32_t start = 0;
951                         switch (startbytes) {
952                         case 4:
953                                 start |= (uint32_t)(ptfunxored[j+8] << 24);
954                         case 3:
955                                 start |= (uint32_t)(ptfunxored[j+7] << 16);
956                         case 2:
957                                 start |= (uint32_t)(ptfunxored[j+6] << 8);
958                         case 1:
959                                 start |= (uint32_t)(ptfunxored[j+5]);
960                         default:
961                                 break;
962                         }
963                         j+=startbytes;
964                         /*
965                         uint32_t something = 0;
966                         switch (somethingbytes) {
967                         case 4:
968                                 something |= (uint32_t)(ptfunxored[j+8] << 24);
969                         case 3:
970                                 something |= (uint32_t)(ptfunxored[j+7] << 16);
971                         case 2:
972                                 something |= (uint32_t)(ptfunxored[j+6] << 8);
973                         case 1:
974                                 something |= (uint32_t)(ptfunxored[j+5]);
975                         default:
976                                 break;
977                         }
978                         j+=somethingbytes;
979                         */
980                         std::string filename = string(name) + extension;
981                         wav_t f = {
982                                 filename,
983                                 0,
984                                 (int64_t)(start*ratefactor),
985                                 (int64_t)(length*ratefactor),
986                         };
987
988                         f.index = findex;
989                         //printf("something=%d\n", something);
990
991                         vector<wav_t>::iterator begin = actualwavs.begin();
992                         vector<wav_t>::iterator finish = actualwavs.end();
993                         vector<wav_t>::iterator found;
994                         // Add file to list only if it is an actual wav
995                         if ((found = std::find(begin, finish, f)) != finish) {
996                                 audiofiles.push_back(f);
997                                 // Also add plain wav as region
998                                 region_t r = {
999                                         name,
1000                                         rindex,
1001                                         (int64_t)(start*ratefactor),
1002                                         (int64_t)(sampleoffset*ratefactor),
1003                                         (int64_t)(length*ratefactor),
1004                                         f
1005                                 };
1006                                 regions.push_back(r);
1007                         // Region only
1008                         } else {
1009                                 if (foundin(filename, string(".grp"))) {
1010                                         continue;
1011                                 }
1012                                 region_t r = {
1013                                         name,
1014                                         rindex,
1015                                         (int64_t)(start*ratefactor),
1016                                         (int64_t)(sampleoffset*ratefactor),
1017                                         (int64_t)(length*ratefactor),
1018                                         f
1019                                 };
1020                                 regions.push_back(r);
1021                         }
1022                         rindex++;
1023                 }
1024         }
1025
1026         while (k < len) {
1027                 if (            (ptfunxored[k  ] == 0x5a) &&
1028                                 (ptfunxored[k+1] == 0x03)) {
1029                                 break;
1030                 }
1031                 k++;
1032         }
1033         while (k < len) {
1034                 if (            (ptfunxored[k  ] == 0x5a) &&
1035                                 (ptfunxored[k+1] == 0x02)) {
1036                                 break;
1037                 }
1038                 k++;
1039         }
1040         k++;
1041
1042         //  Tracks
1043         uint32_t offset;
1044         uint32_t tracknumber = 0;
1045         uint32_t regionspertrack = 0;
1046         for (;k < len; k++) {
1047                 if (    (ptfunxored[k  ] == 0x5a) &&
1048                         (ptfunxored[k+1] == 0x04)) {
1049                         break;
1050                 }
1051                 if (    (ptfunxored[k  ] == 0x5a) &&
1052                         (ptfunxored[k+1] == 0x02)) {
1053
1054                         uint8_t lengthofname = 0;
1055                         lengthofname = ptfunxored[k+9];
1056                         if (lengthofname == 0x5a) {
1057                                 continue;
1058                         }
1059                         track_t tr;
1060
1061                         regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1062
1063                         //printf("regions/track=%d\n", regionspertrack);
1064                         char name[256] = {0};
1065                         for (j = 0; j < lengthofname; j++) {
1066                                 name[j] = ptfunxored[j+k+13];
1067                         }
1068                         name[j] = '\0';
1069                         tr.name = string(name);
1070                         tr.index = tracknumber++;
1071
1072                         for (j = k; regionspertrack > 0 && j < len; j++) {
1073                                 for (l = j; l < len; l++) {
1074                                         if (    (ptfunxored[l  ] == 0x5a) &&
1075                                                 (ptfunxored[l+1] == 0x07)) {
1076                                                 j = l;
1077                                                 break;
1078                                         }
1079                                 }
1080
1081
1082                                 if (regionspertrack == 0) {
1083                                 //      tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1084                                         break;
1085                                 } else {
1086
1087                                         tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1088                                         vector<region_t>::iterator begin = regions.begin();
1089                                         vector<region_t>::iterator finish = regions.end();
1090                                         vector<region_t>::iterator found;
1091                                         if ((found = std::find(begin, finish, tr.reg)) != finish) {
1092                                                 tr.reg = (*found);
1093                                         }
1094                                         i = l+16;
1095                                         offset = 0;
1096                                         offset |= (uint32_t)(ptfunxored[i+3] << 24);
1097                                         offset |= (uint32_t)(ptfunxored[i+2] << 16);
1098                                         offset |= (uint32_t)(ptfunxored[i+1] << 8);
1099                                         offset |= (uint32_t)(ptfunxored[i]);
1100                                         tr.reg.startpos = (int64_t)(offset*ratefactor);
1101                                         if (tr.reg.length > 0) {
1102                                                 tracks.push_back(tr);
1103                                         }
1104                                         regionspertrack--;
1105                                 }
1106                         }
1107                 }
1108         }
1109 }
1110
1111 void
1112 PTFFormat::parserest10(void) {
1113         uint64_t i,j,k,l;
1114         // Find Regions
1115         uint8_t startbytes = 0;
1116         uint8_t lengthbytes = 0;
1117         uint8_t offsetbytes = 0;
1118         uint8_t somethingbytes = 0;
1119         uint8_t skipbytes = 0;
1120
1121         k = 0;
1122         while (k < len) {
1123                 if (            (ptfunxored[k  ] == 'S') &&
1124                                 (ptfunxored[k+1] == 'n') &&
1125                                 (ptfunxored[k+2] == 'a') &&
1126                                 (ptfunxored[k+3] == 'p')) {
1127                         break;
1128                 }
1129                 k++;
1130         }
1131         for (i = k; i < len-70; i++) {
1132                 if (            (ptfunxored[i  ] == 0x5a) &&
1133                                 (ptfunxored[i+1] == 0x02)) {
1134                                 k = i;
1135                                 break;
1136                 }
1137         }
1138         k++;
1139         for (i = k; i < len-70; i++) {
1140                 if (            (ptfunxored[i  ] == 0x5a) &&
1141                                 (ptfunxored[i+1] == 0x02)) {
1142                                 k = i;
1143                                 break;
1144                 }
1145         }
1146         k++;
1147         uint16_t rindex = 0;
1148         uint32_t findex = 0;
1149         for (i = k; i < len-70; i++) {
1150                 if (            (ptfunxored[i  ] == 0x5a) &&
1151                                 (ptfunxored[i+1] == 0x08)) {
1152                                 break;
1153                 }
1154                 if (            (ptfunxored[i  ] == 0x5a) &&
1155                                 (ptfunxored[i+1] == 0x01)) {
1156
1157                         uint8_t lengthofname = ptfunxored[i+9];
1158                         if (ptfunxored[i+13] == 0x5a) {
1159                                 continue;
1160                         }
1161                         char name[256] = {0};
1162                         for (j = 0; j < lengthofname; j++) {
1163                                 name[j] = ptfunxored[i+13+j];
1164                         }
1165                         name[j] = '\0';
1166                         j += i+13;
1167                         //uint8_t disabled = ptfunxored[j];
1168                         //printf("%s\n", name);
1169
1170                         offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1171                         lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1172                         startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1173                         somethingbytes = (ptfunxored[j+3] & 0xf);
1174                         skipbytes = ptfunxored[j+4];
1175                         findex = ptfunxored[j+5
1176                                         +startbytes
1177                                         +lengthbytes
1178                                         +offsetbytes
1179                                         +somethingbytes
1180                                         +skipbytes
1181                                         +37];
1182                         /*rindex = ptfunxored[j+5
1183                                         +startbytes
1184                                         +lengthbytes
1185                                         +offsetbytes
1186                                         +somethingbytes
1187                                         +skipbytes
1188                                         +24];
1189                         */
1190                         uint32_t sampleoffset = 0;
1191                         switch (offsetbytes) {
1192                         case 4:
1193                                 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1194                         case 3:
1195                                 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1196                         case 2:
1197                                 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1198                         case 1:
1199                                 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1200                         default:
1201                                 break;
1202                         }
1203                         j+=offsetbytes;
1204                         uint32_t length = 0;
1205                         switch (lengthbytes) {
1206                         case 4:
1207                                 length |= (uint32_t)(ptfunxored[j+8] << 24);
1208                         case 3:
1209                                 length |= (uint32_t)(ptfunxored[j+7] << 16);
1210                         case 2:
1211                                 length |= (uint32_t)(ptfunxored[j+6] << 8);
1212                         case 1:
1213                                 length |= (uint32_t)(ptfunxored[j+5]);
1214                         default:
1215                                 break;
1216                         }
1217                         j+=lengthbytes;
1218                         uint32_t start = 0;
1219                         switch (startbytes) {
1220                         case 4:
1221                                 start |= (uint32_t)(ptfunxored[j+8] << 24);
1222                         case 3:
1223                                 start |= (uint32_t)(ptfunxored[j+7] << 16);
1224                         case 2:
1225                                 start |= (uint32_t)(ptfunxored[j+6] << 8);
1226                         case 1:
1227                                 start |= (uint32_t)(ptfunxored[j+5]);
1228                         default:
1229                                 break;
1230                         }
1231                         j+=startbytes;
1232                         /*
1233                         uint32_t something = 0;
1234                         switch (somethingbytes) {
1235                         case 4:
1236                                 something |= (uint32_t)(ptfunxored[j+8] << 24);
1237                         case 3:
1238                                 something |= (uint32_t)(ptfunxored[j+7] << 16);
1239                         case 2:
1240                                 something |= (uint32_t)(ptfunxored[j+6] << 8);
1241                         case 1:
1242                                 something |= (uint32_t)(ptfunxored[j+5]);
1243                         default:
1244                                 break;
1245                         }
1246                         j+=somethingbytes;
1247                         */
1248                         std::string filename = string(name) + extension;
1249                         wav_t f = {
1250                                 filename,
1251                                 0,
1252                                 (int64_t)(start*ratefactor),
1253                                 (int64_t)(length*ratefactor),
1254                         };
1255
1256                         if (strlen(name) == 0) {
1257                                 continue;
1258                         }
1259                         if (length == 0) {
1260                                 continue;
1261                         }
1262                         f.index = findex;
1263                         //printf("something=%d\n", something);
1264
1265                         vector<wav_t>::iterator begin = actualwavs.begin();
1266                         vector<wav_t>::iterator finish = actualwavs.end();
1267                         vector<wav_t>::iterator found;
1268                         // Add file to list only if it is an actual wav
1269                         if ((found = std::find(begin, finish, f)) != finish) {
1270                                 audiofiles.push_back(f);
1271                                 // Also add plain wav as region
1272                                 region_t r = {
1273                                         name,
1274                                         rindex,
1275                                         (int64_t)(start*ratefactor),
1276                                         (int64_t)(sampleoffset*ratefactor),
1277                                         (int64_t)(length*ratefactor),
1278                                         f
1279                                 };
1280                                 regions.push_back(r);
1281                         // Region only
1282                         } else {
1283                                 if (foundin(filename, string(".grp"))) {
1284                                         continue;
1285                                 }
1286                                 region_t r = {
1287                                         name,
1288                                         rindex,
1289                                         (int64_t)(start*ratefactor),
1290                                         (int64_t)(sampleoffset*ratefactor),
1291                                         (int64_t)(length*ratefactor),
1292                                         f
1293                                 };
1294                                 regions.push_back(r);
1295                         }
1296                         rindex++;
1297                         //printf("%s\n", name);
1298                 }
1299         }
1300         //  Tracks
1301         uint32_t offset;
1302         uint32_t tracknumber = 0;
1303         uint32_t regionspertrack = 0;
1304         for (;k < len; k++) {
1305                 if (    (ptfunxored[k  ] == 0x5a) &&
1306                         (ptfunxored[k+1] == 0x08)) {
1307                         break;
1308                 }
1309         }
1310         k++;
1311         for (;k < len; k++) {
1312                 if (    (ptfunxored[k  ] == 0x5a) &&
1313                         (ptfunxored[k+1] == 0x04)) {
1314                         break;
1315                 }
1316                 if (    (ptfunxored[k  ] == 0x5a) &&
1317                         (ptfunxored[k+1] == 0x02)) {
1318
1319                         uint8_t lengthofname = 0;
1320                         lengthofname = ptfunxored[k+9];
1321                         if (lengthofname == 0x5a) {
1322                                 continue;
1323                         }
1324                         track_t tr;
1325
1326                         regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1327
1328                         //printf("regions/track=%d\n", regionspertrack);
1329                         char name[256] = {0};
1330                         for (j = 0; j < lengthofname; j++) {
1331                                 name[j] = ptfunxored[j+k+13];
1332                         }
1333                         name[j] = '\0';
1334                         tr.name = string(name);
1335                         tr.index = tracknumber++;
1336
1337                         for (j = k; regionspertrack > 0 && j < len; j++) {
1338                                 for (l = j; l < len; l++) {
1339                                         if (    (ptfunxored[l  ] == 0x5a) &&
1340                                                 (ptfunxored[l+1] == 0x08)) {
1341                                                 j = l+1;
1342                                                 break;
1343                                         }
1344                                 }
1345
1346
1347                                 if (regionspertrack == 0) {
1348                                 //      tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1349                                         break;
1350                                 } else {
1351
1352                                         tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1353                                         vector<region_t>::iterator begin = regions.begin();
1354                                         vector<region_t>::iterator finish = regions.end();
1355                                         vector<region_t>::iterator found;
1356                                         if ((found = std::find(begin, finish, tr.reg)) != finish) {
1357                                                 tr.reg = (*found);
1358                                         }
1359                                         i = l+16;
1360                                         offset = 0;
1361                                         offset |= (uint32_t)(ptfunxored[i+3] << 24);
1362                                         offset |= (uint32_t)(ptfunxored[i+2] << 16);
1363                                         offset |= (uint32_t)(ptfunxored[i+1] << 8);
1364                                         offset |= (uint32_t)(ptfunxored[i]);
1365                                         tr.reg.startpos = (int64_t)(offset*ratefactor);
1366                                         if (tr.reg.length > 0) {
1367                                                 tracks.push_back(tr);
1368                                         }
1369                                         regionspertrack--;
1370                                 }
1371                         }
1372                 }
1373         }
1374 }