1 50 51 package org.openlaszlo.iv.flash.parser; 52 53 import java.io.*; 54 import java.util.*; 55 import java.util.zip.*; 56 57 import org.openlaszlo.iv.flash.api.*; 58 import org.openlaszlo.iv.flash.util.*; 59 60 61 public final class Parser extends FlashBuffer { 62 63 private int tagStartPos; 64 private int tagDataPos; 65 private int tagLength; 66 private int tagEndPos; 67 private int tagCode; 68 69 private byte[] temp_bufb; 70 private char[] temp_bufc; 71 72 private FlashFile file; 74 75 public Parser() { 76 } 77 78 public int getTag() { 79 tagStartPos = getPos(); 80 int code = getUWord(); 81 int length = code & 0x3f; 82 code = code >> 6; 83 84 if( length == 0x3f ) { 85 length = getUDWord(); 86 } 87 88 tagDataPos = getPos(); 89 tagLength = length; 90 tagCode = code; 91 tagEndPos = tagDataPos+length; 92 return code; 93 } 94 95 public void addDef( FlashDef def ) { 96 file.addDef(def); 97 } 98 99 public FlashDef getDef( int id ) { 100 return file.getDef(id); 101 } 102 103 public void addDefToLibrary( String name, FlashDef def ) { 104 file.addDefToLibrary( name, def ); 105 } 106 107 public FlashDef getDefFromLibrary( String name ) { 108 return file.getDefFromLibrary( name ); 109 } 110 111 public int getTagStartPos() { 112 return tagStartPos; 113 } 114 public int getTagDataPos() { 115 return tagDataPos; 116 } 117 public int getTagLength() { 118 return tagLength; 119 } 120 public int getTagEndPos() { 121 return tagEndPos; 122 } 123 public int getTagCode() { 124 return tagCode; 125 } 126 127 public byte[] getTempByteBuf( int size ) { 128 if( temp_bufb == null || temp_bufb.length < size ) { 129 temp_bufb = new byte[size]; 130 } 131 return temp_bufb; 132 } 133 134 public char[] getTempCharBuf( int size ) { 135 if( temp_bufc == null || temp_bufc.length < size ) { 136 temp_bufc = new char[size]; 137 } 138 return temp_bufc; 139 } 140 141 public FlashObject newUnknownTag() { 142 return new UnparsedTag( tagCode, getBuf(), tagStartPos, tagEndPos ); 143 } 144 145 public void skipLastTag() { 146 setPos( tagEndPos ); 147 } 148 149 public FlashFile getFile() { 150 return file; 151 } 152 153 156 public void parseStream( InputStream is, FlashFile file ) throws IVException, IOException { 157 158 byte[] fileHdr = new byte[8]; 159 160 if( is.read(fileHdr, 0, 8) != 8 ) { 161 throw new IVException( Resource.CANTREADHEADER, new Object [] {file.getFullName()} ); 162 } 163 164 if( fileHdr[1] != 'W' || fileHdr[2] != 'S' ) { 165 throw new IVException( Resource.ILLEGALHEADER, new Object [] {file.getFullName()} ); 166 } 167 168 boolean isCompressed = false; 169 if( fileHdr[0] == 'C' ) { 170 isCompressed = true; 171 } else if( fileHdr[0] != 'F' ) { 172 throw new IVException( Resource.ILLEGALHEADER, new Object [] {file.getFullName()} ); 173 } 174 175 int fileSize = Util.getUDWord(fileHdr[4], fileHdr[5], fileHdr[6], fileHdr[7]); 177 178 if( fileSize < 21 ) { 179 throw new IVException( Resource.FILETOOSHORT, new Object [] {file.getFullName()} ); 180 } 181 182 FlashBuffer fb; 183 try { 184 fb = new FlashBuffer(fileSize+8); 185 } catch( OutOfMemoryError e ) { 186 throw new IVException( Resource.FILETOOBIG, new Object [] {file.getFullName()} ); 187 } 188 189 fb.writeArray(fileHdr, 0, 8); 190 191 if( isCompressed ) { 192 is = new InflaterInputStream(is); 193 } 194 195 fb.write(is); 196 197 file.setCompressed(isCompressed); 198 _parseBuffer(fb, file); 199 } 200 201 204 public void parseBuffer( FlashBuffer fob, FlashFile file ) throws IVException { 205 boolean isCompressed = false; 206 207 if( fob.getBuf()[0] == 'C' ) { 208 isCompressed = true; 209 } else if( fob.getBuf()[0] != 'F' ) { 210 throw new IVException( Resource.ILLEGALHEADER, new Object [] {file.getFullName()} ); 211 } 212 213 if( isCompressed ) { int fileSize = fob.getDWordAt(4); 215 216 if( fileSize < 21 ) { 217 throw new IVException( Resource.FILETOOSHORT, new Object [] {file.getFullName()} ); 218 } 219 220 FlashBuffer fb; 221 try { 222 fb = new FlashBuffer(fileSize+8); 223 } catch( OutOfMemoryError e ) { 224 throw new IVException( Resource.FILETOOBIG, new Object [] {file.getFullName()} ); 225 } 226 227 fb.writeArray(fob.getBuf(), 0, 8); 228 229 try { 230 fb.write( new InflaterInputStream(fob.getInputStream(8)) ); 231 } catch( IOException e ) { 232 throw new IVException(e); 233 } 234 fob = fb; 235 } 236 237 file.setCompressed(isCompressed); 238 _parseBuffer(fob, file); 239 } 240 241 243 private void _parseBuffer( FlashBuffer fb, FlashFile file ) throws IVException { 244 this.file = file; 245 246 init( fb.getBuf(), 0, fb.getSize() ); 247 248 file.setVersion( (int) getByteAt(3) ); 250 251 setPos(8); 252 253 file.setFrameSize( getRect() ); 255 256 file.setFrameRate( getUWord() ); 258 259 file.setMainScript( Script.parse( this, true ) ); 261 } 262 263 } 264 | Popular Tags |