Advertisement
xerpi

filexorer

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