module_10.c 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <errno.h>
  5. #define BOLD_GRAY "\033[1;90m"
  6. #define RED "\033[0;31m"
  7. #define WHITE "\033[0;37m"
  8. #define RESET "\033[0m"
  9. // Размер буфера для чтения
  10. #define BUFFER_SIZE 4096
  11. // Максимальный размер файла для полного анализа (1 ГБ)
  12. #define MAX_FILE_SIZE (1024 * 1024 * 1024)
  13. int module_10_run(const char *filename) {
  14. char log_msg[1024];
  15. FILE *file = fopen(filename, "rb");
  16. if (!file) {
  17. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " RED "File type not recognized: %s" RESET, strerror(errno));
  18. printf("%s\n", log_msg);
  19. return 1;
  20. }
  21. // Получаем размер файла
  22. fseek(file, 0, SEEK_END);
  23. long size = ftell(file);
  24. fseek(file, 0, SEEK_SET);
  25. if (size > MAX_FILE_SIZE) {
  26. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " RED "File too large: %ld bytes" RESET, size);
  27. printf("%s\n", log_msg);
  28. fclose(file);
  29. return 1;
  30. }
  31. // Читаем первые BUFFER_SIZE байт
  32. unsigned char *buffer = malloc(BUFFER_SIZE);
  33. if (!buffer) {
  34. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " RED "Memory allocation failed" RESET);
  35. printf("%s\n", log_msg);
  36. fclose(file);
  37. return 1;
  38. }
  39. size_t read_bytes = fread(buffer, 1, BUFFER_SIZE, file);
  40. fclose(file);
  41. if (read_bytes == 0) {
  42. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " RED "File type not recognized: Empty file" RESET);
  43. printf("%s\n", log_msg);
  44. free(buffer);
  45. return 1;
  46. }
  47. // Проверяем, текстовый ли файл
  48. int is_text = 1;
  49. for (size_t i = 0; i < read_bytes; i++) {
  50. if (buffer[i] == 0) {
  51. is_text = 0;
  52. break;
  53. }
  54. if (buffer[i] < 32 && buffer[i] != '\n' && buffer[i] != '\r' && buffer[i] != '\t' && buffer[i] != '\x1B') {
  55. is_text = 0;
  56. break;
  57. }
  58. }
  59. if (is_text) {
  60. char *text = malloc(read_bytes + 1);
  61. if (!text) {
  62. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " RED "Memory allocation failed" RESET);
  63. printf("%s\n", log_msg);
  64. free(buffer);
  65. return 1;
  66. }
  67. memcpy(text, buffer, read_bytes);
  68. text[read_bytes] = '\0';
  69. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " WHITE "Size: %ld bytes, First %zu bytes (text): %.100s..." RESET,
  70. size, read_bytes, text);
  71. free(text);
  72. } else {
  73. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libc: " WHITE "Size: %ld bytes, First %zu bytes (binary)" RESET,
  74. size, read_bytes);
  75. }
  76. printf("%s\n", log_msg);
  77. free(buffer);
  78. return 0;
  79. }