-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathStreamWriteExtensions.cs
263 lines (228 loc) · 8.86 KB
/
StreamWriteExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
using System;
using System.IO;
using System.Text;
namespace AdoNetCore.AseClient.Internal
{
internal static class StreamWriteExtensions
{
public static void Write(this Stream stream, byte[] bytes)
{
stream.Write(bytes, 0, bytes.Length);
}
public static void WriteBool(this Stream stream, bool value)
{
stream.WriteByte((byte)(value ? 1 : 0));
}
public static void WriteShort(this Stream stream, short value)
{
stream.Write(BitConverter.GetBytes(value));
}
public static void WriteUShort(this Stream stream, ushort value)
{
stream.Write(BitConverter.GetBytes(value));
}
private static void WriteRepeatedBytes(this Stream stream, byte value, int repeat)
{
for (var i = 0; i < repeat; i++)
{
stream.WriteByte(value);
}
}
public static void WriteInt(this Stream stream, int value)
{
stream.Write(BitConverter.GetBytes(value));
}
public static void WriteUInt(this Stream stream, uint value)
{
stream.Write(BitConverter.GetBytes(value));
}
public static void WriteLong(this Stream stream, long value)
{
stream.Write(BitConverter.GetBytes(value));
}
public static void WriteULong(this Stream stream, ulong value)
{
stream.Write(BitConverter.GetBytes(value));
}
public static void WriteFloat(this Stream stream, float value)
{
stream.Write(BitConverter.GetBytes(value));
}
public static void WriteDouble(this Stream stream, double value)
{
stream.Write(BitConverter.GetBytes(value));
}
/// <summary>
/// Encode and write-out the string, up to the maximum length. Pad any remaining bytes. Append string length byte.
/// If lengthModifier is supplied, it will be added to the appended length value.
/// </summary>
public static void WritePaddedString(this Stream stream, string value, int maxLength, Encoding enc, int lengthModifier = 0)
{
var bytes = enc.GetBytes(value);
if (bytes.Length <= maxLength)
{
stream.Write(bytes);
stream.WriteRepeatedBytes(0, maxLength - bytes.Length);
stream.WriteByte((byte)(bytes.Length + lengthModifier));
}
else
{
stream.Write(bytes, 0, maxLength);
stream.WriteByte((byte)(maxLength + lengthModifier));
}
}
public static void WriteWeirdPasswordString(this Stream stream, string password, int maxLength, Encoding enc)
{
stream.WriteByte(0); //
stream.WriteByte((byte)password.Length);
stream.WritePaddedString(password, maxLength, enc, 2); //add two bytes to the appended length value to account for the above two bytes
}
public static void WriteBytePrefixedString(this Stream stream, string value, Encoding enc)
{
var bytes = enc.GetBytes(value);
stream.WriteBytePrefixedByteArray(bytes);
}
public static void WriteIntPrefixedString(this Stream stream, string value, Encoding enc)
{
var bytes = enc.GetBytes(value);
stream.WriteIntPrefixedByteArray(bytes);
}
public static void WriteBytePrefixedByteArray(this Stream stream, byte[] value)
{
var len = (byte)value.Length;
stream.WriteByte(len);
stream.Write(value, 0, len);
}
public static void WriteNullableUShortPrefixedByteArray(this Stream stream, byte[] value)
{
var len = (ushort)(value?.Length ?? 0);
stream.WriteUShort(len);
if (len == 0)
{
return;
}
stream.Write(value, 0, len);
}
public static void WriteBlobSpecificIntPrefixedByteArray(this Stream stream, byte[] value)
{
var len = value.Length;
var endOfBlobLen = (uint) len | 0x80000000; //highest-order bit set means there is more data
stream.WriteUInt(endOfBlobLen);
stream.Write(value, 0, len);
}
public static void WriteBlobSpecificTerminator(this Stream stream)
{
var endOfBlobLen = (uint)0; //highest-order bit unset means end of blob data
stream.WriteUInt(endOfBlobLen);
}
public static void WriteIntPrefixedByteArray(this Stream stream, byte[] value)
{
var len = value.Length;
stream.WriteInt(len);
stream.Write(value, 0, len);
}
public static bool TryWriteBytePrefixedNull(this Stream stream, object value)
{
if (value == DBNull.Value)
{
stream.WriteByte(0);
return true;
}
return false;
}
public static bool TryWriteIntPrefixedNull(this Stream stream, object value)
{
if (value == DBNull.Value)
{
stream.WriteInt(0);
return true;
}
return false;
}
public static void WriteIntPartDateTime(this Stream stream, DateTime value)
{
var span = value - Constants.Sql.RegularDateTime.Epoch;
var day = span.Days;
var ticks = span.Ticks - (day * TimeSpan.TicksPerDay);
if (ticks < 0L)
{
day--;
ticks += TimeSpan.TicksPerDay;
}
var milliseconds = (double) ticks / TimeSpan.TicksPerMillisecond;
var time = (int)((milliseconds * Constants.Sql.RegularDateTime.TicksPerMillisecond) + 0.5);
stream.WriteInt(day);
stream.WriteInt(time);
}
public static void WriteBigDateTime(this Stream stream, DateTime value)
{
var timeSinceEpoch = value - Constants.Sql.BigDateTime.Epoch;
const long ticksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000;
var usSinceEpoch = timeSinceEpoch.Ticks / ticksPerMicrosecond;
var usSinceYearZero = usSinceEpoch + Constants.Sql.BigDateTime.EpochMicroSeconds;
stream.WriteByte(8); // length
stream.WriteLong(usSinceYearZero);
}
public static void WriteDate(this Stream stream, DateTime value)
{
var span = value - Constants.Sql.RegularDateTime.Epoch;
var day = span.Days;
if (span.Ticks - (day * TimeSpan.TicksPerDay) < 0L)
{
day--;
}
stream.WriteInt(day);
}
public static void WriteTime(this Stream stream, TimeSpan value)
{
var milliseconds = (double) value.Ticks / TimeSpan.TicksPerMillisecond;
var time = (int)((milliseconds * Constants.Sql.RegularDateTime.TicksPerMillisecond) + 0.5);
stream.WriteInt(time);
}
/// <summary>
/// Write a money value to the stream
/// Note: will always write the 8-byte TDS equivalent (long)
/// </summary>
public static void WriteMoney(this Stream stream, decimal value)
{
var buf = BitConverter.GetBytes(Convert.ToInt64(decimal.Truncate(value * 10000m)));
buf = new[]
{
buf[4], buf[5], buf[6], buf[7],
buf[0], buf[1], buf[2], buf[3]
};
stream.WriteByte(8);
stream.Write(buf, 0, buf.Length);
}
public static void WriteDecimal(this Stream stream, SqlDecimal value)
{
var requiredBytes = value.BytesRequired;
stream.WriteByte((byte)(1 + requiredBytes));
stream.WriteByte(value.IsPositive ? (byte)0 : (byte)1);
var data = value.BinData;
var buf = new byte[requiredBytes];
//reverse and copy the necessary bytes
var iData = 0;
for (var iBuf = buf.Length - 1; iBuf >= 0; iBuf--, iData++)
{
buf[iBuf] = data[iData];
}
stream.Write(buf, 0, buf.Length);
}
public static void WriteDecimal(this Stream stream, AseDecimal value)
{
var requiredBytes = value.BytesRequired;
stream.WriteByte((byte)(1 + requiredBytes));
stream.WriteByte(value.IsPositive ? (byte)0 : (byte)1);
var data = value.BinData;
var buf = new byte[requiredBytes];
//reverse and copy the necessary bytes
var iData = 0;
for (var iBuf = buf.Length - 1; iBuf >= 0; iBuf--, iData++)
{
buf[iBuf] = data[iData];
}
stream.Write(buf, 0, buf.Length);
}
}
}