Skip to content

Commit

Permalink
Improve TzIf format detection (#237)
Browse files Browse the repository at this point in the history
Additional header checks:
- Header length
- Version
- typecnt MUST not be zero
  • Loading branch information
n-vr authored Feb 2, 2022
1 parent eb817bf commit 98e3221
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions internal/magic/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ var (
Elf = prefix([]byte{0x7F, 0x45, 0x4C, 0x46})
// Nes matches a Nintendo Entertainment system ROM file.
Nes = prefix([]byte{0x4E, 0x45, 0x53, 0x1A})
// TzIf matches a Time Zone Information Format (TZif) file.
TzIf = prefix([]byte("TZif"))
// SWF matches an Adobe Flash swf file.
SWF = prefix([]byte("CWS"), []byte("FWS"), []byte("ZWS"))
// Torrent has bencoded text in the beginning.
Expand Down Expand Up @@ -166,3 +164,33 @@ func Marc(raw []byte, limit uint32) bool {
// | g l T F | 1 | ... |
var Glb = prefix([]byte("\x67\x6C\x54\x46\x02\x00\x00\x00"),
[]byte("\x67\x6C\x54\x46\x01\x00\x00\x00"))

// TzIf matches a Time Zone Information Format (TZif) file.
// See more: https://tools.ietf.org/id/draft-murchison-tzdist-tzif-00.html#rfc.section.3
// Its header structure is shown below:
// +---------------+---+
// | magic (4) | <-+-- version (1)
// +---------------+---+---------------------------------------+
// | [unused - reserved for future use] (15) |
// +---------------+---------------+---------------+-----------+
// | isutccnt (4) | isstdcnt (4) | leapcnt (4) |
// +---------------+---------------+---------------+
// | timecnt (4) | typecnt (4) | charcnt (4) |
func TzIf(raw []byte, limit uint32) bool {
// File is at least 44 bytes (header size).
if len(raw) < 44 {
return false
}

if !bytes.HasPrefix(raw, []byte("TZif")) {
return false
}

// Field "typecnt" MUST not be zero.
if binary.BigEndian.Uint32(raw[36:40]) == 0 {
return false
}

// Version has to be NUL (0x00), '2' (0x32) or '3' (0x33).
return raw[4] == 0x00 || raw[4] == 0x32 || raw[4] == 0x33
}

0 comments on commit 98e3221

Please sign in to comment.