Debian Codegraph @

src/com/drew/imaging/riff/RiffReader.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.riff;
22
23 import com.drew.lang.SequentialReader;
24 import com.drew.lang.annotations.NotNull;
25
26 import java.io.IOException;
27
28 /**
29 * Processes RIFF-formatted data, calling into client code via that {@link RiffHandler} interface.
30 * <p></p>
31 * For information on this file format, see:
32 * <ul>
33 * <li>http://en.wikipedia.org/wiki/Resource_Interchange_File_Format</li>
34 * <li>https://developers.google.com/speed/webp/docs/riff_container</li>
35 * <li>https://www.daubnet.com/en/file-format-riff</li>
36 * </ul>
37 * @author Drew Noakes https://drewnoakes.com
38 * @author Payton Garland
39 */
40 public class RiffReader
41 {
42 /**
43 * Processes a RIFF data sequence.
44 *
45 * @param reader the {@link SequentialReader} from which the data should be read
46 * @param handler the {@link RiffHandler} that will coordinate processing and accept read values
47 * @throws RiffProcessingException if an error occurred during the processing of RIFF data that could not be
48 * ignored or recovered from
49 * @throws IOException an error occurred while accessing the required data
50 */
51 public void processRiff(@NotNull final SequentialReader reader,
52 @NotNull final RiffHandler handler) throws RiffProcessingException, IOException
53 {
54 // RIFF files are always little-endian
55 reader.setMotorolaByteOrder(false);
56
57 // PROCESS FILE HEADER
58
59 final String fileFourCC = reader.getString(4);
60
61 if (!fileFourCC.equals("RIFF"))
62 throw new RiffProcessingException("Invalid RIFF header: " + fileFourCC);
63
64 // The total size of the chunks that follow plus 4 bytes for the FourCC
65 final int fileSize = reader.getInt32();
66 int sizeLeft = fileSize;
67
68 final String identifier = reader.getString(4);
69 sizeLeft -= 4;
70
71 if (!handler.shouldAcceptRiffIdentifier(identifier))
72 return;
73
74 // PROCESS CHUNKS
75 processChunks(reader, sizeLeft, handler);
76 }
77
78 public void processChunks(SequentialReader reader, int sectionSize, RiffHandler handler) throws IOException
79 {
80 try {
81 while (reader.getPosition() < sectionSize) {
82 String fourCC = new String(reader.getBytes(4));
83 int size = reader.getInt32();
84 if (size <= 0) {
85 handler.addError("Invalid chunk size: " + size);
86 break;
87 }
88 if (fourCC.equals("LIST") || fourCC.equals("RIFF")) {
89 String listName = new String(reader.getBytes(4));
90 if (size < 4) {
91 handler.addError("Chunk size too small.");
92 } else {
93 if (handler.shouldAcceptList(listName)) {
94 processChunks(reader, size - 4, handler);
95 } else {
96 reader.skip(size - 4);
97 }
98 }
99 } else if (fourCC.equals("IDIT")) {
100 // Avi DateTimeOriginal
101 if (size < 2) {
102 handler.addError("Chunk size too small.");
103 } else {
104 handler.processChunk(fourCC, reader.getBytes(size - 2));
105 reader.skip(2); // ?0A 00?
106 }
107 } else {
108 if (handler.shouldAcceptChunk(fourCC)) {
109 // TODO is it feasible to avoid copying the chunk here, and to pass the sequential reader to the handler?
110 handler.processChunk(fourCC, reader.getBytes(size));
111 } else {
112 reader.skip(size);
113 }
114 // Bytes read must be even - skip one if not
115 if ((size & 1) == 1) {
116 reader.skip(1);
117 }
118 }
119 }
120 } catch (IOException e) {
121 handler.addError(e.getMessage());
122 }
123 }
124 }