View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    * 
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   * 
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License.
18   * 
19   */
20  package org.apache.directory.api.ldap.model.schema.syntaxCheckers;
21  
22  
23  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
24  import org.apache.directory.api.ldap.model.schema.SyntaxChecker;
25  import org.slf4j.Logger;
26  import org.slf4j.LoggerFactory;
27  
28  
29  /**
30   * A SyntaxChecker which verifies that a value is a Jpeg according to RFC 4517.
31   * 
32   * The JFIF (Jpeg File Interchange Format) specify that a jpeg image starts with
33   * the following bytes :
34   * 0xFF 0xD8 (SOI, Start Of Image)
35   * 0xFF 0xE0 (App0 for JFIF) or 0xDD 0xE1 (App1 for Exif)
36   * 0xNN 0xNN (Header length)
37   * "JFIF\0" (JFIF string with an ending \0)
38   * some other bytes which are related to the image.
39   * 
40   * We will check for those 11 bytes, except the length.
41   * 
42   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
43   */
44  @SuppressWarnings("serial")
45  public class JpegSyntaxChecker extends SyntaxChecker
46  {
47      /** A logger for this class */
48      private static final Logger LOG = LoggerFactory.getLogger( JpegSyntaxChecker.class );
49  
50      /**
51       * Creates a new instance of JpegSyntaxChecker.
52       */
53      public JpegSyntaxChecker()
54      {
55          super( SchemaConstants.JPEG_SYNTAX );
56      }
57  
58  
59      /**
60       * {@inheritDoc}
61       */
62      public boolean isValidSyntax( Object value )
63      {
64          if ( value == null )
65          {
66              LOG.debug( "Syntax invalid for 'null'" );
67              return false;
68          }
69  
70          // The value must be a byte array
71          if ( !( value instanceof byte[] ) )
72          {
73              LOG.debug( "Syntax invalid for '{}'", value );
74              return false;
75          }
76  
77          byte[] bytes = ( byte[] ) value;
78  
79          // The header must be at least 11 bytes long
80          if ( bytes.length < 11 )
81          {
82              LOG.debug( "Syntax invalid for '{}'", value );
83              return false;
84          }
85  
86          if ( ( bytes[0] == ( byte ) 0x00FF ) // SOI
87              && ( bytes[1] == ( byte ) 0x00D8 )
88              && ( bytes[2] == ( byte ) 0x00FF ) ) // APP0 or APP1
89          {
90              if ( bytes[3] == (byte)0x00E0 )
91              {
92                  // JFIF format
93                  if ( ( bytes[6] == 'J' ) // JFIF
94                      && ( bytes[7] == 'F' ) // JFIF
95                      && ( bytes[8] == 'I' ) // JFIF
96                      && ( bytes[9] == 'F' )
97                      && ( bytes[10] == 0x00 ) ) // \0
98                  {
99                      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 }