[utils] Fix inet_pton fallback.
[mono.git] / mcs / class / Mono.Security / Mono.Security.Interface / TlsMultiBuffer.cs
1 //
2 // TlsMultiBuffer.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 TlsMultiBuffer
31         {
32                 MemoryChunk first, last;
33
34                 private class MemoryChunk : BufferOffsetSize
35                 {
36                         public MemoryChunk next;
37
38                         public MemoryChunk (byte[] buffer, int offset, int size)
39                                 : base (buffer, offset, size)
40                         {
41                         }
42                 }
43
44                 public bool IsEmpty {
45                         get { return first == null; }
46                 }
47
48                 public bool IsSingle {
49                         get { return first != null && first.next == null; }
50                 }
51
52                 public void Add (TlsBuffer buffer)
53                 {
54                         Add (buffer.Buffer, buffer.Offset, buffer.Size);
55                 }
56
57                 public void Add (byte[] buffer)
58                 {
59                         Add (buffer, 0, buffer.Length);
60                 }
61
62                 public void Add (byte[] buffer, int offset, int size)
63                 {
64                         var chunk = new MemoryChunk (buffer, offset, size);
65                         if (last == null)
66                                 first = last = chunk;
67                         else {
68                                 last.next = chunk;
69                                 last = chunk;
70                         }
71                 }
72
73                 public BufferOffsetSize[] GetBufferArray ()
74                 {
75                         int count = 0;
76                         for (var ptr = first; ptr != null; ptr = ptr.next)
77                                 count++;
78                         var array = new BufferOffsetSize [count];
79                         count = 0;
80                         for (var ptr = first; ptr != null; ptr = ptr.next)
81                                 array [count++] = ptr;
82                         return array;
83                 }
84
85                 public void Clear ()
86                 {
87                         for (var ptr = first; ptr != null; ptr = ptr.next)
88                                 ptr.Dispose ();
89                         first = last = null;
90                 }
91
92                 public BufferOffsetSize GetBuffer ()
93                 {
94                         int totalSize = 0;
95                         for (var ptr = first; ptr != null; ptr = ptr.next)
96                                 totalSize += ptr.Size;
97
98                         var outBuffer = new BufferOffsetSize (new byte [totalSize]);
99                         int offset = 0;
100                         for (var ptr = first; ptr != null; ptr = ptr.next) {
101                                 Buffer.BlockCopy (ptr.Buffer, ptr.Offset, outBuffer.Buffer, offset, ptr.Size);
102                                 offset += ptr.Size;
103                         }
104                         return outBuffer;
105                 }
106
107                 public BufferOffsetSize StealBuffer ()
108                 {
109                         if (IsSingle) {
110                                 var retval = first;
111                                 first = last = null;
112                                 return retval;
113                         }
114
115                         return GetBuffer ();
116                 }
117         }
118 }
119