Skip to content

Commit fc946aa

Browse files
committed
OpenSSL: Replace calls which are defines and not functions
Some functions are defines for calls of other functions with specific parameters in the openssl header files. Add similar methods, and remove unused methods where this would also be neccessary.
1 parent ecd72f8 commit fc946aa

File tree

1 file changed

+10
-43
lines changed

1 file changed

+10
-43
lines changed

src/S7CommPlusDriver/OpenSSL/Native.cs

Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ public class Native
5050
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
5151
public static extern int OPENSSL_init_ssl(UInt64 opts, IntPtr settings);
5252

53-
//void OPENSSL_free(void* addr);
54-
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
55-
public static extern void OPENSSL_free(IntPtr addr);
56-
5753
#endregion
5854

5955
#region SSL
@@ -223,10 +219,6 @@ public class Native
223219
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
224220
public static extern int SSL_want(IntPtr ssl);
225221

226-
// int SSL_want_write(const SSL* ssl);
227-
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
228-
public static extern int SSL_want_write(IntPtr ssl);
229-
230222
// int SSL_write(SSL *ssl, const void *buf, int num);
231223
[DllImport(SSLDLLNAME, CallingConvention = CallingConvention.Cdecl)]
232224
public static extern int SSL_write(IntPtr ssl, byte[] buf, int len);
@@ -291,9 +283,16 @@ public class Native
291283
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
292284
public static extern IntPtr BIO_s_mem();
293285

294-
// int BIO_should_retry(BIO *b);
286+
// int BIO_test_flags(const BIO *b, int flags);
295287
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
296-
public static extern int BIO_should_retry(IntPtr b);
288+
public static extern int BIO_test_flags(IntPtr b, int flags);
289+
290+
// int BIO_should_retry(BIO *b) -> define in bio.h: BIO_test_flags(a, BIO_FLAGS_SHOULD_RETRY)
291+
const int BIO_FLAGS_SHOULD_RETRY = 0x08;
292+
public static int BIO_should_retry(IntPtr b)
293+
{
294+
return Native.BIO_test_flags(b, BIO_FLAGS_SHOULD_RETRY);
295+
}
297296

298297
// int BIO_write(BIO *b, const void *data, int dlen);
299298
[DllImport(DLLNAME, CallingConvention = CallingConvention.Cdecl)]
@@ -330,47 +329,15 @@ public static long BIO_set_mem_eof_return(IntPtr b, int v)
330329
#endregion
331330

332331
#region Utilities
333-
public static string StaticString(IntPtr ptr)
334-
{
335-
return Marshal.PtrToStringAnsi(ptr);
336-
}
337-
338-
public static string PtrToStringAnsi(IntPtr ptr, bool hasOwnership)
339-
{
340-
var len = 0;
341-
for (var i = 0; i < 1024; i++, len++)
342-
{
343-
var octet = Marshal.ReadByte(ptr, i);
344-
if (octet == 0)
345-
break;
346-
}
347-
348-
if (len == 1024)
349-
return "Invalid string";
350-
351-
var buf = new byte[len];
352-
Marshal.Copy(ptr, buf, 0, len);
353-
if (hasOwnership)
354-
Native.OPENSSL_free(ptr);
355-
356-
return Encoding.ASCII.GetString(buf, 0, len);
357-
}
358332

359-
public static IntPtr ExpectNonNull(IntPtr ptr)
333+
public static IntPtr ExpectNonNull(IntPtr ptr)
360334
{
361335
if (ptr == IntPtr.Zero)
362336
throw new Exception();
363337

364338
return ptr;
365339
}
366340

367-
public static int ExpectSuccess(int ret)
368-
{
369-
if (ret <= 0)
370-
throw new Exception();
371-
372-
return ret;
373-
}
374341
#endregion
375342
}
376343
}

0 commit comments

Comments
 (0)