Botan 3.5.0
Crypto and TLS for C&
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
11#include <botan/internal/secqueue.h>
12
13namespace Botan {
14
15/*
16* Read data from a message
17*/
18size_t Output_Buffers::read(uint8_t output[], size_t length, Pipe::message_id msg) {
19 SecureQueue* q = get(msg);
20 if(q) {
21 return q->read(output, length);
22 }
23 return 0;
24}
25
26/*
27* Peek at data in a message
28*/
29size_t Output_Buffers::peek(uint8_t output[], size_t length, size_t stream_offset, Pipe::message_id msg) const {
30 SecureQueue* q = get(msg);
31 if(q) {
32 return q->peek(output, length, stream_offset);
33 }
34 return 0;
35}
36
37/*
38* Check available bytes in a message
39*/
41 SecureQueue* q = get(msg);
42 if(q) {
43 return q->size();
44 }
45 return 0;
46}
47
48/*
49* Return the total bytes of a message that have already been read.
50*/
52 SecureQueue* q = get(msg);
53 if(q) {
54 return q->get_bytes_read();
55 }
56 return 0;
57}
58
59/*
60* Add a new output queue
61*/
63 BOTAN_ASSERT(queue, "queue was provided");
64
65 BOTAN_ASSERT(m_buffers.size() < m_buffers.max_size(), "Room was available in container");
66
67 m_buffers.push_back(std::unique_ptr<SecureQueue>(queue));
68}
69
70/*
71* Retire old output queues
72*/
74 for(auto& buf : m_buffers) {
75 if(buf && buf->empty()) {
76 buf.reset();
77 }
78 }
79
80 while(!m_buffers.empty() && !m_buffers[0]) {
81 m_buffers.pop_front();
82 m_offset = m_offset + Pipe::message_id(1);
83 }
84}
85
86/*
87* Get a particular output queue
88*/
89SecureQueue* Output_Buffers::get(Pipe::message_id msg) const {
90 if(msg < m_offset) {
91 return nullptr;
92 }
93
94 BOTAN_ASSERT(msg < message_count(), "Message number is in range");
95
96 return m_buffers[msg - m_offset].get();
97}
98
99/*
100* Return the total number of messages
101*/
103 return (m_offset + m_buffers.size());
104}
105
106/*
107* Output_Buffers Constructor
108*/
110 m_offset = 0;
111}
112
113} // namespace Botan
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:50
Pipe::message_id message_count() const
Definition out_buf.cpp:102
size_t get_bytes_read(Pipe::message_id) const
Definition out_buf.cpp:51
size_t remaining(Pipe::message_id) const
Definition out_buf.cpp:40
size_t peek(uint8_t[], size_t, size_t, Pipe::message_id) const
Definition out_buf.cpp:29
void add(SecureQueue *)
Definition out_buf.cpp:62
size_t read(uint8_t[], size_t, Pipe::message_id)
Definition out_buf.cpp:18
size_t message_id
Definition pipe.h:34
size_t read(uint8_t[], size_t) override
Definition secqueue.cpp:146
size_t get_bytes_read() const override
Definition secqueue.cpp:193
size_t peek(uint8_t[], size_t, size_t=0) const override
Definition secqueue.cpp:166
size_t size() const
Definition secqueue.cpp:200