Advertisement
aaaaaa123456789

Original autogen.c for TPP AC distribution

Sep 24th, 2018
191
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 19.90 KB
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7.  
  8. const char * pokemon[256] = {
  9.   "-", "Bulbasaur", "Ivysaur", "Venusaur", "Charmander", "Charmeleon", "Charizard", "Squirtle", "Wartortle", "Blastoise", "Caterpie", "Metapod", "Butterfree",
  10.   "Weedle", "Kakuna", "Beedrill", "Pidgey", "Pidgeotto", "Pidgeot", "Rattata", "Raticate", "Spearow", "Fearow", "Ekans", "Arbok", "Pikachu", "Raichu", "Sandshrew",
  11.   "Sandslash", "Nidoran (F)", "Nidorina", "Nidoqueen", "Nidoran (M)", "Nidorino", "Nidoking", "Clefairy", "Clefable", "Vulpix", "Ninetales", "Jigglypuff",
  12.   "Wigglytuff", "Zubat", "Golbat", "Oddish", "Gloom", "Vileplume", "Paras", "Parasect", "Venonat", "Venomoth", "Diglett", "Dugtrio", "Meowth", "Persian", "Psyduck",
  13.   "Golduck", "Mankey", "Primeape", "Growlithe", "Arcanine", "Poliwag", "Poliwhirl", "Poliwrath", "Abra", "Kadabra", "Alakazam", "Machop", "Machoke", "Machamp",
  14.   "Bellsprout", "Weepinbell", "Victreebel", "Tentacool", "Tentacruel", "Geodude", "Graveler", "Golem", "Ponyta", "Rapidash", "Slowpoke", "Slowbro", "Magnemite",
  15.   "Magneton", "Farfetch'd", "Doduo", "Dodrio", "Seel", "Dewgong", "Grimer", "Muk", "Shellder", "Cloyster", "Gastly", "Haunter", "Gengar", "Onix", "Drowzee",
  16.   "Hypno", "Krabby", "Kingler", "Voltorb", "Electrode", "Exeggcute", "Exeggutor", "Cubone", "Marowak", "Hitmonlee", "Hitmonchan", "Lickitung", "Koffing", "Weezing",
  17.   "Rhyhorn", "Rhydon", "Chansey", "Tangela", "Kangaskhan", "Horsea", "Seadra", "Goldeen", "Seaking", "Staryu", "Starmie", "Mr. Mime", "Scyther", "Jynx",
  18.   "Electabuzz", "Magmar", "Pinsir", "Tauros", "Magikarp", "Gyarados", "Lapras", "Ditto", "Eevee", "Vaporeon", "Jolteon", "Flareon", "Porygon", "Omanyte", "Omastar",
  19.   "Kabuto", "Kabutops", "Aerodactyl", "Snorlax", "Articuno", "Zapdos", "Moltres", "Dratini", "Dragonair", "Dragonite", "Mewtwo", "Mew", "Chikorita", "Bayleef",
  20.   "Meganium", "Cyndaquil", "Quilava", "Typhlosion", "Totodile", "Croconaw", "Feraligatr", "Sentret", "Furret", "Hoothoot", "Noctowl", "Ledyba", "Ledian",
  21.   "Spinarak", "Ariados", "Crobat", "Chinchou", "Lanturn", "Pichu", "Cleffa", "Igglybuff", "Togepi", "Togetic", "Natu", "Xatu", "Mareep", "Flaaffy", "Ampharos",
  22.   "Bellossom", "Marill", "Azumarill", "Sudowoodo", "Politoed", "Hoppip", "Skiploom", "Jumpluff", "Aipom", "Sunkern", "Sunflora", "Yanma", "Wooper", "Quagsire",
  23.   "Espeon", "Umbreon", "Murkrow", "Slowking", "Misdreavus", "Unown", "Wobbuffet", "Girafarig", "Pineco", "Forretress", "Dunsparce", "Gligar", "Steelix",
  24.   "Snubbull", "Granbull", "Qwilfish", "Scizor", "Shuckle", "Heracross", "Sneasel", "Teddiursa", "Ursaring", "Slugma", "Magcargo", "Swinub", "Piloswine", "Corsola",
  25.   "Remoraid", "Octillery", "Delibird", "Mantine", "Skarmory", "Houndour", "Houndoom", "Kingdra", "Phanpy", "Donphan", "Porygon2", "Stantler", "Smeargle",
  26.   "Tyrogue", "Hitmontop", "Smoochum", "Elekid", "Magby", "Miltank", "Blissey", "Raikou", "Entei", "Suicune", "Larvitar", "Pupitar", "Tyranitar", "Lugia", "Ho-oh",
  27.   "Celebi", "Phancero", "Egg", NULL, NULL
  28. };
  29.  
  30. const char * moves[256] = {
  31.   "-", "Pound", "Karate Chop", "DoubleSlap", "Gunk Shot", "Zen Headbutt", "Pay Day", "Fire Punch", "Ice Punch", "ThunderPunch", "Scratch", "Fairy Wind",
  32.   "Focus Blast", "Iron Defense", "Swords Dance", "Cut", "Gust", "Wing Attack", "Whirlwind", "Fly", "Bug Buzz", "Slam", "Vine Whip", "Stomp", "Double Kick",
  33.   "Mega Kick", "Flare Blitz", "Rolling Kick", "Sand Attack", "Headbutt", "Horn Attack", "Fury Attack", "Horn Drill", "Tackle", "Body Slam", "Wrap", "Take Down",
  34.   "Thrash", "Double Edge", "Tail Whip", "Poison Sting", "Twineedle", "Pin Missile", "Leer", "Bite", "Growl", "Roar", "Sing", "Supersonic", "SonicBoom", "Disable",
  35.   "Acid", "Ember", "Flamethrower", "Mist", "Water Gun", "Hydro Pump", "Surf", "Ice Beam", "Blizzard", "Psybeam", "BubbleBeam", "Aurora Beam", "Hyper Beam", "Peck",
  36.   "Drill Peck", "Submission", "Heat Wave", "Counter", "Seismic Toss", "Strength", "Absorb", "Mega Drain", "Leech Seed", "Growth", "Razor Leaf", "SolarBeam",
  37.   "PoisonPowder", "Stun Spore", "Sleep Powder", "Petal Dance", "String Shot", "Dragon Rage", "Fire Spin", "ThunderShock", "Thunderbolt", "Thunder Wave", "Thunder",
  38.   "Rock Throw", "Earthquake", "Fissure", "Dig", "Toxic", "Confusion", "Psychic", "Hypnosis", "Meditate", "Agility", "Quick Attack", "Rage", "Teleport",
  39.   "Night Shade", "Mimic", "Screech", "Double Team", "Recover", "Harden", "Minimize", "Smokescreen", "Confuse Ray", "Withdraw", "Defense Curl", "Barrier",
  40.   "Light Screen", "Haze", "Reflect", "Focus Energy", "Bide", "Metronome", "Mirror Move", "Selfdestruct", "Metal Sound", "Lick", "Dragon Pulse", "Sludge",
  41.   "Bone Club", "Fire Blast", "Waterfall", "Clamp", "Swift", "Aqua Jet", "Spike Cannon", "Flash Cannon", "Amnesia", "Will-o-Wisp", "Softboiled", "Hi Jump Kick",
  42.   "Glare", "Dream Eater", "Rock Polish", "Seed Bomb", "Leech Life", "Lovely Kiss", "Sky Attack", "Transform", "Bubble", "Dizzy Punch", "Spore", "Flash", "Psywave",
  43.   "Splash", "Acid Armor", "Crabhammer", "Explosion", "Poison Jab", "Bonemerang", "Rest", "Rock Slide", "Hyper Fang", "Sharpen", "Conversion", "Tri Attack",
  44.   "Super Fang", "Slash", "Substitute", "Struggle", "Sketch", "Wild Charge", "Thief", "X Scissor", "Mind Reader", "Nightmare", "Flame Wheel", "Iron Head", "Curse",
  45.   "Flail", "Conversion2", "Aeroblast", "Cotton Spore", "Reversal", "Spite", "Powder Snow", "Protect", "Mach Punch", "Scary Face", "Faint Attack", "Sweet Kiss",
  46.   "Belly Drum", "Sludge Bomb", "Mud Slap", "Octazooka", "Spikes", "Zap Cannon", "Foresight", "Destiny Bond", "Perish Song", "Icy Wind", "Air Slash", "Dark Pulse",
  47.   "Earth Power", "Outrage", "Sandstorm", "Giga Drain", "Endure", "Charm", "Rollout", "False Swipe", "Swagger", "Milk Drink", "Spark", "Fury Cutter", "Steel Wing",
  48.   "Mean Look", "Attract", "Sleep Talk", "Heal Bell", "Return", "Present", "DazzlinGleam", "Safeguard", "Pain Split", "Sacred Fire", "Magnitude", "DynamicPunch",
  49.   "Megahorn", "DragonBreath", "Baton Pass", "Encore", "Pursuit", "Rapid Spin", "Nasty Plot", "Iron Tail", "Metal Claw", "Vital Throw", "Morning Sun", "Synthesis",
  50.   "Moonlight", "Hidden Power", "Cross Chop", "Twister", "Rain Dance", "Sunny Day", "Crunch", "Mirror Coat", "Shadow Claw", "ExtremeSpeed", "AncientPower",
  51.   "Shadow Ball", "Future Sight", "Rock Smash", "Whirlpool", "Drill Run", "Moonblast", "Play Rough", "Sheer Cold", NULL
  52. };
  53.  
  54. const char * items[256] = {
  55.   "-", "Master Ball", "Ultra Ball", "BrightPowder", "Great Ball", "Poke Ball", "Premier Ball", "Bicycle", "Moon Stone", "Antidote", "Burn Heal", "Ice Heal",
  56.   "Awakening", "Parlyz Heal", "Full Restore", "Max Potion", "Hyper Potion", "Super Potion", "Potion", "Escape Rope", "Repel", "Max Elixer", "Fire Stone",
  57.   "ThunderStone", "Water Stone", "Poison Guard", "HP Up", "Protein", "Iron", "Carbos", "Lucky Punch", "Calcium", "Rare Candy", "X Accuracy", "Leaf Stone",
  58.   "Metal Powder", "Nugget", "Poke Doll", "Full Heal", "Revive", "Max Revive", "Guard Spec.", "Super Repel", "Max Repel", "Dire Hit", "Burn Guard", "Fresh Water",
  59.   "Soda Pop", "Lemonade", "X Attack", "Freeze Guard", "X Defend", "X Speed", "X Special", "Coin Case", "Itemfinder", "Poke Flute", "Exp.Share", "Old Rod",
  60.   "Good Rod", "Silver Leaf", "Super Rod", "PP Up", "Ether", "Max Ether", "Elixer", "Red Scale", "SecretPotion", "S.S.Ticket", "Mystery Egg", "Clear Bell",
  61.   "Silver Wing", "MooMoo Milk", "Quick Claw", "Pecha Berry", "Gold Leaf", "Soft Sand", "Sharp Beak", "Cheri Berry", "Aspear Berry", "Rawst Berry", "Poison Barb",
  62.   "King's Rock", "Persim Berry", "Chesto Berry", "Red Apricorn", "TinyMushroom", "Big Mushroom", "SilverPowder", "Blu Apricorn", "Sleep Guard", "Amulet Coin",
  63.   "Ylw Apricorn", "Grn Apricorn", "Cleanse Tag", "Mystic Water", "TwistedSpoon", "Wht Apricorn", "Blackbelt", "Blk Apricorn", "Parlyz Guard", "Pnk Apricorn",
  64.   "BlackGlasses", "SlowpokeTail", "Pink Bow", "Stick", "Smoke Ball", "NeverMeltIce", "Magnet", "Lum Berry", "Pearl", "Big Pearl", "Everstone", "Spell Tag",
  65.   "RageCandyBar", "GS Ball", "Blue Card", "Miracle Seed", "Thick Club", "Focus Band", "ConfuseGuard", "EnergyPowder", "Energy Root", "Heal Powder", "Revival Herb",
  66.   "Hard Stone", "Lucky Egg", "Card Key", "Machine Part", "Egg Ticket", "Lost Item", "Stardust", "Star Piece", "Basement Key", "Pass", "Helix Fossil", "Dome Fossil",
  67.   "Old Amber", "Charcoal", "Berry Juice", "Scope Lens", "Oak's Parcel", "Power Herb", "Metal Coat", "Dragon Fang", "Friend Charm", "Leftovers", "ResearchNote",
  68.   NULL, NULL, "Leppa Berry", "Dragon Scale", "Berserk Gene", NULL, "X Sp. Def", NULL, "Sacred Ash", "Heavy Ball", "Flower Mail", "Level Ball", "Lure Ball",
  69.   "Fast Ball", NULL, "Light Ball", "Friend Ball", "Moon Ball", "Love Ball", "Normal Box", "Gorgeous Box", "Sun Stone", "Polkadot Bow", NULL, "Up-Grade",
  70.   "Oran Berry", "Sitrus Berry", "SquirtBottle", NULL, "Park Ball", "Rainbow Wing", NULL, "Brick Piece", "Surf Mail", "LiteBlueMail", "PortraitMail", "Lovely Mail",
  71.   "Eon Mail", "Morph Mail", "BlueSky Mail", "Music Mail", "Mirage Mail", NULL, "TM01", "TM02", "TM03", "TM04", NULL, "TM05", "TM06", "TM07", "TM08", "TM09", "TM10",
  72.   "TM11", "TM12", "TM13", "TM14", "TM15", "TM16", "TM17", "TM18", "TM19", "TM20", "TM21", "TM22", "TM23", "TM24", "TM25", "TM26", "TM27", "TM28", NULL, "TM29",
  73.   "TM30", "TM31", "TM32", "TM33", "TM34", "TM35", "TM36", "TM37", "TM38", "TM39", "TM40", "TM41", "TM42", "TM43", "TM44", "TM45", "TM46", "TM47", "TM48", "TM49",
  74.   "TM50", "HM01", "HM02", "HM03", "HM04", "HM05", "HM06", "HM07", NULL, NULL, NULL, NULL, NULL, NULL
  75. };
  76.  
  77. #define ALPHA_OFFSET 63
  78. #define DIGIT_OFFSET 198
  79.  
  80. const char printable_valid[] = " ():;[]'-?!.&_/,";
  81. const unsigned char printable_values[] = {0x7f, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f, 0xe0, 0xe3, 0xe6, 0xe7, 0xe8, 0xe9, 0xf2, 0xf3, 0xf4};
  82. const char * special_valid[] = {"A", "O", "U", "a", "o", "u", "'d", "'l", "'m", "'r", "'s", "'t", "'v", "PK", "MN", "e", "m", "$", "x", "f", NULL};
  83. const unsigned char special_values[] = {0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xd0, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xe1, 0xe2, 0xea, 0xef, 0xf0, 0xf1, 0xf5};
  84.  
  85. struct line_data {
  86.   unsigned short OTID;
  87.   unsigned short DVs;
  88.   unsigned char species;
  89.   unsigned char item;
  90.   unsigned char flags;
  91.   unsigned char level;
  92.   unsigned char code[8];
  93.   unsigned char moves[4];
  94.   unsigned char OT[11];
  95.   unsigned char nickname[11];
  96. };
  97.  
  98. char * read_line(FILE *);
  99. long long convert_string_to_number(const char *);
  100. char * get_next_parameter(char **, unsigned);
  101. long long get_next_number(char **, unsigned);
  102. unsigned char encode_character(char);
  103. unsigned char encode_special(const char *);
  104. void get_next_encoded_string(char **, unsigned, unsigned char *, unsigned, int);
  105. unsigned char get_next_by_lookup(char **, unsigned, const char **);
  106. void get_field_lengths(FILE *, unsigned, unsigned *, unsigned *);
  107. struct line_data * get_data_from_file(FILE *, unsigned *);
  108. struct line_data parse_line_into_data(char *, const unsigned *, const unsigned *);
  109. int main(int, char **);
  110. void generate_data_for_encryption(struct line_data, char *, char *);
  111. void encrypt(const unsigned char *, const unsigned char *, unsigned char *);
  112.  
  113. char * read_line (FILE * file) {
  114.   int character;
  115.   char * result = NULL;
  116.   unsigned length = 0;
  117.   while (1) {
  118.     character = getc(file);
  119.     if ((character == EOF) || (character == '\n')) break;
  120.     result = realloc(result, length + 1);
  121.     result[length ++] = character;
  122.   }
  123.   result = realloc(result, length + 1);
  124.   result[length] = 0;
  125.   if (length && (result[length - 1] == '\r')) result[length - 1] = 0;
  126.   return result;
  127. }
  128.  
  129. long long convert_string_to_number (const char * string) {
  130.   char * errp;
  131.   errno = 0;
  132.   int base;
  133.   if (*string == '$') {
  134.     base = 16;
  135.     string ++;
  136.   } else
  137.     base = 0;
  138.   long long result = strtoll(string, &errp, base);
  139.   if (*errp || (errno == ERANGE)) {
  140.     fprintf(stderr, "error: could not make number from %s\n", string);
  141.     exit(1);
  142.   }
  143.   return result;
  144. }
  145.  
  146. char * get_next_parameter (char ** string, unsigned length) {
  147.   if (strlen(*string) < length) length = strlen(*string);
  148.   char * result = malloc(length + 1);
  149.   memcpy(result, *string, length);
  150.   result[length] = 0;
  151.   *string += length;
  152.   while (length && isspace(result[length - 1])) result[-- length] = 0;
  153.   return result;
  154. }
  155.  
  156. long long get_next_number (char ** string, unsigned length) {
  157.   char * param = get_next_parameter(string, length);
  158.   long long number = convert_string_to_number(param);
  159.   free(param);
  160.   return number;
  161. }
  162.  
  163. unsigned char encode_character (char character) {
  164.   const char * pos = strchr(printable_valid, character);
  165.   if (isalpha(character)) return (unsigned char) character + ALPHA_OFFSET;
  166.   if (isdigit(character)) return (unsigned char) character + DIGIT_OFFSET;
  167.   if (!pos) {
  168.     fprintf(stderr, "error: could not encode character: %c\n", character);
  169.     exit(1);
  170.   }
  171.   return printable_values[pos - printable_valid];
  172. }
  173.  
  174. unsigned char encode_special (const char * special) {
  175.   const char ** current;
  176.   for (current = special_valid; *current; current ++) if (!strcmp(*current, special)) break;
  177.   if (!*current) {
  178.     fprintf(stderr, "error: could not encode special: <%s>\n", special);
  179.     exit(1);
  180.   }
  181.   return special_values[current - special_valid];
  182. }
  183.  
  184. void get_next_encoded_string (char ** string, unsigned length, unsigned char * buffer, unsigned buffer_length, int code_mode) {
  185.   char * name = get_next_parameter(string, length);
  186.   char * p;
  187.   char * closing;
  188.   unsigned buffer_pos = 0;
  189.   for (p = name; *p; p ++) {
  190.     if (buffer_pos >= buffer_length) {
  191.       fprintf(stderr, "error: name too long\n");
  192.       exit(1);
  193.     }
  194.     if (*p == '<') {
  195.       p ++;
  196.       closing = strchr(p, '>');
  197.       if (!closing) {
  198.         fprintf(stderr, "error: mismatched <\n");
  199.         exit(1);
  200.       }
  201.       *closing = 0;
  202.       buffer[buffer_pos ++] = encode_special(p);
  203.       p = closing;
  204.     } else
  205.       buffer[buffer_pos ++] = encode_character(*p);
  206.   }
  207.   free(name);
  208.   if (code_mode) {
  209.     unsigned pos;
  210.     for (pos = 0; pos < buffer_pos; pos ++) if (buffer[pos] == 0x7f) buffer[pos] = 0xbf;
  211.     if (buffer_pos < buffer_length) memset(buffer + buffer_pos, 0xcf, buffer_length - buffer_pos);
  212.   } else if (buffer_pos < buffer_length) {
  213.     buffer[buffer_pos ++] = 0x50;
  214.     while (buffer_pos < buffer_length) buffer[buffer_pos ++] = rand();
  215.   }
  216. }
  217.  
  218. unsigned char get_next_by_lookup (char ** string, unsigned length, const char ** lookup) {
  219.   char * param = get_next_parameter(string, length);
  220.   unsigned p;
  221.   for (p = 0; p < 256; p ++) {
  222.     if (!lookup[p]) continue;
  223.     if (!strcmp(param, lookup[p])) {
  224.       free(param);
  225.       return p;
  226.     }
  227.   }
  228.   fprintf(stderr, "error: invalid value \"%s\"\n", param);
  229.   exit(1);
  230. }
  231.  
  232. void get_field_lengths (FILE * file, unsigned field_count, unsigned * lengths, unsigned * spacings) {
  233.   char * line;
  234.   unsigned field;
  235.   char * pos;
  236.   while (!feof(file)) {
  237.     line = read_line(file);
  238.     if ((strlen(line) != strspn(line, "- \t")) || !strchr(line, '-')) {
  239.       free(line);
  240.       continue;
  241.     }
  242.     pos = line + (*spacings = strcspn(line, "-"));
  243.     for (field = 0; field < field_count; field ++) {
  244.       if (!*pos) {
  245.         fprintf(stderr, "error: input defines %u columns, expected %u\n", field, field_count);
  246.         exit(1);
  247.       }
  248.       pos += (lengths[field] = strspn(pos, "-"));
  249.       pos += (spacings[field + 1] = strcspn(pos, "-"));
  250.     }
  251.     free(line);
  252.     return;
  253.   }
  254.   fprintf(stderr, "error: input does not contain column definitions\n");
  255.   exit(1);
  256. }
  257.  
  258. struct line_data * get_data_from_file (FILE * file, unsigned * count) {
  259.   unsigned field_lengths[16];
  260.   unsigned field_spacings[16];
  261.   get_field_lengths(file, 16, field_lengths, field_spacings);
  262.   *count = 0;
  263.   char * line;
  264.   struct line_data * data = NULL;
  265.   while (!feof(file)) {
  266.     line = read_line(file);
  267.     if (!*line) {
  268.       free(line);
  269.       continue;
  270.     }
  271.     data = realloc(data, sizeof(struct line_data) * (1 + *count));
  272.     data[(*count) ++] = parse_line_into_data(line, field_lengths, field_spacings);
  273.     free(line);
  274.   }
  275.   return data;
  276. }
  277.  
  278. struct line_data parse_line_into_data (char * line, const unsigned * field_lengths, const unsigned * field_spacings) {
  279.   struct line_data result;
  280.   line += *field_spacings;
  281.   get_next_encoded_string(&line, *field_lengths, result.code, 8, 1);
  282.   line += field_spacings[1];
  283.   get_next_encoded_string(&line, field_lengths[1], result.nickname, 11, 0);
  284.   line += field_spacings[2];
  285.   result.species = get_next_by_lookup(&line, field_lengths[2], pokemon);
  286.   line += field_spacings[3];
  287.   result.level = get_next_number(&line, field_lengths[3]);
  288.   line += field_spacings[4];
  289.   result.item = get_next_by_lookup(&line, field_lengths[4], items);
  290.   line += field_spacings[5];
  291.   unsigned char id;
  292.   for (id = 0; id < 4; id ++) {
  293.     result.moves[id] = get_next_by_lookup(&line, field_lengths[5 + id], moves);
  294.     line += field_spacings[6 + id];
  295.   }
  296.   result.DVs = 0;
  297.   for (id = 0; id < 4; id ++) {
  298.     result.DVs |= get_next_number(&line, field_lengths[9 + id]) << ((id ^ 3) << 2);
  299.     line += field_spacings[10 + id];
  300.   }
  301.   result.OTID = get_next_number(&line, field_lengths[13]);
  302.   line += field_spacings[14];
  303.   get_next_encoded_string(&line, field_lengths[14], result.OT, 11, 0);
  304.   line += field_spacings[15];
  305.   result.flags = get_next_number(&line, field_lengths[15]);
  306.   return result;
  307. }
  308.  
  309. int main (int argc, char ** argv) {
  310.   if ((argc < 2) || (argc > 3)) {
  311.     fprintf(stderr, "usage:\n  %s <infile> <outfile>\n  %s <outfile> (takes input from stdin)\n", *argv, *argv);
  312.     return 2;
  313.   }
  314.   FILE * file;
  315.   struct line_data * source_data;
  316.   unsigned count;
  317.   argv ++;
  318.   if (argc == 3) {
  319.     file = fopen(*argv, "r");
  320.     if (!file) {
  321.       fprintf(stderr, "error: could not open file %s for reading\n", *argv);
  322.       return 1;
  323.     }
  324.     argv ++;
  325.   } else
  326.     file = stdin;
  327.   srand(1648035271);
  328.   source_data = get_data_from_file(file, &count);
  329.   fclose(file);
  330.   file = fopen(*argv, "wb");
  331.   if (!file) {
  332.     fprintf(stderr, "error: could not open file %s for writing\n", *argv);
  333.     return 1;
  334.   }
  335.   char encrypted[42];
  336.   char plaintext[42];
  337.   char key[7];
  338.   unsigned line;
  339.   for (line = 0; line < count; line ++) {
  340.     generate_data_for_encryption(source_data[line], plaintext, key);
  341.     encrypt(plaintext, key, encrypted);
  342.     fwrite(encrypted, 1, 42, file);
  343.   }
  344.   fclose(file);
  345.   free(source_data);
  346.   return 0;
  347. }
  348.  
  349. void generate_data_for_encryption (struct line_data source, char * data, char * key) {
  350.   *data = rand();
  351.   memset(data + 1, *data, 7);
  352.   data[8] = source.species;
  353.   data[9] = source.item;
  354.   memcpy(data + 10, source.moves, 4);
  355.   data[14] = source.OTID >> 8;
  356.   data[15] = source.OTID & 255;
  357.   data[16] = source.DVs >> 8;
  358.   data[17] = source.DVs & 255;
  359.   data[18] = source.level;
  360.   memcpy(data + 19, source.OT, 11);
  361.   memcpy(data + 30, source.nickname, 11);
  362.   data[41] = source.flags;
  363.   unsigned long long code = 0, packed = 0;
  364.   unsigned char p;
  365.   for (p = 0; p < 8; p ++) code = (code << 8) | source.code[p];
  366.   for (p = 0; p < 8; p ++) packed |= ((code >> (8 * p)) & 127) << (7 * p);
  367.   for (p = 0; p < 7; p ++) {
  368.     key[6 - p] = packed & 255;
  369.     packed >>= 8;
  370.   }
  371. }
  372.  
  373. // everything below this line is a rewrite in C of pigdevil2010's encryption function
  374.  
  375. void encrypt (const unsigned char * data, const unsigned char * key, unsigned char * encrypted) {
  376.   unsigned char fixed_pos = 2, key_pos = 0;
  377.   unsigned char i, j, k, tp;
  378.   unsigned char width, shift;
  379.   unsigned char temp_data[42];
  380.   const unsigned char fixed_string[] = {0x8e, 0x8b, 0x83, 0x84, 0x8d};
  381.   memcpy(encrypted, data, 42);
  382.   for (i = 41; i < 42; i --) {
  383.     fixed_pos = (fixed_pos + 4) % 5;
  384.     key_pos = (key_pos + 6) % 7;
  385.     encrypted[i] ^= encrypted[(i < 21) ? (41 - i) : (i - 21)] ^ key[key_pos] ^ fixed_string[fixed_pos];
  386.   }
  387.   for (i = 6; i < 7; i --) {
  388.     width = (key[i] & 15) + 2;
  389.     shift = (key[i] >> 4) + 1;
  390.     k = 0;
  391.     for (j = 0; j < width; j ++) for (tp = 0; (j + tp) < 42; tp += width) temp_data[j + tp] = encrypted[k ++];
  392.     memcpy(encrypted, temp_data + (42 - shift), shift);
  393.     memcpy(encrypted + shift, temp_data, 42 - shift);
  394.   }
  395.   memcpy(temp_data, key, 7);
  396.   memcpy(temp_data + 7, fixed_string, 5);
  397.   key_pos = 0;
  398.   for (i = 41; i < 42; i --) {
  399.     key_pos = (key_pos + 11) % 12;
  400.     encrypted[i] += temp_data[key_pos];
  401.   }
  402.   for (i = 41; i < 42; i --) {
  403.     key_pos = (key_pos + 11) % 12;
  404.     encrypted[i] -= temp_data[key_pos];
  405.   }
  406. }
Advertisement
RAW Paste Data Copied
Advertisement