Advertisement
antiquekid3

RK05 Converter

Mar 5th, 2014
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* Compile with -DDJG_TO_MAC or -DMAC_TO_DJG */
  6.  
  7. #ifdef DJG_TO_MAC
  8. #define NUM_IN 4
  9. #define NUM_OUT 3
  10. #endif
  11.  
  12. #ifdef MAC_TO_DJG
  13. #define NUM_IN 3
  14. #define NUM_OUT 4
  15. #endif
  16.  
  17. /*
  18.  * convert from Mac to dumprest
  19.  * convert from dumprest to Mac
  20.  */
  21.  
  22. FILE *input;
  23. FILE *output;
  24.  
  25. char in_file[256];
  26. char out_file[256];
  27.  
  28. char in_buf[6];
  29. char out_buf[6];
  30.  
  31. int c, count_in, count_out = 0;
  32.  
  33. int main(int argc, char* argv[])
  34. {
  35.     if (argc == 3)
  36.     {
  37.         strcpy(in_file, argv[1]);
  38.         strcpy(out_file, argv[2]);
  39.     }
  40.     else
  41.     {
  42.         printf("Usage: %s [input] [output]\n", argv[0]);
  43.         exit(1);
  44.     }
  45.     input = fopen(in_file, "r");
  46.     output = fopen(out_file, "w");
  47.    
  48.     if (input == NULL)
  49.     {
  50.         fprintf(stderr, "On file %s ", in_file);
  51.         perror("open failed");
  52.         exit(1);
  53.     }
  54.     if (output == NULL)
  55.     {
  56.         fprintf(stderr, "On file %s ", out_file);
  57.         perror("open failed");
  58.         exit(1);
  59.     }
  60.  
  61.     while (!feof(input))
  62.     {
  63.         int num_in = NUM_IN;
  64.         int num_out = NUM_OUT;
  65.        
  66.         if ((c = fread(in_buf, 1, num_in, input)) < 0)
  67.         {
  68.             perror("file read failed");
  69.             exit(1);
  70.         }
  71.         count_in += c;
  72.        
  73.         if (!feof(input) && c != num_in)
  74.         {
  75.             fprintf(stderr, "Failed to read %d bytes! Got %d bytes.\n", num_in, c);
  76.             exit(1);
  77.         }
  78.         else if (feof(input))
  79.             break;
  80.  
  81. //Mac: ABCD EFGH = aaabbbcc cdddeeef ffggghhh
  82. //DJG: ABCD EFGH = bbcccddd 0000aaab ffggghhh 0000eeef
  83.  
  84. #ifdef DJG_TO_MAC
  85.         out_buf[0] = ((in_buf[1] << 4) & 0xF0) | ((in_buf[0] >> 4) & 0x0F);
  86.         out_buf[1] = ((in_buf[0] << 4) & 0xF0) | (in_buf[3] & 0xF);
  87.         out_buf[2] = in_buf[2];
  88. #elif defined(MAC_TO_DJG)
  89.         out_buf[0] = ((in_buf[0] << 4) & 0xF0) | ((in_buf[1] >> 4) & 0x0F);
  90.         out_buf[1] = (in_buf[0] >> 4) & 0x0F;
  91.         out_buf[2] = in_buf[2];
  92.         out_buf[3] = in_buf[1] & 0x0F;
  93. #endif
  94.        
  95.         if ((c = fwrite(out_buf, 1, num_out, output)) < 0)
  96.         {
  97.             perror("file write failed");
  98.             exit(1);
  99.         }
  100.         count_out += c;
  101.         if (c != num_out)
  102.         {
  103.             fprintf(stderr, "Failed to write %d bytes! Got %d bytes.\n", num_in, c);
  104.             exit(1);
  105.         }
  106.     }
  107.     printf("Read %d bytes and wrote %d bytes\n", count_in, count_out);
  108.     return 0;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement