]> git.pld-linux.org Git - packages/rpm.git/blob - rpm-file-update.patch
- updated for 4.4.2
[packages/rpm.git] / rpm-file-update.patch
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
3 @@ -45,7 +45,7 @@
4  #endif
5  
6  #ifndef        lint
7 -FILE_RCSID("@(#)$Id$")
8 +FILE_RCSID("@(#)$Id$")
9  #endif /* lint */
10  
11  #define        EATAB {while (isascii((unsigned char) *l) && \
12 @@ -74,13 +74,11 @@
13  #define MAXPATHLEN     1024
14  #endif
15  
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)
19 -
20 -/*@unchecked@*/
21 -private size_t maxmagic = 0;
22 -/*@unchecked@*/
23 -private size_t magicsize = sizeof(struct magic);
24 +    
25 +#define IS_STRING(t) (IS_PLAINSTRING(t) || (t) == FILE_REGEX || \
26 +    (t) == FILE_SEARCH)
27  
28  private int getvalue(struct magic_set *ms, struct magic *m, char **p)
29         /*@globals fileSystem @*/
30 @@ -124,6 +122,12 @@
31         /*@globals fileSystem @*/
32         /*@modifies fileSystem @*/;
33  
34 +
35 +/*@unchecked@*/
36 +private size_t maxmagic = 0;
37 +/*@unchecked@*/
38 +private size_t magicsize = sizeof(struct magic);
39 +
40  #ifdef COMPILE_ONLY
41  
42  int main(int, char *[]);
43 @@ -400,8 +404,8 @@
44                 case FILE_PSTRING:
45                 case FILE_BESTRING16:
46                 case FILE_LESTRING16:
47 -                       break;
48                 case FILE_REGEX:
49 +               case FILE_SEARCH:
50                         break;
51                 default:
52                         if (ms->flags & MAGIC_CHECK)
53 @@ -451,9 +455,15 @@
54                 m->cont_level++; 
55         }
56  
57 +       if (m->cont_level != 0 && *l == '&') {
58 +                ++l;            /* step over */
59 +                m->flag |= OFFADD;
60 +        }
61         if (m->cont_level != 0 && *l == '(') {
62                 ++l;            /* step over */
63                 m->flag |= INDIR;
64 +               if (m->flag & OFFADD)
65 +                       m->flag = (m->flag & ~OFFADD) | INDIROFFADD;
66         }
67         if (m->cont_level != 0 && *l == '&') {
68                  ++l;            /* step over */
69 @@ -506,7 +516,7 @@
70                         l++;
71                 }
72                 if (*l == '~') {
73 -                       m->in_op = FILE_OPINVERSE;
74 +                       m->in_op |= FILE_OPINVERSE;
75                         l++;
76                 }
77                 switch (*l) {
78 @@ -543,11 +553,16 @@
79                         l++;
80                         break;
81                 }
82 -               if (isdigit((unsigned char)*l)) 
83 -                       m->in_offset = (uint32_t)strtoul(l, &t, 0);
84 +               if (*l == '(') {
85 +                       m->in_op |= FILE_OPINDIRECT;
86 +                       l++;
87 +               }
88 +               if (isdigit((unsigned char)*l) || *l == '-') 
89 +                       m->in_offset = (int32_t)strtol(l, &t, 0);
90                 else
91                         t = l;
92 -               if (*t++ != ')') 
93 +               if (*t++ != ')' ||
94 +                   ((m->in_op & FILE_OPINDIRECT) && *t++ != ')')) 
95                         if (ms->flags & MAGIC_CHECK)
96                                 file_magwarn(ms,
97                                     "missing ')' in indirect offset");
98 @@ -577,6 +592,7 @@
99  #define NREGEX         5
100  #define NBESTRING16    10
101  #define NLESTRING16    10
102 +#define NSEARCH                6
103  
104         if (*l == 'u') {
105                 ++l;
106 @@ -641,6 +657,9 @@
107         } else if (strncmp(l, "lestring16", NLESTRING16)==0) {
108                 m->type = FILE_LESTRING16;
109                 l += NLESTRING16;
110 +       } else if (strncmp(l, "search", NSEARCH)==0) {
111 +               m->type = FILE_SEARCH;
112 +               l += NSEARCH;
113         } else {
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? */
118         if (*l == '~') {
119                 if (!IS_STRING(m->type))
120 -                       m->mask_op = FILE_OPINVERSE;
121 +                       m->mask_op |= FILE_OPINVERSE;
122                 ++l;
123         }
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)) {
128                         ++l;
129                         m->mask_op |= op;
130                         val = (uint32_t)strtoul(l, &l, 0);
131 @@ -683,6 +702,7 @@
132                                         return -1;
133                                 }
134                         }
135 +                       ++l;
136                 }
137         }
138         /*
139 @@ -706,15 +726,12 @@
140                 }
141                 break;
142         case '!':
143 -               if (!IS_STRING(m->type)) {
144 -                       m->reln = *l;
145 -                       ++l;
146 -                       break;
147 -               }
148 -               /*@fallthrough@*/
149 +               m->reln = *l;
150 +               ++l;
151 +               break;
152         default:
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])) {
157                         m->reln = *l;
158                         ++l;
159                         goto GetDesc;   /* Bill The Cat */
160 @@ -833,6 +850,7 @@
161         case FILE_STRING:
162         case FILE_PSTRING:
163         case FILE_REGEX:
164 +       case FILE_SEARCH:
165                 *p = getstr(ms, *p, m->value.s, sizeof(m->value.s), &slen);
166                 if (*p == NULL) {
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);
176  }
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
179 @@ -27,7 +27,7 @@
180   */
181  /*
182   * file.h - definitions for file(1) program
183 - * @(#)$Id$
184 + * @(#)$Id$
185   */
186  
187  #ifndef __file_h__
188 @@ -65,9 +65,9 @@
189  #define public
190  
191  #ifndef HOWMANY
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 */
194  #endif
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 */
199  
200 @@ -87,6 +87,7 @@
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,  */
205         /* Word 2 */
206         uint8_t reln;           /* relation (0=eq, '>'=gt, etc) */
207         uint8_t vallen;         /* length of string value, if any */
208 @@ -110,6 +111,7 @@
209  #define                                FILE_REGEX      17
210  #define                                FILE_BESTRING16 18
211  #define                                FILE_LESTRING16 19
212 +#define                                FILE_SEARCH     20
213  
214  #define                                FILE_FORMAT_NAME        \
215  /* 0 */                        "invalid 0",            \
216 @@ -121,7 +123,7 @@
217  /* 6 */                        "date",                 \
218  /* 7 */                        "beshort",              \
219  /* 8 */                        "belong",               \
220 -/* 9 */                        "bedate"                \
221 +/* 9 */                        "bedate",               \
222  /* 10 */                       "leshort",              \
223  /* 11 */                       "lelong",               \
224  /* 12 */                       "ledate",               \
225 @@ -131,7 +133,8 @@
226  /* 16 */                       "leldate",              \
227  /* 17 */                       "regex",                \
228  /* 18 */                       "bestring16",           \
229 -/* 19 */                       "lestring16",
230 +/* 19 */                       "lestring16",           \
231 +/* 20 */                       "search",
232  
233  #define        FILE_FMT_NUM    "cduxXi"
234  #define FILE_FMT_STR   "s"     
235 @@ -156,7 +159,8 @@
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,
242  
243         /* Word 3 */
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
252         /* Word 4 */
253         uint32_t offset;        /* offset to magic number */
254         /* Word 5 */
255 -       uint32_t in_offset;     /* offset from indirection */
256 +       int32_t in_offset;      /* offset from indirection */
257         /* Word 6 */
258         uint32_t mask;  /* mask before comparison with value */
259         /* Word 7 */
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
262 @@ -39,7 +39,7 @@
263  
264  
265  #ifndef        lint
266 -FILE_RCSID("@(#)$Id$")
267 +FILE_RCSID("@(#)$Id$")
268  #endif /* lint */
269  
270  private int match(struct magic_set *ms, struct magic *magic, uint32_t nmagic,
271 @@ -47,7 +47,7 @@
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 @@
281  
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])) {
286 -               case -1:
287 -                       return -1;
288 -               case 0:
289 -                       flush++;
290 -                       /*@switchbreak@*/ break;
291 -               default:
292 -                       /*@switchbreak@*/ break;
293 +               int flush = !mget(ms, &p, s, &magic[magindex], nbytes,
294 +                   cont_level);
295 +               if (flush) {
296 +                       if (magic[magindex].reln == '!') flush = 0;
297 +               } else {        
298 +                       switch (mcheck(ms, &p, &magic[magindex])) {
299 +                       case -1:
300 +                               return -1;
301 +                       case 0:
302 +                               flush++;
303 +                               /*@switchbreak@*/ break;
304 +                       default:
305 +                               /*@switchbreak@*/ break;
306 +                       }
307                 }
308                 if (flush) {
309                         /* 
310 @@ -179,14 +184,18 @@
311                                  */
312                                 cont_level = magic[magindex].cont_level;
313                         }
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];
320                         }
321 -                       if (!mget(ms, &p, s, &magic[magindex], nbytes))
322 +
323 +                       flush = !mget(ms, &p, s, &magic[magindex], nbytes,
324 +                           cont_level);
325 +                       if (flush && magic[magindex].reln != '!')
326                                 goto done;
327                                 
328 -                       switch (mcheck(ms, &p, &magic[magindex])) {
329 +                       switch (flush ? 1 : mcheck(ms, &p, &magic[magindex])) {
330                         case -1:
331                                 return -1;
332                         case 0:
333 @@ -222,9 +231,7 @@
334                                         return -1;
335                         }
336  done:
337 -                       if (magic[magindex].flag & OFFADD) {
338 -                                magic[magindex].offset = oldoff;
339 -                       }
340 +                       magic[magindex].offset = oldoff;
341                 }
342                 firstline = 0;
343                 returnval = 1;
344 @@ -288,10 +295,10 @@
345         case FILE_PSTRING:
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)
351                                 return -1;
352 -                       t = m->offset + strlen(m->value.s);
353 +                       t = m->offset + m->vallen;
354                 }
355                 else {
356                         if (*m->value.s == '\0') {
357 @@ -325,6 +332,11 @@
358                         return -1;
359                 t = m->offset + strlen(p->s);
360                 break;
361 +       case FILE_SEARCH:
362 +               if (file_printf(ms, m->desc, m->value.s) == -1)
363 +                       return -1;
364 +               t = m->offset + m->vallen;
365 +               break;
366  
367         default:
368                 file_error(ms, 0, "invalid m->type (%d) in mprint()", m->type);
369 @@ -344,7 +356,7 @@
370         switch (m->type) {
371         case FILE_BYTE:
372                 if (m->mask)
373 -                       switch (m->mask_op&0x7F) {
374 +                       switch (m->mask_op & 0x7F) {
375                         case FILE_OPAND:
376                                 p->b &= m->mask;
377                                 /*@innerbreak@*/ break;
378 @@ -375,7 +387,7 @@
379                 return 1;
380         case FILE_SHORT:
381                 if (m->mask)
382 -                       switch (m->mask_op&0x7F) {
383 +                       switch (m->mask_op & 0x7F) {
384                         case FILE_OPAND:
385                                 p->h &= m->mask;
386                                 /*@innerbreak@*/ break;
387 @@ -408,7 +420,7 @@
388         case FILE_DATE:
389         case FILE_LDATE:
390                 if (m->mask)
391 -                       switch (m->mask_op&0x7F) {
392 +                       switch (m->mask_op & 0x7F) {
393                         case FILE_OPAND:
394                                 p->l &= m->mask;
395                                 /*@innerbreak@*/ break;
396 @@ -599,6 +611,7 @@
397                         p->l = ~p->l;
398                 return 1;
399         case FILE_REGEX:
400 +       case FILE_SEARCH:
401                 return 1;
402         default:
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
407                  */
408 -               unsigned char *b, *last = NULL;
409 +               char *b, *c, *last = NULL;
410                 if ((p->buf = strdup((const char *)s)) == NULL) {
411                         file_oomem(ms);
412                         return -1;
413                 }
414 -               for (b = (unsigned char *)p->buf; offset &&
415 -                   (b = (unsigned char *)strchr((char *)b, '\n')) != NULL;
416 -                   offset--, s++)
417 +               for (b = p->buf; offset && 
418 +                   ((b = strchr(c = b, '\n')) || (b = strchr(c, '\r')));
419 +                   offset--, b++) {
420                         last = b;
421 +                       if (b[0] == '\r' && b[1] == '\n') b++;
422 +               }
423                 if (last != NULL)
424                         *last = '\0';
425                 return 0;
426 @@ -682,80 +697,77 @@
427  
428  private int
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)
432  {
433         uint32_t offset = m->offset;
434  
435         if (mcopy(ms, p, m->type, m->flag & INDIR, s, offset, nbytes) == -1)
436                 return -1;
437  
438 -       /* Verify we have enough data to match magic type */
439 -       switch (m->type) {
440 -               case FILE_BYTE:
441 -                       if (nbytes < (offset + 1)) /* should alway be true */
442 -                               return 0;
443 -                       break;
444 -
445 -               case FILE_SHORT:
446 -               case FILE_BESHORT:
447 -               case FILE_LESHORT:
448 -                       if (nbytes < (offset + 2))
449 -                               return 0;
450 -                       break;
451 -
452 -               case FILE_LONG:
453 -               case FILE_BELONG:
454 -               case FILE_LELONG:
455 -               case FILE_DATE:
456 -               case FILE_BEDATE:
457 -               case FILE_LEDATE:
458 -               case FILE_LDATE:
459 -               case FILE_BELDATE:
460 -               case FILE_LELDATE:
461 -                       if (nbytes < (offset + 4))
462 -                               return 0;
463 -                       break;
464 -
465 -               case FILE_STRING:
466 -               case FILE_PSTRING:
467 -                       if (nbytes < (offset + m->vallen))
468 -                               return 0;
469 -                       break;
470 -       }
471 -
472         if ((ms->flags & MAGIC_DEBUG) != 0) {
473                 mdebug(offset, (char *)(void *)p, sizeof(union VALUETYPE));
474                 file_mdump(m);
475         }
476  
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) {
483 +                       case FILE_BYTE:
484 +                               off = q->b;
485 +                               break;
486 +                       case FILE_SHORT:
487 +                               off = q->h;
488 +                               break;
489 +                       case FILE_BESHORT:
490 +                               off = (short)((q->hs[0]<<8)|(q->hs[1]));
491 +                               break;
492 +                       case FILE_LESHORT:
493 +                               off = (short)((q->hs[1]<<8)|(q->hs[0]));
494 +                               break;
495 +                       case FILE_LONG:
496 +                               off = q->l;
497 +                               break;
498 +                       case FILE_BELONG:
499 +                               off = (int32_t)((q->hl[0]<<24)|(q->hl[1]<<16)|
500 +                                                (q->hl[2]<<8)|(q->hl[3]));
501 +                               break;
502 +                       case FILE_LELONG:
503 +                               off = (int32_t)((q->hl[3]<<24)|(q->hl[2]<<16)|
504 +                                                (q->hl[1]<<8)|(q->hl[0]));
505 +                               break;
506 +                       }
507 +               }
508                 switch (m->in_type) {
509                 case FILE_BYTE:
510 -                       if (m->in_offset) {
511 -                               switch (m->in_op&0x7F) {
512 +                       if (nbytes < (offset + 1)) return 0;
513 +                       if (off) {
514 +                               switch (m->in_op & 0x3F) {
515                                 case FILE_OPAND:
516 -                                       offset = p->b & m->in_offset;
517 +                                       offset = p->b & off;
518                                         /*@innerbreak@*/ break;
519                                 case FILE_OPOR:
520 -                                       offset = p->b | m->in_offset;
521 +                                       offset = p->b | off;
522                                         /*@innerbreak@*/ break;
523                                 case FILE_OPXOR:
524 -                                       offset = p->b ^ m->in_offset;
525 +                                       offset = p->b ^ off;
526                                         /*@innerbreak@*/ break;
527                                 case FILE_OPADD:
528 -                                       offset = p->b + m->in_offset;
529 +                                       offset = p->b + off;
530                                         /*@innerbreak@*/ break;
531                                 case FILE_OPMINUS:
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;
539                                 case FILE_OPDIVIDE:
540 -                                       offset = p->b / m->in_offset;
541 +                                       offset = p->b / off;
542                                         /*@innerbreak@*/ break;
543                                 case FILE_OPMODULO:
544 -                                       offset = p->b % m->in_offset;
545 +                                       offset = p->b % off;
546                                         /*@innerbreak@*/ break;
547                                 }
548                         } else
549 @@ -764,47 +776,49 @@
550                                 offset = ~offset;
551                         break;
552                 case FILE_BESHORT:
553 -                       if (m->in_offset) {
554 +                       if (nbytes < (offset + 2))
555 +                               return 0;
556 +                       if (off) {
557                                 switch (m->in_op & 0x7F) {
558                                 case FILE_OPAND:
559                                         offset = (short)((p->hs[0]<<8)|
560                                                          (p->hs[1])) &
561 -                                                m->in_offset;
562 +                                                off;
563                                         /*@innerbreak@*/ break;
564                                 case FILE_OPOR:
565                                         offset = (short)((p->hs[0]<<8)|
566                                                          (p->hs[1])) |
567 -                                                m->in_offset;
568 +                                                off;
569                                         /*@innerbreak@*/ break;
570                                 case FILE_OPXOR:
571                                         offset = (short)((p->hs[0]<<8)|
572                                                          (p->hs[1])) ^
573 -                                                m->in_offset;
574 +                                                off;
575                                         /*@innerbreak@*/ break;
576                                 case FILE_OPADD:
577                                         offset = (short)((p->hs[0]<<8)|
578                                                          (p->hs[1])) +
579 -                                                m->in_offset;
580 +                                                off;
581                                         /*@innerbreak@*/ break;
582                                 case FILE_OPMINUS:
583                                         offset = (short)((p->hs[0]<<8)|
584                                                          (p->hs[1])) -
585 -                                                m->in_offset;
586 +                                                off;
587                                         /*@innerbreak@*/ break;
588                                 case FILE_OPMULTIPLY:
589                                         offset = (short)((p->hs[0]<<8)|
590                                                          (p->hs[1])) *
591 -                                                m->in_offset;
592 +                                                off;
593                                         /*@innerbreak@*/ break;
594                                 case FILE_OPDIVIDE:
595                                         offset = (short)((p->hs[0]<<8)|
596                                                          (p->hs[1])) /
597 -                                                m->in_offset;
598 +                                                off;
599                                         /*@innerbreak@*/ break;
600                                 case FILE_OPMODULO:
601                                         offset = (short)((p->hs[0]<<8)|
602                                                          (p->hs[1])) %
603 -                                                m->in_offset;
604 +                                                off;
605                                         /*@innerbreak@*/ break;
606                                 }
607                         } else
608 @@ -814,47 +828,49 @@
609                                 offset = ~offset;
610                         break;
611                 case FILE_LESHORT:
612 -                       if (m->in_offset) {
613 +                       if (nbytes < (offset + 2))
614 +                               return 0;
615 +                       if (off) {
616                                 switch (m->in_op & 0x7F) {
617                                 case FILE_OPAND:
618                                         offset = (short)((p->hs[1]<<8)|
619                                                          (p->hs[0])) &
620 -                                                m->in_offset;
621 +                                                off;
622                                         /*@innerbreak@*/ break;
623                                 case FILE_OPOR:
624                                         offset = (short)((p->hs[1]<<8)|
625                                                          (p->hs[0])) |
626 -                                                m->in_offset;
627 +                                                off;
628                                         /*@innerbreak@*/ break;
629                                 case FILE_OPXOR:
630                                         offset = (short)((p->hs[1]<<8)|
631                                                          (p->hs[0])) ^
632 -                                                m->in_offset;
633 +                                                off;
634                                         /*@innerbreak@*/ break;
635                                 case FILE_OPADD:
636                                         offset = (short)((p->hs[1]<<8)|
637                                                          (p->hs[0])) +
638 -                                                m->in_offset;
639 +                                                off;
640                                         /*@innerbreak@*/ break;
641                                 case FILE_OPMINUS:
642                                         offset = (short)((p->hs[1]<<8)|
643                                                          (p->hs[0])) -
644 -                                                m->in_offset;
645 +                                                off;
646                                         /*@innerbreak@*/ break;
647                                 case FILE_OPMULTIPLY:
648                                         offset = (short)((p->hs[1]<<8)|
649                                                          (p->hs[0])) *
650 -                                                m->in_offset;
651 +                                                off;
652                                         /*@innerbreak@*/ break;
653                                 case FILE_OPDIVIDE:
654                                         offset = (short)((p->hs[1]<<8)|
655                                                          (p->hs[0])) /
656 -                                                m->in_offset;
657 +                                                off;
658                                         /*@innerbreak@*/ break;
659                                 case FILE_OPMODULO:
660                                         offset = (short)((p->hs[1]<<8)|
661                                                          (p->hs[0])) %
662 -                                                m->in_offset;
663 +                                                off;
664                                         /*@innerbreak@*/ break;
665                                 }
666                         } else
667 @@ -864,31 +880,33 @@
668                                 offset = ~offset;
669                         break;
670                 case FILE_SHORT:
671 -                       if (m->in_offset) {
672 +                       if (nbytes < (offset + 2))
673 +                               return 0;
674 +                       if (off) {
675                                 switch (m->in_op & 0x7F) {
676                                 case FILE_OPAND:
677 -                                       offset = p->h & m->in_offset;
678 +                                       offset = p->h & off;
679                                         /*@innerbreak@*/ break;
680                                 case FILE_OPOR:
681 -                                       offset = p->h | m->in_offset;
682 +                                       offset = p->h | off;
683                                         /*@innerbreak@*/ break;
684                                 case FILE_OPXOR:
685 -                                       offset = p->h ^ m->in_offset;
686 +                                       offset = p->h ^ off;
687                                         /*@innerbreak@*/ break;
688                                 case FILE_OPADD:
689 -                                       offset = p->h + m->in_offset;
690 +                                       offset = p->h + off;
691                                         /*@innerbreak@*/ break;
692                                 case FILE_OPMINUS:
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;
700                                 case FILE_OPDIVIDE:
701 -                                       offset = p->h / m->in_offset;
702 +                                       offset = p->h / off;
703                                         /*@innerbreak@*/ break;
704                                 case FILE_OPMODULO:
705 -                                       offset = p->h % m->in_offset;
706 +                                       offset = p->h % off;
707                                         /*@innerbreak@*/ break;
708                                 }
709                         }
710 @@ -898,63 +916,65 @@
711                                 offset = ~offset;
712                         break;
713                 case FILE_BELONG:
714 -                       if (m->in_offset) {
715 +                       if (nbytes < (offset + 4))
716 +                               return 0;
717 +                       if (off) {
718                                 switch (m->in_op & 0x7F) {
719                                 case FILE_OPAND:
720                                         offset = (int32_t)((p->hl[0]<<24)|
721                                                          (p->hl[1]<<16)|
722                                                          (p->hl[2]<<8)|
723                                                          (p->hl[3])) &
724 -                                                m->in_offset;
725 +                                                off;
726                                         /*@innerbreak@*/ break;
727                                 case FILE_OPOR:
728                                         offset = (int32_t)((p->hl[0]<<24)|
729                                                          (p->hl[1]<<16)|
730                                                          (p->hl[2]<<8)|
731                                                          (p->hl[3])) |
732 -                                                m->in_offset;
733 +                                                off;
734                                         /*@innerbreak@*/ break;
735                                 case FILE_OPXOR:
736                                         offset = (int32_t)((p->hl[0]<<24)|
737                                                          (p->hl[1]<<16)|
738                                                          (p->hl[2]<<8)|
739                                                          (p->hl[3])) ^
740 -                                                m->in_offset;
741 +                                                off;
742                                         /*@innerbreak@*/ break;
743                                 case FILE_OPADD:
744                                         offset = (int32_t)((p->hl[0]<<24)|
745                                                          (p->hl[1]<<16)|
746                                                          (p->hl[2]<<8)|
747                                                          (p->hl[3])) +
748 -                                                m->in_offset;
749 +                                                off;
750                                         /*@innerbreak@*/ break;
751                                 case FILE_OPMINUS:
752                                         offset = (int32_t)((p->hl[0]<<24)|
753                                                          (p->hl[1]<<16)|
754                                                          (p->hl[2]<<8)|
755                                                          (p->hl[3])) -
756 -                                                m->in_offset;
757 +                                                off;
758                                         /*@innerbreak@*/ break;
759                                 case FILE_OPMULTIPLY:
760                                         offset = (int32_t)((p->hl[0]<<24)|
761                                                          (p->hl[1]<<16)|
762                                                          (p->hl[2]<<8)|
763                                                          (p->hl[3])) *
764 -                                                m->in_offset;
765 +                                                off;
766                                         /*@innerbreak@*/ break;
767                                 case FILE_OPDIVIDE:
768                                         offset = (int32_t)((p->hl[0]<<24)|
769                                                          (p->hl[1]<<16)|
770                                                          (p->hl[2]<<8)|
771                                                          (p->hl[3])) /
772 -                                                m->in_offset;
773 +                                                off;
774                                         /*@innerbreak@*/ break;
775                                 case FILE_OPMODULO:
776                                         offset = (int32_t)((p->hl[0]<<24)|
777                                                          (p->hl[1]<<16)|
778                                                          (p->hl[2]<<8)|
779                                                          (p->hl[3])) %
780 -                                                m->in_offset;
781 +                                                off;
782                                         /*@innerbreak@*/ break;
783                                 }
784                         } else
785 @@ -966,63 +986,65 @@
786                                 offset = ~offset;
787                         break;
788                 case FILE_LELONG:
789 -                       if (m->in_offset) {
790 +                       if (nbytes < (offset + 4))
791 +                               return 0;
792 +                       if (off) {
793                                 switch (m->in_op & 0x7F) {
794                                 case FILE_OPAND:
795                                         offset = (int32_t)((p->hl[3]<<24)|
796                                                          (p->hl[2]<<16)|
797                                                          (p->hl[1]<<8)|
798                                                          (p->hl[0])) &
799 -                                                m->in_offset;
800 +                                                off;
801                                         /*@innerbreak@*/ break;
802                                 case FILE_OPOR:
803                                         offset = (int32_t)((p->hl[3]<<24)|
804                                                          (p->hl[2]<<16)|
805                                                          (p->hl[1]<<8)|
806                                                          (p->hl[0])) |
807 -                                                m->in_offset;
808 +                                                off;
809                                         /*@innerbreak@*/ break;
810                                 case FILE_OPXOR:
811                                         offset = (int32_t)((p->hl[3]<<24)|
812                                                          (p->hl[2]<<16)|
813                                                          (p->hl[1]<<8)|
814                                                          (p->hl[0])) ^
815 -                                                m->in_offset;
816 +                                                off;
817                                         /*@innerbreak@*/ break;
818                                 case FILE_OPADD:
819                                         offset = (int32_t)((p->hl[3]<<24)|
820                                                          (p->hl[2]<<16)|
821                                                          (p->hl[1]<<8)|
822                                                          (p->hl[0])) +
823 -                                                m->in_offset;
824 +                                                off;
825                                         /*@innerbreak@*/ break;
826                                 case FILE_OPMINUS:
827                                         offset = (int32_t)((p->hl[3]<<24)|
828                                                          (p->hl[2]<<16)|
829                                                          (p->hl[1]<<8)|
830                                                          (p->hl[0])) -
831 -                                                m->in_offset;
832 +                                                off;
833                                         /*@innerbreak@*/ break;
834                                 case FILE_OPMULTIPLY:
835                                         offset = (int32_t)((p->hl[3]<<24)|
836                                                          (p->hl[2]<<16)|
837                                                          (p->hl[1]<<8)|
838                                                          (p->hl[0])) *
839 -                                                m->in_offset;
840 +                                                off;
841                                         /*@innerbreak@*/ break;
842                                 case FILE_OPDIVIDE:
843                                         offset = (int32_t)((p->hl[3]<<24)|
844                                                          (p->hl[2]<<16)|
845                                                          (p->hl[1]<<8)|
846                                                          (p->hl[0])) /
847 -                                                m->in_offset;
848 +                                                off;
849                                         /*@innerbreak@*/ break;
850                                 case FILE_OPMODULO:
851                                         offset = (int32_t)((p->hl[3]<<24)|
852                                                          (p->hl[2]<<16)|
853                                                          (p->hl[1]<<8)|
854                                                          (p->hl[0])) %
855 -                                                m->in_offset;
856 +                                                off;
857                                         /*@innerbreak@*/ break;
858                                 }
859                         } else
860 @@ -1034,31 +1056,33 @@
861                                 offset = ~offset;
862                         break;
863                 case FILE_LONG:
864 -                       if (m->in_offset) {
865 +                       if (nbytes < (offset + 4))
866 +                               return 0;
867 +                       if (off) {
868                                 switch (m->in_op & 0x7F) {
869                                 case FILE_OPAND:
870 -                                       offset = p->l & m->in_offset;
871 +                                       offset = p->l & off;
872                                         /*@innerbreak@*/ break;
873                                 case FILE_OPOR:
874 -                                       offset = p->l | m->in_offset;
875 +                                       offset = p->l | off;
876                                         /*@innerbreak@*/ break;
877                                 case FILE_OPXOR:
878 -                                       offset = p->l ^ m->in_offset;
879 +                                       offset = p->l ^ off;
880                                         /*@innerbreak@*/ break;
881                                 case FILE_OPADD:
882 -                                       offset = p->l + m->in_offset;
883 +                                       offset = p->l + off;
884                                         /*@innerbreak@*/ break;
885                                 case FILE_OPMINUS:
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;
893                                 case FILE_OPDIVIDE:
894 -                                       offset = p->l / m->in_offset;
895 +                                       offset = p->l / off;
896                                         /*@innerbreak@*/ break;
897                                 case FILE_OPMODULO:
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 @@
904                         break;
905                 }
906  
907 +               if (m->flag & INDIROFFADD) offset += ms->c.off[cont_level-1];
908                 if (mcopy(ms, p, m->type, 0, s, offset, nbytes) == -1)
909                         return -1;
910 +               m->offset = offset;
911  
912                 if ((ms->flags & MAGIC_DEBUG) != 0) {
913                         mdebug(offset, (char *)(void *)p,
914 @@ -1084,8 +1110,53 @@
915                         file_mdump(m);
916                 }
917         }
918 +
919 +       /* Verify we have enough data to match magic type */
920 +       switch (m->type) {
921 +               case FILE_BYTE:
922 +                       if (nbytes < (offset + 1)) /* should alway be true */
923 +                               return 0;
924 +                       break;
925 +
926 +               case FILE_SHORT:
927 +               case FILE_BESHORT:
928 +               case FILE_LESHORT:
929 +                       if (nbytes < (offset + 2))
930 +                               return 0;
931 +                       break;
932 +
933 +               case FILE_LONG:
934 +               case FILE_BELONG:
935 +               case FILE_LELONG:
936 +               case FILE_DATE:
937 +               case FILE_BEDATE:
938 +               case FILE_LEDATE:
939 +               case FILE_LDATE:
940 +               case FILE_BELDATE:
941 +               case FILE_LELDATE:
942 +                       if (nbytes < (offset + 4))
943 +                               return 0;
944 +                       break;
945 +
946 +               case FILE_STRING:
947 +               case FILE_PSTRING:
948 +               case FILE_SEARCH:
949 +                       if (nbytes < (offset + m->vallen))
950 +                               return 0;
951 +                       break;
952 +               default: break;
953 +       }
954 +
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");
959 +                       return 0;
960 +               }
961 +               (void)memcpy(p->buf, s + offset, m->mask + m->vallen);
962 +       }
963         if (!mconvert(ms, p, m))
964 -         return 0;
965 +               return 0;
966         return 1;
967  }
968  
969 @@ -1179,7 +1250,9 @@
970                 regex_t rx;
971                 char errmsg[512];
972  
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));
977                 if (rc) {
978                         free(p->buf);
979                         regerror(rc, &rx, errmsg, sizeof(errmsg));
980 @@ -1194,6 +1267,31 @@
981                         return !rc;
982                 }
983         }
984 +       case FILE_SEARCH:
985 +       {
986 +               /*
987 +                * search for a string in a certain range
988 +                */
989 +               unsigned char *a = (unsigned char*)m->value.s;
990 +               unsigned char *b = (unsigned char*)p->buf;
991 +               int len = m->vallen;
992 +               int range = 0;
993 +               l = 0;
994 +               v = 0;
995 +               while (++range <= m->mask) {
996 +                       while (len-- > 0 && (v = *b++ - *a++) == 0)
997 +                               continue;
998 +                       if (!v) {
999 +                               m->offset += range-1;
1000 +                               break;
1001 +                       }
1002 +                       len = m->vallen;
1003 +                       a = (unsigned char*)m->value.s;
1004 +                       b = (unsigned char*)p->buf + range;
1005 +               }
1006 +               free(p->buf);
1007 +               break;
1008 +       }
1009         default:
1010                 file_error(ms, 0, "invalid type %d in mcheck()", m->type);
1011                 return -1;
This page took 0.126849 seconds and 3 git commands to generate.