Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* by xerpi */
- #include <stdio.h>
- #define min(num1, num2) (num1<num2 ? num1 : num2)
- #define BUF_SIZE 1
- int file_size(FILE *file);
- void xorer( FILE *input1, FILE *input2, FILE *output);
- int main(int argc, char **argv){
- if( argc < 3 ){
- printf("Too low arguments.\nUse: xorer <file1> <file2> (<output>)");
- return -1;
- }
- FILE *input1;
- FILE *input2;
- FILE *output;
- char *output_filename;
- //Check if the 4th argument exists, if it exists, it'll be the name of the output file
- if( argc >= 4)
- output_filename = argv[3];
- else
- output_filename = "xored";
- //Open the files
- input1 = fopen (argv[1], "r");
- if(!input1){
- printf("File %s could not be opened.", argv[1]);
- return -1;
- }
- input2 = fopen (argv[2], "r");
- if(!input2){
- printf("File %s could not be opened.", argv[2]);
- return -1;
- }
- output = fopen (output_filename, "w+");
- //Xor the files
- xorer(input1, input2, output);
- //Close the files
- fclose(input1);
- fclose(input2);
- fflush(output);
- fclose(output);
- return 0;
- }
- int file_size(FILE *file){
- fseek(file, 0, SEEK_END);
- return ftell(file);
- }
- void xorer( FILE *input1, FILE *input2, FILE *output){
- int i, min_size, size1, size2;
- unsigned char buffer, data1, data2;
- //Check the file size
- size1 = file_size(input1);
- size2 = file_size(input2);
- //Set the cursor at the begin of the files
- fseek(input1, 0, SEEK_SET);
- fseek(input2, 0, SEEK_SET);
- min_size = min(size1, size2);
- printf("Size file 1: %i\nSize file 2: %i\nOutput file size: %i", size1, size2, min_size);
- //Xor algorithm
- for(i = 0; i< min_size; i++){
- fread(&data1, 1, BUF_SIZE, input1);
- fread(&data2, 1, BUF_SIZE, input2);
- buffer = data1 ^ data2;
- fwrite(&buffer, 1, BUF_SIZE, output);
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement