From e17d959b6cdc3d742229d1b9fb76fbd799a464d4 Mon Sep 17 00:00:00 2001 From: ne1llee <12810839+ne1llee@users.noreply.github.com> Date: Fri, 9 Jul 2021 14:53:04 +0800 Subject: [PATCH] Add support for PE-DLL file formats --- internal/magic/binary.go | 15 +++++++++++++++ mimetype_test.go | 1 + testdata/dll.dll | Bin 0 -> 2048 bytes tree.go | 3 ++- 4 files changed, 18 insertions(+), 1 deletion(-) create mode 100755 testdata/dll.dll diff --git a/internal/magic/binary.go b/internal/magic/binary.go index 6bb4a939..de95dd1d 100644 --- a/internal/magic/binary.go +++ b/internal/magic/binary.go @@ -142,3 +142,18 @@ func Marc(raw []byte, limit uint32) bool { // Field terminator is present. return bytes.Contains(raw, []byte{0x1E}) } + +// Microsoft PE dll +func Dll(raw []byte, limit uint32) bool { + l := len(raw) + if l < 0x40 { + return false + } + signoff := binary.LittleEndian.Uint32(raw[0x3c:]) + if uint32(l) < signoff+0x18 { + return false + } + characteristics := binary.LittleEndian.Uint16(raw[signoff+0x16:]) + + return characteristics&0x2000 == 0x2000 +} diff --git a/mimetype_test.go b/mimetype_test.go index 215f02bb..419c84f1 100644 --- a/mimetype_test.go +++ b/mimetype_test.go @@ -198,6 +198,7 @@ var files = map[string]string{ "xz.xz": "application/x-xz", "zip.zip": "application/zip", "zst.zst": "application/zstd", + "dll.dll": "application/vnd.microsoft.portable-executable-dll", } func TestDetect(t *testing.T) { diff --git a/testdata/dll.dll b/testdata/dll.dll new file mode 100755 index 0000000000000000000000000000000000000000..35aaadfc9b0878e00bf7ccb518920e57a3389987 GIT binary patch literal 2048 zcmdT_O=uHQ5dPNILRy=uR1bo>l`48@=zsK}#+nK(X;Ye7MGumuX%f5HEt|yBTW^-0 zdJ>_R9`zzYJQP9j;xQoNr3aztLGYp%DTuUw^ETV8sVSZWC(L_q=FNOFZ|3dG$ek6` z0jMYM>;UVWg+glFe-K;$>6d=I@Ri!uwMeOLBAvaxobqh&cvIbt-bc|4Oy%U*RRV}n3M)3Ca6ci7$A z#DTUZ&F6>>u9oxRGRvuCeE?{Hw#(E2I+^!Q{gYqAC`>STew$eELe!~z(rp-=xo@8iW<^dLA(4WeJ6 zu+$2?_3RfJ_`O`nGSy)OH!$P z#(YN90cLE`ewOP9eIJkwrH?CZfhamFC+Cw>CMm@@+O!lrk;geoo&{<}lT&9faL$w0 zr1Ht_W)1WsfG%>7t4{tz>PHXnQ6DkSV_wAzvQswCk>$Qq!yxT?(7~~rmVLOuX#Hqn zEU9~5#W7hS!&A{IN>jv<+ya=UWT+e$DQW85Ddiy@nPV@cqba0`20Udpduvy#9|2ll zQ~0_EB}c6j-KkuXIRuD(MdckWTS|Q;Z?~5sIX9wK{`YZ~`H#`xB4UBn&wEZ?tLL{= zWvA&g&w9u$i1O5unRBnC*V5$$?n*tgs#XJN*%Pa}tNZm-K*D5=Ut*lG+*4M*7NDKq z88D$TF&W(`XXM;;@Es_M0ZQAYZRZ`(M8pv_I!{~IhaVr0o+z5bv!8sQSC9QXb!K$y zTk6f1cf*%ApFG^^Dx2<^mZ8d>Bb`4HKq*Ic;r@2^ee&9BzWv)e@GbEPQB OPIvI7;@`)AMZW;%!|)UU literal 0 HcmV?d00001 diff --git a/tree.go b/tree.go index 757ffed9..6d305e9d 100644 --- a/tree.go +++ b/tree.go @@ -171,7 +171,8 @@ var ( shp = newMIME("application/octet-stream", ".shp", magic.Shp) shx = newMIME("application/octet-stream", ".shx", magic.Shx, shp) dbf = newMIME("application/x-dbf", ".dbf", magic.Dbf) - exe = newMIME("application/vnd.microsoft.portable-executable", ".exe", magic.Exe) + dll = newMIME("application/vnd.microsoft.portable-executable-dll", ".dll", magic.Dll) + exe = newMIME("application/vnd.microsoft.portable-executable", ".exe", magic.Exe, dll) elf = newMIME("application/x-elf", "", magic.Elf, elfObj, elfExe, elfLib, elfDump) elfObj = newMIME("application/x-object", "", magic.ElfObj) elfExe = newMIME("application/x-executable", "", magic.ElfExe)