Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
goelp14
GitHub Repository: goelp14/easyctf-iv-problems
Path: blob/master/fanfic/fanfic.c
671 views
1
//gcc -m32 -std=c99 -Wall -fno-stack-protector heaps_of_knowledge.c -o heaps_of_knowledge
2
#define _GNU_SOURCE
3
#include <stdlib.h>
4
#include <unistd.h>
5
#include <stdio.h>
6
#include <string.h>
7
#include <sys/types.h>
8
9
struct chapter {
10
struct chapter *prev_chapter;
11
struct chapter *next_chapter;
12
char title[50];
13
char content[256];
14
void (* print_ch)(int, struct chapter *);
15
};
16
17
typedef struct chapter chapter;
18
19
typedef struct fanfic {
20
char *title;
21
int num_chapters;
22
chapter *first_chapter;
23
} fanfic;
24
25
fanfic *create_fanfic(char *title) {
26
fanfic *b = malloc(sizeof(fanfic));
27
b->title = strdup(title);
28
b->first_chapter = NULL;
29
b->num_chapters = 0;
30
return b;
31
}
32
33
void print_chapter(int num, chapter *ch) {
34
printf("\n---------------\n");
35
printf("Chapter %i: %s", num, ch->title);
36
printf("\n---------------\n");
37
printf("%s\n", ch->content);
38
}
39
40
41
42
int success = 0xFFFF;
43
void validate(int ans) {
44
if ((ans ^ 0xDEADBEEF) == 0xDEADBEAF) {
45
success = 0xC001B4B3;
46
}
47
}
48
49
void be_nice() {
50
gid_t gid = getegid();
51
setresgid(gid, gid, gid);
52
}
53
54
void give_flag() {
55
be_nice();
56
FILE *f = fopen("flag.txt", "r");
57
if (success==0xC001B4B3) {
58
if (f != NULL) {
59
char c;
60
while ((c = fgetc(f)) != EOF) {
61
putchar(c);
62
}
63
}
64
else {
65
printf("Couldn't open flag file!\n");
66
}
67
}
68
else {
69
printf("nope! \n");
70
}
71
fclose(f);
72
}
73
74
75
76
int i;
77
int num_ch;
78
char option[4];
79
int option_val;
80
int main(int argc, char *argv[]) {
81
printf("Please enter the title of your brand new fanfic: ");
82
char title[50];
83
char text[256];
84
fgets(title, 50, stdin);
85
86
//get rid of newline
87
title[strlen(title)-1] = '\0';
88
89
printf("You have started writing the fanfic '%s'. Please select an option to get started!\n", title);
90
91
fanfic *fanfic = create_fanfic(title);
92
while(1) {
93
printf("1. Edit chapter\n");
94
printf("2. Delete chapter\n");
95
printf("3. Publish fanfic\n");
96
printf("> ");
97
i=0;
98
num_ch = fanfic->num_chapters;
99
chapter *curr_ch;
100
101
fgets(option, 4, stdin);
102
option_val = atoi(option);
103
104
switch(option_val) {
105
case 1: //edit
106
printf("Enter chapter number to edit: ");
107
fgets(option, 4, stdin);
108
109
option_val = atoi(option);
110
111
if (option_val > 0 && option_val <= num_ch) {
112
printf("Editing chapter\n");
113
printf("Enter new chapter text: \n");
114
115
curr_ch = fanfic->first_chapter;
116
for (i=1; i<option_val; i++) {
117
curr_ch = curr_ch->next_chapter;
118
}
119
gets(curr_ch->content);
120
}
121
else if (option_val == num_ch+1) {
122
printf("Adding new chapter\n");
123
printf("Enter chapter title: ");
124
fgets(title, 50, stdin);
125
title[strlen(title)-1] = '\0';
126
printf("Enter chapter contents: ");
127
fgets(text, 256, stdin);
128
text[strlen(text)-1] = '\0';
129
130
chapter *ch = malloc(sizeof(chapter));
131
strcpy(ch->title, title);
132
strcpy(ch->content, text);
133
//ch->title = strdup(title);
134
//ch->content = strdup(text);
135
ch->print_ch = &print_chapter;
136
137
curr_ch = fanfic->first_chapter;
138
fanfic->num_chapters++;
139
if (curr_ch == NULL) {
140
fanfic->first_chapter = ch;
141
}
142
else {
143
while (curr_ch->next_chapter != NULL) {
144
curr_ch = curr_ch->next_chapter;
145
}
146
147
curr_ch->next_chapter = ch;
148
ch->prev_chapter = curr_ch;
149
}
150
}
151
else {
152
printf("Invalid chapter number!\n");
153
return 0;
154
}
155
break;
156
case 2: //delete
157
printf("Enter chapter number to delete: ");
158
fgets(option, 4, stdin);
159
160
option_val = atoi(option);
161
162
if (option_val > 0 && option_val <= num_ch) {
163
printf("deleting chapter\n");
164
fanfic->num_chapters--;
165
if (option_val == 1) {
166
167
chapter *old_first = fanfic->first_chapter;
168
fanfic->first_chapter = old_first->next_chapter;
169
free(old_first->title);
170
free(old_first->content);
171
free(old_first);
172
if (fanfic->first_chapter != NULL)
173
fanfic->first_chapter->prev_chapter = NULL;
174
}
175
else {
176
curr_ch = fanfic->first_chapter;
177
for(i=2;i<option_val;i++) {
178
curr_ch = curr_ch->next_chapter;
179
}
180
181
chapter *to_del = curr_ch->next_chapter;
182
183
curr_ch->next_chapter = to_del->next_chapter;
184
if (to_del->next_chapter != NULL)
185
to_del->next_chapter->prev_chapter = to_del->prev_chapter;
186
187
free(to_del->title);
188
free(to_del->content);
189
free(to_del);
190
}
191
}
192
else {
193
printf("Invalid chapter number!\n");
194
return 0;
195
}
196
break;
197
default: //publish
198
printf("Fanfic published! Here it is:\n");
199
printf("===============\n");
200
printf("%s\n", fanfic->title);
201
printf("===============\n");
202
i = 1;
203
curr_ch = fanfic->first_chapter;
204
while (curr_ch != NULL) {
205
curr_ch->print_ch(i, curr_ch);
206
curr_ch = curr_ch->next_chapter;
207
i++;
208
}
209
return 0;
210
}
211
}
212
return 0;
213
}
214
215