Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ByteEncoder.cs
Go to the documentation of this file.
1using System.Text;
2
3namespace System.Net.Mime;
4
5internal abstract class ByteEncoder : IByteEncoder
6{
7 internal abstract WriteStateInfoBase WriteState { get; }
8
9 protected abstract bool HasSpecialEncodingForCRLF { get; }
10
11 public string GetEncodedString()
12 {
13 return Encoding.ASCII.GetString(WriteState.Buffer, 0, WriteState.Length);
14 }
15
16 public int EncodeBytes(byte[] buffer, int offset, int count, bool dontDeferFinalBytes, bool shouldAppendSpaceToCRLF)
17 {
18 WriteState.AppendHeader();
19 bool hasSpecialEncodingForCRLF = HasSpecialEncodingForCRLF;
20 int i;
21 for (i = offset; i < count + offset; i++)
22 {
23 if (LineBreakNeeded(buffer[i]))
24 {
26 WriteState.AppendCRLF(shouldAppendSpaceToCRLF);
27 }
28 if (hasSpecialEncodingForCRLF && IsCRLF(buffer, i, count + offset))
29 {
31 i++;
32 }
33 else
34 {
36 }
37 }
38 if (dontDeferFinalBytes)
39 {
41 }
42 WriteState.AppendFooter();
43 return i - offset;
44 }
45
46 public int EncodeString(string value, Encoding encoding)
47 {
48 if (encoding == Encoding.Latin1)
49 {
50 byte[] bytes = encoding.GetBytes(value);
51 return EncodeBytes(bytes, 0, bytes.Length, dontDeferFinalBytes: true, shouldAppendSpaceToCRLF: true);
52 }
53 WriteState.AppendHeader();
54 bool hasSpecialEncodingForCRLF = HasSpecialEncodingForCRLF;
55 int num = 0;
56 byte[] bytes2 = new byte[encoding.GetMaxByteCount(2)];
57 for (int i = 0; i < value.Length; i++)
58 {
59 int codepointSize = GetCodepointSize(value, i);
60 int bytes3 = encoding.GetBytes(value, i, codepointSize, bytes2, 0);
61 if (codepointSize == 2)
62 {
63 i++;
64 }
65 if (LineBreakNeeded(bytes2, bytes3))
66 {
68 WriteState.AppendCRLF(includeSpace: true);
69 }
70 if (hasSpecialEncodingForCRLF && IsCRLF(bytes2, bytes3))
71 {
73 }
74 else
75 {
76 AppendEncodedCodepoint(bytes2, bytes3);
77 }
78 num += bytes3;
79 }
81 WriteState.AppendFooter();
82 return num;
83 }
84
85 protected abstract void AppendEncodedCRLF();
86
87 protected abstract bool LineBreakNeeded(byte b);
88
89 protected abstract bool LineBreakNeeded(byte[] bytes, int count);
90
91 protected abstract int GetCodepointSize(string value, int i);
92
93 public abstract void AppendPadding();
94
95 protected abstract void ApppendEncodedByte(byte b);
96
97 private void AppendEncodedCodepoint(byte[] bytes, int count)
98 {
99 for (int i = 0; i < count; i++)
100 {
102 }
103 }
104
105 protected bool IsSurrogatePair(string value, int i)
106 {
107 if (char.IsSurrogate(value[i]) && i + 1 < value.Length)
108 {
109 return char.IsSurrogatePair(value[i], value[i + 1]);
110 }
111 return false;
112 }
113
114 protected bool IsCRLF(byte[] bytes, int count)
115 {
116 if (count == 2)
117 {
118 return IsCRLF(bytes, 0, count);
119 }
120 return false;
121 }
122
123 private bool IsCRLF(byte[] buffer, int i, int bufferSize)
124 {
125 if (buffer[i] == 13 && i + 1 < bufferSize)
126 {
127 return buffer[i + 1] == 10;
128 }
129 return false;
130 }
131}
void ApppendEncodedByte(byte b)
bool LineBreakNeeded(byte[] bytes, int count)
bool IsSurrogatePair(string value, int i)
int EncodeBytes(byte[] buffer, int offset, int count, bool dontDeferFinalBytes, bool shouldAppendSpaceToCRLF)
bool IsCRLF(byte[] buffer, int i, int bufferSize)
void AppendEncodedCodepoint(byte[] bytes, int count)
int EncodeString(string value, Encoding encoding)
int GetCodepointSize(string value, int i)
bool IsCRLF(byte[] bytes, int count)
static Encoding Latin1
Definition Encoding.cs:513
static Encoding ASCII
Definition Encoding.cs:511
virtual byte[] GetBytes(char[] chars)
Definition Encoding.cs:781
int GetMaxByteCount(int charCount)