Advertisement
Guest User

Untitled

a guest
Sep 26th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. ;;;;;;; Fast Text-to-Unix Conversion (ftuc.asm) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;
  3. ;; Started: 21-Dec-2000
  4. ;; Updated: 22-Dec-2000
  5. ;;
  6. ;; Copyright 2000 G. Adam Stanislav.
  7. ;; All rights reserved.
  8. ;;
  9. ;;;;;;; v.1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  10. %include 'system.inc'
  11.  
  12. section .data
  13. db 'Copyright 2000 G. Adam Stanislav.', 0Ah
  14. db 'All rights reserved.', 0Ah
  15. usg db 'Usage: ftuc filename', 0Ah
  16. usglen equ usg
  17. co db "ftuc: Can't open file.", 0Ah
  18. colen equ co
  19. fae db 'ftuc: File access error.', 0Ah
  20. faelen equ fae
  21. ftl db 'ftuc: File too long, use regular tuc instead.', 0Ah
  22. ftllen equ ftl
  23. mae db 'ftuc: Memory allocation error.', 0Ah
  24. maelen equ mae
  25.  
  26. section .text
  27.  
  28. align 4
  29. memerr:
  30. push dword maelen
  31. push dword mae
  32. jmp short error
  33.  
  34. align 4
  35. toolong:
  36. push dword ftllen
  37. push dword ftl
  38. jmp short error
  39.  
  40. align 4
  41. facerr:
  42. push dword faelen
  43. push dword fae
  44. jmp short error
  45.  
  46. align 4
  47. cantopen:
  48. push dword colen
  49. push dword co
  50. jmp short error
  51.  
  52. align 4
  53. usage:
  54. push dword usglen
  55. push dword usg
  56.  
  57. error:
  58. push dword stderr
  59. sys.write
  60.  
  61. push dword 1
  62. sys.exit
  63.  
  64. align 4
  65. global _start
  66. _start:
  67. pop eax ; argc
  68. pop eax ; program name
  69. pop ecx ; file to convert
  70. jecxz usage
  71.  
  72. pop eax
  73. or eax, eax ; Too many arguments?
  74. jne usage
  75.  
  76. ; Open the file
  77. push dword O_RDWR
  78. push ecx
  79. sys.open
  80. jc cantopen
  81.  
  82. mov ebp, eax ; Save fd
  83.  
  84. sub esp, byte stat_size
  85. mov ebx, esp
  86.  
  87. ; Find file size
  88. push ebx
  89. push ebp ; fd
  90. sys.fstat
  91. jc facerr
  92.  
  93. mov edx, [ebx + st_size + 4]
  94.  
  95. ; File is too long if EDX != 0 ...
  96. or edx, edx
  97. jne near toolong
  98. mov ecx, [ebx + st_size]
  99. ; ... or if it is above 2 GB
  100. or ecx, ecx
  101. js near toolong
  102.  
  103. ; Do nothing if the file is 0 bytes in size
  104. jecxz .quit
  105.  
  106. ; Map the entire file in memory
  107. push edx
  108. push edx ; starting at offset 0
  109. push edx ; pad
  110. push ebp ; fd
  111. push dword MAP_SHARED
  112. push dword PROT_READ | PROT_WRITE
  113. push ecx ; entire file size
  114. push edx ; let system decide on the address
  115. sys.mmap
  116. jc near memerr
  117.  
  118. mov edi, eax
  119. mov esi, eax
  120. push ecx ; for SYS_munmap
  121. push edi
  122.  
  123. ; Use EBX for state machine
  124. mov ebx, ordinary
  125. mov ah, 0Ah
  126. cld
  127.  
  128. .loop:
  129. lodsb
  130. call ebx
  131. loop .loop
  132.  
  133. cmp ebx, ordinary
  134. je .filesize
  135.  
  136. ; Output final lf
  137. mov al, ah
  138. stosb
  139. inc edx
  140.  
  141. .filesize:
  142. ; truncate file to new size
  143. push dword 0 ; high dword
  144. push edx ; low dword
  145. push eax ; pad
  146. push ebp
  147. sys.ftruncate
  148.  
  149. ; close it (ebp still pushed)
  150. sys.close
  151.  
  152. add esp, byte 16
  153. sys.munmap
  154.  
  155. .quit:
  156. push dword 0
  157. sys.exit
  158.  
  159. align 4
  160. ordinary:
  161. cmp al, 0Dh
  162. je .cr
  163.  
  164. cmp al, ah
  165. je .lf
  166.  
  167. stosb
  168. inc edx
  169. ret
  170.  
  171. align 4
  172. .cr:
  173. mov ebx, cr
  174. ret
  175.  
  176. align 4
  177. .lf:
  178. mov ebx, lf
  179. ret
  180.  
  181. align 4
  182. cr:
  183. cmp al, 0Dh
  184. je .cr
  185.  
  186. cmp al, ah
  187. je .lf
  188.  
  189. xchg al, ah
  190. stosb
  191. inc edx
  192.  
  193. xchg al, ah
  194. ; fall through
  195.  
  196. .lf:
  197. stosb
  198. inc edx
  199. mov ebx, ordinary
  200. ret
  201.  
  202. align 4
  203. .cr:
  204. mov al, ah
  205. stosb
  206. inc edx
  207. ret
  208.  
  209. align 4
  210. lf:
  211. cmp al, ah
  212. je .lf
  213.  
  214. cmp al, 0Dh
  215. je .cr
  216.  
  217. xchg al, ah
  218. stosb
  219. inc edx
  220.  
  221. xchg al, ah
  222. stosb
  223. inc edx
  224. mov ebx, ordinary
  225. ret
  226.  
  227. align 4
  228. .cr:
  229. mov ebx, ordinary
  230. mov al, ah
  231. ; fall through
  232.  
  233. .lf:
  234. stosb
  235. inc edx
  236. ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement