/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.atolsystems.memop;
import com.atolsystems.atolutilities.MemRange;
import com.atolsystems.atolutilities.CommandLine.ArgDef;
import com.atolsystems.atolutilities.ProgrammingChunk;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author sebastien.riou
*/
public abstract class AbstractMemScrambler implements MemScrambler{
protected List<MemRange> dataKeysMap;
protected List<MemRange> addressKeysMap;
protected Boolean encrypt=null;
protected boolean skipDataScrambling;
protected boolean skipAddrScrambling;
@Override
public void apply(List<ProgrammingChunk> image) {
if((false==encrypt) && (!skipAddrScrambling)){//apply address unscrambling first
MemRange.checkMemRangesBoundaries(image, addressKeysMap);
addressScrambling(encrypt,image);
}
if(!skipDataScrambling){
//check for overlap and rearrange chunks according to key ranges
MemRange.checkMemRangesBoundaries(image, dataKeysMap);
dataScrambling(encrypt,image,dataKeysMap);
}
if(encrypt && (!skipAddrScrambling)){//apply address scrambling
MemRange.checkMemRangesBoundaries(image, addressKeysMap);
addressScrambling(encrypt,image);
}
}
abstract public void addressScrambling(boolean encrypt, List<ProgrammingChunk> image);
abstract public ProgrammingChunk processChunk(boolean encrypt, ProgrammingChunk chunk, byte []key);
private void dataScrambling(boolean encrypt, List<ProgrammingChunk> image, List<MemRange> dataKeysMap){
for(int i=0;i<image.size();i++){
ProgrammingChunk chunk=image.get(i);
MemRange range=MemRange.address2MemRange(dataKeysMap,chunk.getAddress());
if(null!=range){
byte []key=range.key;
image.remove(i);
image.add(i,processChunk(encrypt, chunk, key));
}
}
}
@Override
public LinkedList<ArgDef> getArgDefs() {
return null;
}
@Override
public void setScramblingContext(boolean encrypt, List<MemRange> dataKeysMap, List<MemRange> addressKeysMap, boolean skipDataScrambling, boolean skipAddrScrambling) {
this.encrypt=encrypt;
this.dataKeysMap=dataKeysMap;
this.addressKeysMap=addressKeysMap;
this.skipDataScrambling = skipDataScrambling;
this.skipAddrScrambling = skipAddrScrambling;
}
}