1 #include "mp3_check.h"
2
cmp_str(const char * full_str,const char * chk_str,int full_str_offset)3 static inline int cmp_str(const char *full_str, const char *chk_str,
4 int full_str_offset)
5 {
6 int check_state = FAIL;
7 int start = 31 - full_str_offset; /* ?? */
8
9 if (!memcmp(full_str + start, chk_str, strlen(chk_str)))
10 return PASS;
11
12 return check_state;
13 }
14
15 /*
16 * This is the one function that truely will make this a bad-ass proggie.
17 * Lots of logic can go in here to check for compliant mp3s.
18 */
check_header_value(unsigned int * header,char * filename,frame_info * FI)19 int check_header_value(unsigned int *header, char *filename, frame_info * FI)
20 {
21 char bin_string[33] = { 0 };
22 int i = 0;
23 int value_part = 0;
24 int column_part = 0;
25
26
27 /*
28 * The last column makes it easy to trap the 'everything else' stuff.
29 */
30 static int bitrate_matrix[6][16] = {
31
32 {1, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352,
33 384, 416, 448, 0},
34 {1, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320,
35 384, 0},
36 {1, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256,
37 320, 0},
38 {1, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224,
39 256, 0},
40 {1, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160,
41 0},
42 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
43 };
44
45 static int sampling_rate_matrix[4][3] = {
46 {44100, 22050, 11025},
47 {48000, 24000, 12000},
48 {32000, 16000, 8000},
49 {0, 0, 0}
50 };
51
52 #ifdef DEBUG
53 printf("HV: %d\n", *header);
54 #endif
55
56 /*
57 * Set the default return value of this funtion to FAIL in case of a
58 * invalid header.
59 */
60 FI->check_state = FAIL;
61
62 bin_string[32] = '\0';
63
64 for (i = 0; i < 32; i++) {
65 bin_string[i] = '0';
66 }
67
68
69 /* Convert 32bit integer to binary. */
70 for (i = 0; i < 32; i++) {
71 if (((*header >> i) & 1) == 1) {
72 bin_string[31 - i] = '1';
73 }
74 }
75
76 /*
77 * Store this data in case I want to print it out for debugging stuff.
78 */
79 strcpy(FI->BIN_STRING, bin_string);
80 FI->INT_HEADER = *header;
81
82 /*
83 * Is it a mp3 sync bit?
84 *
85 * Don't even continue if it doesn't pass this one.
86 */
87 if (cmp_str(bin_string, "11111111111", 31))
88 FI->check_state = PASS;
89 else
90 return FI->check_state;
91
92
93 /* Get the MPEG version */
94 if (cmp_str(bin_string, "00", 20))
95 FI->MPV_25 = TRUE;
96
97 else if (cmp_str(bin_string, "01", 20))
98 FI->MPV_RESERVED = TRUE;
99
100 else if (cmp_str(bin_string, "10", 20))
101 FI->MPV_2 = TRUE;
102
103 else if (cmp_str(bin_string, "11", 20))
104 FI->MPV_1 = TRUE;
105
106
107 /* Layer Version */
108 if (cmp_str(bin_string, "00", 18)) {
109 FI->L_RESERVED = TRUE;
110 FI->SAMPLES_PER_FRAME = 0;
111
112 } else if (cmp_str(bin_string, "01", 18)) {
113 FI->L3 = TRUE;
114 FI->SAMPLES_PER_FRAME = 1152;
115
116 } else if (cmp_str(bin_string, "10", 18)) {
117 FI->L2 = TRUE;
118 FI->SAMPLES_PER_FRAME = 1152;
119
120 } else if (cmp_str(bin_string, "11", 18)) {
121 FI->L1 = TRUE;
122 FI->SAMPLES_PER_FRAME = 384;
123 }
124
125 /* CRC Bit */
126 if (cmp_str(bin_string, "0", 16))
127 FI->PROT_BIT = TRUE;
128
129
130 /*
131 * BITRATE
132 *
133 * Grab the specific integer from the "header" that I need.
134 *
135 * Truncate at the 12th bit (0xf), and shift right 12 bits to just
136 * get bitrange 15 -> 12.
137 */
138 value_part = ((*header >> 12) & 0xf);
139
140 /* Column 5 is for everything else (junk) */
141 if (FI->MPV_1 && FI->L1)
142 column_part = 0;
143
144 else if (FI->MPV_1 && FI->L2)
145 column_part = 1;
146
147 else if (FI->MPV_1 && FI->L3)
148 column_part = 2;
149
150 else if ((FI->MPV_2 || FI->MPV_25) && FI->L1)
151 column_part = 3;
152
153 else if ((FI->MPV_2 || FI->MPV_25) && (FI->L2 || FI->L3))
154 column_part = 4;
155
156 else
157 column_part = 5;
158
159
160 FI->BIT_RATE = bitrate_matrix[column_part][value_part];
161
162
163 /*
164 * Sampling Rate
165 *
166 * Truncate at the 2nd bit (0x3), and shift right 9 bits to just get
167 * bitrange 11 -> 10.
168 */
169 value_part = ((*header >> 10) & 0x3);
170
171 if (FI->MPV_1)
172 FI->SAMPLE_FREQ = sampling_rate_matrix[value_part][0];
173
174 else if (FI->MPV_2)
175 FI->SAMPLE_FREQ = sampling_rate_matrix[value_part][1];
176
177 else if (FI->MPV_25)
178 FI->SAMPLE_FREQ = sampling_rate_matrix[value_part][2];
179
180 else
181 FI->SAMPLE_FREQ = 0;
182
183
184 /* Padding Bit */
185 if (cmp_str(bin_string, "1", 9))
186 FI->PAD_BIT = TRUE;
187
188
189 /* Private Bit */
190 if (cmp_str(bin_string, "1", 8))
191 FI->PRIV_BIT = TRUE;
192
193
194 /*
195 * Get the channel mode
196 *
197 * The 'mode_extension' will be added later perhaps in V2.0.
198 */
199 if (cmp_str(bin_string, "00", 7))
200 FI->STEREO = TRUE;
201
202 else if (cmp_str(bin_string, "01", 7)) {
203 FI->JOINT_STEREO = TRUE;
204 FI->MODE_EXTENSION = TRUE;
205 }
206
207 else if (cmp_str(bin_string, "10", 7))
208 FI->DUAL_STEREO = TRUE;
209
210 else if (cmp_str(bin_string, "11", 7))
211 FI->SINGLE_CHANNEL = TRUE;
212
213
214 /* Copyright Bit */
215 if (cmp_str(bin_string, "1", 3))
216 FI->COPYRIGHT = TRUE;
217
218
219 /* Original Bit */
220 if (cmp_str(bin_string, "1", 2))
221 FI->ORIGINAL = TRUE;
222
223
224 /* Emphasis */
225 if (cmp_str(bin_string, "00", 1))
226 FI->EMPH_NONE = TRUE;
227
228 else if (cmp_str(bin_string, "01", 1))
229 FI->EMPH_5015 = TRUE;
230
231 else if (cmp_str(bin_string, "10", 1))
232 FI->EMPH_RESERV = TRUE;
233
234 else if (cmp_str(bin_string, "11", 1))
235 FI->EMPH_CCIT = TRUE;
236
237
238 /* FRAME LENGTH */
239 if (FI->SAMPLE_FREQ > 0) {
240 if (FI->MPV_1) {
241 if (FI->L1 && FI->PAD_BIT == TRUE) {
242 FI->FRAME_LENGTH =
243 (12 * FI->BIT_RATE * 1000 /
244 FI->SAMPLE_FREQ + 4) * 4;
245
246 } else if (FI->L1 && FI->PAD_BIT == FALSE) {
247 FI->FRAME_LENGTH =
248 (12 * FI->BIT_RATE * 1000 /
249 FI->SAMPLE_FREQ) * 4;
250
251 } else if ((FI->L2 || FI->L3) && FI->PAD_BIT == TRUE) {
252 FI->FRAME_LENGTH =
253 (144 * FI->BIT_RATE * 1000 /
254 FI->SAMPLE_FREQ + 1);
255
256 } else if ((FI->L2 || FI->L3) && FI->PAD_BIT == FALSE) {
257 FI->FRAME_LENGTH =
258 (144 * FI->BIT_RATE * 1000 /
259 FI->SAMPLE_FREQ);
260 }
261 } else if (FI->MPV_2 || FI->MPV_25) {
262 if (FI->L1 && FI->PAD_BIT == TRUE) {
263 FI->FRAME_LENGTH =
264 (240 * FI->BIT_RATE * 1000 /
265 FI->SAMPLE_FREQ + 4) * 4;
266
267 } else if (FI->L1 && FI->PAD_BIT == FALSE) {
268 FI->FRAME_LENGTH =
269 (240 * FI->BIT_RATE * 1000 /
270 FI->SAMPLE_FREQ) * 4;
271
272 } else if ((FI->L2 || FI->L3) && FI->PAD_BIT == TRUE) {
273 FI->FRAME_LENGTH =
274 (72 * FI->BIT_RATE * 1000 /
275 FI->SAMPLE_FREQ + 1);
276
277 } else if ((FI->L2 || FI->L3) && FI->PAD_BIT == FALSE) {
278 FI->FRAME_LENGTH =
279 (72 * FI->BIT_RATE * 1000 /
280 FI->SAMPLE_FREQ);
281 }
282 }
283 } else
284 FI->FRAME_LENGTH = 0;
285
286
287
288 /*
289 * ERROR TRAPPING HAPPENS HERE. IN FUTURE VERSIONS, I WILL CHECK FOR
290 * JOINT STEREO and INTENSITY STEREO.
291 */
292 if (FI->L_RESERVED || FI->BIT_RATE == 0 || FI->BIT_RATE == 1
293 || FI->SAMPLE_FREQ == 0)
294 FI->check_state = FAIL;
295
296 else if (FI->L2 && FI->BIT_RATE == 32 && FI->SINGLE_CHANNEL == FALSE)
297 FI->check_state = FAIL;
298
299 else if (FI->L2 && FI->BIT_RATE == 48 && FI->SINGLE_CHANNEL == FALSE)
300 FI->check_state = FAIL;
301
302 else if (FI->L2 && FI->BIT_RATE == 56 && FI->SINGLE_CHANNEL == FALSE)
303 FI->check_state = FAIL;
304
305 else if (FI->L2 && FI->BIT_RATE == 80 && FI->SINGLE_CHANNEL == FALSE)
306 FI->check_state = FAIL;
307
308 else if (FI->L2 && FI->BIT_RATE == 32 && FI->SINGLE_CHANNEL == FALSE)
309 FI->check_state = FAIL;
310
311 else if (FI->L2 && FI->BIT_RATE == 224
312 && !(FI->STEREO || FI->DUAL_STEREO)) FI->check_state = FAIL;
313
314 else if (FI->L2 && FI->BIT_RATE == 256
315 && !(FI->STEREO || FI->DUAL_STEREO)) FI->check_state = FAIL;
316
317 else if (FI->L2 && FI->BIT_RATE == 320
318 && !(FI->STEREO || FI->DUAL_STEREO)) FI->check_state = FAIL;
319
320 else if (FI->L2 && FI->BIT_RATE == 384
321 && !(FI->STEREO || FI->DUAL_STEREO)) FI->check_state = FAIL;
322
323
324 return FI->check_state;
325 }
326
transform_char_array(char * byte_list,gen_info * file_info)327 int transform_char_array(char *byte_list, gen_info *file_info)
328 {
329 int place_holder = 0;
330 int counter;
331 char trans_list[128] = { '*' };
332
333 place_holder = file_info->byte_count % 128 + 1;
334
335 /*
336 * This is going to be a little confusing at the 'place_holder' mark,
337 * the data from place_holder to 127 is the oldest, and data from 0
338 * to place_holder is newest
339 *
340 * Get the oldest stuff first, and put it at the beginning of the
341 * array.
342 */
343 for (counter = place_holder; counter < 128; counter++)
344 *(trans_list + (counter - place_holder)) =
345 *(byte_list + counter);
346
347 /* Now, grab the newest stuff, and slap it at the end of the array. */
348 for (counter = 0; counter < place_holder; counter++)
349 *(trans_list + (128 - place_holder + counter)) =
350 *(byte_list + counter);
351
352 memcpy(byte_list, trans_list, 128);
353
354 return (TRUE);
355 }
356
translate_time(gen_info * file_info,mp3_time * song_time)357 void translate_time(gen_info *file_info, mp3_time *song_time)
358 {
359 double float_minute = 0.0;
360 double float_second = 0.0;
361
362 float_minute = file_info->time_in_seconds / 60.0;
363 float_second = 60.0 * (float_minute - (int) float_minute);
364
365 /* Return the values. */
366 song_time->frac_second = 100.0 * (float_second - (int) float_second);
367 song_time->minutes = (int) float_minute;
368 song_time->seconds = (int) float_second;
369 }
370