package com.cossacklabs.themis;

import java.io.UnsupportedEncodingException;

public class SecureCompare {
	
	static {
		System.loadLibrary("themis_jni");
	}
	
	public enum CompareResult {
		NOT_READY,
		NO_MATCH,
		MATCH
	}
	
	public enum ProtocolResult {
		NO_OUTPUT,
		SEND_TO_PEER
	}
	
	static final String CHARSET = "UTF-16";
	
	private long nativeCtx = 0;
	
	native long create();
	native void destroy();
	
	public SecureCompare() throws SecureCompareException {
		
		nativeCtx = create();
		
		if (0 == nativeCtx) {
			throw new SecureCompareException();
		}
	}
	
	public SecureCompare(byte[] secret) throws SecureCompareException {
		
		this();
		appendSecret(secret);
		
	}
	
	public SecureCompare(String password) throws UnsupportedEncodingException, SecureCompareException {
		this(password.getBytes(CHARSET));
	}
	
	public void close() {
		if (0 != nativeCtx) {
			destroy();
		}
	}
	
	@Override
	protected void finalize() {
		close();
	}
	
	native int jniAppend(byte[] secret);
	
	public void appendSecret(byte[] secretData) throws SecureCompareException {
		if (0 != jniAppend(secretData)) {
			throw new SecureCompareException();
		}
	}
	
	native byte[] jniBegin();
	
	static CompareResult parseResult(int result) throws SecureCompareException {
		switch (result) {
		case SCOMPARE_NOT_READY():
			return CompareResult.NOT_READY;
		case SCOMPARE_NO_MATCH():
			return CompareResult.NO_MATCH;
		case SCOMPARE_MATCH():
			return CompareResult.MATCH;
		}
		
		throw new SecureCompareException();
	}
	
	public byte[] begin() throws SecureCompareException {
		byte[] compareData = jniBegin();
		
		if (null == compareData) {
			throw new SecureCompareException();
		}
		
		return compareData;
	}
	
	native byte[] jniProceed(byte[] compareData);
	
	public byte[] proceed(byte[] compareData) throws SecureCompareException {
		return jniProceed(compareData);
	}
	
	native int jniGetResult();
	
	public CompareResult getResult() throws SecureCompareException {
		return parseResult(jniGetResult());
	}

	static native int SCOMPARE_MATCH();
	static native int SCOMPARE_NO_MATCH();
	static native int SCOMPARE_NOT_READY();
}
@@ -1261,22 +1261,18 @@
 %7B%0A%09%09
-switch
+if
  (result
 ) %7B%0A
@@ -1271,18 +1271,11 @@
 sult
-) %7B%0A%09%09case
+ ==
  SCO
@@ -1291,21 +1291,26 @@
 _READY()
-:%0A%09%09%09
+) %7B%0A%09%09    
 return C
@@ -1331,28 +1331,43 @@
 OT_READY;%0A%09%09
-case
+%7Delse if (result ==
  SCOMPARE_NO
@@ -1366,37 +1366,41 @@
 MPARE_NO_MATCH()
-:%0A%09%09%09
+)%7B%0A%09%09    
 return CompareRe
@@ -1420,12 +1420,27 @@
 ;%0A%09%09
-case
+%7Delse if (result ==
  SCO
@@ -1456,13 +1456,17 @@
 CH()
-:%0A%09%09%09
+)%7B%0A%09%09    
 retu
@@ -1493,19 +1493,16 @@
 CH;%0A%09%09%7D%0A
-%09%09%0A
 %09%09throw 

Add java corrections