Debian Codegraph @

src/com/drew/imaging/FileTypeDetector.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21 package com.drew.imaging;
22
23 import com.drew.imaging.mp3.MpegAudioTypeChecker;
24 import com.drew.imaging.quicktime.QuickTimeTypeChecker;
25 import com.drew.imaging.riff.RiffTypeChecker;
26 import com.drew.lang.ByteTrie;
27 import com.drew.lang.annotations.NotNull;
28
29 import java.io.InputStream;
30 import java.io.IOException;
31
32 /**
33 * Examines the a file's first bytes and estimates the file's type.
34 */
35 public class FileTypeDetector
36 {
37 private final static ByteTrie<FileType> _root;
38 private final static TypeChecker[] _fixedCheckers;
39 private final static int _bytesNeeded;
40
41 static
42 {
43 _fixedCheckers = new TypeChecker[] {
44 new QuickTimeTypeChecker(),
45 new RiffTypeChecker(),
46 new MpegAudioTypeChecker()
47 };
48
49 _root = new ByteTrie<FileType>();
50 _root.setDefaultValue(FileType.Unknown);
51
52 // https://en.wikipedia.org/wiki/List_of_file_signatures
53
54 _root.addPath(FileType.Jpeg, new byte[]{(byte)0xff, (byte)0xd8});
55 _root.addPath(FileType.Tiff, "II".getBytes(), new byte[]{0x2a, 0x00});
56 _root.addPath(FileType.Tiff, "MM".getBytes(), new byte[]{0x00, 0x2a});
57 _root.addPath(FileType.Psd, "8BPS".getBytes());
58 _root.addPath(FileType.Png, new byte[]{(byte)0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52});
59 _root.addPath(FileType.Bmp, "BM".getBytes()); // Standard Bitmap Windows and OS/2
60 _root.addPath(FileType.Bmp, "BA".getBytes()); // OS/2 Bitmap Array
61 _root.addPath(FileType.Bmp, "CI".getBytes()); // OS/2 Color Icon
62 _root.addPath(FileType.Bmp, "CP".getBytes()); // OS/2 Color Pointer
63 _root.addPath(FileType.Bmp, "IC".getBytes()); // OS/2 Icon
64 _root.addPath(FileType.Bmp, "PT".getBytes()); // OS/2 Pointer
65 _root.addPath(FileType.Gif, "GIF87a".getBytes());
66 _root.addPath(FileType.Gif, "GIF89a".getBytes());
67 _root.addPath(FileType.Ico, new byte[]{0x00, 0x00, 0x01, 0x00});
68 _root.addPath(FileType.Pcx, new byte[]{0x0A, 0x00, 0x01}); // multiple PCX versions, explicitly listed
69 _root.addPath(FileType.Pcx, new byte[]{0x0A, 0x02, 0x01});
70 _root.addPath(FileType.Pcx, new byte[]{0x0A, 0x03, 0x01});
71 _root.addPath(FileType.Pcx, new byte[]{0x0A, 0x05, 0x01});
72 _root.addPath(FileType.Arw, "II".getBytes(), new byte[]{0x2a, 0x00, 0x08, 0x00});
73 _root.addPath(FileType.Crw, "II".getBytes(), new byte[]{0x1a, 0x00, 0x00, 0x00}, "HEAPCCDR".getBytes());
74 _root.addPath(FileType.Cr2, "II".getBytes(), new byte[]{0x2a, 0x00, 0x10, 0x00, 0x00, 0x00, 0x43, 0x52});
75 // NOTE this doesn't work for NEF as it incorrectly flags many other TIFF files as being NEF
76 // _root.addPath(FileType.Nef, "MM".getBytes(), new byte[]{0x00, 0x2a, 0x00, 0x00, 0x00, (byte)0x08, 0x00});
77 _root.addPath(FileType.Orf, "IIRO".getBytes(), new byte[]{(byte)0x08, 0x00});
78 _root.addPath(FileType.Orf, "MMOR".getBytes(), new byte[]{(byte)0x00, 0x00});
79 _root.addPath(FileType.Orf, "IIRS".getBytes(), new byte[]{(byte)0x08, 0x00});
80 _root.addPath(FileType.Raf, "FUJIFILMCCD-RAW".getBytes());
81 _root.addPath(FileType.Rw2, "II".getBytes(), new byte[]{0x55, 0x00});
82 _root.addPath(FileType.Eps, "%!PS".getBytes());
83 _root.addPath(FileType.Eps, new byte[]{(byte)0xC5, (byte)0xD0, (byte)0xD3, (byte)0xC6});
84
85 // Only file detection
86 _root.addPath(FileType.Aac, new byte[]{(byte)0xFF, (byte)0xF1});
87 _root.addPath(FileType.Aac, new byte[]{(byte)0xFF, (byte)0xF9});
88 _root.addPath(FileType.Asf, new byte[]{0x30, 0x26, (byte)0xB2, 0x75, (byte)0x8E, 0x66, (byte)0xCF, 0x11, (byte)0xA6, (byte)0xD9, 0x00, (byte)0xAA, 0x00, 0x62, (byte)0xCE, 0x6C});
89 _root.addPath(FileType.Cfbf, new byte[]{(byte)0xD0, (byte)0xCF, 0x11, (byte)0xE0, (byte)0xA1, (byte)0xB1, 0x1A, (byte)0xE1, 0x00});
90 _root.addPath(FileType.Flv, new byte[]{0x46, 0x4C, 0x56});
91 _root.addPath(FileType.Indd, new byte[]{0x06, 0x06, (byte)0xED, (byte)0xF5, (byte)0xD8, 0x1D, 0x46, (byte)0xE5, (byte)0xBD, 0x31, (byte)0xEF, (byte)0xE7, (byte)0xFE, 0x74, (byte)0xB7, 0x1D});
92 _root.addPath(FileType.Mxf, new byte[]{0x06, 0x0e, 0x2b, 0x34, 0x02, 0x05, 0x01, 0x01, 0x0d, 0x01, 0x02, 0x01, 0x01, 0x02}); // has offset?
93 _root.addPath(FileType.Qxp, new byte[]{0x00, 0x00, 0x49, 0x49, 0x58, 0x50, 0x52, 0x33}); // "..IIXPR3" (little-endian - intel)
94 _root.addPath(FileType.Qxp, new byte[]{0x00, 0x00, 0x4D, 0x4D, 0x58, 0x50, 0x52, 0x33}); // "..MMXPR3" (big-endian - motorola)
95 _root.addPath(FileType.Ram, new byte[]{0x72, 0x74, 0x73, 0x70, 0x3A, 0x2F, 0x2F});
96 _root.addPath(FileType.Rtf, new byte[]{0x7B, 0x5C, 0x72, 0x74, 0x66, 0x31});
97 _root.addPath(FileType.Sit, new byte[]{ 0x53, 0x49, 0x54, 0x21, 0x00 }); // SIT!);
98 _root.addPath(FileType.Sit, new byte[]{ 0x53, 0x74, 0x75, 0x66, 0x66, 0x49, 0x74, 0x20, 0x28, 0x63, 0x29, 0x31, 0x39, 0x39, 0x37, 0x2D}); // StuffIt (c)1997-
99 _root.addPath(FileType.Sitx, new byte[]{ 0x53, 0x74, 0x75, 0x66, 0x66, 0x49, 0x74, 0x21 });
100 _root.addPath(FileType.Swf, "CWS".getBytes());
101 _root.addPath(FileType.Swf, "FWS".getBytes());
102 _root.addPath(FileType.Swf, "ZWS".getBytes());
103 _root.addPath(FileType.Vob, new byte[]{0x00, 0x00, 0x01, (byte)0xBA});
104 _root.addPath(FileType.Zip, "PK".getBytes());
105
106 int bytesNeeded = _root.getMaxDepth();
107 for (TypeChecker fixedChecker : _fixedCheckers) {
108 if (fixedChecker.getByteCount() > bytesNeeded)
109 bytesNeeded = fixedChecker.getByteCount();
110 }
111 _bytesNeeded = bytesNeeded;
112 }
113
114 private FileTypeDetector() throws Exception
115 {
116 throw new Exception("Not intended for instantiation");
117 }
118
119 /**
120 * Examines the file's bytes and estimates the file's type.
121 * <p>
122 * The input stream must support mark and reset, in order to
123 * return the stream to the position at which it was provided
124 * to this method once completed.
125 * <p>
126 * Requires the stream to contain at least eight bytes.
127 *
128 * @throws IOException if the stream does not support mark/reset.
129 */
130 @NotNull
131 public static FileType detectFileType(@NotNull final InputStream inputStream) throws IOException
132 {
133 if (!inputStream.markSupported())
134 throw new IOException("Stream must support mark/reset");
135
136 inputStream.mark(_bytesNeeded);
137
138 byte[] bytes = new byte[_bytesNeeded];
139 int offset = 0;
140 int count = _bytesNeeded;
141 while (count != 0) {
142 int bytesRead = inputStream.read(bytes, offset, count);
143 if (bytesRead == -1)
144 break;
145 count -= bytesRead;
146 offset += bytesRead;
147 }
148
149 inputStream.reset();
150
151 FileType fileType = _root.find(bytes, 0, offset);
152
153 assert(fileType != null);
154
155 if (fileType == FileType.Unknown) {
156 for (TypeChecker checker : _fixedCheckers) {
157 fileType = checker.checkType(bytes);
158 if (fileType != FileType.Unknown)
159 return fileType;
160 }
161 }
162
163 return fileType;
164 }
165 }