Skip to content

Commit

Permalink
Merge 9afdbf3 into 272584c
Browse files Browse the repository at this point in the history
  • Loading branch information
n-vr committed Jan 31, 2022
2 parents 272584c + 9afdbf3 commit d5cbd67
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
38 changes: 36 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,39 @@ 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 bytes.Equal(raw[36:40], []byte{0x00}) {
return false
}

// Version has to be NUL (0x00), '2' (0x32) or '3' (0x33).
version := raw[4:5]
for _, validVersion := range [][]byte{{0x00}, {0x32}, {0x33}} {
if bytes.Equal(version, validVersion) {
return true
}
}
return false
}
7 changes: 7 additions & 0 deletions mimetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,3 +599,10 @@ func TestExtend(t *testing.T) {
})
}
}

func TestTzif(t *testing.T) {
mtype, _ := DetectFile("testdata/tzif.txt")
if mtype.Is("application/tzif") {
t.Fatal("simple text file should not be detected as Tzif file")
}
}
2 changes: 2 additions & 0 deletions testdata/tzif.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
TZif
This is a simple text file and it should not be detected as a Tzif file.

0 comments on commit d5cbd67

Please sign in to comment.