001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 * 019 */ 020 package org.apache.directory.shared.ldap.schema.syntaxCheckers; 021 022 import org.apache.directory.shared.ldap.constants.SchemaConstants; 023 import org.apache.directory.shared.ldap.schema.SyntaxChecker; 024 import org.slf4j.Logger; 025 import org.slf4j.LoggerFactory; 026 027 028 /** 029 * A SyntaxChecker which verifies that a value is a Jpeg according to RFC 4517. 030 * 031 * The JFIF (Jpeg File Interchange Format) specify that a jpeg image starts with 032 * the following bytes : 033 * 0xFF 0xD8 (SOI, Start Of Image) 034 * 0xFF 0xE0 (App0) 035 * 0xNN 0xNN (Header length) 036 * "JFIF\0" (JFIF string with an ending \0) 037 * some other bytes which are related to the image. 038 * 039 * We will check for those 11 bytes, except the length. 040 * 041 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 042 * @version $Rev: 437007 $ 043 */ 044 public class JpegSyntaxChecker extends SyntaxChecker 045 { 046 /** A logger for this class */ 047 private static final Logger LOG = LoggerFactory.getLogger( JpegSyntaxChecker.class ); 048 049 /** The serialVersionUID */ 050 private static final long serialVersionUID = 1L; 051 052 /** 053 * Creates a new instance of JpegSyntaxChecker. 054 */ 055 public JpegSyntaxChecker() 056 { 057 super( SchemaConstants.JPEG_SYNTAX ); 058 } 059 060 061 /** 062 * {@inheritDoc} 063 */ 064 public boolean isValidSyntax( Object value ) 065 { 066 if ( value == null ) 067 { 068 LOG.debug( "Syntax invalid for '{}'", value ); 069 return false; 070 } 071 072 // The value must be a byte array 073 if ( ! ( value instanceof byte[] ) ) 074 { 075 LOG.debug( "Syntax invalid for '{}'", value ); 076 return false; 077 } 078 079 byte[] bytes = (byte[])value; 080 081 // The header must be at least 11 bytes long 082 if ( bytes.length < 11 ) 083 { 084 LOG.debug( "Syntax invalid for '{}'", value ); 085 return false; 086 } 087 088 if ( ( bytes[0] == (byte)0x00FF ) && // SOI 089 ( bytes[1] == (byte)0x00D8 ) && 090 ( bytes[2] == (byte)0x00FF ) && // APP0 091 ( bytes[3] == (byte)0x00E0 ) && 092 ( bytes[6] == 'J' ) && // JFIF 093 ( bytes[7] == 'F' ) && // JFIF 094 ( bytes[8] == 'I' ) && // JFIF 095 ( bytes[9] == 'F' ) && 096 ( bytes[10] == 0x00 ) ) // \0 097 { 098 // Note : this is not because the header is correct 099 // that the file is a jpeg file. There are much more 100 // elements to check, but we are not writing a jpeg 101 // file checker... 102 LOG.debug( "Syntax valid for '{}'", value ); 103 return true; 104 } 105 106 LOG.debug( "Syntax invalid for '{}'", value ); 107 return false; 108 } 109 }