Created
November 7, 2016 09:03
-
-
Save elw00d/3fa607851c70fad7fc1417f8acb0f426 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ru.yandex; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import java.nio.file.Paths; | |
/** | |
* @author elwood | |
*/ | |
public class Raid { | |
private final static int BLOCK_SIZE = 4 * 1024; | |
public static void copy(byte[] dest, byte[] src, int block) { | |
for (int i = block * BLOCK_SIZE; i < (block + 1) * BLOCK_SIZE; i++) { | |
dest[i] = src[i]; | |
} | |
} | |
public static void copyXor(byte[] dest, byte[] src1, byte[] src2, int block) { | |
for (int i = block * BLOCK_SIZE; i < (block + 1) * BLOCK_SIZE; i++) { | |
dest[i] = (byte) (src1[i] ^ src2[i]); | |
} | |
} | |
public static void main(String[] args) throws IOException { | |
String file1 = "/home/elwood/Downloads/ctf/disks/disk1"; | |
String file2 = "/home/elwood/Downloads/ctf/disks/disk3"; | |
String fileOut = "/home/elwood/Downloads/ctf/disks/test/output"; | |
byte[] data1 = Files.readAllBytes(Paths.get(file1)); | |
byte[] data3 = Files.readAllBytes(Paths.get(file2)); | |
byte[] result = new byte[data1.length]; | |
int currentParityDisk = 2; | |
int layer = 0; | |
while (layer < ((data1.length / BLOCK_SIZE) / (3 - 1))) { // В каждой строчке по 2 блока и 1 parity | |
switch (currentParityDisk) { | |
case 0: { | |
// В первом диске лежит disk2 ^ disk3 | |
// Во втором диске лежит первый блок (второго диска нет, берём disk1 ^ disk3) | |
// В третьем диске лежит второй блок | |
copyXor(result, data1, data3, layer * 2); | |
copy(result, data3, layer * 2 + 1); | |
break; | |
} | |
case 1: { | |
// Просто копируем с первого и третьего диска | |
copy(result, data1, layer * 2); | |
copy(result, data3, layer * 2 + 1); | |
break; | |
} | |
case 2: { | |
// В третьем диске лежит disk1 ^ disk2 | |
// Второго диска нет, поэтому получаем его через xor disk1 ^ disk3 | |
copy(result, data1, layer * 2); | |
copyXor(result, data1, data3, layer * 2 + 1); | |
} | |
} | |
// currentParityDisk = (currentParityDisk + 1) % 3; | |
currentParityDisk--; | |
if (-1 == currentParityDisk) | |
currentParityDisk = 2; | |
layer++; | |
} | |
Files.write(Paths.get(fileOut), result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment