-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFILE.C
More file actions
265 lines (238 loc) · 7.78 KB
/
Copy pathFILE.C
File metadata and controls
265 lines (238 loc) · 7.78 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
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
/***************************************************************************
* FILE.C -- This file is part of diskdump. *
* *
* Copyright (C) 2021 Imanol-Mikel Barba Sabariego *
* *
* diskdump 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, either version 3 of the License, *
* or (at your option) any later version. *
* *
* diskdump 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, see http://www.gnu.org/licenses/. *
* *
***************************************************************************/
#include "file.h"
const char ext_sequence[MAX_EXT_SEQUENCE] = {
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5',
'6','7','8','9','!','#','$','%','&','\'','(',')','-','@','^','_',
'`','{','}','~'
};
// Why tf am I dealing with DOS file handle routines instead of
// using stdio FILEs? Well because those don't take far pointers :_D
uint16_t file_tell(uint16_t handle, ulongint* file_offset) {
uint16_t status = 0;
_asm {
MOV ah, 42h
MOV al, 1
MOV bx, handle
XOR cx, cx
XOR dx, dx
INT 21h
JNC noerror
LEA si, status
MOV WORD PTR [si], ax
noerror:
MOV si, file_offset
MOV WORD PTR [si], ax
MOV WORD PTR [si+2], dx
}
return status;
}
uint16_t file_close(uint16_t handle) {
uint16_t status = 0;
_asm {
MOV ah, 3Eh
MOV bx, handle
INT 21h
JNC noerror
LEA si, status
MOV WORD PTR [si], ax
noerror:
}
return status;
}
uint16_t file_creat(const char* path) {
uint16_t handle = 0;
uint16_t error_code = 0;
uint16_t path_seg = FP_SEG(path);
uint16_t path_off = FP_OFF(path);
_asm {
MOV ah, 3Ch
XOR cx, cx
PUSH ds
MOV ds, path_seg
MOV dx, path_off
INT 21h
POP ds
JC error
LEA si, handle
MOV WORD PTR [si], ax
JMP end
error:
LEA si, error_code
MOV WORD PTR [si], ax
end:
}
if(error_code) {
printf("Error opening file: status = %d\n", handle);
}
return handle;
}
uint16_t file_write(uint16_t handle, uint16_t num_bytes, uint8_t far *buf, uint16_t* num_written) {
uint16_t status = 0;
uint16_t buf_segment = FP_SEG(buf);
uint16_t buf_offset = FP_OFF(buf);
_asm {
MOV ah, 40h
MOV bx, handle
MOV cx, num_bytes
PUSH ds
MOV ds, buf_segment
MOV dx, buf_offset
INT 21h
POP ds
JC error
MOV si, num_written
MOV WORD PTR [si], ax
JMP end
error:
LEA si, status
MOV WORD PTR [si], ax
end:
}
return status;
}
uint8_t determine_drive_number(const char* path) {
// Determine drive number from path
// This is NOT BIOS drive number, but MSDOS drive number
if(path[1] != ':') {
return 0;
}
return (path[0] - 'A' + 1);
}
void increment_extension(file_medium_data* fmd) {
uint8_t carry = 1;
int i;
for(i = 0; i < 3; ++i) {
fmd->current_extension_indexes[i] = (fmd->current_extension_indexes[i] + carry) % MAX_EXT_SEQUENCE;
if(!(fmd->current_extension_indexes[i] == 0 && carry)) {
carry = 0;
}
}
}
int check_free_space(uint8_t drive_number, ulongint space_needed) {
uint16_t sectors_per_cluster;
uint16_t available_clusters;
uint16_t bytes_per_sector;
ulongint free_space = 0;
_asm {
MOV dl, drive_number
MOV ah, 36h
INT 21h
LEA si, sectors_per_cluster
MOV WORD PTR [si], ax
LEA si, available_clusters
MOV WORD PTR [si], bx
LEA si, bytes_per_sector
MOV WORD PTR [si], cx
}
free_space = (ulongint)available_clusters * (ulongint)sectors_per_cluster * (ulongint)bytes_per_sector;
return free_space > space_needed;
}
ssize_t file_medium_send(uint8_t far *buf, ulongint buf_len, medium_data md) {
uint16_t status;
ulongint remaining_bytes = buf_len;
ulongint current_pos;
uint16_t bytes_to_write;
uint16_t bytes_written;
file_medium_data* fmd = (file_medium_data*)md;
char path[MAX_PATH_LENGTH + 1];
char filename[DUMP_FILE_NAME_LENGTH + 1];
while(remaining_bytes) {
if(fmd->fd == 0) {
if(!check_free_space(determine_drive_number(fmd->target_directory), fmd->file_size)) {
printf("Insufficient disk space for another file\n");
return -1;
}
sprintf(filename, "dump.%c%c%c", ext_sequence[fmd->current_extension_indexes[2]], ext_sequence[fmd->current_extension_indexes[1]], ext_sequence[fmd->current_extension_indexes[0]]);
sprintf(path, "%s\\%s", fmd->target_directory, filename);
fmd->fd = file_creat(path);
if(!fmd->fd) {
printf("Error opening new file\n");
return -1;
}
}
status = file_tell(fmd->fd, ¤t_pos);
if(status) {
printf("Error getting current file offset\n");
status = file_close(fmd->fd);
if(status) {
printf("Error closing file. Oh well :(\n");
}
return -1;
}
bytes_to_write = min(remaining_bytes, MAX_BYTES_FILE);
bytes_to_write = min(bytes_to_write, fmd->file_size - current_pos);
status = file_write(fmd->fd, bytes_to_write, buf + (buf_len - remaining_bytes), &bytes_written);
if(status) {
printf("Error writing to file\n");
status = file_close(fmd->fd);
if(status) {
printf("Error closing file. Oh well :(\n");
}
return -1;
}
if(bytes_written + current_pos == fmd->file_size) {
status = file_close(fmd->fd);
if(status) {
printf("Error closing file\n");
}
fmd->fd = 0;
increment_extension(fmd);
}
remaining_bytes -= bytes_written;
}
return buf_len;
}
int file_medium_ready(medium_data md) {
md = md;
return MEDIUM_READY;
}
void file_medium_done(medium_data md, char* hash) {
md = md;
hash = hash;
}
int create_file_medium(const char* target_directory, ulongint file_size, Medium* m, file_medium_data* fmd, Digest* digest) {
uint16_t file_handle;
char path[MAX_PATH_LENGTH + 1];
// Test that the directory is writeable
if(strlen(target_directory) > (MAX_PATH_LENGTH + 1 - DUMP_FILE_NAME_LENGTH)) {
printf("Path is too long\n");
return -1;
}
sprintf(path, "%s\\DUMP.AAA", target_directory);
file_handle = file_creat(path);
if(file_handle == 0) {
printf("Unable to create a file in the specified path: %s. Check if the path exists and the media is not write protected\n", target_directory);
return -1;
}
file_close(file_handle);
fmd->fd = 0;
memset(fmd->current_extension_indexes, 0x00, sizeof(fmd->current_extension_indexes));
fmd->target_directory = target_directory;
fmd->file_size = file_size;
m->send = &file_medium_send;
m->ready = &file_medium_ready;
m->data = (void*)fmd;
m->done = &file_medium_done;
m->digest = digest;
m->mtu = MAX_BYTES_FILE;
return 0;
}