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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
|
//------------------------------------------------------------------------------
// <copyright file="DbBuffer.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Data.ProviderBase
{
using System;
using System.Data.Common;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using System.Threading;
// DbBuffer is abstract to require derived class to exist
// so that when debugging, we can tell the difference between one DbBuffer and another
internal abstract class DbBuffer : SafeHandle {
internal const int LMEM_FIXED = 0x0000;
internal const int LMEM_MOVEABLE = 0x0002;
internal const int LMEM_ZEROINIT = 0x0040;
private readonly int _bufferLength;
private DbBuffer(int initialSize, bool zeroBuffer) : base(IntPtr.Zero, true) {
if (0 < initialSize) {
int flags = ((zeroBuffer) ? LMEM_ZEROINIT : LMEM_FIXED);
_bufferLength = initialSize;
RuntimeHelpers.PrepareConstrainedRegions();
try {} finally {
base.handle = SafeNativeMethods.LocalAlloc(flags, (IntPtr)initialSize);
}
if (IntPtr.Zero == base.handle) {
throw new OutOfMemoryException();
}
}
}
protected DbBuffer(int initialSize) : this(initialSize, true) {
}
protected DbBuffer(IntPtr invalidHandleValue, bool ownsHandle) : base(invalidHandleValue, ownsHandle) {
}
private int BaseOffset { get { return 0; } }
public override bool IsInvalid {
get {
return (IntPtr.Zero == base.handle);
}
}
internal int Length {
get {
return _bufferLength;
}
}
internal string PtrToStringUni(int offset) {
offset += BaseOffset;
Validate(offset, 2);
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
string value = null;
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
int length = UnsafeNativeMethods.lstrlenW(ptr);
Validate(offset, (2*(length+1)));
value = Marshal.PtrToStringUni(ptr, length);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
return value;
}
internal String PtrToStringUni(int offset, int length) {
offset += BaseOffset;
Validate(offset, 2*length);
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
string value = null;
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
value = Marshal.PtrToStringUni(ptr, length);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
return value;
}
internal byte ReadByte(int offset) {
offset += BaseOffset;
ValidateCheck(offset, 1);
Debug.Assert(0 == offset%4, "invalid alignment");
byte value;
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = DangerousGetHandle();
value = Marshal.ReadByte(ptr, offset);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
return value;
}
internal byte[] ReadBytes(int offset, int length) {
byte[] value = new byte[length];
return ReadBytes(offset, value, 0, length);
}
internal byte[] ReadBytes(int offset, byte[] destination, int startIndex, int length) {
offset += BaseOffset;
Validate(offset, length);
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
Debug.Assert(null != destination, "null destination");
Debug.Assert(startIndex + length <= destination.Length, "destination too small");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
Marshal.Copy(ptr, destination, startIndex, length);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
return destination;
}
internal Char ReadChar(int offset) {
short value = ReadInt16(offset);
return unchecked((char)value);
}
internal char[] ReadChars(int offset, char[] destination, int startIndex, int length) {
offset += BaseOffset;
Validate(offset, 2*length);
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
Debug.Assert(null != destination, "null destination");
Debug.Assert(startIndex + length <= destination.Length, "destination too small");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
Marshal.Copy(ptr, destination, startIndex, length);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
return destination;
}
internal Double ReadDouble(int offset) {
Int64 value = ReadInt64(offset);
return BitConverter.Int64BitsToDouble(value);
}
internal Int16 ReadInt16(int offset) {
offset += BaseOffset;
ValidateCheck(offset, 2);
Debug.Assert(0 == offset%2, "invalid alignment");
short value;
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = DangerousGetHandle();
value = Marshal.ReadInt16(ptr, offset);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
return value;
}
internal void ReadInt16Array(int offset, short[] destination, int startIndex, int length) {
offset += BaseOffset;
Validate(offset, 2*length);
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
Debug.Assert(null != destination, "null destination");
Debug.Assert(startIndex + length <= destination.Length, "destination too small");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
Marshal.Copy(ptr, destination, startIndex, length);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal Int32 ReadInt32(int offset) {
offset += BaseOffset;
ValidateCheck(offset, 4);
Debug.Assert(0 == offset%4, "invalid alignment");
int value;
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = DangerousGetHandle();
value = Marshal.ReadInt32(ptr, offset);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
return value;
}
internal void ReadInt32Array(int offset, int[] destination, int startIndex, int length) {
offset += BaseOffset;
Validate(offset, 4*length);
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
Debug.Assert(null != destination, "null destination");
Debug.Assert(startIndex + length <= destination.Length, "destination too small");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
Marshal.Copy(ptr, destination, startIndex, length);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal Int64 ReadInt64(int offset) {
offset += BaseOffset;
ValidateCheck(offset, 8);
Debug.Assert(0 == offset%IntPtr.Size, "invalid alignment");
long value;
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = DangerousGetHandle();
value = Marshal.ReadInt64(ptr, offset);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
return value;
}
internal IntPtr ReadIntPtr(int offset) {
offset += BaseOffset;
ValidateCheck(offset, IntPtr.Size);
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
IntPtr value;
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = DangerousGetHandle();
value = Marshal.ReadIntPtr(ptr, offset);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
return value;
}
internal unsafe Single ReadSingle(int offset) {
Int32 value = ReadInt32(offset);
return *(Single*)&value;
}
override protected bool ReleaseHandle() {
// NOTE: The SafeHandle class guarantees this will be called exactly once.
IntPtr ptr = base.handle;
base.handle = IntPtr.Zero;
if (IntPtr.Zero != ptr) {
SafeNativeMethods.LocalFree(ptr);
}
return true;
}
private void StructureToPtr(int offset, object structure) {
Debug.Assert(null != structure, "null structure");
offset += BaseOffset;
ValidateCheck(offset, Marshal.SizeOf(structure.GetType()));
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
Marshal.StructureToPtr(structure, ptr, false/*fDeleteOld*/);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal void WriteByte(int offset, byte value) {
offset += BaseOffset;
ValidateCheck(offset, 1);
Debug.Assert(0 == offset%4, "invalid alignment");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = DangerousGetHandle();
Marshal.WriteByte(ptr, offset, value);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal void WriteBytes(int offset, byte[] source, int startIndex, int length) {
offset += BaseOffset;
Validate(offset, length);
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
Debug.Assert(null != source, "null source");
Debug.Assert(startIndex + length <= source.Length, "source too small");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
Marshal.Copy(source, startIndex, ptr, length);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal void WriteCharArray(int offset, char[] source, int startIndex, int length) {
offset += BaseOffset;
Validate(offset, 2*length);
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
Debug.Assert(null != source, "null source");
Debug.Assert(startIndex + length <= source.Length, "source too small");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
Marshal.Copy(source, startIndex, ptr, length);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal void WriteDouble(int offset, Double value) {
WriteInt64(offset, BitConverter.DoubleToInt64Bits(value));
}
internal void WriteInt16(int offset, short value) {
offset += BaseOffset;
ValidateCheck(offset, 2);
Debug.Assert(0 == offset%2, "invalid alignment");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = DangerousGetHandle();
Marshal.WriteInt16(ptr, offset, value);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal void WriteInt16Array(int offset, short[] source, int startIndex, int length) {
offset += BaseOffset;
Validate(offset, 2*length);
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
Debug.Assert(null != source, "null source");
Debug.Assert(startIndex + length <= source.Length, "source too small");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
Marshal.Copy(source, startIndex, ptr, length);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal void WriteInt32(int offset, int value) {
offset += BaseOffset;
ValidateCheck(offset, 4);
Debug.Assert(0 == offset%4, "invalid alignment");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = DangerousGetHandle();
Marshal.WriteInt32(ptr, offset, value);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal void WriteInt32Array(int offset, int[] source, int startIndex, int length) {
offset += BaseOffset;
Validate(offset, 4*length);
Debug.Assert(0 == offset%ADP.PtrSize, "invalid alignment");
Debug.Assert(null != source, "null source");
Debug.Assert(startIndex + length <= source.Length, "source too small");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
Marshal.Copy(source, startIndex, ptr, length);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal void WriteInt64(int offset, long value) {
offset += BaseOffset;
ValidateCheck(offset, 8);
Debug.Assert(0 == offset%IntPtr.Size, "invalid alignment");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = DangerousGetHandle();
Marshal.WriteInt64(ptr, offset, value);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal void WriteIntPtr(int offset, IntPtr value) {
offset += BaseOffset;
ValidateCheck(offset, IntPtr.Size);
Debug.Assert(0 == offset%IntPtr.Size, "invalid alignment");
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = DangerousGetHandle();
Marshal.WriteIntPtr(ptr, offset, value);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal unsafe void WriteSingle(int offset, Single value) {
WriteInt32(offset, *(Int32*)&value);
}
internal void ZeroMemory() {
bool mustRelease = false;
RuntimeHelpers.PrepareConstrainedRegions();
try {
DangerousAddRef(ref mustRelease);
IntPtr ptr = DangerousGetHandle();
SafeNativeMethods.ZeroMemory(ptr, (IntPtr)Length);
}
finally {
if (mustRelease) {
DangerousRelease();
}
}
}
internal Guid ReadGuid(int offset) {
// faster than Marshal.PtrToStructure(offset, typeof(Guid))
byte[] buffer = new byte[16];
ReadBytes(offset, buffer, 0, 16);
return new Guid(buffer);
}
internal void WriteGuid(int offset, Guid value) {
// faster than Marshal.Copy(value.GetByteArray()
StructureToPtr(offset, value);
}
internal DateTime ReadDate(int offset) {
short[] buffer = new short[3];
ReadInt16Array(offset, buffer, 0, 3);
return new DateTime(
unchecked((ushort)buffer[0]), // Year
unchecked((ushort)buffer[1]), // Month
unchecked((ushort)buffer[2])); // Day
}
internal void WriteDate(int offset, DateTime value) {
short[] buffer = new short[3] {
unchecked((short)value.Year),
unchecked((short)value.Month),
unchecked((short)value.Day),
};
WriteInt16Array(offset, buffer, 0, 3);
}
internal TimeSpan ReadTime(int offset) {
short[] buffer = new short[3];
ReadInt16Array(offset, buffer, 0, 3);
return new TimeSpan(
unchecked((ushort)buffer[0]), // Hours
unchecked((ushort)buffer[1]), // Minutes
unchecked((ushort)buffer[2])); // Seconds
}
internal void WriteTime(int offset, TimeSpan value) {
short[] buffer = new short[3] {
unchecked((short)value.Hours),
unchecked((short)value.Minutes),
unchecked((short)value.Seconds),
};
WriteInt16Array(offset, buffer, 0, 3);
}
internal DateTime ReadDateTime(int offset) {
short[] buffer = new short[6];
ReadInt16Array(offset, buffer, 0, 6);
int ticks = ReadInt32(offset + 12);
DateTime value = new DateTime(
unchecked((ushort)buffer[0]), // Year
unchecked((ushort)buffer[1]), // Month
unchecked((ushort)buffer[2]), // Day
unchecked((ushort)buffer[3]), // Hours
unchecked((ushort)buffer[4]), // Minutes
unchecked((ushort)buffer[5])); // Seconds
return value.AddTicks(ticks / 100);
}
internal void WriteDateTime(int offset, DateTime value) {
int ticks = (int)(value.Ticks % 10000000L)*100;
short[] buffer = new short[6] {
unchecked((short)value.Year),
unchecked((short)value.Month),
unchecked((short)value.Day),
unchecked((short)value.Hour),
unchecked((short)value.Minute),
unchecked((short)value.Second),
};
WriteInt16Array(offset, buffer, 0, 6);
WriteInt32(offset + 12, ticks);
}
internal Decimal ReadNumeric(int offset) {
byte[] bits = new byte[20];
ReadBytes(offset, bits, 1, 19);
int[] buffer = new int[4];
buffer[3] = ((int) bits[2]) << 16; // scale
if (0 == bits[3]) {
buffer[3] |= unchecked((int)0x80000000); //sign
}
buffer[0] = BitConverter.ToInt32(bits, 4); // low
buffer[1] = BitConverter.ToInt32(bits, 8); // mid
buffer[2] = BitConverter.ToInt32(bits, 12); // high
if (0 != BitConverter.ToInt32(bits, 16)) {
throw ADP.NumericToDecimalOverflow();
}
return new Decimal(buffer);
}
internal void WriteNumeric(int offset, Decimal value, byte precision) {
int[] tmp = Decimal.GetBits(value);
byte[] buffer = new byte[20];
buffer[1] = precision;
Buffer.BlockCopy(tmp, 14, buffer, 2, 2); // copy sign and scale
buffer[3] = (Byte) ((0 == buffer[3]) ? 1 : 0); // flip sign for native
Buffer.BlockCopy(tmp, 0, buffer, 4, 12);
buffer[16] = 0;
buffer[17] = 0;
buffer[18] = 0;
buffer[19] = 0;
WriteBytes(offset, buffer, 1, 19);
}
[ConditionalAttribute("DEBUG")]
protected void ValidateCheck(int offset, int count) {
Validate(offset, count);
}
protected void Validate(int offset, int count) {
if ((offset < 0) || (count < 0) || (Length < checked(offset + count))) {
throw ADP.InternalError(ADP.InternalErrorCode.InvalidBuffer);
}
}
}
}
|