1 #include "mp3_check.h"
2 
3 #include <stdio.h>
4 
5 
6 static int get_last_char_offset(char *fat_string);
7 static int copy_int_array_to_str(char *possible_mp3_tag, char *tag_field,
8 				 int offset, int length, int max_length);
9 
10 
get_last_char_offset(char * fat_string)11 static int get_last_char_offset(char *fat_string)
12 {
13 	int i = 0;
14 
15 	for (i = strlen(fat_string) - 1; i >= 0; i--) {
16 		if (ISGRAPH((int) *(fat_string + i)))
17 			return (i + 1);
18 	}
19 
20 	return (strlen(fat_string));
21 }
22 
dump_id3_tag(id3_tag_info * id3_tag)23 int dump_id3_tag(id3_tag_info * id3_tag)
24 {
25 	char title[30];
26 	char artist[30];
27 	char album[30];
28 	char year[4];
29 	char comment[30];
30 	int i = 0;
31 
32 	memset(title, '\0', sizeof(title));
33 	memset(artist, '\0', sizeof(artist));
34 	memset(album, '\0', sizeof(album));
35 	memset(year, '\0', sizeof(year));
36 	memset(comment, '\0', sizeof(comment));
37 
38 	/*
39 	 * The rules state, that nulls should pad the fields,
40 	 * so, that is what I am doing here.
41 	 */
42 	strncpy(title, id3_tag->TITLE, get_last_char_offset(id3_tag->TITLE));
43 	strncpy(artist, id3_tag->ARTIST,
44 		get_last_char_offset(id3_tag->ARTIST));
45 	strncpy(album, id3_tag->ALBUM, get_last_char_offset(id3_tag->ALBUM));
46 	strncpy(year, id3_tag->YEAR, get_last_char_offset(id3_tag->YEAR));
47 	strncpy(comment, id3_tag->COMMENT,
48 		get_last_char_offset(id3_tag->COMMENT));
49 
50 
51 	/*
52 	 * Now, I am going to send the null-pointed fields to the file pointer.
53 	 * This below is not very elegant. It works though,
54 	 * so I will revisit this at a later time.
55 	 *
56 	 * The problem is that I need to print the null values,
57 	 * if I didn't need to do that, it would be much cleaner.
58 	 */
59 	fputs("TAG", stdout);
60 
61 	for (i = 0; i < 30; i++)
62 		fputc(*(title + i), stdout);
63 
64 	for (i = 0; i < 30; i++)
65 		fputc(*(artist + i), stdout);
66 
67 	for (i = 0; i < 30; i++)
68 		fputc(*(album + i), stdout);
69 
70 	for (i = 0; i < 4; i++)
71 		fputc(*(year + i), stdout);
72 
73 	/* It's 29 because the last byte may be the track number. */
74 	for (i = 0; i < 29; i++)
75 		fputc(*(comment + i), stdout);
76 
77 	if (id3_tag->TRACK_NUMBER != 0)
78 		fputc(id3_tag->TRACK_NUMBER, stdout);
79 	else
80 		fputc(*(comment + 29), stdout);
81 
82 	fputc(id3_tag->GENRE, stdout);
83 
84 	return (TRUE);
85 }
86 
copy_int_array_to_str(char * possible_mp3_tag,char * tag_field,int offset,int length,int max_length)87 static int copy_int_array_to_str(char *possible_mp3_tag, char *tag_field,
88 				 int offset, int length, int max_length)
89 {
90 	int counter = 0;
91 	int position = 0;
92 
93 	if (max_length < (offset + length))
94 		return (FAIL);
95 
96 	for (position = offset; position < (offset + length); position++) {
97 		*(tag_field + counter) = (char) *(possible_mp3_tag + position);
98 		counter++;
99 	}
100 
101 	return (PASS);
102 }
103 
validate_id3_tag(char * possible_mp3_tag,id3_tag_info * id3_tag)104 int validate_id3_tag(char *possible_mp3_tag, id3_tag_info * id3_tag)
105 {
106 	/* The first 3 characters must be "TAG" for it to be an id3 tag. */
107 
108 	/*
109 	 * This section is just to see the 128 bit buffer in numerical format
110 	 */
111 	if ((possible_mp3_tag[0] == 'T') && (possible_mp3_tag[1] == 'A')
112 	    && (possible_mp3_tag[2] == 'G')) {
113 		id3_tag->TAG_PRESENT = YES;
114 
115 		if (possible_mp3_tag[126] != '\0') {
116 			id3_tag->TRACK_NUMBER = possible_mp3_tag[126];
117 			id3_tag->ID3_311_VERSION = YES;
118 		} else {
119 			id3_tag->TRACK_NUMBER = 0;
120 			id3_tag->ID3_311_VERSION = NO;
121 		}
122 
123 
124 		id3_tag->GENRE = possible_mp3_tag[127];
125 
126 		copy_int_array_to_str(possible_mp3_tag, id3_tag->TITLE, 3, 30,
127 				      128);
128 		copy_int_array_to_str(possible_mp3_tag, id3_tag->ARTIST, 33,
129 				      30, 128);
130 		copy_int_array_to_str(possible_mp3_tag, id3_tag->ALBUM, 63, 30,
131 				      128);
132 		copy_int_array_to_str(possible_mp3_tag, id3_tag->YEAR, 93, 4,
133 				      128);
134 		copy_int_array_to_str(possible_mp3_tag, id3_tag->COMMENT, 97,
135 				      30, 128);
136 
137 #if 0
138 		/*
139 		 * This section below does not work because I am working with
140 		 * an integer array and not a character array. The reason for
141 		 * that is that with a character array, if there are any
142 		 * nulls, the array shortens and I need the (null)s to find
143 		 * out certain info...
144 		 */
145 		memcpy(id3_tag->TITLE, possible_mp3_tag + 3, 30);
146 		memcpy(id3_tag->ARTIST, possible_mp3_tag + 33, 30);
147 		memcpy(id3_tag->ALBUM, possible_mp3_tag + 63, 30);
148 		memcpy(id3_tag->YEAR, possible_mp3_tag + 93, 4);
149 		memcpy(id3_tag->COMMENT, possible_mp3_tag + 97, 30);
150 #endif
151 	} else {
152 		id3_tag->TAG_PRESENT = NO;
153 		return (FAIL);
154 	}
155 
156 	return (PASS);
157 }
158