Debian Codegraph @

internal/magic/document.go

raw symbols SCIP
1 package magic
2
3 import (
4 "bytes"
5 "encoding/binary"
6
7 "github.com/gabriel-vasile/mimetype/internal/scan"
8 )
9
10 // Pdf matches a Portable Document Format file.
11 // https://github.com/file/file/blob/11010cc805546a3e35597e67e1129a481aed40e8/magic/Magdir/pdf
12 func Pdf(raw []byte, _ uint32) bool {
13 // usual pdf signature
14 return bytes.HasPrefix(raw, []byte("%PDF-")) ||
15 // new-line prefixed signature
16 bytes.HasPrefix(raw, []byte("\012%PDF-")) ||
17 // UTF-8 BOM prefixed signature
18 bytes.HasPrefix(raw, []byte("\xef\xbb\xbf%PDF-"))
19 }
20
21 // Fdf matches a Forms Data Format file.
22 func Fdf(raw []byte, _ uint32) bool {
23 return bytes.HasPrefix(raw, []byte("%FDF"))
24 }
25
26 // Mobi matches a Mobi file.
27 func Mobi(raw []byte, _ uint32) bool {
28 return offset(raw, []byte("BOOKMOBI"), 60)
29 }
30
31 // Lit matches a Microsoft Lit file.
32 func Lit(raw []byte, _ uint32) bool {
33 return bytes.HasPrefix(raw, []byte("ITOLITLS"))
34 }
35
36 // PDF matches a Portable Document Format file.
37 // The %PDF- header should be the first thing inside the file but many
38 // implementations don't follow the rule. The PDF spec at Appendix H says the
39 // signature can be prepended by anything.
40 // https://bugs.astron.com/view.php?id=446
41 func PDF(raw []byte, _ uint32) bool {
42 raw = raw[:min(len(raw), 1024)]
43 return bytes.Contains(raw, []byte("%PDF-"))
44 }
45
46 // DjVu matches a DjVu file.
47 func DjVu(raw []byte, _ uint32) bool {
48 if len(raw) < 12 {
49 return false
50 }
51 if !bytes.HasPrefix(raw, []byte{0x41, 0x54, 0x26, 0x54, 0x46, 0x4F, 0x52, 0x4D}) {
52 return false
53 }
54 return bytes.HasPrefix(raw[12:], []byte("DJVM")) ||
55 bytes.HasPrefix(raw[12:], []byte("DJVU")) ||
56 bytes.HasPrefix(raw[12:], []byte("DJVI")) ||
57 bytes.HasPrefix(raw[12:], []byte("THUM"))
58 }
59
60 // P7s matches an .p7s signature File (PEM, Base64).
61 func P7s(raw []byte, _ uint32) bool {
62 // Check for PEM Encoding.
63 if bytes.HasPrefix(raw, []byte("-----BEGIN PKCS7")) {
64 return true
65 }
66 // Check if DER Encoding is long enough.
67 if len(raw) < 20 {
68 return false
69 }
70 // Magic Bytes for the signedData ASN.1 encoding.
71 startHeader := [][]byte{{0x30, 0x80}, {0x30, 0x81}, {0x30, 0x82}, {0x30, 0x83}, {0x30, 0x84}}
72 signedDataMatch := []byte{0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07}
73 // Check if Header is correct. There are multiple valid headers.
74 for i, match := range startHeader {
75 // If first bytes match, then check for ASN.1 Object Type.
76 if bytes.HasPrefix(raw, match) {
77 if bytes.HasPrefix(raw[i+2:], signedDataMatch) {
78 return true
79 }
80 }
81 }
82
83 return false
84 }
85
86 // Lotus123 matches a Lotus 1-2-3 spreadsheet document.
87 func Lotus123(raw []byte, _ uint32) bool {
88 if len(raw) <= 20 {
89 return false
90 }
91 version := binary.BigEndian.Uint32(raw)
92 if version == 0x00000200 {
93 return raw[6] != 0 && raw[7] == 0
94 }
95
96 return version == 0x00001a00 && raw[20] > 0 && raw[20] < 32
97 }
98
99 // CHM matches a Microsoft Compiled HTML Help file.
100 func CHM(raw []byte, _ uint32) bool {
101 return bytes.HasPrefix(raw, []byte("ITSF\003\000\000\000\x60\000\000\000"))
102 }
103
104 // Inf matches an OS/2 .inf file.
105 func Inf(raw []byte, _ uint32) bool {
106 return bytes.HasPrefix(raw, []byte("HSP\x01\x9b\x00"))
107 }
108
109 // Hlp matches an OS/2 .hlp file.
110 func Hlp(raw []byte, _ uint32) bool {
111 return bytes.HasPrefix(raw, []byte("HSP\x10\x9b\x00"))
112 }
113
114 // FrameMaker matches an Adobe FrameMaker file.
115 func FrameMaker(raw []byte, _ uint32) bool {
116 b := scan.Bytes(raw)
117 if !bytes.HasPrefix(b, []byte("<MakerFile")) &&
118 !bytes.HasPrefix(b, []byte("<MakerDictionary")) &&
119 b.Match([]byte("<BOOKFILE"), scan.IgnoreCase) == -1 {
120 return false
121 }
122
123 // To avoid plain text false positives.
124 return bytes.IndexByte(b[:min(len(b), 512)], 0x00) != -1
125 }