62fc0293c7736c7e36f8dbecf31b0dab3af8f7fd
[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 = {
599                                         name,
600                                         (uint16_t)tracknumber,
601                                         uint8_t(0),
602                                         r
603                                 };
604                                 tracks.push_back(t);
605                         } else {
606                                 region_t r = {
607                                         name,
608                                         rindex,
609                                         (int64_t)(start*ratefactor),
610                                         (int64_t)(sampleoffset*ratefactor),
611                                         (int64_t)(length*ratefactor),
612                                         f,
613                                 };
614                                 regions.push_back(r);
615                                 vector<track_t>::iterator ti;
616                                 vector<track_t>::iterator bt = tracks.begin();
617                                 vector<track_t>::iterator et = tracks.end();
618                                 track_t tr = { name, 0, 0, r };
619                                 if ((ti = std::find(bt, et, tr)) != et) {
620                                         tracknumber = (*ti).index;
621                                 } else {
622                                         tracknumber = tracks.size() + 1;
623                                 }
624                                 track_t t = {
625                                         name,
626                                         (uint16_t)tracknumber,
627                                         uint8_t(0),
628                                         r
629                                 };
630                                 tracks.push_back(t);
631                         }
632                         rindex++;
633                         k++;
634                 }
635                 k++;
636         }
637 }
638
639 void
640 PTFFormat::resort(std::vector<wav_t> *ws) {
641         int j = 0;
642         std::sort((*ws).begin(), (*ws).end());
643         for (std::vector<wav_t>::iterator i = (*ws).begin();
644                         i != (*ws).end(); ++i) {
645                 (*i).index = j;
646                 j++;
647         }
648 }
649
650 void
651 PTFFormat::parseaudio5(void) {
652         uint64_t i,k,l;
653         uint64_t lengthofname, wavnumber;
654
655         // Find end of wav file list
656         k = 0;
657         while (k < len) {
658                 if (            (ptfunxored[k  ] == 0x5f) &&
659                                 (ptfunxored[k+1] == 0x50) &&
660                                 (ptfunxored[k+2] == 0x35)) {
661                         break;
662                 }
663                 k++;
664         }
665         k++;
666         while (k < len) {
667                 if (            (ptfunxored[k  ] == 0x5f) &&
668                                 (ptfunxored[k+1] == 0x50) &&
669                                 (ptfunxored[k+2] == 0x35)) {
670                         break;
671                 }
672                 k++;
673         }
674
675         // Find actual wav names
676         uint16_t numberofwavs = ptfunxored[k-23];
677         char wavname[256];
678         for (i = k; i < len; i++) {
679                 if (            (ptfunxored[i  ] == 'F') &&
680                                 (ptfunxored[i+1] == 'i') &&
681                                 (ptfunxored[i+2] == 'l') &&
682                                 (ptfunxored[i+3] == 'e') &&
683                                 (ptfunxored[i+4] == 's')) {
684                         break;
685                 }
686         }
687
688         wavnumber = 0;
689         i+=16;
690         char ext[5];
691         while (i < len && numberofwavs > 0) {
692                 i++;
693                 if (            (ptfunxored[i  ] == 0x5a) &&
694                                 (ptfunxored[i+1] == 0x00) &&
695                                 (ptfunxored[i+2] == 0x05)) {
696                         break;
697                 }
698                 lengthofname = ptfunxored[i];
699                 i++;
700                 l = 0;
701                 while (l < lengthofname) {
702                         wavname[l] = ptfunxored[i+l];
703                         l++;
704                 }
705                 i+=lengthofname;
706                 ext[0] = ptfunxored[i++];
707                 ext[1] = ptfunxored[i++];
708                 ext[2] = ptfunxored[i++];
709                 ext[3] = ptfunxored[i++];
710                 ext[4] = '\0';
711
712                 wavname[l] = 0;
713                 if (foundin(wavname, ".L") || foundin(wavname, ".R")) {
714                         extension = string("");
715                 } else if (foundin(wavname, ".wav") || foundin(ext, "WAVE")) {
716                         extension = string(".wav");
717                 } else if (foundin(wavname, ".aif") || foundin(ext, "AIFF")) {
718                         extension = string(".aif");
719                 } else {
720                         extension = string("");
721                 }
722
723                 std::string wave = string(wavname);
724                 wav_t f = { wave, (uint16_t)(wavnumber++), 0, 0 };
725
726                 if (foundin(wave, string(".grp"))) {
727                         continue;
728                 }
729
730                 actualwavs.push_back(f);
731                 audiofiles.push_back(f);
732                 //printf("done\n");
733                 numberofwavs--;
734                 i += 7;
735         }
736         resort(&actualwavs);
737         resort(&audiofiles);
738 }
739
740 void
741 PTFFormat::parseaudio(void) {
742         uint64_t i,j,k,l;
743
744         // Find end of wav file list
745         k = 0;
746         while (k < len) {
747                 if (            (ptfunxored[k  ] == 0xff) &&
748                                 (ptfunxored[k+1] == 0xff) &&
749                                 (ptfunxored[k+2] == 0xff) &&
750                                 (ptfunxored[k+3] == 0xff)) {
751                         break;
752                 }
753                 k++;
754         }
755
756         // Find actual wav names
757         bool first = true;
758         uint16_t numberofwavs;
759         char wavname[256];
760         for (i = k; i > 4; i--) {
761                 if (            ((ptfunxored[i  ] == 'W') || (ptfunxored[i  ] == 'A')) &&
762                                 ((ptfunxored[i-1] == 'A') || (ptfunxored[i-1] == 'I')) &&
763                                 ((ptfunxored[i-2] == 'V') || (ptfunxored[i-2] == 'F')) &&
764                                 ((ptfunxored[i-3] == 'E') || (ptfunxored[i-3] == 'F'))) {
765                         j = i-4;
766                         l = 0;
767                         while (ptfunxored[j] != '\0') {
768                                 wavname[l] = ptfunxored[j];
769                                 l++;
770                                 j--;
771                         }
772                         wavname[l] = 0;
773                         if (ptfunxored[i] == 'W') {
774                                 extension = string(".wav");
775                         } else {
776                                 extension = string(".aif");
777                         }
778                         //uint8_t playlist = ptfunxored[j-8];
779
780                         if (first) {
781                                 first = false;
782                                 for (j = k; j > 4; j--) {
783                                         if (    (ptfunxored[j  ] == 0x01) &&
784                                                 (ptfunxored[j-1] == 0x5a)) {
785
786                                                 numberofwavs = 0;
787                                                 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
788                                                 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
789                                                 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
790                                                 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
791                                                 //printf("%d wavs\n", numberofwavs);
792                                                 break;
793                                         }
794                                 k--;
795                                 }
796                         }
797
798                         std::string wave = string(wavname);
799                         std::reverse(wave.begin(), wave.end());
800                         wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0, 0 };
801
802                         if (foundin(wave, string(".grp"))) {
803                                 continue;
804                         }
805
806                         actualwavs.push_back(f);
807
808                         numberofwavs--;
809                         if (numberofwavs <= 0)
810                                 break;
811                 }
812         }
813 }
814
815 void
816 PTFFormat::parserest89(void) {
817         uint64_t i,j,k,l;
818         // Find Regions
819         uint8_t startbytes = 0;
820         uint8_t lengthbytes = 0;
821         uint8_t offsetbytes = 0;
822         uint8_t somethingbytes = 0;
823         uint8_t skipbytes = 0;
824
825         k = 0;
826         while (k < len) {
827                 if (            (ptfunxored[k  ] == 'S') &&
828                                 (ptfunxored[k+1] == 'n') &&
829                                 (ptfunxored[k+2] == 'a') &&
830                                 (ptfunxored[k+3] == 'p')) {
831                         break;
832                 }
833                 k++;
834         }
835         uint16_t rindex = 0;
836         uint32_t findex = 0;
837         for (i = k; i < len-70; i++) {
838                 if (            (ptfunxored[i  ] == 0x5a) &&
839                                 (ptfunxored[i+1] == 0x0a)) {
840                                 break;
841                 }
842                 if (            (ptfunxored[i  ] == 0x5a) &&
843                                 (ptfunxored[i+1] == 0x0c)) {
844
845                         uint8_t lengthofname = ptfunxored[i+9];
846
847                         char name[256] = {0};
848                         for (j = 0; j < lengthofname; j++) {
849                                 name[j] = ptfunxored[i+13+j];
850                         }
851                         name[j] = '\0';
852                         j += i+13;
853                         //uint8_t disabled = ptfunxored[j];
854
855                         offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
856                         lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
857                         startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
858                         somethingbytes = (ptfunxored[j+3] & 0xf);
859                         skipbytes = ptfunxored[j+4];
860                         findex = ptfunxored[j+5
861                                         +startbytes
862                                         +lengthbytes
863                                         +offsetbytes
864                                         +somethingbytes
865                                         +skipbytes
866                                         +40];
867                         /*rindex = ptfunxored[j+5
868                                         +startbytes
869                                         +lengthbytes
870                                         +offsetbytes
871                                         +somethingbytes
872                                         +skipbytes
873                                         +24];
874                         */
875                         uint32_t sampleoffset = 0;
876                         switch (offsetbytes) {
877                         case 4:
878                                 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
879                         case 3:
880                                 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
881                         case 2:
882                                 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
883                         case 1:
884                                 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
885                         default:
886                                 break;
887                         }
888                         j+=offsetbytes;
889                         uint32_t length = 0;
890                         switch (lengthbytes) {
891                         case 4:
892                                 length |= (uint32_t)(ptfunxored[j+8] << 24);
893                         case 3:
894                                 length |= (uint32_t)(ptfunxored[j+7] << 16);
895                         case 2:
896                                 length |= (uint32_t)(ptfunxored[j+6] << 8);
897                         case 1:
898                                 length |= (uint32_t)(ptfunxored[j+5]);
899                         default:
900                                 break;
901                         }
902                         j+=lengthbytes;
903                         uint32_t start = 0;
904                         switch (startbytes) {
905                         case 4:
906                                 start |= (uint32_t)(ptfunxored[j+8] << 24);
907                         case 3:
908                                 start |= (uint32_t)(ptfunxored[j+7] << 16);
909                         case 2:
910                                 start |= (uint32_t)(ptfunxored[j+6] << 8);
911                         case 1:
912                                 start |= (uint32_t)(ptfunxored[j+5]);
913                         default:
914                                 break;
915                         }
916                         j+=startbytes;
917                         /*
918                         uint32_t something = 0;
919                         switch (somethingbytes) {
920                         case 4:
921                                 something |= (uint32_t)(ptfunxored[j+8] << 24);
922                         case 3:
923                                 something |= (uint32_t)(ptfunxored[j+7] << 16);
924                         case 2:
925                                 something |= (uint32_t)(ptfunxored[j+6] << 8);
926                         case 1:
927                                 something |= (uint32_t)(ptfunxored[j+5]);
928                         default:
929                                 break;
930                         }
931                         j+=somethingbytes;
932                         */
933                         std::string filename = string(name) + extension;
934                         wav_t f = {
935                                 filename,
936                                 0,
937                                 (int64_t)(start*ratefactor),
938                                 (int64_t)(length*ratefactor),
939                         };
940
941                         f.index = findex;
942                         //printf("something=%d\n", something);
943
944                         vector<wav_t>::iterator begin = actualwavs.begin();
945                         vector<wav_t>::iterator finish = actualwavs.end();
946                         vector<wav_t>::iterator found;
947                         // Add file to list only if it is an actual wav
948                         if ((found = std::find(begin, finish, f)) != finish) {
949                                 audiofiles.push_back(f);
950                                 // Also add plain wav as region
951                                 region_t r = {
952                                         name,
953                                         rindex,
954                                         (int64_t)(start*ratefactor),
955                                         (int64_t)(sampleoffset*ratefactor),
956                                         (int64_t)(length*ratefactor),
957                                         f
958                                 };
959                                 regions.push_back(r);
960                         // Region only
961                         } else {
962                                 if (foundin(filename, string(".grp"))) {
963                                         continue;
964                                 }
965                                 region_t r = {
966                                         name,
967                                         rindex,
968                                         (int64_t)(start*ratefactor),
969                                         (int64_t)(sampleoffset*ratefactor),
970                                         (int64_t)(length*ratefactor),
971                                         f
972                                 };
973                                 regions.push_back(r);
974                         }
975                         rindex++;
976                 }
977         }
978
979         while (k < len) {
980                 if (            (ptfunxored[k  ] == 0x5a) &&
981                                 (ptfunxored[k+1] == 0x03)) {
982                                 break;
983                 }
984                 k++;
985         }
986         while (k < len) {
987                 if (            (ptfunxored[k  ] == 0x5a) &&
988                                 (ptfunxored[k+1] == 0x02)) {
989                                 break;
990                 }
991                 k++;
992         }
993         k++;
994
995         //  Tracks
996         uint32_t offset;
997         uint32_t tracknumber = 0;
998         uint32_t regionspertrack = 0;
999         for (;k < len; k++) {
1000                 if (    (ptfunxored[k  ] == 0x5a) &&
1001                         (ptfunxored[k+1] == 0x04)) {
1002                         break;
1003                 }
1004                 if (    (ptfunxored[k  ] == 0x5a) &&
1005                         (ptfunxored[k+1] == 0x02)) {
1006
1007                         uint8_t lengthofname = 0;
1008                         lengthofname = ptfunxored[k+9];
1009                         if (lengthofname == 0x5a) {
1010                                 continue;
1011                         }
1012                         track_t tr;
1013
1014                         regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1015
1016                         //printf("regions/track=%d\n", regionspertrack);
1017                         char name[256] = {0};
1018                         for (j = 0; j < lengthofname; j++) {
1019                                 name[j] = ptfunxored[j+k+13];
1020                         }
1021                         name[j] = '\0';
1022                         tr.name = string(name);
1023                         tr.index = tracknumber++;
1024
1025                         for (j = k; regionspertrack > 0 && j < len; j++) {
1026                                 for (l = j; l < len; l++) {
1027                                         if (    (ptfunxored[l  ] == 0x5a) &&
1028                                                 (ptfunxored[l+1] == 0x07)) {
1029                                                 j = l;
1030                                                 break;
1031                                         }
1032                                 }
1033
1034
1035                                 if (regionspertrack == 0) {
1036                                 //      tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1037                                         break;
1038                                 } else {
1039
1040                                         tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1041                                         vector<region_t>::iterator begin = regions.begin();
1042                                         vector<region_t>::iterator finish = regions.end();
1043                                         vector<region_t>::iterator found;
1044                                         if ((found = std::find(begin, finish, tr.reg)) != finish) {
1045                                                 tr.reg = (*found);
1046                                         }
1047                                         i = l+16;
1048                                         offset = 0;
1049                                         offset |= (uint32_t)(ptfunxored[i+3] << 24);
1050                                         offset |= (uint32_t)(ptfunxored[i+2] << 16);
1051                                         offset |= (uint32_t)(ptfunxored[i+1] << 8);
1052                                         offset |= (uint32_t)(ptfunxored[i]);
1053                                         tr.reg.startpos = (int64_t)(offset*ratefactor);
1054                                         if (tr.reg.length > 0) {
1055                                                 tracks.push_back(tr);
1056                                         }
1057                                         regionspertrack--;
1058                                 }
1059                         }
1060                 }
1061         }
1062 }
1063
1064 void
1065 PTFFormat::parserest10(void) {
1066         uint64_t i,j,k,l;
1067         // Find Regions
1068         uint8_t startbytes = 0;
1069         uint8_t lengthbytes = 0;
1070         uint8_t offsetbytes = 0;
1071         uint8_t somethingbytes = 0;
1072         uint8_t skipbytes = 0;
1073
1074         k = 0;
1075         while (k < len) {
1076                 if (            (ptfunxored[k  ] == 'S') &&
1077                                 (ptfunxored[k+1] == 'n') &&
1078                                 (ptfunxored[k+2] == 'a') &&
1079                                 (ptfunxored[k+3] == 'p')) {
1080                         break;
1081                 }
1082                 k++;
1083         }
1084         for (i = k; i < len-70; i++) {
1085                 if (            (ptfunxored[i  ] == 0x5a) &&
1086                                 (ptfunxored[i+1] == 0x02)) {
1087                                 k = i;
1088                                 break;
1089                 }
1090         }
1091         k++;
1092         for (i = k; i < len-70; i++) {
1093                 if (            (ptfunxored[i  ] == 0x5a) &&
1094                                 (ptfunxored[i+1] == 0x02)) {
1095                                 k = i;
1096                                 break;
1097                 }
1098         }
1099         k++;
1100         uint16_t rindex = 0;
1101         uint32_t findex = 0;
1102         for (i = k; i < len-70; i++) {
1103                 if (            (ptfunxored[i  ] == 0x5a) &&
1104                                 (ptfunxored[i+1] == 0x08)) {
1105                                 break;
1106                 }
1107                 if (            (ptfunxored[i  ] == 0x5a) &&
1108                                 (ptfunxored[i+1] == 0x01)) {
1109
1110                         uint8_t lengthofname = ptfunxored[i+9];
1111                         if (ptfunxored[i+13] == 0x5a) {
1112                                 continue;
1113                         }
1114                         char name[256] = {0};
1115                         for (j = 0; j < lengthofname; j++) {
1116                                 name[j] = ptfunxored[i+13+j];
1117                         }
1118                         name[j] = '\0';
1119                         j += i+13;
1120                         //uint8_t disabled = ptfunxored[j];
1121                         //printf("%s\n", name);
1122
1123                         offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
1124                         lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
1125                         startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
1126                         somethingbytes = (ptfunxored[j+3] & 0xf);
1127                         skipbytes = ptfunxored[j+4];
1128                         findex = ptfunxored[j+5
1129                                         +startbytes
1130                                         +lengthbytes
1131                                         +offsetbytes
1132                                         +somethingbytes
1133                                         +skipbytes
1134                                         +37];
1135                         /*rindex = ptfunxored[j+5
1136                                         +startbytes
1137                                         +lengthbytes
1138                                         +offsetbytes
1139                                         +somethingbytes
1140                                         +skipbytes
1141                                         +24];
1142                         */
1143                         uint32_t sampleoffset = 0;
1144                         switch (offsetbytes) {
1145                         case 4:
1146                                 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
1147                         case 3:
1148                                 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
1149                         case 2:
1150                                 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
1151                         case 1:
1152                                 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
1153                         default:
1154                                 break;
1155                         }
1156                         j+=offsetbytes;
1157                         uint32_t length = 0;
1158                         switch (lengthbytes) {
1159                         case 4:
1160                                 length |= (uint32_t)(ptfunxored[j+8] << 24);
1161                         case 3:
1162                                 length |= (uint32_t)(ptfunxored[j+7] << 16);
1163                         case 2:
1164                                 length |= (uint32_t)(ptfunxored[j+6] << 8);
1165                         case 1:
1166                                 length |= (uint32_t)(ptfunxored[j+5]);
1167                         default:
1168                                 break;
1169                         }
1170                         j+=lengthbytes;
1171                         uint32_t start = 0;
1172                         switch (startbytes) {
1173                         case 4:
1174                                 start |= (uint32_t)(ptfunxored[j+8] << 24);
1175                         case 3:
1176                                 start |= (uint32_t)(ptfunxored[j+7] << 16);
1177                         case 2:
1178                                 start |= (uint32_t)(ptfunxored[j+6] << 8);
1179                         case 1:
1180                                 start |= (uint32_t)(ptfunxored[j+5]);
1181                         default:
1182                                 break;
1183                         }
1184                         j+=startbytes;
1185                         /*
1186                         uint32_t something = 0;
1187                         switch (somethingbytes) {
1188                         case 4:
1189                                 something |= (uint32_t)(ptfunxored[j+8] << 24);
1190                         case 3:
1191                                 something |= (uint32_t)(ptfunxored[j+7] << 16);
1192                         case 2:
1193                                 something |= (uint32_t)(ptfunxored[j+6] << 8);
1194                         case 1:
1195                                 something |= (uint32_t)(ptfunxored[j+5]);
1196                         default:
1197                                 break;
1198                         }
1199                         j+=somethingbytes;
1200                         */
1201                         std::string filename = string(name) + extension;
1202                         wav_t f = {
1203                                 filename,
1204                                 0,
1205                                 (int64_t)(start*ratefactor),
1206                                 (int64_t)(length*ratefactor),
1207                         };
1208
1209                         if (strlen(name) == 0) {
1210                                 continue;
1211                         }
1212                         if (length == 0) {
1213                                 continue;
1214                         }
1215                         f.index = findex;
1216                         //printf("something=%d\n", something);
1217
1218                         vector<wav_t>::iterator begin = actualwavs.begin();
1219                         vector<wav_t>::iterator finish = actualwavs.end();
1220                         vector<wav_t>::iterator found;
1221                         // Add file to list only if it is an actual wav
1222                         if ((found = std::find(begin, finish, f)) != finish) {
1223                                 audiofiles.push_back(f);
1224                                 // Also add plain wav as region
1225                                 region_t r = {
1226                                         name,
1227                                         rindex,
1228                                         (int64_t)(start*ratefactor),
1229                                         (int64_t)(sampleoffset*ratefactor),
1230                                         (int64_t)(length*ratefactor),
1231                                         f
1232                                 };
1233                                 regions.push_back(r);
1234                         // Region only
1235                         } else {
1236                                 if (foundin(filename, string(".grp"))) {
1237                                         continue;
1238                                 }
1239                                 region_t r = {
1240                                         name,
1241                                         rindex,
1242                                         (int64_t)(start*ratefactor),
1243                                         (int64_t)(sampleoffset*ratefactor),
1244                                         (int64_t)(length*ratefactor),
1245                                         f
1246                                 };
1247                                 regions.push_back(r);
1248                         }
1249                         rindex++;
1250                         //printf("%s\n", name);
1251                 }
1252         }
1253         //  Tracks
1254         uint32_t offset;
1255         uint32_t tracknumber = 0;
1256         uint32_t regionspertrack = 0;
1257         for (;k < len; k++) {
1258                 if (    (ptfunxored[k  ] == 0x5a) &&
1259                         (ptfunxored[k+1] == 0x08)) {
1260                         break;
1261                 }
1262         }
1263         k++;
1264         for (;k < len; k++) {
1265                 if (    (ptfunxored[k  ] == 0x5a) &&
1266                         (ptfunxored[k+1] == 0x04)) {
1267                         break;
1268                 }
1269                 if (    (ptfunxored[k  ] == 0x5a) &&
1270                         (ptfunxored[k+1] == 0x02)) {
1271
1272                         uint8_t lengthofname = 0;
1273                         lengthofname = ptfunxored[k+9];
1274                         if (lengthofname == 0x5a) {
1275                                 continue;
1276                         }
1277                         track_t tr;
1278
1279                         regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
1280
1281                         //printf("regions/track=%d\n", regionspertrack);
1282                         char name[256] = {0};
1283                         for (j = 0; j < lengthofname; j++) {
1284                                 name[j] = ptfunxored[j+k+13];
1285                         }
1286                         name[j] = '\0';
1287                         tr.name = string(name);
1288                         tr.index = tracknumber++;
1289
1290                         for (j = k; regionspertrack > 0 && j < len; j++) {
1291                                 for (l = j; l < len; l++) {
1292                                         if (    (ptfunxored[l  ] == 0x5a) &&
1293                                                 (ptfunxored[l+1] == 0x08)) {
1294                                                 j = l+1;
1295                                                 break;
1296                                         }
1297                                 }
1298
1299
1300                                 if (regionspertrack == 0) {
1301                                 //      tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
1302                                         break;
1303                                 } else {
1304
1305                                         tr.reg.index = (uint8_t)(ptfunxored[l+11]);
1306                                         vector<region_t>::iterator begin = regions.begin();
1307                                         vector<region_t>::iterator finish = regions.end();
1308                                         vector<region_t>::iterator found;
1309                                         if ((found = std::find(begin, finish, tr.reg)) != finish) {
1310                                                 tr.reg = (*found);
1311                                         }
1312                                         i = l+16;
1313                                         offset = 0;
1314                                         offset |= (uint32_t)(ptfunxored[i+3] << 24);
1315                                         offset |= (uint32_t)(ptfunxored[i+2] << 16);
1316                                         offset |= (uint32_t)(ptfunxored[i+1] << 8);
1317                                         offset |= (uint32_t)(ptfunxored[i]);
1318                                         tr.reg.startpos = (int64_t)(offset*ratefactor);
1319                                         if (tr.reg.length > 0) {
1320                                                 tracks.push_back(tr);
1321                                         }
1322                                         regionspertrack--;
1323                                 }
1324                         }
1325                 }
1326         }
1327 }