Advertisement
morphesus

Untitled

Sep 14th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.DataInputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6.  
  7. public class XLDContainer {
  8.     public static final String XLD_SIGNATURE = "XLD0I\0";
  9.    
  10.     // Header
  11.     public int   magic   = 0;
  12.     public byte  unknown = 0;
  13.     public short entries = 0;
  14.     public int[] length  = new int[0];
  15.     public byte[][] subfiles = new byte[0][];
  16.    
  17.     public XLDContainer(File xldFile) throws Exception {
  18.         if (xldFile.exists() && xldFile.isFile()) {
  19.             parseXLD(xldFile);
  20.         } else {
  21.             System.err.println(String.format("Invalid File: %s", xldFile.getName()));
  22.             return;
  23.         }
  24.     }
  25.    
  26.     public void parseXLD(File file) throws Exception {
  27.         FileInputStream fin = new FileInputStream(file);
  28.         DataInputStream din = new DataInputStream(fin);
  29.        
  30.         // dbg
  31.         debug(String.valueOf(swapInteger(1)));
  32.         debug(String.valueOf(swapInteger(255)));
  33.         //debug(String.valueOf(swapInteger(4278190080)));
  34.  
  35.         int    lastEntry = -1;
  36.         short  nEntries  = 0;
  37.         int[]  entrylen  = null;
  38.         String sig       = "";
  39.        
  40.         // Read signature
  41.         byte[] sigBuf = new byte[6];
  42.         din.read(sigBuf, 0, 6);
  43.         sig = new String(sigBuf);
  44.        
  45.         if (sig.equals(XLD_SIGNATURE)) {
  46.             nEntries = (short) swapShort(din.readShort());
  47.             entrylen = new int[nEntries];
  48.            
  49.             debug("nEntries: " + nEntries);
  50.            
  51.             for (int i = 0; i < nEntries; i++) {
  52.                 entrylen[i] = din.readInt();
  53.                
  54.                 debug("EntryLen[" + i + "] = " + swapInteger(entrylen[i]));
  55.             }
  56.            
  57.             byte[][] entries = new byte[nEntries][];
  58.             for(int i = 0; i < nEntries; i++)
  59.             {
  60.                 lastEntry = i;
  61.                 //din.read(entries[i], 0, entrylen[i]);
  62.             }
  63.            
  64.             din.close();
  65.         } else {
  66.             din.close();
  67.             throw new Exception("This is not a valid XLD File.");
  68.         }
  69.     }
  70.    
  71.     public static short swapShort(short val) {
  72.         return (short) (((val & 0xFF00) >> 8) + ((val & 0xFF) << 8));
  73.     }
  74.    
  75.     public static int swapInteger(int value) {
  76.         return ((value&0xFF)<<24)|((value&0xFF00)<<8)|((value&0xFF0000)>>8)|((value&0xFF000000)>>24);
  77.     }
  78.    
  79.    
  80.    
  81.     public static void debug(final String str) {
  82.         System.out.printf("[DEBUG] %s\n", str);
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement