Skip to content

Commit

Permalink
Naive incomplete chunk header packer
Browse files Browse the repository at this point in the history
  • Loading branch information
ChillerDragon committed Jul 29, 2024
1 parent 7e47ba4 commit 7b4c890
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 16 deletions.
79 changes: 79 additions & 0 deletions src/chunks.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
pack_chunk_header:
; pack_chunk_header [rax] [rdi] [rsi]
; rax = flags
; rdi = size
; rsi = output buffer
;
; example:
;
; mov rax, 0
; set_rax_flag CHUNKFLAG_VITAL
; mov rdi, 10
; mov rsi, my_chunk_buffer
; call pack_chunk_header
;

push rax

cmp rdi, 63
jg .pack_chunk_header_error_size

push rax
mov dword rax, [connection_sequence]
cmp rax, 255
jg .pack_chunk_header_error_seq
pop rax

; Chunk header (vital)
; +---------+---------+-----------------+--------+-----------------+
; | Flags | Size | Sequence number | Size | Sequence number |
; | 2 bits | 6 bits | 2 bits | 6 bits | 8 bits |
; +---------+---------+-----------------+--------+-----------------+

; flags
mov [rsi], al
; size (only in the 2nd byte for now)
mov [rsi+1], dil

; sequence only included if it is a vital chunk
is_rax_flag CHUNKFLAG_VITAL
jz .pack_chunk_header_end

; sequence (only in the 3rd byte for now)
mov byte al, [connection_sequence]
mov [rsi+2], al

jmp .pack_chunk_header_end

.pack_chunk_header_error_seq:
print s_unsupported_seq_size
mov rax, [connection_sequence]
call print_uint32
exit 1

.pack_chunk_header_error_size:
print s_unsupported_chunk_size
mov rax, rdi
call print_uint32
exit 1

.pack_chunk_header_end:
pop rax
ret

queue_chunk:
; queue_chunk [rax] [rdi] [rsi] [rdx]
; rax = system (1=system 0=game)
; rdi = payload
; rsi = payload size

; resend flag is not supported yet

push_registers

; increment if vital
; connection_sequence

pop_registers
ret

3 changes: 3 additions & 0 deletions src/data/logger.asm
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ char_space db 0x20

s_dbg_rax_digit db "[debug] value of rax is: ", 0
l_s_dbg_rax_digit equ $ - s_dbg_rax_digit

s_dbg_reg_digit db "[debug] value of register is: ", 0
l_s_dbg_reg_digit equ $ - s_dbg_reg_digit
3 changes: 3 additions & 0 deletions src/data/teeworlds.asm
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ PACKETFLAG_RESEND equ 0b00_0010_00
PACKETFLAG_COMPRESSION equ 0b00_0100_00
PACKETFLAG_CONNLESS equ 0b00_1000_00

CHUNKFLAG_VITAL equ 0b0100_0000
CHUNKFLAG_RESEND equ 0b1000_0000

token db 0xDD, 0xDD, 0xCC, 0xCC
peer_token db 0xFF, 0xFF, 0xFF, 0xFF

Expand Down
16 changes: 16 additions & 0 deletions src/data/teeworlds_strings.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
s_got_peer_token db "[client] got peer token: "
l_s_got_peer_token equ $ - s_got_peer_token
s_got_accept db "[client] got accept", 0x0a
l_s_got_accept equ $ - s_got_accept
s_got_ctrl_msg db "[client] got ctrl msg: "
l_s_got_ctrl_msg equ $ - s_got_ctrl_msg
s_unknown_ctrl_msg db "[client] unknown ctrl msg: "
l_s_unknown_ctrl_msg equ $ - s_unknown_ctrl_msg
s_got_packet_with_chunks db "[client] got packet with chunks: "
l_s_got_packet_with_chunks equ $ - s_got_packet_with_chunks
s_unhandled_packet db "[client] UNHANDLED PACKET!!"
l_s_unhandled_packet equ $ - s_unhandled_packet
s_unsupported_chunk_size db "[error] chunk sizes higher than 63 are not supported yet. got size: "
l_s_unsupported_chunk_size equ $ - s_unsupported_chunk_size
s_unsupported_seq_size db "[error] chunk sequence numbers higher than 255 are not supported yet. got sequence number: "
l_s_unsupported_seq_size equ $ - s_unsupported_seq_size
34 changes: 34 additions & 0 deletions src/macros.asm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,40 @@
pop_registers
%endmacro

%macro is_rax_flag 1
; is_rax_flag [teeworlds bit flag constant]
;
; example:
;
; mov rax, [udp_recv_buf]
; is_rax_flag PACKETFLAG_CONTROL
; jnz on_ctrl_message
;
; no idea if this "if statement" is correct
push rax
and al, %1
cmp al, 0
pop rax
%endmacro

%macro set_rax_flag 1
; set_rax_flag [teeworlds bit flag constant]
;
; sets a bit flag
;
; example:
;
; mov rax, 0
; set_rax_flag CHUNKFLAG_VITAL
;
push rcx

mov cl, %1
or al, cl

pop rcx
%endmacro

%macro push_registers 0
push rax
push rbx
Expand Down
17 changes: 1 addition & 16 deletions src/teeworlds_asmr.asm
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ section .data
%include "src/data/posix.asm"
%include "src/data/syscalls.asm"
%include "src/data/teeworlds.asm"
%include "src/data/teeworlds_strings.asm"
%include "src/data/terminal.asm"
%include "src/data/logger.asm"
%include "src/data/hex.asm"
Expand All @@ -113,8 +114,6 @@ section .data
l_s_you_pressed_a equ $ - s_you_pressed_a
s_you_pressed_d db "you pressed d",0x0a
l_s_you_pressed_d equ $ - s_you_pressed_d
s_dbg_reg_digit db "[debug] value of register is: ", 0
l_s_dbg_reg_digit equ $ - s_dbg_reg_digit
s_got_file_desc db "got file descriptor: "
l_s_got_file_desc equ $ - s_got_file_desc
s_got_udp db "[client] got udp: "
Expand All @@ -130,20 +129,6 @@ section .data
s_packer_size db "[packer] amount of bytes packed: "
l_s_packer_size equ $ - s_packer_size

; teeworlds strings
s_got_peer_token db "[client] got peer token: "
l_s_got_peer_token equ $ - s_got_peer_token
s_got_accept db "[client] got accept", 0x0a
l_s_got_accept equ $ - s_got_accept
s_got_ctrl_msg db "[client] got ctrl msg: "
l_s_got_ctrl_msg equ $ - s_got_ctrl_msg
s_unknown_ctrl_msg db "[client] unknown ctrl msg: "
l_s_unknown_ctrl_msg equ $ - s_unknown_ctrl_msg
s_got_packet_with_chunks db "[client] got packet with chunks: "
l_s_got_packet_with_chunks equ $ - s_got_packet_with_chunks
s_unhandled_packet db "[client] UNHANDLED PACKET!!"
l_s_unhandled_packet equ $ - s_unhandled_packet

section .bss
%include "src/bss/hex.asm"
%include "src/bss/teeworlds.asm"
Expand Down
2 changes: 2 additions & 0 deletions tests/assert.asm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ section .data
%include "src/data/syscalls.asm"
%include "src/data/logger.asm"
%include "src/data/hex.asm"
%include "src/data/teeworlds.asm"
%include "src/data/teeworlds_strings.asm"

s_assert_ok db "[assert] OK", 0x0a
l_s_assert_ok equ $ - s_assert_ok
Expand Down
20 changes: 20 additions & 0 deletions tests/chunk_test.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%include "tests/assert.asm"
%include "src/chunks.asm"

_start:
mov dword [connection_sequence], 9

mov rax, 0
set_rax_flag CHUNKFLAG_VITAL
mov rdi, 6
mov rsi, assert_actual_buf
call pack_chunk_header

mov al, [assert_actual_buf]
assert_al_eq 0x40
mov al, [assert_actual_buf + 1]
assert_al_eq 0x06
mov al, [assert_actual_buf + 2]
assert_al_eq 0x09
exit 0

0 comments on commit 7b4c890

Please sign in to comment.