5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2014-2016 Xamarin Inc. (http://www.xamarin.com)
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
28 namespace Mono.Security.Interface
30 public class TlsMultiBuffer
32 MemoryChunk first, last;
34 private class MemoryChunk : BufferOffsetSize
36 public MemoryChunk next;
38 public MemoryChunk (byte[] buffer, int offset, int size)
39 : base (buffer, offset, size)
45 get { return first == null; }
48 public bool IsSingle {
49 get { return first != null && first.next == null; }
52 public void Add (TlsBuffer buffer)
54 Add (buffer.Buffer, buffer.Offset, buffer.Size);
57 public void Add (byte[] buffer)
59 Add (buffer, 0, buffer.Length);
62 public void Add (byte[] buffer, int offset, int size)
64 var chunk = new MemoryChunk (buffer, offset, size);
73 public BufferOffsetSize[] GetBufferArray ()
76 for (var ptr = first; ptr != null; ptr = ptr.next)
78 var array = new BufferOffsetSize [count];
80 for (var ptr = first; ptr != null; ptr = ptr.next)
81 array [count++] = ptr;
87 for (var ptr = first; ptr != null; ptr = ptr.next)
92 public BufferOffsetSize GetBuffer ()
95 for (var ptr = first; ptr != null; ptr = ptr.next)
96 totalSize += ptr.Size;
98 var outBuffer = new BufferOffsetSize (new byte [totalSize]);
100 for (var ptr = first; ptr != null; ptr = ptr.next) {
101 Buffer.BlockCopy (ptr.Buffer, ptr.Offset, outBuffer.Buffer, offset, ptr.Size);
107 public BufferOffsetSize StealBuffer ()