Skip to content

Instantly share code, notes, and snippets.

@icchy
Last active May 30, 2016 17:32
Show Gist options
  • Save icchy/53c7f431fa825558dd3ae9ac639fadbe to your computer and use it in GitHub Desktop.
Save icchy/53c7f431fa825558dd3ae9ac639fadbe to your computer and use it in GitHub Desktop.
セキュリティ・キャンプ2016 選択課題4
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
REJECTED
PASS
PASS
PASS
REJECTED
REJECTED
PASS
PASS
PASS
REJECTED
REJECTED
REJECTED
PASS
PASS
PASS
REJECTED
REJECTED
REJECTED
REJECTED
PASS
PASS
PASS
REJECTED
REJECTED
REJECTED
REJECTED
REJECTED
PASS
PASS
PASS
REJECTED
REJECTED
REJECTED
REJECTED
REJECTED
REJECTED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#ifdef __linux
#include <sys/time.h>
#include <sys/resource.h>
#endif
char Magic[2];
char Source[20];
char Destination[20];
unsigned int DataLength;
char *Data;
int parse();
int cond1();
int cond2();
int cond3();
int cond4();
int cond5();
int cond6();
void reject();
#ifdef DEBUG
long getmem() {
struct rusage r;
if(getrusage(RUSAGE_SELF, &r) != 0) {
perror("getrusage");
}
return r.ru_maxrss;
}
#endif
int main() {
#ifdef DEBUG
clock_t clock_tmp;
long mem_tmp;
#endif
while(!parse()) {
#ifdef DEBUG
clock_tmp = clock();
getmem();
#endif
if(cond1() && cond2() && cond3() && cond4() && cond5() && cond6()) {
#ifdef DEBUG
fprintf(stderr, "matching: %d clock\n", clock() - clock_tmp);
#endif
fprintf(stdout, "PASS\n");
}
else {
#ifdef DEBUG
fprintf(stderr, "matching: %d clock\n", clock() - clock_tmp);
#endif
fprintf(stdout, "REJECTED\n");
}
free(Data);
}
#ifdef DEBUG
fprintf(stderr, "memory: %ld", getmem());
#endif
return 0;
}
void reject() {
fprintf(stdout, "REJECTED\n");
}
int parse() {
unsigned int DataLength_big;
if(fread(Magic, 1, 2, stdin) != 2) {
return -1;
}
if(fread(Source, 1, 20, stdin) != 20) {
return -1;
}
if(fread(Destination, 1, 20, stdin) != 20) {
return -1;
}
if(fread((char*)(&DataLength), 1, 4, stdin) != 4) {
return -1;
}
DataLength_big = htonl(DataLength);
Data = (char*)malloc(DataLength_big);
if(Data == NULL) {
perror("malloc");
}
if(fread(Data, 1, DataLength_big, stdin) != DataLength_big) {
return -1;
}
return 0;
}
int cond1() {
return Magic[0] == 'R' && Magic[1] == 'H';
}
int cond2() {
if(strcasecmp(Source, "rise-san") * strcasecmp(Source, "cocoa-san") == 0) {
return 1;
}
return 0;
}
int cond3() {
if(strcasecmp(Destination, "Chino-chan") * strcasecmp(Destination, "Chino") == 0) {
return 1;
}
return 0;
}
int cond4() {
if(strcasecmp(Source, "cocoa-san") == 0
&&strcasecmp(Source, "Chino") == 0) {
return 0;
}
return 1;
}
int cond5() {
const char *valid_order_brand[20] = {
"BlueMountain",
"Columbia",
"OriginalBlend"
};
int i;
for(i = 0; i < 3; i++) {
if(strstr(Data, valid_order_brand[i]) != NULL) {
return 1;
}
}
return 0;
}
int cond6() {
const char *invalid_order_brand[20] = {
"DandySoda",
"FrozenEvergreen"
};
int i;
for(i = 0; i < 2; i++) {
if(strstr(Data, invalid_order_brand[i]) != NULL) {
return 0;
}
}
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment