module_09.c 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. typedef struct {
  11. const char *name;
  12. const unsigned char *signature;
  13. size_t sig_len;
  14. const char *mime_type;
  15. int offset; // Смещение для чтения сигнатуры
  16. } Signature;
  17. // Расширенная база сигнатур
  18. static const Signature signatures[] = {
  19. {"PNG", (unsigned char *)"\x89PNG\r\n\x1A\n", 8, "image/png", 0},
  20. {"JPEG", (unsigned char *)"\xFF\xD8\xFF", 3, "image/jpeg", 0},
  21. {"ELF", (unsigned char *)"\x7F" "ELF", 4, "application/x-executable", 0},
  22. {"PDF", (unsigned char *)"%PDF-", 5, "application/pdf", 0},
  23. {"GIF", (unsigned char *)"GIF89a", 6, "image/gif", 0},
  24. {"GIF", (unsigned char *)"GIF87a", 6, "image/gif", 0},
  25. {"BMP", (unsigned char *)"BM", 2, "image/bmp", 0},
  26. {"WEBP", (unsigned char *)"RIFF....WEBP", 12, "image/webp", 0},
  27. {"MP3", (unsigned char *)"ID3", 3, "audio/mpeg", 0},
  28. {"WAV", (unsigned char *)"RIFF....WAVE", 12, "audio/wav", 0},
  29. {"FLAC", (unsigned char *)"fLaC", 4, "audio/flac", 0},
  30. {"MP4", (unsigned char *)"\x00\x00\x00\x20\x66\x74\x79\x70\x69\x73\x6F\x6D", 12, "video/mp4", 0}, // ftypisom
  31. {"MP4", (unsigned char *)"\x00\x00\x00\x18\x66\x74\x79\x70\x6D\x70\x34\x32", 12, "video/mp4", 0}, // ftypmp42
  32. {"MP4", (unsigned char *)"\x00\x00\x00\x1C\x66\x74\x79\x70\x64\x61\x73\x68", 12, "video/mp4", 0}, // ftypdash
  33. {"MKV", (unsigned char *)"\x1A\x45\xDF\xA3", 4, "video/x-matroska", 0},
  34. {"ZIP", (unsigned char *)"PK\x03\x04", 4, "application/zip", 0},
  35. {"TAR", (unsigned char *)"ustar", 5, "application/x-tar", 257}, // ustar at offset 257
  36. {"7Z", (unsigned char *)"\x37\x7A\xBC\xAF\x27\x1C", 6, "application/x-7z-compressed", 0},
  37. {"DOCX", (unsigned char *)"PK\x03\x04", 4, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", 0},
  38. {"XLSX", (unsigned char *)"PK\x03\x04", 4, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 0},
  39. {"SQLITE", (unsigned char *)"SQLite format 3", 15, "application/x-sqlite3", 0},
  40. {"EXE", (unsigned char *)"MZ", 2, "application/x-msdownload", 0},
  41. {"ISO", (unsigned char *)"\x43\x44\x30\x30\x31", 5, "application/x-iso9660-image", 32769}, // CD001 at offset 32769
  42. {"OGG", (unsigned char *)"OggS", 4, "audio/ogg", 0},
  43. {"AVI", (unsigned char *)"RIFF....AVI ", 12, "video/x-msvideo", 0},
  44. {"RAR", (unsigned char *)"Rar!\x1A\x07\x00", 7, "application/x-rar-compressed", 0},
  45. {"EPUB", (unsigned char *)"PK\x03\x04", 4, "application/epub+zip", 0},
  46. {"ODT", (unsigned char *)"PK\x03\x04", 4, "application/vnd.oasis.opendocument.text", 0},
  47. {"SIXEL", (unsigned char *)"\x1B\x50\x71", 3, "image/sixel", 0},
  48. {NULL, NULL, 0, NULL, 0}
  49. };
  50. int module_09_run(const char *filename) {
  51. char log_msg[1024];
  52. FILE *file = fopen(filename, "rb");
  53. if (!file) {
  54. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library custom_signature: " RED "File type not recognized: %s" RESET, strerror(errno));
  55. printf("%s\n", log_msg);
  56. return 1;
  57. }
  58. int found = 0;
  59. for (int i = 0; signatures[i].name; i++) {
  60. fseek(file, signatures[i].offset, SEEK_SET);
  61. unsigned char *buffer = malloc(signatures[i].sig_len);
  62. if (!buffer) {
  63. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library custom_signature: " RED "Memory allocation failed" RESET);
  64. printf("%s\n", log_msg);
  65. fclose(file);
  66. return 1;
  67. }
  68. size_t read_bytes = fread(buffer, 1, signatures[i].sig_len, file);
  69. if (read_bytes >= signatures[i].sig_len &&
  70. memcmp(buffer, signatures[i].signature, signatures[i].sig_len) == 0) {
  71. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library custom_signature: " WHITE "Signature match: %s, MIME: %s" RESET,
  72. signatures[i].name, signatures[i].mime_type);
  73. printf("%s\n", log_msg);
  74. found = 1;
  75. free(buffer);
  76. break;
  77. }
  78. free(buffer);
  79. }
  80. fclose(file);
  81. if (!found) {
  82. snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library custom_signature: " RED "No known signature found" RESET);
  83. printf("%s\n", log_msg);
  84. }
  85. return 0;
  86. }