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 */
020package org.apache.directory.shared.ldap.model.schema.syntaxCheckers;
021
022import org.apache.directory.shared.ldap.model.constants.SchemaConstants;
023import org.apache.directory.shared.ldap.model.schema.SyntaxChecker;
024import org.slf4j.Logger;
025import 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 */
043@SuppressWarnings("serial")
044public class JpegSyntaxChecker extends SyntaxChecker
045{
046    /** A logger for this class */
047    private static final Logger LOG = LoggerFactory.getLogger( JpegSyntaxChecker.class );
048
049    /**
050     * Creates a new instance of JpegSyntaxChecker.
051     */
052    public JpegSyntaxChecker()
053    {
054        super( SchemaConstants.JPEG_SYNTAX );
055    }
056    
057    
058    /**
059     * {@inheritDoc}
060     */
061    public boolean isValidSyntax( Object value )
062    {
063        if ( value == null )
064        {
065            LOG.debug( "Syntax invalid for 'null'" );
066            return false;
067        }
068        
069        // The value must be a byte array
070        if ( ! ( value instanceof byte[] ) )
071        {
072            LOG.debug( "Syntax invalid for '{}'", value );
073            return false;
074        }
075        
076        byte[] bytes = (byte[])value;
077
078        // The header must be at least 11 bytes long
079        if ( bytes.length < 11 )
080        {
081            LOG.debug( "Syntax invalid for '{}'", value );
082            return false;
083        }
084
085        if ( ( bytes[0] == ( byte ) 0x00FF ) // SOI
086            && ( bytes[1] == ( byte ) 0x00D8 )
087            && ( bytes[2] == ( byte ) 0x00FF ) // APP0
088            && ( bytes[3] == ( byte ) 0x00E0 )
089            && ( bytes[6] == 'J' ) // JFIF
090            && ( bytes[7] == 'F' ) // JFIF
091            && ( bytes[8] == 'I' ) // JFIF
092            && ( bytes[9] == 'F' )
093            && ( bytes[10] == 0x00 ) ) // \0
094        {
095            // Note : this is not because the header is correct
096            // that the file is a jpeg file. There are much more
097            // elements to check, but we are not writing a jpeg
098            // file checker...
099            LOG.debug( "Syntax valid for '{}'", value );
100            return true;
101        }
102
103        LOG.debug( "Syntax invalid for '{}'", value );
104        return false;
105    }
106}