@@ -51,164 +51,21 @@ public static class Codecs
5151 } ;
5252
5353 public static void UUDecode ( Stream input , Stream output )
54- {
55- if ( input == null )
56- throw new ArgumentNullException ( nameof ( input ) ) ;
57-
58- if ( output == null )
59- throw new ArgumentNullException ( nameof ( output ) ) ;
60-
61- long len = input . Length ;
62- if ( len == 0 )
63- return ;
64-
65- long didx = 0 ;
66- int nextByte = input . ReadByte ( ) ;
67- while ( nextByte >= 0 )
68- {
69- // get line length (in number of encoded octets)
70- int line_len = UUDecMap [ nextByte ] ;
71-
72- // ascii printable to 0-63 and 4-byte to 3-byte conversion
73- long end = didx + line_len ;
74- byte A , B , C , D ;
75- if ( end > 2 )
76- {
77- while ( didx < end - 2 )
78- {
79- A = UUDecMap [ input . ReadByte ( ) ] ;
80- B = UUDecMap [ input . ReadByte ( ) ] ;
81- C = UUDecMap [ input . ReadByte ( ) ] ;
82- D = UUDecMap [ input . ReadByte ( ) ] ;
83-
84- output . WriteByte ( ( byte ) ( ( ( A << 2 ) & 255 ) | ( ( B >> 4 ) & 3 ) ) ) ;
85- output . WriteByte ( ( byte ) ( ( ( B << 4 ) & 255 ) | ( ( C >> 2 ) & 15 ) ) ) ;
86- output . WriteByte ( ( byte ) ( ( ( C << 6 ) & 255 ) | ( D & 63 ) ) ) ;
87- didx += 3 ;
88- }
89- }
90-
91- if ( didx < end )
92- {
93- A = UUDecMap [ input . ReadByte ( ) ] ;
94- B = UUDecMap [ input . ReadByte ( ) ] ;
95- output . WriteByte ( ( byte ) ( ( ( A << 2 ) & 255 ) | ( ( B >> 4 ) & 3 ) ) ) ;
96- didx ++ ;
97-
98- if ( didx < end )
99- {
100- C = UUDecMap [ input . ReadByte ( ) ] ;
101- output . WriteByte ( ( byte ) ( ( ( B << 4 ) & 255 ) | ( ( C >> 2 ) & 15 ) ) ) ;
102- didx ++ ;
103- }
104- }
105-
106- // skip padding
107- do
108- {
109- nextByte = input . ReadByte ( ) ;
110- }
111- while ( nextByte >= 0 && nextByte != '\n ' && nextByte != '\r ' ) ;
112-
113- // skip end of line
114- do
115- {
116- nextByte = input . ReadByte ( ) ;
117- }
118- while ( nextByte >= 0 && ( nextByte == '\n ' || nextByte == '\r ' ) ) ;
119- }
120- }
54+ => DecodeWithMap ( input , output , UUDecMap ) ;
12155
12256 public static void UUEncode ( Stream input , Stream output )
123- {
124- if ( input == null )
125- throw new ArgumentNullException ( nameof ( input ) ) ;
126-
127- if ( output == null )
128- throw new ArgumentNullException ( nameof ( output ) ) ;
129-
130- long len = input . Length ;
131- if ( len == 0 )
132- return ;
133-
134- int sidx = 0 ;
135- int line_len = 45 ;
136- byte [ ] nl = Encoding . ASCII . GetBytes ( Environment . NewLine ) ;
137-
138- byte A , B , C ;
139- // split into lines, adding line-length and line terminator
140- while ( sidx + line_len < len )
141- {
142- // line length
143- output . WriteByte ( UUEncMap [ line_len ] ) ;
144-
145- // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
146- for ( int end = sidx + line_len ; sidx < end ; sidx += 3 )
147- {
148- A = ( byte ) input . ReadByte ( ) ;
149- B = ( byte ) input . ReadByte ( ) ;
150- C = ( byte ) input . ReadByte ( ) ;
57+ => EncodeWithMap ( input , output , UUEncMap ) ;
15158
152- output . WriteByte ( UUEncMap [ ( A >> 2 ) & 63 ] ) ;
153- output . WriteByte ( UUEncMap [ ( B >> 4 ) & 15 | ( A << 4 ) & 63 ] ) ;
154- output . WriteByte ( UUEncMap [ ( C >> 6 ) & 3 | ( B << 2 ) & 63 ] ) ;
155- output . WriteByte ( UUEncMap [ C & 63 ] ) ;
156- }
157-
158- // line terminator
159- for ( int idx = 0 ; idx < nl . Length ; idx ++ )
160- output . WriteByte ( nl [ idx ] ) ;
161- }
162-
163- // line length
164- output . WriteByte ( UUEncMap [ len - sidx ] ) ;
165-
166- // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
167- while ( sidx + 2 < len )
168- {
169- A = ( byte ) input . ReadByte ( ) ;
170- B = ( byte ) input . ReadByte ( ) ;
171- C = ( byte ) input . ReadByte ( ) ;
172-
173- output . WriteByte ( UUEncMap [ ( A >> 2 ) & 63 ] ) ;
174- output . WriteByte ( UUEncMap [ ( B >> 4 ) & 15 | ( A << 4 ) & 63 ] ) ;
175- output . WriteByte ( UUEncMap [ ( C >> 6 ) & 3 | ( B << 2 ) & 63 ] ) ;
176- output . WriteByte ( UUEncMap [ C & 63 ] ) ;
177- sidx += 3 ;
178- }
179-
180- if ( sidx < len - 1 )
181- {
182- A = ( byte ) input . ReadByte ( ) ;
183- B = ( byte ) input . ReadByte ( ) ;
184-
185- output . WriteByte ( UUEncMap [ ( A >> 2 ) & 63 ] ) ;
186- output . WriteByte ( UUEncMap [ ( B >> 4 ) & 15 | ( A << 4 ) & 63 ] ) ;
187- output . WriteByte ( UUEncMap [ ( B << 2 ) & 63 ] ) ;
188- output . WriteByte ( UUEncMap [ 0 ] ) ;
189- }
190- else if ( sidx < len )
191- {
192- A = ( byte ) input . ReadByte ( ) ;
193-
194- output . WriteByte ( UUEncMap [ ( A >> 2 ) & 63 ] ) ;
195- output . WriteByte ( UUEncMap [ ( A << 4 ) & 63 ] ) ;
196- output . WriteByte ( UUEncMap [ 0 ] ) ;
197- output . WriteByte ( UUEncMap [ 0 ] ) ;
198- }
59+ public static void XXDecode ( Stream input , Stream output )
60+ => DecodeWithMap ( input , output , XXDecMap ) ;
19961
200- // line terminator
201- for ( int idx = 0 ; idx < nl . Length ; idx ++ )
202- output . WriteByte ( nl [ idx ] ) ;
203- }
62+ public static void XXEncode ( Stream input , Stream output )
63+ => EncodeWithMap ( input , output , XXEncMap ) ;
20464
205- public static void XXDecode ( Stream input , Stream output )
65+ private static void DecodeWithMap ( Stream input , Stream output , byte [ ] decMap )
20666 {
207- if ( input == null )
208- throw new ArgumentNullException ( nameof ( input ) ) ;
209-
210- if ( output == null )
211- throw new ArgumentNullException ( nameof ( output ) ) ;
67+ ArgumentNullException . ThrowIfNull ( input ) ;
68+ ArgumentNullException . ThrowIfNull ( output ) ;
21269
21370 long len = input . Length ;
21471 if ( len == 0 )
@@ -219,7 +76,7 @@ public static void XXDecode(Stream input, Stream output)
21976 while ( nextByte >= 0 )
22077 {
22178 // get line length (in number of encoded octets)
222- int line_len = XXDecMap [ nextByte ] ;
79+ int line_len = decMap [ nextByte ] ;
22380
22481 // ascii printable to 0-63 and 4-byte to 3-byte conversion
22582 long end = didx + line_len ;
@@ -228,10 +85,10 @@ public static void XXDecode(Stream input, Stream output)
22885 {
22986 while ( didx < end - 2 )
23087 {
231- A = XXDecMap [ input . ReadByte ( ) ] ;
232- B = XXDecMap [ input . ReadByte ( ) ] ;
233- C = XXDecMap [ input . ReadByte ( ) ] ;
234- D = XXDecMap [ input . ReadByte ( ) ] ;
88+ A = decMap [ input . ReadByte ( ) ] ;
89+ B = decMap [ input . ReadByte ( ) ] ;
90+ C = decMap [ input . ReadByte ( ) ] ;
91+ D = decMap [ input . ReadByte ( ) ] ;
23592
23693 output . WriteByte ( ( byte ) ( ( ( A << 2 ) & 255 ) | ( ( B >> 4 ) & 3 ) ) ) ;
23794 output . WriteByte ( ( byte ) ( ( ( B << 4 ) & 255 ) | ( ( C >> 2 ) & 15 ) ) ) ;
@@ -242,14 +99,14 @@ public static void XXDecode(Stream input, Stream output)
24299
243100 if ( didx < end )
244101 {
245- A = XXDecMap [ input . ReadByte ( ) ] ;
246- B = XXDecMap [ input . ReadByte ( ) ] ;
102+ A = decMap [ input . ReadByte ( ) ] ;
103+ B = decMap [ input . ReadByte ( ) ] ;
247104 output . WriteByte ( ( byte ) ( ( ( A << 2 ) & 255 ) | ( ( B >> 4 ) & 3 ) ) ) ;
248105 didx ++ ;
249106
250107 if ( didx < end )
251108 {
252- C = XXDecMap [ input . ReadByte ( ) ] ;
109+ C = decMap [ input . ReadByte ( ) ] ;
253110 output . WriteByte ( ( byte ) ( ( ( B << 4 ) & 255 ) | ( ( C >> 2 ) & 15 ) ) ) ;
254111 didx ++ ;
255112 }
@@ -271,13 +128,10 @@ public static void XXDecode(Stream input, Stream output)
271128 }
272129 }
273130
274- public static void XXEncode ( Stream input , Stream output )
131+ private static void EncodeWithMap ( Stream input , Stream output , byte [ ] encMap )
275132 {
276- if ( input == null )
277- throw new ArgumentNullException ( nameof ( input ) ) ;
278-
279- if ( output == null )
280- throw new ArgumentNullException ( nameof ( output ) ) ;
133+ ArgumentNullException . ThrowIfNull ( input ) ;
134+ ArgumentNullException . ThrowIfNull ( output ) ;
281135
282136 long len = input . Length ;
283137 if ( len == 0 )
@@ -292,7 +146,7 @@ public static void XXEncode(Stream input, Stream output)
292146 while ( sidx + line_len < len )
293147 {
294148 // line length
295- output . WriteByte ( XXEncMap [ line_len ] ) ;
149+ output . WriteByte ( encMap [ line_len ] ) ;
296150
297151 // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
298152 for ( int end = sidx + line_len ; sidx < end ; sidx += 3 )
@@ -301,10 +155,10 @@ public static void XXEncode(Stream input, Stream output)
301155 B = ( byte ) input . ReadByte ( ) ;
302156 C = ( byte ) input . ReadByte ( ) ;
303157
304- output . WriteByte ( XXEncMap [ ( A >> 2 ) & 63 ] ) ;
305- output . WriteByte ( XXEncMap [ ( B >> 4 ) & 15 | ( A << 4 ) & 63 ] ) ;
306- output . WriteByte ( XXEncMap [ ( C >> 6 ) & 3 | ( B << 2 ) & 63 ] ) ;
307- 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 ] ) ;
308162 }
309163
310164 // line terminator
@@ -313,7 +167,7 @@ public static void XXEncode(Stream input, Stream output)
313167 }
314168
315169 // line length
316- output . WriteByte ( XXEncMap [ len - sidx ] ) ;
170+ output . WriteByte ( encMap [ len - sidx ] ) ;
317171
318172 // 3-byte to 4-byte conversion + 0-63 to ascii printable conversion
319173 while ( sidx + 2 < len )
@@ -322,10 +176,10 @@ public static void XXEncode(Stream input, Stream output)
322176 B = ( byte ) input . ReadByte ( ) ;
323177 C = ( byte ) input . ReadByte ( ) ;
324178
325- output . WriteByte ( XXEncMap [ ( A >> 2 ) & 63 ] ) ;
326- output . WriteByte ( XXEncMap [ ( B >> 4 ) & 15 | ( A << 4 ) & 63 ] ) ;
327- output . WriteByte ( XXEncMap [ ( C >> 6 ) & 3 | ( B << 2 ) & 63 ] ) ;
328- 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 ] ) ;
329183 sidx += 3 ;
330184 }
331185
@@ -334,19 +188,19 @@ public static void XXEncode(Stream input, Stream output)
334188 A = ( byte ) input . ReadByte ( ) ;
335189 B = ( byte ) input . ReadByte ( ) ;
336190
337- output . WriteByte ( XXEncMap [ ( A >> 2 ) & 63 ] ) ;
338- output . WriteByte ( XXEncMap [ ( B >> 4 ) & 15 | ( A << 4 ) & 63 ] ) ;
339- output . WriteByte ( XXEncMap [ ( B << 2 ) & 63 ] ) ;
340- 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 ] ) ;
341195 }
342196 else if ( sidx < len )
343197 {
344198 A = ( byte ) input . ReadByte ( ) ;
345199
346- output . WriteByte ( XXEncMap [ ( A >> 2 ) & 63 ] ) ;
347- output . WriteByte ( XXEncMap [ ( A << 4 ) & 63 ] ) ;
348- output . WriteByte ( XXEncMap [ 0 ] ) ;
349- 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 ] ) ;
350204 }
351205
352206 // line terminator
0 commit comments