Skip to content

Commit

Permalink
Add quad indirect offsets
Browse files Browse the repository at this point in the history
  • Loading branch information
zoulasc committed Jun 22, 2018
1 parent a642587 commit cc32246
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
4 changes: 4 additions & 0 deletions ChangeLog
@@ -1,3 +1,7 @@
2018-06-22 16:38 Christos Zoulas <christos@zoulas.com>

* Add Quad indirect offsets

2018-05-24 14:10 Christos Zoulas <christos@zoulas.com>

* Enable parsing of ELF dynamic sections to handle PIE better
Expand Down
23 changes: 19 additions & 4 deletions doc/magic.man
@@ -1,5 +1,6 @@
.\" $File: magic.man,v 1.92 2017/11/04 01:11:32 christos Exp $
.Dd Noveber 3, 2017
.It S2
.\" $File: magic.man,v 1.93 2018/06/22 20:39:49 christos Exp $
.Dd June 22, 2018
.Dt MAGIC __FSECTION__
.Os
.\" install as magic.4 on USG, magic.5 on V7, Berkeley and Linux systems.
Expand Down Expand Up @@ -553,12 +554,12 @@ the file.
The value at that offset is read, and is used again as an offset
in the file.
Indirect offsets are of the form:
.Em (( x [[.,][bislBISL]][+\-][ y ]) .
.Em (( x [[.,][bBcCeEfFgGhHiIlmsSqQ]][+\-][ y ]) .
The value of
.Em x
is used as an offset in the file.
A byte, id3 length, short or long is read at that offset depending on the
.Em [bislBISLm]
.Em [bBcCeEfFgGhHiIlmsSqQ]
type specifier.
The value is treated as signed if
.Dq ,
Expand All @@ -575,6 +576,20 @@ To that number the value of
.Em y
is added and the result is used as an offset in the file.
The default type if one is not specified is long.
The following types are recognized:
.Bl -column -offset indent "Type" "Half/Short" "Little" "Size"
.It Sy Type Sy Mnemonic Sy Endian Sy Size
.It bcBc Byte/Char N/A 1
.It efg Double Little 8
.It EFG Double Big 8
.It hs Half/Short Little 2
.It HS Half/Short Big 2
.It i ID3 Little 4
.It I ID3 Big 4
.It m Middle Middle 4
.It q Quad Little 8
.It Q Quad Big 8
.El
.Pp
That way variable length structures can be examined:
.Bd -literal -offset indent
Expand Down
8 changes: 7 additions & 1 deletion src/apprentice.c
Expand Up @@ -32,7 +32,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: apprentice.c,v 1.271 2018/04/15 19:57:59 christos Exp $")
FILE_RCSID("@(#)$File: apprentice.c,v 1.272 2018/06/22 20:39:50 christos Exp $")
#endif /* lint */

#include "magic.h"
Expand Down Expand Up @@ -1980,6 +1980,12 @@ parse(struct magic_set *ms, struct magic_entry *me, const char *line,
case 'I':
m->in_type = FILE_BEID3;
break;
case 'q':
m->in_type = FILE_LEQUAD;
break;
case 'Q':
m->in_type = FILE_BEQUAD;
break;
default:
if (ms->flags & MAGIC_CHECK)
file_magwarn(ms,
Expand Down
22 changes: 20 additions & 2 deletions src/softmagic.c
Expand Up @@ -32,7 +32,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: softmagic.c,v 1.261 2018/05/24 18:09:17 christos Exp $")
FILE_RCSID("@(#)$File: softmagic.c,v 1.262 2018/06/22 20:39:50 christos Exp $")
#endif /* lint */

#include "magic.h"
Expand Down Expand Up @@ -1535,6 +1535,14 @@ mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
case FILE_MELONG:
off = SEXT(sgn,32,ME32(q));
break;
case FILE_BEQUAD:
off = SEXT(sgn,64,BE64(q));
break;
case FILE_LEQUAD:
off = SEXT(sgn,64,LE64(q));
break;
default:
abort();
}
if ((ms->flags & MAGIC_DEBUG) != 0)
fprintf(stderr, "indirect offs=%jd\n", off);
Expand Down Expand Up @@ -1588,8 +1596,18 @@ mget(struct magic_set *ms, struct magic *m, const struct buffer *b,
return 0;
offset = do_ops(m, SEXT(sgn,32,p->l), off);
break;
default:
case FILE_LEQUAD:
if (OFFSET_OOB(nbytes, offset, 8))
return 0;
offset = do_ops(m, SEXT(sgn,64,LE64(p)), off);
break;
case FILE_BEQUAD:
if (OFFSET_OOB(nbytes, offset, 8))
return 0;
offset = do_ops(m, SEXT(sgn,64,BE64(p)), off);
break;
default:
abort();
}

if (m->flag & INDIROFFADD) {
Expand Down

0 comments on commit cc32246

Please sign in to comment.