/****************************************************************************
 * Copyright (C) 2016 ecsec GmbH.
 * All rights reserved.
 * Contact: ecsec GmbH (info@ecsec.de)
 *
 * This file is part of the Open eCard App.
 *
 * GNU General Public License Usage
 * This file may be used under the terms of the GNU General Public
 * License version 3.0 as published by the Free Software Foundation
 * and appearing in the file LICENSE.GPL included in the packaging of
 * this file. Please review the following information to ensure the
 * GNU General Public License version 3.0 requirements will be met:
 * http://www.gnu.org/copyleft/gpl.html.
 *
 * Other Usage
 * Alternatively, this file may be used in accordance with the terms
 * and conditions contained in a signed written agreement between
 * you and ecsec GmbH.
 *
 ***************************************************************************/

package org.openecard.mdlw.sal;

import java.nio.charset.StandardCharsets;
import javax.annotation.Nullable;
import org.openecard.mdlw.sal.struct.CkAttribute;


/**
 *
 * @author Tobias Wich
 */
public class AttributeUtils {

    @Nullable
    public static byte[] getBytes(CkAttribute raw) {
	int dataLen = raw.getLength().intValue();
	if (dataLen > 0) {
	    return raw.getData().getByteArray(0, dataLen);
	} else {
	    return new byte[0];
	}
    }

    @Nullable
    public static String getString(CkAttribute raw) {
	int dataLen = raw.getLength().intValue();
	if (dataLen > 0) {
	    byte[] rawData = raw.getData().getByteArray(0, dataLen);
	    return new String(rawData, StandardCharsets.UTF_8).trim();
	} else {
	    return null;
	}
    }

    @Nullable
    public static Boolean getBool(CkAttribute raw) {
	int dataLen = raw.getLength().intValue();
	if (dataLen > 0) {
	    return raw.getData().getByte(0) == 0 ? Boolean.FALSE : Boolean.TRUE;
	} else {
	    return null;
	}
    }

    @Nullable
    public static long getLong(CkAttribute raw) {
	int dataLen = raw.getLength().intValue();
	if (dataLen > 0) {
	    if (dataLen == 4) {
		return raw.getData().getInt(0);
	    } else {
		return raw.getData().getLong(0);
	    }
	} else {
	    return -1;
	}
    }

    @Nullable
    public static long[] getLongs(CkAttribute raw) {
	int dataLen = raw.getLength().intValue();
	if (dataLen > 0) {
	    return raw.getData().getLongArray(0, dataLen / 8);
	} else {
	    return new long[0];
	}
    }

}

@@ -927,16 +927,75 @@
 w.sal;%0A%0A
+import com.sun.jna.NativeLong;%0Aimport com.sun.jna.Pointer;%0A
 import j
@@ -1935,32 +1935,173 @@
 null;%0A%09%7D%0A    %7D%0A%0A
+    private static long getLongFromPointer(Pointer p, int offset) %7B%0A%09NativeLong nl = p.getNativeLong(offset);%0A%09return nl.longValue();%0A    %7D%0A%0A
     @Nullable%0A  
@@ -2220,117 +2220,52 @@
     
-if (dataLen == 4) %7B%0A%09%09return raw.getData().getInt(0);%0A%09    %7D else %7B%0A%09%09return raw.getData().getLong(0);%0A%09    %7D
+return getLongFromPointer(raw.getData(), 0);
 %0A%09%7D 
@@ -2412,120 +2412,196 @@
 );%0A%09
-if (dataLen %3E 0) %7B%0A%09    return raw.getData().getLongArray(0, dataLen / 8);%0A%09%7D else %7B%0A%09    return new long%5B0%5D;%0A%09%7D
+long%5B%5D result = new long%5BdataLen / NativeLong.SIZE%5D;%0A%09for (int i=0, o=0; o %3C dataLen; i++, o += NativeLong.SIZE) %7B%0A%09    result%5Bi%5D = getLongFromPointer(raw.getData(), o);%0A%09%7D%0A%09return result;
 %0A   

Fix platform type size bug on Windows systems