summaryrefslogtreecommitdiff
path: root/plugins/shn/wave.c
blob: a21a69411a613b8d48de8e8d5ed233d7e46644ff (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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*  wave.c - functions to parse and verify WAVE headers
 *  Copyright (C) 2000-2007  Jason Jordan <shnutils@freeshell.org>
 *
 *  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 2 of the License, or
 *  (at your option) 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

/*
 * $Id: wave.c,v 1.13 2007/03/23 05:49:48 jason Exp $
 */

#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include "shorten.h"
#include "shn.h"

int is_valid_file(shn_file *info)
/* determines whether the given filename (info->filename) is a regular file, and is readable */
{
  struct stat sz;
  FILE *f;

  if (0 != stat(info->wave_header.filename,&sz)) {
    if (errno == ENOENT)
      shn_error("cannot open '%s' because it does not exist",info->wave_header.filename);
    else if (errno == EACCES)
      shn_error("cannot open '%s' due to insufficient permissions",info->wave_header.filename);
    else if (errno == EFAULT)
      shn_error("cannot open '%s' due to bad address",info->wave_header.filename);
    else if (errno == ENOMEM)
      shn_error("cannot open '%s' because the kernel ran out of memory",info->wave_header.filename);
    else if (errno == ENAMETOOLONG)
      shn_error("cannot open '%s' because the file name is too long",info->wave_header.filename);
    else
      shn_error("cannot open '%s' due to an unknown problem",info->wave_header.filename);
    return 0;
  }
  if (0 == S_ISREG(sz.st_mode)) {
    if (S_ISLNK(sz.st_mode))
      shn_error("'%s' is a symbolic link, not a regular file",info->wave_header.filename);
    else if (S_ISDIR(sz.st_mode))
      shn_error("'%s' is a directory, not a regular file",info->wave_header.filename);
    else if (S_ISCHR(sz.st_mode))
      shn_error("'%s' is a character device, not a regular file",info->wave_header.filename);
    else if (S_ISBLK(sz.st_mode))
      shn_error("'%s' is a block device, not a regular file",info->wave_header.filename);
    else if (S_ISFIFO(sz.st_mode))
      shn_error("'%s' is a fifo, not a regular file",info->wave_header.filename);
    else if (S_ISSOCK(sz.st_mode))
      shn_error("'%s' is a socket, not a regular file",info->wave_header.filename);
    return 0;
  }
  info->wave_header.actual_size = (ulong)sz.st_size;

  if (NULL == (f = fopen(info->wave_header.filename,"rb"))) {
    shn_error("could not open '%s': %s",info->wave_header.filename,strerror(errno));
    return 0;
  }
  fclose(f);

  return 1;
}

int shn_verify_header(shn_file *this_shn)
{
	ulong l;
	int cur = 0;

	if (0 == is_valid_file(this_shn))
	{
		shn_debug("while processing '%s': something went wrong while opening this file, see above",this_shn->wave_header.filename);
		return 0;
	}

	if (this_shn->vars.bytes_in_header < CANONICAL_HEADER_SIZE) {
		shn_debug("while processing '%s': header is only %d bytes (should be at least %d bytes)",
			this_shn->wave_header.filename,this_shn->vars.bytes_in_header,CANONICAL_HEADER_SIZE);
		return 0;
	}

	if (WAVE_RIFF != shn_uchar_to_ulong_le(this_shn->vars.header+cur))
	{
		if (AIFF_FORM == shn_uchar_to_ulong_le(this_shn->vars.header+cur))
			shn_debug("while processing '%s': file contains AIFF data, which is currently not supported",this_shn->wave_header.filename);
		else
			shn_debug("while processing '%s': WAVE header is missing RIFF tag - possible corrupt file",this_shn->wave_header.filename);
		return 0;
	}
	cur += 4;

	this_shn->wave_header.chunk_size = shn_uchar_to_ulong_le(this_shn->vars.header+cur);
	cur += 4;

	if (WAVE_WAVE != shn_uchar_to_ulong_le(this_shn->vars.header+cur))
	{
		shn_debug("while processing '%s': WAVE header is missing WAVE tag",this_shn->wave_header.filename);
		return 0;
	}
	cur += 4;

	for (;;)
	{
		cur += 4;

		l = shn_uchar_to_ulong_le(this_shn->vars.header+cur);
		cur += 4;

		if (WAVE_FMT == shn_uchar_to_ulong_le(this_shn->vars.header+cur-8))
			break;

		cur += l;
	}

	if (l < 16)
	{
		shn_debug("while processing '%s': fmt chunk in WAVE header was too short",this_shn->wave_header.filename);
		return 0;
	}

	this_shn->wave_header.wave_format = shn_uchar_to_ushort_le(this_shn->vars.header+cur);
	cur += 2;

	switch (this_shn->wave_header.wave_format)
	{
		case WAVE_FORMAT_PCM:
			break;
		default:
			shn_debug("while processing '%s': unsupported format 0x%04x (%s) - only PCM data is supported at this time",
				this_shn->wave_header.filename,this_shn->wave_header.wave_format,shn_format_to_str(this_shn->wave_header.wave_format));
                        return 0;
	}

	this_shn->wave_header.channels = shn_uchar_to_ushort_le(this_shn->vars.header+cur);
	cur += 2;
	this_shn->wave_header.samples_per_sec = shn_uchar_to_ulong_le(this_shn->vars.header+cur);
	cur += 4;
	this_shn->wave_header.avg_bytes_per_sec = shn_uchar_to_ulong_le(this_shn->vars.header+cur);
	cur += 4;
	this_shn->wave_header.block_align = shn_uchar_to_ushort_le(this_shn->vars.header+cur);
	cur += 2;
	this_shn->wave_header.bits_per_sample = shn_uchar_to_ushort_le(this_shn->vars.header+cur);
	cur += 2;

	if (this_shn->wave_header.bits_per_sample != 8 && this_shn->wave_header.bits_per_sample != 16)
	{
		shn_debug("while processing '%s': bits per sample is neither 8 nor 16",this_shn->wave_header.filename);
		return 0;
	}

	l -= 16;

	if (l > 0)
		cur += l;

	for (;;)
	{
		cur += 4;

		l = shn_uchar_to_ulong_le(this_shn->vars.header+cur);
		cur += 4;

		if (WAVE_DATA == shn_uchar_to_ulong_le(this_shn->vars.header+cur-8))
			break;

		cur += l;
	}

	this_shn->wave_header.rate = ((uint)this_shn->wave_header.samples_per_sec *
				      (uint)this_shn->wave_header.channels *
				      (uint)this_shn->wave_header.bits_per_sample) / 8;
	this_shn->wave_header.header_size = cur;
	this_shn->wave_header.data_size = l;
	this_shn->wave_header.total_size = this_shn->wave_header.chunk_size + 8;
	this_shn->wave_header.length = this_shn->wave_header.data_size / this_shn->wave_header.rate;
	this_shn->wave_header.exact_length = (double)this_shn->wave_header.data_size / (double)this_shn->wave_header.rate;

	if (this_shn->wave_header.channels == CD_CHANNELS &&
	    this_shn->wave_header.bits_per_sample == CD_BITS_PER_SAMPLE &&
	    this_shn->wave_header.samples_per_sec == CD_SAMPLES_PER_SEC &&
	    this_shn->wave_header.avg_bytes_per_sec == CD_RATE &&
	    this_shn->wave_header.rate == CD_RATE)
	{
		if (this_shn->wave_header.data_size < CD_MIN_BURNABLE_SIZE)
			this_shn->wave_header.problems |= PROBLEM_CD_BUT_TOO_SHORT;
		if (this_shn->wave_header.data_size % CD_BLOCK_SIZE != 0)
			this_shn->wave_header.problems |= PROBLEM_CD_BUT_BAD_BOUND;
	}
	else
		this_shn->wave_header.problems |= PROBLEM_NOT_CD_QUALITY;

	if (this_shn->wave_header.header_size != CANONICAL_HEADER_SIZE)
		this_shn->wave_header.problems |= PROBLEM_HEADER_NOT_CANONICAL;

	if ((ulong)this_shn->wave_header.header_size + this_shn->wave_header.data_size > this_shn->wave_header.total_size)
		this_shn->wave_header.problems |= PROBLEM_HEADER_INCONSISTENT;

	if ((ulong)this_shn->wave_header.header_size + this_shn->wave_header.data_size < this_shn->wave_header.total_size)
		this_shn->wave_header.problems |= PROBLEM_EXTRA_CHUNKS;

	shn_length_to_str(this_shn);

	/* header looks ok */
	return 1;
}

char *shn_format_to_str(ushort format)
{
  switch (format) {
    case WAVE_FORMAT_UNKNOWN:
      return "Microsoft Official Unknown";
    case WAVE_FORMAT_PCM:
      return "Microsoft PCM";
    case WAVE_FORMAT_ADPCM:
      return "Microsoft ADPCM";
    case WAVE_FORMAT_IEEE_FLOAT:
      return "IEEE Float";
    case WAVE_FORMAT_ALAW:
      return "Microsoft A-law";
    case WAVE_FORMAT_MULAW:
      return "Microsoft U-law";
    case WAVE_FORMAT_OKI_ADPCM:
      return "OKI ADPCM format";
    case WAVE_FORMAT_IMA_ADPCM:
      return "IMA ADPCM";
    case WAVE_FORMAT_DIGISTD:
      return "Digistd format";
    case WAVE_FORMAT_DIGIFIX:
      return "Digifix format";
    case WAVE_FORMAT_DOLBY_AC2:
      return "Dolby AC2";
    case WAVE_FORMAT_GSM610:
      return "GSM 6.10";
    case WAVE_FORMAT_ROCKWELL_ADPCM:
      return "Rockwell ADPCM";
    case WAVE_FORMAT_ROCKWELL_DIGITALK:
      return "Rockwell DIGITALK";
    case WAVE_FORMAT_G721_ADPCM:
      return "G.721 ADPCM";
    case WAVE_FORMAT_G728_CELP:
      return "G.728 CELP";
    case WAVE_FORMAT_MPEG:
      return "MPEG";
    case WAVE_FORMAT_MPEGLAYER3:
      return "MPEG Layer 3";
    case WAVE_FORMAT_G726_ADPCM:
      return "G.726 ADPCM";
    case WAVE_FORMAT_G722_ADPCM:
      return "G.722 ADPCM";
  }
  return "Unknown";
}