add Bytes.java

This commit is contained in:
Neels Hofmeyr 2020-02-25 03:19:26 +01:00
parent 583bfecc34
commit ad4531a414
3 changed files with 88 additions and 19 deletions

View File

@ -6,7 +6,11 @@ PACKAGE_AID = 0xd0:0x70:0x02:0xCA:0x44:0x90:0x01
PACKAGE_NAME = org.osmocom.IMSIPseudo
PACKAGE_VERSION = 1.0
SOURCES = src/org/osmocom/IMSIPseudo/MobileIdentity.java src/org/osmocom/IMSIPseudo/IMSIPseudo.java
SOURCES = \
src/org/osmocom/IMSIPseudo/Bytes.java \
src/org/osmocom/IMSIPseudo/MobileIdentity.java \
src/org/osmocom/IMSIPseudo/IMSIPseudo.java \
$(NULL)
CAP_FILE = build/javacard/org/osmocom/IMSIPseudo/javacard/IMSIPseudo.cap
@ -42,6 +46,7 @@ delete: remove
.PHONY: test
test:
mkdir -p ./test/classes
javac -target 1.1 -source 1.3 -classpath test/classes -g -d ./test/classes src/org/osmocom/IMSIPseudo/Bytes.java
javac -target 1.1 -source 1.3 -classpath test/classes -g -d ./test/classes src/org/osmocom/IMSIPseudo/MobileIdentity.java
javac -target 1.1 -source 1.3 -classpath test/classes -g -d ./test/classes src/org/osmocom/IMSIPseudo/Test.java
java -classpath test/classes org.osmocom.IMSIPseudo.Test

View File

@ -0,0 +1,82 @@
/* Copyright 2020 sysmocom s.f.m.c. GmbH
* SPDX-License-Identifier: Apache-2.0 */
package org.osmocom.IMSIPseudo;
public class Bytes {
public static byte nibble2hex(byte nibble)
{
nibble = (byte)(nibble & 0xf);
if (nibble < 0xa)
return (byte)('0' + nibble);
else
return (byte)('a' + nibble - 0xa);
}
public static byte[] hexdump(byte data[])
{
byte res[] = new byte[(byte)(data.length*2)];
for (byte i = 0; i < data.length; i++) {
res[(byte)(i*2)] = nibble2hex((byte)(data[i] >> 4));
res[(byte)(i*2 + 1)] = nibble2hex(data[i]);
}
return res;
}
public static boolean equals(byte a[], byte b[])
{
if (a.length != b.length)
return false;
for (short i = 0; i < (short)a.length; i++) {
if (a[i] != b[i])
return false;
}
return true;
}
public static boolean isDigit(byte digits[])
{
for (short i = 0; i < (short)digits.length; i++) {
if (digits[i] < '0' || digits[i] > '9')
return false;
}
return true;
}
public static byte[] toStr(byte byte_nr)
{
byte str[];
short nr = byte_nr;
byte d;
byte l = 0;
if (nr < 0) {
l = 1;
nr = (short)-nr;
}
if (nr > 99) {
l += 3;
d = 100;
}
else if (nr > 9) {
l += 2;
d = 10;
}
else {
str = new byte[1];
l += 1;
d = 1;
}
byte i = 0;
str = new byte[l];
if (byte_nr < 0)
str[i++] = '-';
while (d > 0) {
str[i++] = (byte)('0' + (nr / d));
nr %= d;
d /= 10;
}
return str;
}
}

View File

@ -129,24 +129,6 @@ public class IMSIPseudo extends Applet implements ToolkitInterface, ToolkitConst
showMsg(msg);
}
private byte nibble2hex(byte nibble)
{
nibble = (byte)(nibble & 0xf);
if (nibble < 0xa)
return (byte)('0' + nibble);
else
return (byte)('a' + nibble - 0xa);
}
private byte[] hexdump(byte data[])
{
byte res[] = new byte[(byte)(data.length*2)];
for (byte i = 0; i < data.length; i++) {
res[(byte)(i*2)] = nibble2hex((byte)(data[i] >> 4));
res[(byte)(i*2 + 1)] = nibble2hex(data[i]);
}
return res;
}
private void showIMSI() {
/* 3GPP TS 31.102 4.2.2: IMSI */