~ubuntu-branches/ubuntu/quantal/psi/quantal

« back to all changes in this revision

Viewing changes to third-party/qca/qca/src/botantools/botan/botan/secmem.h

  • Committer: Bazaar Package Importer
  • Author(s): Jan Niehusmann
  • Date: 2008-04-14 18:57:30 UTC
  • mfrom: (2.1.9 hardy)
  • Revision ID: james.westby@ubuntu.com-20080414185730-528re3zp0m2hdlhi
Tags: 0.11-8
* added CONFIG -= link_prl to .pro files and removed dependencies
  which are made unnecessary by this change
* Fix segfault when closing last chat tab with qt4.4
  (This is from upstream svn, rev. 1101) (Closes: Bug#476122)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 1999-2007 The Botan Project. All rights reserved.
 
3
 
 
4
Redistribution and use in source and binary forms, for any use, with or without
 
5
modification, is permitted provided that the following conditions are met:
 
6
 
 
7
1. Redistributions of source code must retain the above copyright notice, this
 
8
list of conditions, and the following disclaimer.
 
9
 
 
10
2. Redistributions in binary form must reproduce the above copyright notice,
 
11
this list of conditions, and the following disclaimer in the documentation
 
12
and/or other materials provided with the distribution.
 
13
 
 
14
THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) "AS IS" AND ANY EXPRESS OR IMPLIED
 
15
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
16
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ARE DISCLAIMED.
 
17
 
 
18
IN NO EVENT SHALL THE AUTHOR(S) OR CONTRIBUTOR(S) BE LIABLE FOR ANY DIRECT,
 
19
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
20
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 
21
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 
22
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 
23
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 
24
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
25
*/
 
26
// LICENSEHEADER_END
 
27
namespace QCA { // WRAPNS_LINE
 
28
/*************************************************
 
29
* Secure Memory Buffers Header File              *
 
30
* (C) 1999-2007 The Botan Project                *
 
31
*************************************************/
 
32
 
 
33
#ifndef BOTAN_SECURE_MEMORY_BUFFERS_H__
 
34
#define BOTAN_SECURE_MEMORY_BUFFERS_H__
 
35
 
 
36
} // WRAPNS_LINE
 
37
#include <botan/allocate.h>
 
38
namespace QCA { // WRAPNS_LINE
 
39
} // WRAPNS_LINE
 
40
#include <botan/mem_ops.h>
 
41
namespace QCA { // WRAPNS_LINE
 
42
 
 
43
namespace Botan {
 
44
 
 
45
/*************************************************
 
46
* Variable Length Memory Buffer                  *
 
47
*************************************************/
 
48
template<typename T>
 
49
class MemoryRegion
 
50
   {
 
51
   public:
 
52
      u32bit size() const { return used; }
 
53
      u32bit is_empty() const { return (used == 0); }
 
54
      u32bit has_items() const { return (used != 0); }
 
55
 
 
56
      operator T* () { return buf; }
 
57
      operator const T* () const { return buf; }
 
58
 
 
59
      T* begin() { return buf; }
 
60
      const T* begin() const { return buf; }
 
61
 
 
62
      T* end() { return (buf + size()); }
 
63
      const T* end() const { return (buf + size()); }
 
64
 
 
65
      bool operator==(const MemoryRegion<T>& other) const
 
66
         {
 
67
         return (size() == other.size() &&
 
68
                 same_mem(buf, other.buf, size()));
 
69
         }
 
70
 
 
71
      bool operator<(const MemoryRegion<T>&) const;
 
72
 
 
73
      bool operator!=(const MemoryRegion<T>& in) const
 
74
         { return (!(*this == in)); }
 
75
      MemoryRegion<T>& operator=(const MemoryRegion<T>& in)
 
76
         { if(this != &in) set(in); return (*this); }
 
77
 
 
78
      void copy(const T in[], u32bit n)
 
79
         { copy(0, in, n); }
 
80
      void copy(u32bit off, const T in[], u32bit n)
 
81
         { copy_mem(buf + off, in, (n > size() - off) ? (size() - off) : n); }
 
82
 
 
83
      void set(const T in[], u32bit n)    { create(n); copy(in, n); }
 
84
      void set(const MemoryRegion<T>& in) { set(in.begin(), in.size()); }
 
85
 
 
86
      void append(const T data[], u32bit n)
 
87
         { grow_to(size()+n); copy(size() - n, data, n); }
 
88
      void append(T x) { append(&x, 1); }
 
89
      void append(const MemoryRegion<T>& x) { append(x.begin(), x.size()); }
 
90
 
 
91
      void clear() { clear_mem(buf, allocated); }
 
92
      void destroy() { create(0); }
 
93
 
 
94
      void create(u32bit);
 
95
      void grow_to(u32bit) const;
 
96
      void swap(MemoryRegion<T>&);
 
97
 
 
98
      ~MemoryRegion() { deallocate(buf, allocated); }
 
99
   protected:
 
100
      MemoryRegion() { buf = 0; alloc = 0; used = allocated = 0; }
 
101
      MemoryRegion(const MemoryRegion<T>& copy)
 
102
         {
 
103
         buf = 0;
 
104
         used = allocated = 0;
 
105
         alloc = copy.alloc;
 
106
         set(copy.buf, copy.used);
 
107
         }
 
108
 
 
109
      void init(bool locking, u32bit size = 0)
 
110
         { alloc = Allocator::get(locking); create(size); }
 
111
   private:
 
112
      T* allocate(u32bit n) const { return (T*)alloc->allocate(sizeof(T)*n); }
 
113
      void deallocate(T* p, u32bit n) const
 
114
         { alloc->deallocate(p, sizeof(T)*n); }
 
115
 
 
116
      mutable T* buf;
 
117
      mutable u32bit used;
 
118
      mutable u32bit allocated;
 
119
      mutable Allocator* alloc;
 
120
   };
 
121
 
 
122
/*************************************************
 
123
* Create a new buffer                            *
 
124
*************************************************/
 
125
template<typename T>
 
126
void MemoryRegion<T>::create(u32bit n)
 
127
   {
 
128
   if(n <= allocated) { clear(); used = n; return; }
 
129
   deallocate(buf, allocated);
 
130
   buf = allocate(n);
 
131
   allocated = used = n;
 
132
   }
 
133
 
 
134
/*************************************************
 
135
* Increase the size of the buffer                *
 
136
*************************************************/
 
137
template<typename T>
 
138
void MemoryRegion<T>::grow_to(u32bit n) const
 
139
   {
 
140
   if(n > used && n <= allocated)
 
141
      {
 
142
      clear_mem(buf + used, n - used);
 
143
      used = n;
 
144
      return;
 
145
      }
 
146
   else if(n > allocated)
 
147
      {
 
148
      T* new_buf = allocate(n);
 
149
      copy_mem(new_buf, buf, used);
 
150
      deallocate(buf, allocated);
 
151
      buf = new_buf;
 
152
      allocated = used = n;
 
153
      }
 
154
   }
 
155
 
 
156
/*************************************************
 
157
* Compare this buffer with another one           *
 
158
*************************************************/
 
159
template<typename T>
 
160
bool MemoryRegion<T>::operator<(const MemoryRegion<T>& in) const
 
161
   {
 
162
   if(size() < in.size()) return true;
 
163
   if(size() > in.size()) return false;
 
164
 
 
165
   for(u32bit j = 0; j != size(); j++)
 
166
      {
 
167
      if(buf[j] < in[j]) return true;
 
168
      if(buf[j] > in[j]) return false;
 
169
      }
 
170
 
 
171
   return false;
 
172
   }
 
173
 
 
174
/*************************************************
 
175
* Swap this buffer with another one              *
 
176
*************************************************/
 
177
template<typename T>
 
178
void MemoryRegion<T>::swap(MemoryRegion<T>& x)
 
179
   {
 
180
   std::swap(buf, x.buf);
 
181
   std::swap(used, x.used);
 
182
   std::swap(allocated, x.allocated);
 
183
   std::swap(alloc, x.alloc);
 
184
   }
 
185
 
 
186
/*************************************************
 
187
* Unlocked Variable Length Buffer                *
 
188
*************************************************/
 
189
template<typename T>
 
190
class MemoryVector : public MemoryRegion<T>
 
191
   {
 
192
   public:
 
193
      MemoryVector<T>& operator=(const MemoryRegion<T>& in)
 
194
         { if(this != &in) set(in); return (*this); }
 
195
 
 
196
      MemoryVector(u32bit n = 0) { MemoryRegion<T>::init(false, n); }
 
197
      MemoryVector(const T in[], u32bit n)
 
198
         { MemoryRegion<T>::init(false); set(in, n); }
 
199
      MemoryVector(const MemoryRegion<T>& in)
 
200
         { MemoryRegion<T>::init(false); set(in); }
 
201
      MemoryVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2)
 
202
         { MemoryRegion<T>::init(false); set(in1); append(in2); }
 
203
   };
 
204
 
 
205
/*************************************************
 
206
* Locked Variable Length Buffer                  *
 
207
*************************************************/
 
208
template<typename T>
 
209
class SecureVector : public MemoryRegion<T>
 
210
   {
 
211
   public:
 
212
      SecureVector<T>& operator=(const MemoryRegion<T>& in)
 
213
         { if(this != &in) set(in); return (*this); }
 
214
 
 
215
      SecureVector(u32bit n = 0) { MemoryRegion<T>::init(true, n); }
 
216
      SecureVector(const T in[], u32bit n)
 
217
         { MemoryRegion<T>::init(true); set(in, n); }
 
218
      SecureVector(const MemoryRegion<T>& in)
 
219
         { MemoryRegion<T>::init(true); set(in); }
 
220
      SecureVector(const MemoryRegion<T>& in1, const MemoryRegion<T>& in2)
 
221
         { MemoryRegion<T>::init(true); set(in1); append(in2); }
 
222
   };
 
223
 
 
224
/*************************************************
 
225
* Locked Fixed Length Buffer                     *
 
226
*************************************************/
 
227
template<typename T, u32bit L>
 
228
class SecureBuffer : public MemoryRegion<T>
 
229
   {
 
230
   public:
 
231
      SecureBuffer<T,L>& operator=(const SecureBuffer<T,L>& in)
 
232
         { if(this != &in) set(in); return (*this); }
 
233
 
 
234
      SecureBuffer() { MemoryRegion<T>::init(true, L); }
 
235
      SecureBuffer(const T in[], u32bit n)
 
236
         { MemoryRegion<T>::init(true, L); copy(in, n); }
 
237
   private:
 
238
      SecureBuffer<T, L>& operator=(const MemoryRegion<T>& in)
 
239
         { if(this != &in) set(in); return (*this); }
 
240
   };
 
241
 
 
242
}
 
243
 
 
244
#endif
 
245
} // WRAPNS_LINE