1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
| #include <conio.h> #include <vector> using namespace std; const bool printAddresses = true; class Mutator { protected: int param; public: Mutator(int param) : param(param) {} virtual int getParam() const { return param; } virtual void mutate(void *data, int size) const = 0; }; class Multiplier : public Mutator { int reserved[40]; public: Multiplier(int multiplier = 0) : Mutator(multiplier) {} virtual void mutate(void *data, int size) const { int *ptr = (int *)data; for (int i = 0; i < size / 4; ++i) ptr[i] *= getParam(); } }; class LowerCaser : public Mutator { public: LowerCaser() : Mutator(0) {} virtual void mutate(void *data, int size) const { char *ptr = (char *)data; for (int i = 0; i < size; ++i) if (ptr[i] >= 'a' && ptr[i] <= 'z') ptr[i] -= 0x20; } }; class Block { void *data; int size; public: Block(void *data, int size) : data(data), size(size) {} void *getData() const { return data; } int getSize() const { return size; } };
void flush() { char ch; while ((ch = getchar()) != '\n' && ch != EOF); }
vector<Block> blocks; Mutator *mutators[] = { new Multiplier(2), new LowerCaser() };
void configureMutator() { while (true) { printf( "1) Multiplier (multiplier = %d)\n" "2) LowerCaser\n" "3) Exit\n" "\n" "Your choice [1-3]: ", mutators[0]->getParam()); int choice = _getch(); printf("\n\n"); if (choice == '3') break; if (choice >= '1' && choice <= '3') { if (choice == '1') { if (printAddresses) printf("mutators[0] = 0x%08x\n", mutators[0]); delete mutators[0]; printf("multiplier (int): "); int multiplier; int res = scanf_s("%d", &multiplier); flush(); if (res) { mutators[0] = new Multiplier(multiplier); if (printAddresses) printf("mutators[0] = 0x%08x\n", mutators[0]); printf("Multiplier was configured\n\n"); } break; } else { printf("LowerCaser is not configurable for now!\n\n"); } } else printf("Wrong choice!\n"); } } void listBlocks() { printf("------- Blocks -------\n"); if (!printAddresses) for (size_t i = 0; i < blocks.size(); ++i) printf("block %d: size = %d\n", i, blocks[i].getSize()); else for (size_t i = 0; i < blocks.size(); ++i) printf("block %d: address = 0x%08x; size = %d\n", i, blocks[i].getData(), blocks[i].getSize()); printf("----------------------\n\n"); } void readBlock() { char *data; char filePath[1024]; while (true) { printf("File path ('exit' to exit): "); scanf_s("%s", filePath, sizeof(filePath)); fflush(stdin); printf("\n"); if (!strcmp(filePath, "exit")) return; FILE *f = fopen(filePath, "rb"); if (!f) printf("Can't open the file!\n\n"); else { fseek(f, 0L, SEEK_END); long bytes = ftell(f); data = new char[bytes]; fseek(f, 0L, SEEK_SET); int pos = 0; while (pos < bytes) { int len = bytes - pos > 200 ? 200 : bytes - pos; fread(data + pos, 1, len, f); pos += len; } fclose(f); blocks.push_back(Block(data, bytes)); printf("Block read (%d bytes)\n\n", bytes); break; } } } void duplicateBlock() { listBlocks(); while (true) { printf("Index of block to duplicate (-1 to exit): "); int index; scanf_s("%d", &index); fflush(stdin); if (index == -1) return; if (index < 0 || index >= (int)blocks.size()) { printf("Wrong index!\n"); } else { while (true) { int copies; printf("Number of copies (-1 to exit): "); scanf_s("%d", &copies); fflush(stdin); if (copies == -1) return; if (copies <= 0) printf("Wrong number of copies!\n"); else { for (int i = 0; i < copies; ++i) { int size = blocks[index].getSize(); void *data = new char[size]; memcpy(data, blocks[index].getData(), size); blocks.push_back(Block(data, size)); } return; } } } } } void myExit() { exit(0); } void mutateBlock() { listBlocks(); while (true) { printf("Index of block to mutate (-1 to exit): "); int index; scanf_s("%d", &index); fflush(stdin); if (index == -1) break; if (index < 0 || index >= (int)blocks.size()) { printf("Wrong index!\n"); } else { while (true) { printf( "1) Multiplier\n" "2) LowerCaser\n" "3) Exit\n" "Your choice [1-3]: "); int choice = _getch(); printf("\n\n"); if (choice == '3') break; if (choice >= '1' && choice <= '3') { choice -= '0'; mutators[choice - 1]->mutate(blocks[index].getData(), blocks[index].getSize()); printf("The block was mutated.\n\n"); break; } else printf("Wrong choice!\n\n"); } break; } } } int handleMenu() { while (true) { printf( "1) Read block from file\n" "2) List blocks\n" "3) Duplicate Block\n" "4) Configure mutator\n" "5) Mutate block\n" "6) Exit\n" "\n" "Your choice [1-6]: "); int choice = _getch();
printf("\n\n"); if (choice >= '1' && choice <= '6') return choice - '0'; else printf("Wrong choice!\n\n"); } } int main() { typedef void(*funcPtr)(); funcPtr functions[] = { readBlock, listBlocks, duplicateBlock, configureMutator, mutateBlock, myExit }; while (true) { int choice = handleMenu(); functions[choice - 1](); } return 0; }
|