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
|
#import "XADSWFTagParser.h"
#import "CSFileHandle.h"
#import "CSZlibHandle.h"
NSString *SWFWrongMagicException=@"SWFWrongMagicException";
NSString *SWFNoMoreTagsException=@"SWFNoMoreTagsException";
@implementation XADSWFTagParser
+(XADSWFTagParser *)parserWithHandle:(CSHandle *)handle
{
return [[[XADSWFTagParser alloc] initWithHandle:handle] autorelease];
}
+(XADSWFTagParser *)parserForPath:(NSString *)path
{
CSFileHandle *handle=[CSFileHandle fileHandleForReadingAtPath:path];
return [[[XADSWFTagParser alloc] initWithHandle:handle] autorelease];
}
-(id)initWithHandle:(CSHandle *)handle
{
if((self=[super init]))
{
fh=[handle retain];
}
return self;
}
-(void)dealloc
{
[fh release];
[super dealloc];
}
-(void)parseHeader
{
uint8_t magic[4];
[fh readBytes:4 toBuffer:magic];
version=magic[3];
totallen=[fh readUInt32LE];
if((magic[0]!='F'&&magic[0]!='C')||magic[1]!='W'||magic[2]!='S')
[NSException raise:SWFWrongMagicException format:@"Not a Shockwave Flash file."];
if(magic[0]=='C')
{
CSZlibHandle *zh=[CSZlibHandle zlibHandleWithHandle:fh];
[fh release];
fh=[zh retain];
compressed=YES;
}
else
{
compressed=NO;
}
rect=SWFParseRect(fh);
fps=[fh readUInt16LE];
frames=[fh readUInt16LE];
nexttag=[fh offsetInFile];
currtag=0;
currlen=0;
currframe=0;
}
-(int)version { return version; }
-(BOOL)isCompressed { return compressed; }
-(SWFRect)rect { return rect; }
-(int)frames { return frames; }
-(int)framesPerSecond { return fps; }
-(int)nextTag
{
if(!nexttag) [NSException raise:SWFNoMoreTagsException format:@"No more tags available in the SWF file."];
if(currtag==SWFShowFrameTag) currframe++;
[fh seekToFileOffset:nexttag];
int tagval=[fh readUInt16LE];
currtag=tagval>>6;
if(currtag==0)
{
nexttag=0;
return 0;
}
currlen=tagval&0x3f;
if(currlen==0x3f) currlen=[fh readUInt32LE];
nexttag=[fh offsetInFile]+currlen;
return currtag;
}
-(int)tag { return currtag; }
-(int)tagLength { return currlen; }
-(int)tagBytesLeft { return nexttag-[fh offsetInFile]; }
-(int)frame { return currframe; }
-(double)time { return (double)currframe/((double)fps/256.0); }
-(CSHandle *)handle { return fh; }
-(CSHandle *)tagHandle { return [fh subHandleOfLength:[self tagBytesLeft]]; }
-(NSData *)tagContents { return [fh readDataOfLength:[self tagBytesLeft]]; }
-(void)parseDefineSpriteTag
{
spriteid=[fh readUInt16LE];
subframes=[fh readUInt16LE];
nextsubtag=[fh offsetInFile];
subtag=0;
sublen=0;
subframe=0;
}
-(int)spriteID { return spriteid; }
-(int)subFrames { return subframes; }
-(int)nextSubTag
{
if(!nextsubtag) [NSException raise:SWFNoMoreTagsException format:@"No more subtags available in the SWF file."];
if(subtag==SWFShowFrameTag) subframe++;
[fh seekToFileOffset:nextsubtag];
int tagval=[fh readUInt16LE];
subtag=tagval>>6;
if(subtag==0)
{
nextsubtag=0;
return 0;
}
sublen=tagval&0x3f;
if(sublen==0x3f) sublen=[fh readUInt32LE];
nextsubtag=[fh offsetInFile]+sublen;
return subtag;
}
-(int)subTag { return subtag; }
-(int)subTagLength { return sublen; }
-(int)subTagBytesLeft { return nextsubtag-[fh offsetInFile]; }
-(int)subFrame { return subframe; }
-(double)subTime { return (double)subframe/((double)fps/256.0); }
@end
|