forked from rqx110/SnmpSharpNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MutableByte.cs
795 lines (747 loc) · 23.9 KB
/
MutableByte.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
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
// This file is part of SNMP#NET.
//
// SNMP#NET is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// SNMP#NET is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SNMP#NET. If not, see <http://www.gnu.org/licenses/>.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
namespace SnmpSharpNet
{
/// <summary>Mutable byte implementation class</summary>
/// <remarks>
/// Mutable byte class allows for manipulation of a byte array with
/// operations like append, prepend.
///
/// Functionality is implemented through temporary buffer creation
/// and data duplication.
///
/// <code>
/// MutableByte buffer = new MutableByte();
/// buffer += "test data";
/// buffer.Append(". More test data");
/// buffer.Prepend("This is ");
/// Console.WriteLine(buffer.ToString()); // Prints out "This is test data. More test data"
/// buffer.RemoveBeginning(8); // The buffer now holds "test data. More test data"
/// buffer.Prepend("It could be "); // buffer is "It could be test data. More test data"
/// buffer.RemoveEnd(" More test data".Length); // buffer: "It could be test data."
/// buffer.Remove(12,5); // buffer: "It could be data"
/// Console.WriteLine("{0}",Convert.ToChar(buffer[1])); // Output: "t"
/// byte[] tmpBuffer = buffer; // Implicit conversion to byte[]
/// buffer.Reset(); // Erase all the data from the buffer
/// </code>
/// </remarks>
public class MutableByte: Object, ICloneable, IComparable<MutableByte>, IComparable<byte[]>
{
/// <summary>
/// Internal byte buffer
/// </summary>
private byte[] _buffer;
/// <summary>
/// Standard constructor. Initializes the internal buffer to null.
/// </summary>
public MutableByte()
{
}
/// <summary>
/// Constructor. Initialize internal buffer with supplied value.
/// </summary>
/// <param name="buf">Byte array to copy into internal buffer</param>
public MutableByte(byte[] buf)
{
if (buf != null)
{
_buffer = new byte[buf.Length];
System.Buffer.BlockCopy(buf, 0, _buffer, 0, buf.Length);
}
}
/// <summary>
/// Create new <see cref="MutableByte"/> class initialized by adding two byte buffers together. If
/// one of the supplied byte arrays is value null then <see cref="ArgumentNullException"/> is thrown.
/// </summary>
/// <param name="buf1">First byte array</param>
/// <param name="buf2">Second byte array</param>
/// <exception cref="ArgumentNullException">If one or both arguments are null or length 0</exception>
public MutableByte(byte[] buf1, byte[] buf2)
{
if (buf1 == null || buf1.Length == 0)
throw new ArgumentNullException("buf1");
if (buf2 == null || buf2.Length == 0)
throw new ArgumentNullException("buf2");
_buffer = new byte[buf1.Length + buf2.Length];
System.Buffer.BlockCopy(buf1, 0, _buffer, 0, buf1.Length);
System.Buffer.BlockCopy(buf2, 0, _buffer, buf1.Length, buf2.Length);
}
/// <summary>
/// Create new <see cref="MutableByte"/> class initialized with data from the supplied array up to length of buflen. Internaly,
/// a call is made to MutableByte.Set(buf[],int) to initialize the new class data buffer.
/// </summary>
/// <param name="buf">Array used to initialize the class data</param>
/// <param name="buflen">Number of bytes to use from the argument array to initialize the class.</param>
public MutableByte(byte[] buf, int buflen)
{
Set(buf, buflen);
}
/// <summary>
/// Get byte[] buffer value. This property is internal because it exposes the internal byte array.
/// </summary>
internal byte[] Value
{
get
{
return _buffer;
}
}
/// <summary>
/// Byte buffer current length
/// </summary>
public int Length
{
get
{
if (_buffer == null)
{
return 0;
}
return _buffer.Length;
}
}
/// <summary>
/// Set internal buffer to supplied value. Overwrites existing data.
/// </summary>
/// <param name="buf">Value to copy into internal buffer</param>
public void Set(byte[] buf)
{
_buffer = null;
if (buf == null || buf.Length == 0)
return;
_buffer = new byte[buf.Length];
Buffer.BlockCopy(buf, 0, _buffer, 0, buf.Length);
}
/// <summary>
/// Copy source buffer array up to length into the class.
/// </summary>
/// <param name="buf">Source byte array</param>
/// <param name="length">Number of items to copy</param>
/// <exception cref="ArgumentNullException">Thrown if buf argument is null or length of zero</exception>
public void Set(byte[] buf, int length)
{
_buffer = null;
if (buf == null || buf.Length == 0)
throw new ArgumentNullException("buf","Byte array is null.");
_buffer = new byte[length];
Buffer.BlockCopy(buf, 0, _buffer, 0, length);
}
/// <summary>
/// Set internal buffer to size 1 and copy supplied byte value into it
/// </summary>
/// <param name="buf">Byte value to copy into internal byte array of size 1</param>
public void Set(byte buf)
{
_buffer = new byte[1];
_buffer[0] = buf;
}
/// <summary>
/// Set value at specified position to the supplied value
/// </summary>
/// <param name="position">Zero based offset from the beginning of the buffer</param>
/// <param name="value">Value to set</param>
public void Set(int position, byte value)
{
if (position < 0 || position >= Length)
{
return;
}
_buffer[position] = value;
}
/// <summary>
/// Set class value to the contents of the supplied array starting from offset with specified length
/// </summary>
/// <param name="value">Value to set the class to</param>
/// <param name="offset">From the value start copying data from this offset</param>
/// <param name="length">Byte count to copy</param>
public void Set(byte[] value, int offset, int length)
{
if (offset < 0 || length < 0 || (offset + length) > value.Length)
throw new ArgumentOutOfRangeException();
_buffer = new byte[length];
Buffer.BlockCopy(value, offset, _buffer, 0, length);
}
/// <summary>
/// Set class value with bytes from the string. UTF8 encoding is assumed.
/// </summary>
/// <param name="value">String value</param>
public void Set(string value)
{
if (value == null || value.Length <= 0)
_buffer = null;
else
Set(ASCIIEncoding.UTF8.GetBytes(value));
}
/// <summary>
/// Append data to the internal buffer
/// </summary>
/// <param name="buf">Byte array to append to the internal byte array</param>
/// <exception cref="ArgumentNullException">Thrown when argument buf is null or length of zero</exception>
public void Append(byte[] buf)
{
if( buf == null || buf.Length == 0 )
{
// throw new ArgumentNullException("buf");
return; // null value received. nothing to append
}
if (_buffer == null)
{
Set(buf);
}
else
{
int oldLen = _buffer.Length;
Array.Resize(ref _buffer, _buffer.Length + buf.Length);
Buffer.BlockCopy(buf, 0, _buffer, oldLen, buf.Length);
}
}
/// <summary>
/// Append a single byte value to the internal buffer
/// </summary>
/// <param name="buf">Byte value to append to the internal buffer</param>
public void Append(byte buf)
{
if (_buffer == null)
{
Set(buf);
}
else
{
Array.Resize(ref _buffer, _buffer.Length + 1);
_buffer[_buffer.Length-1] = buf;
}
}
/// <summary>
/// Insert byte array at position
/// </summary>
/// <param name="position">Insert position</param>
/// <param name="buf">Byte array to insert at specified position</param>
public void Insert(int position, byte[] buf)
{
if (position < 0 || position >= this.Length)
{
throw new ArgumentOutOfRangeException("position","Index outside of the buffer scope");
}
if (buf == null)
{
throw new ArgumentNullException("buf");
}
if (position == 0)
{
Prepend(buf);
return;
}
byte[] tmp = new byte[_buffer.Length + buf.Length];
Buffer.BlockCopy(_buffer, 0, tmp, 0, position);
Buffer.BlockCopy(buf, 0, tmp, position, buf.Length);
Buffer.BlockCopy(_buffer, position, tmp, position + buf.Length, _buffer.Length - position);
_buffer = tmp;
}
/// <summary>
/// Insert single byte at specified location
/// </summary>
/// <param name="position">Location to perform insert (0 based)</param>
/// <param name="buf">Byte value to insert</param>
public void Insert(int position, byte buf)
{
if (position < 0 || position >= this.Length)
{
throw new ArgumentOutOfRangeException("position","Index outside of the buffer scope");
}
if (position == 0)
{
Prepend(buf);
return;
}
byte[] tmp = new byte[_buffer.Length + 1];
Buffer.BlockCopy(_buffer, 0, tmp, 0, position);
tmp[position] = buf;
Buffer.BlockCopy(_buffer, position, tmp, position + 2, _buffer.Length - position);
_buffer = tmp;
}
/// <summary>
/// Prepend (insert at beginning) a byte array
/// </summary>
/// <param name="buf">Byte array to prepend</param>
public void Prepend(byte[] buf)
{
if (this.Length <= 0)
{
// Don't throw an exception, just change the call to set
Set(buf);
}
else
{
byte[] tmp = new byte[_buffer.Length + buf.Length];
Buffer.BlockCopy(buf, 0, tmp, 0, buf.Length);
Buffer.BlockCopy(_buffer, 0, tmp, buf.Length, _buffer.Length);
_buffer = tmp;
}
}
/// <summary>
/// Prepend (add at the beginning) a single byte value
/// </summary>
/// <param name="buf">Byte value to prepend</param>
public void Prepend(byte buf)
{
if (this.Length <= 0)
{
// Don't throw an exception, just change the call to set
Set(buf);
}
else
{
byte[] tmp = new byte[_buffer.Length + 1];
tmp[0] = buf;
Buffer.BlockCopy(_buffer, 0, tmp, 1, _buffer.Length);
_buffer = tmp;
}
}
/// <summary>
/// Remove bytes from the beginning of the array
/// </summary>
/// <param name="count">Number of bytes to remove</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when count points beyond the bounds of the internal byte array</exception>
public void RemoveBeginning(int count)
{
if (this.Length == 0)
{
throw new ArgumentOutOfRangeException("count","Buffer is length 0. Unable to remove members.");
}
if (count > _buffer.Length)
{
throw new ArgumentOutOfRangeException("count","Byte count is greater then the length of the array");
}
if (count == this.Length)
{
// Remove all values
Reset();
return;
}
byte[] tmp = new byte[_buffer.Length - count];
Buffer.BlockCopy(_buffer, count, tmp, 0, _buffer.Length - count);
_buffer = tmp;
}
/// <summary>
/// Remove number of byte values from the end of the internal buffer
/// </summary>
/// <param name="count">Number of bytes to remove</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when count points beyond the bounds of the internal byte array</exception>
public void RemoveEnd(int count)
{
if (this.Length == 0)
{
throw new ArgumentOutOfRangeException("count","Buffer is length 0. Unable to remove members.");
}
if (count > _buffer.Length)
{
throw new ArgumentOutOfRangeException("count", "Byte count is greater then the length of the array");
}
if (count == this.Length)
{
// Remove all values
Reset();
return;
}
Array.Resize(ref _buffer, _buffer.Length - count);
}
/// <summary>
/// Remove array byte members starting with position start for the length length bytes.
/// </summary>
/// <param name="start">Start position of bytes to remove</param>
/// <param name="count">How many bytes to remove</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown if internal buffer is null or length zero, if start argument
/// is less then zero or past the end of the internal byte array, and if argument count is greater then length of the
/// internal byte array, start + count is past greater then the length of the buffer array or if argument count is less then 1.</exception>
public void Remove(int start, int count)
{
if (_buffer.Length == 0)
{
throw new ArgumentOutOfRangeException("start","Byte array is empty. Unable to remove members.");
}
if (start < 0 || start >= this.Length)
{
throw new ArgumentOutOfRangeException("start","Start argument is beyond the bounds of the array.");
}
if (count > this.Length || (start + count) > this.Length || count < 1)
{
throw new ArgumentOutOfRangeException("count", "Length argument is beyond the bounds of the array.");
}
if (start == 0)
{
RemoveBeginning(count);
}
else if (start + count == this.Length)
{
RemoveEnd(count);
}
else
{
byte[] tmp = new byte[_buffer.Length - count];
// Copy data from the start of the existing array
Buffer.BlockCopy(_buffer, 0, tmp, 0, start);
// Copy data from the end of the existing array
Buffer.BlockCopy(_buffer, start + count, tmp, start, _buffer.Length - start - count);
_buffer = tmp;
}
}
/// <summary>
/// Get sub-array
/// </summary>
/// <param name="position">Start of the sub-array. Zero based.</param>
/// <param name="length">Count of bytes to copy</param>
/// <returns>MutableByte array containing the sub-array.</returns>
/// <exception cref="OverflowException">Thrown when position starts before the beginning of the array (position is less then 0) or
/// position + length is greater then the length of the byte array contained in the object.</exception>
public MutableByte Get(int position, int length)
{
if( _buffer.Length <= position || _buffer.Length < (position + length) )
throw new OverflowException("Buffer is too small to extract sub-array.\r\n" + string.Format("buffer length: {0} offset: {1} length: {2}",_buffer.Length, position, length));
byte[] buf = new byte[length];
Buffer.BlockCopy(_buffer, position, buf, 0, length);
return new MutableByte(buf);
}
/// <summary>
/// Add <see cref="MutableByte"/> and byte array values into a new MutableByte class.
/// </summary>
/// <param name="buf1"><see cref="MutableByte"/> class</param>
/// <param name="buf2"><see cref="MutableByte"/> class</param>
/// <returns>New <see cref="MutableByte"/> class containing concatenated result</returns>
public static MutableByte operator +(MutableByte buf1, byte[] buf2)
{
return new MutableByte(buf1.Value, buf2);
}
/// <summary>
/// Add <see cref="MutableByte"/> buffer values.
/// </summary>
/// <param name="buf1"><see cref="MutableByte"/> class</param>
/// <param name="buf2"><see cref="MutableByte"/> class</param>
/// <returns>New <see cref="MutableByte"/> class containing concatenated result</returns>
public static MutableByte operator +(MutableByte buf1, MutableByte buf2)
{
return new MutableByte(buf1, buf2);
}
/// <summary>
/// Add a MutableByte array and a single byte value
/// </summary>
/// <param name="buf1">MutableByte array</param>
/// <param name="b">Byte value</param>
/// <returns>New MutableByte array with values added</returns>
public static MutableByte operator +(MutableByte buf1, byte b)
{
MutableByte tmp = new MutableByte(buf1.Value);
tmp.Append(b);
return tmp;
}
/// <summary>
/// Compare two <see cref="MutableByte"/> class contents
/// </summary>
/// <param name="buf1"><see cref="MutableByte"/> class</param>
/// <param name="buf2"><see cref="MutableByte"/> class</param>
/// <returns>true if the same, otherwise falseB</returns>
public static bool operator ==(MutableByte buf1,MutableByte buf2)
{
if (((Object)buf1) == null && ((Object)buf2) == null)
return true;
if (((Object)buf1) == null || ((Object)buf2) == null)
return false;
return buf1.Equals(buf2);
}
/// <summary>
/// Negative compare.
/// </summary>
/// <param name="buf1">First MutableByte array</param>
/// <param name="buf2">Second MutableByte array</param>
/// <returns>true if class values are not equal, otherwise false.</returns>
public static bool operator !=(MutableByte buf1, MutableByte buf2)
{
if (((Object)buf1) == null && ((Object)buf2) == null)
return false;
if (((Object)buf1) == null || ((Object)buf2) == null)
return true;
return !(buf1.Equals(buf2));
}
/// <summary>
/// Allow implicit casting of this object as a byte array for any callers.
/// </summary>
/// <param name="obj">MutableByte object whose values should be cast as byte array</param>
/// <returns>Byte array represented in the MutableObject class.</returns>
public static implicit operator byte[](MutableByte obj)
{
return obj.Value;
}
/// <summary>
/// Index operator. Index access to the underlying byte array
/// </summary>
/// <param name="index">Index to access</param>
/// <returns>byte array value</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown if requested byte index is outside the size of the internal byte array</exception>
public byte this[int index]
{
get
{
if (index < 0 || index >= this.Length)
return (byte)0x00; // don't throw an exception here
return _buffer[index];
}
set
{
if (index < 0 || index >= this.Length)
return; // don't throw an exception here
_buffer[index] = value;
}
}
/* Following two operators are a total waste of time (why would you want to compare two byte buffers). Anyway, it's here to shut FxCop up */
/// <summary>
/// Lesser then operator overload.
/// </summary>
/// <param name="firstClass">First MutableByte class</param>
/// <param name="secondClass">Second MutableByte class</param>
/// <returns>True if firstClass is lesser then second class, otherwise false</returns>
public static bool operator <(MutableByte firstClass, MutableByte secondClass)
{
if (firstClass == null && secondClass == null)
return false;
if (firstClass == null && secondClass != null)
return true; // If first one is null then it is less then second one
if (firstClass != null && secondClass == null)
return false;
if (firstClass.CompareTo(secondClass) < 0)
return true;
return false;
}
/// <summary>
/// Greater then operator overload.
/// </summary>
/// <param name="firstClass">First MutableByte class</param>
/// <param name="secondClass">Second MutableByte class</param>
/// <returns>True if firstClass is greater then second class, otherwise false</returns>
public static bool operator >(MutableByte firstClass, MutableByte secondClass)
{
if (firstClass == null && secondClass == null)
return false;
if (firstClass == null && secondClass != null)
return false; // If first one is null then it is less then second one
if (firstClass != null && secondClass == null)
return true;
if (firstClass.CompareTo(secondClass) < 0)
return false;
return true;
}
/// <summary>
/// Compares MutableByte object against another MutableByte object or a byte array
/// </summary>
/// <param name="obj">Object to compare class value with. Argument can be a byte array or an instance of MutableByte class.</param>
/// <returns>Returns true if objects match, otherwise false.</returns>
public override bool Equals(object obj)
{
if (obj == null)
return false;
if (obj is MutableByte)
{
MutableByte b = obj as MutableByte;
if (this.CompareTo(b) == 0)
return true;
}
else if (obj is byte[])
{
byte[] b = obj as byte[];
if( this.CompareTo(b) == 0 )
return true;
}
return false;
}
/// <summary>
/// Compare two byte arrays
/// </summary>
/// <param name="buf1">First byte array</param>
/// <param name="buf2">Second byte array</param>
/// <returns>True if array contents are the same, otherwise false.</returns>
public static bool Equals(byte[] buf1, byte[] buf2)
{
if (buf1.Length != buf2.Length)
{
return false;
}
for (int i = 0; i < buf1.Length; i++)
{
if (buf1[i] != buf2[i])
{
return false;
}
}
return true;
}
/// <summary>
/// Returns object hash code. Just calls the base class implementation.
/// </summary>
/// <returns>Base class hash code value</returns>
public override int GetHashCode()
{
return base.GetHashCode();
}
/// <summary>
/// Convert array to a string
/// </summary>
/// <returns>String representation of the object as a hex string</returns>
public override string ToString()
{
if (_buffer == null)
{
return "";
}
StringBuilder str = new StringBuilder();
for (int i = 0; i < _buffer.Length; i++)
{
str.Append(string.Format("{0:x02} ", _buffer[i]));
if (i > 0 && i < (_buffer.Length - 1 ) && (i % 16) == 0)
{
str.Append("\n");
}
else if( i < (_buffer.Length - 1 ) )
{
str.Append(" ");
}
}
return str.ToString();
}
/// <summary>
/// Hexadecimal data dump of the specific range of buffer values
/// </summary>
/// <param name="start">Start position for data dump (0 based)</param>
/// <param name="length">Number of bytes to include in the dump</param>
/// <returns>String representation of the selected range.</returns>
/// <exception cref="ArgumentOutOfRangeException">Thrown when start and length arguments point to internal byte array locations that are outside of the array bounds.</exception>
public string ToString(int start, int length)
{
if (_buffer == null)
{
return "";
}
if (_buffer.Length <= start || _buffer.Length < (start + length))
throw new ArgumentOutOfRangeException("start","Range specification past boundaries of the buffer.");
StringBuilder output = new StringBuilder();
StringBuilder dec = new StringBuilder();
int pcnt = 0;
output.AppendFormat("{0:d03} ", start);
for (int i = start; i < (start + length); i++)
{
output.AppendFormat("{0:x2}", _buffer[i]);
if (_buffer[i] > 31 && _buffer[i] < 128)
{
dec.Append(Convert.ToChar(_buffer[i]));
}
++pcnt;
if (pcnt == 16)
{
output.Append(" ");
output.Append(dec.ToString());
output.Append("\n");
output.AppendFormat("{0:d03} ", (i + 1));
dec.Remove(0, dec.Length);
pcnt = 0;
}
else
{
output.Append(" ");
}
}
return output.ToString();
}
/// <summary>
/// Clone object
/// </summary>
/// <returns>Cloned copy of the object cast as <see cref="Object"/></returns>
public object Clone()
{
return new MutableByte(_buffer);
}
/// <summary>
/// Reset object data to null
/// </summary>
public void Reset()
{
_buffer = null;
}
/// <summary>
/// Reset object data to null
/// </summary>
public void Clear()
{
_buffer = null;
}
/// <summary>
/// Compare class to another MutableByte class.
/// </summary>
/// <param name="other">Class to compare with</param>
/// <returns>-1 if class is less then, 0 if equal or 1 if greater then class it's compared against</returns>
public int CompareTo(MutableByte other)
{
if (other.Length > this.Length)
{
return -1;
}
else if (other.Length < this.Length)
{
return 1;
}
for (int i = 0; i < this.Length; i++)
{
if (other.Value[i] > this.Value[i])
{
return -1;
}
else if (other.Value[i] < this.Value[i])
{
return 1;
}
}
return 0;
}
/// <summary>
/// Compare class to a byte[] array.
/// </summary>
/// <param name="other">Byte array to compare with</param>
/// <returns>-1 if class is less then, 0 if equal or 1 if greater then array it's compared against</returns>
public int CompareTo(byte[] other)
{
if (other.Length > this.Length)
{
return -1;
}
else if (other.Length < this.Length)
{
return 1;
}
for (int i = 0; i < this.Length; i++)
{
if (other[i] > this.Value[i])
{
return -1;
}
else if (other[i] < this.Value[i])
{
return 1;
}
}
return 0;
}
}
}