Path: blob/master/src/java.desktop/share/classes/sun/java2d/cmm/ProfileDataVerifier.java
41159 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.cmm;2627public class ProfileDataVerifier {28/**29* Throws an IllegalArgumentException if the data does not correspond30* to a valid ICC Profile.31*32* @param data the specified profile data.33*/34public static void verify(byte[] data) {35if (data == null) {36throw new IllegalArgumentException("Invalid ICC Profile Data");37}3839if (data.length < TOC_OFFSET) {40// not enough data for profile header41throw new IllegalArgumentException("Invalid ICC Profile Data");42}4344// check profile size45final int size = readInt32(data, 0);46final int tagCount = readInt32(data, HEADER_SIZE);4748if (tagCount < 0 || tagCount > MAX_TAG_COUNT) {49throw new IllegalArgumentException("Invalid ICC Profile Data");50}5152if (size < (TOC_OFFSET + (tagCount * TOC_RECORD_SIZE)) ||53size > data.length)54{55throw new IllegalArgumentException("Invalid ICC Profile Data");56}5758final int sig = readInt32(data, 36);5960if (PROFILE_FILE_SIGNATURE != sig) {61throw new IllegalArgumentException("Invalid ICC Profile Data");62}6364// verify table of content65for (int i = 0; i < tagCount; i++) {66final int tag_offset = getTagOffset(i, data);67final int tag_size = getTagSize(i, data);6869if (tag_offset < TOC_OFFSET || tag_offset > size) {70throw new IllegalArgumentException("Invalid ICC Profile Data");71}7273if (tag_size < 0 ||74tag_size > (Integer.MAX_VALUE - tag_offset) ||75tag_size + tag_offset > size)76{77throw new IllegalArgumentException("Invalid ICC Profile Data");78}79}80}8182private static int getTagOffset(int idx, byte[] data) {83final int pos = TOC_OFFSET + idx * TOC_RECORD_SIZE + 4;84return readInt32(data, pos);85}8687private static int getTagSize(int idx, byte[] data) {88final int pos = TOC_OFFSET + idx * TOC_RECORD_SIZE + 8;89return readInt32(data, pos);90}9192private static int readInt32(byte[] data, int off) {93int res = 0;94for (int i = 0; i < 4; i++) {95res = res << 8;9697res |= (0xff & data[off++]);98}99return res;100}101102/**103* Lcms limit for the number of tags: 100104* Kcms limit for the number of tags: N/A105*/106private static final int MAX_TAG_COUNT = 100;107108private static final int HEADER_SIZE = 128;109private static final int TOC_OFFSET = HEADER_SIZE + 4;110private static final int TOC_RECORD_SIZE = 12;111112private static final int PROFILE_FILE_SIGNATURE = 0x61637370;113}114115116