-
Notifications
You must be signed in to change notification settings - Fork 0
/
OS lab1.4.c
138 lines (115 loc) · 2.43 KB
/
OS lab1.4.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
typedef unsigned char byte;
typedef unsigned int byte4;
typedef unsigned long long ull;
int search_for_mask(FILE* stream, ull* res, const char* mask) {
char* t_ptr = NULL;
byte4 mask_bin = strtoul(mask, &t_ptr, 16);
if (t_ptr != mask + strlen(mask)) {
return 1;
}
byte4 cur_mask = 0;
byte byte_from_file;
ull count = 0;
while (fread(&byte_from_file, sizeof(byte), 1, stream) == 1) {
cur_mask = cur_mask << 8;
cur_mask = cur_mask | byte_from_file;
if (cur_mask == mask_bin) {
count++;
}
}
*res = count;
return 0;
}
typedef enum {
ok,
inv_argc,
unknown_file,
mask_not_ok
} st_code;
byte xor8(FILE* stream) {
if (!stream) {
return 0;
}
byte res = 0;
int a;
while ((a = fgetc(stream)) != EOF) {
res = a ^ res;
}
return res;
}
byte4 xor32(FILE* stream) {
if (!stream) {
return 0;
}
byte4 res = 0;
byte4 temp;
while (fread(&temp, sizeof(byte4), 1, stream) == 1) {
res = res ^ temp;
}
temp = 0;
byte temp1;
while (fread(&temp1, sizeof(byte), 1, stream) == 1) {
temp = temp << 8;
temp = temp | temp1;
}
res = res ^ temp;
return res;
}
st_code find_func_and_print_result(int argc, char* argv[], FILE* stream) {
const char* flag = argv[2];
if (strcmp(flag, "xor8") == 0) {
if (argc != 3) {
return inv_argc;
}
printf("%d\n", xor8(stream));
} else if (strcmp(flag, "xor32") == 0) {
if (argc != 3) {
return inv_argc;
}
printf("%d\n", xor32(stream));
} else if (strcmp(flag, "mask") == 0) {
if (argc != 4) {
return inv_argc;
}
ull res;
if (search_for_mask(stream, &res, argv[3]) == 1) {
return mask_not_ok;
}
printf("%llu\n", res);
}
return ok;
}
st_code input(int argc, char* argv[]) {
if (argc < 3 || argc > 4) {
return inv_argc;
}
FILE* stream = fopen(argv[1], "rb");
if (stream == NULL) {
return unknown_file;
}
st_code res = find_func_and_print_result(argc, argv, stream);
fclose(stream);
return res;
}
int main(int argc, char* argv[]) {
switch(input(argc, argv)){
case ok:
printf("all ok\n");
break;
case inv_argc:
printf("invalid argc\n");
break;
case unknown_file:
printf("unknown file\n");
break;
case mask_not_ok:
printf("mask is unsuitable\n");
break;
default:
printf("unknown status code\n");
}
return 0;
}