-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.c
139 lines (128 loc) · 2.73 KB
/
main.c
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
/*
* main.c
* Copyright (C) 2018 hzshang <[email protected]>
*
* Distributed under terms of the MIT license.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <openssl/md5.h>
#define LENGTH 0x100
FILE* note;
char ID[LENGTH];
char *md5(unsigned char *data){
unsigned char md[16];
char buf[33]={0};
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, data, strlen(data));
MD5_Final(md, &ctx);
for(int i=0;i<16;i++){
sprintf(buf+i*2,"%02X",md[i]);
}
return strdup(buf);
}
void init() {
char *logo =
"\n"
" # # #### ##### ######\n"
" # # # # # #\n"
"### ### # # #####\n"
" # # # # #\n"
" # # # # # #\n"
" #### # #\n"
"\n";
puts(logo);
setvbuf(stdout, 0, 2, 0);
setvbuf(stdin, 0, 2, 0);
chdir("/tmp");
}
void menu(){
puts("=====MENU======");
puts("1. Edit note");
puts("2. Show note");
puts("3. Save note");
puts("4. Change ID");
puts("5. Exit");
}
char* read_file(){
char buf[LENGTH];
if(note){
fseek(note,0,SEEK_SET);
fscanf(note,"%256s",buf);
return strdup(buf);
}else{
return NULL;
}
}
void save_file(char *buf){
if(buf){
fseek(note,0,SEEK_SET);
fputs(buf,note);
}
}
char* read_str(){
char buffer[LENGTH];
scanf("%256s",buffer);
return strdup(buffer);
}
void login(){
printf("Input your ID:");
scanf("%256s",ID);
char *file=md5(ID);
note=fopen(file,"r+");
if(!note){
note=fopen(file,"w+");
}
}
void main() {
asm(
"shr $0x8,%rsp\n"
"shl $0x8,%rsp\n"
"xor $0x80,%rsp\n"
"push %rbp\n"
"mov %rsp,%rbp\n"
"sub $0x20,%rsp\n"
);
init();
char *fmt="%d";
int opt;
login();
char *buffer=read_file();
menu();
while(1){
printf("> ");
if(scanf(fmt,&opt)<=0){
exit(0);
};
switch(opt){
case 1:
printf("Note:");
buffer=read_str();
break;
case 2:
printf("Note:%s\n",buffer);
break;
case 3:
save_file(buffer);
puts("Saved!");
break;
case 4:
fclose(note);
unlink(md5(ID));
login();
puts("Done!");
break;
case 5:
if(note)
fclose(note);
exit(0);
break;
default:
puts("Invalid choice");
break;
}
}
}