• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kio/kio
 

kio/kio

  • kio
  • kio
kshred.cpp
1 /*--------------------------------------------------------------------------*
2  KShred.h Copyright (c) 2000 MieTerra LLC.
3  Credits: Andreas F. Pour <bugs@mieterra.com>
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy
6 of this software and associated documentation files (the "Software"), to deal
7 in the Software without restriction, including without limitation the rights
8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 copies of the Software, and to permit persons to whom the Software is
10 furnished to do so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in
13 all copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22 
23 #include "kshred.h"
24 #include <time.h>
25 #include <klocale.h>
26 #include <kdebug.h>
27 #include <stdlib.h>
28 #include <kapplication.h>
29 
30 // antlarr: KDE 4: Make it const TQString &
31 KShred::KShred(TQString fileName)
32 {
33  if (fileName.isEmpty())
34  {
35  kdError() << "KShred: missing file name in constructor" << endl;
36  file = 0L;
37  }
38  else
39  {
40  file = new TQFile();
41  file->setName(fileName);
42  if (!file->open(IO_ReadWrite))
43  {
44  kdError() << "KShred: cannot open file '" << fileName.local8Bit().data() << "' for writing\n" << endl;
45  file = 0L;
46  fileSize = 0;
47  }
48  else
49  fileSize = file->size();
50 
51  totalBytes = 0;
52  bytesWritten = 0;
53  lastSignalled = 0;
54  tbpc = 0;
55  fspc = 0;
56  }
57 }
58 
59 
60 KShred::~KShred()
61 {
62  if (file != 0L)
63  delete file;
64 }
65 
66 
67 bool
68 KShred::fill1s()
69 {
70  return fillbyte(0xFF);
71 }
72 
73 
74 bool
75 KShred::fill0s()
76 {
77  return fillbyte(0x0);
78 }
79 
80 
81 bool
82 KShred::fillbyte(unsigned int byte)
83 {
84  if (file == 0L)
85  return false;
86  unsigned char buff[4096];
87  memset((void *) buff, byte, 4096);
88 
89  unsigned int n;
90  for (unsigned int todo = fileSize; todo > 0; todo -= n)
91  {
92  n = (todo > 4096 ? 4096 : todo);
93  if (!writeData(buff, n))
94  return false;
95  }
96  if (!flush())
97  return false;
98  return file->at(0);
99 }
100 
101 
102 bool
103 KShred::fillpattern(unsigned char *data, unsigned int size)
104 {
105  if (file == 0L)
106  return false;
107 
108  unsigned int n;
109  for (unsigned int todo = fileSize; todo > 0; todo -= n)
110  {
111  n = (todo > size ? size : todo);
112  if (!writeData(data, n))
113  return false;
114  }
115  if (!flush())
116  return false;
117  return file->at(0);
118 }
119 
120 
121 bool
122 KShred::fillrandom()
123 {
124  if (file == 0L)
125  return false;
126 
127  long int buff[4096 / sizeof(long int)];
128  unsigned int n;
129 
130  for (unsigned int todo = fileSize; todo > 0; todo -= n)
131  {
132  n = (todo > 4096 ? 4096 : todo);
133  // assumes that 4096 is a multipe of sizeof(long int)
134  int limit = (n + sizeof(long int) - 1) / sizeof(long int);
135  for (int i = 0; i < limit; i++)
136  buff[i] = kapp->random();
137 
138  if (!writeData((unsigned char *) buff, n))
139  return false;
140  }
141  if (!flush())
142  return false;
143  return file->at(0);
144 }
145 
146 
147 // antlarr: KDE 4: Make it const TQString &
148 bool
149 KShred::shred(TQString fileName)
150 {
151  if (fileName.isEmpty())
152  return false;
153 
154  KShred shredder(fileName);
155  return shredder.shred();
156 }
157 
158 
159 bool
160 KShred::writeData(unsigned char *data, unsigned int size)
161 {
162  unsigned int ret = 0;
163 
164  // write 'data' of size 'size' to the file
165  while ((ret < size) && (file->putch((int) data[ret]) >= 0))
166  ret++;
167 
168  if ((totalBytes > 0) && (ret > 0))
169  {
170  if (tbpc == 0)
171  {
172  tbpc = ((unsigned int) (totalBytes / 100)) == 0 ? 1 : totalBytes / 100;
173  fspc = ((unsigned int) (fileSize / 100)) == 0 ? 1 : fileSize / 100;
174  }
175  bytesWritten += ret;
176  unsigned int pc = (unsigned int) (bytesWritten / tbpc);
177  if (pc > lastSignalled)
178  {
179  emit processedSize(fspc * pc);
180  lastSignalled = pc;
181  }
182  }
183  return ret == size;
184 }
185 
186 
187 bool
188 KShred::flush()
189 {
190  if (file == 0L)
191  return false;
192 
193  file->flush();
194  return (fsync(file->handle()) == 0);
195 }
196 
197 
198 // shred the file, then close and remove it
199 //
200 // UPDATED: this function now uses 35 passes based on the the article
201 // Peter Gutmann, "Secure Deletion of Data from Magnetic and Solid-State
202 // Memory", first published in the Sixth USENIX Security Symposium
203 // Proceedings, San Jose, CA, July 22-25, 1996 (available online at
204 // http://rootprompt.org/article.php3?article=473)
205 
206 bool
207 KShred::shred()
208 {
209  unsigned char p[6][3] = {{'\222', '\111', '\044'}, {'\111', '\044', '\222'},
210  {'\044', '\222', '\111'}, {'\155', '\266', '\333'},
211  {'\266', '\333', '\155'}, {'\333', '\155', '\266'}};
212  TQString msg = i18n("Shredding: pass %1 of 35");
213 
214  emit processedSize(0);
215 
216  // thirty-five times writing the entire file size
217  totalBytes = fileSize * 35;
218  int iteration = 1;
219 
220  for (int ctr = 0; ctr < 4; ctr++)
221  if (!fillrandom())
222  return false;
223  else
224  {
225  emit infoMessage(msg.arg(iteration));
226  }
227 
228  if (!fillbyte((unsigned int) 0x55)) // '0x55' is 01010101
229  return false;
230  emit infoMessage(msg.arg(iteration));
231 
232  if (!fillbyte((unsigned int) 0xAA)) // '0xAA' is 10101010
233  return false;
234  emit infoMessage(msg.arg(iteration));
235 
236  for (unsigned int ctr = 0; ctr < 3; ctr++)
237  if (!fillpattern(p[ctr], 3)) // '0x92', '0x49', '0x24'
238  return false;
239  else
240  {
241  emit infoMessage(msg.arg(iteration));
242  }
243 
244  for (unsigned int ctr = 0; ctr <= 255 ; ctr += 17)
245  if (!fillbyte(ctr)) // sequence of '0x00', '0x11', ..., '0xFF'
246  return false;
247  else
248  {
249  emit infoMessage(msg.arg(iteration));
250  }
251 
252  for (unsigned int ctr = 0; ctr < 6; ctr++)
253  if (!fillpattern(p[ctr], 3)) // '0x92', '0x49', '0x24'
254  return false;
255  else
256  {
257  emit infoMessage(msg.arg(iteration));
258  }
259 
260  for (int ctr = 0; ctr < 4; ctr++)
261  if (!fillrandom())
262  return false;
263  else
264  {
265  emit infoMessage(msg.arg(iteration));
266  }
267 
268  if (!file->remove())
269  return false;
270  file = 0L;
271  emit processedSize(fileSize);
272  return true;
273 }
274 
275 #include "kshred.moc"
276 

kio/kio

Skip menu "kio/kio"
  • Main Page
  • Modules
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kio/kio

Skip menu "kio/kio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •     interface
  •     library
  •   kspeech
  •   ktexteditor
  • kabc
  • kate
  • kcmshell
  • kdecore
  • kded
  • kdefx
  • kdeprint
  • kdesu
  • kdeui
  • kdoctools
  • khtml
  • kimgio
  • kinit
  • kio
  •   bookmarks
  •   httpfilter
  •   kfile
  •   kio
  •   kioexec
  •   kpasswdserver
  •   kssl
  • kioslave
  •   http
  • kjs
  • kmdi
  •   kmdi
  • knewstuff
  • kparts
  • krandr
  • kresources
  • kspell2
  • kunittest
  • kutils
  • kwallet
  • libkmid
  • libkscreensaver
Generated for kio/kio by doxygen 1.8.3.1
This website is maintained by Timothy Pearson.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. |