File: id3_tag_parse_lyrics3.cpp

package info (click to toggle)
libid3 3.05-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 560 kB
  • ctags: 568
  • sloc: cpp: 3,582; makefile: 85
file content (227 lines) | stat: -rw-r--r-- 5,009 bytes parent folder | download
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
//  The authors have released ID3Lib as Public Domain (PD) and claim no copyright,
//  patent or other intellectual property protection in this work.  This means that
//  it may be modified, redistributed and used in commercial and non-commercial
//  software and hardware without restrictions.  ID3Lib is distributed on an "AS IS"
//  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
//  
//  The ID3Lib authors encourage improvements and optimisations to be sent to the
//  ID3Lib coordinator, currently Dirk Mahoney (dirk@id3.org).  Approved
//  submissions may be altered, and will be included and released under these terms.
//  
//  Mon Nov 23 18:34:01 1998


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include "id3_tag.h"
#include "id3_misc_support.h"


luint			ID3_CRLFtoLF					( char *buffer, luint size )
{
	luint	newSize	= 0;
	char	*dest	= buffer;
	char	*source	= buffer;

	if	( buffer && size )
	{
		while	( source < ( buffer + size ) )
		{
			if	( *source == 0x0D )
				source++;
			else
				*dest++ = *source++;
		}

		newSize = dest - buffer;
	}
	else
		ID3_THROW ( ID3E_NoData );

	return newSize;
}


luint			ID3_StripTimeStamps				( char *buffer, luint size )
{
	luint	newSize	= 0;
	char	*dest	= buffer;
	char	*source	= buffer;

	if	( buffer && size )
	{
		while	( source < ( buffer + size ) )
		{
			if	( *source == '[' )
				source += 7;
			else
				*dest++ = *source++;
		}

		newSize = dest - buffer;
	}
	else
		ID3_THROW ( ID3E_NoData );

	return newSize;
}


void			ID3_Tag::ParseLyrics3			( void )
{
	if	( fileHandle )
	{
		uchar	buffer[ 18 ];

		fseek ( fileHandle, -143, SEEK_END );
		fread ( buffer, 1, 18, fileHandle );

		// first check for an ID3v1 tag
		if	( memcmp ( &buffer[ 15 ], "TAG", 3 ) == 0 )
		{
			if	( memcmp ( &buffer[ 6 ], "LYRICSEND", 9 ) == 0 )
			{
				// we have a Lyrics3 v1.00 tag
			}
			else if ( memcmp ( &buffer[ 6 ], "LYRICS200", 9 ) == 0 )
			{
				// we have a Lyrics3 v2.00 tag
				luint	lyricsSize;

				buffer[ 6 ] = 0;
				lyricsSize = atoi ( (char *) buffer );

				fseek ( fileHandle, -18 - lyricsSize, SEEK_CUR );
				fread ( buffer, 1, 11, fileHandle );

				if	( memcmp ( buffer, "LYRICSBEGIN", 11 ) == 0 )
				{
					luint	bytesToRead	= lyricsSize - 11;
					uchar	*buff2;

					extraBytes += lyricsSize + 9 + 6;

					if	( buff2 = new uchar[ bytesToRead ] )
					{
						luint	posn		= 0;
						bool	stampsUsed	= false;

						fread ( buff2, 1, bytesToRead, fileHandle );

						while	( posn < bytesToRead )
						{
							uchar	fid[ 4 ];
							uchar	sizeT[ 6 ];
							luint	size;

							fid[ 3 ] = 0;
							sizeT[ 5 ] = 0;

							memcpy ( fid, &buff2[ posn ], 3 );
							memcpy ( sizeT, &buff2[ posn + 3 ], 5 );
							size = atoi ( (char *) sizeT );

							// the IND field
							if	( strcmp ( (char *) fid, "IND" ) == 0 )
							{
								if	( buff2[ posn + 8 + 1 ] == '1' )
									stampsUsed = true;
							}

							// the TITLE field
							if	( strcmp ( (char *) fid, "ETT" ) == 0 )
							{
								char	*text;

								if	( text = new char[ size + 1 ] )
								{
									text[ size ] = 0;
									memcpy ( text, &buff2[ posn + 8 ], size );

									ID3_AddTitle ( this, text );

									delete[] text;
								}
								else
									ID3_THROW ( ID3E_NoMemory );
							}

							// the ARTIST field
							if	( strcmp ( (char *) fid, "EAR" ) == 0 )
							{
								char	*text;

								if	( text = new char[ size + 1 ] )
								{
									text[ size ] = 0;
									memcpy ( text, &buff2[ posn + 8 ], size );

									ID3_AddArtist ( this, text );

									delete[] text;
								}
								else
									ID3_THROW ( ID3E_NoMemory );
							}

							// the ALBUM field
							if	( strcmp ( (char *) fid, "EAL" ) == 0 )
							{
								char	*text;

								if	( text = new char[ size + 1 ] )
								{
									text[ size ] = 0;
									memcpy ( text, &buff2[ posn + 8 ], size );

									ID3_AddAlbum ( this, text );

									delete[] text;
								}
								else
									ID3_THROW ( ID3E_NoMemory );
							}

							// the LYRICS field
							if	( strcmp ( (char *) fid, "LYR" ) == 0 )
							{
								char	*text;
								luint	newSize;

								newSize = ID3_CRLFtoLF ( (char *) &buff2[ posn + 8 ], size );

								if	( stampsUsed )
									newSize = ID3_StripTimeStamps ( (char *) &buff2[ posn + 8 ], newSize );

								if	( text = new char[ newSize + 1 ] )
								{
									text[ newSize ] = 0;

									memcpy ( text, &buff2[ posn + 8 ], newSize );

									ID3_AddLyrics ( this, text );

									delete[] text;
								}
								else
									ID3_THROW ( ID3E_NoMemory );
							}

							posn += size + 8;
						}

						delete[] buff2;
					}
				}
			}
		}
	}
	else
		ID3_THROW ( ID3E_NoData );

	return;
}