regex.patch 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. diff -bBur file-3.20.1/apprentice.c zzz-file/apprentice.c
  2. --- file-3.20.1/apprentice.c Wed Jun 18 23:58:04 1997
  3. +++ zzz-file/apprentice.c Wed Mar 12 17:07:11 1997
  4. @@ -45,7 +45,7 @@
  5. static int getvalue __P((struct magic *, char **));
  6. static int hextoint __P((int));
  7. -static char *getstr __P((char *, char *, int, int *));
  8. +static char *getstr __P((char *, char *, int, int *, char));
  9. static int parse __P((char *, int *, int));
  10. static void eatsize __P((char **));
  11. @@ -162,6 +162,7 @@
  12. v = (long) v;
  13. break;
  14. case STRING:
  15. + case REGEX:
  16. break;
  17. default:
  18. magwarn("can't happen: m->type=%d\n",
  19. @@ -276,6 +277,7 @@
  20. #define NLESHORT 7
  21. #define NLELONG 6
  22. #define NLEDATE 6
  23. +#define NREGEX 5
  24. if (*l == 'u') {
  25. ++l;
  26. @@ -316,6 +318,10 @@
  27. } else if (strncmp(l, "ledate", NLEDATE)==0) {
  28. m->type = LEDATE;
  29. l += NLEDATE;
  30. + } else if (strncmp(l, "regex", NREGEX)==0) {
  31. + m->type = REGEX;
  32. + m->reln = '=';
  33. + l += NREGEX;
  34. } else {
  35. magwarn("type %s invalid", l);
  36. return -1;
  37. @@ -329,7 +335,7 @@
  38. m->mask = ~0L;
  39. EATAB;
  40. - switch (*l) {
  41. + if (m->type!=REGEX) switch (*l) { // regex ignores reln
  42. case '>':
  43. case '<':
  44. /* Old-style anding: "0 byte &0x80 dynamically linked" */
  45. @@ -402,8 +408,8 @@
  46. {
  47. int slen;
  48. - if (m->type == STRING) {
  49. - *p = getstr(*p, m->value.s, sizeof(m->value.s), &slen);
  50. + if (m->type == STRING || m->type == REGEX) {
  51. + *p = getstr(*p, m->value.s, sizeof(m->value.s), &slen, m->type==REGEX);
  52. m->vallen = slen;
  53. } else
  54. if (m->reln != 'x') {
  55. @@ -420,18 +426,25 @@
  56. * Return updated scan pointer as function result.
  57. */
  58. static char *
  59. -getstr(s, p, plen, slen)
  60. +getstr(s, p, plen, slen, regex)
  61. register char *s;
  62. register char *p;
  63. int plen, *slen;
  64. +char regex;
  65. {
  66. char *origs = s, *origp = p;
  67. char *pmax = p + plen - 1;
  68. register int c;
  69. register int val;
  70. + if (regex)
  71. + {
  72. + regex = *s++;
  73. + if (regex==0) goto out;
  74. + }
  75. +
  76. while ((c = *s++) != '\0') {
  77. - if (isspace((unsigned char) c))
  78. + if ((regex && (c==regex)) || (!regex && isspace((unsigned char) c)))
  79. break;
  80. if (p >= pmax) {
  81. fprintf(stderr, "String too long: %s\n", origs);
  82. @@ -519,6 +532,7 @@
  83. } else
  84. *p++ = (char)c;
  85. }
  86. + if (regex) s++;
  87. out:
  88. *p = '\0';
  89. *slen = p - origp;
  90. diff -bBur file-3.20.1/print.c zzz-file/print.c
  91. --- file-3.20.1/print.c Wed Jun 18 23:58:19 1997
  92. +++ zzz-file/print.c Wed Mar 12 17:09:30 1997
  93. @@ -52,7 +52,7 @@
  94. static char *typ[] = { "invalid", "byte", "short", "invalid",
  95. "long", "string", "date", "beshort",
  96. "belong", "bedate", "leshort", "lelong",
  97. - "ledate" };
  98. + "ledate","regex" };
  99. (void) fputc('[', stderr);
  100. (void) fprintf(stderr, ">>>>>>>> %d" + 8 - (m->cont_level & 7),
  101. m->offset);
  102. @@ -84,6 +84,7 @@
  103. case BELONG:
  104. (void) fprintf(stderr, "%ld", m->value.l);
  105. break;
  106. + case REGEX:
  107. case STRING:
  108. showstr(stderr, m->value.s, -1);
  109. break;
  110. diff -bBur file-3.20.1/softmagic.c zzz-file/softmagic.c
  111. --- file-3.20.1/softmagic.c Thu Jun 19 00:36:54 1997
  112. +++ zzz-file/softmagic.c Wed Mar 12 18:45:11 1997
  113. @@ -30,10 +30,11 @@
  114. #include <string.h>
  115. #include <time.h>
  116. #include <sys/types.h>
  117. +#include <regex.h>
  118. #include "file.h"
  119. -#if 0
  120. +#ifndef lint
  121. static char *moduleid =
  122. "@(#)$Id: softmagic.c,v 1.31 1996/06/22 22:04:22 christos Exp $";
  123. #endif /* lint */
  124. @@ -233,12 +234,17 @@
  125. else {
  126. /* nick@feedback.com.ar Only print things before first newline */
  127. char *qqq;
  128. - if ((qqq=strchr(p->s,'\n'))) *qqq='\0';
  129. + if (qqq=strchr(p->s,'\n')) *qqq='\0';
  130. (void) printf(m->desc, p->s);
  131. t = m->offset + strlen(p->s);
  132. }
  133. break;
  134. + case REGEX:
  135. + (void) fputs(m->desc,stdout);
  136. + t = m->offset + 32; /* FIXME! */
  137. + break;
  138. +
  139. case DATE:
  140. case BEDATE:
  141. case LEDATE:
  142. @@ -247,7 +253,7 @@
  143. *rt = '\0';
  144. (void) printf(m->desc, pp);
  145. t = m->offset + sizeof(time_t);
  146. - break;
  147. + return;
  148. default:
  149. error("invalid m->type (%d) in mprint().\n", m->type);
  150. /*NOTREACHED*/
  151. @@ -268,6 +274,7 @@
  152. case SHORT:
  153. case LONG:
  154. case DATE:
  155. + case REGEX:
  156. return 1;
  157. case STRING:
  158. {
  159. @@ -376,6 +383,24 @@
  160. return 1;
  161. }
  162. +static int regex_match(const char *s, const char *pat)
  163. +{
  164. + regex_t reg;
  165. + regmatch_t *rm;
  166. + int err;
  167. +
  168. + err=regcomp(&reg,pat,REG_NEWLINE);
  169. + if (err)
  170. + {
  171. + error("Bad regex pattern: %s\n",pat);
  172. + }
  173. + rm=(regmatch_t*) malloc( sizeof(regmatch_t) * (reg.re_nsub+1) );
  174. + err=regexec(&reg,s,reg.re_nsub+1,rm,0);
  175. + regfree(&reg);
  176. + free(rm);
  177. + return err;
  178. +}
  179. +
  180. static int
  181. mcheck(p, m)
  182. union VALUETYPE* p;
  183. @@ -428,6 +453,10 @@
  184. if ((v = *b++ - *a++) != 0)
  185. break;
  186. }
  187. + break;
  188. + case REGEX:
  189. + l = 0;
  190. + v = regex_match(p->s, m->value.s);
  191. break;
  192. default:
  193. error("invalid type %d in mcheck().\n", m->type);