detect.c (990B)
1 #include <stddef.h> 2 3 #include "common.h" 4 #include "scan.h" 5 6 #ifdef __cplusplus 7 extern "C" { 8 #endif 9 10 static int hdr_is_hex(unsigned char c) { 11 return (c >= '0' && c <= '9') || 12 (c >= 'a' && c <= 'f') || 13 (c >= 'A' && c <= 'F'); 14 } 15 16 char fmt_hdr_detect(unsigned char *cipherdata, size_t len) { 17 const unsigned char *pos = cipherdata; 18 const unsigned char *end = cipherdata + len; 19 const unsigned char *line; 20 size_t linelen; 21 int which; 22 const unsigned char *val; 23 size_t vallen; 24 25 // Claim the buffer when any key-labeled line with a non-empty value 26 // appears anywhere in it; surrounding stream content is irrelevant. 27 while (hdr_next_line(&pos, end, &line, &linelen)) { 28 if (!hdr_match_label(line, linelen, &which, &val, &vallen)) continue; 29 if (vallen == 0) continue; 30 // A hex run or a parenthesized absent-marker both count as values 31 if (val[0] == '(' || hdr_is_hex(val[0])) return 1; 32 } 33 return 0; 34 } 35 36 #ifdef __cplusplus 37 } // extern "C" 38 #endif