Skip to content

Commit d55d2f6

Browse files
authored
Merge branch 'thomas-v2:master' into master
2 parents 70aa39c + 9bb7021 commit d55d2f6

File tree

7 files changed

+52
-74
lines changed

7 files changed

+52
-74
lines changed

src/S7CommPlusDriver/ClientApi/Browser.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,9 @@ private bool IsSoftdatatypeSupported(uint softdatatype)
355355
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMIDENT:
356356
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTANY:
357357
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTATT:
358-
case Softdatatype.S7COMMP_SOFTDATATYPE_FOLDER: // Should we support this internal datatype? Only used internally in SFBs
358+
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMAID:
359359
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMLINK:
360+
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTHWINT:
360361
case Softdatatype.S7COMMP_SOFTDATATYPE_HWANY:
361362
case Softdatatype.S7COMMP_SOFTDATATYPE_HWIOSYSTEM:
362363
case Softdatatype.S7COMMP_SOFTDATATYPE_HWDPMASTER:

src/S7CommPlusDriver/ClientApi/PlcTags.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,12 @@ public static PlcTag TagFactory(string name, ItemAddress address, uint softdatat
162162
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTATT:
163163
return new PlcTagDWord(name, address, softdatatype);
164164

165-
case Softdatatype.S7COMMP_SOFTDATATYPE_FOLDER:
166-
// Softdatatype 132: This type is only used as parameter for internal SFBs (e.g. AID input parameter)
167-
// Length of value (4 byte) calculated from the offsetinfo byte addresses.
165+
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMAID:
168166
return new PlcTagDWord(name, address, softdatatype);
169-
170167
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMLINK:
171168
return new PlcTagDWord(name, address, softdatatype);
172-
169+
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTHWINT:
170+
return new PlcTagDWord(name, address, softdatatype);
173171
case Softdatatype.S7COMMP_SOFTDATATYPE_HWANY:
174172
return new PlcTagWord(name, address, softdatatype);
175173

src/S7CommPlusDriver/Core/S7p.cs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public static int EncodeInt64(System.IO.Stream buffer, Int64 value)
8787

8888
public static int DecodeByte(System.IO.Stream buffer, out byte value)
8989
{
90-
if (buffer.Position >= buffer.Length)
90+
if (buffer.Length - buffer.Position < 1)
9191
{
9292
value = 0;
9393
return 0;
@@ -98,7 +98,7 @@ public static int DecodeByte(System.IO.Stream buffer, out byte value)
9898

9999
public static int DecodeUInt16(System.IO.Stream buffer, out UInt16 value)
100100
{
101-
if (buffer.Position >= buffer.Length)
101+
if (buffer.Length - buffer.Position < 2)
102102
{
103103
value = 0;
104104
return 0;
@@ -110,7 +110,7 @@ public static int DecodeUInt16(System.IO.Stream buffer, out UInt16 value)
110110
// Little Endian
111111
public static int DecodeUInt16LE(System.IO.Stream buffer, out UInt16 value)
112112
{
113-
if (buffer.Position >= buffer.Length)
113+
if (buffer.Length - buffer.Position < 2)
114114
{
115115
value = 0;
116116
return 0;
@@ -121,7 +121,7 @@ public static int DecodeUInt16LE(System.IO.Stream buffer, out UInt16 value)
121121

122122
public static int DecodeInt16(System.IO.Stream buffer, out Int16 value)
123123
{
124-
if (buffer.Position >= buffer.Length)
124+
if (buffer.Length - buffer.Position < 2)
125125
{
126126
value = 0;
127127
return 0;
@@ -132,7 +132,7 @@ public static int DecodeInt16(System.IO.Stream buffer, out Int16 value)
132132

133133
public static int DecodeUInt32(System.IO.Stream buffer, out UInt32 value)
134134
{
135-
if (buffer.Position >= buffer.Length)
135+
if (buffer.Length - buffer.Position < 4)
136136
{
137137
value = 0;
138138
return 0;
@@ -144,7 +144,7 @@ public static int DecodeUInt32(System.IO.Stream buffer, out UInt32 value)
144144
// Little Endian
145145
public static int DecodeUInt32LE(System.IO.Stream buffer, out UInt32 value)
146146
{
147-
if (buffer.Position >= buffer.Length)
147+
if (buffer.Length - buffer.Position < 4)
148148
{
149149
value = 0;
150150
return 0;
@@ -156,7 +156,7 @@ public static int DecodeUInt32LE(System.IO.Stream buffer, out UInt32 value)
156156
// Little Endian
157157
public static int DecodeInt32LE(System.IO.Stream buffer, out Int32 value)
158158
{
159-
if (buffer.Position >= buffer.Length)
159+
if (buffer.Length - buffer.Position < 4)
160160
{
161161
value = 0;
162162
return 0;
@@ -167,7 +167,7 @@ public static int DecodeInt32LE(System.IO.Stream buffer, out Int32 value)
167167

168168
public static int DecodeInt32(System.IO.Stream buffer, out Int32 value)
169169
{
170-
if (buffer.Position >= buffer.Length)
170+
if (buffer.Length - buffer.Position < 4)
171171
{
172172
value = 0;
173173
return 0;
@@ -178,7 +178,7 @@ public static int DecodeInt32(System.IO.Stream buffer, out Int32 value)
178178

179179
public static int DecodeUInt64(System.IO.Stream buffer, out UInt64 value)
180180
{
181-
if (buffer.Position >= buffer.Length)
181+
if (buffer.Length - buffer.Position < 8)
182182
{
183183
value = 0;
184184
return 0;
@@ -192,7 +192,7 @@ public static int DecodeUInt64(System.IO.Stream buffer, out UInt64 value)
192192

193193
public static int DecodeInt64(System.IO.Stream buffer, out Int64 value)
194194
{
195-
if (buffer.Position >= buffer.Length)
195+
if (buffer.Length - buffer.Position < 8)
196196
{
197197
value = 0;
198198
return 0;
@@ -600,6 +600,11 @@ public static int EncodeWString(System.IO.Stream buffer, string value)
600600

601601
public static int DecodeWString(System.IO.Stream buffer, int length, out string value)
602602
{
603+
if (buffer.Length - buffer.Position < length)
604+
{
605+
value = string.Empty;
606+
return 0;
607+
}
603608
byte[] tmp = new byte[length];
604609
buffer.Read(tmp, 0, length);
605610
value = Encoding.UTF8.GetString(tmp);
@@ -615,7 +620,7 @@ public static int EncodeOctets(System.IO.Stream buffer, byte[] value)
615620

616621
public static int DecodeOctets(System.IO.Stream buffer, int length, out byte[] value)
617622
{
618-
if (length <= 0)
623+
if (length <= 0 || buffer.Length - buffer.Position < length)
619624
{
620625
value = null;
621626
return 0;
@@ -715,7 +720,14 @@ public static int DecodeObject(System.IO.Stream buffer, ref PObject obj, bool As
715720

716721
public static int DecodeHeader(System.IO.Stream buffer, out byte version, out UInt16 length)
717722
{
718-
buffer.ReadByte();
723+
if (buffer.Length - buffer.Position < 4)
724+
{
725+
version = 0;
726+
length = 0;
727+
return 0;
728+
}
729+
730+
buffer.ReadByte(); // Skip one byte (purpose unclear)
719731
version = (byte)buffer.ReadByte();
720732
DecodeUInt16(buffer, out length);
721733
return 4;

src/S7CommPlusDriver/Core/Softdatatype.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ public static class Softdatatype
9999
public const uint S7COMMP_SOFTDATATYPE_AOMIDENT = 128;
100100
public const uint S7COMMP_SOFTDATATYPE_EVENTANY = 129;
101101
public const uint S7COMMP_SOFTDATATYPE_EVENTATT = 130;
102-
public const uint S7COMMP_SOFTDATATYPE_EVENTHWINT = 131;
103-
public const uint S7COMMP_SOFTDATATYPE_FOLDER = 132;
104-
public const uint S7COMMP_SOFTDATATYPE_AOMAID = 133;
105-
public const uint S7COMMP_SOFTDATATYPE_AOMLINK = 134;
102+
public const uint S7COMMP_SOFTDATATYPE_FOLDER = 131;
103+
public const uint S7COMMP_SOFTDATATYPE_AOMAID = 132;
104+
public const uint S7COMMP_SOFTDATATYPE_AOMLINK = 133;
105+
public const uint S7COMMP_SOFTDATATYPE_EVENTHWINT = 134;
106106
public const uint S7COMMP_SOFTDATATYPE_HWANY = 144;
107107
public const uint S7COMMP_SOFTDATATYPE_HWIOSYSTEM = 145;
108108
public const uint S7COMMP_SOFTDATATYPE_HWDPMASTER = 146;
@@ -235,10 +235,10 @@ public static class Softdatatype
235235
{ S7COMMP_SOFTDATATYPE_AOMIDENT, "AOM_IDENT" },
236236
{ S7COMMP_SOFTDATATYPE_EVENTANY, "EVENT_ANY" },
237237
{ S7COMMP_SOFTDATATYPE_EVENTATT, "EVENT_ATT" },
238-
{ S7COMMP_SOFTDATATYPE_EVENTHWINT, "EVENT_HWINT" },
239238
{ S7COMMP_SOFTDATATYPE_FOLDER, "FOLDER" },
240239
{ S7COMMP_SOFTDATATYPE_AOMAID, "AOM_AID" },
241240
{ S7COMMP_SOFTDATATYPE_AOMLINK, "AOM_LINK" },
241+
{ S7COMMP_SOFTDATATYPE_EVENTHWINT, "EVENT_HWINT" },
242242
{ S7COMMP_SOFTDATATYPE_HWANY, "HW_ANY" },
243243
{ S7COMMP_SOFTDATATYPE_HWIOSYSTEM, "HW_IOSYSTEM" },
244244
{ S7COMMP_SOFTDATATYPE_HWDPMASTER, "HW_DPMASTER" },

src/S7CommPlusDriver/OpenSSL/Native.cs

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,6 @@ private static IntPtr DllImportResolver(string libraryName, Assembly assembly, D
7979
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
8080
public static extern int OPENSSL_init_ssl(UInt64 opts, IntPtr settings);
8181

82-
//void OPENSSL_free(void* addr);
83-
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
84-
public static extern void OPENSSL_free(IntPtr addr);
85-
8682
#endregion
8783

8884
#region SSL
@@ -252,10 +248,6 @@ private static IntPtr DllImportResolver(string libraryName, Assembly assembly, D
252248
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
253249
public static extern int SSL_want(IntPtr ssl);
254250

255-
// int SSL_want_write(const SSL* ssl);
256-
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
257-
public static extern int SSL_want_write(IntPtr ssl);
258-
259251
// int SSL_write(SSL *ssl, const void *buf, int num);
260252
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
261253
public static extern int SSL_write(IntPtr ssl, byte[] buf, int len);
@@ -320,9 +312,16 @@ private static IntPtr DllImportResolver(string libraryName, Assembly assembly, D
320312
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
321313
public static extern IntPtr BIO_s_mem();
322314

323-
// int BIO_should_retry(BIO *b);
315+
// int BIO_test_flags(const BIO *b, int flags);
324316
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
325-
public static extern int BIO_should_retry(IntPtr b);
317+
public static extern int BIO_test_flags(IntPtr b, int flags);
318+
319+
// int BIO_should_retry(BIO *b) -> define in bio.h: BIO_test_flags(a, BIO_FLAGS_SHOULD_RETRY)
320+
const int BIO_FLAGS_SHOULD_RETRY = 0x08;
321+
public static int BIO_should_retry(IntPtr b)
322+
{
323+
return Native.BIO_test_flags(b, BIO_FLAGS_SHOULD_RETRY);
324+
}
326325

327326
// int BIO_write(BIO *b, const void *data, int dlen);
328327
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
@@ -359,47 +358,15 @@ public static long BIO_set_mem_eof_return(IntPtr b, int v)
359358
#endregion
360359

361360
#region Utilities
362-
public static string StaticString(IntPtr ptr)
363-
{
364-
return Marshal.PtrToStringAnsi(ptr);
365-
}
366-
367-
public static string PtrToStringAnsi(IntPtr ptr, bool hasOwnership)
368-
{
369-
var len = 0;
370-
for (var i = 0; i < 1024; i++, len++)
371-
{
372-
var octet = Marshal.ReadByte(ptr, i);
373-
if (octet == 0)
374-
break;
375-
}
376-
377-
if (len == 1024)
378-
return "Invalid string";
379-
380-
var buf = new byte[len];
381-
Marshal.Copy(ptr, buf, 0, len);
382-
if (hasOwnership)
383-
Native.OPENSSL_free(ptr);
384-
385-
return Encoding.ASCII.GetString(buf, 0, len);
386-
}
387361

388-
public static IntPtr ExpectNonNull(IntPtr ptr)
362+
public static IntPtr ExpectNonNull(IntPtr ptr)
389363
{
390364
if (ptr == IntPtr.Zero)
391365
throw new Exception();
392366

393367
return ptr;
394368
}
395369

396-
public static int ExpectSuccess(int ret)
397-
{
398-
if (ret <= 0)
399-
throw new Exception();
400-
401-
return ret;
402-
}
403370
#endregion
404371
}
405372
}

src/S7CommPlusDriver/S7CommPlusConnection.cs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ public PObject getTypeInfoByRelId(uint ti_relid)
10971097
/// <exception cref="Exception">Symbol syntax error</exception>
10981098
private void calcAccessSeqFor1DimArray(ref string symbol, PVartypeListElement varType, VarInfo varInfo)
10991099
{
1100-
Regex re = new Regex(@"^\[(\d+)\]");
1100+
Regex re = new Regex(@"^\[(-?\d+)\]");
11011101
Match m = re.Match(symbol);
11021102
if (!m.Success) throw new Exception("Symbol syntax error");
11031103
parseSymbolLevel(ref symbol); // remove index from symbol string
@@ -1122,16 +1122,14 @@ private void calcAccessSeqFor1DimArray(ref string symbol, PVartypeListElement va
11221122
/// <exception cref="Exception">Symbol syntax error</exception>
11231123
private void calcAccessSeqForMDimArray(ref string symbol, PVartypeListElement varType, VarInfo varInfo)
11241124
{
1125-
Regex re = new Regex(@"^\[([0-9, ]+)\]");
1125+
Regex re = new Regex(@"^\[( ?-?\d+ ?(, ?-?\d+ ?)+)\]");
11261126
Match m = re.Match(symbol);
11271127
if (!m.Success) throw new Exception("Symbol syntax error");
11281128
parseSymbolLevel(ref symbol); // remove index from symbol string
11291129
string idxs = m.Groups[1].Value.Replace(" ", "");
11301130

1131-
uint[] indexes = Array.ConvertAll(idxs.Split(','), e => uint.Parse(e));
1131+
int[] indexes = Array.ConvertAll(idxs.Split(','), e => int.Parse(e));
11321132
var ioit = (IOffsetInfoType_MDim)varType.OffsetInfoType;
1133-
uint ArrayElementCount = ioit.GetArrayElementCount();
1134-
int ArrayLowerBounds = ioit.GetArrayLowerBounds();
11351133
uint[] MdimArrayElementCount = (uint[])ioit.GetMdimArrayElementCount().Clone();
11361134
int[] MdimArrayLowerBounds = ioit.GetMdimArrayLowerBounds();
11371135

@@ -1141,7 +1139,7 @@ private void calcAccessSeqForMDimArray(ref string symbol, PVartypeListElement va
11411139
// check bounds
11421140
for (int i = 0; i < dimCount; ++i)
11431141
{
1144-
indexes[i] = (uint)(indexes[i] - MdimArrayLowerBounds[dimCount - i - 1]);
1142+
indexes[i] = (indexes[i] - MdimArrayLowerBounds[dimCount - i - 1]);
11451143
if (indexes[i] > MdimArrayElementCount[dimCount - i - 1]) throw new Exception("Out of bounds");
11461144
if (indexes[i] < 0) throw new Exception("Out of bounds");
11471145
}
@@ -1161,10 +1159,10 @@ private void calcAccessSeqForMDimArray(ref string symbol, PVartypeListElement va
11611159
dimSize[dimCount - 1] = g;
11621160

11631161
// calc id
1164-
uint arrayIndex = 0;
1162+
int arrayIndex = 0;
11651163
for (int i = 0; i < dimCount; ++i)
11661164
{
1167-
arrayIndex += indexes[i] * dimSize[dimCount - i - 1];
1165+
arrayIndex += indexes[i] * (int)dimSize[dimCount - i - 1];
11681166
}
11691167

11701168
varInfo.AccessSequence += "." + String.Format("{0:X}", arrayIndex);

src/S7CommPlusGUIBrowser/Form1.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ private void SetImageKey(ref TreeNode tn, PVartypeListElement vte)
260260
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMIDENT:
261261
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTANY:
262262
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTATT:
263+
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMAID:
263264
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMLINK:
265+
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTHWINT:
264266
case Softdatatype.S7COMMP_SOFTDATATYPE_HWANY:
265267
case Softdatatype.S7COMMP_SOFTDATATYPE_HWIOSYSTEM:
266268
case Softdatatype.S7COMMP_SOFTDATATYPE_HWDPMASTER:

0 commit comments

Comments
 (0)