asm2html
001 %include 'system.inc'
002
003 section .data
004 db 'Copyright 2000 G. Adam Stanislav.',
0Ah
005 db 'All rights reserved.',
0Ah
006 usg
db 'Usage: ftuc filename',
0Ah
007 usglen
equ $-usg
008 co
db "ftuc: Can't open file.",
0Ah
009 colen
equ $-co
010 fae
db 'ftuc: File access error.',
0Ah
011 faelen
equ $-fae
012 ftl
db 'ftuc: File too long, use regular tuc instead.',
0Ah
013 ftllen
equ $-ftl
014 mae
db 'ftuc: Memory allocation error.',
0Ah
015 maelen
equ $-mae
016
017 section .text
018
019 align 4
020 memerr:
021 push dword maelen
022 push dword mae
023 jmp short error
024
025 align 4
026 toolong:
027 push dword ftllen
028 push dword ftl
029 jmp short error
030
031 align 4
032 facerr:
033 push dword faelen
034 push dword fae
035 jmp short error
036
037 align 4
038 cantopen:
039 push dword colen
040 push dword co
041 jmp short error
042
043 align 4
044 usage:
045 push dword usglen
046 push dword usg
047
048 error:
049 push dword stderr
050 sys.write
051
052 push dword 1
053 sys.exit
054
055 align 4
056 global _start
057 _start:
058 pop eax ; argc
059 pop eax ; program name
060 pop ecx ; file to convert
061 jecxz usage
062
063 pop eax
064 or eax,
eax ; Too many arguments?
065 jne usage
066
067 ; Open the file
068 push dword O_RDWR
069 push ecx
070 sys.open
071 jc cantopen
072
073 mov ebp,
eax ; Save fd
074
075 sub esp,
byte stat_size
076 mov ebx,
esp
077
078 ; Find file size
079 push ebx
080 push ebp ; fd
081 sys.fstat
082 jc facerr
083
084 mov edx, [
ebx + st_size +
4]
085
086 ; File is too long if EDX != 0 ...
087 or edx,
edx
088 jne near toolong
089 mov ecx, [
ebx + st_size]
090 ; ... or if it is above 2 GB
091 or ecx,
ecx
092 js near toolong
093
094 ; Do nothing if the file is 0 bytes in size
095 jecxz .quit
096
097 ; Map the entire file in memory
098 push edx
099 push edx ; starting at offset 0
100 push edx ; pad
101 push ebp ; fd
102 push dword MAP_SHARED
103 push dword PROT_READ | PROT_WRITE
104 push ecx ; entire file size
105 push edx ; let system decide on the address
106 sys.mmap
107 jc near memerr
108
109 mov edi,
eax
110 mov esi,
eax
111 push ecx ; for SYS_munmap
112 push edi
113
114 ; Use EBX for state machine
115 mov ebx, ordinary
116 mov ah,
0Ah
117 cld
118
119 .
loop:
120 lodsb
121 call ebx
122 loop .
loop
123
124 cmp ebx, ordinary
125 je .filesize
126
127 ; Output final lf
128 mov al,
ah
129 stosb
130 inc edx
131
132 .filesize:
133 ; truncate file to new size
134 push dword 0 ; high dword
135 push edx ; low dword
136 push eax ; pad
137 push ebp
138 sys.ftruncate
139
140 ; close it (ebp still pushed)
141 sys.close
142
143 add esp,
byte 16
144 sys.munmap
145
146 .quit:
147 push dword 0
148 sys.exit
149
150 align 4
151 ordinary:
152 cmp al,
0Dh
153 je .cr
154
155 cmp al,
ah
156 je .lf
157
158 stosb
159 inc edx
160 ret
161
162 align 4
163 .cr:
164 mov ebx, cr
165 ret
166
167 align 4
168 .lf:
169 mov ebx, lf
170 ret
171
172 align 4
173 cr:
174 cmp al,
0Dh
175 je .cr
176
177 cmp al,
ah
178 je .lf
179
180 xchg al,
ah
181 stosb
182 inc edx
183
184 xchg al,
ah
185 ; fall through
186
187 .lf:
188 stosb
189 inc edx
190 mov ebx, ordinary
191 ret
192
193 align 4
194 .cr:
195 mov al,
ah
196 stosb
197 inc edx
198 ret
199
200 align 4
201 lf:
202 cmp al,
ah
203 je .lf
204
205 cmp al,
0Dh
206 je .cr
207
208 xchg al,
ah
209 stosb
210 inc edx
211
212 xchg al,
ah
213 stosb
214 inc edx
215 mov ebx, ordinary
216 ret
217
218 align 4
219 .cr:
220 mov ebx, ordinary
221 mov al,
ah
222 ; fall through
223
224 .lf:
225 stosb
226 inc edx
227 ret
228 Generated with asm2html
asm2html