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