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

« back to all changes in this revision

Viewing changes to third-party/qca/qca/src/botantools/botan/alloc_mmap/mmap_mem.cpp

  • 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
* Memory Mapping Allocator Source File           *
 
30
* (C) 1999-2007 The Botan Project                *
 
31
*************************************************/
 
32
 
 
33
} // WRAPNS_LINE
 
34
#include <botan/mmap_mem.h>
 
35
namespace QCA { // WRAPNS_LINE
 
36
} // WRAPNS_LINE
 
37
#include <cstring>
 
38
namespace QCA { // WRAPNS_LINE
 
39
 
 
40
#ifndef _XOPEN_SOURCE
 
41
  #define _XOPEN_SOURCE 500
 
42
#endif
 
43
 
 
44
#ifndef _XOPEN_SOURCE_EXTENDED
 
45
  #define _XOPEN_SOURCE_EXTENDED 1
 
46
#endif
 
47
 
 
48
} // WRAPNS_LINE
 
49
#include <sys/types.h>
 
50
namespace QCA { // WRAPNS_LINE
 
51
} // WRAPNS_LINE
 
52
#include <sys/mman.h>
 
53
namespace QCA { // WRAPNS_LINE
 
54
} // WRAPNS_LINE
 
55
#include <sys/stat.h>
 
56
namespace QCA { // WRAPNS_LINE
 
57
} // WRAPNS_LINE
 
58
#include <unistd.h>
 
59
namespace QCA { // WRAPNS_LINE
 
60
} // WRAPNS_LINE
 
61
#include <stdlib.h>
 
62
namespace QCA { // WRAPNS_LINE
 
63
} // WRAPNS_LINE
 
64
#include <fcntl.h>
 
65
namespace QCA { // WRAPNS_LINE
 
66
 
 
67
#ifndef MAP_FAILED
 
68
   #define MAP_FAILED -1
 
69
#endif
 
70
 
 
71
namespace Botan {
 
72
 
 
73
namespace {
 
74
 
 
75
/*************************************************
 
76
* MemoryMapping_Allocator Exception              *
 
77
*************************************************/
 
78
class MemoryMapping_Failed : public Exception
 
79
   {
 
80
   public:
 
81
      MemoryMapping_Failed(const std::string& msg) :
 
82
         Exception("MemoryMapping_Allocator: " + msg) {}
 
83
   };
 
84
 
 
85
}
 
86
 
 
87
/*************************************************
 
88
* Memory Map a File into Memory                  *
 
89
*************************************************/
 
90
void* MemoryMapping_Allocator::alloc_block(u32bit n)
 
91
   {
 
92
   class TemporaryFile
 
93
      {
 
94
      public:
 
95
         int get_fd() const { return fd; }
 
96
         const std::string path() const { return filepath; }
 
97
 
 
98
         TemporaryFile(const std::string& base)
 
99
            {
 
100
            const std::string path = base + "XXXXXX";
 
101
 
 
102
            filepath = new char[path.length() + 1];
 
103
            std::strcpy(filepath, path.c_str());
 
104
 
 
105
            mode_t old_umask = umask(077);
 
106
            fd = mkstemp(filepath);
 
107
            umask(old_umask);
 
108
            }
 
109
 
 
110
         ~TemporaryFile()
 
111
            {
 
112
            delete[] filepath;
 
113
            if(fd != -1 && close(fd) == -1)
 
114
               throw MemoryMapping_Failed("Could not close file");
 
115
            }
 
116
      private:
 
117
         int fd;
 
118
         char* filepath;
 
119
      };
 
120
 
 
121
   TemporaryFile file("/tmp/botan_");
 
122
 
 
123
   if(file.get_fd() == -1)
 
124
      throw MemoryMapping_Failed("Could not create file");
 
125
 
 
126
   if(unlink(file.path().c_str()))
 
127
      throw MemoryMapping_Failed("Could not unlink file " + file.path());
 
128
 
 
129
   lseek(file.get_fd(), n-1, SEEK_SET);
 
130
   if(write(file.get_fd(), "\0", 1) != 1)
 
131
      throw MemoryMapping_Failed("Could not write to file");
 
132
 
 
133
   void* ptr = mmap(0, n, PROT_READ | PROT_WRITE, MAP_SHARED,
 
134
                    file.get_fd(), 0);
 
135
 
 
136
   if(ptr == (void*)MAP_FAILED)
 
137
      throw MemoryMapping_Failed("Could not map file");
 
138
 
 
139
   return ptr;
 
140
   }
 
141
 
 
142
/*************************************************
 
143
* Remove a Memory Mapping                        *
 
144
*************************************************/
 
145
void MemoryMapping_Allocator::dealloc_block(void* ptr, u32bit n)
 
146
   {
 
147
   if(ptr == 0) return;
 
148
#ifdef MLOCK_NOT_VOID_PTR
 
149
# define MLOCK_TYPE_CAST (char *)
 
150
#else
 
151
# define MLOCK_TYPE_CAST
 
152
#endif
 
153
 
 
154
   const u32bit OVERWRITE_PASSES = 12;
 
155
   const byte PATTERNS[] = { 0x00, 0xFF, 0xAA, 0x55, 0x73, 0x8C, 0x5F, 0xA0,
 
156
                             0x6E, 0x91, 0x30, 0xCF, 0xD3, 0x2C, 0xAC, 0x53 };
 
157
 
 
158
   for(u32bit j = 0; j != OVERWRITE_PASSES; j++)
 
159
      {
 
160
      std::memset(ptr, PATTERNS[j % sizeof(PATTERNS)], n);
 
161
      if(msync(MLOCK_TYPE_CAST ptr, n, MS_SYNC))
 
162
         throw MemoryMapping_Failed("Sync operation failed");
 
163
      }
 
164
   std::memset(ptr, 0, n);
 
165
   if(msync(MLOCK_TYPE_CAST ptr, n, MS_SYNC))
 
166
      throw MemoryMapping_Failed("Sync operation failed");
 
167
 
 
168
   if(munmap(MLOCK_TYPE_CAST ptr, n))
 
169
      throw MemoryMapping_Failed("Could not unmap file");
 
170
   }
 
171
 
 
172
}
 
173
} // WRAPNS_LINE