xerpi

padxorer

Aug 30th, 2014
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.22 KB | None | 0 0
  1. /*
  2.     padxorer by xerpi
  3.     compile with:
  4.         gcc -O3 padxorer.c -o padxorer
  5.     usage:
  6.         padxorer <input file 1> <input file 2>
  7.  
  8.     the output will be <input file 1>.out
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <malloc.h>
  15.  
  16. #define BUF_SIZE (8*1024*1024)
  17.  
  18. void print_usage();
  19.  
  20. int main(int argc, char *argv[])
  21. {
  22.     int ret_val = EXIT_SUCCESS;
  23.    
  24.     if (argc < 3) {
  25.         print_usage();
  26.         ret_val = EXIT_FAILURE;
  27.         goto exit_fail_1;
  28.     }
  29.    
  30.     FILE *fd_in1, *fd_in2, *fd_out;
  31.     if (!(fd_in1 = fopen(argv[1], "rb"))) {
  32.         printf("Error opening input file 1\n");
  33.         ret_val = EXIT_FAILURE;
  34.         goto exit_fail_1;
  35.     }
  36.     if (!(fd_in2 = fopen(argv[2], "rb"))) {
  37.         printf("Error opening input file 2\n");
  38.         ret_val = EXIT_FAILURE;
  39.         goto exit_fail_2;
  40.     }
  41.    
  42.     fseek(fd_in1, 0, SEEK_END);
  43.     unsigned int in_size1 = ftell(fd_in1);
  44.     fseek(fd_in1, 0, SEEK_SET);
  45.    
  46.     fseek(fd_in2, 0, SEEK_END);
  47.     unsigned int in_size2 = ftell(fd_in2);
  48.     fseek(fd_in2, 0, SEEK_SET);
  49.    
  50.     unsigned int min_size = (in_size1 < in_size2) ? in_size1 : in_size2;
  51.  
  52.     char *out_name = malloc(strlen(argv[1]) + strlen(".out") + 1);
  53.     sprintf(out_name, "%s.out", argv[1]);
  54.    
  55.     if (!(fd_out = fopen(out_name, "wb+"))) {
  56.         printf("Cannot create output file\n");
  57.         ret_val = EXIT_FAILURE;
  58.         goto exit_fail_3;
  59.     }
  60.    
  61.     unsigned char *data_buf1 = (unsigned char *)malloc(BUF_SIZE);
  62.     unsigned char *data_buf2 = (unsigned char *)malloc(BUF_SIZE);
  63.  
  64.     #define BAR_LEN 50
  65.     unsigned int step_bytes = min_size/100;
  66.     unsigned int temp_bytes = 0;
  67.     size_t bytes_read1, bytes_read2, total_read = 0, min_read;
  68.     while ((total_read < min_size) && (bytes_read1 = fread(data_buf1, 1, BUF_SIZE, fd_in1)) &&
  69.                                       (bytes_read2 = fread(data_buf2, 1, BUF_SIZE, fd_in2))) {
  70.         min_read = (bytes_read1 < bytes_read2) ? bytes_read1 : bytes_read2;
  71.         total_read += min_read;
  72.         temp_bytes += min_read;
  73.         size_t i;
  74.         for (i = 0; i < min_read; i++) {
  75.             data_buf1[i] ^= data_buf2[i];
  76.         }
  77.         fwrite(data_buf1, 1, min_read, fd_out);
  78.        
  79.         if ((temp_bytes >= step_bytes) || (total_read == min_size)) {
  80.             temp_bytes = 0;
  81.             unsigned int percent = (unsigned int)(total_read*(100.0/min_size));
  82.             printf("%3i%% [", percent);
  83.             int j;
  84.             int bar_size = (BAR_LEN*percent)/100;
  85.             for (j = 0; j < BAR_LEN; j++) {
  86.                 if (j < bar_size) printf("=");
  87.                 else if (j == bar_size) printf(">");
  88.                 else printf(" ");
  89.             }
  90.             printf("]\r");
  91.             fflush(stdout);
  92.         }
  93.     }
  94.     fflush(fd_out);
  95.     fclose(fd_out);
  96.     free(data_buf1);
  97.     free(data_buf2);
  98.     printf("\nFinished!\n");
  99. exit_fail_3:
  100.     free(out_name);
  101.     fclose(fd_in2);
  102. exit_fail_2:
  103.     fclose(fd_in1);
  104. exit_fail_1:
  105.     return ret_val;
  106. }
  107.  
  108. void print_usage()
  109. {
  110.     printf("padxorer by xerpi\n"
  111.            "usage: padxorer <input file 1> <input file 2>\n"
  112.            "the output will be <input file 1>.out\n");
  113. }
Add Comment
Please, Sign In to add comment