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.i18n.I18n;
024import org.apache.directory.api.ldap.model.constants.SchemaConstants;
025import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
026
027
028/**
029 * A SyntaxChecker which verifies that a value is a Jpeg according to RFC 4517.
030 * <p>
031 * The JFIF (Jpeg File Interchange Format) specify that a jpeg image starts with
032 * the following bytes :
033 * <pre>
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 * <pre>
040 * 
041 * We will check for those 11 bytes, except the length.
042 * 
043 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
044 */
045@SuppressWarnings("serial")
046public final class JpegSyntaxChecker extends SyntaxChecker
047{
048    /**
049     * A static instance of JpegSyntaxChecker
050     */
051    public static final JpegSyntaxChecker INSTANCE = new JpegSyntaxChecker( SchemaConstants.JPEG_SYNTAX );
052    
053    /**
054     * A static Builder for this class
055     */
056    public static final class Builder extends SCBuilder<JpegSyntaxChecker>
057    {
058        /**
059         * The Builder constructor
060         */
061        private Builder()
062        {
063            super( SchemaConstants.JPEG_SYNTAX );
064        }
065        
066        
067        /**
068         * Create a new instance of JpegSyntaxChecker
069         * @return A new instance of JpegSyntaxChecker
070         */
071        @Override
072        public JpegSyntaxChecker build()
073        {
074            return new JpegSyntaxChecker( oid );
075        }
076    }
077
078    
079    /**
080     * Creates a new instance of JpegSyntaxChecker.
081     * 
082     * @param oid The OID to use for this SyntaxChecker
083     */
084    private JpegSyntaxChecker( String oid )
085    {
086        super( oid );
087    }
088
089    
090    /**
091     * @return An instance of the Builder for this class
092     */
093    public static Builder builder()
094    {
095        return new Builder();
096    }
097
098
099    /**
100     * {@inheritDoc}
101     */
102    @Override
103    public boolean isValidSyntax( Object value )
104    {
105        if ( value == null )
106        {
107            if ( LOG.isDebugEnabled() )
108            {
109                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, "null" ) );
110            }
111            
112            return false;
113        }
114
115        // The value must be a byte array
116        if ( !( value instanceof byte[] ) )
117        {
118            if ( LOG.isDebugEnabled() )
119            {
120                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
121            }
122            
123            return false;
124        }
125
126        byte[] bytes = ( byte[] ) value;
127
128        // The header must be at least 11 bytes long
129        if ( bytes.length < 11 )
130        {
131            if ( LOG.isDebugEnabled() )
132            {
133                LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
134            }
135            
136            return false;
137        }
138
139        // SOI or APP0 or APP1 format
140        if ( ( bytes[0] == ( byte ) 0x00FF )
141            && ( bytes[1] == ( byte ) 0x00D8 )
142            && ( bytes[2] == ( byte ) 0x00FF ) )
143        {
144            // JFIF format
145            if ( ( bytes[3] == ( byte ) 0x00E0 )
146                && ( bytes[6] == 'J' )
147                && ( bytes[7] == 'F' )
148                && ( bytes[8] == 'I' )
149                && ( bytes[9] == 'F' )
150                && ( bytes[10] == 0x00 ) )
151            {
152                if ( LOG.isDebugEnabled() )
153                {
154                    LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
155                }
156                
157                return true;
158            }
159        // EXIF Format
160            else if ( ( bytes[3] == ( byte ) 0x00E1 ) 
161                    && ( bytes[6] == 'E' )
162                    && ( bytes[7] == 'x' )
163                    && ( bytes[8] == 'i' )
164                    && ( bytes[9] == 'f' )
165                    && ( bytes[10] == 0x00 ) )
166            {
167                if ( LOG.isDebugEnabled() )
168                {
169                    LOG.debug( I18n.msg( I18n.MSG_04489_SYNTAX_VALID, value ) );
170                }
171                
172                return true;
173            }
174        }
175
176        if ( LOG.isDebugEnabled() )
177        {
178            LOG.debug( I18n.err( I18n.ERR_04488_SYNTAX_INVALID, value ) );
179        }
180        
181        return false;
182    }
183}