forked from ela-compil/BACnet
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathEncodeBuffer.cs
More file actions
117 lines (104 loc) · 2.89 KB
/
EncodeBuffer.cs
File metadata and controls
117 lines (104 loc) · 2.89 KB
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
namespace System.IO.BACnet.Serialize;
public class EncodeBuffer
{
public byte[] buffer; //buffer to serialize into
public int offset; //offset in buffer ... will go beyond max_offset (so that you may count what's needed)
public int max_offset; //don't write beyond this offset
public int serialize_counter; //used with 'min_limit'
public int min_limit; //don't write before this limit (used for segmentation)
public EncodeResult result;
public bool expandable;
public EncodeBuffer()
{
expandable = true;
buffer = new byte[128];
max_offset = buffer.Length - 1;
}
public EncodeBuffer(byte[] buffer, int offset)
{
if (buffer == null) buffer = new byte[0];
expandable = false;
this.buffer = buffer;
this.offset = offset;
max_offset = buffer.Length;
}
public void Increment()
{
if (offset < max_offset)
{
if (serialize_counter >= min_limit)
offset++;
serialize_counter++;
}
else
{
if (serialize_counter >= min_limit)
offset++;
}
}
public void Add(byte b)
{
if (offset < max_offset)
{
if (serialize_counter >= min_limit)
buffer[offset] = b;
}
else
{
if (expandable)
{
Array.Resize(ref buffer, buffer.Length * 2);
max_offset = buffer.Length - 1;
if (serialize_counter >= min_limit)
buffer[offset] = b;
}
else
result |= EncodeResult.NotEnoughBuffer;
}
Increment();
}
public void Add(byte[] buffer, int count)
{
for (var i = 0; i < count; i++)
Add(buffer[i]);
}
public int GetDiff(EncodeBuffer buffer)
{
var diff = Math.Abs(buffer.offset - offset);
diff = Math.Max(Math.Abs(buffer.serialize_counter - serialize_counter), diff);
return diff;
}
public EncodeBuffer Copy()
{
return new EncodeBuffer
{
buffer = buffer,
max_offset = max_offset,
min_limit = min_limit,
offset = offset,
result = result,
serialize_counter = serialize_counter,
expandable = expandable
};
}
public byte[] ToArray()
{
var ret = new byte[offset];
Array.Copy(buffer, 0, ret, 0, ret.Length);
return ret;
}
public void Reset(int newOffset)
{
offset = newOffset;
serialize_counter = 0;
result = EncodeResult.Good;
}
public override string ToString()
{
return offset + " - " + serialize_counter;
}
public int GetLength()
{
return Math.Min(offset, max_offset);
}
}