· 4 years ago · May 17, 2021, 01:16 PM
1//21-0
2#include <dlfcn.h>
3#include <stdio.h>
4
5int main(int argc, char** argv) {
6 char* libso_name = argv[1];
7 char* func_name = argv[2];
8
9 void* handle = dlopen(libso_name, RTLD_LAZY);
10 double (*ptr_func)(double) = dlsym(handle, func_name);
11
12 double num;
13 while (scanf("%lf", &num) != EOF) {
14 printf("%.3f\n", ptr_func(num));
15 }
16 dlclose(handle);
17 return 0;
18}
19
20//21-1
21#include <dlfcn.h>
22#include <fcntl.h>
23#include <semaphore.h>
24#include <stdio.h>
25#include <string.h>
26#include <sys/mman.h>
27#include <unistd.h>
28
29typedef struct {
30 sem_t request_ready; // начальное значение 0
31 sem_t response_ready; // начальное значение 0
32 char func_name[20];
33 double value;
34 double result;
35} shared_data_t;
36
37typedef double (*double_to_double)(double);
38
39int main(int argc, char** argv) {
40 char* libso_name = argv[1];
41 char shm_name[] = "/nickzay";
42 int shm_fd = shm_open(shm_name, O_CREAT | O_RDWR, 0644);
43 ftruncate(shm_fd, sizeof(shared_data_t));
44
45 shared_data_t* data = mmap(
46 /* desired addr, addr = */ NULL,
47 /* length = */ sizeof(shared_data_t),
48 /* access attributes, prot = */ PROT_READ | PROT_WRITE,
49 /* flags = */ MAP_SHARED,
50 /* fd = */ shm_fd,
51 /* offset in file, offset = */ 0
52 );
53 sem_init(&data->request_ready, 1, 0);
54 sem_init(&data->response_ready, 1, 0);
55
56 void* handle = dlopen(libso_name, RTLD_LAZY);
57 printf("%s\n", shm_name);
58 fflush(stdout);
59
60 while (1) {
61 sem_wait(&data->request_ready);
62 if (strlen(data->func_name) == 0) {
63 break;
64 }
65 double_to_double ptr_func = dlsym(handle, data->func_name);
66 data->result = ptr_func(data->value);
67 sem_post(&data->response_ready);
68 }
69
70 shm_unlink(shm_name);
71 sem_destroy(&data->request_ready);
72 sem_destroy(&data->response_ready);
73 munmap(data, sizeof(shared_data_t));
74 dlclose(handle);
75 return 0;
76}
77
78//22-0
79#include <arpa/inet.h>
80#include <assert.h>
81#include <errno.h>
82#include <netdb.h>
83#include <netinet/in.h>
84#include <netinet/ip.h>
85#include <netinet/ip_icmp.h>
86#include <pthread.h>
87#include <stdio.h>
88#include <stdlib.h>
89#include <stdbool.h>
90#include <string.h>
91#include <sys/socket.h>
92#include <sys/time.h>
93#include <sys/types.h>
94#include <time.h>
95#include <unistd.h>
96
97
98int main(int argc, char **argv) {
99 int port_number = strtol(argv[1], NULL, 10);
100 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
101
102 struct sockaddr_in addr = {
103 .sin_family = AF_INET,
104 .sin_addr = inet_addr("127.0.0.1"),
105 .sin_port = htons(port_number)
106 };
107 int number;
108 while (scanf("%d", &number) > 0) {
109 sendto(socket_fd, &number, sizeof(number), 0, (struct sockaddr*)&addr, sizeof(addr));
110 recvfrom(socket_fd, &number, sizeof(number), 0, NULL, NULL);
111 printf("%d\n", number);
112 }
113 close(socket_fd);
114}
115
116//22-1
117#include <stdio.h>
118#include <stdlib.h>
119#include <unistd.h>
120#include <sys/time.h>
121#include <sys/socket.h>
122#include <stdint.h>
123#include <string.h>
124#include <fcntl.h>
125#include <dlfcn.h>
126#include <netinet/in.h>
127#include <netinet/ip_icmp.h>
128#include <time.h>
129#include <netdb.h>
130
131typedef enum {
132 PING_PKT_S = 64,
133 PORT_NO = 0,
134 RECV_TIMEOUT = 1
135} constants;
136
137// ping packet structure
138typedef struct {
139 struct icmphdr hdr;
140 char msg[PING_PKT_S - sizeof(struct icmphdr)];
141} ping_pkt_t;
142
143unsigned short checksum(void* b, int len) {
144 unsigned short* buf = b;
145 unsigned int sum = 0;
146 unsigned short result;
147
148 for (sum = 0; len > 1; len -= 2) {
149 sum += *buf++;
150 }
151 if (len == 1) {
152 sum += *(unsigned char *) buf;
153 }
154 sum = (sum >> 16) + (sum & 0xFFFF);
155 sum += (sum >> 16);
156 result = ~sum;
157 return result;
158}
159
160void ping(char* host, uint64_t interval, uint64_t timeout) {
161 struct hostent *host_entity = gethostbyname(host);
162 struct sockaddr_in ping_addr = {
163 .sin_family = host_entity->h_addrtype,
164 .sin_port = htons(PORT_NO),
165 .sin_addr.s_addr = *(long *)host_entity->h_addr
166 };
167
168 int socket_fd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP);
169 int msg_count = 0;
170 int msg_received_count = 0;
171
172 ping_pkt_t ping_pkt;
173 struct sockaddr_in recv_addr;
174 struct timespec tfs, tfe;
175
176 struct timeval tv_out;
177 tv_out.tv_sec = RECV_TIMEOUT;
178 tv_out.tv_usec = 0;
179 setsockopt(socket_fd, SOL_SOCKET, SO_RCVTIMEO, (char*)&tv_out, sizeof(tv_out));
180
181 long double total_sec = 0;
182 clock_gettime(CLOCK_MONOTONIC, &tfs);
183
184 while (total_sec < timeout) {
185 bzero(&ping_pkt, sizeof(ping_pkt));
186
187 ping_pkt.hdr.type = ICMP_ECHO;
188 ping_pkt.hdr.un.echo.id = getpid();
189 int i = 0;
190 for (i = 0; i < sizeof(ping_pkt.msg) - 1; i++) {
191 ping_pkt.msg[i] = i + '0';
192 }
193 ping_pkt.msg[i] = 0;
194 ping_pkt.hdr.un.echo.sequence = msg_count++;
195 ping_pkt.hdr.checksum = checksum(&ping_pkt, sizeof(ping_pkt));
196 usleep(interval);
197 sendto(socket_fd,
198 &ping_pkt,
199 sizeof(ping_pkt),
200 0,
201 (const struct sockaddr*)&ping_addr,
202 sizeof(ping_addr));
203
204 int addr_len = sizeof(recv_addr);
205 if (recvfrom(socket_fd,
206 &ping_pkt,
207 sizeof(ping_pkt),
208 0,
209 (struct sockaddr *) &recv_addr,
210 &addr_len) <= 0
211 && msg_count > 1) {
212 } else {
213 ++msg_received_count;
214 }
215 clock_gettime(CLOCK_MONOTONIC, &tfe);
216 total_sec = ((double) (tfe.tv_sec - tfs.tv_sec)) +
217 ((double) (tfe.tv_nsec - tfs.tv_nsec)) / 1000000000.0;
218 }
219 printf("%d\n", msg_received_count);
220}
221
222int main(int argc, char *argv[]) {
223 char *host = argv[1];
224 uint64_t timeout = strtoll(argv[2], NULL, 10);
225 uint64_t interval = strtoll(argv[3], NULL, 10);
226 ping(host, interval, timeout);
227}
228Resubmit Download run
229Run comments
230Author Run comment
231Judge
2322021/05/13 13:23:02
233Кожагулов Рамазан - посылка 2388 в контесте 13
234
235
236//23-0
237#include <arpa/inet.h>
238#include <limits.h>
239#include <stdio.h>
240#include <stdlib.h>
241#include <string.h>
242#include <sys/socket.h>
243#include <unistd.h>
244#include <stdbool.h>
245
246#define kMaxLength 1024
247
248char* EncodeHost(char* encoded, char* host_nm) {
249 char* begin = host_nm;
250 char* end = begin;
251 bool is_end = false;
252 while (!is_end) {
253 end = strchr(end, '.');
254 if (end == NULL) {
255 end = host_nm + strlen(host_nm);
256 is_end = true;
257 }
258 size_t len = end - begin;
259 *encoded = len;
260 ++encoded;
261 memcpy(encoded, begin, len);
262 encoded += len;
263 ++end;
264 begin = end;
265 }
266 return encoded;
267}
268
269void SendDnsRequest(int socket_fd, char* host_name) {
270 const int head_len = 12;
271 const int tail_len = 5;
272 unsigned char head[] = "\x00\x00\x01\x20\x00\x01\x00\x00\x00\x00\x00\x00";
273 unsigned char tail[] = "\x00\x00\x01\x00\x01";
274 const int request_length = HOST_NAME_MAX * 2 + head_len + tail_len;
275
276 unsigned char request[request_length];
277 memset(request, 0, request_length);
278
279 memcpy(request, head, head_len);
280 unsigned char* request_end = EncodeHost(request + head_len, host_name);
281 memcpy(request_end, tail, tail_len);
282 size_t request_len = request_end - request + tail_len;
283
284 struct sockaddr_in addr = {
285 .sin_family = AF_INET,
286 .sin_port = htons(53),
287 .sin_addr.s_addr = inet_addr("8.8.8.8")
288 };
289 sendto(socket_fd, &request, request_len, 0, (struct sockaddr*)&addr, sizeof(addr));
290}
291
292in_addr_t ReceiveResponse(int socket_fd) {
293 unsigned char response[kMaxLength];
294 size_t total_read = read(socket_fd, response, sizeof(response));
295 in_addr_t addr = *(in_addr_t*)(response + (total_read - 4));
296 return addr;
297}
298
299in_addr_t GetIp(char* host_name) {
300 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
301 SendDnsRequest(socket_fd, host_name);
302 return ReceiveResponse(socket_fd);
303}
304
305
306char* ReadFile(FILE* file) {
307 char line[100];
308 line[0] = '\0';
309
310 size_t length = 0;
311 while (strcmp(line, "\r\n") != 0) {
312 fgets(line, sizeof(line), file);
313 sscanf(line, "Content-Length: %ld", &length);
314 }
315
316 char* buffer = calloc(length, sizeof(char));
317 fread(buffer, sizeof(char), length, file);
318 return buffer;
319}
320
321
322int main(int argc, char** argv) {
323 char* host_name = argv[1];
324 char* path = argv[2];
325
326 size_t header_info = 30;
327 char request[strlen(path) + strlen(host_name) + header_info];
328 snprintf(request, sizeof(request),
329 "GET %s HTTP/1.1\r\n"
330 "Host: %s\r\n"
331 "\r\n",
332 path, host_name);
333
334 int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
335
336 struct sockaddr_in addr = {
337 .sin_family = AF_INET,
338 .sin_port = htons(80),
339 .sin_addr.s_addr = GetIp(host_name)
340 };
341
342 connect(socket_fd, (const struct sockaddr*)&addr, sizeof(struct sockaddr_in));
343
344 FILE* socket_file = fdopen(socket_fd, "r+");
345 fwrite(request, sizeof(char), strlen(request), socket_file);
346
347 char* buffer = ReadFile(socket_file);
348 printf("%s", buffer);
349 free(buffer);
350
351 shutdown(socket_fd, SHUT_RDWR);
352 close(socket_fd);
353 return 0;
354}
355
356//23-1
357#include <arpa/inet.h>
358#include <limits.h>
359#include <stdio.h>
360#include <stdlib.h>
361#include <string.h>
362#include <sys/socket.h>
363#include <unistd.h>
364#include <stdbool.h>
365
366#define kMaxLength 1024
367
368char* EncodeHost(char* encoded, char* host_name) {
369 char* begin = host_name;
370 char* end = begin;
371 bool is_end = false;
372 while (!is_end) {
373 end = strchr(end, '.');
374 if (end == NULL) {
375 end = host_name + strlen(host_name);
376 is_end = true;
377 }
378 size_t len = end - begin;
379 *encoded = len;
380 ++encoded;
381 memcpy(encoded, begin, len);
382 encoded += len;
383 ++end;
384 begin = end;
385 }
386 return encoded;
387}
388
389void SendDnsRequest(int socket_fd, char* host_name) {
390 const int head_len = 12;
391 const int tail_len = 5;
392 unsigned char head[] = "\x00\x00\x01\x20\x00\x01\x00\x00\x00\x00\x00\x00";
393 unsigned char tail[] = "\x00\x00\x01\x00\x01";
394 const int request_length = HOST_NAME_MAX * 2 + head_len + tail_len;
395
396 unsigned char request[request_length];
397 memset(request, 0, request_length);
398
399 memcpy(request, head, head_len);
400 unsigned char* request_end = EncodeHost(request + head_len, host_name);
401 memcpy(request_end, tail, tail_len);
402 size_t request_len = request_end - request + tail_len;
403
404 struct sockaddr_in addr = {
405 .sin_family = AF_INET,
406 .sin_port = htons(53),
407 .sin_addr.s_addr = inet_addr("8.8.8.8")
408 };
409 sendto(socket_fd, &request, request_len, 0, (struct sockaddr*)&addr, sizeof(addr));
410}
411
412in_addr_t ReceiveResponse(int socket_fd) {
413 unsigned char response[kMaxLength];
414 size_t total_read = read(socket_fd, response, sizeof(response));
415 in_addr_t addr = *(in_addr_t*)(response + (total_read - 4));
416 return addr;
417}
418
419in_addr_t GetIp(char* host_name) {
420 int socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
421 SendDnsRequest(socket_fd, host_name);
422 return ReceiveResponse(socket_fd);
423}
424
425char* ReadFile(FILE* file) {
426 char line[100];
427 line[0] = '\0';
428
429 size_t length = 0;
430 while (strcmp(line, "\r\n") != 0) {
431 fgets(line, sizeof(line), file);
432 sscanf(line, "Content-Length: %ld", &length);
433 }
434
435 char* buffer = calloc(length, sizeof(char));
436 fread(buffer, sizeof(char), length, file);
437 return buffer;
438}
439
440size_t GetFileSize(FILE* file) {
441 fseek(file, 0L, SEEK_END);
442 size_t size = ftell(file);
443 rewind(file);
444 return size;
445}
446
447int main(int argc, char** argv) {
448 char* host_name = argv[1];
449 char* path = argv[2];
450
451 FILE* local_file = fopen(argv[3], "r");
452 size_t file_size = GetFileSize(local_file);
453
454 size_t header_info = 100;
455 char header[strlen(path) + strlen(host_name) + header_info];
456 snprintf(header, sizeof(header),
457 "POST %s HTTP/1.1\r\n"
458 "Host: %s\r\n"
459 "Content-Length: %ld\r\n"
460 "\r\n",
461 path, host_name, file_size);
462
463 int socket_fd = socket(AF_INET, SOCK_STREAM, 0);
464
465 struct sockaddr_in addr = {
466 .sin_family = AF_INET,
467 .sin_port = htons(80),
468 .sin_addr.s_addr = GetIp(host_name)
469 };
470 connect(socket_fd, (const struct sockaddr *)&addr, sizeof(struct sockaddr_in));
471
472 char* buffer = calloc(file_size, sizeof(char));
473 fread(buffer, sizeof(char), file_size, local_file);
474
475 FILE* socket_file = fdopen(socket_fd, "r+");
476 fwrite(header, sizeof(char), strlen(header), socket_file);
477 fwrite(buffer, sizeof(char), strlen(buffer), socket_file);
478 free(buffer);
479
480 buffer = ReadFile(socket_file);
481 printf("%s", buffer);
482 free(buffer);
483
484 shutdown(socket_fd, SHUT_RDWR);
485 close(socket_fd);
486 return 0;
487}
488
489//24-0
490#define FUSE_USE_VERSION 30 // API version 3.0
491#define _FILE_OFFSET_BITS 64
492
493#include <fuse.h>
494#include <limits.h>
495#include <stdlib.h>
496#include <stddef.h>
497#include <stdio.h>
498#include <string.h>
499#include <errno.h>
500#include <sys/mman.h>
501#include <sys/types.h>
502#include <sys/stat.h>
503#include <fcntl.h>
504#include <unistd.h>
505#include <stdbool.h>
506
507
508struct File {
509 char name[NAME_MAX];
510 size_t size;
511 char* data;
512};
513
514struct {
515 size_t files_cnt;
516 struct File* files;
517 size_t data_sz;
518 char* data;
519} image;
520
521
522void InitImage(const char* file_name) {
523 FILE* file = fopen(file_name, "r");
524 fscanf(file, "%ld\n", &image.files_cnt);
525 image.files = calloc(image.files_cnt, sizeof(struct File));
526
527 for (size_t i = 0; i < image.files_cnt; ++i) {
528 fscanf(file, "%s %ld\n", image.files[i].name, &image.files[i].size);
529 }
530 fscanf(file, "\n");
531 off_t offset = ftell(file);
532
533 fclose(file);
534 int fd = open(file_name, O_RDONLY);
535
536 struct stat st;
537 fstat(fd, &st);
538 image.data_sz = st.st_size;
539 image.data = mmap(NULL, image.data_sz, PROT_READ, MAP_PRIVATE, fd, 0);
540
541 for (size_t i = 0; i < image.files_cnt; ++i) {
542 image.files[i].data = image.data + offset;
543 offset += image.files[i].size;
544 }
545 close(fd);
546}
547
548void DestroyImage() {
549 munmap(image.data, image.data_sz);
550 free(image.files);
551}
552
553// Самый важный колбэк. Вызывается первым при любом другом колбэке.
554// Заполняет структуру stbuf.
555int getattr_callback(
556 const char* path,
557 struct stat* st,
558 struct fuse_file_info* fi) {
559 // check if accessing root directory
560 if (strcmp(path, "/") == 0) {
561 st->st_mode = 0555 | S_IFDIR; // file type - dir, access read only, executable by default
562 st->st_nlink = 2; // at least 2 links: '.' and parent
563 return 0; // success!
564 }
565
566 bool file_exist = false;
567 for (size_t i = 0; i < image.files_cnt; ++i) {
568 if (strcmp(path + 1, image.files[i].name) == 0) {
569 st->st_size = image.files[i].size; // bytes available
570 file_exist = true;
571 break;
572 }
573 }
574 if (!file_exist) {
575 return -ENOENT; // error: file not found
576 }
577
578 st->st_mode = S_IFREG | 0444; // file type - regular, access read only
579 st->st_nlink = 1; // only one link to file
580 return 0; // success
581}
582
583// узнать содержимое каталога
584int readdir_callback(
585 const char* path,
586 void* buf,
587 fuse_fill_dir_t filler,
588 off_t offset,
589 struct fuse_file_info* fi,
590 enum fuse_readdir_flags flags) {
591 if (strcmp(path, "/") != 0) {
592 return -ENOENT; // we do not have subdirectories
593 }
594 // filler(buf, filename, stat, flags) -- заполняет информацию о файле и вставляет её в buf.
595 // two mandatory entries: the directory itself and its parent
596 filler(buf, ".", NULL, 0, 0);
597 filler(buf, "..", NULL, 0, 0);
598 // directory contents
599
600 for (size_t i = 0; i < image.files_cnt; ++i) {
601 filler(buf, image.files[i].name, NULL, 0, 0);
602 }
603 return 0; // success
604}
605
606// Вызывается после успешной обработки open.
607int read_callback(
608 const char* path,
609 char* out,
610 size_t size,
611 off_t off,
612 struct fuse_file_info *fi) {
613 // look for file
614 struct File* file = NULL;
615 for (size_t i = 0; i < image.files_cnt; ++i) {
616 if (strcmp(path + 1, image.files[i].name) == 0) {
617 file = &image.files[i];
618 break;
619 }
620 }
621 if (!file) {
622 return -ENOENT; // error: file not found
623 }
624
625 if (off > file->size) {
626 return 0;
627 }
628 if (off + size > file->size) {
629 size = file->size - off;
630 }
631 const char* data = file->data + off;
632 // copy contents into the buffer to be filled by 'read' system call
633 memcpy(out, data, size);
634 // return value - bytes count
635 return size;
636}
637
638
639static struct fuse_operations operations = {
640 .getattr = getattr_callback,
641 .read = read_callback,
642 .readdir = readdir_callback,
643};
644
645
646int main(int argc, char** argv) {
647 // arguments to be preprocessed before passing to /sbin/mount.fuse3
648 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
649
650 typedef struct {
651 char* src;
652 } src_opt_t;
653 src_opt_t src_opt = {NULL};
654
655
656 struct fuse_opt opt_specs[] = {
657 // the string value to be written to src_opt_t.src
658 {"--src %s", offsetof(src_opt_t, src), 0},
659 FUSE_OPT_END // end-of-array
660 };
661 fuse_opt_parse(&args, &src_opt, opt_specs, NULL);
662
663 InitImage(src_opt.src);
664 // run daemon
665 int ret = fuse_main(
666 args.argc, args.argv, // arguments to be passed to /sbin/mount.fuse3
667 &operations, // pointer to callback functions
668 NULL // optional pointer to user-defined data
669 );
670 fuse_opt_free_args(&args);
671 DestroyImage();
672 return ret;
673}
674
675//24-1
676#define FUSE_USE_VERSION 30 // API version 3.0
677#define _FILE_OFFSET_BITS 64
678
679#include <fuse.h>
680#include <unordered_set>
681#include <cstring>
682#include <cerrno>
683#include <iostream>
684#include <chrono>
685
686#include <filesystem>
687#include <fstream>
688#include <vector>
689#include <sys/types.h>
690#include <sys/stat.h>
691
692namespace fs = std::filesystem;
693std::vector<fs::path> roots;
694
695void InitRoots(char* format) {
696 size_t dirs_cnt = 0;
697 for (char* colon = format; colon != nullptr; colon = strchr(colon, ':')) {
698 ++colon;
699 ++dirs_cnt;
700 }
701
702 for (size_t i = 0; i < dirs_cnt; ++i) {
703 char* colon = strchr(format, ':');
704 if (colon == nullptr) {
705 colon = format + strlen(format);
706 }
707 std::string root_name(format, colon - format);
708 fs::path root_dir(root_name);
709 if (!root_dir.is_absolute()) {
710 root_dir = fs::absolute(root_dir);
711 }
712 roots.emplace_back(root_dir);
713
714 ++colon;
715 format = colon;
716 }
717}
718
719fs::path GetMostRecentChangedPath(const std::string& rel_path) {
720 fs::file_time_type recent = fs::file_time_type::min();
721 ssize_t root_id = -1;
722
723 for (ssize_t i = 0; i < roots.size(); ++i) {
724 fs::path full_path = roots[i] / rel_path;
725 if (exists(full_path)) {
726 auto time = fs::last_write_time(full_path);
727 if (time > recent) {
728 recent = time;
729 root_id = i;
730 }
731 }
732 }
733 if (root_id == -1) {
734 return fs::path();
735 }
736 return roots[root_id] / rel_path;
737}
738
739// Самый важный колбэк. Вызывается первым при любом другом колбэке.
740// Заполняет структуру stbuf.
741int getattr_callback(
742 const char* path,
743 struct stat* st,
744 struct fuse_file_info *fi){
745 ++path;
746 auto full_path = GetMostRecentChangedPath(path);
747 if (full_path.empty()) {
748 return -ENOENT; // error: file not found
749 }
750
751 memset(st, 0, sizeof(struct stat));
752 auto status = stat(full_path.c_str(), st);
753 if (S_ISDIR(st->st_mode)) {
754 st->st_mode = 0555 | S_IFDIR;
755 }
756 if (S_ISREG(st->st_mode)) {
757 st->st_mode = 0444 | S_IFREG;
758 }
759 return status;
760}
761
762// узнать содержимое каталога
763int readdir_callback(
764 const char* path,
765 void* buf,
766 fuse_fill_dir_t filler,
767 off_t offset,
768 struct fuse_file_info* fi,
769 enum fuse_readdir_flags flags) {
770 ++path;
771 std::vector<fs::path> dirs;
772 for (const auto& root : roots) {
773 fs::path full_path = root / path;
774 if (fs::exists(full_path) &&
775 fs::is_directory(fs::directory_entry(full_path).status())) {
776 dirs.push_back(full_path);
777 }
778 }
779
780 if (dirs.empty()) {
781 return -ENOENT; // no dirs found
782 }
783 // filler(buf, filename, stat, flags) -- заполняет информацию о файле и вставляет её в buf.
784 // two mandatory entries: the directory itself and its parent
785 filler(buf, ".", nullptr, 0, fuse_fill_dir_flags(0));
786 filler(buf, "..", nullptr, 0, fuse_fill_dir_flags(0));
787
788 std::unordered_set<std::string> added;
789 for (const auto& dir : dirs) {
790 for (const auto& entity : fs::directory_iterator(dir)) {
791 std::string filename(fs::path(entity).filename().c_str());
792 if (added.count(filename) == 0) {
793 added.insert(filename);
794 filler(buf, filename.data(), nullptr, 0, fuse_fill_dir_flags(0));
795 }
796 }
797 }
798 return 0; // success
799}
800
801// Вызывается после успешной обработки open.
802int read_callback(const char* path,
803 char* out,
804 size_t size,
805 off_t off,
806 struct fuse_file_info *fi){
807 ++path;
808 auto full_path = GetMostRecentChangedPath(path);
809 if (full_path.empty()) {
810 return -ENOENT; // error: file not found
811 }
812
813 size_t file_sz = fs::file_size(full_path);
814 if (off > file_sz) {
815 return 0;
816 }
817 if (off + size > file_sz) {
818 size = file_sz - off;
819 }
820 std::ifstream file(full_path, std::ios::in | std::ios::binary);
821
822 // copy contents into the buffer to be filled by 'read' system call
823 file.seekg(off);
824 file.read(out, size);
825 // return value - bytes count
826 return size;
827}
828
829struct cpp_fuse_operations : fuse_operations {
830 cpp_fuse_operations() {
831 getattr = getattr_callback;
832 read = read_callback;
833 readdir = readdir_callback;
834 }
835} operations;
836
837
838int main(int argc, char** argv) {
839 // arguments to be preprocessed before passing to /sbin/mount.fuse3
840 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
841
842 typedef struct {
843 char* dirs;
844 } src_opt_t;
845
846 src_opt_t src_opt{nullptr};
847
848 struct fuse_opt opt_specs[] = {
849 // the string value to be written to src_opt_t.dirs
850 {"--src %s", offsetof(src_opt_t, dirs), 0},
851 FUSE_OPT_END // end-of-array
852 };
853
854 fuse_opt_parse(&args, &src_opt, opt_specs, nullptr);
855 InitRoots(src_opt.dirs);
856
857 // run daemon
858 int ret = fuse_main(
859 args.argc, args.argv, // arguments to be passed to /sbin/mount.fuse3
860 &operations, // pointer to callback functions
861 nullptr // optional pointer to user-defined data
862 );
863 fuse_opt_free_args(&args);
864 return ret;
865}
866Resubmit Download run
867Run comments
868Author Run comment
869Judge
8702021/05/13 13:08:30
871Баюк Илья - посылка 1800 в контесте 34 (МФТИ, ФПМИ.Индустрия, АКОС 2019/20)
872
873//25-0
874#include <openssl/sha.h>
875#include <string.h>
876#include <stdio.h>
877#include <unistd.h>
878
879int main() {
880 SHA512_CTX ctx;
881 SHA512_Init(&ctx);
882
883 size_t bytes_read = 0;
884 char buffer[SHA512_DIGEST_LENGTH];
885 while ((bytes_read = read(STDIN_FILENO, buffer, SHA512_DIGEST_LENGTH)) > 0) {
886 SHA512_Update(&ctx, buffer, bytes_read);
887 }
888
889 unsigned char hash[SHA512_DIGEST_LENGTH];
890 SHA512_Final(hash, &ctx);
891
892 printf("0x");
893 for (size_t i = 0; i < SHA512_DIGEST_LENGTH; ++i) {
894 printf("%02x", hash[i]);
895 }
896 printf("\n");
897 return 0;
898}
899
900//25-1
901#include <openssl/evp.h>
902#include <string.h>
903#include <stdio.h>
904#include <unistd.h>
905
906int main(int argc, char** argv) {
907 const char* password = argv[1];
908
909 unsigned char preamble[16];
910 read(STDIN_FILENO, preamble, sizeof(preamble));
911 unsigned char salt[8];
912 memcpy(salt, preamble + 8, sizeof(salt));
913
914 unsigned char key[32] = {'\0'};
915 unsigned char iv[16] = {'\0'};
916
917 // Генерация ключа и начального вектора из
918 // пароля произвольной длины и 8-байтной соли
919 EVP_BytesToKey(
920 EVP_aes_256_cbc(), // алгоритм шифрования
921 EVP_sha256(), // алгоритм хеширования пароля
922 salt, // соль
923 password, strlen(password), // пароль
924 1, // количество итераций хеширования
925 key, // результат: ключ нужной длины
926 iv // результат: начальный вектор нужной длины
927 );
928 // Создание контекста
929 EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
930
931 // Начальная стадия: инициализация
932 EVP_DecryptInit_ex(
933 ctx, // контекст для хранения состояния
934 EVP_aes_256_cbc(), // алгоритм шифрования
935 NULL,
936 key, // ключ нужного размера
937 iv // начальное значение нужного размера
938 );
939
940 const size_t block_size = EVP_CIPHER_CTX_block_size(ctx);
941 unsigned char encoded[block_size];
942 unsigned char decoded[block_size];
943
944 ssize_t bytes_read = 0;
945 int bytes_written = 0;
946 while ((bytes_read = read(STDIN_FILENO, encoded, sizeof(encoded))) > 0) {
947 EVP_DecryptUpdate(ctx, decoded, &bytes_written, encoded, bytes_read);
948 printf("%.*s", bytes_written, decoded);
949 }
950 EVP_DecryptFinal_ex(ctx, decoded, &bytes_written);
951 printf("%.*s", bytes_written, decoded);
952
953 EVP_CIPHER_CTX_free(ctx);
954 return 0;
955}
956