-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdir.asm
More file actions
257 lines (178 loc) · 11.1 KB
/
dir.asm
File metadata and controls
257 lines (178 loc) · 11.1 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
;//////////////////////////////////////////////////////////////////////////////////////////////////
;// [ DIR ] ; DIR(pFolder, cFolder) rsi = pFolder, rax = cFolder
;//////////////////////////////////////////////////////////////////////////////////////////////////
DIR: push rbp ; create the stack frame
mov rbp, rsp
;/////////////////////////////////////////////////////////////////
sub rsp, cSTACKVq
push r14
push r15
;/////////////////////////////////////////////////////////////////
mov qword [rbp - cInput], rax ; cInput
mov qword [rbp - pInput], rsi ; pInput
;/////////////////////////////////////////////////////////////////
; copy pInput -> Stack
;/////////////////////////////////////////////////////////////////
mov rcx, qword [rbp - cInput] ; cInput
lea rdi, [rbp - pSTRING]
xor rdx, rdx
xor r9, r9
DIR11: mov dl, byte [rsi + r9] ; rsi = pInput
mov byte [rdi + r9], dl
inc r9
loop DIR11
;/////////////////////////////////////////////////////////////////
; add slash ( /home -> /home/
;/////////////////////////////////////////////////////////////////
mov rax, qword [rbp - cInput]
lea rsi, [rbp - pSTRING]
cmp byte [rsi + rax - 1], 0x2F
je NOSLASH1
mov byte [rsi + rax], 0x2F
inc qword [rbp - cInput]
mov rax, qword [rbp - cInput]
NOSLASH1: mov byte [rsi + rax], 0x00
;/////////////////////////////////////////////////////////////////
; print FolderName to stdout
;/////////////////////////////////////////////////////////////////
write STR_OPEN, LEN_OPEN
write SPACE, 1
write rsi, rax
write NEW_LINE, 1
;/////////////////////////////////////////////////////////////////
; open Folder
;/////////////////////////////////////////////////////////////////
mov rdi, rsi ; filename
mov rsi, SYS_READ ; read mode
mov rdx, 0 ;
mov rax, SYS_OPEN ; open syscall
syscall
cmp rax, 0 ; check if fd in eax > 0 (ok)
jl DIR_ERR ; cannot open file. Exit with error status
mov qword [rbp - pFile], rax ; save file handle
;/////////////////////////////////////////////////////////////////
; LOOP1 until rax == 0x00 || rax == -1
;/////////////////////////////////////////////////////////////////
DENT64: mov rdi, qword [rbp - pFile] ; file handle
lea rsi, [rbp - pDIR64]
mov rdx, cDIR64 ; cBuffer
mov rax, SYS_DIR64 ; getdents64 syscall
syscall
cmp rax, 0 ; check if fd in eax > -1 (ok)
jl DIR_ERR ; cannot read folder. Exit with error status
cmp rax, 0 ; check if fd in eax > 0 (ok)
je FCLOSE
mov qword [rbp - cBuffer], rax ; save size
;/////////////////////////////////////////////////////////////////
; LOOP2 until qword [rbp - cBuffer] == 0x00
;/////////////////////////////////////////////////////////////////
lea r14, [rbp - pDIR64]
xor r15, r15
DIR1_LOOP: xor rax, rax
mov ax, word [r14 + r15 + 16] ; Size of this dirent
sub ax, 19 ; Sub Size of Struct without FileName
mov rdx, r15 ; Add Offset
add rdx, 19 ; Add Size of Struct without FileName
add rdx, r14
;/////////////////////////////////////////////////////////////////
; find string_terminator 0x00
;/////////////////////////////////////////////////////////////////
xor rcx, rcx
mov rcx, r15
TZERO: cmp byte [r14 + 19 + rcx], 0x00
je TZERO1
inc rcx
jmp TZERO
TZERO1: sub rcx, r15
mov qword [rbp - cFName], rcx ; save cFilename
mov qword [rbp - pFName], rdx ; save pFilename
;/////////////////////////////////////////////
; CHECK TYPE
;/////////////////////////////////////////////
cmp byte [r14 + r15 + 18], DT_DIR
jne NOFOLDER1
;/////////////////////////////////////////////////////////////////
; [ FOLDER ]
;/////////////////////////////////////////////////////////////////
cmp byte [r14 + r15 + 19], 0x2E ; Jump if No DOT string = "test", 0
jne DOTRDY
cmp byte [r14 + r15 + 20], 0x2E ; Jump if DOT string = "..", 0
je NEXT
cmp byte [r14 + r15 + 20], 0x00 ; Jump if ZERO string = ".", 0
je NEXT
DOTRDY:
;/////////////////////////////////////////////////////////////////
; copy pFName -> Stack(rbp - pSTRING + cInput)
;/////////////////////////////////////////////////////////////////
mov rax, qword [rbp - cInput] ; cInput
lea rdi, [rbp - pSTRING + rax] ; pLocalString[cInput]
mov rsi, qword [rbp - pFName]
xor rcx, rcx
xor rbx, rbx
COPYFNAME1: mov bl, byte [rsi + rcx]
mov byte [rdi + rcx], bl
inc rcx
cmp rcx, qword [rbp - cFName]
jne COPYFNAME1
;/////////////////////////////////////////////
inc qword [DCounter]
;/////////////////////////////////////////////
lea rsi, [rbp - pSTRING]
mov rax, qword [rbp - cInput] ; cInputString
add rax, qword [rbp - cFName] ; cFilename
call DIR ; Rekursion
;/////////////////////////////////////////////
jmp NEXT
NOFOLDER1: cmp byte [r14 + r15 + 18], DT_REG
jne NOFILE1
;/////////////////////////////////////////////////////////////////
; [ FILE ]
;/////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////
; copy pFName -> Stack(r10 + pSTRING + cInput)
;/////////////////////////////////////////////////////////////////
mov rax, qword [rbp - cInput] ; cInput
lea rdi, [rbp - pSTRING + rax] ; pLocalString[cInput]
xor rcx, rcx
xor rbx, rbx
mov rsi, qword [rbp - pFName]
COPYFNAME2: mov bl, byte [rsi + rcx]
mov byte [rdi + rcx], bl
inc rcx
cmp rcx, qword [rbp - cFName]
jne COPYFNAME2
;/////////////////////////////////////////////
inc qword [FCounter]
jmp NEXT
NOFILE1: ;/////////////////////////////////////////////
inc qword [ECounter]
;/////////////////////////////////////////////////////////////////
NEXT: xor rax, rax
mov ax, word [r14 + r15 + 16] ; Size of this dirent
add r15, rax
sub qword [rbp - cBuffer], rax
cmp qword [rbp - cBuffer], 0x00 ; check factor with 0
jne DIR1_LOOP ; loop again
;/////////////////////////////////////////////////////////////////
; LOOP2 END
;/////////////////////////////////////////////////////////////////
jmp DENT64
;/////////////////////////////////////////////////////////////////
; LOOP1 END
;/////////////////////////////////////////////////////////////////
;/////////////////////////////////////////////////////////////////
; Close File Handle
;/////////////////////////////////////////////////////////////////
FCLOSE: mov rdi, qword [rbp - pFile] ; file handle
mov rax, SYS_CLOSE ; close syscall
syscall
jmp DIR_READY
;/////////////////////////////////////////////////////////////////
DIR_ERR: write STR_ERROR, LEN_ERROR
;/////////////////////////////////////////////////////////////////
DIR_READY:
pop r15
pop r14
mov rsp, rbp ; destroy the stack frame
pop rbp
ret