Advertisement
r57shell

gopher bitswap

Mar 24th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.96 KB | None | 0 0
  1. #include <cstdio>
  2. #include <cstring>
  3.  
  4. unsigned char rom[0x1000000];
  5.  
  6. //            0123456789ABCDEF6789012
  7. char str[] = "abcdefghtijklmnoprsuqvw";
  8.  
  9. int convert(int a)
  10. {
  11.     int r = 0;
  12.     for (int i=0; i<23; ++i)
  13.     {
  14.         if (a&(1<<(strchr(str,'a'+i)-str)))
  15.             r|=(1<<i);
  16.     }
  17.     return r;
  18. }
  19.  
  20. int convert2(int a)
  21. {
  22.     int r = 0;
  23.     for (int i=0; i<23; ++i)
  24.     {
  25.         if (a&(1<<i))
  26.             r|=(1<<(strchr(str,'a'+i)-str));
  27.     }
  28.     return r;
  29. }
  30.  
  31. int main(int argc, char **args)
  32. {
  33.     if (argc != 4)
  34.     {
  35.         printf("gopher_bitswap.exe from|to input output");
  36.         return 0;
  37.     }
  38.     FILE *f = fopen(args[2],"rb");
  39.     if (!f)
  40.         return 0;
  41.  
  42.     fread(rom,1,sizeof(rom),f);
  43.     fclose(f);
  44.  
  45.     f = fopen(args[3],"wb");
  46.     if (!f)
  47.         return 0;
  48.  
  49.     bool from = true;
  50.     if (!strcmp(args[1],"to"))
  51.         from = false;
  52.     for (int i=0; i<sizeof(rom); ++i)
  53.     {
  54.         int idx = (from?convert(i>>1):convert2(i>>1));
  55.         unsigned char q = rom[(idx<<1)|(i&1)];
  56.         fwrite(&q,1,1,f);
  57.     }
  58.     fclose(f);
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement