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.syntaxes;
021    
022    import org.apache.directory.shared.ldap.constants.SchemaConstants;
023    import org.apache.directory.shared.ldap.schema.AbstractSyntaxChecker;
024    
025    
026    /**
027     * A SyntaxChecker which verifies that a value is a Jpeg according to RFC 4517.
028     * 
029     * The JFIF (Jpeg File Interchange Format) specify that a jpeg image starts with
030     * the following bytes :
031     * 0xFF 0xD8 (SOI, Start Of Image)
032     * 0xFF 0xE0 (App0)
033     * 0xNN 0xNN (Header length)
034     * "JFIF\0" (JFIF string with an ending \0)
035     * some other bytes which are related to the image.
036     * 
037     * We will check for those 11 bytes, except the length.
038     * 
039     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040     * @version $Rev: 437007 $
041     */
042    public class JpegSyntaxChecker extends AbstractSyntaxChecker
043    {
044        /**
045         * 
046         * Creates a new instance of JpegSyntaxChecker.
047         *
048         */
049        public JpegSyntaxChecker()
050        {
051            super( SchemaConstants.JPEG_SYNTAX );
052        }
053        
054        
055        /**
056         * 
057         * Creates a new instance of JpegSyntaxChecker.
058         * 
059         * @param oid the oid to associate with this new SyntaxChecker
060         *
061         */
062        protected JpegSyntaxChecker( String oid )
063        {
064            super( oid );
065        }
066        
067        /* (non-Javadoc)
068         * @see org.apache.directory.shared.ldap.schema.SyntaxChecker#isValidSyntax(java.lang.Object)
069         */
070        public boolean isValidSyntax( Object value )
071        {
072            if ( value == null )
073            {
074                return false;
075            }
076            
077            // The value must be a byte array
078            if ( ! ( value instanceof byte[] ) )
079            {
080                return false;
081            }
082            
083            byte[] bytes = (byte[])value;
084    
085            // The header must be at least 11 bytes long
086            if ( bytes.length < 11 )
087            {
088                return false;
089            }
090    
091            if ( ( bytes[0] == (byte)0x00FF ) && // SOI
092                 ( bytes[1] == (byte)0x00D8 ) &&
093                 ( bytes[2] == (byte)0x00FF ) && // APP0
094                 ( bytes[3] == (byte)0x00E0 ) &&
095                 ( bytes[6] == 'J' ) && // JFIF
096                 ( bytes[7] == 'F' ) && // JFIF
097                 ( bytes[8] == 'I' ) && // JFIF
098                 ( bytes[9] == 'F' ) &&
099                 ( bytes[10] == 0x00 ) ) // \0
100            {
101                // Note : this is not because the header is correct
102                // that the file is a jpeg file. There are much more
103                // elements to check, but we are not writing a jpeg
104                // file checker...
105                return true;
106            }
107    
108            return false;
109        }
110    }