Repository navigation
Expand file tree
/
Copy pathTransactionLog.java
More file actions
143 lines (118 loc) · 4.74 KB
/
Copy pathTransactionLog.java
File metadata and controls
143 lines (118 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2010 sasc
*
* 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 sasc.emv;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;
import sasc.iso7816.TagAndLength;
import sasc.util.Log;
import sasc.util.Util;
/**
*
* @author sasc
*/
public class TransactionLog {
private LogFormat logFormat;
private List<Record> logRecords = new ArrayList<Record>();
private boolean isProcessed = false;
public TransactionLog(LogFormat logFormat) {
this.logFormat = logFormat;
}
public LogFormat getLogFormat() {
return logFormat;
}
public void addRecord(byte[] logRecord) {
if (logRecord != null) {
if (logRecord.length == logFormat.getRecordLength()) {
logRecords.add(new Record(logRecord));
} else {
Log.debug("logRecord length (" + logRecord.length + ") does not match logFormat length (" + logFormat.getRecordLength() + ")");
}
}
}
public boolean isEmpty() {
return logRecords.isEmpty();
}
public boolean isProcessed() {
return isProcessed;
}
public void setProcessed() {
this.isProcessed = true;
}
public List<Record> getRecords() {
return logRecords;
}
@Override
public String toString() {
StringWriter sw = new StringWriter();
dump(new PrintWriter(sw), 0);
return sw.toString();
}
public void dump(PrintWriter pw, int indent) {
pw.println(Util.getSpaces(indent) + "Transaction Log:");
String indentStr = Util.getSpaces(indent + Log.INDENT_SIZE);
logFormat.dump(pw, indent + Log.INDENT_SIZE);
pw.println(indentStr + "Log Record(s):");
if (!logRecords.isEmpty()) {
for (Record record : logRecords) {
//TODO format record
record.dump(pw, indent + Log.INDENT_SIZE*2);
}
} else {
pw.println(Util.getSpaces(indent+Log.INDENT_SIZE*2) + "No Log Records found");
}
}
public class Record {
private byte[] recordData;
private Record(byte[] recordData) {
this.recordData = recordData;
}
@Override
public String toString() {
StringWriter sw = new StringWriter();
dump(new PrintWriter(sw), 0);
return sw.toString();
}
public void dump(PrintWriter pw, int indent) {
pw.println(Util.getSpaces(indent) + "Record:");
String indentStr = Util.getSpaces(indent + 3);
int offset = 0;
for (TagAndLength tagAndLength : logFormat.getTagAndLengthList()) {
byte[] value = new byte[tagAndLength.getLength()];
System.arraycopy(recordData, offset, value, 0, value.length);
pw.println(indentStr + tagAndLength.getTag().getName() + ": " + Util.prettyPrintHexNoWrap(value));
offset += value.length;
}
}
}
public static void main(String[] args) {
LogFormat logFormat = new LogFormat(Util.fromHexString("9f 02 06 9f 27 01 9f 1a 02 5f 2a 02 9a 03 9c 01"));
TransactionLog tl = new TransactionLog(logFormat);
tl.addRecord(Util.fromHexString("00 00 00 00 38 70 40 02 50 09 78 12 04 21 00"));
tl.addRecord(Util.fromHexString("00 00 00 00 21 90 40 02 50 09 78 12 04 17 00"));
tl.addRecord(Util.fromHexString("00 00 00 00 39 20 40 02 50 09 78 12 04 13 00"));
tl.addRecord(Util.fromHexString("00 00 00 01 30 00 40 02 50 09 78 12 04 07 00"));
tl.addRecord(Util.fromHexString("00 00 00 00 61 24 40 02 50 09 78 12 04 05 00"));
tl.addRecord(Util.fromHexString("00 00 00 00 06 73 40 02 50 09 78 12 04 03 00"));
tl.addRecord(Util.fromHexString("00 00 00 00 15 20 40 02 50 09 78 12 03 28 00"));
tl.addRecord(Util.fromHexString("00 00 00 00 17 50 40 02 50 09 78 12 03 26 00"));
tl.addRecord(Util.fromHexString("00 00 00 00 09 80 40 02 50 09 78 12 03 26 00"));
tl.addRecord(Util.fromHexString("00 00 00 00 61 24 40 02 50 09 78 12 03 20 00"));
tl.addRecord(Util.fromHexString("00 00 00 01 00 00 40 02 50 09 78 12 02 28 00"));
System.out.println(tl);
}
}