Project

General

Profile

« Previous | Next » 

Revision ad4531a4

Added by Neels Hofmeyr about 4 years ago

add Bytes.java

View differences:

sim-applet/Makefile
6 6
PACKAGE_NAME    = org.osmocom.IMSIPseudo
7 7
PACKAGE_VERSION = 1.0
8 8

  
9
SOURCES = src/org/osmocom/IMSIPseudo/MobileIdentity.java src/org/osmocom/IMSIPseudo/IMSIPseudo.java
9
SOURCES = \
10
	  src/org/osmocom/IMSIPseudo/Bytes.java \
11
	  src/org/osmocom/IMSIPseudo/MobileIdentity.java \
12
	  src/org/osmocom/IMSIPseudo/IMSIPseudo.java \
13
	  $(NULL)
10 14

  
11 15
CAP_FILE = build/javacard/org/osmocom/IMSIPseudo/javacard/IMSIPseudo.cap
12 16

  
......
42 46
.PHONY: test
43 47
test:
44 48
	mkdir -p ./test/classes
49
	javac -target 1.1 -source 1.3 -classpath test/classes -g -d ./test/classes src/org/osmocom/IMSIPseudo/Bytes.java
45 50
	javac -target 1.1 -source 1.3 -classpath test/classes -g -d ./test/classes src/org/osmocom/IMSIPseudo/MobileIdentity.java
46 51
	javac -target 1.1 -source 1.3 -classpath test/classes -g -d ./test/classes src/org/osmocom/IMSIPseudo/Test.java
47 52
	java -classpath test/classes org.osmocom.IMSIPseudo.Test
sim-applet/src/org/osmocom/IMSIPseudo/Bytes.java
1
/* Copyright 2020 sysmocom s.f.m.c. GmbH
2
 * SPDX-License-Identifier: Apache-2.0 */
3
package org.osmocom.IMSIPseudo;
4

  
5
public class Bytes {
6
	public static byte nibble2hex(byte nibble)
7
	{
8
		nibble = (byte)(nibble & 0xf);
9
		if (nibble < 0xa)
10
			return (byte)('0' + nibble);
11
		else
12
			return (byte)('a' + nibble - 0xa);
13
	}
14

  
15
	public static byte[] hexdump(byte data[])
16
	{
17
		byte res[] = new byte[(byte)(data.length*2)];
18
		for (byte i = 0; i < data.length; i++) {
19
			res[(byte)(i*2)] = nibble2hex((byte)(data[i] >> 4));
20
			res[(byte)(i*2 + 1)] = nibble2hex(data[i]);
21
		}
22
		return res;
23
	}
24

  
25
	public static boolean equals(byte a[], byte b[])
26
	{
27
		if (a.length != b.length)
28
			return false;
29
		for (short i = 0; i < (short)a.length; i++) {
30
			if (a[i] != b[i])
31
				return false;
32
		}
33
		return true;
34
	}
35

  
36
	public static boolean isDigit(byte digits[])
37
	{
38
		for (short i = 0; i < (short)digits.length; i++) {
39
			if (digits[i] < '0' || digits[i] > '9')
40
				return false;
41
		}
42
		return true;
43
	}
44

  
45
	public static byte[] toStr(byte byte_nr)
46
	{
47
		byte str[];
48
		short nr = byte_nr;
49
		byte d;
50
		byte l = 0;
51
		if (nr < 0) {
52
			l = 1;
53
			nr = (short)-nr;
54
		}
55

  
56
		if (nr > 99) {
57
			l += 3;
58
			d = 100;
59
		}
60
		else if (nr > 9) {
61
			l += 2;
62
			d = 10;
63
		}
64
		else {
65
			str = new byte[1];
66
			l += 1;
67
			d = 1;
68
		}
69

  
70
		byte i = 0;
71
		str = new byte[l];
72
		if (byte_nr < 0)
73
			str[i++] = '-';
74

  
75
		while (d > 0) {
76
			str[i++] = (byte)('0' + (nr / d));
77
			nr %= d;
78
			d /= 10;
79
		}
80
		return str;
81
	}
82
}
sim-applet/src/org/osmocom/IMSIPseudo/IMSIPseudo.java
129 129
		showMsg(msg);
130 130
	}
131 131

  
132
	private byte nibble2hex(byte nibble)
133
	{
134
		nibble = (byte)(nibble & 0xf);
135
		if (nibble < 0xa)
136
			return (byte)('0' + nibble);
137
		else
138
			return (byte)('a' + nibble - 0xa);
139
	}
140

  
141
	private byte[] hexdump(byte data[])
142
	{
143
		byte res[] = new byte[(byte)(data.length*2)];
144
		for (byte i = 0; i < data.length; i++) {
145
			res[(byte)(i*2)] = nibble2hex((byte)(data[i] >> 4));
146
			res[(byte)(i*2 + 1)] = nibble2hex(data[i]);
147
		}
148
		return res;
149
	}
150 132

  
151 133
	private void showIMSI() {
152 134
		/* 3GPP TS 31.102 4.2.2: IMSI */

Also available in: Unified diff

Add picture from clipboard (Maximum size: 48.8 MB)