-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathBinaryToStringFormatter.cs
More file actions
241 lines (224 loc) · 8.72 KB
/
BinaryToStringFormatter.cs
File metadata and controls
241 lines (224 loc) · 8.72 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
using System;
using System.Buffers;
using System.Buffers.Text;
using System.Text;
using SysadminsLV.Asn1Parser.Utils.CLRExtensions;
namespace SysadminsLV.Asn1Parser;
static class BinaryToStringFormatter {
public static String ToHexRaw(ReadOnlySpan<Byte> rawData, EncodingFormat format, Boolean forceUpperCase) {
String eol = format.GetEndOfLine();
var sb = new StringBuilder();
foreach (Byte b in rawData) {
sb.AppendHexOctet(b, forceUpperCase);
}
return sb.Append(eol).ToString();
}
public static String ToHex(ReadOnlySpan<Byte> rawData, EncodingFormat format, Boolean forceUpperCase) {
var sb = new StringBuilder();
for (Int32 index = 0; index < rawData.Length; index++) {
sb.AppendHexOctet(rawData[index], forceUpperCase);
if (index == 0) {
sb.Append(" ");
continue;
}
if ((index + 1) % 16 == 0) {
// if current octet is the last octet in a row, append EOL format
switch (format) {
case EncodingFormat.NOCRLF:
sb.Append(" ");
break;
case EncodingFormat.CRLF:
sb.Append("\r\n"); break;
case EncodingFormat.NOCR:
sb.Append("\n"); break;
}
} else if ((index + 1) % 8 == 0 && format != EncodingFormat.NOCRLF) {
sb.Append(" ");
} else {
sb.Append(" ");
}
}
return sb.Append(format.GetEndOfLine()).ToString();
}
public static String ToHexAddress(ReadOnlySpan<Byte> rawData, EncodingFormat format, Boolean forceUpperCase) {
Int32 rowCount = 0;
Int32 addrLength = getAddrLength(rawData.Length);
String eol = format == EncodingFormat.NOCR ? "\n" : "\r\n";
String eof = format.GetEndOfLine();
Int32 totalRows = (Int32)Math.Ceiling(rawData.Length / 16d);
Int32 bufferSize = addrLength + (52 + eol.Length) * totalRows + eof.Length;
var sb = new StringBuilder(bufferSize);
for (Int32 index = 0; index < rawData.Length; index++) {
if (index % 16 == 0) {
String hexAddress = Convert.ToString(rowCount, 16).PadLeft(addrLength, '0');
if (forceUpperCase) {
hexAddress = hexAddress.ToUpper();
}
sb.Append(hexAddress);
sb.Append(" ");
rowCount += 16;
}
sb.AppendHexOctet(rawData[index], forceUpperCase);
if (index == 0) {
sb.Append(" ");
continue;
}
if ((index + 1) % 16 == 0) {
// if current octet is the last octet in a row, append EOL format
sb.Append(eol);
} else if ((index + 1) % 8 == 0) {
// if current octet is center octet in a row, append extra space
sb.Append(" ");
} else {
sb.Append(" ");
}
}
return sb.Append(eof).ToString();
}
public static String ToHexAscii(ReadOnlySpan<Byte> rawData, EncodingFormat format, Boolean forceUpperCase) {
var sb = new StringBuilder();
var ascii = new StringBuilder(8);
for (Int32 index = 0; index < rawData.Length; index++) {
sb.AppendHexOctet(rawData[index], forceUpperCase);
Char c = rawData[index] < 32 || rawData[index] > 126
? '.'
: (Char)rawData[index];
ascii.Append(c);
if (index == 0) {
sb.Append(" ");
continue;
}
if ((index + 1) % 16 == 0) {
sb.Append(" ");
sb.Append(ascii);
ascii.Clear();
// if current octet is the last octet in a row, append EOL format
sb.Append(format == EncodingFormat.NOCR ? "\n" : "\r\n");
} else if ((index + 1) % 8 == 0) {
sb.Append(" ");
} else {
sb.Append(" ");
}
// handle last byte to complete partial ASCII panel.
if (index + 1 == rawData.Length) {
sb.Append(getAsciiPadding(index + 1));
sb.Append(ascii);
}
}
return sb.Append(format.GetEndOfLine()).ToString();
}
public static String ToHexAddressAndAscii(ReadOnlySpan<Byte> rawData, EncodingFormat format, Boolean forceUpperCase) {
var sb = new StringBuilder();
var ascii = new StringBuilder(8);
Int32 addrLength = getAddrLength(rawData.Length);
Int32 rowCount = 0;
for (Int32 index = 0; index < rawData.Length; index++) {
if (index % 16 == 0) {
String hexAddress = Convert.ToString(rowCount, 16).PadLeft(addrLength, '0');
if (forceUpperCase) {
hexAddress = hexAddress.ToUpper();
}
sb.Append(hexAddress);
sb.Append(" ");
rowCount += 16;
}
sb.AppendHexOctet(rawData[index], forceUpperCase);
Char c = rawData[index] < 32 || rawData[index] > 126
? '.'
: (Char)rawData[index];
ascii.Append(c);
if (index == 0) {
sb.Append(" ");
continue;
}
if ((index + 1) % 16 == 0) {
sb.Append(" ");
sb.Append(ascii);
ascii.Clear();
sb.Append(format == EncodingFormat.NOCR ? "\n" : "\r\n");
} else if ((index + 1) % 8 == 0) {
sb.Append(" ");
} else {
sb.Append(" ");
}
// handle last byte to complete partial ASCII panel.
if (index + 1 == rawData.Length) {
sb.Append(getAsciiPadding(index + 1));
sb.Append(ascii);
}
}
return sb.Append(format.GetEndOfLine()).ToString();
}
public static String ToBase64(ReadOnlySpan<Byte> rawData, EncodingType encoding, EncodingFormat format) {
Int32 b64Length = Base64.GetMaxEncodedToUtf8Length(rawData.Length);
Span<Byte> base64 = new Byte[b64Length];
OperationStatus result = Base64.EncodeToUtf8(rawData, base64, out _, out _);
String eol = format.GetEndOfLine();
Int32 rowCount = (Int32)Math.Floor(b64Length / 64d);
Int32 eolCount = rowCount * eol.Length + eol.Length;
PemHeader? pem = null;
switch (encoding) {
case EncodingType.Base64:
break;
default:
pem = getPemHeader(encoding);
break;
}
Int32 totalLength = b64Length + eolCount;
if (pem is not null) {
// total length is a sum of:
// - PEM header length + EOL
// - main base64 content with EOLs
// - PEM footer length + EOL
// - final EOL
totalLength = totalLength + pem.GetHeader().Length + pem.GetFooter().Length + eol.Length * 2 + 1;
}
var sb = new StringBuilder(totalLength);
// append PEM header if available
if (pem is not null) {
sb.Append(pem.GetHeader()).Append(eol);
}
// copy first full lines
for (Int32 i = 0; i < rowCount; i++) {
for (Int32 j = 0; j < 64; j++) {
sb.Append((Char)base64[i * 64 + j]);
}
sb.Append(eol);
}
for (Int32 i = rowCount * 64; i < b64Length; i++) {
sb.Append((Char)base64[i]);
}
// append PEM footer if available
if (pem is not null) {
sb.Append(eol);
sb.Append(pem.GetFooter());
}
sb.Append(eol);
return sb.ToString();
}
#region string finalizers
static PemHeader getPemHeader(EncodingType encoding) {
if (PemHeader.ContainsEncoding(encoding)) {
return PemHeader.GetHeader(encoding);
}
throw new ArgumentException("Specified encoding is not valid Base64 encoding.");
}
#endregion
#region helper methods
static String getAsciiPadding(Int32 index) {
Int32 remainder = index % 16;
if (remainder > 7) {
return new String(' ', (17 - remainder) * 3 - 1);
}
return new String(' ', (17 - remainder) * 3);
}
static Int32 getAddrLength(Int32 size) {
Int32 div = size / 16;
if (size % 16 > 0) { div++; }
String h = $"{div:x}";
return h.Length < 4
? 4
: (h.Length % 2 == 0 ? h.Length : h.Length + 1);
}
#endregion
}