Botan 1.10.17

out_buf.cpp
Go to the documentation of this file.
1 /*
2 * Pipe Output Buffer
3 * (C) 1999-2007,2011 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7
8 #include <botan/internal/out_buf.h>
9 #include <botan/secqueue.h>
10 #include <botan/internal/assert.h>
11
12 namespace Botan {
13
14 /*
15 * Read data from a message
16 */
17 size_t Output_Buffers::read(byte 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(byte 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 */
42 size_t Output_Buffers::remaining(Pipe::message_id msg) const
43 {
44 SecureQueue* q = get(msg);
45 if(q)
46 return q->size();
47 return 0;
48 }
49
50 /*
51 * Add a new output queue
52 */
53 void Output_Buffers::add(SecureQueue* queue)
54 {
55 BOTAN_ASSERT(queue, "argument was NULL");
56
57 BOTAN_ASSERT(buffers.size() < buffers.max_size(),
58 "No more room in container");
59
60 buffers.push_back(queue);
61 }
62
63 /*
64 * Retire old output queues
65 */
66 void Output_Buffers::retire()
67 {
68 for(size_t i = 0; i != buffers.size(); ++i)
69 if(buffers[i] && buffers[i]->size() == 0)
70 {
71 delete buffers[i];
72 buffers[i] = 0;
73 }
74
75 while(buffers.size() && !buffers[0])
76 {
77 buffers.pop_front();
78 offset = offset + Pipe::message_id(1);
79 }
80 }
81
82 /*
83 * Get a particular output queue
84 */
85 SecureQueue* Output_Buffers::get(Pipe::message_id msg) const
86 {
87 if(msg < offset)
88 return 0;
89
90 BOTAN_ASSERT(msg < message_count(),
91 "Message number out of range");
92
93 return buffers[msg-offset];
94 }
95
96 /*
97 * Return the total number of messages
98 */
99 Pipe::message_id Output_Buffers::message_count() const
100 {
101 return (offset + buffers.size());
102 }
103
104 /*
105 * Output_Buffers Constructor
106 */
107 Output_Buffers::Output_Buffers()
108 {
109 offset = 0;
110 }
111
112 /*
113 * Output_Buffers Destructor
114 */
115 Output_Buffers::~Output_Buffers()
116 {
117 for(size_t j = 0; j != buffers.size(); ++j)
118 delete buffers[j];
119 }
120
121 }
Botan::Output_Buffers::read
size_t read(byte[], size_t, Pipe::message_id)
Definition: out_buf.cpp:17
Botan::SecureQueue
Definition: secqueue.h:19
Botan::SecureQueue::size
size_t size() const
Definition: secqueue.cpp:190
Botan::SecureQueue::read
size_t read(byte[], size_t)
Definition: secqueue.cpp:137
Botan::Output_Buffers::remaining
size_t remaining(Pipe::message_id) const
Definition: out_buf.cpp:42
Botan::Pipe::message_id
size_t message_id
Definition: pipe.h:31
Botan::Output_Buffers::~Output_Buffers
~Output_Buffers()
Definition: out_buf.cpp:115
Botan::Output_Buffers::retire
void retire()
Definition: out_buf.cpp:66
BOTAN_ASSERT
#define BOTAN_ASSERT(expr, msg)
Definition: assert.h:19
Botan::byte
unsigned char byte
Definition: types.h:22
Botan::Output_Buffers::message_count
Pipe::message_id message_count() const
Definition: out_buf.cpp:99
Botan::Output_Buffers::Output_Buffers
Output_Buffers()
Definition: out_buf.cpp:107
Botan
Definition: algo_base.h:14
Botan::Output_Buffers::peek
size_t peek(byte[], size_t, size_t, Pipe::message_id) const
Definition: out_buf.cpp:29
Botan::Output_Buffers::add
void add(class SecureQueue *)
Definition: out_buf.cpp:53
Botan::SecureQueue::peek
size_t peek(byte[], size_t, size_t=0) const
Definition: secqueue.cpp:159