stabbish

:)
git clone https://git.sr.ht/~ashymad/stabbish
Log | Files | Refs | README | LICENSE

magic.c (586B)


      1 #include "magic.h"
      2 
      3 #include <unistd.h>
      4 
      5 const struct magic MAGICS[] = {
      6     {"xzcat", 6, {0xFD, 0x37, 0x7A, 0x58, 0x5A, 0x00}},
      7     {"zcat", 2, {0x1F, 0x8B}},
      8     {"bzcat", 3, {0x42, 0x5A, 0x68}},
      9 };
     10 
     11 struct magic magic_detect(fd file) {
     12   static struct magic try_return = {NULL, 0, {0x00}};
     13 
     14   unsigned char header[10];
     15 
     16   size_t header_len = try(read(file, &header, 10));
     17 
     18   for (int i = 0; i < tsizeof(MAGICS); i++) {
     19     if (header_len > MAGICS[i].length &&
     20         memcmp(header, MAGICS[i].byte, MAGICS[i].length) == 0) {
     21       return MAGICS[i];
     22     }
     23   }
     24 
     25   return try_return;
     26 }