monotone

monotone Mtn Source Tree

Root/botan/secqueue.cpp

1/*************************************************
2* SecureQueue Source File *
3* (C) 1999-2005 The Botan Project *
4*************************************************/
5
6#include <botan/secqueue.h>
7
8namespace Botan {
9
10/*************************************************
11* SecureQueueNode *
12*************************************************/
13class SecureQueueNode
14 {
15 public:
16 u32bit write(const byte input[], u32bit length)
17 {
18 u32bit copied = std::min(length, buffer.size() - end);
19 copy_mem(buffer + end, input, copied);
20 end += copied;
21 return copied;
22 }
23 u32bit read(byte output[], u32bit length)
24 {
25 u32bit copied = std::min(length, end - start);
26 copy_mem(output, buffer + start, copied);
27 start += copied;
28 return copied;
29 }
30 u32bit peek(byte output[], u32bit length, u32bit offset = 0)
31 {
32 const u32bit left = end - start;
33 if(offset >= left) return 0;
34 u32bit copied = std::min(length, left - offset);
35 copy_mem(output, buffer + start + offset, copied);
36 return copied;
37 }
38 u32bit size() const { return (end - start); }
39 SecureQueueNode() { next = 0; start = end = 0; }
40 ~SecureQueueNode() { next = 0; start = end = 0; }
41 private:
42 friend class SecureQueue;
43 SecureQueueNode* next;
44 SecureBuffer<byte, DEFAULT_BUFFERSIZE> buffer;
45 u32bit start, end;
46 };
47
48/*************************************************
49* Create a SecureQueue *
50*************************************************/
51SecureQueue::SecureQueue()
52 {
53 set_next(0, 0);
54 head = tail = new SecureQueueNode;
55 }
56
57/*************************************************
58* Copy a SecureQueue *
59*************************************************/
60SecureQueue::SecureQueue(const SecureQueue& input) :
61 Fanout_Filter(), DataSource()
62 {
63 set_next(0, 0);
64
65 head = tail = new SecureQueueNode;
66 SecureQueueNode* temp = input.head;
67 while(temp)
68 {
69 write(temp->buffer + temp->start, temp->end - temp->start);
70 temp = temp->next;
71 }
72 }
73
74/*************************************************
75* Destroy this SecureQueue *
76*************************************************/
77void SecureQueue::destroy()
78 {
79 SecureQueueNode* temp = head;
80 while(temp)
81 {
82 SecureQueueNode* holder = temp->next;
83 delete temp;
84 temp = holder;
85 }
86 head = tail = 0;
87 }
88
89/*************************************************
90* Copy a SecureQueue *
91*************************************************/
92SecureQueue& SecureQueue::operator=(const SecureQueue& input)
93 {
94 destroy();
95 head = tail = new SecureQueueNode;
96 SecureQueueNode* temp = input.head;
97 while(temp)
98 {
99 write(temp->buffer + temp->start, temp->end - temp->start);
100 temp = temp->next;
101 }
102 return (*this);
103 }
104
105/*************************************************
106* Add some bytes to the queue *
107*************************************************/
108void SecureQueue::write(const byte input[], u32bit length)
109 {
110 if(!head)
111 head = tail = new SecureQueueNode;
112 while(length)
113 {
114 const u32bit n = tail->write(input, length);
115 input += n;
116 length -= n;
117 if(length)
118 {
119 tail->next = new SecureQueueNode;
120 tail = tail->next;
121 }
122 }
123 }
124
125/*************************************************
126* Read some bytes from the queue *
127*************************************************/
128u32bit SecureQueue::read(byte output[], u32bit length)
129 {
130 u32bit got = 0;
131 while(length && head)
132 {
133 const u32bit n = head->read(output, length);
134 output += n;
135 got += n;
136 length -= n;
137 if(head->size() == 0)
138 {
139 SecureQueueNode* holder = head->next;
140 delete head;
141 head = holder;
142 }
143 }
144 return got;
145 }
146
147/*************************************************
148* Read data, but do not remove it from queue *
149*************************************************/
150u32bit SecureQueue::peek(byte output[], u32bit length, u32bit offset) const
151 {
152 SecureQueueNode* current = head;
153
154 while(offset && current)
155 {
156 if(offset >= current->size())
157 {
158 offset -= current->size();
159 current = current->next;
160 }
161 else
162 break;
163 }
164
165 u32bit got = 0;
166 while(length && current)
167 {
168 const u32bit n = current->peek(output, length, offset);
169 offset = 0;
170 output += n;
171 got += n;
172 length -= n;
173 current = current->next;
174 }
175 return got;
176 }
177
178/*************************************************
179* Return how many bytes the queue holds *
180*************************************************/
181u32bit SecureQueue::size() const
182 {
183 SecureQueueNode* current = head;
184 u32bit count = 0;
185
186 while(current)
187 {
188 count += current->size();
189 current = current->next;
190 }
191 return count;
192 }
193
194/*************************************************
195* Test if the queue has any data in it *
196*************************************************/
197bool SecureQueue::end_of_data() const
198 {
199 return (size() == 0);
200 }
201
202}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status