Skip to content

Commit 8a06da5

Browse files
committed
follow the code analyzer rules
1 parent be35fd5 commit 8a06da5

File tree

5 files changed

+92
-77
lines changed

5 files changed

+92
-77
lines changed

src/Smdn.Fundamental.PrintableEncoding.ModifiedBase64/Smdn.Formats.ModifiedBase64/FromRFC2152ModifiedBase64Transform.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,16 @@ public FromRFC2152ModifiedBase64Transform(bool ignoreWhiteSpaces)
3232

3333
public void Dispose()
3434
{
35-
fromBase64Transform?.Dispose();
36-
fromBase64Transform = null;
35+
Dispose(true);
36+
GC.SuppressFinalize(this);
37+
}
38+
39+
protected virtual void Dispose(bool disposing)
40+
{
41+
if (disposing) {
42+
fromBase64Transform?.Dispose();
43+
fromBase64Transform = null;
44+
}
3745
}
3846

3947
public virtual int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
@@ -57,7 +65,7 @@ public virtual byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, i
5765
// Modified Base64 because of the conflict with its use as an escape
5866
// character for the Q content transfer encoding in RFC 2047 header
5967
// fields, as mentioned above.
60-
var paddingCount = 4 - (count + inputCount) & 3;
68+
var paddingCount = 4 - ((count + inputCount) & 3);
6169

6270
count = 0; // initialize
6371

src/Smdn.Fundamental.PrintableEncoding.ModifiedBase64/Smdn.Formats.ModifiedBase64/FromRFC3501ModifiedBase64Transform.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public FromRFC3501ModifiedBase64Transform(bool ignoreWhiteSpaces)
2626
{
2727
}
2828

29-
private byte[] ReplaceInput(byte[] inputBuffer, int inputOffset, int inputCount)
29+
private static byte[] ReplaceInput(byte[] inputBuffer, int inputOffset, int inputCount)
3030
{
3131
var replaced = new byte[inputCount];
3232

src/Smdn.Fundamental.PrintableEncoding.ModifiedBase64/Smdn.Formats.ModifiedBase64/ModifiedUTF7.cs

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -20,46 +20,46 @@ public static string Encode(string str)
2020
if (str.Length == 0)
2121
return string.Empty;
2222

23-
using (var transform = new ToRFC3501ModifiedBase64Transform()) {
24-
var encoded = new StringBuilder(str.Length * 2);
25-
var index = -1;
26-
var shiftFrom = -1;
27-
28-
foreach (var c in str) {
29-
index++;
30-
31-
if (('\u0020' <= c && c <= '\u007e')) {
32-
if (0 <= shiftFrom) {
33-
// string -> modified UTF7
34-
encoded.Append('&');
35-
encoded.Append(transform.TransformStringTo(str.Substring(shiftFrom, index - shiftFrom), Encoding.BigEndianUnicode));
36-
encoded.Append('-');
37-
38-
shiftFrom = -1;
39-
}
40-
41-
// printable US-ASCII characters
42-
if (c == '\u0026')
43-
// except for "&"
44-
encoded.Append("&-");
45-
else
46-
encoded.Append(c);
47-
}
48-
else {
49-
if (shiftFrom == -1)
50-
shiftFrom = index;
23+
using var transform = new ToRFC3501ModifiedBase64Transform();
24+
25+
var encoded = new StringBuilder(str.Length * 2);
26+
var index = -1;
27+
var shiftFrom = -1;
28+
29+
foreach (var c in str) {
30+
index++;
31+
32+
if (c is >= '\u0020' and <= '\u007e') {
33+
if (0 <= shiftFrom) {
34+
// string -> modified UTF7
35+
encoded.Append('&');
36+
encoded.Append(transform.TransformStringTo(str.Substring(shiftFrom, index - shiftFrom), Encoding.BigEndianUnicode));
37+
encoded.Append('-');
38+
39+
shiftFrom = -1;
5140
}
52-
}
5341

54-
if (0 <= shiftFrom) {
55-
// string -> modified UTF7
56-
encoded.Append('&');
57-
encoded.Append(transform.TransformStringTo(str.Substring(shiftFrom), Encoding.BigEndianUnicode));
58-
encoded.Append('-');
42+
// printable US-ASCII characters
43+
if (c == '\u0026')
44+
// except for "&"
45+
encoded.Append("&-");
46+
else
47+
encoded.Append(c);
5948
}
49+
else {
50+
if (shiftFrom == -1)
51+
shiftFrom = index;
52+
}
53+
}
6054

61-
return encoded.ToString();
55+
if (0 <= shiftFrom) {
56+
// string -> modified UTF7
57+
encoded.Append('&');
58+
encoded.Append(transform.TransformStringTo(str.Substring(shiftFrom), Encoding.BigEndianUnicode));
59+
encoded.Append('-');
6260
}
61+
62+
return encoded.ToString();
6363
}
6464

6565
public static string Decode(string str)
@@ -69,52 +69,52 @@ public static string Decode(string str)
6969
if (str.Length == 0)
7070
return string.Empty;
7171

72-
using (var transform = new FromRFC3501ModifiedBase64Transform(ignoreWhiteSpaces: false)) {
73-
var decoded = new StringBuilder(str.Length);
72+
using var transform = new FromRFC3501ModifiedBase64Transform(ignoreWhiteSpaces: false);
7473

75-
for (var index = 0; index < str.Length; index++) {
76-
var c = str[index];
74+
var decoded = new StringBuilder(str.Length);
7775

78-
if (c < '\u0020' || '\u007e' < c)
79-
throw new FormatException(string.Format("contains non-ascii or non-printable character: at index {0} of '{1}', \\u{2:x4}", index, str, (int)c));
76+
for (var index = 0; index < str.Length; index++) {
77+
var c = str[index];
8078

81-
// In modified UTF-7, printable US-ASCII characters, except for "&",
82-
// represent themselves
83-
// "&" is used to shift to modified BASE64
84-
if (c != '&') {
85-
decoded.Append(c);
86-
continue;
87-
}
79+
if (c is < '\u0020' or > '\u007e')
80+
throw new FormatException($"contains non-ascii or non-printable character: at index {index} of '{str}', \\u{(int)c:x4}");
8881

89-
if (str.Length <= ++index)
90-
// incorrect form
91-
throw new FormatException("incorrect form");
82+
// In modified UTF-7, printable US-ASCII characters, except for "&",
83+
// represent themselves
84+
// "&" is used to shift to modified BASE64
85+
if (c != '&') {
86+
decoded.Append(c);
87+
continue;
88+
}
9289

93-
if (str[index] == '-') {
94-
// The character "&" (0x26) is represented by the two-octet sequence "&-".
95-
decoded.Append('&');
96-
continue;
97-
}
90+
if (str.Length <= ++index)
91+
// incorrect form
92+
throw new FormatException("incorrect form");
9893

99-
var nonPrintableChars = new byte[str.Length - index];
100-
var len = 0;
94+
if (str[index] == '-') {
95+
// The character "&" (0x26) is represented by the two-octet sequence "&-".
96+
decoded.Append('&');
97+
continue;
98+
}
10199

102-
for (; index < str.Length; index++) {
103-
c = str[index];
100+
var nonPrintableChars = new byte[str.Length - index];
101+
var len = 0;
104102

105-
if (c == '-')
106-
// "-" is used to shift back to US-ASCII
107-
break;
108-
else
109-
nonPrintableChars[len++] = (byte)c;
110-
}
103+
for (; index < str.Length; index++) {
104+
c = str[index];
111105

112-
// modified UTF7 -> string
113-
decoded.Append(Encoding.BigEndianUnicode.GetString(transform.TransformBytes(nonPrintableChars, 0, len)));
106+
if (c == '-')
107+
// "-" is used to shift back to US-ASCII
108+
break;
109+
else
110+
nonPrintableChars[len++] = (byte)c;
114111
}
115112

116-
return decoded.ToString();
113+
// modified UTF7 -> string
114+
decoded.Append(Encoding.BigEndianUnicode.GetString(transform.TransformBytes(nonPrintableChars, 0, len)));
117115
}
116+
117+
return decoded.ToString();
118118
}
119119
}
120120
}

src/Smdn.Fundamental.PrintableEncoding.ModifiedBase64/Smdn.Formats.ModifiedBase64/ToRFC2152ModifiedBase64Transform.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,16 @@ public ToRFC2152ModifiedBase64Transform()
2020

2121
public void Dispose()
2222
{
23-
toBase64Transform?.Dispose();
24-
toBase64Transform = null;
23+
Dispose(true);
24+
GC.SuppressFinalize(this);
25+
}
26+
27+
protected virtual void Dispose(bool disposing)
28+
{
29+
if (disposing) {
30+
toBase64Transform?.Dispose();
31+
toBase64Transform = null;
32+
}
2533
}
2634

2735
public virtual int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)

src/Smdn.Fundamental.PrintableEncoding.ModifiedBase64/Smdn.Formats.ModifiedBase64/ToRFC3501ModifiedBase64Transform.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// SPDX-FileCopyrightText: 2009 smdn <smdn@smdn.jp>
22
// SPDX-License-Identifier: MIT
3-
using System;
43
using System.Security.Cryptography;
54

65
namespace Smdn.Formats.ModifiedBase64 {
@@ -14,7 +13,7 @@ public ToRFC3501ModifiedBase64Transform()
1413
{
1514
}
1615

17-
private void ReplaceOutput(byte[] buffer, int offset, int count)
16+
private static void ReplaceOutput(byte[] buffer, int offset, int count)
1817
{
1918
// "," is used instead of "/"
2019
while (0 < count--) {

0 commit comments

Comments
 (0)