Skip to content

Commit

Permalink
Merge branch 'thomas-v2:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
jogibear9988 authored Jan 22, 2025
2 parents 70aa39c + 9bb7021 commit d55d2f6
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 74 deletions.
3 changes: 2 additions & 1 deletion src/S7CommPlusDriver/ClientApi/Browser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,9 @@ private bool IsSoftdatatypeSupported(uint softdatatype)
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMIDENT:
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTANY:
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTATT:
case Softdatatype.S7COMMP_SOFTDATATYPE_FOLDER: // Should we support this internal datatype? Only used internally in SFBs
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMAID:
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMLINK:
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTHWINT:
case Softdatatype.S7COMMP_SOFTDATATYPE_HWANY:
case Softdatatype.S7COMMP_SOFTDATATYPE_HWIOSYSTEM:
case Softdatatype.S7COMMP_SOFTDATATYPE_HWDPMASTER:
Expand Down
8 changes: 3 additions & 5 deletions src/S7CommPlusDriver/ClientApi/PlcTags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,12 @@ public static PlcTag TagFactory(string name, ItemAddress address, uint softdatat
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTATT:
return new PlcTagDWord(name, address, softdatatype);

case Softdatatype.S7COMMP_SOFTDATATYPE_FOLDER:
// Softdatatype 132: This type is only used as parameter for internal SFBs (e.g. AID input parameter)
// Length of value (4 byte) calculated from the offsetinfo byte addresses.
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMAID:
return new PlcTagDWord(name, address, softdatatype);

case Softdatatype.S7COMMP_SOFTDATATYPE_AOMLINK:
return new PlcTagDWord(name, address, softdatatype);

case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTHWINT:
return new PlcTagDWord(name, address, softdatatype);
case Softdatatype.S7COMMP_SOFTDATATYPE_HWANY:
return new PlcTagWord(name, address, softdatatype);

Expand Down
36 changes: 24 additions & 12 deletions src/S7CommPlusDriver/Core/S7p.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public static int EncodeInt64(System.IO.Stream buffer, Int64 value)

public static int DecodeByte(System.IO.Stream buffer, out byte value)
{
if (buffer.Position >= buffer.Length)
if (buffer.Length - buffer.Position < 1)
{
value = 0;
return 0;
Expand All @@ -98,7 +98,7 @@ public static int DecodeByte(System.IO.Stream buffer, out byte value)

public static int DecodeUInt16(System.IO.Stream buffer, out UInt16 value)
{
if (buffer.Position >= buffer.Length)
if (buffer.Length - buffer.Position < 2)
{
value = 0;
return 0;
Expand All @@ -110,7 +110,7 @@ public static int DecodeUInt16(System.IO.Stream buffer, out UInt16 value)
// Little Endian
public static int DecodeUInt16LE(System.IO.Stream buffer, out UInt16 value)
{
if (buffer.Position >= buffer.Length)
if (buffer.Length - buffer.Position < 2)
{
value = 0;
return 0;
Expand All @@ -121,7 +121,7 @@ public static int DecodeUInt16LE(System.IO.Stream buffer, out UInt16 value)

public static int DecodeInt16(System.IO.Stream buffer, out Int16 value)
{
if (buffer.Position >= buffer.Length)
if (buffer.Length - buffer.Position < 2)
{
value = 0;
return 0;
Expand All @@ -132,7 +132,7 @@ public static int DecodeInt16(System.IO.Stream buffer, out Int16 value)

public static int DecodeUInt32(System.IO.Stream buffer, out UInt32 value)
{
if (buffer.Position >= buffer.Length)
if (buffer.Length - buffer.Position < 4)
{
value = 0;
return 0;
Expand All @@ -144,7 +144,7 @@ public static int DecodeUInt32(System.IO.Stream buffer, out UInt32 value)
// Little Endian
public static int DecodeUInt32LE(System.IO.Stream buffer, out UInt32 value)
{
if (buffer.Position >= buffer.Length)
if (buffer.Length - buffer.Position < 4)
{
value = 0;
return 0;
Expand All @@ -156,7 +156,7 @@ public static int DecodeUInt32LE(System.IO.Stream buffer, out UInt32 value)
// Little Endian
public static int DecodeInt32LE(System.IO.Stream buffer, out Int32 value)
{
if (buffer.Position >= buffer.Length)
if (buffer.Length - buffer.Position < 4)
{
value = 0;
return 0;
Expand All @@ -167,7 +167,7 @@ public static int DecodeInt32LE(System.IO.Stream buffer, out Int32 value)

public static int DecodeInt32(System.IO.Stream buffer, out Int32 value)
{
if (buffer.Position >= buffer.Length)
if (buffer.Length - buffer.Position < 4)
{
value = 0;
return 0;
Expand All @@ -178,7 +178,7 @@ public static int DecodeInt32(System.IO.Stream buffer, out Int32 value)

public static int DecodeUInt64(System.IO.Stream buffer, out UInt64 value)
{
if (buffer.Position >= buffer.Length)
if (buffer.Length - buffer.Position < 8)
{
value = 0;
return 0;
Expand All @@ -192,7 +192,7 @@ public static int DecodeUInt64(System.IO.Stream buffer, out UInt64 value)

public static int DecodeInt64(System.IO.Stream buffer, out Int64 value)
{
if (buffer.Position >= buffer.Length)
if (buffer.Length - buffer.Position < 8)
{
value = 0;
return 0;
Expand Down Expand Up @@ -600,6 +600,11 @@ public static int EncodeWString(System.IO.Stream buffer, string value)

public static int DecodeWString(System.IO.Stream buffer, int length, out string value)
{
if (buffer.Length - buffer.Position < length)
{
value = string.Empty;
return 0;
}
byte[] tmp = new byte[length];
buffer.Read(tmp, 0, length);
value = Encoding.UTF8.GetString(tmp);
Expand All @@ -615,7 +620,7 @@ public static int EncodeOctets(System.IO.Stream buffer, byte[] value)

public static int DecodeOctets(System.IO.Stream buffer, int length, out byte[] value)
{
if (length <= 0)
if (length <= 0 || buffer.Length - buffer.Position < length)
{
value = null;
return 0;
Expand Down Expand Up @@ -715,7 +720,14 @@ public static int DecodeObject(System.IO.Stream buffer, ref PObject obj, bool As

public static int DecodeHeader(System.IO.Stream buffer, out byte version, out UInt16 length)
{
buffer.ReadByte();
if (buffer.Length - buffer.Position < 4)
{
version = 0;
length = 0;
return 0;
}

buffer.ReadByte(); // Skip one byte (purpose unclear)
version = (byte)buffer.ReadByte();
DecodeUInt16(buffer, out length);
return 4;
Expand Down
10 changes: 5 additions & 5 deletions src/S7CommPlusDriver/Core/Softdatatype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,10 @@ public static class Softdatatype
public const uint S7COMMP_SOFTDATATYPE_AOMIDENT = 128;
public const uint S7COMMP_SOFTDATATYPE_EVENTANY = 129;
public const uint S7COMMP_SOFTDATATYPE_EVENTATT = 130;
public const uint S7COMMP_SOFTDATATYPE_EVENTHWINT = 131;
public const uint S7COMMP_SOFTDATATYPE_FOLDER = 132;
public const uint S7COMMP_SOFTDATATYPE_AOMAID = 133;
public const uint S7COMMP_SOFTDATATYPE_AOMLINK = 134;
public const uint S7COMMP_SOFTDATATYPE_FOLDER = 131;
public const uint S7COMMP_SOFTDATATYPE_AOMAID = 132;
public const uint S7COMMP_SOFTDATATYPE_AOMLINK = 133;
public const uint S7COMMP_SOFTDATATYPE_EVENTHWINT = 134;
public const uint S7COMMP_SOFTDATATYPE_HWANY = 144;
public const uint S7COMMP_SOFTDATATYPE_HWIOSYSTEM = 145;
public const uint S7COMMP_SOFTDATATYPE_HWDPMASTER = 146;
Expand Down Expand Up @@ -235,10 +235,10 @@ public static class Softdatatype
{ S7COMMP_SOFTDATATYPE_AOMIDENT, "AOM_IDENT" },
{ S7COMMP_SOFTDATATYPE_EVENTANY, "EVENT_ANY" },
{ S7COMMP_SOFTDATATYPE_EVENTATT, "EVENT_ATT" },
{ S7COMMP_SOFTDATATYPE_EVENTHWINT, "EVENT_HWINT" },
{ S7COMMP_SOFTDATATYPE_FOLDER, "FOLDER" },
{ S7COMMP_SOFTDATATYPE_AOMAID, "AOM_AID" },
{ S7COMMP_SOFTDATATYPE_AOMLINK, "AOM_LINK" },
{ S7COMMP_SOFTDATATYPE_EVENTHWINT, "EVENT_HWINT" },
{ S7COMMP_SOFTDATATYPE_HWANY, "HW_ANY" },
{ S7COMMP_SOFTDATATYPE_HWIOSYSTEM, "HW_IOSYSTEM" },
{ S7COMMP_SOFTDATATYPE_HWDPMASTER, "HW_DPMASTER" },
Expand Down
53 changes: 10 additions & 43 deletions src/S7CommPlusDriver/OpenSSL/Native.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,6 @@ private static IntPtr DllImportResolver(string libraryName, Assembly assembly, D
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int OPENSSL_init_ssl(UInt64 opts, IntPtr settings);

//void OPENSSL_free(void* addr);
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern void OPENSSL_free(IntPtr addr);

#endregion

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

// int SSL_want_write(const SSL* ssl);
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int SSL_want_write(IntPtr ssl);

// int SSL_write(SSL *ssl, const void *buf, int num);
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int SSL_write(IntPtr ssl, byte[] buf, int len);
Expand Down Expand Up @@ -320,9 +312,16 @@ private static IntPtr DllImportResolver(string libraryName, Assembly assembly, D
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr BIO_s_mem();

// int BIO_should_retry(BIO *b);
// int BIO_test_flags(const BIO *b, int flags);
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
public static extern int BIO_should_retry(IntPtr b);
public static extern int BIO_test_flags(IntPtr b, int flags);

// int BIO_should_retry(BIO *b) -> define in bio.h: BIO_test_flags(a, BIO_FLAGS_SHOULD_RETRY)
const int BIO_FLAGS_SHOULD_RETRY = 0x08;
public static int BIO_should_retry(IntPtr b)
{
return Native.BIO_test_flags(b, BIO_FLAGS_SHOULD_RETRY);
}

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

#region Utilities
public static string StaticString(IntPtr ptr)
{
return Marshal.PtrToStringAnsi(ptr);
}

public static string PtrToStringAnsi(IntPtr ptr, bool hasOwnership)
{
var len = 0;
for (var i = 0; i < 1024; i++, len++)
{
var octet = Marshal.ReadByte(ptr, i);
if (octet == 0)
break;
}

if (len == 1024)
return "Invalid string";

var buf = new byte[len];
Marshal.Copy(ptr, buf, 0, len);
if (hasOwnership)
Native.OPENSSL_free(ptr);

return Encoding.ASCII.GetString(buf, 0, len);
}

public static IntPtr ExpectNonNull(IntPtr ptr)
public static IntPtr ExpectNonNull(IntPtr ptr)
{
if (ptr == IntPtr.Zero)
throw new Exception();

return ptr;
}

public static int ExpectSuccess(int ret)
{
if (ret <= 0)
throw new Exception();

return ret;
}
#endregion
}
}
14 changes: 6 additions & 8 deletions src/S7CommPlusDriver/S7CommPlusConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1097,7 +1097,7 @@ public PObject getTypeInfoByRelId(uint ti_relid)
/// <exception cref="Exception">Symbol syntax error</exception>
private void calcAccessSeqFor1DimArray(ref string symbol, PVartypeListElement varType, VarInfo varInfo)
{
Regex re = new Regex(@"^\[(\d+)\]");
Regex re = new Regex(@"^\[(-?\d+)\]");
Match m = re.Match(symbol);
if (!m.Success) throw new Exception("Symbol syntax error");
parseSymbolLevel(ref symbol); // remove index from symbol string
Expand All @@ -1122,16 +1122,14 @@ private void calcAccessSeqFor1DimArray(ref string symbol, PVartypeListElement va
/// <exception cref="Exception">Symbol syntax error</exception>
private void calcAccessSeqForMDimArray(ref string symbol, PVartypeListElement varType, VarInfo varInfo)
{
Regex re = new Regex(@"^\[([0-9, ]+)\]");
Regex re = new Regex(@"^\[( ?-?\d+ ?(, ?-?\d+ ?)+)\]");
Match m = re.Match(symbol);
if (!m.Success) throw new Exception("Symbol syntax error");
parseSymbolLevel(ref symbol); // remove index from symbol string
string idxs = m.Groups[1].Value.Replace(" ", "");

uint[] indexes = Array.ConvertAll(idxs.Split(','), e => uint.Parse(e));
int[] indexes = Array.ConvertAll(idxs.Split(','), e => int.Parse(e));
var ioit = (IOffsetInfoType_MDim)varType.OffsetInfoType;
uint ArrayElementCount = ioit.GetArrayElementCount();
int ArrayLowerBounds = ioit.GetArrayLowerBounds();
uint[] MdimArrayElementCount = (uint[])ioit.GetMdimArrayElementCount().Clone();
int[] MdimArrayLowerBounds = ioit.GetMdimArrayLowerBounds();

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

// calc id
uint arrayIndex = 0;
int arrayIndex = 0;
for (int i = 0; i < dimCount; ++i)
{
arrayIndex += indexes[i] * dimSize[dimCount - i - 1];
arrayIndex += indexes[i] * (int)dimSize[dimCount - i - 1];
}

varInfo.AccessSequence += "." + String.Format("{0:X}", arrayIndex);
Expand Down
2 changes: 2 additions & 0 deletions src/S7CommPlusGUIBrowser/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,9 @@ private void SetImageKey(ref TreeNode tn, PVartypeListElement vte)
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMIDENT:
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTANY:
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTATT:
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMAID:
case Softdatatype.S7COMMP_SOFTDATATYPE_AOMLINK:
case Softdatatype.S7COMMP_SOFTDATATYPE_EVENTHWINT:
case Softdatatype.S7COMMP_SOFTDATATYPE_HWANY:
case Softdatatype.S7COMMP_SOFTDATATYPE_HWIOSYSTEM:
case Softdatatype.S7COMMP_SOFTDATATYPE_HWDPMASTER:
Expand Down

0 comments on commit d55d2f6

Please sign in to comment.