View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.icc;
18  
19  import static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.nio.ByteOrder;
25  import java.nio.charset.StandardCharsets;
26  import java.util.logging.Logger;
27  
28  import org.apache.commons.imaging.ImagingException;
29  
30  public enum IccTagDataTypes implements IccTagDataType {
31      DESC_TYPE("descType", 0x64657363) {
32          @Override
33          public void dump(final String prefix, final byte[] bytes) throws ImagingException, IOException {
34              try (InputStream bis = new ByteArrayInputStream(bytes)) {
35                  read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
36  
37                  // bis.setDebug(true);
38                  read4Bytes("ignore", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
39                  final int stringLength = read4Bytes("stringLength", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
40  
41                  // bis.readByteArray("ignore", bytes.length -12, "none");
42                  final String s = new String(bytes, 12, stringLength - 1, StandardCharsets.US_ASCII);
43                  LOGGER.fine(prefix + "s: '" + s + "'");
44              }
45          }
46  
47      },
48  
49      DATA_TYPE("dataType", 0x64617461) {
50          @Override
51          public void dump(final String prefix, final byte[] bytes) throws ImagingException, IOException {
52              try (InputStream bis = new ByteArrayInputStream(bytes)) {
53                  read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
54              }
55          }
56  
57      },
58  
59      MULTI_LOCALIZED_UNICODE_TYPE("multiLocalizedUnicodeType", 0x6D6C7563) {
60          @Override
61          public void dump(final String prefix, final byte[] bytes) throws ImagingException, IOException {
62              try (InputStream bis = new ByteArrayInputStream(bytes)) {
63                  read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
64              }
65          }
66  
67      },
68  
69      SIGNATURE_TYPE("signatureType", 0x73696720) {
70          @Override
71          public void dump(final String prefix, final byte[] bytes) throws ImagingException, IOException {
72              try (InputStream bis = new ByteArrayInputStream(bytes)) {
73                  read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
74                  read4Bytes("ignore", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
75                  final int theSignature = read4Bytes("theSignature ", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
76                  LOGGER.fine(prefix + "theSignature: " + Integer.toHexString(theSignature) + " ("
77                          + new String(new byte[] { (byte) (0xff & theSignature >> 24), (byte) (0xff & theSignature >> 16),
78                                  (byte) (0xff & theSignature >> 8), (byte) (0xff & theSignature >> 0), }, StandardCharsets.US_ASCII)
79                          + ")");
80              }
81          }
82  
83      },
84  
85      TEXT_TYPE("textType", 0x74657874) {
86          @Override
87          public void dump(final String prefix, final byte[] bytes) throws ImagingException, IOException {
88              try (InputStream bis = new ByteArrayInputStream(bytes)) {
89                  read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
90                  read4Bytes("ignore", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
91                  final String s = new String(bytes, 8, bytes.length - 8, StandardCharsets.US_ASCII);
92                  LOGGER.fine(prefix + "s: '" + s + "'");
93              }
94          }
95  
96      };
97  
98      private static final Logger LOGGER = Logger.getLogger(IccTagDataTypes.class.getName());
99  
100     public final String name;
101     public final int signature;
102 
103     IccTagDataTypes(final String name, final int signature) {
104         this.name = name;
105         this.signature = signature;
106     }
107 
108     @Override
109     public String getName() {
110         return name;
111     }
112 
113     @Override
114     public int getSignature() {
115         return signature;
116     }
117 }