[amd64] fix tailcall insn size (#5483)
[mono.git] / mcs / class / Mono.Security / Mono.Security.Interface / SecureBuffer.cs
1 //
2 // SecureBuffer.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2014-2016 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 using System;
27
28 namespace Mono.Security.Interface
29 {
30         public class SecureBuffer : SecretParameters, IBufferOffsetSize
31         {
32                 byte[] buffer;
33
34                 public byte[] Buffer {
35                         get {
36                                 CheckDisposed ();
37                                 return buffer;
38                         }
39                 }
40
41                 public int Size {
42                         get {
43                                 CheckDisposed ();
44                                 return buffer != null ? buffer.Length : 0;
45                         }
46                 }
47
48                 int IBufferOffsetSize.Offset {
49                         get { return 0; }
50                 }
51
52                 public SecureBuffer (int size)
53                 {
54                         buffer = new byte [size];
55                 }
56
57                 public SecureBuffer (byte[] buffer)
58                 {
59                         this.buffer = buffer;
60                 }
61
62                 public byte[] StealBuffer ()
63                 {
64                         CheckDisposed ();
65                         var retval = this.buffer;
66                         this.buffer = null;
67                         return retval;
68                 }
69
70                 public static SecureBuffer CreateCopy (byte[] buffer)
71                 {
72                         var copy = new byte [buffer.Length];
73                         Array.Copy (buffer, copy, buffer.Length);
74                         return new SecureBuffer (copy);
75                 }
76
77                 protected override void Clear ()
78                 {
79                         if (buffer != null) {
80                                 Array.Clear (buffer, 0, buffer.Length);
81                                 buffer = null;
82                         }
83                 }
84         }
85 }
86