summaryrefslogtreecommitdiff
path: root/ptparse.c
blob: 5e6f50366cadce62468e7fcb1eff7ff0cf5eb0e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
 * Copyright (C) 2015  Damien Zammit <damien@zamaudio.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 */

#include <stdio.h>
#include <inttypes.h>
#include <stdlib.h>
#include <string.h>

#define NOTMAPPED 0
#define SAMPLES 1

void inplace_reverse(unsigned char * str)
{
  if (str)
  {
    unsigned char * end = str + strlen(str) - 1;

#   define XOR_SWAP(a,b) do\
    {\
      a ^= b;\
      b ^= a;\
      a ^= b;\
    } while (0)

    while (str < end)
    {
      XOR_SWAP(*str, *end);
      str++;
      end--;
    }
#   undef XOR_SWAP
  }
}

int main(int argc, char** argv) {
	FILE *fp;
	int i;
	int j;
	int k;
	int l;
	int len;
	int type;
	unsigned char *ptf;

	fp = fopen(argv[1], "rb");
	fseek(fp, 0, SEEK_END);
	len = ftell(fp);
	
	fseek(fp, 0x0, SEEK_SET);
	if ((ptf = malloc(len*sizeof(unsigned char))) == 0) {
		printf("Out of memory\n");
		return 1;
	}
	
	fread(ptf, 1, len, fp);

	// Find end of wav file list
	k = 0;
	while (k < len) {
		if (		(ptf[k  ] == 0xff) &&
				(ptf[k+1] == 0xff) &&
				(ptf[k+2] == 0xff) &&
				(ptf[k+3] == 0xff)) {
			break;
		}	
		k++;
	}

	j = 0;
	l = 0;
	unsigned char wavname[256] = {0};
	for (i = k; i > 4; i--) {
		if (		(ptf[i  ] == 'W') &&
				(ptf[i-1] == 'A') &&
				(ptf[i-2] == 'V') &&
				(ptf[i-3] == 'E')) {
			j = i-4;
			l = 0;
			while (ptf[j] != '\0') {
				wavname[l] = ptf[j];
				l++;
				j--;
			}
			wavname[l] = 0;
			inplace_reverse(wavname);
			printf("%s\n", wavname);
		}
	}

	while (		(ptf[k  ] != 0x5a) &&
			(ptf[k+1] != 0x01) &&
			(ptf[k+2] != 0x00)) {
		k++;
	}
	for (i = k; i < len-70; i++) {
		if (		(ptf[i  ] == 0x5a) &&
				(ptf[i+1] == 0x0c) &&
				(ptf[i+2] == 0x00)) {
			unsigned char name[65] = {0};
			for (j = i + 13; j < i + 13 + 64; j++) {
				name[j-(i+13)] = ptf[j];
				if (ptf[j] == 0x00) {
					type = SAMPLES;
					name[j-(i+13)] = 0x00;
					break;
				}
				if (ptf[j] == 0x01) {
					type = NOTMAPPED;
					name[j-(i+13)] = 0x00;
					break;
				}
			}
			int samplebytes1 = (ptf[j+3] & 0xf);
			unsigned char samplebytehigh = ptf[j+16];

			if ((samplebytes1 == 3) && (samplebytehigh == 0xfe)) {
				samplebytes1 = 4;
			}
			//int samplebytes2 = (ptf[j+1] & 0xf0) >> 4;
			//printf("sbh=0x%x\n", samplebytehigh);
			int samplebytes = samplebytes1;
			if (type == SAMPLES) {
				uint32_t offset = 0;
				switch (samplebytes) {
				case 4:
					offset |= (uint32_t)(ptf[j+11] << 24);
				case 3:
					offset |= (uint32_t)(ptf[j+10] << 16);
				case 2:
					offset |= (uint32_t)(ptf[j+9] << 8);
				case 1:
					offset |= (uint32_t)(ptf[j+8]);
				default:
					break;
				}
				//printf("%s sampleoffset = %08x = %d\n", name, offset, offset);
				printf("%s\t%u\n", name, offset);
			}
		}
	}

	fclose(fp);
	free(ptf);
	return 0;
}