-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrwfile.c
More file actions
148 lines (114 loc) · 2.92 KB
/
Copy pathrwfile.c
File metadata and controls
148 lines (114 loc) · 2.92 KB
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
/* ====================================================================
* Copyright (c) 2002 Johnny Shelley. All rights reserved.
*
* Bcrypt is licensed under the BSD software license. See the file
* called 'LICENSE' that you should have received with this software
* for details
* ====================================================================
*/
#include "includes.h"
#include "defines.h"
#include "functions.h"
int getremain(uLong sz, int dv) {
int r;
r = sz / dv;
r++;
r = r * dv;
r = r - sz;
return(r);
}
uLong padInput(char **input, uLong sz) {
int r, j;
j = sizeof(uInt32) * 2;
if (sz >= j)
r = getremain(sz, j);
else
r = j - sz;
if ( r < j) {
if ((*input = realloc(*input, sz + r + 1)) == NULL)
memerror();
memset(*input+sz, 0, r + 1);
sz+=r;
}
return(sz);
}
uLong attachKey(char **input, char *key, uLong sz) {
/* +3 so we have room for info tags at the beginning of the file */
if ((*input = realloc(*input, sz + MAXKEYBYTES + 3)) == NULL)
memerror();
memcpy(*input+sz, key, MAXKEYBYTES);
sz += MAXKEYBYTES;
return(sz);
}
uLong readfile(char *infile, char **input, int type, char *key,
struct stat statbuf) {
FILE *fd;
int readsize;
uLong sz = 0;
readsize = statbuf.st_size + 1;
fd = fopen(infile, "rb");
if (!fd) {
fprintf(stderr, "Unable to open file %s\n", infile);
return(-1);
}
if ((*input = malloc(readsize + sz + 1)) == NULL)
memerror();
memset(*input+sz, 0, readsize);
sz += fread(*input+sz, 1, readsize - 1, fd);
fclose(fd);
return(sz);
}
uLong writefile(char *outfile, char *output, uLong sz,
BCoptions options, struct stat statbuf) {
FILE *fd;
if (options.standardout == 1)
fd = stdout;
else
fd = fopen(outfile, "wb");
if (!fd) {
fprintf(stderr, "Unable to create file %s\n", outfile);
exit(1);
}
if (fwrite(output, 1, sz, fd) != sz) {
fprintf(stderr, "Out of space while writing file %s\n", outfile);
exit(1);
}
if (!options.standardout) {
fclose(fd);
chmod(outfile, statbuf.st_mode);
}
return(0);
}
int deletefile(char *file, BCoptions options, char *key, struct stat statbuf) {
int lsize;
long g;
uLong j = 0, k = 0;
signed char i;
char *state, *garbage;
FILE *fd;
if (options.securedelete > 0) {
lsize = sizeof(long);
k = (statbuf.st_size / lsize) + 1;
if ((state = malloc(257)) == NULL)
memerror();
initstate((unsigned long) key, state, 256);
if ((garbage = malloc(lsize + 1)) == NULL)
memerror();
fd = fopen(file, "r+b");
for (i = options.securedelete; i > 0; i--) {
fseek(fd, 0, SEEK_SET);
for (j = 0; j < k; j += lsize) {
g = random();
memcpy(garbage, &g, lsize);
fwrite(garbage, lsize, 1, fd);
}
fflush(fd);
}
fclose(fd);
}
if (unlink(file)) {
fprintf(stderr, "Error deleting file %s\n", file);
return(1);
}
return(0);
}