/*
 * Copyright (C) 2013 The ChameleonOS Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.germainz.identiconizer.identicons;

import java.io.UnsupportedEncodingException;
import java.util.Arrays;

public class IdenticonUtils {

    private static final byte[] PNG_HEADER = new byte[] { (byte) 137, (byte) 80, (byte) 78,
            (byte) 71, (byte) 13, (byte) 10, (byte) 26, (byte) 10 };

    public static boolean isIdenticon(byte[] data) {
        if (data == null || !isPngFormat(data))
            return false;

        byte[] tag = Arrays.copyOfRange(data, data.length - (Identicon.IDENTICON_MARKER.length() + 1), data.length - 1);
        String tagString;
        try {
            tagString = new String(tag, "ISO-8859-1");
        } catch (UnsupportedEncodingException e) {
            return false;
        }

        return Identicon.IDENTICON_MARKER.equals(tagString);
    }

    private static boolean isPngFormat(byte[] data) {
        if (data.length < PNG_HEADER.length)
            return false;

        for (int i = 0; i < PNG_HEADER.length; i++) {
            if (data[i] != PNG_HEADER[i])
                return false;
        }

        return true;
    }
}

@@ -768,16 +768,99 @@
 tils %7B%0A%0A
+    private static final byte%5B%5D JPG_HEADER = new byte%5B%5D%7B(byte) 0xFF, (byte) 0xD8%7D;%0A
     priv
@@ -910,11 +910,9 @@
 te%5B%5D
- %7B 
+%7B
 (byt
@@ -1010,262 +1010,748 @@
 ) 10
- %7D;%0A%0A    public static boolean isIdenticon(byte%5B%5D data) %7B%0A        if (data == null %7C%7C !isPngFormat(data))%0A            return false;%0A%0A        byte%5B%5D tag = Arrays.copyOfRange(data, data.length - (Identicon.IDENTICON_MARKER.length() + 1), data.length - 1
+%7D;%0A    private static final int JPG_FORMAT = 0;%0A    private static final int PNG_FORMAT = 1;%0A    private static final int OTHER_FORMAT = 2;%0A%0A    public static boolean isIdenticon(byte%5B%5D data) %7B%0A        int format = getDataFormat(data);%0A        if (data == null %7C%7C format == OTHER_FORMAT)%0A            return false;%0A%0A        int start, end;%0A        String charSet;%0A        if (format == JPG_FORMAT) %7B%0A            start = data.length - 18;%0A            end = data.length -2;%0A            charSet = %22US-ASCII%22;%0A        %7D else %7B%0A            start = data.length - (Identicon.IDENTICON_MARKER.length() + 1);%0A            end = data.length - 1;%0A            charSet = %22ISO-8859-1%22;%0A        %7D%0A        byte%5B%5D tag = Arrays.copyOfRange(data, start, end
 );%0A
+%0A
     
@@ -1826,28 +1826,23 @@
 ng(tag, 
-%22ISO-8859-1%22
+charSet
 );%0A     
@@ -2015,21 +2015,19 @@
 tic 
-boolean isPng
+int getData
 Form
@@ -2036,32 +2036,62 @@
 (byte%5B%5D data) %7B%0A
+        boolean isPng = true;%0A
         if (data
@@ -2118,16 +2118,18 @@
 .length)
+ %7B
 %0A       
@@ -2129,38 +2129,39 @@
             
-return
+isPng =
  false;%0A
 %0A        for
@@ -2140,33 +2140,53 @@
  isPng = false;%0A
-%0A
+        %7D else %7B%0A    
         for (int
@@ -2227,32 +2227,36 @@
 ) %7B%0A            
+    
 if (data%5Bi%5D != P
@@ -2289,52 +2289,464 @@
     
+  
-return false;%0A        %7D%0A%0A        return true
+  isPng = false;%0A            %7D%0A        %7D%0A        if (isPng)%0A            return PNG_FORMAT;%0A%0A        boolean isJpg = true;%0A        if (data.length %3C JPG_HEADER.length) %7B%0A            isJpg = false;%0A        %7D else %7B%0A            for (int i = 0; i %3C JPG_HEADER.length; i++) %7B%0A                if (data%5Bi%5D != JPG_HEADER%5Bi%5D)%0A                    isJpg = false;%0A            %7D%0A        %7D%0A        if (isJpg)%0A            return JPG_FORMAT;%0A%0A        return OTHER_FORMAT
 ;%0A  

Maintain compatibility with older versions (JPG format instead of PNG.)