12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <errno.h>
- #define BOLD_GRAY "\033[1;90m"
- #define RED "\033[0;31m"
- #define WHITE "\033[0;37m"
- #define RESET "\033[0m"
- // Размер буфера для чтения
- #define BUFFER_SIZE 4096
- // Максимальный размер файла для полного анализа (1 ГБ)
- #define MAX_FILE_SIZE (1024 * 1024 * 1024)
- int module_10_run(const char *filename) {
- char log_msg[1024];
- FILE *file = fopen(filename, "rb");
- if (!file) {
- snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " RED "File type not recognized: %s" RESET, strerror(errno));
- printf("%s\n", log_msg);
- return 1;
- }
- // Получаем размер файла
- fseek(file, 0, SEEK_END);
- long size = ftell(file);
- fseek(file, 0, SEEK_SET);
- if (size > MAX_FILE_SIZE) {
- snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " RED "File too large: %ld bytes" RESET, size);
- printf("%s\n", log_msg);
- fclose(file);
- return 1;
- }
- // Читаем первые BUFFER_SIZE байт
- unsigned char *buffer = malloc(BUFFER_SIZE);
- if (!buffer) {
- snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " RED "Memory allocation failed" RESET);
- printf("%s\n", log_msg);
- fclose(file);
- return 1;
- }
- size_t read_bytes = fread(buffer, 1, BUFFER_SIZE, file);
- fclose(file);
- if (read_bytes == 0) {
- snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " RED "File type not recognized: Empty file" RESET);
- printf("%s\n", log_msg);
- free(buffer);
- return 1;
- }
- // Проверяем, текстовый ли файл
- int is_text = 1;
- for (size_t i = 0; i < read_bytes; i++) {
- if (buffer[i] == 0) {
- is_text = 0;
- break;
- }
- if (buffer[i] < 32 && buffer[i] != '\n' && buffer[i] != '\r' && buffer[i] != '\t' && buffer[i] != '\x1B') {
- is_text = 0;
- break;
- }
- }
- if (is_text) {
- char *text = malloc(read_bytes + 1);
- if (!text) {
- snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " RED "Memory allocation failed" RESET);
- printf("%s\n", log_msg);
- free(buffer);
- return 1;
- }
- memcpy(text, buffer, read_bytes);
- text[read_bytes] = '\0';
- snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " WHITE "Size: %ld bytes, First %zu bytes (text): %.100s..." RESET,
- size, read_bytes, text);
- free(text);
- } else {
- snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " WHITE "Size: %ld bytes, First %zu bytes (binary)" RESET,
- size, read_bytes);
- }
- printf("%s\n", log_msg);
- free(buffer);
- return 0;
- }
|