initial commit: a mess of assembly code
[fmap.git] / x86_64_sse2_x87 / inflate / zlib / text.s
blobf0a8b29b74146f5efd547c55c7c7ccec92cf90fa
1 ; vim: set filetype=fasm foldmethod=marker commentstring=;%s colorcolumn=101 :
2 inflate: namespace inflate
3 init_once:
4 enter 0,0
5 and rsp, not 0xf
7 lea rdi, [module.name]
8 mov rsi, 0x0000000000000001 ; RTLD_LAZY
9 call PLT.dlopen
10 test rax, rax
11 jz .err_module_open_failed
12 mov [module.handle], rax
14 symbol inflateInit
15 symbol inflateEnd
16 symbol inflate
17 stc
18 .exit:
19 leave
20 ret
21 error module_open, module_open_failed, module.name
22 error module_inflateInit_sym, sym_resolution_failed, inflateInit.sym
23 error module_inflateEnd_sym, sym_resolution_failed, inflateEnd.sym
24 error module_inflate_sym, sym_resolution_failed, inflate.sym
25 ;---------------------------------------------------------------------------------------------------
26 ; in: rdi = inflate_bytes_start
27 ; in: rsi = inflate_bytes_n_max
28 ; in: rdx = deflate_bytes_start
29 ; in: rcx = deflate_bytes_n
30 define inflate_bytes_start rbx
31 define inflate_bytes_n_max r12
32 define inflate_bytes_n_max_dword r12d ; 32bits dword
33 define deflate_bytes_start r13
34 define deflate_bytes_n r14
35 define deflate_bytes_n_dword r14d ; 32bits dword
36 align_nops
37 do:
38 enter 4 * 8, 0
39 and rsp, not 0xf ; align the stack here for external calls
40 mov [rbp - 8 * 1], inflate_bytes_start
41 mov [rbp - 8 * 2], inflate_bytes_n_max
42 mov [rbp - 8 * 3], deflate_bytes_start
43 mov [rbp - 8 * 4], deflate_bytes_n
45 mov inflate_bytes_start, rdi
46 mov inflate_bytes_n_max, rsi
47 mov deflate_bytes_start, rdx
48 mov deflate_bytes_n, rcx
50 mov qword [z_stream + z_stream_t.zalloc], 0
51 mov qword [z_stream + z_stream_t.zfree], 0
52 mov dword [z_stream + z_stream_t.avail_in], 0
53 mov qword [z_stream + z_stream_t.next_in], 0
54 lea rdi, [z_stream]
55 lea rsi, [zlib_version]
56 mov rdx, z_stream_t.bytes_n
57 call qword [inflateInit]
58 test rax, rax
59 jnz .exit_nok
61 mov qword [z_stream + z_stream_t.next_in], deflate_bytes_start
62 mov dword [z_stream + z_stream_t.avail_in], deflate_bytes_n_dword
63 mov qword [z_stream + z_stream_t.next_out], inflate_bytes_start
64 mov dword [z_stream + z_stream_t.avail_out], inflate_bytes_n_max_dword
65 lea rdi, [z_stream]
66 mov rsi, 4 ; = Z_FINISH we want a 1 call inflate
67 call qword [inflate]
68 cmp rax, 1 ; = Z_STREAM_END
69 setne bl ; error or deflate not performed in 1 call (bad omens)
71 lea rdi, [z_stream]
72 call qword [inflateEnd]
73 test bl, bl
74 jnz .exit_nok
75 .exit_ok:
76 stc
77 .exit:
78 mov inflate_bytes_start, [rbp - 8 * 1]
79 mov inflate_bytes_n_max, [rbp - 8 * 2]
80 mov deflate_bytes_start, [rbp - 8 * 3]
81 mov deflate_bytes_n, [rbp - 8 * 4]
82 leave
83 ret
84 .exit_nok:
85 clc
86 jmp .exit
87 ;---------------------------------------------------------------------------------------------------
88 purge inflate_bytes_start
89 purge inflate_bytes_n_max
90 purge inflate_bytes_n_max_dword
91 purge deflate_bytes_start
92 purge deflate_bytes_n
93 purge deflate_bytes_n_dword
94 ;---------------------------------------------------------------------------------------------------
95 fini_once:
96 enter 0,0
97 and rsp, not 0xf
99 mov rdi, [module.handle]
100 call PLT.dlclose ; 0 on success
102 leave
104 ;---------------------------------------------------------------------------------------------------
105 ;{{{ macros
106 macro symbol name
107 mov rdi, [module.handle]
108 lea rsi, [name.sym]
109 call PLT.dlsym
110 test rax, rax
111 jz .err_module_#name#_sym_failed
112 mov [name], rax
113 end macro
115 macro error name, fmt, s
116 .err_#name#_failed:
117 lea rdi, [msg.fmt]
118 lea rsi, [s]
119 call qword [libc.printf]
121 jmp .exit
122 end macro
123 ;}}} macros
124 end namespace