home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.c      Meh, in C you gotta define EVERYTHING      243,242 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 242,412 of 243,242   
   Michael Sanders to Michael Sanders   
   Re: is_binary_file()   
   10 Dec 25 20:57:49   
   
   From: porkchop@invalid.foo   
      
   On Wed, 10 Dec 2025 18:41:22 -0000 (UTC), Michael Sanders wrote:   
      
   > Last version for me (I have to pivot to other things).   
   >   
   > [...]   
      
   smaller look up table still + bit shifting!   
      
   *fastest implantation yet* but virtually unreadable =(   
      
   #include    
   #include    
   #include    
      
   // is_text_file()   
   // Returns:   
   //   -1 : could not open file   
   //    0 : is NOT a text file (binary indicators found)   
   //    1 : is PROBABLY a text file (no strong binary signatures)   
      
   int is_text_file(const char *path) {   
       FILE *f = fopen(path, "rb");   
       if (!f) return -1;   
      
       unsigned char chunk[4096];   
       size_t n, i;   
      
       // 128-bit bitmask (16 bytes × 8 bits / byte), 1=allowed, 0=disallowed   
       // Allowed bytes: TAB(0x09), LF(0x0A), CR(0x0D), printable ASCII   
   0x20–0x7E   
      
       static const uint8_t MASK[16] = {   
           0x00, 0x24, 0x00, 0x00, // 0x00–0x0F: TAB(09), LF(0A), CR(0D)   
           0xFF, 0xFF, 0xFF, 0xFF, // 0x10–0x2F: SPC!"#$%&'()*+,-./   
           0xFF, 0xFF, 0xFF, 0xFF, // 0x30–0x4F: 0123456789:;<=>?@   
           0xFF, 0xFF, 0xFF, 0x7F  // 0x50–0x7F: ABCDEFGHIJKLMNOP   
   RSTUVWXYZ[\]^_`abcdef...   
       };   
      
       while ((n = fread(chunk, 1, sizeof(chunk), f)) > 0) {   
           for (i = 0; i < n; i++) {   
               if (chunk[i] < 128 && !(MASK[chunk[i] >> 3] & (1 << (chunk[i] &   
   7)))) {   
                   fclose(f);   
                   return 0; // binary indicator found   
               }   
           // bytes >= 128 are accepted as probably text   
           }   
       }   
      
       fclose(f);   
       return 1; // probably text   
   }   
      
   --   
   :wq   
   Mike Sanders   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca