Advertisement
Guest User

CRC 32 Java translation

a guest
Nov 17th, 2017
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.40 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.LinkedList;
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileReader;
  6. import java.io.IOException;
  7. import java.io.RandomAccessFile;
  8.  
  9. public class testClass
  10. {
  11.     public long[] EDC_crctable;//call proper method here
  12.     public long[][] L2sq;
  13.     private int[] sector;
  14.  
  15.     public testClass()
  16.     {
  17.         EDC_crctable = genEDCcrctable();
  18.         L2sq = genL2sq();
  19.         //printData();
  20.         //private int crc32(File file, long sectorOffset, int expectedNumOfBytes)
  21.         System.out.println("" + crc32(new File("sagafrontier.iso"), 0, 2352));
  22.         //^output of 1449755743
  23.     }
  24.  
  25.     public static void main(String[] args)
  26.     {
  27.         testClass t = new testClass();
  28.     }
  29.  
  30.     public long[] genEDCcrctable()
  31.     {
  32.         LinkedList<Long> tempList = new LinkedList<Long>();
  33.         long[] tempArray = null;
  34.         BufferedReader br = null;
  35.         try
  36.         {
  37.             br = new BufferedReader(new FileReader(new File("EDC_crctable.txt")));
  38.             String line = br.readLine();
  39.             while (line != null)
  40.             {
  41.                 tempList.add(Long.decode(line));
  42.                 line = br.readLine();
  43.             }
  44.             tempArray = new long[(tempList.size())];
  45.             int counter = 0;
  46.             for (Long curr : tempList)
  47.             {
  48.                 tempArray[counter] = (long)curr;
  49.                 counter ++;
  50.             }
  51.         }
  52.         catch (Exception e)
  53.         {
  54.             e.printStackTrace();
  55.         }
  56.         finally
  57.         {
  58.             if (br != null)
  59.             {
  60.                 try
  61.                 {
  62.                     br.close();
  63.                 }
  64.                 catch (Exception e) {}
  65.             }
  66.             return tempArray;
  67.         }
  68.     }
  69.  
  70.     public long[][] genL2sq()
  71.     {
  72.         LinkedList<Long[]> tempList = new LinkedList<Long[]>();
  73.         long[][] tempArray = null;
  74.         BufferedReader br = null;
  75.         try
  76.         {
  77.             br = new BufferedReader(new FileReader(new File("L2sq.txt")));
  78.             String line = br.readLine();
  79.             int columnLength = line.split(",").length;
  80.             int rowLength = 0;
  81.             while (line != null)
  82.             {
  83.                 String[] lineStringArray = line.split(",");
  84.                 Long[] lineLongArray = new Long[lineStringArray.length];
  85.                 int i = 0;
  86.                 for (String part : lineStringArray)
  87.                 {
  88.                     lineLongArray[i] = Long.decode(lineStringArray[i]);
  89.                     i ++;
  90.                 }
  91.                 tempList.add(lineLongArray);
  92.                 line = br.readLine();
  93.                 rowLength ++;
  94.             }
  95.  
  96.             tempArray = new long[rowLength][columnLength];
  97.             for (int rowCounter = 0; rowCounter < rowLength; rowCounter ++)
  98.             {
  99.                 for (int columnCounter = 0; columnCounter < columnLength; columnCounter ++)
  100.                 {
  101.                     if (columnCounter == 0)
  102.                     {
  103.                         System.out.print("");
  104.                     }
  105.                     tempArray[rowCounter][columnCounter] = tempList.get(rowCounter)[columnCounter];
  106.                 }
  107.             }
  108.         }
  109.         catch (Exception e)
  110.         {
  111.             e.printStackTrace();
  112.         }
  113.         finally
  114.         {
  115.             if (br != null)
  116.             {
  117.                 try
  118.                 {
  119.                     br.close();
  120.                 }
  121.                 catch (Exception e) {}
  122.             }
  123.             return tempArray;
  124.         }
  125.     }
  126.  
  127.     //private int crc32(int[] sctr)
  128.     private int crc32(File file, long sectorOffset, int expectedNumOfBytes)
  129.     {
  130.         RandomAccessFile raf = null;
  131.         int returnVal = 0;
  132.         try
  133.         {
  134.             raf = new RandomAccessFile(file, "r");
  135.             raf.seek(sectorOffset * 2352);//need to account for location in sector
  136.             byte[] sector = new byte[expectedNumOfBytes];
  137.             for (int totalBytesRead=0, offset=0; totalBytesRead < expectedNumOfBytes; )
  138.             {
  139.                 int numberOfBytesRemaining = expectedNumOfBytes - totalBytesRead;
  140.                 int bytesRead = raf.read(sector, offset, numberOfBytesRemaining);
  141.                 if (bytesRead == -1)
  142.                 {
  143.                     throw new IOException("The expected number of bytes wasn't found for " + file);
  144.                 }
  145.                 totalBytesRead += bytesRead;
  146.                 offset += bytesRead;
  147.             }
  148.             //byte[] sector = raf.read
  149.             //^look up how you did read(byte[]) in snakeoil
  150.             //int expectedNumberOfBytes = entrySize;
  151.             //byte[] byteBuffer = new byte[expectedNumberOfBytes];
  152.             //for (int totalBytesRead=0, offset=0; totalBytesRead < expectedNumberOfBytes; ) {
  153.             //    int numberOfBytesRemaining = expectedNumberOfBytes - totalBytesRead;
  154.             //    int bytesRead = zis.read(byteBuffer, offset, numberOfBytesRemaining);
  155.             //    if (bytesRead == -1) {
  156.             //        throw new IOException("The expected number of bytes was not found for: " + zipEntry.getName());
  157.             //    }
  158.             //    totalBytesRead += bytesRead;
  159.             //    offset += bytesRead;
  160.             //
  161.             //test above to see if it works after you write the rest of it.
  162.            
  163.             for (int i = 0; i < sector.length; i++)
  164.             {
  165.                 //not sure if long or int, check later
  166.                 returnVal = ((int)EDC_crctable[(returnVal ^ sector[i]) & 0xFF] ^ (returnVal >> 8));
  167.             }
  168.         }
  169.         catch (Exception e)
  170.         {
  171.             e.printStackTrace();
  172.         }
  173.         finally
  174.         {
  175.             if (raf != null)
  176.             {
  177.                 try
  178.                 {
  179.                     raf.close();
  180.                     return returnVal;
  181.                 }
  182.                 catch (Exception e) {}
  183.             }
  184.         }
  185.         return returnVal;
  186.     }
  187.  
  188.     public void printData()
  189.     {
  190.         int c = 0;
  191.         for (long i : EDC_crctable)
  192.         {
  193.             System.out.println(c + ":\t" + i + "");
  194.             c ++;
  195.         }
  196.         for (long[] arr : L2sq)
  197.         {
  198.             for (long i : arr)
  199.             {
  200.                 System.out.print(i + ",");
  201.             }
  202.             System.out.println();
  203.         }
  204.     }
  205.  
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement