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
| #define BUF_SIZE 0x100
enum book_status{ CREATED = 0X12345, ABANDONED = 0X67890, } ; typedef struct { char content[BUF_SIZE]; int status; } notebook;
notebook * notebooks[10] = {NULL};
void init() { setvbuf(stdin, 0, 2, 0); setvbuf(stdout, 0, 2, 0); setvbuf(stderr, 0, 2, 0); }
int read_int() { char buf[0x10]; read(0, buf, 0x10); return atoi(buf); }
void read_flag() { char flag[0x100]; int fd, size = 0;
fd = open("/flag", 0); if (fd < 0) { puts("Open flag failed"); exit(-1); } size = read(fd, flag, 0x100); write(1, flag, size); close(fd); exit(0); }
void create_notebook() { notebook *book = NULL; int idx;
puts("Input your notebook index:"); idx = read_int(); if (idx < 0 || idx >= 0x20) { puts("Invalid index for notebooks, Hacker!"); return; }
book = malloc(sizeof(notebook)); if (book == NULL) { puts("malloc error"); exit(-1); }
puts("Input your notebook content:"); read(0, book->content, BUF_SIZE); book->status = CREATED;
notebooks[idx] = book; puts("Done!"); }
void edit_notebook() { int idx;
puts("Input your notebook index:"); idx = read_int(); if (idx < 0 || idx >= 0x10) { puts("Invalid index, Hacker!"); return; }
if (notebooks[idx] == NULL) { puts("You don't have this notebook, create it first"); return; }
notebooks[idx]->status = ABANDONED; puts("Done!"); }
void delete_notebook() { int idx;
puts("Input your notebook index:"); idx = read_int(); if (idx < 0 || idx >= 0x10) { puts("Invalid index, Hacker!"); return; }
if (notebooks[idx] == NULL) { puts("You don't have this notebook, create it first"); return; }
free(notebooks[idx]); puts("Done!"); }
void show_notebook() { int idx;
puts("Input your notebook index:"); idx = read_int(); if (idx < 0 || idx >= 0x10) { puts("Invalid index, Hacker!"); return; }
if (notebooks[idx] == NULL) { puts("You don't have this notebook, create it first"); return; }
printf("content = %s, status = 0x%x\n", notebooks[idx]->content, notebooks[idx]->status); }
int bypass_me() { int flag = 0;
if ((notebooks[0] == NULL) || (strcmp(notebooks[0]->content, "hello ") != 0) || (notebooks[0]->status != CREATED)) return flag;
if ((notebooks[1] == NULL) || (strcmp(notebooks[1]->content, "world,") != 0) || (notebooks[1]->status != ABANDONED)) return flag;
if ((notebooks[3] == NULL) || (strcmp(notebooks[3]->content, "magic ") != 0) || (notebooks[3]->status != CREATED)) return flag;
if ((notebooks[5] == NULL) || (strcmp(notebooks[5]->content, "notebook") != 0) || (notebooks[5]->status != ABANDONED)) return flag;
flag = 1;
return flag; }
void menu() { puts("1. Create Notebook"); puts("2. Edit Notebook"); puts("3. Delete Notebook"); puts("4. Show Notebook"); puts("5. Gift for You"); puts("Choice >> "); }
int main() { int choice, flag = 1;
init(); puts("We have a magic notebook for you:");
while (flag) { menu(); scanf("%d", &choice); switch (choice) { case 1: create_notebook(); break; case 2: edit_notebook(); break; case 3: delete_notebook(); break; case 4: show_notebook(); break; case 5: if (bypass_me()) read_flag(); flag = 0; break; default: puts("Invalid choice"); break; } }
puts("Bye bye~");
return 0; }
|