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

tdeio/tdeio

  • tdeio
  • tdeio
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 <tdelocale.h>
26 #include <kdebug.h>
27 #include <stdlib.h>
28 #include <tdeapplication.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  // WARNING
210  // These numbers are octal. Do not remove the leading zeros!
211  unsigned char p[6][3] = {{0222, 0111, 044}, {0111, 044, 0222},
212  { 044, 0222, 0111}, {0155, 0266, 0333},
213  {0266, 0333, 0155}, {0333, 0155, 0266}};
214  TQString msg = i18n("Shredding: pass %1 of 35");
215 
216  emit processedSize(0);
217 
218  // thirty-five times writing the entire file size
219  totalBytes = fileSize * 35;
220  int iteration = 1;
221 
222  for (int ctr = 0; ctr < 4; ctr++)
223  if (!fillrandom())
224  return false;
225  else
226  {
227  emit infoMessage(msg.arg(iteration));
228  }
229 
230  if (!fillbyte((unsigned int) 0x55)) // '0x55' is 01010101
231  return false;
232  emit infoMessage(msg.arg(iteration));
233 
234  if (!fillbyte((unsigned int) 0xAA)) // '0xAA' is 10101010
235  return false;
236  emit infoMessage(msg.arg(iteration));
237 
238  for (unsigned int ctr = 0; ctr < 3; ctr++)
239  if (!fillpattern(p[ctr], 3)) // '0x92', '0x49', '0x24'
240  return false;
241  else
242  {
243  emit infoMessage(msg.arg(iteration));
244  }
245 
246  for (unsigned int ctr = 0; ctr <= 255 ; ctr += 17)
247  if (!fillbyte(ctr)) // sequence of '0x00', '0x11', ..., '0xFF'
248  return false;
249  else
250  {
251  emit infoMessage(msg.arg(iteration));
252  }
253 
254  for (unsigned int ctr = 0; ctr < 6; ctr++)
255  if (!fillpattern(p[ctr], 3)) // '0x92', '0x49', '0x24'
256  return false;
257  else
258  {
259  emit infoMessage(msg.arg(iteration));
260  }
261 
262  for (int ctr = 0; ctr < 4; ctr++)
263  if (!fillrandom())
264  return false;
265  else
266  {
267  emit infoMessage(msg.arg(iteration));
268  }
269 
270  if (!file->remove())
271  return false;
272  file = 0L;
273  emit processedSize(fileSize);
274  return true;
275 }
276 
277 #include "kshred.moc"
278 
KShred::fill1s
bool fill1s()
Writes all 1's over the entire file and flushes the file buffers.
Definition: kshred.cpp:68
KShred::fillbyte
bool fillbyte(unsigned int byte)
Writes the specified byte over the entire file and flushes the file buffers.
Definition: kshred.cpp:82
KShred::shred
bool shred()
Shreds a file by writing a series of values over it (uses fill0s, then fill1s, then fillrandom...
Definition: kshred.cpp:207
KShred::fillpattern
bool fillpattern(unsigned char *pattern, unsigned int size)
Writes the specified byte array over the entire file and flushes the file buffers.
Definition: kshred.cpp:103
KShred::fillrandom
bool fillrandom()
Writes random bites over the entire file and flushes the file buffers.
Definition: kshred.cpp:122
KShred::infoMessage
void infoMessage(const TQString &message)
Shows a message in the progress dialog.
KShred::KShred
KShred(TQString fileName)
Initialize the class using the name of the file to 'shred'.
Definition: kshred.cpp:31
KShred::processedSize
void processedSize(TDEIO::filesize_t bytes)
Shows progress of the shredding.
KShred
Definition: kshred.h:45
KShred::fill0s
bool fill0s()
Writes all 0's over the entire file and flushes the file buffers.
Definition: kshred.cpp:75

tdeio/tdeio

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

tdeio/tdeio

Skip menu "tdeio/tdeio"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  •     tdecore
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  • tdeioslave
  •   http
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for tdeio/tdeio by doxygen 1.8.8
This website is maintained by Timothy Pearson.