From 10003bcd970eed414b09eb567d36facbfe92cecc Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Fri, 25 Mar 2022 12:56:55 +0100 Subject: afc: Some minor code polishing --- src/afc.c | 49 +++++++++++++++++++++++++------------------------ 1 file changed, 25 insertions(+), 24 deletions(-) (limited to 'src/afc.c') diff --git a/src/afc.c b/src/afc.c index a17226b..d0f5cb7 100644 --- a/src/afc.c +++ b/src/afc.c @@ -213,7 +213,8 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t uint32_t this_len = 0; uint32_t current_count = 0; uint64_t param1 = -1; - char* dump_here = NULL; + char *buf = NULL; + uint32_t recv_len = 0; if (bytes_recv) { *bytes_recv = 0; @@ -223,12 +224,12 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t } /* first, read the AFC header */ - service_receive(client->parent, (char*)&header, sizeof(AFCPacket), bytes_recv); + service_receive(client->parent, (char*)&header, sizeof(AFCPacket), &recv_len); AFCPacket_from_LE(&header); - if (*bytes_recv == 0) { + if (recv_len == 0) { debug_info("Just didn't get enough."); return AFC_E_MUX_ERROR; - } else if (*bytes_recv < sizeof(AFCPacket)) { + } else if (recv_len < sizeof(AFCPacket)) { debug_info("Did not even get the AFCPacket header"); return AFC_E_MUX_ERROR; } @@ -252,7 +253,6 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t } else if ((header.this_length == header.entire_length) && header.entire_length == sizeof(AFCPacket)) { debug_info("Empty AFCPacket received!"); - *bytes_recv = 0; if (header.operation == AFC_OP_DATA) { return AFC_E_SUCCESS; } else { @@ -265,15 +265,16 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t entire_len = (uint32_t)header.entire_length - sizeof(AFCPacket); this_len = (uint32_t)header.this_length - sizeof(AFCPacket); - dump_here = (char*)malloc(entire_len); + buf = (char*)malloc(entire_len); if (this_len > 0) { - service_receive(client->parent, dump_here, this_len, bytes_recv); - if (*bytes_recv <= 0) { - free(dump_here); + recv_len = 0; + service_receive(client->parent, buf, this_len, &recv_len); + if (recv_len <= 0) { + free(buf); debug_info("Did not get packet contents!"); return AFC_E_NOT_ENOUGH_DATA; - } else if (*bytes_recv < this_len) { - free(dump_here); + } else if (recv_len < this_len) { + free(buf); debug_info("Could not receive this_len=%d bytes", this_len); return AFC_E_NOT_ENOUGH_DATA; } @@ -283,12 +284,13 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t if (entire_len > this_len) { while (current_count < entire_len) { - service_receive(client->parent, dump_here+current_count, entire_len - current_count, bytes_recv); - if (*bytes_recv <= 0) { - debug_info("Error receiving data (recv returned %d)", *bytes_recv); + recv_len = 0; + service_receive(client->parent, buf+current_count, entire_len - current_count, &recv_len); + if (recv_len <= 0) { + debug_info("Error receiving data (recv returned %d)", recv_len); break; } - current_count += *bytes_recv; + current_count += recv_len; } if (current_count < entire_len) { debug_info("WARNING: could not receive full packet (read %s, size %d)", current_count, entire_len); @@ -296,16 +298,16 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t } if (current_count >= sizeof(uint64_t)) { - param1 = le64toh(*(uint64_t*)(dump_here)); + param1 = le64toh(*(uint64_t*)(buf)); } debug_info("packet data size = %i", current_count); if (current_count > 256) { debug_info("packet data follows (256/%u)", current_count); - debug_buffer(dump_here, 256); + debug_buffer(buf, 256); } else { debug_info("packet data follows"); - debug_buffer(dump_here, current_count); + debug_buffer(buf, current_count); } /* check operation types */ @@ -316,7 +318,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t if (param1 != AFC_E_SUCCESS) { /* error status */ /* free buffer */ - free(dump_here); + free(buf); return (afc_error_t)param1; } } else if (header.operation == AFC_OP_DATA) { @@ -330,8 +332,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t debug_info("got a tell response, position=%lld", param1); } else { /* unknown operation code received */ - free(dump_here); - *bytes_recv = 0; + free(buf); debug_info("WARNING: Unknown operation code received 0x%llx param1=%lld", header.operation, param1); #ifndef WIN32 @@ -342,9 +343,9 @@ static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t } if (bytes) { - *bytes = dump_here; + *bytes = buf; } else { - free(dump_here); + free(buf); } *bytes_recv = current_count; @@ -798,7 +799,7 @@ LIBIMOBILEDEVICE_API afc_error_t afc_file_write(afc_client_t client, uint64_t ha ret = afc_receive_data(client, NULL, &bytes_loc); afc_unlock(client); if (ret != AFC_E_SUCCESS) { - debug_info("uh oh?"); + debug_info("Failed to receive reply (%d)", ret); } *bytes_written = current_count; return ret; -- cgit v1.1-32-gdbae