1 --- rpm-4.4.1/file/src/apprentice.c.orig 2005-01-26 04:39:56.000000000 +0100
2 +++ rpm-4.4.1/file/src/apprentice.c 2005-07-19 20:52:58.981441608 +0200
7 -FILE_RCSID("@(#)$Id$")
8 +FILE_RCSID("@(#)$Id$")
11 #define EATAB {while (isascii((unsigned char) *l) && \
13 #define MAXPATHLEN 1024
16 -#define IS_STRING(t) ((t) == FILE_STRING || (t) == FILE_PSTRING || \
17 +#define IS_PLAINSTRING(t) ((t) == FILE_STRING || (t) == FILE_PSTRING || \
18 (t) == FILE_BESTRING16 || (t) == FILE_LESTRING16)
21 -private size_t maxmagic = 0;
23 -private size_t magicsize = sizeof(struct magic);
25 +#define IS_STRING(t) (IS_PLAINSTRING(t) || (t) == FILE_REGEX || \
28 private int getvalue(struct magic_set *ms, struct magic *m, char **p)
29 /*@globals fileSystem @*/
31 /*@globals fileSystem @*/
32 /*@modifies fileSystem @*/;
36 +private size_t maxmagic = 0;
38 +private size_t magicsize = sizeof(struct magic);
42 int main(int, char *[]);
52 if (ms->flags & MAGIC_CHECK)
57 + if (m->cont_level != 0 && *l == '&') {
58 + ++l; /* step over */
61 if (m->cont_level != 0 && *l == '(') {
64 + if (m->flag & OFFADD)
65 + m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
67 if (m->cont_level != 0 && *l == '&') {
73 - m->in_op = FILE_OPINVERSE;
74 + m->in_op |= FILE_OPINVERSE;
82 - if (isdigit((unsigned char)*l))
83 - m->in_offset = (uint32_t)strtoul(l, &t, 0);
85 + m->in_op |= FILE_OPINDIRECT;
88 + if (isdigit((unsigned char)*l) || *l == '-')
89 + m->in_offset = (int32_t)strtol(l, &t, 0);
94 + ((m->in_op & FILE_OPINDIRECT) && *t++ != ')'))
95 if (ms->flags & MAGIC_CHECK)
97 "missing ')' in indirect offset");
100 #define NBESTRING16 10
101 #define NLESTRING16 10
107 } else if (strncmp(l, "lestring16", NLESTRING16)==0) {
108 m->type = FILE_LESTRING16;
110 + } else if (strncmp(l, "search", NSEARCH)==0) {
111 + m->type = FILE_SEARCH;
114 if (ms->flags & MAGIC_CHECK)
115 file_magwarn(ms, "type `%s' invalid", l);
116 @@ -650,12 +669,12 @@
117 /* New and improved: ~ & | ^ + - * / % -- exciting, isn't it? */
119 if (!IS_STRING(m->type))
120 - m->mask_op = FILE_OPINVERSE;
121 + m->mask_op |= FILE_OPINVERSE;
124 if ((t = strchr(fops, *l)) != NULL) {
125 uint32_t op = (uint32_t)(t - fops);
126 - if (op != FILE_OPDIVIDE || !IS_STRING(m->type)) {
127 + if (op != FILE_OPDIVIDE || !IS_PLAINSTRING(m->type)) {
130 val = (uint32_t)strtoul(l, &l, 0);
139 @@ -706,15 +726,12 @@
143 - if (!IS_STRING(m->type)) {
153 - if (*l == 'x' && isascii((unsigned char)l[1]) &&
154 - isspace((unsigned char)l[1])) {
155 + if (*l == 'x' && ((isascii((unsigned char)l[1]) &&
156 + isspace((unsigned char)l[1])) || !l[1])) {
159 goto GetDesc; /* Bill The Cat */
165 *p = getstr(ms, *p, m->value.s, sizeof(m->value.s), &slen);
167 if (ms->flags & MAGIC_CHECK)
168 @@ -1259,7 +1277,7 @@
169 m->cont_level = swap2(m->cont_level);
170 m->offset = swap4((uint32_t)m->offset);
171 m->in_offset = swap4((uint32_t)m->in_offset);
172 - if (IS_STRING(m->type))
173 + if (!IS_STRING(m->type))
174 m->value.l = swap4(m->value.l);
175 m->mask = swap4(m->mask);
177 --- rpm-4.4.1/file/src/file.h.orig 2005-01-03 00:11:30.000000000 +0100
178 +++ rpm-4.4.1/file/src/file.h 2005-07-19 19:22:21.264100296 +0200
182 * file.h - definitions for file(1) program
192 -# define HOWMANY 65536 /* how much of the file to look at */
193 +# define HOWMANY (256 * 1024) /* how much of the file to look at */
195 -#define MAXMAGIS 4096 /* max entries in /etc/magic */
196 +#define MAXMAGIS 8192 /* max entries in /etc/magic */
197 #define MAXDESC 64 /* max leng of text description */
198 #define MAXstring 32 /* max leng of "string" types */
201 #define INDIR 1 /* if '>(...)' appears, */
202 #define UNSIGNED 2 /* comparison is unsigned */
203 #define OFFADD 4 /* if '>&' appears, */
204 +#define INDIROFFADD 8 /* if '>&(' appears, */
206 uint8_t reln; /* relation (0=eq, '>'=gt, etc) */
207 uint8_t vallen; /* length of string value, if any */
209 #define FILE_REGEX 17
210 #define FILE_BESTRING16 18
211 #define FILE_LESTRING16 19
212 +#define FILE_SEARCH 20
214 #define FILE_FORMAT_NAME \
215 /* 0 */ "invalid 0", \
222 /* 10 */ "leshort", \
226 /* 16 */ "leldate", \
228 /* 18 */ "bestring16", \
229 -/* 19 */ "lestring16",
230 +/* 19 */ "lestring16", \
233 #define FILE_FMT_NUM "cduxXi"
234 #define FILE_FMT_STR "s"
236 /* 16 */ FILE_FMT_STR, \
237 /* 17 */ FILE_FMT_STR, \
238 /* 18 */ FILE_FMT_STR, \
239 -/* 19 */ FILE_FMT_STR,
240 +/* 19 */ FILE_FMT_STR, \
241 +/* 20 */ FILE_FMT_STR,
244 uint8_t in_op; /* operator for indirection */
245 @@ -172,11 +176,12 @@
246 #define FILE_OPMULTIPLY 5
247 #define FILE_OPDIVIDE 6
248 #define FILE_OPMODULO 7
249 -#define FILE_OPINVERSE 0x80
250 +#define FILE_OPINVERSE 0x40
251 +#define FILE_OPINDIRECT 0x80
253 uint32_t offset; /* offset to magic number */
255 - uint32_t in_offset; /* offset from indirection */
256 + int32_t in_offset; /* offset from indirection */
258 uint32_t mask; /* mask before comparison with value */
260 --- rpm-4.4.1/file/src/softmagic.c.orig 2005-01-26 04:39:56.000000000 +0100
261 +++ rpm-4.4.1/file/src/softmagic.c 2005-07-19 18:50:47.923931752 +0200
266 -FILE_RCSID("@(#)$Id$")
267 +FILE_RCSID("@(#)$Id$")
270 private int match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
272 /*@globals fileSystem @*/
273 /*@modifies ms, magic, fileSystem @*/;
274 private int mget(struct magic_set *ms, union VALUETYPE *p, const unsigned char *s,
275 - struct magic *m, size_t nbytes)
276 + struct magic *m, size_t nbytes, int i)
277 /*@globals fileSystem @*/
278 /*@modifies ms, p, m, fileSystem @*/;
279 private int mcheck(struct magic_set *ms, union VALUETYPE *p, struct magic *m)
280 @@ -128,15 +128,20 @@
282 for (magindex = 0; magindex < nmagic; magindex++) {
283 /* if main entry matches, print it... */
284 - int flush = !mget(ms, &p, s, &magic[magindex], nbytes);
285 - switch (mcheck(ms, &p, &magic[magindex])) {
290 - /*@switchbreak@*/ break;
292 - /*@switchbreak@*/ break;
293 + int flush = !mget(ms, &p, s, &magic[magindex], nbytes,
296 + if (magic[magindex].reln == '!') flush = 0;
298 + switch (mcheck(ms, &p, &magic[magindex])) {
303 + /*@switchbreak@*/ break;
305 + /*@switchbreak@*/ break;
310 @@ -179,14 +184,18 @@
312 cont_level = magic[magindex].cont_level;
314 + oldoff = magic[magindex].offset;
315 if (magic[magindex].flag & OFFADD) {
316 - oldoff=magic[magindex].offset;
317 - magic[magindex].offset += ms->c.off[cont_level-1];
318 + magic[magindex].offset +=
319 + ms->c.off[cont_level-1];
321 - if (!mget(ms, &p, s, &magic[magindex], nbytes))
323 + flush = !mget(ms, &p, s, &magic[magindex], nbytes,
325 + if (flush && magic[magindex].reln != '!')
328 - switch (mcheck(ms, &p, &magic[magindex])) {
329 + switch (flush ? 1 : mcheck(ms, &p, &magic[magindex])) {
337 - if (magic[magindex].flag & OFFADD) {
338 - magic[magindex].offset = oldoff;
340 + magic[magindex].offset = oldoff;
344 @@ -288,10 +295,10 @@
346 case FILE_BESTRING16:
347 case FILE_LESTRING16:
348 - if (m->reln == '=') {
349 + if (m->reln == '=' || m->reln == '!') {
350 if (file_printf(ms, m->desc, m->value.s) == -1)
352 - t = m->offset + strlen(m->value.s);
353 + t = m->offset + m->vallen;
356 if (*m->value.s == '\0') {
359 t = m->offset + strlen(p->s);
362 + if (file_printf(ms, m->desc, m->value.s) == -1)
364 + t = m->offset + m->vallen;
368 file_error(ms, 0, "invalid m->type (%d) in mprint()", m->type);
373 - switch (m->mask_op&0x7F) {
374 + switch (m->mask_op & 0x7F) {
377 /*@innerbreak@*/ break;
382 - switch (m->mask_op&0x7F) {
383 + switch (m->mask_op & 0x7F) {
386 /*@innerbreak@*/ break;
391 - switch (m->mask_op&0x7F) {
392 + switch (m->mask_op & 0x7F) {
395 /*@innerbreak@*/ break;
403 file_error(ms, 0, "invalid type %d in mconvert()", m->type);
404 @@ -625,15 +638,17 @@
405 * offset is interpreted as last line to search,
406 * (starting at 1), not as bytes-from start-of-file
408 - unsigned char *b, *last = NULL;
409 + char *b, *c, *last = NULL;
410 if ((p->buf = strdup((const char *)s)) == NULL) {
414 - for (b = (unsigned char *)p->buf; offset &&
415 - (b = (unsigned char *)strchr((char *)b, '\n')) != NULL;
417 + for (b = p->buf; offset &&
418 + ((b = strchr(c = b, '\n')) || (b = strchr(c, '\r')));
421 + if (b[0] == '\r' && b[1] == '\n') b++;
426 @@ -682,80 +697,77 @@
429 mget(struct magic_set *ms, union VALUETYPE *p, const unsigned char *s,
430 - struct magic *m, size_t nbytes)
431 + struct magic *m, size_t nbytes, int cont_level)
433 uint32_t offset = m->offset;
435 if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes) == -1)
438 - /* Verify we have enough data to match magic type */
441 - if (nbytes < (offset + 1)) /* should alway be true */
448 - if (nbytes < (offset + 2))
461 - if (nbytes < (offset + 4))
467 - if (nbytes < (offset + m->vallen))
472 if ((ms->flags & MAGIC_DEBUG) != 0) {
473 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
477 if (m->flag & INDIR) {
478 + int off = m->in_offset;
479 + if (m->in_op & FILE_OPINDIRECT) {
480 + const union VALUETYPE *q =
481 + ((const union VALUETYPE *)(s + offset + off));
482 + switch (m->in_type) {
490 + off = (short)((q->hs[0]<<8)|(q->hs[1]));
493 + off = (short)((q->hs[1]<<8)|(q->hs[0]));
499 + off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
500 + (q->hl[2]<<8)|(q->hl[3]));
503 + off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
504 + (q->hl[1]<<8)|(q->hl[0]));
508 switch (m->in_type) {
510 - if (m->in_offset) {
511 - switch (m->in_op&0x7F) {
512 + if (nbytes < (offset + 1)) return 0;
514 + switch (m->in_op & 0x3F) {
516 - offset = p->b & m->in_offset;
517 + offset = p->b & off;
518 /*@innerbreak@*/ break;
520 - offset = p->b | m->in_offset;
521 + offset = p->b | off;
522 /*@innerbreak@*/ break;
524 - offset = p->b ^ m->in_offset;
525 + offset = p->b ^ off;
526 /*@innerbreak@*/ break;
528 - offset = p->b + m->in_offset;
529 + offset = p->b + off;
530 /*@innerbreak@*/ break;
532 - offset = p->b - m->in_offset;
533 + offset = p->b - off;
534 /*@innerbreak@*/ break;
535 case FILE_OPMULTIPLY:
536 - offset = p->b * m->in_offset;
537 + offset = p->b * off;
538 /*@innerbreak@*/ break;
540 - offset = p->b / m->in_offset;
541 + offset = p->b / off;
542 /*@innerbreak@*/ break;
544 - offset = p->b % m->in_offset;
545 + offset = p->b % off;
546 /*@innerbreak@*/ break;
549 @@ -764,47 +776,49 @@
553 - if (m->in_offset) {
554 + if (nbytes < (offset + 2))
557 switch (m->in_op & 0x7F) {
559 offset = (short)((p->hs[0]<<8)|
563 /*@innerbreak@*/ break;
565 offset = (short)((p->hs[0]<<8)|
569 /*@innerbreak@*/ break;
571 offset = (short)((p->hs[0]<<8)|
575 /*@innerbreak@*/ break;
577 offset = (short)((p->hs[0]<<8)|
581 /*@innerbreak@*/ break;
583 offset = (short)((p->hs[0]<<8)|
587 /*@innerbreak@*/ break;
588 case FILE_OPMULTIPLY:
589 offset = (short)((p->hs[0]<<8)|
593 /*@innerbreak@*/ break;
595 offset = (short)((p->hs[0]<<8)|
599 /*@innerbreak@*/ break;
601 offset = (short)((p->hs[0]<<8)|
605 /*@innerbreak@*/ break;
608 @@ -814,47 +828,49 @@
612 - if (m->in_offset) {
613 + if (nbytes < (offset + 2))
616 switch (m->in_op & 0x7F) {
618 offset = (short)((p->hs[1]<<8)|
622 /*@innerbreak@*/ break;
624 offset = (short)((p->hs[1]<<8)|
628 /*@innerbreak@*/ break;
630 offset = (short)((p->hs[1]<<8)|
634 /*@innerbreak@*/ break;
636 offset = (short)((p->hs[1]<<8)|
640 /*@innerbreak@*/ break;
642 offset = (short)((p->hs[1]<<8)|
646 /*@innerbreak@*/ break;
647 case FILE_OPMULTIPLY:
648 offset = (short)((p->hs[1]<<8)|
652 /*@innerbreak@*/ break;
654 offset = (short)((p->hs[1]<<8)|
658 /*@innerbreak@*/ break;
660 offset = (short)((p->hs[1]<<8)|
664 /*@innerbreak@*/ break;
667 @@ -864,31 +880,33 @@
671 - if (m->in_offset) {
672 + if (nbytes < (offset + 2))
675 switch (m->in_op & 0x7F) {
677 - offset = p->h & m->in_offset;
678 + offset = p->h & off;
679 /*@innerbreak@*/ break;
681 - offset = p->h | m->in_offset;
682 + offset = p->h | off;
683 /*@innerbreak@*/ break;
685 - offset = p->h ^ m->in_offset;
686 + offset = p->h ^ off;
687 /*@innerbreak@*/ break;
689 - offset = p->h + m->in_offset;
690 + offset = p->h + off;
691 /*@innerbreak@*/ break;
693 - offset = p->h - m->in_offset;
694 + offset = p->h - off;
695 /*@innerbreak@*/ break;
696 case FILE_OPMULTIPLY:
697 - offset = p->h * m->in_offset;
698 + offset = p->h * off;
699 /*@innerbreak@*/ break;
701 - offset = p->h / m->in_offset;
702 + offset = p->h / off;
703 /*@innerbreak@*/ break;
705 - offset = p->h % m->in_offset;
706 + offset = p->h % off;
707 /*@innerbreak@*/ break;
710 @@ -898,63 +916,65 @@
714 - if (m->in_offset) {
715 + if (nbytes < (offset + 4))
718 switch (m->in_op & 0x7F) {
720 offset = (int32_t)((p->hl[0]<<24)|
726 /*@innerbreak@*/ break;
728 offset = (int32_t)((p->hl[0]<<24)|
734 /*@innerbreak@*/ break;
736 offset = (int32_t)((p->hl[0]<<24)|
742 /*@innerbreak@*/ break;
744 offset = (int32_t)((p->hl[0]<<24)|
750 /*@innerbreak@*/ break;
752 offset = (int32_t)((p->hl[0]<<24)|
758 /*@innerbreak@*/ break;
759 case FILE_OPMULTIPLY:
760 offset = (int32_t)((p->hl[0]<<24)|
766 /*@innerbreak@*/ break;
768 offset = (int32_t)((p->hl[0]<<24)|
774 /*@innerbreak@*/ break;
776 offset = (int32_t)((p->hl[0]<<24)|
782 /*@innerbreak@*/ break;
785 @@ -966,63 +986,65 @@
789 - if (m->in_offset) {
790 + if (nbytes < (offset + 4))
793 switch (m->in_op & 0x7F) {
795 offset = (int32_t)((p->hl[3]<<24)|
801 /*@innerbreak@*/ break;
803 offset = (int32_t)((p->hl[3]<<24)|
809 /*@innerbreak@*/ break;
811 offset = (int32_t)((p->hl[3]<<24)|
817 /*@innerbreak@*/ break;
819 offset = (int32_t)((p->hl[3]<<24)|
825 /*@innerbreak@*/ break;
827 offset = (int32_t)((p->hl[3]<<24)|
833 /*@innerbreak@*/ break;
834 case FILE_OPMULTIPLY:
835 offset = (int32_t)((p->hl[3]<<24)|
841 /*@innerbreak@*/ break;
843 offset = (int32_t)((p->hl[3]<<24)|
849 /*@innerbreak@*/ break;
851 offset = (int32_t)((p->hl[3]<<24)|
857 /*@innerbreak@*/ break;
860 @@ -1034,31 +1056,33 @@
864 - if (m->in_offset) {
865 + if (nbytes < (offset + 4))
868 switch (m->in_op & 0x7F) {
870 - offset = p->l & m->in_offset;
871 + offset = p->l & off;
872 /*@innerbreak@*/ break;
874 - offset = p->l | m->in_offset;
875 + offset = p->l | off;
876 /*@innerbreak@*/ break;
878 - offset = p->l ^ m->in_offset;
879 + offset = p->l ^ off;
880 /*@innerbreak@*/ break;
882 - offset = p->l + m->in_offset;
883 + offset = p->l + off;
884 /*@innerbreak@*/ break;
886 - offset = p->l - m->in_offset;
887 + offset = p->l - off;
888 /*@innerbreak@*/ break;
889 case FILE_OPMULTIPLY:
890 - offset = p->l * m->in_offset;
891 + offset = p->l * off;
892 /*@innerbreak@*/ break;
894 - offset = p->l / m->in_offset;
895 + offset = p->l / off;
896 /*@innerbreak@*/ break;
898 - offset = p->l % m->in_offset;
899 + offset = p->l % off;
900 /*@innerbreak@*/ break;
901 /* case TOOMANYSWITCHBLOCKS:
902 * ugh = p->eye % m->strain;
903 @@ -1075,8 +1099,10 @@
907 + if (m->flag & INDIROFFADD) offset += ms->c.off[cont_level-1];
908 if (mcopy(ms, p, m->type, 0, s, offset, nbytes) == -1)
910 + m->offset = offset;
912 if ((ms->flags & MAGIC_DEBUG) != 0) {
913 mdebug(offset, (char *)(void *)p,
914 @@ -1084,8 +1110,53 @@
919 + /* Verify we have enough data to match magic type */
922 + if (nbytes < (offset + 1)) /* should alway be true */
929 + if (nbytes < (offset + 2))
942 + if (nbytes < (offset + 4))
949 + if (nbytes < (offset + m->vallen))
955 + if (m->type == FILE_SEARCH) {
956 + p->buf = malloc(m->mask + m->vallen);
957 + if (p->buf == NULL) {
958 + file_error(ms, errno, "Cannot allocate search buffer");
961 + (void)memcpy(p->buf, s + offset, m->mask + m->vallen);
963 if (!mconvert(ms, p, m))
969 @@ -1179,7 +1250,9 @@
973 - rc = regcomp(&rx, m->value.s, REG_EXTENDED|REG_NOSUB);
974 + rc = regcomp(&rx, m->value.s,
975 + REG_EXTENDED|REG_NOSUB|REG_NEWLINE|
976 + ((m->mask & STRING_IGNORE_LOWERCASE) ? REG_ICASE : 0));
979 regerror(rc, &rx, errmsg, sizeof(errmsg));
980 @@ -1194,6 +1267,31 @@
987 + * search for a string in a certain range
989 + unsigned char *a = (unsigned char*)m->value.s;
990 + unsigned char *b = (unsigned char*)p->buf;
991 + int len = m->vallen;
995 + while (++range <= m->mask) {
996 + while (len-- > 0 && (v = *b++ - *a++) == 0)
999 + m->offset += range-1;
1003 + a = (unsigned char*)m->value.s;
1004 + b = (unsigned char*)p->buf + range;
1010 file_error(ms, 0, "invalid type %d in mcheck()", m->type);