Terraria v1.4.4.9
Terraria source code documentation
All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros
BinXmlSqlDecimal.cs
Go to the documentation of this file.
2
3namespace System.Xml;
4
5internal struct BinXmlSqlDecimal
6{
7 internal byte m_bLen;
8
9 internal byte m_bPrec;
10
11 internal byte m_bScale;
12
13 internal byte m_bSign;
14
15 internal uint m_data1;
16
17 internal uint m_data2;
18
19 internal uint m_data3;
20
21 internal uint m_data4;
22
23 public bool IsPositive => m_bSign == 0;
24
25 public BinXmlSqlDecimal(byte[] data, int offset, bool trim)
26 {
27 m_bLen = data[offset] switch
28 {
29 7 => 1,
30 11 => 2,
31 15 => 3,
32 19 => 4,
33 _ => throw new XmlException(System.SR.XmlBinary_InvalidSqlDecimal, (string[])null),
34 };
35 m_bPrec = data[offset + 1];
36 m_bScale = data[offset + 2];
37 m_bSign = ((data[offset + 3] == 0) ? ((byte)1) : ((byte)0));
38 m_data1 = UIntFromByteArray(data, offset + 4);
39 m_data2 = ((m_bLen > 1) ? UIntFromByteArray(data, offset + 8) : 0u);
40 m_data3 = ((m_bLen > 2) ? UIntFromByteArray(data, offset + 12) : 0u);
41 m_data4 = ((m_bLen > 3) ? UIntFromByteArray(data, offset + 16) : 0u);
42 if (m_bLen == 4 && m_data4 == 0)
43 {
44 m_bLen = 3;
45 }
46 if (m_bLen == 3 && m_data3 == 0)
47 {
48 m_bLen = 2;
49 }
50 if (m_bLen == 2 && m_data2 == 0)
51 {
52 m_bLen = 1;
53 }
54 if (trim)
55 {
57 }
58 }
59
60 private static uint UIntFromByteArray(byte[] data, int offset)
61 {
63 }
64
65 private static void MpDiv1(uint[] rgulU, ref int ciulU, uint iulD, out uint iulR)
66 {
67 uint num = 0u;
68 ulong num2 = iulD;
69 int num3 = ciulU;
70 while (num3 > 0)
71 {
72 num3--;
73 ulong num4 = ((ulong)num << 32) + rgulU[num3];
74 rgulU[num3] = (uint)(num4 / num2);
75 num = (uint)(num4 - rgulU[num3] * num2);
76 }
77 iulR = num;
79 }
80
81 private static void MpNormalize(uint[] rgulU, ref int ciulU)
82 {
83 while (ciulU > 1 && rgulU[ciulU - 1] == 0)
84 {
85 ciulU--;
86 }
87 }
88
89 private static char ChFromDigit(uint uiDigit)
90 {
91 return (char)(uiDigit + 48);
92 }
93
94 public decimal ToDecimal()
95 {
96 if (m_data4 != 0 || m_bScale > 28)
97 {
98 throw new XmlException(System.SR.SqlTypes_ArithOverflow, (string)null);
99 }
100 return new decimal((int)m_data1, (int)m_data2, (int)m_data3, !IsPositive, m_bScale);
101 }
102
103 private void TrimTrailingZeros()
104 {
105 uint[] array = new uint[4] { m_data1, m_data2, m_data3, m_data4 };
106 int ciulU = m_bLen;
107 if (ciulU == 1 && array[0] == 0)
108 {
109 m_bScale = 0;
110 return;
111 }
112 while (m_bScale > 0 && (ciulU > 1 || array[0] != 0))
113 {
115 if (iulR != 0)
116 {
117 break;
118 }
119 m_data1 = array[0];
120 m_data2 = array[1];
121 m_data3 = array[2];
122 m_data4 = array[3];
123 m_bScale--;
124 }
125 if (m_bLen == 4 && m_data4 == 0)
126 {
127 m_bLen = 3;
128 }
129 if (m_bLen == 3 && m_data3 == 0)
130 {
131 m_bLen = 2;
132 }
133 if (m_bLen == 2 && m_data2 == 0)
134 {
135 m_bLen = 1;
136 }
137 }
138
139 public override string ToString()
140 {
141 uint[] array = new uint[4] { m_data1, m_data2, m_data3, m_data4 };
142 int ciulU = m_bLen;
143 char[] array2 = new char[39];
144 int num = 0;
145 while (ciulU > 1 || array[0] != 0)
146 {
148 array2[num++] = ChFromDigit(iulR);
149 }
150 while (num <= m_bScale)
151 {
152 array2[num++] = ChFromDigit(0u);
153 }
154 bool isPositive = IsPositive;
155 int num2 = (isPositive ? num : (num + 1));
156 if (m_bScale > 0)
157 {
158 num2++;
159 }
160 char[] array3 = new char[num2];
161 int num3 = 0;
162 if (!isPositive)
163 {
164 array3[num3++] = '-';
165 }
166 while (num > 0)
167 {
168 if (num-- == m_bScale)
169 {
170 array3[num3++] = '.';
171 }
172 array3[num3++] = array2[num];
173 }
174 return new string(array3);
175 }
176}
static uint ReadUInt32LittleEndian(ReadOnlySpan< byte > source)
static string XmlBinary_InvalidSqlDecimal
Definition SR.cs:1368
static string SqlTypes_ArithOverflow
Definition SR.cs:1390
Definition SR.cs:7
BinXmlSqlDecimal(byte[] data, int offset, bool trim)
static char ChFromDigit(uint uiDigit)
static void MpNormalize(uint[] rgulU, ref int ciulU)
static void MpDiv1(uint[] rgulU, ref int ciulU, uint iulD, out uint iulR)
static uint UIntFromByteArray(byte[] data, int offset)