1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
|
/*
* miscellaneous.c: functions that may be generally used throughout nwipes code,
* mainly string processing functions but also time related functions.
*
* Copyright PartialVolume <https://github.com/PartialVolume>.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#ifndef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#endif
#ifndef _POSIX_SOURCE
#define _POSIX_SOURCE
#endif
#include <stdio.h>
#include "nwipe.h"
#include "context.h"
#include "logging.h"
#include "miscellaneous.h"
/* Convert string to upper case
*/
void strupper( char* str )
{
int idx;
idx = 0;
while( str[idx] != 0 )
{
/* If upper case alpha character, change to lower case */
if( str[idx] >= 'A' && str[idx] <= 'Z' )
{
str[idx] -= 32;
}
idx++;
}
}
/* Convert string to lower case
*/
void strlower( char* str )
{
int idx;
idx = 0;
while( str[idx] != 0 )
{
/* If upper case alpha character, change to lower case */
if( str[idx] >= 'A' && str[idx] <= 'Z' )
{
str[idx] += 32;
}
idx++;
}
}
void strip_CR_LF( char* str )
{
/* In the specified string, replace any CR or LF with a space */
int idx = 0;
int len = strlen( str );
while( idx < len )
{
if( str[idx] == 0x0A || str[idx] == 0x0D )
{
str[idx] = ' ';
}
idx++;
}
}
/* Search a string for a positive number, convert the first
* number found to binary and return the binary number.
* returns the number or -1 if number too large or -2 if
* no number found.
*/
u64 str_ascii_number_to_ll( char* str )
{
int idx;
int idx2;
char number_copy[20];
idx = 0; // index into the main string we are searching
idx2 = 0; // index used for backup copy of ascii number
while( str[idx] != 0 )
{
/* Find the start of the number */
if( str[idx] >= '0' && str[idx] <= '9' )
{
while( str[idx] != 0 )
{
/* Find the end of the number */
if( str[idx] >= '0' && str[idx] <= '9' )
{
if( idx2 < sizeof( number_copy ) - 1 )
{
number_copy[idx2++] = str[idx++];
}
else
{
/* Number is too large ! */
return -1;
}
}
else
{
/* end found */
number_copy[idx2] = 0; // terminate our copy
/* convert ascii number to longlong */
return atoll( number_copy );
}
}
}
else
{
idx++;
}
}
return -2; /* no number found */
}
void Determine_C_B_nomenclature( u64 qty, char* result, int result_array_size )
{
/* C_B ? Determine Capacity or Bandwidth nomenclature
*
* A pointer to a result character string with a minimum of 13 characters in length
* should be provided.
*
* Outputs a string of the form xxxTB, xxxGB, xxxMB, xxxKB B depending on the value of 'qty'
*/
/* Initialise the output array */
int idx = 0;
while( idx < result_array_size )
{
result[idx++] = 0;
}
/* Determine the size of throughput so that the correct nomenclature can be used */
if( qty >= INT64_C( 10000000000000 ) )
{
snprintf( result, result_array_size, "%4llu TB", qty / INT64_C( 1000000000000 ) );
}
else if( qty >= INT64_C( 10000000000 ) )
{
snprintf( result, result_array_size, "%4llu GB", qty / INT64_C( 1000000000 ) );
}
else if( qty >= INT64_C( 10000000 ) )
{
snprintf( result, result_array_size, "%4llu MB", qty / INT64_C( 1000000 ) );
}
else if( qty >= INT64_C( 10000 ) )
{
snprintf( result, result_array_size, "%4llu KB", qty / INT64_C( 1000 ) );
}
else
{
snprintf( result, result_array_size, "%4llu B", qty / INT64_C( 1 ) );
}
}
void convert_seconds_to_hours_minutes_seconds( u64 total_seconds, int* hours, int* minutes, int* seconds )
{
/* Convert binary seconds into binary hours, minutes and seconds */
if( total_seconds % 60 )
{
*minutes = total_seconds / 60;
*seconds = total_seconds - ( *minutes * 60 );
}
else
{
*minutes = total_seconds / 60;
*seconds = 0;
}
if( *minutes > 59 )
{
*hours = *minutes / 60;
if( *minutes % 60 )
{
*minutes = *minutes - ( *hours * 60 );
}
else
{
*minutes = 0;
}
}
}
int nwipe_strip_path( char* output, char* input )
{
/* Take the input string, say "/dev/sda" and remove the "/dev/", prefix the result
* with 'length' spaces. So if length=8 and input=/dev/sda, output will
* be " sda", a string 8 characters long right justified with spaces.
*/
int idx_dest;
int idx_src;
idx_dest = 8;
// idx_dest = length;
output[idx_dest--] = 0;
idx_src = strlen( input );
idx_src--;
while( idx_dest >= 0 )
{
/* if the device name contains a / start prefixing spaces */
if( input[idx_src] == '/' )
{
output[idx_dest--] = ' ';
continue;
}
if( idx_src >= 0 )
{
output[idx_dest--] = input[idx_src--];
}
}
return 0;
}
void replace_non_alphanumeric( char* str, char replacement_char )
{
int i = 0;
while( str[i] != 0 )
{
if( str[i] < '0' || ( str[i] > '9' && str[i] < 'A' ) || ( str[i] > 'Z' && str[i] < 'a' ) || str[i] > 'z' )
{
str[i] = replacement_char;
}
i++;
}
}
void convert_double_to_string( char* output_str, double value )
{
int idx = 0;
int idx2;
int idx3 = 0;
char percstr[512] = "";
snprintf( percstr, sizeof( percstr ), "%5.32lf", value );
while( percstr[idx] != 0 )
{
if( percstr[idx] == '.' )
{
for( idx2 = 0; idx2 < 3; idx2++ )
{
output_str[idx3++] = percstr[idx++];
}
break;
}
output_str[idx3++] = percstr[idx++];
}
output_str[idx3] = 0;
}
int read_system_datetime( char* year, char* month, char* day, char* hours, char* minutes, char* seconds )
{
/* Reads system date & time and populates the caller provided strings.
* Each string is null terminated by this function. The calling program
* must provide the minimum string sizes as shown below.
*
* year 5 bytes (4 numeric digits plus NULL terminator)
* month 3 bytes (2 numeric digits plus NULL terminator)
* day 3 bytes (2 numeric digits plus NULL terminator)
* hours 3 bytes (2 numeric digits plus NULL terminator)
* minutes 3 bytes (2 numeric digits plus NULL terminator)
* seconds 3 bytes (2 numeric digits plus NULL terminator)
*
* return value:
* 0 = success
* -1 = Failure, see nwipe log for detail.
*/
FILE* fp;
int r; // A result buffer.
int idx; // general index
int status = 0;
/**
* Obtain the year
*/
fp = popen( "date +%Y", "r" );
if( fp == NULL )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to obtain system year using commmand = date +%Y" );
}
else
{
/* Read the first line and validate it. Should be 4 numeric digits */
if( fgets( year, FOUR_DIGITS + 1, fp ) != NULL )
{
idx = 0;
while( idx < 4 )
{
if( year[idx] >= '0' && year[idx] <= '9' )
{
idx++;
}
else
{
/* if we haven't reached the correct number of digits due to invalid data, log error */
year[++idx] = 0; /* terminate the string, prior to using in nwipe_log */
nwipe_log( NWIPE_LOG_ERROR,
"Obtained system year using command = date +%Y, but result appears invalid = %s",
year );
status = -1;
break;
}
}
year[idx] = 0; /* terminate the string */
}
r = pclose( fp );
}
/**
* Obtain the month
*/
fp = popen( "date +%m", "r" );
if( fp == NULL )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to obtain system month using the command = date +%m" );
}
else
{
/* Read the first line and validate it. Should be 2 numeric digits */
if( fgets( month, TWO_DIGITS + 1, fp ) != NULL )
{
idx = 0;
while( idx < 2 )
{
if( month[idx] >= '0' && month[idx] <= '9' )
{
idx++;
}
else
{
/* if we haven't reached the correct number of digits due to invalid data, log error */
month[++idx] = 0; /* terminate the string, prior to using in nwipe_log */
nwipe_log( NWIPE_LOG_ERROR,
"Obtained system month using command = date +%m, but result appears invalid = %s",
month );
status = -1;
break;
}
}
month[idx] = 0; /* terminate the string */
}
r = pclose( fp );
}
/**
* Obtain the day
*/
fp = popen( "date +\%d", "r" );
if( fp == NULL )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to obtain system day using the command = date +\%d" );
}
else
{
/* Read the first line and validate it. Should be 2 numeric digits */
if( fgets( day, TWO_DIGITS + 1, fp ) != NULL )
{
idx = 0;
while( idx < 2 )
{
if( day[idx] >= '0' && day[idx] <= '9' )
{
idx++;
}
else
{
/* if we haven't reached the correct number of digits due to invalid data, log error */
day[++idx] = 0; /* terminate the string, prior to using in nwipe_log */
nwipe_log( NWIPE_LOG_ERROR,
"Obtained system day using command = date +\%d, but result appears invalid = %s",
day );
status = -1;
break;
}
}
day[idx] = 0; /* terminate the string */
}
r = pclose( fp );
}
/**
* Obtain the hours
*/
fp = popen( "date +%H", "r" );
if( fp == NULL )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to obtain system hour using the command = date +%H" );
}
else
{
/* Read the first line and validate it. Should be 2 numeric digits */
if( fgets( hours, TWO_DIGITS + 1, fp ) != NULL )
{
// nwipe_log( NWIPE_LOG_INFO, "Seconds = %s, Year = %s", seconds, year);
idx = 0;
while( idx < 2 )
{
if( hours[idx] >= '0' && hours[idx] <= '9' )
{
idx++;
}
else
{
/* if we haven't reached the correct number of digits due to invalid data, log error */
hours[++idx] = 0; /* terminate the string, prior to using in nwipe_log */
nwipe_log( NWIPE_LOG_ERROR,
"Obtained system hours using command = date +%H, but result appears invalid = %s",
hours );
status = -1;
break;
}
}
hours[idx] = 0; /* terminate the string */
}
r = pclose( fp );
}
/**
* Obtain the minutes
*/
fp = popen( "date +%M", "r" );
if( fp == NULL )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to obtain system minutes using the command = date +%M" );
}
else
{
/* Read the first line and validate it. Should be 2 numeric digits */
if( fgets( minutes, TWO_DIGITS + 1, fp ) != NULL )
{
// nwipe_log( NWIPE_LOG_INFO, "Seconds = %s, Year = %s", seconds, year);
idx = 0;
while( idx < 2 )
{
if( minutes[idx] >= '0' && minutes[idx] <= '9' )
{
idx++;
}
else
{
/* if we haven't reached the correct number of digits due to invalid data, log the error */
minutes[++idx] = 0; /* terminate the string, prior to using in nwipe_log */
nwipe_log( NWIPE_LOG_ERROR,
"Obtained system minutes using command = date +%H, but result appears invalid = %s",
minutes );
status = -1;
break;
}
}
minutes[idx] = 0; /* terminate the string */
}
r = pclose( fp );
}
/**
* Obtain the seconds
*/
fp = popen( "date +%S", "r" );
if( fp == NULL )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to obtain system seconds using the command = date +%S" );
}
else
{
/* Read the first line and validate it. Should be 2 numeric digits */
if( fgets( seconds, TWO_DIGITS + 1, fp ) != NULL )
{
// nwipe_log( NWIPE_LOG_INFO, "Seconds = %s, Year = %s", seconds, year);
idx = 0;
while( idx < 2 )
{
if( seconds[idx] >= '0' && seconds[idx] <= '9' )
{
idx++;
}
else
{
/* if we haven't reached the correct number of digits due to invalid data, log error */
seconds[++idx] = 0; /* terminate the string, prior to using in nwipe_log */
nwipe_log( NWIPE_LOG_ERROR,
"Obtained system seconds using command = date +%S, but result appears invalid = %s",
seconds );
status = -1;
break;
}
}
seconds[idx] = 0; /* terminate the string */
}
r = pclose( fp );
}
return status;
}
int write_system_datetime( char* year, char* month, char* day, char* hours, char* minutes, char* seconds )
{
/* Writes the system date & time using data from the caller provided strings.
* The calling program must provide the minimum string sizes as shown below
* populated with current date and time data.
*
* year 5 bytes (4 numeric digits plus NULL terminator)
* month 3 bytes (2 numeric digits plus NULL terminator)
* day 3 bytes (2 numeric digits plus NULL terminator)
* hours 3 bytes (2 numeric digits plus NULL terminator)
* minutes 3 bytes (2 numeric digits plus NULL terminator)
* seconds 3 bytes (2 numeric digits plus NULL terminator)
*
* return value:
* 0 = success
* -1 = Failure, see nwipe log for detail.
*/
FILE* fp;
int r; // A result buffer.
int idx; // general index
int strIdx; // Index into each string
int bufferIdx; // Index into the buffer
char buffer[5];
/**
* Basic validation that confirms the input strings are numeric and of the correct length, we do this
* by first constructing three arrays. The first are the names of the variables in order
* year, month, day, hours, minutes and seconds. The second array contains the address of
* each of those strings. The third array are the lengths.
* This allows us to create a single loop to validate all fields.
*/
char* names[] = { "year", "month", "day", "hours", "minutes", "seconds" };
char* pdata[] = { year, month, day, hours, minutes, seconds };
int lengths[] = { 4, 2, 2, 2, 2, 2 };
char cmd_format[] = "date %s%s%s%s%s.%s >/dev/null 2>&1";
char cmd[256];
for( idx = 0; idx < 6; idx++ )
{
strIdx = 0; // initialise string index
/* check each characters is numeric */
while( strIdx < lengths[idx] )
{
if( pdata[idx][strIdx] >= '0' && pdata[idx][strIdx] <= '9' )
{
strIdx++;
}
else
{
/* if we haven't reached the correct number of digits due to invalid data, log error,
* but first we read the valid data acquired so far into a buffer, this is done to avoid
* writing to the user provided string because if they did not size the string correctly
* writing a zero at the end could cause a segfault.
*/
for( bufferIdx = 0; bufferIdx < strIdx + 1; bufferIdx++ )
{
buffer[bufferIdx] = pdata[idx][bufferIdx];
}
buffer[bufferIdx] = 0; /* terminate the string, prior to using in nwipe_log */
/* A typical error will look like ..
* "User provided year data that appear invalid = 202£" */
nwipe_log( NWIPE_LOG_ERROR, "User provided %s data that appears invalid = %s", names[idx], buffer );
return -1;
}
}
}
/**
* Now using the validated strings construct the date command that we will use to write the system date/time
*/
sprintf( cmd, cmd_format, month, day, hours, minutes, year, seconds );
/**
* Run the date command to write the new date/time
*/
fp = popen( cmd, "w" );
r = pclose( fp );
if( fp == NULL || r != 0 )
{
nwipe_log( NWIPE_LOG_ERROR, "Failed to write system date/time using command = %s", cmd );
}
else
{
nwipe_log( NWIPE_LOG_INFO, "Date/time succesfully writen to system using command = %s", cmd );
}
return 0;
}
void fix_endian_model_names( char* model )
{
/* Some IDE USB adapters get the endian wrong, we can't determine the endian from the drive
* as the drive standard doesn't provide that information, so we have to resort to matching
* the model name against known strings with the endian incorrect, then reverse the endian.
*/
int idx = 0;
int idx2 = 0;
unsigned int length = 0;
char* tmp_string;
length = strlen( model );
tmp_string = calloc( length, 1 );
/* "ASSMNU G" = "SAMSUNG ", tested against model Samsung HM160HC so that
* "ASSMNU G MH61H0 C" becomes "SAMSUNG HM160HC ")
*/
if( !( strncmp( model, "ASSMNU G", 8 ) ) )
{
while( model[idx] != 0 )
{
tmp_string[idx2 + 1] = model[idx];
if( model[idx + 1] != 0 )
{
tmp_string[idx2] = model[idx + 1];
}
else
{
break;
}
if( tmp_string[idx2 + 1] == ' ' && model[idx + 2] == ' ' )
{
idx++;
}
idx += 2;
idx2 += 2;
}
tmp_string[idx2 + 1] = 0;
strcpy( model, tmp_string );
}
}
|