dde7367c6c16bb005085bf0ab45c3e2b2496cd26
[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 <assert.h>
22
23 using namespace std;
24
25 static const uint32_t baselut[16] = {
26         0xaaaaaaaa, 0xaa955555, 0xa9554aaa, 0xa552a955,
27         0xb56ad5aa, 0x95a95a95, 0x94a5294a, 0x9696b4b5,
28         0xd2d25a5a, 0xd24b6d25, 0xdb6db6da, 0xd9249b6d,
29         0xc9b64d92, 0xcd93264d, 0xccd99b32, 0xcccccccd
30 };
31
32 static const uint32_t xorlut[16] = {
33         0x00000000, 0x00000b00, 0x000100b0, 0x00b0b010,
34         0x010b0b01, 0x0b10b10b, 0x01bb101b, 0x0111bbbb,
35         0x1111bbbb, 0x1bbb10bb, 0x1bb0bb0b, 0xbb0b0bab,
36         0xbab0b0ba, 0xb0abaaba, 0xba0aabaa, 0xbaaaaaaa
37 };
38
39 static const uint32_t swapbytes32 (const uint32_t v) {
40         uint32_t rv = 0;
41         rv |= ((v >>  0) & 0xf) << 28;
42         rv |= ((v >>  4) & 0xf) << 24;
43         rv |= ((v >>  8) & 0xf) << 20;
44         rv |= ((v >> 12) & 0xf) << 16;
45         rv |= ((v >> 16) & 0xf) << 12;
46         rv |= ((v >> 20) & 0xf) <<  8;
47         rv |= ((v >> 24) & 0xf) <<  4;
48         rv |= ((v >> 28) & 0xf) <<  0;
49         return rv;
50 }
51
52 static const uint64_t gen_secret (int i) {
53         assert (i > 0 && i < 256);
54         int iwrap = i & 0x7f; // wrap at 0x80;
55         uint32_t xor_lo = 0;  // 0x40 flag
56         int idx;              // mirror at 0x40;
57
58         if (iwrap & 0x40) {
59                 xor_lo = 0x1;
60                 idx    = 0x80 - iwrap;
61         } else {
62                 idx    = iwrap;
63         }
64
65         int i16 = (idx >> 1) & 0xf;
66         if (idx & 0x20) {
67                 i16 = 15 - i16;
68         }
69
70         uint32_t lo = baselut [i16];
71         uint32_t xk = xorlut  [i16];
72
73         if (idx & 0x20) {
74                 lo ^= 0xaaaaaaab;
75                 xk ^= 0x10000000; 
76         }
77         uint32_t hi = swapbytes32 (lo) ^ xk;
78         return  ((uint64_t)hi << 32) | (lo ^ xor_lo);
79 }
80
81 PTFFormat::PTFFormat() {
82 }
83
84 PTFFormat::~PTFFormat() {
85         if (ptfunxored) {
86                 free(ptfunxored);
87         }
88 }
89
90 bool
91 PTFFormat::foundin(std::string haystack, std::string needle) {
92         size_t found = haystack.find(needle);
93         if (found != std::string::npos) {
94                 return true;
95         } else {
96                 return false;
97         }
98 }
99
100 /* Return values:       0            success
101                         0x01 to 0xff value of missing lut
102                         -1           could not open file as ptf
103 */
104 int
105 PTFFormat::load(std::string path) {
106         FILE *fp;
107         unsigned char xxor[256];
108         unsigned char ct;
109         unsigned char px;
110         uint64_t key;
111         uint16_t i;
112         int j;
113         int inv;
114         unsigned char message;
115
116         if (! (fp = fopen(path.c_str(), "rb"))) {
117                 return -1;
118         }
119
120         fseek(fp, 0, SEEK_END);
121         len = ftell(fp);
122         if (len < 0x40) {
123                 fclose(fp);
124                 return -1;
125         }
126         fseek(fp, 0x40, SEEK_SET);
127         fread(&c0, 1, 1, fp);
128         fread(&c1, 1, 1, fp);
129
130         if (! (ptfunxored = (unsigned char*) malloc(len * sizeof(unsigned char)))) {
131                 /* Silently fail -- out of memory*/
132                 fclose(fp);
133                 ptfunxored = 0;
134                 return -1;
135         }
136
137         i = 2;
138
139         fseek(fp, 0x0, SEEK_SET);
140
141         switch (c0) {
142         case 0x00:
143                 // Success! easy one
144                 xxor[0] = c0;
145                 xxor[1] = c1;
146                 for (i = 2; i < 64; i++) {
147                         xxor[i] = (xxor[i-1] + c1 - c0) & 0xff;
148                 }
149                 px = xxor[0];
150                 fread(&ct, 1, 1, fp);
151                 message = px ^ ct;
152                 ptfunxored[0] = message;
153                 px  = xxor[1];
154                 fread(&ct, 1, 1, fp);
155                 message = px ^ ct;
156                 ptfunxored[1] = message;
157                 i = 2;
158                 j = 2;
159                 while (fread(&ct, 1, 1, fp) != 0) {
160                         if (i%64 == 0) {
161                                 i = 0;
162                         }
163                         message = xxor[i] ^ ct;
164                         ptfunxored[j] = message;
165                         i++;
166                         j++;
167                 }
168                 break;
169         case 0x80:
170                 //Success! easy two
171                 xxor[0] = c0;
172                 xxor[1] = c1;
173                 for (i = 2; i < 256; i++) {
174                         if (i%64 == 0) {
175                                 xxor[i] = c0;
176                         } else {
177                                 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
178                         }
179                 }
180                 for (i = 0; i < 64; i++) {
181                         xxor[i] ^= 0x80;
182                 }
183                 for (i = 128; i < 192; i++) {
184                         xxor[i] ^= 0x80;
185                 }
186                 px = xxor[0];
187                 fread(&ct, 1, 1, fp);
188                 message = px ^ ct;
189                 ptfunxored[0] = message;
190                 px  = xxor[1];
191                 fread(&ct, 1, 1, fp);
192                 message = px ^ ct;
193                 ptfunxored[1] = message;
194                 i = 2;
195                 j = 2;
196                 while (fread(&ct, 1, 1, fp) != 0) {
197                         if (i%256 == 0) {
198                                 i = 0;
199                         }
200                         message = xxor[i] ^ ct;
201                         ptfunxored[j] = message;
202                         i++;
203                         j++;
204                 }
205                 break;
206         case 0x40:
207         case 0xc0:
208                 xxor[0] = c0;
209                 xxor[1] = c1;
210                 for (i = 2; i < 256; i++) {
211                         if (i%64 == 0) {
212                                 xxor[i] = c0;
213                         } else {
214                                 xxor[i] = ((xxor[i-1] + c1 - c0) & 0xff);
215                         }
216                 }
217
218                 key = gen_secret(c1);
219                 for (i = 0; i < 64; i++) {
220                         xxor[i] ^= (((key >> i) & 1) * 2 * 0x40) + 0x40;
221                 }
222                 for (i = 128; i < 192; i++) {
223                         inv = (((key >> (i-128)) & 1) == 1) ? 1 : 3;
224                         xxor[i] ^= (inv * 0x40);
225                 }
226                 
227                 for (i = 192; i < 256; i++) {
228                         xxor[i] ^= 0x80;
229                 }
230                 px = xxor[0];
231                 fread(&ct, 1, 1, fp);
232                 message = px ^ ct;
233                 ptfunxored[0] = message;
234                 px  = xxor[1];
235                 fread(&ct, 1, 1, fp);
236                 message = px ^ ct;
237                 ptfunxored[1] = message;
238                 i = 2;
239                 j = 2;
240                 while (fread(&ct, 1, 1, fp) != 0) {
241                         if (i%256 == 0) {
242                                 i = 0;
243                         }
244                         message = xxor[i] ^ ct;
245                         ptfunxored[j] = message;
246                         i++;
247                         j++;
248                 }
249                 break;
250                 break;
251         default:
252                 //Should not happen, failed c[0] c[1]
253                 return -1;
254                 break;
255         }
256         fclose(fp);
257         parse();
258         return 0;
259 }
260
261 void
262 PTFFormat::parse(void) {
263         this->version = ptfunxored[61];
264
265         if (this->version == 8) {
266                 parse8header();
267                 parserest();
268         } else if (this->version == 9) {
269                 parse9header();
270                 parserest();
271         } else {
272                 // Should not occur
273         }       
274 }
275
276 void
277 PTFFormat::parse8header(void) {
278         int k;
279
280         // Find session sample rate
281         k = 0;
282         while (k < len) {
283                 if (            (ptfunxored[k  ] == 0x5a) &&
284                                 (ptfunxored[k+1] == 0x05)) {
285                         break;
286                 }
287                 k++;
288         }
289
290         this->sessionrate = 0;
291         this->sessionrate |= ptfunxored[k+11];
292         this->sessionrate |= ptfunxored[k+12] << 8;
293         this->sessionrate |= ptfunxored[k+13] << 16;
294 }
295
296 void
297 PTFFormat::parse9header(void) {
298         int k;
299
300         // Find session sample rate
301         k = 0x100;
302         while (k < len) {
303                 if (            (ptfunxored[k  ] == 0x5a) &&
304                                 (ptfunxored[k+1] == 0x06)) {
305                         break;
306                 }
307                 k++;
308         }
309
310         this->sessionrate = 0;
311         this->sessionrate |= ptfunxored[k+11];
312         this->sessionrate |= ptfunxored[k+12] << 8;
313         this->sessionrate |= ptfunxored[k+13] << 16;
314 }
315
316 void
317 PTFFormat::parserest(void) {
318         int i,j,k,l;
319         
320         // Find end of wav file list
321         k = 0;
322         while (k < len) {
323                 if (            (ptfunxored[k  ] == 0xff) &&
324                                 (ptfunxored[k+1] == 0xff) &&
325                                 (ptfunxored[k+2] == 0xff) &&
326                                 (ptfunxored[k+3] == 0xff)) {
327                         break;
328                 }
329                 k++;
330         }
331
332         j = 0;
333         l = 0;
334
335         // Find actual wav names
336         bool first = true;
337         uint16_t numberofwavs;
338         char wavname[256];
339         for (i = k; i > 4; i--) {
340                 if (            (ptfunxored[i  ] == 'W') &&
341                                 (ptfunxored[i-1] == 'A') &&
342                                 (ptfunxored[i-2] == 'V') &&
343                                 (ptfunxored[i-3] == 'E')) {
344                         j = i-4;
345                         l = 0;
346                         while (ptfunxored[j] != '\0') {
347                                 wavname[l] = ptfunxored[j];
348                                 l++;
349                                 j--;
350                         }
351                         wavname[l] = 0;
352                         //uint8_t playlist = ptfunxored[j-8];
353
354                         if (first) {
355                                 first = false;
356                                 for (j = k; j > 4; j--) {
357                                         if (    (ptfunxored[j  ] == 0x01) &&
358                                                 (ptfunxored[j-1] == 0x5a)) {
359
360                                                 numberofwavs = 0;
361                                                 numberofwavs |= (uint32_t)(ptfunxored[j-2] << 24);
362                                                 numberofwavs |= (uint32_t)(ptfunxored[j-3] << 16);
363                                                 numberofwavs |= (uint32_t)(ptfunxored[j-4] << 8);
364                                                 numberofwavs |= (uint32_t)(ptfunxored[j-5]);
365                                                 //printf("%d wavs\n", numberofwavs);
366                                                 break;
367                                         }
368                                 k--;
369                                 }
370                         }
371
372                         std::string wave = string(wavname);
373                         std::reverse(wave.begin(), wave.end());
374                         wav_t f = { wave, (uint16_t)(numberofwavs - 1), 0 };
375
376                         if (foundin(wave, string(".grp"))) {
377                                 continue;
378                         }
379
380                         actualwavs.push_back(f);
381
382                         numberofwavs--;
383                         if (numberofwavs <= 0)
384                                 break;
385                 }
386         }
387
388         // Find Regions
389         uint8_t startbytes = 0;
390         uint8_t lengthbytes = 0;
391         uint8_t offsetbytes = 0;
392         uint8_t somethingbytes = 0;
393         uint8_t skipbytes = 0;
394
395         while (k < len) {
396                 if (            (ptfunxored[k  ] == 'S') &&
397                                 (ptfunxored[k+1] == 'n') &&
398                                 (ptfunxored[k+2] == 'a') &&
399                                 (ptfunxored[k+3] == 'p')) {
400                         break;
401                 }
402                 k++;
403         }
404         first = true;
405         uint16_t rindex = 0;
406         uint32_t findex = 0;
407         for (i = k; i < len-70; i++) {
408                 if (            (ptfunxored[i  ] == 0x5a) &&
409                                 (ptfunxored[i+1] == 0x0a)) {
410                                 break;
411                 }
412                 if (            (ptfunxored[i  ] == 0x5a) &&
413                                 (ptfunxored[i+1] == 0x0c)) {
414
415                         uint8_t lengthofname = ptfunxored[i+9];
416
417                         char name[256] = {0};
418                         for (j = 0; j < lengthofname; j++) {
419                                 name[j] = ptfunxored[i+13+j];
420                         }
421                         name[j] = '\0';
422                         j += i+13;
423                         //uint8_t disabled = ptfunxored[j];
424
425                         offsetbytes = (ptfunxored[j+1] & 0xf0) >> 4;
426                         lengthbytes = (ptfunxored[j+2] & 0xf0) >> 4;
427                         startbytes = (ptfunxored[j+3] & 0xf0) >> 4;
428                         somethingbytes = (ptfunxored[j+3] & 0xf);
429                         skipbytes = ptfunxored[j+4];
430                         findex = ptfunxored[j+5
431                                         +startbytes
432                                         +lengthbytes
433                                         +offsetbytes
434                                         +somethingbytes
435                                         +skipbytes
436                                         +40];
437                         /*rindex = ptfunxored[j+5
438                                         +startbytes
439                                         +lengthbytes
440                                         +offsetbytes
441                                         +somethingbytes
442                                         +skipbytes
443                                         +24];
444                         */
445                         uint32_t sampleoffset = 0;
446                         switch (offsetbytes) {
447                         case 4:
448                                 sampleoffset |= (uint32_t)(ptfunxored[j+8] << 24);
449                         case 3:
450                                 sampleoffset |= (uint32_t)(ptfunxored[j+7] << 16);
451                         case 2:
452                                 sampleoffset |= (uint32_t)(ptfunxored[j+6] << 8);
453                         case 1:
454                                 sampleoffset |= (uint32_t)(ptfunxored[j+5]);
455                         default:
456                                 break;
457                         }
458                         j+=offsetbytes;
459                         uint32_t length = 0;
460                         switch (lengthbytes) {
461                         case 4:
462                                 length |= (uint32_t)(ptfunxored[j+8] << 24);
463                         case 3:
464                                 length |= (uint32_t)(ptfunxored[j+7] << 16);
465                         case 2:
466                                 length |= (uint32_t)(ptfunxored[j+6] << 8);
467                         case 1:
468                                 length |= (uint32_t)(ptfunxored[j+5]);
469                         default:
470                                 break;
471                         }
472                         j+=lengthbytes;
473                         uint32_t start = 0;
474                         switch (startbytes) {
475                         case 4:
476                                 start |= (uint32_t)(ptfunxored[j+8] << 24);
477                         case 3:
478                                 start |= (uint32_t)(ptfunxored[j+7] << 16);
479                         case 2:
480                                 start |= (uint32_t)(ptfunxored[j+6] << 8);
481                         case 1:
482                                 start |= (uint32_t)(ptfunxored[j+5]);
483                         default:
484                                 break;
485                         }
486                         j+=startbytes;
487                         uint32_t something = 0;
488                         switch (somethingbytes) {
489                         case 4:
490                                 something |= (uint32_t)(ptfunxored[j+8] << 24);
491                         case 3:
492                                 something |= (uint32_t)(ptfunxored[j+7] << 16);
493                         case 2:
494                                 something |= (uint32_t)(ptfunxored[j+6] << 8);
495                         case 1:
496                                 something |= (uint32_t)(ptfunxored[j+5]);
497                         default:
498                                 break;
499                         }
500                         j+=somethingbytes;
501                         std::string filename = string(name) + ".wav";
502                         wav_t f = { 
503                                 filename,
504                                 0,
505                                 (int64_t)start,
506                                 (int64_t)length,
507                         };
508
509                         f.index = findex;
510                         //printf("something=%d\n", something);
511
512                         vector<wav_t>::iterator begin = this->actualwavs.begin();
513                         vector<wav_t>::iterator finish = this->actualwavs.end();
514                         vector<wav_t>::iterator found;
515                         // Add file to list only if it is an actual wav
516                         if ((found = std::find(begin, finish, f)) != finish) {
517                                 this->audiofiles.push_back(f);
518                                 // Also add plain wav as region
519                                 region_t r = {
520                                         name,
521                                         rindex,
522                                         start,
523                                         sampleoffset,
524                                         length,
525                                         f
526                                 };
527                                 this->regions.push_back(r);
528                         // Region only
529                         } else {
530                                 if (foundin(filename, string(".grp"))) {
531                                         continue;
532                                 }
533                                 region_t r = {
534                                         name,
535                                         rindex,
536                                         start,
537                                         sampleoffset,
538                                         length,
539                                         f
540                                 };
541                                 this->regions.push_back(r);
542                         }
543                         rindex++;
544                 }
545         }
546
547         while (k < len) {
548                 if (            (ptfunxored[k  ] == 0x5a) &&
549                                 (ptfunxored[k+1] == 0x03)) {
550                                 break;
551                 }
552                 k++;
553         }
554         while (k < len) {
555                 if (            (ptfunxored[k  ] == 0x5a) &&
556                                 (ptfunxored[k+1] == 0x02)) {
557                                 break;
558                 }
559                 k++;
560         }
561         k++;
562
563         //  Tracks
564         uint32_t offset;
565         uint32_t tracknumber = 0;
566         uint32_t regionspertrack = 0;
567         for (;k < len; k++) {
568                 if (    (ptfunxored[k  ] == 0x5a) &&
569                         (ptfunxored[k+1] == 0x04)) {
570                         break;
571                 }
572                 if (    (ptfunxored[k  ] == 0x5a) &&
573                         (ptfunxored[k+1] == 0x02)) {
574
575                         uint8_t lengthofname = 0;
576                         lengthofname = ptfunxored[k+9];
577                         if (lengthofname == 0x5a) {
578                                 continue;
579                         }
580                         track_t tr;
581
582                         regionspertrack = (uint8_t)(ptfunxored[k+13+lengthofname]);
583
584                         //printf("regions/track=%d\n", regionspertrack);
585                         char name[256] = {0};
586                         for (j = 0; j < lengthofname; j++) {
587                                 name[j] = ptfunxored[j+k+13];
588                         }
589                         name[j] = '\0';
590                         tr.name = string(name);
591                         tr.index = tracknumber++;
592
593                         for (j = k; regionspertrack > 0 && j < len; j++) {
594                                 for (l = j; l < len; l++) {
595                                         if (    (ptfunxored[l  ] == 0x5a) &&
596                                                 (ptfunxored[l+1] == 0x07)) {
597                                                 j = l;
598                                                 break;
599                                         }
600                                 }
601
602
603                                 if (regionspertrack == 0) {
604                                 //      tr.reg.index = (uint8_t)ptfunxored[j+13+lengthofname+5];
605                                         break;
606                                 } else {
607
608                                         tr.reg.index = (uint8_t)(ptfunxored[l+11]);
609                                         vector<region_t>::iterator begin = this->regions.begin();
610                                         vector<region_t>::iterator finish = this->regions.end();
611                                         vector<region_t>::iterator found;
612                                         if ((found = std::find(begin, finish, tr.reg)) != finish) {
613                                                 tr.reg = (*found);
614                                         }
615                                         startbytes = (ptfunxored[l+3] & 0xf0) >> 4;
616
617                                         i = l+16;
618                                         offset = 0;
619                                         switch (startbytes) {
620                                         case 4:
621                                                 offset |= (uint32_t)(ptfunxored[i+3] << 24);
622                                         case 3:
623                                                 offset |= (uint32_t)(ptfunxored[i+2] << 16);
624                                         case 2:
625                                                 offset |= (uint32_t)(ptfunxored[i+1] << 8);
626                                         case 1:
627                                                 offset |= (uint32_t)(ptfunxored[i]);
628                                         default:
629                                                 break;
630                                         }
631                                         tr.reg.startpos = (int64_t)offset;
632                                         if (tr.reg.length > 0) {
633                                                 this->tracks.push_back(tr);
634                                         }
635                                         regionspertrack--;
636                                 }
637                         }
638                 }
639         }
640 }