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,435 of 243,242   
   Michael Sanders to Michael Sanders   
   Re: is_binary_file()   
   12 Dec 25 22:54:58   
   
   From: porkchop@invalid.foo   
      
   On Fri, 12 Dec 2025 19:25:41 -0000 (UTC), Michael Sanders wrote:   
      
   > [...]   
      
   Done.   
      
   Features...   
      
   - plugin maps   
   - follows sylinks   
   - rejects directories, devices, sockets   
      
   #include    
   #include    
   #include    
      
   /*   
    * map_strict[]   
    *   
    * Valid bytes:   
    *   - ASCII printable: 0x20–0x7E   
    *   - ISO-8859-1 high printable: 0xA0–0xFF   
    *   - Whitespace/control: TAB (0x09), LF (0x0A), CR (0x0D)   
    *   
    * Invalid bytes (binary indicators):   
    *   - NULL byte (0x00)   
    *   - C0 controls (0x01–0x08, 0x0B–0x0C, 0x0E–0x1F)   
    *   - DEL (0x7F)   
    *   - C1 controls (0x80–0x9F)   
    */   
      
   static const uint8_t map_strict[256] = {   
       0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0, // 00   
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 10   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 20   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 30   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 40   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 50   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 60   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, // 70   
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 80   
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 90   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // A0   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // B0   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // C0   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // D0   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // E0   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1  // F0   
   };   
      
   /*   
    * map_loose[]   
    *   
    * Valid bytes:   
    *   - ASCII printable characters: 0x20–0x7E   
    *   - Whitespace/control characters: TAB (0x09), LF (0x0A), CR (0x0D)   
    *   - High bytes: 0x80–0xFF   
    *   
    * Invalid bytes (binary indicators):   
    *   - NULL byte: 0x00   
    *   - C0 control codes: 0x01–0x08, 0x0B–0x0C, 0x0E–0x1F   
    *   - DEL character: 0x7F   
    */   
      
   static const uint8_t map_loose[256] = {   
       0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0, // 00   
       0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 10   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 20   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 30   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 40   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 50   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 60   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 70   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 80   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 90   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // A0   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // B0   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // C0   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // D0   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // E0   
       1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1  // F0   
   };   
      
   /*   
    * is_text_file()   
    *   
    * just plug in your own map[]...   
    *   
    * Returns:   
    *   1 - text   
    *   0 - binary indicator   
    *  -1 - could not open   
    */   
      
   int is_text_file(const char *path, const uint8_t map[256]) {   
      
       // now we follow symlinks...   
       struct stat st;   
       if (stat(path, &st) != 0) return -1; // can not access file   
       if (!S_ISREG(st.st_mode)) return -1; // reject: directories/devices/sockets   
      
       FILE *f = fopen(path, "rb");   
       if (!f) return -1; // could not open file   
      
       // 4KB: 4096, 8KB: 8192, 16KB: 16384, 32KB: 32768, 64KB: 65536   
       unsigned char buf[16384];   
       size_t n, i;   
      
       while ((n = fread(buf, 1, sizeof(buf), f)) > 0) {   
           for (i = 0; i < n; i++) {   
               if (!map[buf[i]]) {   
                   fclose(f);   
                   return 0; // not text (binary indicator detected)   
               }   
           }   
       }   
      
       fclose(f);   
       return 1; // probally text   
   }   
      
   // eof   
      
   --   
   :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