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