Advertisement
NovaYoshi

Game Genie converter

Apr 29th, 2015
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.16 KB | None | 0 0
  1. char *NESGenie(const char *Input, char *Output) {
  2.   char Key[] = "APZLGITYEOXUKSVN", Upper[512];
  3.   int i, Addr=0, Data=0, Compare=0;
  4.   strcpy(Upper, Input);
  5.   for(i=0;Upper[i];i++)
  6.     Upper[i] = toupper(Upper[i]);
  7.  
  8.   if(strlen(Input) == 6 || strlen(Input) == 8) {
  9.     char n[strlen(Input)];
  10.     for(i=0;Input[i];i++) {
  11.       char *Find = strchr(Key, Upper[i]);
  12.       if(!Find) return "Unrecognized character in code";
  13.       n[i] = Find - Key;
  14.     }
  15.     Addr = 0x8000 + (((n[3] & 7) << 12) | ((n[5] & 7) << 8) | ((n[4] & 8) << 8) \
  16.         | ((n[2] & 7) << 4) | ((n[1] & 8) << 4) | (n[4] & 7) | (n[3] & 8));
  17.     if(strlen(Input) == 6) {
  18.       Data = ((n[1] & 7) << 4) | ((n[0] & 8) << 4) | (n[0] & 7) | (n[5] & 8);
  19.       sprintf(Output, "%.4x %.2x", Addr, Data);
  20.       return Output;
  21.      } else {
  22.       Data = ((n[1] & 7) << 4) | ((n[0] & 8) << 4) | (n[0] & 7) | (n[7] & 8);
  23.       Compare = ((n[7] & 7) << 4) | ((n[6] & 8) << 4) | (n[6] & 7) | (n[5] & 8);
  24.       sprintf(Output, "%.4x %.2x %.2x", Addr, Data, Compare);
  25.       return Output;
  26.     }
  27.   } else if(strlen(Input) == 7 || strlen(Input) == 10) {
  28.     int UseCompare = (strlen(Input)==10);
  29.     Addr = strtol(Input+0, NULL, 16);
  30.     Data = strtol(Input+5, NULL, 16);
  31.     if(!UseCompare) {
  32.       Output[0] = Key[(Data>>4 & 8) | (Data & 7)];
  33.       Output[1] = Key[(Addr>>4 & 8) | (Data>>4 & 7)];
  34.       Output[2] = Key[8 | (Addr>>4 & 7)];
  35.       Output[3] = Key[(Addr & 8) | (Addr>>12 & 7)];
  36.       Output[4] = Key[(Addr>>8 & 8) | (Addr & 7)];
  37.       Output[5] = Key[(Data & 8) | (Addr>>8 & 7)];
  38.       Output[6] = 0;
  39.       return Output;
  40.     } else {
  41.       Compare = strtol(Input+8, NULL, 16);
  42.       Output[0] = Key[(Data>>4 & 8) | (Data & 7)];
  43.       Output[1] = Key[(Addr>>4 & 8) | (Data>>4 & 7)];
  44.       Output[2] = Key[8 | (Addr>>4 & 7)];
  45.       Output[3] = Key[(Addr & 8) | (Addr>>12 & 7)];
  46.       Output[4] = Key[(Addr>>8 & 8) | (Addr & 7)];
  47.       Output[5] = Key[(Compare & 8) | (Addr>>8 & 7)];
  48.       Output[6] = Key[(Compare>>4 & 8) | (Compare & 7)];
  49.       Output[7] = Key[(Data & 8) | (Compare>>4 & 7)];
  50.       Output[8] = 0;
  51.       return Output;
  52.     }
  53.   } else {
  54.     return "Invalid syntax?";
  55.   }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement