-
Notifications
You must be signed in to change notification settings - Fork 3
/
ID3Parser.m
326 lines (284 loc) · 9.31 KB
/
ID3Parser.m
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//
// ID3Parser.m
//
#import "ID3Parser.h"
#import "ID3Frame.h"
static NSString * const ID3ParserDomain = @"ID3ParserDomain";
//Private Class interface
@interface ID3Parser ()
+ (NSArray *)descriptionsForFramesArray:(NSArray *)tagsArray;
@end
@implementation ID3Parser
+ (NSArray *)parseTagWithData:(NSData *)data error:(NSError **)error
{
//NSDictionary *tagDictionary = nil;
NSUInteger length = [data length];
if(!length){
if(error) *error = [ID3Parser errorForCode:ID3_PARSERDOMAIN_EEMPTY
underlyingError:NULL
recoveryObject:nil];
return nil;
}
[data retain];
//find start of tag, end of tag
NSRange id3Header = [data rangeOfData:[NSData dataWithBytes:"ID3" length:3] options:0 range:NSMakeRange(0, length)];
if(id3Header.location == NSNotFound){
if(error) *error = [ID3Parser errorForCode:ID3_PARSERDOMAIN_ENOTFOUND
underlyingError:NULL
recoveryObject:nil];
[data release];
return nil;
}
const char *id3HeaderBegin = (const char*)[data bytes] + id3Header.location;
char version = *(id3HeaderBegin + ID3_HEADER_VERSION_OFFSET);
int tagSize;
ID3_VERSION majorVersion;
switch (version){
case 2:
{
char num[4];
num[0] = 0;
const char* ptr = id3HeaderBegin + ID3_HEADER_SIZE_OFFSET;
memcpy(num + 1, ptr, 3);
tagSize = CFSwapInt32BigToHost(*(uint32_t*)num);
majorVersion = ID3_VERSION_2;
break;
}
case 3:
{
const int *sizePtr = (const int*)(id3HeaderBegin + ID3_HEADER_SIZE_OFFSET);
tagSize = CFSwapInt32BigToHost(*sizePtr);
majorVersion = ID3_VERSION_3;
break;
}
case 4:
{
majorVersion = ID3_VERSION_4;
int syncInt = *(int *)(id3HeaderBegin + ID3_HEADER_SIZE_OFFSET);
tagSize = [ID3Parser integerFromSyncsafeInteger:syncInt];
assert(tagSize <= 0xFFFFFFF);
break;
}
default:
{
if(error) *error = [ID3Parser errorForCode:ID3_PARSERDOMAIN_EVERSION
underlyingError:NULL
recoveryObject:nil];
[data release];
return nil;
}
}
NSLog(@"\n\nMAJOR VERSION IS: %d\n", majorVersion);
NSLog(@"Tag size is: %d\n", tagSize);
char flags = *(id3HeaderBegin + ID3_HEADER_FLAGS_OFFSET);
if(majorVersion == ID3_VERSION_2 && (flags & V2_HEADER_FLAG_COMPRESSION)){
if(error) *error = [ID3Parser errorForCode:ID3_PARSERDOMAIN_ECOMPR underlyingError:NULL recoveryObject:nil];
[data release];
return nil;
}
//check for unsynchronisation
const char* id3BodyBegin = id3HeaderBegin + ID3_HEADER_LENGTH;
if((char)V4_HEADER_FLAG_UNSYNC & flags){
if(majorVersion == ID3_VERSION_3 || majorVersion == ID3_VERSION_2){
NSLog(@"Tag has been unsynchronised\n");
NSData *tagBody = [ID3Parser unsyncData:[NSData dataWithBytes:id3HeaderBegin + ID3_HEADER_LENGTH length:tagSize]];
tagSize = [tagBody length];
id3BodyBegin = (const char*)[tagBody bytes];
[tagBody retain];
[data release];
}
}
//check for extended header
int extendedHeaderSize = 0;
if(majorVersion == ID3_VERSION_3 || majorVersion == ID3_VERSION_4){//extended header only in v2.3 and younger
if((char)V4_HEADER_FLAG_EXTEND_HDR & flags){
NSLog(@"Extended header is present in mask\n");
int syncInt = *(int *)(id3BodyBegin);
if(majorVersion == ID3_VERSION_4){
extendedHeaderSize = [ID3Parser integerFromSyncsafeInteger:syncInt];
assert(extendedHeaderSize <=0xFFFFFFF);
}
else{
extendedHeaderSize = CFSwapInt32BigToHost(syncInt);
}
}
}
//Begin parsing frames
NSUInteger frameHeaderLength;
if(majorVersion == ID3_VERSION_2){ frameHeaderLength = V2_FRAMEHDR_LENGTH;}
else {frameHeaderLength = V4_FRAMEHDR_LENGTH;}
NSMutableArray *framesArray = [[NSMutableArray alloc] init];
const char* framePointer = (const char*)(id3BodyBegin + extendedHeaderSize);
const char *headerEnd = framePointer + tagSize;
while(framePointer != headerEnd && *framePointer != 0){
NSError *frameError = nil;
ID3Frame *frame = [ID3Frame getFrameFromBytes:framePointer version:majorVersion error:&frameError];
if(frame == nil){
NSLog(@"Error parsing frame. error is: %@\n", frameError);
NSUInteger errorCode = [frameError code];
if(errorCode == ID3_PARSERDOMAIN_EEMPTY || errorCode == ID3_PARSERDOMAIN_EFRAMEID){//Stop parsing: no useful information gathered
NSLog(@"Stopping parser due to unrecoverable frame parsing error.\n");
if(error) *error = [ID3Parser errorForCode:ID3_PARSERDOMAIN_EFRAME
underlyingError:frameError
recoveryObject:nil];
[framesArray release];
[data release];
return nil;
}
else{//Continue parsing: location of next frame can be found.
ID3Frame *recoveredFrame = [frameError recoveryAttempter];
NSLog(@"Ignoring frame with frame ID: %@ and ID description: %@\n\n\n", recoveredFrame.frameID, recoveredFrame.frameDescription);
framePointer = framePointer + frameHeaderLength + recoveredFrame.size;
continue;
}
}
framePointer = framePointer + frameHeaderLength + frame.size;
[framesArray addObject:frame];
}
NSArray *frameDictionaries = [ID3Parser descriptionsForFramesArray:framesArray];
[framesArray release];
return frameDictionaries;
}
+ (NSError *)errorForCode:(NSInteger)errorCode underlyingError:(NSError *)otherError recoveryObject:(id)recoverObject
{
NSString *description;
NSString *reason;
NSMutableDictionary *userInfo;
NSError *error = nil;
switch (errorCode) {
case ID3_PARSERDOMAIN_EEMPTY:
{
description = @"Error: Unable to parse. Can't parse empty data";
reason = @"Argument is nil or empty";
break;
}
case ID3_PARSERDOMAIN_ENOTFOUND:
{
description = @"Error: Unable to parse tag. Start of ID3 tag not found.";
reason = @"Start of ID3 tag not found in data.";
break;
}
case ID3_PARSERDOMAIN_EVERSION:
{
description = @"Error: Can't parse ID3 tag of unsupported version.";
reason = @"ID3 version of tag currently not supported.";
break;
}
case ID3_PARSERDOMAIN_EFRAMEID:
{
description = @"Error: Unable to parse frame. Frame ID unknown";
reason = @"Frame ID unknown";
break;
}
case ID3_PARSERDOMAIN_EENCRYP:
{
description = @"Error: Unable to parse frame, parsing of encrypted frames is not supported.";
reason = @"Frame is encrypted";
break;
}
case ID3_PARSERDOMAIN_ECOMPR:
{
description = @"Error: Unable to parse the tag, parsing of compressed data is not supported.";
reason = @"Data is compressed.";
break;
}
case ID3_PARSERDOMAIN_EENCOD:
{
description = @"Error: unable to parse frame. Could not determine text encoding.";
reason = @"Text encoding undetermined.";
break;
}
case ID3_PARSERDOMAIN_EDELIM:
{
description = @"Error: unable to parse frame. Could not find delimiter character";
reason = @"Delimiter character not found.";
break;
}
case ID3_PARSERDOMAIN_EFRAME:
{
description = @"Unable to parse tag. There was frame parsing error. Check the underlying error.";
reason = @"Unable to parse frame.";
break;
}
case ID3_PARSERDOMAIN_EFORMAT:
{
description = @"Unable to parse frame body. Frame body format is incorrect.";
reason = @"Unexpected frame body format";
break;
}
case ID3_PARSERDOMAIN_EUNKOWN:
{
description = @"Did not finish parsing frame. This frame is not currently supported. Check recovery object for frame header info.";
reason = @"Frame is not supported at this time.";
break;
}
default:
return nil;
break;
}
userInfo = [NSMutableDictionary dictionaryWithObjectsAndKeys:
description, NSLocalizedDescriptionKey, reason, NSLocalizedFailureReasonErrorKey, nil];
if(otherError != nil) [userInfo setObject:otherError forKey:NSUnderlyingErrorKey];
if(recoverObject != nil) [userInfo setObject:recoverObject forKey:NSRecoveryAttempterErrorKey];
error = [[NSError alloc] initWithDomain:ID3ParserDomain code:errorCode userInfo:userInfo];
return [error autorelease];
}
/*
Function takes a 4 byte syncsafe integer in big endian notation!!!!!.
Returns a uint32_t with the sync bits removed AND in LITTLE ENDIAN notation.
*/
+ (int)integerFromSyncsafeInteger:(int)syncsafeInt
{
int tagSize = 0;
char* tagPtr = (char*)(&tagSize);
char* sizePtr = (char *)(&syncsafeInt);
int i = 0;
for(; i<3; i++){
*tagPtr = *tagPtr | (*(sizePtr + i));
tagSize = tagSize<<7;
}
*tagPtr = *tagPtr | *(sizePtr + i);
return tagSize;
}
+ (NSArray *)descriptionsForFramesArray:(NSArray *)tagsArray
{
NSMutableArray *descriptions = [[[NSMutableArray alloc] init] autorelease];
for(id frame in tagsArray){
[descriptions addObject:[frame descriptionOfFrame]];
}
return [NSArray arrayWithArray:descriptions];
}
/*
Function takes some data as argument. Returns an NSData object with the unsynchronisation scheme reversed
(ie all byte patterns 0xFF 00 -> 0xFF)
*/
+ (NSData *)unsyncData:(NSData *)data
{
NSUInteger bufferSize = [data length];
if(!bufferSize) return nil;
char *buffer = malloc(bufferSize *sizeof(char));
const char *bytes = (const char*)[data bytes];
NSUInteger index = 0;
BOOL possibleSync = NO;
const char* ptr = bytes;
const char* ptr_end = bytes + bufferSize;
char FF = 0xFF;
for(; ptr != ptr_end; ptr++){
char value = *ptr;
if(possibleSync){
if(value) possibleSync = NO;
else{
possibleSync = NO;
continue;
}
}
if(value == FF){
possibleSync = YES;
}
buffer[index++] = value;
}
NSData *finalData = [NSData dataWithBytes:buffer length:index];
free(buffer);
return finalData;
}
@end