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.api.ldap.model.schema.syntaxCheckers;
021
022
023import org.apache.directory.api.ldap.model.constants.SchemaConstants;
024import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028
029/**
030 * A SyntaxChecker which verifies that a value is a Jpeg according to RFC 4517.
031 * 
032 * The JFIF (Jpeg File Interchange Format) specify that a jpeg image starts with
033 * the following bytes :
034 * 0xFF 0xD8 (SOI, Start Of Image)
035 * 0xFF 0xE0 (App0 for JFIF) or 0xDD 0xE1 (App1 for Exif)
036 * 0xNN 0xNN (Header length)
037 * "JFIF\0" (JFIF string with an ending \0)
038 * some other bytes which are related to the image.
039 * 
040 * We will check for those 11 bytes, except the length.
041 * 
042 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
043 */
044@SuppressWarnings("serial")
045public class JpegSyntaxChecker extends SyntaxChecker
046{
047    /** A logger for this class */
048    private static final Logger LOG = LoggerFactory.getLogger( JpegSyntaxChecker.class );
049
050    /**
051     * Creates a new instance of JpegSyntaxChecker.
052     */
053    public JpegSyntaxChecker()
054    {
055        super( SchemaConstants.JPEG_SYNTAX );
056    }
057
058
059    /**
060     * {@inheritDoc}
061     */
062    public boolean isValidSyntax( Object value )
063    {
064        if ( value == null )
065        {
066            LOG.debug( "Syntax invalid for 'null'" );
067            return false;
068        }
069
070        // The value must be a byte array
071        if ( !( value instanceof byte[] ) )
072        {
073            LOG.debug( "Syntax invalid for '{}'", value );
074            return false;
075        }
076
077        byte[] bytes = ( byte[] ) value;
078
079        // The header must be at least 11 bytes long
080        if ( bytes.length < 11 )
081        {
082            LOG.debug( "Syntax invalid for '{}'", value );
083            return false;
084        }
085
086        if ( ( bytes[0] == ( byte ) 0x00FF ) // SOI
087            && ( bytes[1] == ( byte ) 0x00D8 )
088            && ( bytes[2] == ( byte ) 0x00FF ) ) // APP0 or APP1
089        {
090            if ( bytes[3] == (byte)0x00E0 )
091            {
092                // JFIF format
093                if ( ( bytes[6] == 'J' ) // JFIF
094                    && ( bytes[7] == 'F' ) // JFIF
095                    && ( bytes[8] == 'I' ) // JFIF
096                    && ( bytes[9] == 'F' )
097                    && ( bytes[10] == 0x00 ) ) // \0
098                {
099                    LOG.debug( "Syntax valid for '{}'", value );
100                    return true;
101                }
102            }
103            else if ( bytes[3] == (byte)0x00E1 )
104            {
105                // Exif format
106                if ( ( bytes[6] == 'E' )   // Exif
107                    && ( bytes[7] == 'x' ) // Exif
108                    && ( bytes[8] == 'i' ) // Exif
109                    && ( bytes[9] == 'f' ) // Exif
110                    && ( bytes[10] == 0x00 ) ) // \0
111                {
112                    LOG.debug( "Syntax valid for '{}'", value );
113                    return true;
114                }
115            }
116        }
117
118        LOG.debug( "Syntax invalid for '{}'", value );
119        return false;
120    }
121}