1
Name:
Anonymous
2007-07-24 4:27
ID:i4DLiYyV
yes, the monolith kid is retarded and his ADD-induced idea of ripping off onetime pads could never work because that's not how the law works, but that's not the point here. Here's my implementation
import qualified Data.ByteString.Lazy as B
import Data.Bits
import System.Environment
main = do
(file1:file2:file3:_) <- getArgs
c1 <- B.readFile file1
c2 <- B.readFile file2
B.writeFile file3 (B.pack (B.zipWith xor c1 (B.cycle c2)))
8
Name:
Anonymous
2007-07-24 5:36
ID:Heaven
int monolith_internalXORTwoFiles(
char *inFileAPath,
char *inFileBPath,
char *inDestinationPath,
int inFileNumber = 0,
char (*inProgressHandlerFunction)( int, int, void * ) = NULL,
void *inExtraHandlerArgument = NULL ) {
FILE *fileA = fopen( inFileAPath, "rb" );
if( fileA == NULL ) {
return -1;
}
FILE *fileB = fopen( inFileBPath, "rb" );
if( fileB == NULL ) {
return -1;
}
FILE *fileOut = fopen( inDestinationPath, "wb" );
if( fileOut == NULL ) {
return -1;
}
char sawError = false;
int totalBytesWritten = 0;
int bufferSize = 4000;
unsigned char *bufferA = new unsigned char[ bufferSize ];
unsigned char *bufferB = new unsigned char[ bufferSize ];
unsigned char *bufferOut = new unsigned char[ bufferSize ];
char seenEndOfB = false;
while( ! sawError && ! seenEndOfB ) {
// read a block from B
int numReadB = fread( bufferB, 1, bufferSize, fileB );
if( numReadB > 0 ) {
if( numReadB < bufferSize ) {
// we have seen the end of B
seenEndOfB = true;
}
// read enough bytes from A to match the bytes from B
int numReadA =
monolith_internalReadFileDataWrapAround( fileA, bufferA,
numReadB );
if( numReadA == numReadB ) {
// XOR the bytes of A and B together
for( int i=0; i<numReadB; i++ ) {
bufferOut[i] = bufferA[i] ^ bufferB[i];
}
// write the XORed bytes out
int numWritten =
fwrite( bufferOut, 1, numReadB, fileOut );
if( numWritten != numReadB ) {
sawError = true;
}
else {
totalBytesWritten += numWritten;
if( inProgressHandlerFunction != NULL ) {
char keepGoing = inProgressHandlerFunction(
inFileNumber,
totalBytesWritten,
inExtraHandlerArgument );
if( !keepGoing ) {
sawError = true;
}
}
}
}
else {
sawError = true;
}
}
else {
sawError = true;
}
}
fclose( fileOut );
fclose( fileA );
fclose( fileB );
delete [] bufferA;
delete [] bufferB;
delete [] bufferOut;
if( sawError ) {
return -1;
}
else {
return totalBytesWritten;
}
}