Skip to content

Commit d0b2e7f

Browse files
committed
refactor(cms): collapse Codecs UU/XX encoders onto shared map helpers
The four public methods now delegate to private EncodeWithMap / DecodeWithMap routines parameterised by the encoding map, eliminating the byte-for-byte duplication between the UU and XX variants.
1 parent 23c322a commit d0b2e7f

1 file changed

Lines changed: 38 additions & 185 deletions

File tree

web/Areas/CMS/Data/Codecs.cs

Lines changed: 38 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// FROM https://rextester.com/TGN19503
22

3-
using System.IO;
43
using System.Text;
54

65
namespace Viper.Areas.CMS.Data
@@ -52,164 +51,21 @@ public static class Codecs
5251
};
5352

5453
public static void UUDecode(Stream input, Stream output)
55-
{
56-
if (input == null)
57-
throw new ArgumentNullException(nameof(input));
58-
59-
if (output == null)
60-
throw new ArgumentNullException(nameof(output));
61-
62-
long len = input.Length;
63-
if (len == 0)
64-
return;
65-
66-
long didx = 0;
67-
int nextByte = input.ReadByte();
68-
while (nextByte >= 0)
69-
{
70-
// get line length (in number of encoded octets)
71-
int line_len = UUDecMap[nextByte];
72-
73-
// ascii printable to 0-63 and 4-byte to 3-byte conversion
74-
long end = didx + line_len;
75-
byte A, B, C, D;
76-
if (end > 2)
77-
{
78-
while (didx < end - 2)
79-
{
80-
A = UUDecMap[input.ReadByte()];
81-
B = UUDecMap[input.ReadByte()];
82-
C = UUDecMap[input.ReadByte()];
83-
D = UUDecMap[input.ReadByte()];
84-
85-
output.WriteByte((byte)(((A << 2) & 255) | ((B >> 4) & 3)));
86-
output.WriteByte((byte)(((B << 4) & 255) | ((C >> 2) & 15)));
87-
output.WriteByte((byte)(((C << 6) & 255) | (D & 63)));
88-
didx += 3;
89-
}
90-
}
91-
92-
if (didx < end)
93-
{
94-
A = UUDecMap[input.ReadByte()];
95-
B = UUDecMap[input.ReadByte()];
96-
output.WriteByte((byte)(((A << 2) & 255) | ((B >> 4) & 3)));
97-
didx++;
98-
99-
if (didx < end)
100-
{
101-
C = UUDecMap[input.ReadByte()];
102-
output.WriteByte((byte)(((B << 4) & 255) | ((C >> 2) & 15)));
103-
didx++;
104-
}
105-
}
106-
107-
// skip padding
108-
do
109-
{
110-
nextByte = input.ReadByte();
111-
}
112-
while (nextByte >= 0 && nextByte != '\n' && nextByte != '\r');
113-
114-
// skip end of line
115-
do
116-
{
117-
nextByte = input.ReadByte();
118-
}
119-
while (nextByte >= 0 && (nextByte == '\n' || nextByte == '\r'));
120-
}
121-
}
54+
=> DecodeWithMap(input, output, UUDecMap);
12255

12356
public static void UUEncode(Stream input, Stream output)
124-
{
125-
if (input == null)
126-
throw new ArgumentNullException(nameof(input));
127-
128-
if (output == null)
129-
throw new ArgumentNullException(nameof(output));
130-
131-
long len = input.Length;
132-
if (len == 0)
133-
return;
134-
135-
int sidx = 0;
136-
int line_len = 45;
137-
byte[] nl = Encoding.ASCII.GetBytes(Environment.NewLine);
138-
139-
byte A, B, C;
140-
// split into lines, adding line-length and line terminator
141-
while (sidx + line_len < len)
142-
{
143-
// line length
144-
output.WriteByte(UUEncMap[line_len]);
145-
146-
// 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
147-
for (int end = sidx + line_len; sidx < end; sidx += 3)
148-
{
149-
A = (byte)input.ReadByte();
150-
B = (byte)input.ReadByte();
151-
C = (byte)input.ReadByte();
57+
=> EncodeWithMap(input, output, UUEncMap);
15258

153-
output.WriteByte(UUEncMap[(A >> 2) & 63]);
154-
output.WriteByte(UUEncMap[(B >> 4) & 15 | (A << 4) & 63]);
155-
output.WriteByte(UUEncMap[(C >> 6) & 3 | (B << 2) & 63]);
156-
output.WriteByte(UUEncMap[C & 63]);
157-
}
158-
159-
// line terminator
160-
for (int idx = 0; idx < nl.Length; idx++)
161-
output.WriteByte(nl[idx]);
162-
}
163-
164-
// line length
165-
output.WriteByte(UUEncMap[len - sidx]);
166-
167-
// 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
168-
while (sidx + 2 < len)
169-
{
170-
A = (byte)input.ReadByte();
171-
B = (byte)input.ReadByte();
172-
C = (byte)input.ReadByte();
173-
174-
output.WriteByte(UUEncMap[(A >> 2) & 63]);
175-
output.WriteByte(UUEncMap[(B >> 4) & 15 | (A << 4) & 63]);
176-
output.WriteByte(UUEncMap[(C >> 6) & 3 | (B << 2) & 63]);
177-
output.WriteByte(UUEncMap[C & 63]);
178-
sidx += 3;
179-
}
180-
181-
if (sidx < len - 1)
182-
{
183-
A = (byte)input.ReadByte();
184-
B = (byte)input.ReadByte();
185-
186-
output.WriteByte(UUEncMap[(A >> 2) & 63]);
187-
output.WriteByte(UUEncMap[(B >> 4) & 15 | (A << 4) & 63]);
188-
output.WriteByte(UUEncMap[(B << 2) & 63]);
189-
output.WriteByte(UUEncMap[0]);
190-
}
191-
else if (sidx < len)
192-
{
193-
A = (byte)input.ReadByte();
194-
195-
output.WriteByte(UUEncMap[(A >> 2) & 63]);
196-
output.WriteByte(UUEncMap[(A << 4) & 63]);
197-
output.WriteByte(UUEncMap[0]);
198-
output.WriteByte(UUEncMap[0]);
199-
}
59+
public static void XXDecode(Stream input, Stream output)
60+
=> DecodeWithMap(input, output, XXDecMap);
20061

201-
// line terminator
202-
for (int idx = 0; idx < nl.Length; idx++)
203-
output.WriteByte(nl[idx]);
204-
}
62+
public static void XXEncode(Stream input, Stream output)
63+
=> EncodeWithMap(input, output, XXEncMap);
20564

206-
public static void XXDecode(Stream input, Stream output)
65+
private static void DecodeWithMap(Stream input, Stream output, byte[] decMap)
20766
{
208-
if (input == null)
209-
throw new ArgumentNullException(nameof(input));
210-
211-
if (output == null)
212-
throw new ArgumentNullException(nameof(output));
67+
ArgumentNullException.ThrowIfNull(input);
68+
ArgumentNullException.ThrowIfNull(output);
21369

21470
long len = input.Length;
21571
if (len == 0)
@@ -220,7 +76,7 @@ public static void XXDecode(Stream input, Stream output)
22076
while (nextByte >= 0)
22177
{
22278
// get line length (in number of encoded octets)
223-
int line_len = XXDecMap[nextByte];
79+
int line_len = decMap[nextByte];
22480

22581
// ascii printable to 0-63 and 4-byte to 3-byte conversion
22682
long end = didx + line_len;
@@ -229,10 +85,10 @@ public static void XXDecode(Stream input, Stream output)
22985
{
23086
while (didx < end - 2)
23187
{
232-
A = XXDecMap[input.ReadByte()];
233-
B = XXDecMap[input.ReadByte()];
234-
C = XXDecMap[input.ReadByte()];
235-
D = XXDecMap[input.ReadByte()];
88+
A = decMap[input.ReadByte()];
89+
B = decMap[input.ReadByte()];
90+
C = decMap[input.ReadByte()];
91+
D = decMap[input.ReadByte()];
23692

23793
output.WriteByte((byte)(((A << 2) & 255) | ((B >> 4) & 3)));
23894
output.WriteByte((byte)(((B << 4) & 255) | ((C >> 2) & 15)));
@@ -243,14 +99,14 @@ public static void XXDecode(Stream input, Stream output)
24399

244100
if (didx < end)
245101
{
246-
A = XXDecMap[input.ReadByte()];
247-
B = XXDecMap[input.ReadByte()];
102+
A = decMap[input.ReadByte()];
103+
B = decMap[input.ReadByte()];
248104
output.WriteByte((byte)(((A << 2) & 255) | ((B >> 4) & 3)));
249105
didx++;
250106

251107
if (didx < end)
252108
{
253-
C = XXDecMap[input.ReadByte()];
109+
C = decMap[input.ReadByte()];
254110
output.WriteByte((byte)(((B << 4) & 255) | ((C >> 2) & 15)));
255111
didx++;
256112
}
@@ -272,13 +128,10 @@ public static void XXDecode(Stream input, Stream output)
272128
}
273129
}
274130

275-
public static void XXEncode(Stream input, Stream output)
131+
private static void EncodeWithMap(Stream input, Stream output, byte[] encMap)
276132
{
277-
if (input == null)
278-
throw new ArgumentNullException(nameof(input));
279-
280-
if (output == null)
281-
throw new ArgumentNullException(nameof(output));
133+
ArgumentNullException.ThrowIfNull(input);
134+
ArgumentNullException.ThrowIfNull(output);
282135

283136
long len = input.Length;
284137
if (len == 0)
@@ -293,7 +146,7 @@ public static void XXEncode(Stream input, Stream output)
293146
while (sidx + line_len < len)
294147
{
295148
// line length
296-
output.WriteByte(XXEncMap[line_len]);
149+
output.WriteByte(encMap[line_len]);
297150

298151
// 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
299152
for (int end = sidx + line_len; sidx < end; sidx += 3)
@@ -302,10 +155,10 @@ public static void XXEncode(Stream input, Stream output)
302155
B = (byte)input.ReadByte();
303156
C = (byte)input.ReadByte();
304157

305-
output.WriteByte(XXEncMap[(A >> 2) & 63]);
306-
output.WriteByte(XXEncMap[(B >> 4) & 15 | (A << 4) & 63]);
307-
output.WriteByte(XXEncMap[(C >> 6) & 3 | (B << 2) & 63]);
308-
output.WriteByte(XXEncMap[C & 63]);
158+
output.WriteByte(encMap[(A >> 2) & 63]);
159+
output.WriteByte(encMap[(B >> 4) & 15 | (A << 4) & 63]);
160+
output.WriteByte(encMap[(C >> 6) & 3 | (B << 2) & 63]);
161+
output.WriteByte(encMap[C & 63]);
309162
}
310163

311164
// line terminator
@@ -314,7 +167,7 @@ public static void XXEncode(Stream input, Stream output)
314167
}
315168

316169
// line length
317-
output.WriteByte(XXEncMap[len - sidx]);
170+
output.WriteByte(encMap[len - sidx]);
318171

319172
// 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
320173
while (sidx + 2 < len)
@@ -323,10 +176,10 @@ public static void XXEncode(Stream input, Stream output)
323176
B = (byte)input.ReadByte();
324177
C = (byte)input.ReadByte();
325178

326-
output.WriteByte(XXEncMap[(A >> 2) & 63]);
327-
output.WriteByte(XXEncMap[(B >> 4) & 15 | (A << 4) & 63]);
328-
output.WriteByte(XXEncMap[(C >> 6) & 3 | (B << 2) & 63]);
329-
output.WriteByte(XXEncMap[C & 63]);
179+
output.WriteByte(encMap[(A >> 2) & 63]);
180+
output.WriteByte(encMap[(B >> 4) & 15 | (A << 4) & 63]);
181+
output.WriteByte(encMap[(C >> 6) & 3 | (B << 2) & 63]);
182+
output.WriteByte(encMap[C & 63]);
330183
sidx += 3;
331184
}
332185

@@ -335,19 +188,19 @@ public static void XXEncode(Stream input, Stream output)
335188
A = (byte)input.ReadByte();
336189
B = (byte)input.ReadByte();
337190

338-
output.WriteByte(XXEncMap[(A >> 2) & 63]);
339-
output.WriteByte(XXEncMap[(B >> 4) & 15 | (A << 4) & 63]);
340-
output.WriteByte(XXEncMap[(B << 2) & 63]);
341-
output.WriteByte(XXEncMap[0]);
191+
output.WriteByte(encMap[(A >> 2) & 63]);
192+
output.WriteByte(encMap[(B >> 4) & 15 | (A << 4) & 63]);
193+
output.WriteByte(encMap[(B << 2) & 63]);
194+
output.WriteByte(encMap[0]);
342195
}
343196
else if (sidx < len)
344197
{
345198
A = (byte)input.ReadByte();
346199

347-
output.WriteByte(XXEncMap[(A >> 2) & 63]);
348-
output.WriteByte(XXEncMap[(A << 4) & 63]);
349-
output.WriteByte(XXEncMap[0]);
350-
output.WriteByte(XXEncMap[0]);
200+
output.WriteByte(encMap[(A >> 2) & 63]);
201+
output.WriteByte(encMap[(A << 4) & 63]);
202+
output.WriteByte(encMap[0]);
203+
output.WriteByte(encMap[0]);
351204
}
352205

353206
// line terminator

0 commit comments

Comments
 (0)