Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;;;;;; Fast Text-to-Unix Conversion (ftuc.asm) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- ;;
- ;; Started: 21-Dec-2000
- ;; Updated: 22-Dec-2000
- ;;
- ;; Copyright 2000 G. Adam Stanislav.
- ;; All rights reserved.
- ;;
- ;;;;;;; v.1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
- %include 'system.inc'
- section .data
- db 'Copyright 2000 G. Adam Stanislav.', 0Ah
- db 'All rights reserved.', 0Ah
- usg db 'Usage: ftuc filename', 0Ah
- usglen equ usg
- co db "ftuc: Can't open file.", 0Ah
- colen equ co
- fae db 'ftuc: File access error.', 0Ah
- faelen equ fae
- ftl db 'ftuc: File too long, use regular tuc instead.', 0Ah
- ftllen equ ftl
- mae db 'ftuc: Memory allocation error.', 0Ah
- maelen equ mae
- section .text
- align 4
- memerr:
- push dword maelen
- push dword mae
- jmp short error
- align 4
- toolong:
- push dword ftllen
- push dword ftl
- jmp short error
- align 4
- facerr:
- push dword faelen
- push dword fae
- jmp short error
- align 4
- cantopen:
- push dword colen
- push dword co
- jmp short error
- align 4
- usage:
- push dword usglen
- push dword usg
- error:
- push dword stderr
- sys.write
- push dword 1
- sys.exit
- align 4
- global _start
- _start:
- pop eax ; argc
- pop eax ; program name
- pop ecx ; file to convert
- jecxz usage
- pop eax
- or eax, eax ; Too many arguments?
- jne usage
- ; Open the file
- push dword O_RDWR
- push ecx
- sys.open
- jc cantopen
- mov ebp, eax ; Save fd
- sub esp, byte stat_size
- mov ebx, esp
- ; Find file size
- push ebx
- push ebp ; fd
- sys.fstat
- jc facerr
- mov edx, [ebx + st_size + 4]
- ; File is too long if EDX != 0 ...
- or edx, edx
- jne near toolong
- mov ecx, [ebx + st_size]
- ; ... or if it is above 2 GB
- or ecx, ecx
- js near toolong
- ; Do nothing if the file is 0 bytes in size
- jecxz .quit
- ; Map the entire file in memory
- push edx
- push edx ; starting at offset 0
- push edx ; pad
- push ebp ; fd
- push dword MAP_SHARED
- push dword PROT_READ | PROT_WRITE
- push ecx ; entire file size
- push edx ; let system decide on the address
- sys.mmap
- jc near memerr
- mov edi, eax
- mov esi, eax
- push ecx ; for SYS_munmap
- push edi
- ; Use EBX for state machine
- mov ebx, ordinary
- mov ah, 0Ah
- cld
- .loop:
- lodsb
- call ebx
- loop .loop
- cmp ebx, ordinary
- je .filesize
- ; Output final lf
- mov al, ah
- stosb
- inc edx
- .filesize:
- ; truncate file to new size
- push dword 0 ; high dword
- push edx ; low dword
- push eax ; pad
- push ebp
- sys.ftruncate
- ; close it (ebp still pushed)
- sys.close
- add esp, byte 16
- sys.munmap
- .quit:
- push dword 0
- sys.exit
- align 4
- ordinary:
- cmp al, 0Dh
- je .cr
- cmp al, ah
- je .lf
- stosb
- inc edx
- ret
- align 4
- .cr:
- mov ebx, cr
- ret
- align 4
- .lf:
- mov ebx, lf
- ret
- align 4
- cr:
- cmp al, 0Dh
- je .cr
- cmp al, ah
- je .lf
- xchg al, ah
- stosb
- inc edx
- xchg al, ah
- ; fall through
- .lf:
- stosb
- inc edx
- mov ebx, ordinary
- ret
- align 4
- .cr:
- mov al, ah
- stosb
- inc edx
- ret
- align 4
- lf:
- cmp al, ah
- je .lf
- cmp al, 0Dh
- je .cr
- xchg al, ah
- stosb
- inc edx
- xchg al, ah
- stosb
- inc edx
- mov ebx, ordinary
- ret
- align 4
- .cr:
- mov ebx, ordinary
- mov al, ah
- ; fall through
- .lf:
- stosb
- inc edx
- ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement