Debian Codegraph @

internal/magic/magic.go

raw symbols SCIP
1 // Package magic holds the matching functions used to find MIME types.
2 package magic
3
4 import (
5 "bytes"
6
7 "github.com/gabriel-vasile/mimetype/internal/scan"
8 )
9
10 type (
11 // Detector receiveѕ the raw data of a file and returns whether the data
12 // meets any conditions. The limit parameter is an upper limit to the number
13 // of bytes received and is used to tell if the byte slice represents the
14 // whole file or is just the header of a file: len(raw) < limit or len(raw)>limit.
15 Detector func(raw []byte, limit uint32) bool
16 xmlSig struct {
17 // the local name of the root tag
18 localName []byte
19 // the namespace of the XML document
20 xmlns []byte
21 }
22 )
23
24 // offset returns true if the provided signature can be
25 // found at offset in the raw input.
26 func offset(raw []byte, sig []byte, offset int) bool {
27 return len(raw) > offset && bytes.HasPrefix(raw[offset:], sig)
28 }
29
30 // ciPrefix is like prefix but the check is case insensitive.
31 func ciPrefix(raw []byte, sigs ...[]byte) bool {
32 for _, s := range sigs {
33 if ciCheck(s, raw) {
34 return true
35 }
36 }
37 return false
38 }
39 func ciCheck(sig, raw []byte) bool {
40 if len(raw) < len(sig)+1 {
41 return false
42 }
43 // perform case insensitive check
44 for i, b := range sig {
45 db := raw[i]
46 if 'A' <= b && b <= 'Z' {
47 db &= 0xDF
48 }
49 if b != db {
50 return false
51 }
52 }
53
54 return true
55 }
56
57 // xml returns true if any of the provided XML signatures matches the raw input.
58 func xml(b scan.Bytes, sigs ...xmlSig) bool {
59 b.TrimLWS()
60 if len(b) == 0 {
61 return false
62 }
63 for _, s := range sigs {
64 if xmlCheck(s, b) {
65 return true
66 }
67 }
68 return false
69 }
70 func xmlCheck(sig xmlSig, raw []byte) bool {
71 raw = raw[:min(len(raw), 512)]
72
73 if len(sig.localName) == 0 {
74 return bytes.Index(raw, sig.xmlns) > 0
75 }
76 if len(sig.xmlns) == 0 {
77 return bytes.Index(raw, sig.localName) > 0
78 }
79
80 localNameIndex := bytes.Index(raw, sig.localName)
81 return localNameIndex != -1 && localNameIndex < bytes.Index(raw, sig.xmlns)
82 }
83
84 // markup returns true is any of the HTML signatures matches the raw input.
85 func markup(b scan.Bytes, sigs ...[]byte) bool {
86 if bytes.HasPrefix(b, []byte{0xEF, 0xBB, 0xBF}) {
87 // We skip the UTF-8 BOM if present to ensure we correctly
88 // process any leading whitespace. The presence of the BOM
89 // is taken into account during charset detection in charset.go.
90 b.Advance(3)
91 }
92 b.TrimLWS()
93 if len(b) == 0 {
94 return false
95 }
96 for _, s := range sigs {
97 if markupCheck(s, b) {
98 return true
99 }
100 }
101 return false
102 }
103 func markupCheck(sig, raw []byte) bool {
104 if len(raw) < len(sig)+1 {
105 return false
106 }
107
108 // perform case insensitive check
109 for i, b := range sig {
110 db := raw[i]
111 if 'A' <= b && b <= 'Z' {
112 db &= 0xDF
113 }
114 if b != db {
115 return false
116 }
117 }
118 // Next byte must be space or right angle bracket.
119 if db := raw[len(sig)]; !scan.ByteIsWS(db) && db != '>' {
120 return false
121 }
122
123 return true
124 }
125
126 // ftyp returns true if any of the FTYP signatures matches the raw input.
127 func ftyp(raw []byte, sigs ...[]byte) bool {
128 if len(raw) < 12 {
129 return false
130 }
131 for _, s := range sigs {
132 if bytes.Equal(raw[8:12], s) {
133 return true
134 }
135 }
136 return false
137 }
138
139 // A valid shebang starts with the "#!" characters,
140 // followed by any number of spaces,
141 // followed by the path to the interpreter,
142 // and, optionally, followed by the arguments for the interpreter.
143 //
144 // Ex:
145 //
146 // #! /usr/bin/env php
147 //
148 // /usr/bin/env is the interpreter, php is the first and only argument.
149 func shebang(b scan.Bytes, matchFlags scan.Flags, sigs ...[]byte) bool {
150 line := b.Line()
151 if len(line) < 2 || line[0] != '#' || line[1] != '!' {
152 return false
153 }
154 line = line[2:]
155 line.TrimLWS()
156 for _, s := range sigs {
157 if line.Match(s, matchFlags) != -1 {
158 return true
159 }
160 }
161 return false
162 }