I am reverse engineering some assembly for a server binary, and am having some trouble understanding some code around a recv call.
I derived the following pseudo-code from the assembly, and the while loop condition confuses me:
func_804a890(int socket, void *buffer_start, size_t buffer_len, int num_open_descriptors) {
int a, b, c;
if (buffer_start == 0 || buffer_len == 0)
//goto 0x804a8e8
size_t msg_len = 0;
void* buffer;
ssize_t packet_len = 0;
do {
buffer_len -= packet_len;
buffer = buffer_start + msg_len;
packet_len = recv(socket, buffer, buffer_len, 0);
if (packet_len == -1) {
return -1;
}
else if (packet_len == 0) {
//goto 0x804a8f8
}
else {
//goto 0x804a8bc
}
msg_len += packet_len;
} while(buffer_len > msg_len);
}
Why would I loop if the buffer is larger than the message from the client? Next is the relevant assembly function in case I am not interpreting one of the variables correctly in my pseudo-code:
.text:0804a890 55 push ebp
.text:0804a891 57 push edi
.text:0804a892 56 push esi
.text:0804a893 53 push ebx
.text:0804a894 83 ec 0c sub esp,0xc
.text:0804a897 8b 74 24 24 mov esi,DWORD PTR [esp+0x24]
.text:0804a89b 8b 7c 24 20 mov edi,DWORD PTR [esp+0x20]
.text:0804a89f 8b 5c 24 28 mov ebx,DWORD PTR [esp+0x28]
.text:0804a8a3 85 f6 test esi,esi
.text:0804a8a5 74 41 je 0x0804a8e8
.text:0804a8a7 85 db test ebx,ebx
.text:0804a8a9 74 3d je 0x0804a8e8
.text:0804a8ab 31 c0 xor eax,eax
.text:0804a8ad 31 ed xor ebp,ebp
.text:0804a8af eb 13 jmp 0x0804a8c4
.text:0804a8b1 8d b4 26 00 00 00 00 lea esi,[esi+eiz*1+0x0]
.text:0804a8b8 85 c0 test eax,eax
.text:0804a8ba 74 3c je 0x0804a8f8
.text:0804a8bc 01 c5 add ebp,eax
.text:0804a8be 39 eb cmp ebx,ebp
.text:0804a8c0 89 e8 mov eax,ebp
.text:0804a8c2 76 24 jbe 0x0804a8e8
.text:0804a8c4 89 da mov edx,ebx
.text:0804a8c6 6a 00 push 0x0
.text:0804a8c8 29 c2 sub edx,eax
.text:0804a8ca 01 f0 add eax,esi
.text:0804a8cc 52 push edx
.text:0804a8cd 50 push eax
.text:0804a8ce 57 push edi
.text:0804a8cf e8 3c ec ff ff call 0x08049510
.text:0804a8d4 83 c4 10 add esp,0x10
.text:0804a8d7 83 f8 ff cmp eax,0xffffffff
.text:0804a8da 75 dc jne 0x0804a8b8
.text:0804a8dc 83 c4 0c add esp,0xc
.text:0804a8df 5b pop ebx
.text:0804a8e0 5e pop esi
.text:0804a8e1 5f pop edi
.text:0804a8e2 5d pop ebp
.text:0804a8e3 c3 ret