-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileops.asm
321 lines (264 loc) · 10.6 KB
/
fileops.asm
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;; Description ;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;This file contains methods related to files to be downloaded
;Methods of this file mainly involves file creation operation.
;These methods are useful as part of pre-processing.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;; Procedure Area ;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Desc : Creating file space along with directory structure
;Input : pointer to torrent data structure, download location string
;Outcome : array of pieces filled with details
;ErrorCode : eax = 0 -> success (File space successfully created)
; eax = -1 -> error (Fail to create file space)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
proc torrent._.allocate_file_space _torrent, _downloadlocation
push ebx esi edi
mov ebx, [_torrent]
cmp [ebx+torrent.files_cnt], 1
je .single_file
lea esi, [ebx+torrent.name] ;stores directory name in case of multifile.
stdcall fileops._.prepare_abs_path, [_downloadlocation], esi, absolute_path
stdcall fileops._.create_folder, absolute_path
cmp eax, -1
jne .multi_files
DEBUGF 3, "ERROR : Can not create root folder\n"
jmp .error
.multi_files:
;change download location to newly created root folder path
lea esi,[absolute_path]
mov edi,[_downloadlocation]
@@: cmp byte [esi], 0x00
je @f
movsb
jmp @b
@@: mov byte[edi], 0x00
mov ecx, 0
.next_file:
cmp ecx, [ebx+torrent.files_cnt]
je .quit
mov esi,ecx
imul esi,0x1000
add esi,[ebx+torrent.files]
lodsd
mov [file_size], eax
lea edi, [name]
.loop: cmp byte[esi], '/'
je .process_dir
cmp byte[esi], 0x00
je .process_file
movsb
jmp .loop
.process_dir:
mov byte[edi], 0x00
stdcall fileops._.prepare_abs_path, [_downloadlocation], name, absolute_path
stdcall fileops._.create_folder, absolute_path
cmp eax, -1
jne @f
DEBUGF 3, "ERROR : Can not create root folder\n"
jmp .error
@@: mov byte[edi], '/'
inc edi
inc esi
jmp .loop
.process_file:
mov byte[edi], 0x00
stdcall fileops._.prepare_abs_path, [_downloadlocation], name, absolute_path
stdcall fileops._.create_file, absolute_path, [file_size]
;stdcall fileops._.create_file, absolute_path, 1024
cmp eax, -1
jne @f
DEBUGF 3, "ERROR : Can not create a file\n"
jmp .error
@@: mov edi, ecx
imul edi,0x1000
add edi, [ebx+torrent.files]
add edi, 4
mov esi, absolute_path
@@: cmp byte[esi], 0x00
je @f
movsb
jmp @b
@@: mov byte[edi],0x00
inc ecx
jmp .next_file
.single_file:
mov esi, [ebx+torrent.files]
lodsd
mov [file_size], eax
lea edi, [name]
@@: cmp byte[esi], 0x00
je @f
movsb
jmp @b
@@: mov byte[edi], 0x00
stdcall fileops._.prepare_abs_path, [_downloadlocation], name, absolute_path
stdcall fileops._.create_file, absolute_path, [file_size]
;stdcall fileops._.create_file, absolute_path, 1024
cmp eax, -1
jne .copy_file_name
DEBUGF 3, "ERROR : Can not create a file\n"
jmp .error
.copy_file_name:
mov edi, [ebx+torrent.files]
add edi, 4
mov esi, absolute_path
@@: cmp byte[esi], 0x00
je @f
movsb
jmp @b
@@: mov byte[edi],0x00
jmp .quit
.error: DEBUGF 3, "ERROR: Procedure ended with error.\n"
mov eax, -1
pop edi esi ebx
ret
.quit: DEBUGF 2, "INFO : Procedure ended successfully.\n"
mov eax, 0
pop edi esi ebx
ret
endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Desc : Concatenates absolute directrory path and file-name
;Input : absolute path of directory, filename and pointer to location for storing complete path
;Outcome : stores complete path at _path
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
proc fileops._.prepare_abs_path _dirname, _filename, _path
push esi edi
DEBUGF 2, "INFO: In fileops._.prepare_absolute_path\n"
mov edi, [_path]
mov esi, [_dirname]
@@: cmp byte [esi], 0x00
je @f
movsb
jmp @b
@@: dec esi
cmp byte [esi], '/'
je @f
mov byte [edi], '/'
inc edi
@@: mov esi, [_filename]
@@: cmp byte [esi], 0x00
je @f
movsb
jmp @b
@@: mov byte[edi], 0x00
.quit: DEBUGF 2, "INFO: Procedure ended successfully\n"
mov eax, 0
pop edi esi
ret
endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Desc : Creates an empty file of given size
;Input : absolute path of file, file size
;Outcome : Empty file of given size is created at given path
;ErrorCode : eax = 0 -> success (file is created)
; eax = -1 -> error (file is not created)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
proc fileops._.create_file _name, _size
DEBUGF 2, "INFO: In fileops._.create_file\n"
push ebx ecx
mov eax, [_name]
mov [fileinfo_create.name],eax
;creating a file
mov [fileinfo_create.subfunction], 2
mcall 70, fileinfo_create
cmp eax, 0
je @f
DEBUGF 2, "ERROR : Problem creating a file.\n"
jmp .error
;extending a file
@@: mov [fileinfo_create.subfunction], 4
mov eax, [_size]
mov [fileinfo_create.filesize_low], eax
mcall 70, fileinfo_create
cmp eax, 0
je .quit
DEBUGF 2, "ERROR : Problem extending a file.\n"
.error: DEBUGF 2, "ERROR: Procedure ended with error\n"
mov eax, -1
pop ecx ebx
ret
.quit: DEBUGF 2, "INFO: Procedure ended successfully\n"
mov eax, 0
pop ecx ebx
ret
endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Desc : Creates an empty folder
;Input : absolute path of folder
;Outcome : Folder of is created at given path
;ErrorCode : eax = 0 -> success (folder is created)
; eax = -1 -> error (folder is not created)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
proc fileops._.create_folder _name
DEBUGF 2, "INFO: In fileops._.create_folder\n"
push ebx ecx
mov eax, [_name]
mov [fileinfo_create.name],eax
mov [fileinfo_create.filesize_low],0
;creating a folder
mov [fileinfo_create.subfunction], 9
mcall 70, fileinfo_create
cmp eax, 0
je .quit
DEBUGF 2, "ERROR : Problem creating a folder\n", eax
.error: DEBUGF 2, "ERROR: Procedure ended with error\n"
mov eax, -1
pop ecx ebx
ret
.quit: DEBUGF 2, "INFO: Procedure ended successfully\n"
mov eax, 0
pop ecx ebx
ret
endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;Desc : Writes data to file
;Input : absolute path to file, size of data to be written, pointer to data
;ErrorCode : eax = 0 -> success (data is written to file)
; eax = -1 -> error (data is not written to file)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
proc fileops._.write_to_file _name, _size, _data
DEBUGF 2, "INFO: In fileops._.write_to_file\n"
push ebx ecx
mov eax, [_name]
mov [fileinfo_create.name],eax
;creating a file
mov [fileinfo_create.subfunction], 3
mov [fileinfo_create.filesize_low], 0
mov eax, [_size]
mov [fileinfo_create.reserved1], eax
mov eax, [_data]
mov [fileinfo_create.reserved2], eax
mov eax, [_name]
mov [fileinfo_create.name],eax
mcall 70, fileinfo_create
cmp eax, 0
je @f
DEBUGF 2, "ERROR : Problem writing to a file.\n"
jmp .error
.error: DEBUGF 2, "ERROR: Procedure ended with error\n"
mov eax, -1
pop ecx ebx
ret
.quit: DEBUGF 2, "INFO: Procedure ended successfully\n"
mov eax, 0
pop ecx ebx
ret
endp
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;; Data Area ;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
absolute_path rb 4096
file_size dd ?
name rb 4096
fileinfo_create:
.subfunction dd ?
.filesize_low dd ?
.filesize_high dd 0
.reserved1 dd 0
.reserved2 dd 0
db 0
.name dd ?