Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1. import java.io.*;
  2. import java.nio.charset.StandardCharsets;
  3. import java.util.*;
  4.  
  5. public class Main {
  6.  
  7.     public static TreeMap<Long, Long> blocks = new TreeMap<>();
  8.     public static Set<Long> offsets = new HashSet<>();
  9.     public static Set<Long> block_offsets = new HashSet<>();
  10.     public static Set<Long> block_offsets2 = new HashSet<>();
  11.     public static long file_length;
  12.  
  13.     public static void main(String[] args) {
  14.  
  15.         try {
  16. //            File file = new File("hello.bin");
  17.             File file = new File("geperion2006.gmail.com.bin");
  18.  
  19.             RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
  20.  
  21.  
  22.             file_length = file.length();
  23.             System.out.println("File length: " + file_length);
  24.             offsets.add(0L);
  25.  
  26.             // Read first block
  27.             block_offsets = readBlock(randomAccessFile, 0L);
  28.  
  29.             // Read rest blocks
  30.             while (!block_offsets.isEmpty() || !block_offsets2.isEmpty()) {
  31.  
  32.                 for (long offset : block_offsets) {
  33.                     block_offsets2.addAll(readBlock(randomAccessFile, offset));
  34.                 }
  35.                 block_offsets.clear();
  36.  
  37.                 for (long offset : block_offsets2) {
  38.                     block_offsets.addAll(readBlock(randomAccessFile, offset));
  39.                 }
  40.                 block_offsets2.clear();
  41.  
  42. //                 System.out.println(blocks.size());
  43.             }
  44.  
  45.             System.out.println("=================");
  46.  
  47.             // Print hidden message
  48.             while (!blocks.isEmpty()) {
  49.                 Map.Entry<Long, Long> entry = blocks.pollFirstEntry();
  50.                 long last_offset = entry.getKey() + entry.getValue();
  51.                 long next_offset;
  52.  
  53.                 if (!blocks.isEmpty()) {
  54.                     next_offset = blocks.firstKey();
  55.                 } else {
  56.                     next_offset = file_length;
  57.                 }
  58.  
  59.                 if (last_offset < next_offset) {
  60.                     int read = (int) (next_offset - last_offset);
  61.                     try {
  62.                         byte[] tmp = new byte[read];
  63.                         randomAccessFile.seek(last_offset);
  64.                         randomAccessFile.read(tmp, 0, read);
  65.  
  66.                         String str = new String(tmp, 0, read, StandardCharsets.UTF_8);
  67.                         System.out.print(str);
  68.  
  69.                     } catch (IOException e) {
  70.                         e.printStackTrace();
  71.                     }
  72.                 }
  73.             }
  74.         } catch (FileNotFoundException e) {
  75.             e.printStackTrace();
  76.         } finally {
  77.         }
  78.  
  79.     }
  80.  
  81.     public static Set<Long> readBlock(RandomAccessFile randomAccessFile, long offset) {
  82.         Set<Long> block_offsets = new HashSet<>();
  83. //          System.out.println("Read block with offset: " + offset);
  84.         try {
  85.             randomAccessFile.seek(offset);
  86.             // read block size
  87.             long block_size = readNextVarInt(randomAccessFile);
  88. //            System.out.println("Read block size: " + block_size);
  89.  
  90.  
  91.             if (block_size < 1) {
  92.                 System.out.println("Invalid block size: " + block_size);
  93.                 return new HashSet<>();
  94.             } else {
  95.                 blocks.put(offset, block_size);
  96.             }
  97.  
  98.             // read block pointers
  99.             block_offsets = readPointers(randomAccessFile, block_size);
  100.  
  101.         } catch (IOException e) {
  102.             e.printStackTrace();
  103.         }
  104.         return block_offsets;
  105.     }
  106.  
  107.  
  108.     public static Set<Long> readPointers(RandomAccessFile raFile, long block_size) {
  109.         long value = 0;
  110.         int shift = 0;
  111.  
  112.         Set<Long> block_offsets = new HashSet<>();
  113.  
  114.         // Calculate size of VarInt size
  115.         int size_block_size = 1;
  116.         long max_block_size = 0x7f;
  117.         while (block_size > max_block_size) {
  118.             max_block_size |= 0x7F << (size_block_size * 7);
  119.             size_block_size++;
  120.         }
  121.  
  122.         byte[] data = new byte[(int) block_size - size_block_size];
  123.         try {
  124.             raFile.read(data, 0, (int) block_size - size_block_size);
  125.  
  126.             for (int i = 0; i < block_size - size_block_size && data[i] != 0; i++) {
  127.                 value |= (data[i] & 0x7F) << shift;
  128.                 if ((data[i] & 0x80) != 0) {
  129.                     shift += 7;
  130.                 } else {
  131.                     if (value > 0 && value < file_length && !offsets.contains(value))
  132.                         block_offsets.add(value);
  133.                     value = 0;
  134.                     shift = 0;
  135.                 }
  136.             }
  137.             //        System.out.println(block_offsets);
  138.             offsets.addAll(block_offsets);
  139.         } catch (IOException e) {
  140.             e.printStackTrace();
  141.         }
  142.         return block_offsets;
  143.     }
  144.  
  145.     public static long readNextVarInt(RandomAccessFile raFile) {
  146.         long value = 0;
  147.         byte data;
  148.         int shift = 0;
  149.         try {
  150.             while ((data = (byte) raFile.read()) != -1) {
  151.                 value |= (data & 0x7F) << shift;
  152.                 if ((data & 0x80) != 0) {
  153.                     shift += 7;
  154.                 } else {
  155.                     break;
  156.                 }
  157.             }
  158.         } catch (IOException e) {
  159.             e.printStackTrace();
  160.         }
  161.         return value;
  162.     }
  163.  
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement