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