Botan  2.8.0
Crypto and TLS for C++11
out_buf.cpp
Go to the documentation of this file.
1 /*
2 * Pipe Output Buffer
3 * (C) 1999-2007,2011 Jack Lloyd
4 * 2012 Markus Wanner
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8 
9 #include <botan/internal/out_buf.h>
10 #include <botan/secqueue.h>
11 
12 namespace Botan {
13 
14 /*
15 * Read data from a message
16 */
17 size_t Output_Buffers::read(uint8_t output[], size_t length,
18  Pipe::message_id msg)
19  {
20  SecureQueue* q = get(msg);
21  if(q)
22  return q->read(output, length);
23  return 0;
24  }
25 
26 /*
27 * Peek at data in a message
28 */
29 size_t Output_Buffers::peek(uint8_t output[], size_t length,
30  size_t stream_offset,
31  Pipe::message_id msg) const
32  {
33  SecureQueue* q = get(msg);
34  if(q)
35  return q->peek(output, length, stream_offset);
36  return 0;
37  }
38 
39 /*
40 * Check available bytes in a message
41 */
43  {
44  SecureQueue* q = get(msg);
45  if(q)
46  return q->size();
47  return 0;
48  }
49 
50 /*
51 * Return the total bytes of a message that have already been read.
52 */
54  {
55  SecureQueue* q = get(msg);
56  if (q)
57  return q->get_bytes_read();
58  return 0;
59  }
60 
61 /*
62 * Add a new output queue
63 */
65  {
66  BOTAN_ASSERT(queue, "queue was provided");
67 
68  BOTAN_ASSERT(m_buffers.size() < m_buffers.max_size(),
69  "Room was available in container");
70 
71  m_buffers.push_back(std::unique_ptr<SecureQueue>(queue));
72  }
73 
74 /*
75 * Retire old output queues
76 */
78  {
79  for(size_t i = 0; i != m_buffers.size(); ++i)
80  if(m_buffers[i] && m_buffers[i]->size() == 0)
81  {
82  m_buffers[i].reset();
83  }
84 
85  while(m_buffers.size() && !m_buffers[0])
86  {
87  m_buffers.pop_front();
88  m_offset = m_offset + Pipe::message_id(1);
89  }
90  }
91 
92 /*
93 * Get a particular output queue
94 */
95 SecureQueue* Output_Buffers::get(Pipe::message_id msg) const
96  {
97  if(msg < m_offset)
98  return nullptr;
99 
100  BOTAN_ASSERT(msg < message_count(), "Message number is in range");
101 
102  return m_buffers[msg-m_offset].get();
103  }
104 
105 /*
106 * Return the total number of messages
107 */
109  {
110  return (m_offset + m_buffers.size());
111  }
112 
113 /*
114 * Output_Buffers Constructor
115 */
117  {
118  m_offset = 0;
119  }
120 
121 }
size_t read(uint8_t[], size_t) override
Definition: secqueue.cpp:144
size_t get_bytes_read(Pipe::message_id) const
Definition: out_buf.cpp:53
size_t size() const
Definition: secqueue.cpp:206
size_t remaining(Pipe::message_id) const
Definition: out_buf.cpp:42
size_t message_id
Definition: pipe.h:35
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:55
size_t peek(uint8_t[], size_t, size_t=0) const override
Definition: secqueue.cpp:167
Pipe::message_id message_count() const
Definition: out_buf.cpp:108
size_t get_bytes_read() const override
Definition: secqueue.cpp:198
Definition: alg_id.cpp:13
size_t peek(uint8_t[], size_t, size_t, Pipe::message_id) const
Definition: out_buf.cpp:29
void add(class SecureQueue *)
Definition: out_buf.cpp:64
size_t read(uint8_t[], size_t, Pipe::message_id)
Definition: out_buf.cpp:17