forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.cs
executable file
·162 lines (137 loc) · 4.97 KB
/
Parser.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
using System;
namespace QuickFix
{
/// <summary>
/// </summary>
public class Parser
{
private byte[] buffer_ = new byte[512];
int usedBufferLength = 0;
public void AddToStream(ref byte[] data, int bytesAdded)
{
if (buffer_.Length < usedBufferLength + bytesAdded)
System.Array.Resize<byte>(ref buffer_, (usedBufferLength + bytesAdded));
System.Buffer.BlockCopy(data, 0, buffer_, usedBufferLength , bytesAdded);
usedBufferLength += bytesAdded;
}
[Obsolete("Unused and will be removed in a future version.")]
public void AddToStream(string data)
{
byte[] bytes = CharEncoding.DefaultEncoding.GetBytes(data);
AddToStream(ref bytes, bytes.Length);
}
public bool ReadFixMessage(out string msg)
{
msg = "";
if(buffer_.Length < 2)
return false;
int pos = 0;
pos = IndexOf(buffer_, "8=", 0);
if(-1 == pos)
return false;
buffer_ = Remove(buffer_, pos);
pos = 0;
int length = 0;
try
{
if (!ExtractLength(out length, out pos, buffer_))
return false;
// pos is at first character past the BodyLength field (tag 9)
pos += length;
if (buffer_.Length < pos)
return false;
pos = IndexOf(buffer_, "\x01" + "10=", pos - 1);
if (-1 == pos)
return false; // no tag 10 received yet
pos += 4; // pos now just after "10="
pos = IndexOf(buffer_, "\x01", pos);
if (-1 == pos)
return false;
pos += 1;
msg = Substring(buffer_, 0, pos);
buffer_ = Remove(buffer_, pos);
return true;
}
catch (MessageParseError e)
{
if ((length > 0) && (pos <= buffer_.Length))
buffer_ = Remove(buffer_, pos);
else
buffer_ = Remove(buffer_, buffer_.Length);
throw e;
}
}
public bool ExtractLength(out int length, out int pos, string buf)
{
return ExtractLength(out length, out pos, CharEncoding.DefaultEncoding.GetBytes(buf));
}
public bool ExtractLength(out int length, out int pos, byte[] buf)
{
length = 0;
pos = 0;
if (buf.Length < 1)
return false;
int startPos = IndexOf(buf, "\x01" + "9=", 0);
if(-1 == startPos)
return false;
startPos +=3;
int endPos = IndexOf(buf, "\x01", startPos);
if(-1 == endPos)
return false;
string strLength = Substring(buf, startPos, endPos - startPos);
try
{
length = Fields.Converters.IntConverter.Convert(strLength);
if(length < 0)
throw new MessageParseError("Invalid BodyLength (" + length + ")");
}
catch(FieldConvertError e)
{
throw new MessageParseError(e.Message, e);
}
pos = endPos + 1;
return true;
}
private bool Fail(string what)
{
System.Console.WriteLine("Parser failed: " + what);
return false;
}
private int IndexOf(byte[] arrayToSearchThrough, string stringPatternToFind, int offset)
{
byte[] patternToFind = CharEncoding.DefaultEncoding.GetBytes(stringPatternToFind);
if (patternToFind.Length > arrayToSearchThrough.Length)
return -1;
for (int i = offset; i <= arrayToSearchThrough.Length - patternToFind.Length; i++)
{
bool found = true;
for (int j = 0; j < patternToFind.Length; j++)
{
if (arrayToSearchThrough[i + j] != patternToFind[j])
{
found = false;
break;
}
}
if (found)
{
return i;
}
}
return -1;
}
private byte[] Remove(byte[] array, int count)
{
byte[] returnByte = new byte[array.Length - count];
System.Buffer.BlockCopy(array, count, returnByte, 0, array.Length - count);
usedBufferLength -= count;
return returnByte;
}
private string Substring(byte[] array, int startIndex, int length)
{
byte[] returnByte = new byte[length];
System.Buffer.BlockCopy(array, startIndex, returnByte, 0, length);
return CharEncoding.DefaultEncoding.GetString(returnByte);
}
}
}