Archived
1
0

Init project

This commit is contained in:
Sweet Bread 2022-11-06 13:51:51 +03:00
commit c163efd83d
32 changed files with 1110 additions and 0 deletions

44
README.md Normal file
View File

@ -0,0 +1,44 @@
This project for me just for fun to not spending time for routine operations
1. std
- print
- str
- int
- [ ] float
- [ ] hex
- char
- bin
- nl
- exit
2. file
- open
- close
- content
- [ ] size
- [ ] next_line
3. path
- filename
- [ ] join
- [ ] is_dir
- [ ] is_file
4. string
- copy
- len
- [ ] split
- [ ] replace
- [ ] find
- [ ] parse_int
- [ ] parse_float
5. network
- get_ip
- IP_to_str
6. dns
- send
- get_field
7. [dnstoys](https://www.dns.toys/)
- myip
- [ ] weather and other
8. [ ] http
9. [ ] json
<sup>A lot of shit code included</sup>

BIN
dns.o Normal file

Binary file not shown.

BIN
dnstoys.o Normal file

Binary file not shown.

Binary file not shown.

BIN
examples/HWA/main Normal file

Binary file not shown.

33
examples/HWA/main.asm Normal file
View File

@ -0,0 +1,33 @@
format ELF
public _start
extrn print.int
extrn print.bin
extrn print.str
extrn print.nl
extrn exit
macro nl {
call print.nl
}
section '.strtab'
str1 db "Hello world!", 0
section '.text' executable
_start:
push dword 123
call print.int
nl
push dword 123
call print.bin
nl
push str1
call print.str
nl
call exit

BIN
examples/HWA/main.o Normal file

Binary file not shown.

11
examples/HWA/makefile Normal file
View File

@ -0,0 +1,11 @@
all: main
main: ../../std.o
fasm main.asm
ld -m elf_i386 main.o ../../std.o --output main
../../std.o:
make -C ../../ std.o
clear:
rm main.o main

BIN
examples/file/main Normal file

Binary file not shown.

39
examples/file/main.asm Normal file
View File

@ -0,0 +1,39 @@
format ELF
public _start
extrn file.open
extrn file.content
extrn file.close
extrn print.str
extrn print.nl
extrn exit
section '.strtab'
filename db "/home/sweetbread/headers", 0
section '.bss' writeable
file_ rd 1
section '.text' executable
_start:
push filename
call file.open
pop eax
mov [file_], eax
push dword [file_]
call print.str
call print.nl
call print.nl
push dword [file_]
call file.content
call print.str
push dword [file_]
call file.close
call exit

BIN
examples/file/main.o Normal file

Binary file not shown.

24
examples/file/makefile Normal file
View File

@ -0,0 +1,24 @@
all: main
main: ../../file.o ../../std.o ../../path.o ../../string.o
fasm main.asm
ld -m elf_i386 main.o ../../file.o ../../std.o ../../path.o ../../string.o --output main
# ../../string.o:
# make -C ../.. string.o
../../std.o:
make -C ../.. std.o
../../file.o:
make -C ../.. file.o
../../path.o:
make -C ../.. path.o
../../string.o:
make -C ../.. string.o
clear:
rm main.o main

BIN
examples/network/main Normal file

Binary file not shown.

28
examples/network/main.asm Normal file
View File

@ -0,0 +1,28 @@
format ELF
public _start
extrn network.IP_to_str
extrn network.get_ip
extrn dnstoys.myip
extrn print.str
extrn print.nl
extrn exit
section '.strtab'
hostname db "google.com", 0
section '.text' executable
_start:
call dnstoys.myip
call print.str
call print.nl
push hostname
call network.get_ip
call network.IP_to_str
call print.str
call print.nl
call exit

BIN
examples/network/main.o Normal file

Binary file not shown.

24
examples/network/makefile Normal file
View File

@ -0,0 +1,24 @@
all: main
main: ../../std.o ../../string.o ../../network.o ../../dns.o ../../dnstoys.o
fasm main.asm
ld -m elf_i386 main.o ../../std.o ../../string.o ../../network.o ../../dns.o ../../dnstoys.o --output main
../../std.o:
make -C ../.. std.o
../../string.o:
make -C ../.. string.o
../../network.o:
make -C ../.. network.o
../../dns.o:
make -C ../.. dns.o
../../dnstoys.o:
make -C ../.. dnstoys.o
clear:
rm main.o main

BIN
file.o Normal file

Binary file not shown.

47
include/_dns.inc Normal file
View File

@ -0,0 +1,47 @@
AF_INET equ 2
SOCK_STREAM equ 80001h
SOCK_DGRAM equ 2
IPPROTO_TCP equ 6
IPPROTO_UDP equ 17
IPPROTO_IP equ 0
SOL_TCP equ 6
SOL_UDP equ 17
TCP_NODELAY equ 1
FIODIO equ 4
flags equ 0
TYPE_A equ 1
TYPE_NS equ 2
MD equ 3
MF equ 4
CNAME equ 5
SOA equ 6
MB equ 7
MG equ 8
MR equ 9
NULL equ 10
WKS equ 11
PTR equ 12
HINFO equ 13
MINFO equ 14
MX equ 15
TXT equ 16
opt_val equ 1
opt_len equ 4
struc sockaddr port0, port1, ip0, ip1, ip2, ip3{
.sa_family dw AF_INET
.sa_data db port0, port1, ip0, ip1, ip2, ip3
}
struc dnsreq {
DNS_header:
ID rb 2
params db 01h, 00h
QDCOUNT db 00h, 01h
ANCOUNT rb 2
NSCOUNT rb 2
ARCOUNT rb 2
DNS_question rb 256
}

46
include/_file.inc Normal file
View File

@ -0,0 +1,46 @@
O_RDONLY equ 0
O_WRONLY equ 1
O_RDWR equ 2
PROT_READ equ 1 ; 001
PROT_WRITE equ 2 ; 010
PROT_EXEC equ 4 ; 100
MAP_PRIVATE equ 02h
MAP_ANONYMOUS equ 20h
O_DIRECTORY equ 0200000
struc stat {
.dev_t rd 1; /* ID of device containing file */
.ino_t rd 1; /* Inode number */
.mode_t rw 1; /* File type and mode */
.nlink_t rw 1; /* Number of hard links */
.uid_t rw 1; /* User ID of owner */
.gid_t rw 1; /* Group ID of owner */
.s_dev_t rd 1; /* Device ID (if special file) */
.off_t rd 1; /* Total size, in bytes */
.blksize_t rd 1; /* Block size for filesystem I/O */
.blkcnt_t rd 1; /* Number of 512B blocks allocated */
; /* Since Linux 2.6, the kernel supports nanosecond
; precision for the following timestamp fields.
; For the details before Linux 2.6, see NOTES. */
; struct timespec st_atim; /* Time of last access */
; struct timespec st_mtim; /* Time of last modification */
; struct timespec st_ctim; /* Time of last status change */
; st_atime equ st_atim.tv_sec /* Backward compatibility */
; st_mtime equ st_mtim.tv_sec
; st_ctime equ st_ctim.tv_sec
}
struc sizes {
.str1 rb 1
.filename rb 1
; .str2 db 14
.full_filename rb 1
.fd db 4
.all_size db 4
}

9
include/_macros.inc Normal file
View File

@ -0,0 +1,9 @@
macro prelude {
push ebp
mov ebp, esp
push eax ebx ecx edx esi edi
}
macro postlude {
pop edi esi edx ecx ebx eax ebp
}

25
makefile Normal file
View File

@ -0,0 +1,25 @@
all: std.o string.o file.o path.o network.o dns.o dnstoys.o
std.o:
fasm src/std.asm std.o
string.o:
fasm src/string.asm string.o
file.o:
fasm src/file.asm file.o
path.o:
fasm src/path.asm path.o
network.o:
fasm src/network.asm network.o
dns.o:
fasm src/dns.asm dns.o
dnstoys.o:
fasm src/dnstoys.asm dnstoys.o
clear:
rm *.o

BIN
network.o Normal file

Binary file not shown.

BIN
path.o Normal file

Binary file not shown.

191
src/dns.asm Normal file
View File

@ -0,0 +1,191 @@
format ELF
public send as 'dns.send'
public get_field as 'dns.get_field'
include '../include/_macros.inc'
include '../include/_dns.inc'
extrn string.len
section '.strtab'
random_filename db "/dev/random", 0
section '.bss' writeable
sock_fd rd 1
request_len rw 1
DNS_request dnsreq
DNS_server sockaddr 00,53, 8,8,8,8
section '.dns.text' executable
send:
prelude
; open
mov eax, 5
mov ebx, random_filename
mov ecx, 0
mov edx, 0
int 80h
push eax
; read
mov ebx, eax
mov eax, 3
mov ecx, ID
mov edx, 2
int 80h
; close
mov eax, 6
pop ebx
int 80h
mov eax, [ebp+2*4]
mov ecx, DNS_question
.bigloop:
xor ebx, ebx
.small0loop:
cmp [eax+ebx], byte '.'
je .small0break
cmp [eax+ebx], byte 0
je .small0break
inc ebx
jmp .small0loop
.small0break:
mov [ecx], ebx
inc ecx
xor ebx, ebx
.small1loop:
cmp [eax], byte '.'
je .small1break
cmp [eax], byte 0
je .bigbreak
mov bl, [eax]
mov [ecx], bl
inc ecx
inc eax
jmp .small1loop
.small1break:
inc eax
jmp .bigloop
.bigbreak:
mov [ecx], byte 0
mov [ecx+1], dword 01000100h
add ecx, 5
sub ecx, DNS_request
mov [request_len], cx
; socket
mov eax, 167h
mov ebx, AF_INET
mov ecx, SOCK_DGRAM
mov edx, IPPROTO_IP
int 80h
mov [sock_fd], eax
; sendto
mov eax, 369
mov ebx, [sock_fd]
mov ecx, DNS_request
xor edx, edx
mov dx, [request_len]
mov esi, flags
mov edi, [ebp+3*4]
push ebp
mov ebp, 16
int 80h
pop ebp
; read
mov eax, 3
mov ebx, [sock_fd]
mov ecx, [ebp+4*4]
mov edx, [ebp+5*4]
int 80h
; close
mov eax, 6
mov ebx, [sock_fd]
int 80h
mov eax, [ebp+4]
mov [ebp+5*4], eax
postlude
add esp, 4*4
ret
get_field:
prelude
mov eax, [ebp+3*4]
mov bx , [ebp+2*4]
mov ch , [eax+2*2]
mov cl , [eax+2*2+1]
cmp bx, cx
jl @f
mov [ebp+3*4], dword 0
mov eax, [ebp+4]
mov [ebp+2*4], eax
postlude
add esp, 4
ret
@@:
add eax, 3*4
mov bh , [eax-8]
mov bl , [eax-7]
@@:
dec bx
push eax
call string.len
pop ecx
add eax, ecx
cmp ecx, 2
je .a
inc eax
.a:
add eax, 2*2
cmp bx , 0
je @f
jmp @b
@@:
cmp word [ebp+2*4], 0
je @f
push eax
call string.len
pop ecx
add eax, ecx
cmp ecx, 2
je .b
inc eax
.b:
add eax, 2*2+4
xor ebx, ebx
mov bx , [eax]
add eax, ebx
inc word [ebp+2*4]
jmp @b
@@:
mov [ebp+3*4], eax
mov eax, [ebp+4]
mov [ebp+2*4], eax
postlude
add esp, 4
ret

57
src/dnstoys.asm Normal file
View File

@ -0,0 +1,57 @@
format ELF
public myip as 'dnstoys.myip'
include '../include/_macros.inc'
include '../include/_dns.inc'
extrn network.get_ip
extrn dns.send
extrn dns.get_field
extrn string.len
section '.strtab'
dnstoys db "dns.toys", 0
endpoints:
.ip db "ip", 0
section '.bss' writeable
response rb 200
DNS_server sockaddr 00,53, ?,?,?,?
DNS_request dnsreq
section '.dnstoys.text'
myip:
push 0
prelude
push dnstoys
call network.get_ip
pop eax
mov eax, [eax]
mov [DNS_server+4], eax
push dword 200
push response
push DNS_server
push endpoints.ip
call dns.send
push response
push dword 0
call dns.get_field
pop eax
push eax
call string.len
pop ebx
add eax, ebx
cmp ebx, 2
je @f
inc eax
@@:
add eax, 2*3+4+1
mov ebx, [ebp+2*4]
mov [ebp+4], ebx
mov [ebp+2*4], eax
postlude
ret

194
src/file.asm Normal file
View File

@ -0,0 +1,194 @@
format ELF
public open as 'file.open'
public close as 'file.close'
public content as 'file.content'
include '../include/_file.inc'
include '../include/_macros.inc'
extrn string.len
extrn string.copy
extrn path.filename
section '.strtab'
str1 db "File object ", 0
; str2 db " at "
section '.bss' writeable
pointer rd 1
fd rd 1
memory rd 1
sizes_ sizes
file_stat stat
section '.file.text' executable
open:
prelude
; open
mov eax, 5
mov ebx, [ebp+2*4]
mov ecx, O_RDONLY
mov edx, O_DIRECTORY
int 80h
mov [fd], eax
; fstat
mov eax, 108
mov ebx, [fd]
mov ecx, file_stat
int 80h
push str1
call string.len
pop eax
mov [sizes_.str1], al
push dword [ebp+2*4]
call string.len
pop eax
mov [sizes_.full_filename], al
push dword [ebp+2*4]
call path.filename
call string.len
pop ebx
mov [sizes_.filename], bl
mov eax, [file_stat.off_t]
add al , [sizes_.str1]
add al , [sizes_.full_filename]
add al , [sizes_.filename]
add al , [sizes_.fd]
add al , [sizes_.all_size]
mov [memory], eax
push eax
; mmap2
mov eax, 192
xor ebx, ebx
pop ecx
mov edx, PROT_READ
or edx, PROT_WRITE
mov esi, MAP_PRIVATE
or esi, MAP_ANONYMOUS
mov edi, -1
push ebp
xor ebp, ebp
int 80h
pop ebp
mov [pointer], eax
push str1
push eax
call string.copy
xor ebx, ebx
mov bl , [sizes_.str1]
add eax, ebx
mov ebx, [ebp+2*4]
push ebx
call path.filename
push eax
call string.copy
xor ebx, ebx
mov bl , [sizes_.filename]
add eax, ebx
inc eax
push dword [ebp+2*4]
push eax
call string.copy
xor ebx, ebx
mov bl , [sizes_.full_filename]
add eax, ebx
inc eax
mov ebx, [fd]
mov [eax], ebx
xor ebx, ebx
mov bl , [sizes_.fd]
add eax, ebx
mov ebx, [memory]
mov [eax], ebx
xor ebx, ebx
mov bl , [sizes_.all_size]
add eax, ebx
inc eax
push eax
; read
mov eax, 3
mov ebx, [fd]
pop ecx
mov edx, [file_stat.off_t]
int 80h
mov eax, [pointer]
mov [ebp+2*4], eax
postlude
ret
close:
prelude
mov eax, [ebp+2*4]
push eax
call string.len
pop ebx
add eax, ebx
inc eax
push eax
call string.len
pop ebx
add eax, ebx
inc eax
push eax
; close
mov ebx, [eax]
mov eax, 6
int 80h
; munmap
mov eax, 91
mov ebx, [ebp+2*4]
pop ecx
add ecx, 4
mov ecx, [ecx]
int 80h
mov eax, [ebp+4]
mov [ebp+2*4], eax
postlude
add esp, 4
ret
content:
prelude
mov eax, [ebp+2*4]
push eax
call string.len
pop ebx
add eax, ebx
inc eax
push eax
call string.len
pop ebx
add eax, ebx
add eax, 1+4+4+1
mov [ebp+2*4], eax
postlude
ret

90
src/network.asm Normal file
View File

@ -0,0 +1,90 @@
format ELF
public get_ip as 'network.get_ip'
public IP_to_str as 'network.IP_to_str'
include '../include/_macros.inc'
include '../include/_dns.inc'
extrn dns.send
extrn dns.get_field
extrn string.len
section '.bss' writeable
IP_str rb 19
DNS_server sockaddr 00,53, 8,8,8,8
response rb 200
section '.network.text' executable
get_ip:
prelude
push dword 200
push response
push DNS_server
push dword [ebp+2*4]
call dns.send
push response
push dword 0
call dns.get_field
pop eax
push eax
call string.len
pop ebx
add eax, ebx
cmp ebx, 2
je @f
inc eax
@@:
add eax, 2*3+4
mov [ebp+2*4], eax
postlude
ret
IP_to_str:
prelude
mov ecx, [ebp+2*4]
xor ebx, ebx
mov esi, IP_str
.loop:
mov al, [ecx+ebx]
push 10
.smloop:
cmp ax, 10
jnl @f
push eax
jmp .smbr
@@:
mov edx, 10
div dl
mov dl, ah
push edx
xor ah, ah
jmp .smloop
.smbr:
pop eax
cmp al, byte 10
je .smbrbr
add al, '0'
mov [esi], al
inc esi
jmp .smbr
.smbrbr:
inc ebx
cmp ebx, 4
je .break
mov [esi], byte '.'
inc esi
jmp .loop
.break:
mov [esi], byte 0
mov [ebp+2*4], dword IP_str
postlude
ret

29
src/path.asm Normal file
View File

@ -0,0 +1,29 @@
format ELF
public filename as 'path.filename'
include '../include/_macros.inc'
extrn string.len
section '.text' executable
filename:
prelude
mov eax, [ebp+2*4]
push eax
call string.len
pop ebx
.loop:
cmp [ebx+eax], byte '/'
je .break
cmp ebx, 0
je .break
dec ebx
jmp .loop
.break:
add eax, ebx
inc eax
mov [ebp+2*4], eax
postlude
ret

173
src/std.asm Normal file
View File

@ -0,0 +1,173 @@
format ELF
public char as 'print.char'
public _str as 'print.str'
public nl as 'print.nl'
public _bin as 'print.bin'
public _int as 'print.int'
public exit
include '../include/_macros.inc'
_new_line equ 10
section '.bss' writeable
buffer rb 32
section '.std.text' executable
char:
prelude
mov cl, [ebp+2*4]
mov [buffer], cl
mov eax, 4
mov ebx, 1
mov ecx, buffer
mov edx, 1
int 80h
mov [buffer], byte 0
mov eax, [ebp+4]
mov [ebp+2*4], eax
postlude
add esp, 4
ret
_str:
prelude
mov ecx, [ebp+2*4]
xor edx, edx
@@:
cmp [ecx+edx], byte 0
je @f
inc edx
jmp @b
@@:
mov eax, 4
mov ebx, 1
int 80h
mov eax, [ebp+4]
mov [ebp+2*4], eax
postlude
add esp, 4
ret
nl:
push _new_line
call char
ret
_bin:
prelude
mov eax, [ebp+2*4]
push 2
mov ebx, 2
@@:
cmp eax, 2
jl @f
div ebx
push edx
xor edx, edx
jmp @b
@@:
push eax
mov ebx, buffer
@@:
pop eax
cmp al, 2
je @f
add al, '0'
mov [ebx], al
inc ebx
jmp @b
@@:
push buffer
call _str
mov eax, buffer
@@:
cmp eax, ebx
je @f
mov [ebx-1], byte 0
dec ebx
jmp @b
@@:
mov eax, [ebp+4]
mov [ebp+2*4], eax
postlude
add esp, 4
ret
_int:
prelude
mov eax, [ebp+2*4]
push 10
mov ebx, 10
@@:
cmp eax, 10
jl @f
div ebx
push edx
xor edx, edx
jmp @b
@@:
push eax
mov ebx, buffer
@@:
pop eax
cmp al, 10
je @f
add al, '0'
mov [ebx], al
inc ebx
jmp @b
@@:
mov [ebx], byte 0
push buffer
call _str
mov eax, buffer
@@:
cmp eax, ebx
je @f
mov [ebx-1], byte 0
dec ebx
jmp @b
@@:
mov eax, [ebp+4]
mov [ebp+2*4], eax
postlude
add esp, 4
ret
exit:
mov eax, 1
xor ebx, ebx
int 80h

46
src/string.asm Normal file
View File

@ -0,0 +1,46 @@
format ELF
public copy as 'string.copy'
public len as 'string.len'
include "../include/_macros.inc"
section '.string.text' executable
copy:
prelude
mov eax, [ebp+3*4]
mov ebx, [ebp+2*4]
xor ecx, ecx
.loop:
cmp [eax+ecx], byte 0
je .break
mov dl, [eax+ecx]
mov [ebx+ecx], dl
inc ecx
jmp .loop
.break:
mov eax, [ebp+4]
mov [ebp+3*4], eax
postlude
add esp, 4*2
ret
len:
prelude
mov eax, [ebp+2*4]
xor ebx, ebx
.loop:
cmp [eax+ebx], byte 0
je .break
inc ebx
jmp .loop
.break:
mov [ebp+2*4], ebx
postlude
ret

BIN
std.o Normal file

Binary file not shown.

BIN
string.o Normal file

Binary file not shown.