aboutsummaryrefslogtreecommitdiffstats
path: root/src/out_buf.cpp
blob: 1dc89addc898621ced5e882391263e5ffe058677 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*************************************************
* Pipe Output Buffer Source file                 *
* (C) 1999-2007 The Botan Project                *
*************************************************/

#include <botan/out_buf.h>
#include <botan/secqueue.h>

namespace Botan {

/*************************************************
* Read data from a message                       *
*************************************************/
u32bit Output_Buffers::read(byte output[], u32bit length, u32bit msg)
   {
   SecureQueue* q = get(msg);
   if(q)
      return q->read(output, length);
   return 0;
   }

/*************************************************
* Peek at data in a message                      *
*************************************************/
u32bit Output_Buffers::peek(byte output[], u32bit length,
                            u32bit stream_offset, u32bit msg) const
   {
   SecureQueue* q = get(msg);
   if(q)
      return q->peek(output, length, stream_offset);
   return 0;
   }

/*************************************************
* Check available bytes in a message             *
*************************************************/
u32bit Output_Buffers::remaining(u32bit msg) const
   {
   SecureQueue* q = get(msg);
   if(q)
      return q->size();
   return 0;
   }

/*************************************************
* Add a new output queue                         *
*************************************************/
void Output_Buffers::add(SecureQueue* queue)
   {
   if(!queue)
      throw Internal_Error("Output_Buffers::add: Argument was NULL");

   if(buffers.size() == buffers.max_size())
      throw Internal_Error("Output_Buffers::add: No more room in container");

   buffers.push_back(queue);
   }

/*************************************************
* Retire old output queues                       *
*************************************************/
void Output_Buffers::retire()
   {
   while(buffers.size())
      {
      if(buffers[0] == 0 || buffers[0]->size() == 0)
         {
         delete buffers[0];
         buffers.pop_front();
         ++offset;
         }
      else
         break;
      }
   }

/*************************************************
* Get a particular output queue                  *
*************************************************/
SecureQueue* Output_Buffers::get(u32bit msg) const
   {
   if(msg < offset)
      return 0;
   if(msg > message_count())
      throw Internal_Error("Output_Buffers::get: msg > size");

   return buffers[msg-offset];
   }

/*************************************************
* Return the total number of messages            *
*************************************************/
u32bit Output_Buffers::message_count() const
   {
   return (buffers.size() + offset);
   }

/*************************************************
* Output_Buffers Constructor                     *
*************************************************/
Output_Buffers::Output_Buffers()
   {
   offset = 0;
   }

/*************************************************
* Output_Buffers Destructor                      *
*************************************************/
Output_Buffers::~Output_Buffers()
   {
   for(u32bit j = 0; j != buffers.size(); ++j)
      delete buffers[j];
   }

}