Skip to content

Commit 75c2f1a

Browse files
committed
Refactor ASN1Dump
1 parent da4636f commit 75c2f1a

File tree

2 files changed

+75
-88
lines changed

2 files changed

+75
-88
lines changed

core/src/main/java/org/bouncycastle/asn1/ASN1BitString.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,16 @@ public byte[] getBytes()
311311
return rv;
312312
}
313313

314+
public int getBytesLength()
315+
{
316+
return contents.length - 1;
317+
}
318+
319+
public boolean isOctetAligned()
320+
{
321+
return getPadBits() == 0;
322+
}
323+
314324
public int getPadBits()
315325
{
316326
return contents[0] & 0xFF;

core/src/main/java/org/bouncycastle/asn1/util/ASN1Dump.java

Lines changed: 65 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,18 @@ public class ASN1Dump
5252
*
5353
* @param obj the ASN1Primitive to be dumped out.
5454
*/
55-
static void _dumpAsString(
56-
String indent,
57-
boolean verbose,
58-
ASN1Primitive obj,
59-
StringBuffer buf)
55+
static void _dumpAsString(String indent, boolean verbose, ASN1Primitive obj, StringBuffer buf)
6056
{
6157
String nl = Strings.lineSeparator();
58+
buf.append(indent);
59+
6260
if (obj instanceof ASN1Null)
6361
{
64-
buf.append(indent);
6562
buf.append("NULL");
6663
buf.append(nl);
6764
}
6865
else if (obj instanceof ASN1Sequence)
6966
{
70-
buf.append(indent);
7167
if (obj instanceof BERSequence)
7268
{
7369
buf.append("BER Sequence");
@@ -92,7 +88,6 @@ else if (obj instanceof DERSequence)
9288
}
9389
else if (obj instanceof ASN1Set)
9490
{
95-
buf.append(indent);
9691
if (obj instanceof BERSet)
9792
{
9893
buf.append("BER Set");
@@ -117,7 +112,6 @@ else if (obj instanceof DERSet)
117112
}
118113
else if (obj instanceof ASN1TaggedObject)
119114
{
120-
buf.append(indent);
121115
if (obj instanceof BERTaggedObject)
122116
{
123117
buf.append("BER Tagged ");
@@ -137,7 +131,7 @@ else if (obj instanceof DERTaggedObject)
137131

138132
if (!o.isExplicit())
139133
{
140-
buf.append(" IMPLICIT ");
134+
buf.append(" IMPLICIT");
141135
}
142136

143137
buf.append(nl);
@@ -146,130 +140,124 @@ else if (obj instanceof DERTaggedObject)
146140

147141
_dumpAsString(baseIndent, verbose, o.getBaseObject().toASN1Primitive(), buf);
148142
}
143+
else if (obj instanceof ASN1ObjectIdentifier)
144+
{
145+
buf.append("ObjectIdentifier(" + ((ASN1ObjectIdentifier)obj).getId() + ")" + nl);
146+
}
147+
else if (obj instanceof ASN1RelativeOID)
148+
{
149+
buf.append("RelativeOID(" + ((ASN1RelativeOID)obj).getId() + ")" + nl);
150+
}
151+
else if (obj instanceof ASN1Boolean)
152+
{
153+
buf.append("Boolean(" + ((ASN1Boolean)obj).isTrue() + ")" + nl);
154+
}
155+
else if (obj instanceof ASN1Integer)
156+
{
157+
buf.append("Integer(" + ((ASN1Integer)obj).getValue() + ")" + nl);
158+
}
149159
else if (obj instanceof ASN1OctetString)
150160
{
151161
ASN1OctetString oct = (ASN1OctetString)obj;
152162

153163
if (obj instanceof BEROctetString)
154164
{
155-
buf.append(indent + "BER Constructed Octet String" + "[" + oct.getOctets().length + "] ");
165+
buf.append("BER Constructed Octet String[");
156166
}
157167
else
158168
{
159-
buf.append(indent + "DER Octet String" + "[" + oct.getOctets().length + "] ");
169+
buf.append("DER Octet String[");
160170
}
171+
172+
buf.append(oct.getOctetsLength() + "]" + nl);
173+
161174
if (verbose)
162175
{
163-
buf.append(dumpBinaryDataAsString(indent, oct.getOctets()));
164-
}
165-
else
166-
{
167-
buf.append(nl);
176+
dumpBinaryDataAsString(buf, indent, oct.getOctets());
168177
}
169178
}
170-
else if (obj instanceof ASN1ObjectIdentifier)
171-
{
172-
buf.append(indent + "ObjectIdentifier(" + ((ASN1ObjectIdentifier)obj).getId() + ")" + nl);
173-
}
174-
else if (obj instanceof ASN1RelativeOID)
175-
{
176-
buf.append(indent + "RelativeOID(" + ((ASN1RelativeOID)obj).getId() + ")" + nl);
177-
}
178-
else if (obj instanceof ASN1Boolean)
179-
{
180-
buf.append(indent + "Boolean(" + ((ASN1Boolean)obj).isTrue() + ")" + nl);
181-
}
182-
else if (obj instanceof ASN1Integer)
183-
{
184-
buf.append(indent + "Integer(" + ((ASN1Integer)obj).getValue() + ")" + nl);
185-
}
186179
else if (obj instanceof ASN1BitString)
187180
{
188181
ASN1BitString bitString = (ASN1BitString)obj;
189182

190-
byte[] bytes = bitString.getBytes();
191-
int padBits = bitString.getPadBits();
192-
193183
if (bitString instanceof DERBitString)
194184
{
195-
buf.append(indent + "DER Bit String" + "[" + bytes.length + ", " + padBits + "] ");
185+
buf.append("DER Bit String[");
196186
}
197187
else if (bitString instanceof DLBitString)
198188
{
199-
buf.append(indent + "DL Bit String" + "[" + bytes.length + ", " + padBits + "] ");
189+
buf.append("DL Bit String[");
200190
}
201191
else
202192
{
203-
buf.append(indent + "BER Bit String" + "[" + bytes.length + ", " + padBits + "] ");
193+
buf.append("BER Bit String[");
204194
}
205195

196+
buf.append(bitString.getBytesLength() + ", " + bitString.getPadBits() + "]" + nl);
197+
206198
if (verbose)
207199
{
208-
buf.append(dumpBinaryDataAsString(indent, bytes));
209-
}
210-
else
211-
{
212-
buf.append(nl);
200+
dumpBinaryDataAsString(buf, indent, bitString.getBytes());
213201
}
214202
}
215203
else if (obj instanceof ASN1IA5String)
216204
{
217-
buf.append(indent + "IA5String(" + ((ASN1IA5String)obj).getString() + ") " + nl);
205+
buf.append("IA5String(" + ((ASN1IA5String)obj).getString() + ") " + nl);
218206
}
219207
else if (obj instanceof ASN1UTF8String)
220208
{
221-
buf.append(indent + "UTF8String(" + ((ASN1UTF8String)obj).getString() + ") " + nl);
209+
buf.append("UTF8String(" + ((ASN1UTF8String)obj).getString() + ") " + nl);
222210
}
223211
else if (obj instanceof ASN1NumericString)
224212
{
225-
buf.append(indent + "NumericString(" + ((ASN1NumericString)obj).getString() + ") " + nl);
213+
buf.append("NumericString(" + ((ASN1NumericString)obj).getString() + ") " + nl);
226214
}
227215
else if (obj instanceof ASN1PrintableString)
228216
{
229-
buf.append(indent + "PrintableString(" + ((ASN1PrintableString)obj).getString() + ") " + nl);
217+
buf.append("PrintableString(" + ((ASN1PrintableString)obj).getString() + ") " + nl);
230218
}
231219
else if (obj instanceof ASN1VisibleString)
232220
{
233-
buf.append(indent + "VisibleString(" + ((ASN1VisibleString)obj).getString() + ") " + nl);
221+
buf.append("VisibleString(" + ((ASN1VisibleString)obj).getString() + ") " + nl);
234222
}
235223
else if (obj instanceof ASN1BMPString)
236224
{
237-
buf.append(indent + "BMPString(" + ((ASN1BMPString)obj).getString() + ") " + nl);
225+
buf.append("BMPString(" + ((ASN1BMPString)obj).getString() + ") " + nl);
238226
}
239227
else if (obj instanceof ASN1T61String)
240228
{
241-
buf.append(indent + "T61String(" + ((ASN1T61String)obj).getString() + ") " + nl);
229+
buf.append("T61String(" + ((ASN1T61String)obj).getString() + ") " + nl);
242230
}
243231
else if (obj instanceof ASN1GraphicString)
244232
{
245-
buf.append(indent + "GraphicString(" + ((ASN1GraphicString)obj).getString() + ") " + nl);
233+
buf.append("GraphicString(" + ((ASN1GraphicString)obj).getString() + ") " + nl);
246234
}
247235
else if (obj instanceof ASN1VideotexString)
248236
{
249-
buf.append(indent + "VideotexString(" + ((ASN1VideotexString)obj).getString() + ") " + nl);
237+
buf.append("VideotexString(" + ((ASN1VideotexString)obj).getString() + ") " + nl);
250238
}
251239
else if (obj instanceof ASN1UTCTime)
252240
{
253-
buf.append(indent + "UTCTime(" + ((ASN1UTCTime)obj).getTime() + ") " + nl);
241+
buf.append("UTCTime(" + ((ASN1UTCTime)obj).getTime() + ") " + nl);
254242
}
255243
else if (obj instanceof ASN1GeneralizedTime)
256244
{
257-
buf.append(indent + "GeneralizedTime(" + ((ASN1GeneralizedTime)obj).getTime() + ") " + nl);
245+
buf.append("GeneralizedTime(" + ((ASN1GeneralizedTime)obj).getTime() + ") " + nl);
258246
}
259247
else if (obj instanceof ASN1Enumerated)
260248
{
261249
ASN1Enumerated en = (ASN1Enumerated) obj;
262-
buf.append(indent + "DER Enumerated(" + en.getValue() + ")" + nl);
250+
buf.append("DER Enumerated(" + en.getValue() + ")" + nl);
263251
}
264252
else if (obj instanceof ASN1ObjectDescriptor)
265253
{
266254
ASN1ObjectDescriptor od = (ASN1ObjectDescriptor)obj;
267-
buf.append(indent + "ObjectDescriptor(" + od.getBaseGraphicString().getString() + ") " + nl);
255+
buf.append("ObjectDescriptor(" + od.getBaseGraphicString().getString() + ") " + nl);
268256
}
269257
else if (obj instanceof ASN1External)
270258
{
271259
ASN1External ext = (ASN1External) obj;
272-
buf.append(indent + "External " + nl);
260+
buf.append("External " + nl);
273261
String tab = indent + TAB;
274262
if (ext.getDirectReference() != null)
275263
{
@@ -288,7 +276,7 @@ else if (obj instanceof ASN1External)
288276
}
289277
else
290278
{
291-
buf.append(indent + obj.toString() + nl);
279+
buf.append(obj.toString() + nl);
292280
}
293281
}
294282

@@ -334,53 +322,42 @@ else if (obj instanceof ASN1Encodable)
334322
return buf.toString();
335323
}
336324

337-
private static String dumpBinaryDataAsString(String indent, byte[] bytes)
325+
private static void dumpBinaryDataAsString(StringBuffer buf, String indent, byte[] bytes)
338326
{
327+
if (bytes.length < 1)
328+
{
329+
return;
330+
}
331+
339332
String nl = Strings.lineSeparator();
340-
StringBuffer buf = new StringBuffer();
341333

342334
indent += TAB;
343-
344-
buf.append(nl);
335+
345336
for (int i = 0; i < bytes.length; i += SAMPLE_SIZE)
346337
{
347-
if (bytes.length - i > SAMPLE_SIZE)
348-
{
349-
buf.append(indent);
350-
buf.append(Strings.fromByteArray(Hex.encode(bytes, i, SAMPLE_SIZE)));
351-
buf.append(TAB);
352-
buf.append(calculateAscString(bytes, i, SAMPLE_SIZE));
353-
buf.append(nl);
354-
}
355-
else
338+
int remaining = bytes.length - i;
339+
int chunk = Math.min(remaining, SAMPLE_SIZE);
340+
341+
buf.append(indent);
342+
buf.append(Hex.toHexString(bytes, i, chunk));
343+
for (int j = chunk; j < SAMPLE_SIZE; ++j)
356344
{
357-
buf.append(indent);
358-
buf.append(Strings.fromByteArray(Hex.encode(bytes, i, bytes.length - i)));
359-
for (int j = bytes.length - i; j != SAMPLE_SIZE; j++)
360-
{
361-
buf.append(" ");
362-
}
363-
buf.append(TAB);
364-
buf.append(calculateAscString(bytes, i, bytes.length - i));
365-
buf.append(nl);
345+
buf.append(" ");
366346
}
347+
buf.append(TAB);
348+
appendAscString(buf, bytes, i, chunk);
349+
buf.append(nl);
367350
}
368-
369-
return buf.toString();
370351
}
371352

372-
private static String calculateAscString(byte[] bytes, int off, int len)
353+
private static void appendAscString(StringBuffer buf, byte[] bytes, int off, int len)
373354
{
374-
StringBuffer buf = new StringBuffer();
375-
376355
for (int i = off; i != off + len; i++)
377356
{
378357
if (bytes[i] >= ' ' && bytes[i] <= '~')
379358
{
380359
buf.append((char)bytes[i]);
381360
}
382361
}
383-
384-
return buf.toString();
385362
}
386363
}

0 commit comments

Comments
 (0)