2645a926bc396c0f58d422a2a7cd984872f073cb
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsError.cs
1 //
2 // MonoBtlsError.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2015 Xamarin Inc. (http://www.xamarin.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 // #if SECURITY_DEP
27 using System;
28 using System.IO;
29 using System.Text;
30 using System.Runtime.InteropServices;
31 using System.Runtime.CompilerServices;
32
33 #if MONOTOUCH
34 using MonoTouch;
35 #endif
36
37 namespace Mono.Btls
38 {
39         static class MonoBtlsError
40         {
41                 [MethodImpl (MethodImplOptions.InternalCall)]
42                 extern static int mono_btls_error_peek_error ();
43
44                 [MethodImpl (MethodImplOptions.InternalCall)]
45                 extern static int mono_btls_error_get_error ();
46
47                 [MethodImpl (MethodImplOptions.InternalCall)]
48                 extern static void mono_btls_error_clear_error ();
49
50                 [MethodImpl (MethodImplOptions.InternalCall)]
51                 extern static void mono_btls_error_get_error_string_n (int error, IntPtr buf, int len);
52
53                 public static int PeekError ()
54                 {
55                         return mono_btls_error_peek_error ();
56                 }
57
58                 public static int GetError ()
59                 {
60                         return mono_btls_error_get_error ();
61                 }
62
63                 public static void ClearError ()
64                 {
65                         mono_btls_error_clear_error ();
66                 }
67
68                 public static string GetErrorString (int error)
69                 {
70                         var size = 1024;
71                         var buffer = Marshal.AllocHGlobal (size);
72                         if (buffer == IntPtr.Zero)
73                                 throw new OutOfMemoryException ();
74                         try {
75                                 mono_btls_error_get_error_string_n (error, buffer, size);
76                                 return Marshal.PtrToStringAnsi (buffer);
77                         } finally {
78                                 Marshal.FreeHGlobal (buffer);
79                         }
80                 }
81         }
82 }
83 // #endif