From 43852c74160394c0be876945b85c0656fa2bff81 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Wed, 20 Nov 2019 01:39:46 +0100 Subject: debugserver: Return size of the returned buffer --- include/libimobiledevice/debugserver.h | 6 ++++-- src/debugserver.c | 19 ++++++++++--------- tools/idevicedebug.c | 20 ++++++++++---------- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/include/libimobiledevice/debugserver.h b/include/libimobiledevice/debugserver.h index 19a4f5d..8ac116c 100644 --- a/include/libimobiledevice/debugserver.h +++ b/include/libimobiledevice/debugserver.h @@ -142,22 +142,24 @@ debugserver_error_t debugserver_client_receive(debugserver_client_t client, char * @param client The debugserver client * @param command Command to process and send * @param response Response received for the command (can be NULL to ignore) + * @param response_size Pointer to receive response size. Set to NULL to ignore. * * @return DEBUGSERVER_E_SUCCESS on success, * DEBUGSERVER_E_INVALID_ARG when client or command is NULL */ -debugserver_error_t debugserver_client_send_command(debugserver_client_t client, debugserver_command_t command, char** response); +debugserver_error_t debugserver_client_send_command(debugserver_client_t client, debugserver_command_t command, char** response, size_t* response_size); /** * Receives and parses response of debugserver service. * * @param client The debugserver client * @param response Response received for last command (can be NULL to ignore) + * @param response_size Pointer to receive response size. Set to NULL to ignore. * * @return DEBUGSERVER_E_SUCCESS on success, * DEBUGSERVER_E_INVALID_ARG when client is NULL */ -debugserver_error_t debugserver_client_receive_response(debugserver_client_t client, char** response); +debugserver_error_t debugserver_client_receive_response(debugserver_client_t client, char** response, size_t* response_size); /** * Controls status of ACK mode when sending commands or receiving responses. diff --git a/src/debugserver.c b/src/debugserver.c index 967d01d..0b0d614 100644 --- a/src/debugserver.c +++ b/src/debugserver.c @@ -369,7 +369,7 @@ static int debugserver_client_receive_internal_check(debugserver_client_t client return did_receive_char; } -LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive_response(debugserver_client_t client, char** response) +LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive_response(debugserver_client_t client, char** response, size_t* response_size) { debugserver_error_t res = DEBUGSERVER_E_SUCCESS; @@ -449,10 +449,11 @@ LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive_response(deb if (client->noack_mode || debugserver_response_is_checksum_valid(buffer, buffer_size)) { if (response) { /* assemble response string */ - uint32_t response_size = sizeof(char) * (buffer_size - DEBUGSERVER_CHECKSUM_HASH_LENGTH - 1); - *response = (char*)malloc(response_size + 1); - memcpy(*response, buffer + 1, response_size); - (*response)[response_size] = '\0'; + uint32_t resp_size = sizeof(char) * (buffer_size - DEBUGSERVER_CHECKSUM_HASH_LENGTH - 1); + *response = (char*)malloc(resp_size + 1); + memcpy(*response, buffer + 1, resp_size); + (*response)[resp_size] = '\0'; + if (response_size) *response_size = resp_size; } if (!client->noack_mode) { /* confirm valid command */ @@ -481,7 +482,7 @@ LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive_response(deb return res; } -LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_send_command(debugserver_client_t client, debugserver_command_t command, char** response) +LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_send_command(debugserver_client_t client, debugserver_command_t command, char** response, size_t* response_size) { debugserver_error_t res = DEBUGSERVER_E_SUCCESS; int i; @@ -512,7 +513,7 @@ LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_send_command(debugse } /* receive response */ - res = debugserver_client_receive_response(client, response); + res = debugserver_client_receive_response(client, response, response_size); debug_info("response result: %d", res); if (res != DEBUGSERVER_E_SUCCESS) { goto cleanup; @@ -548,7 +549,7 @@ LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_set_environment_hex_ debugserver_command_t command = NULL; debugserver_command_new("QEnvironmentHexEncoded:", 1, env_arg, &command); - result = debugserver_client_send_command(client, command, response); + result = debugserver_client_send_command(client, command, response, NULL); debugserver_command_free(command); free(env_tmp); @@ -617,7 +618,7 @@ LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_set_argv(debugserver debugserver_command_t command = NULL; debugserver_command_new(pkt, 0, NULL, &command); - result = debugserver_client_send_command(client, command, response); + result = debugserver_client_send_command(client, command, response, NULL); debugserver_command_free(command); if (pkt) diff --git a/tools/idevicedebug.c b/tools/idevicedebug.c index c7508e3..6c2f5cf 100644 --- a/tools/idevicedebug.c +++ b/tools/idevicedebug.c @@ -127,7 +127,7 @@ static debugserver_error_t debugserver_client_handle_response(debugserver_client /* send reply */ debugserver_command_new("OK", 0, NULL, &command); - dres = debugserver_client_send_command(client, command, response); + dres = debugserver_client_send_command(client, command, response, NULL); debug_info("result: %d", dres); debugserver_command_free(command); command = NULL; @@ -153,7 +153,7 @@ static debugserver_error_t debugserver_client_handle_response(debugserver_client /* send reply */ debugserver_command_new("OK", 0, NULL, &command); - dres = debugserver_client_send_command(client, command, response); + dres = debugserver_client_send_command(client, command, response, NULL); debug_info("result: %d", dres); debugserver_command_free(command); command = NULL; @@ -166,7 +166,7 @@ static debugserver_error_t debugserver_client_handle_response(debugserver_client /* no command */ debugserver_command_new("OK", 0, NULL, &command); - dres = debugserver_client_send_command(client, command, response); + dres = debugserver_client_send_command(client, command, response, NULL); debug_info("result: %d", dres); debugserver_command_free(command); command = NULL; @@ -342,7 +342,7 @@ int main(int argc, char *argv[]) if (debug_level) { debug_info("Setting logging bitmask..."); debugserver_command_new("QSetLogging:bitmask=LOG_ALL|LOG_RNB_REMOTE|LOG_RNB_PACKETS", 0, NULL, &command); - dres = debugserver_client_send_command(debugserver_client, command, &response); + dres = debugserver_client_send_command(debugserver_client, command, &response, NULL); debugserver_command_free(command); command = NULL; if (response) { @@ -360,7 +360,7 @@ int main(int argc, char *argv[]) char* packet_size[2] = {strdup("1024"), NULL}; debugserver_command_new("QSetMaxPacketSize:", 1, packet_size, &command); free(packet_size[0]); - dres = debugserver_client_send_command(debugserver_client, command, &response); + dres = debugserver_client_send_command(debugserver_client, command, &response, NULL); debugserver_command_free(command); command = NULL; if (response) { @@ -376,7 +376,7 @@ int main(int argc, char *argv[]) debug_info("Setting working directory..."); char* working_dir[2] = {working_directory, NULL}; debugserver_command_new("QSetWorkingDir:", 1, working_dir, &command); - dres = debugserver_client_send_command(debugserver_client, command, &response); + dres = debugserver_client_send_command(debugserver_client, command, &response, NULL); debugserver_command_free(command); command = NULL; if (response) { @@ -417,7 +417,7 @@ int main(int argc, char *argv[]) /* check if launch succeeded */ debug_info("Checking if launch succeeded..."); debugserver_command_new("qLaunchSuccess", 0, NULL, &command); - dres = debugserver_client_send_command(debugserver_client, command, &response); + dres = debugserver_client_send_command(debugserver_client, command, &response, NULL); debugserver_command_free(command); command = NULL; if (response) { @@ -432,7 +432,7 @@ int main(int argc, char *argv[]) /* set thread */ debug_info("Setting thread..."); debugserver_command_new("Hc0", 0, NULL, &command); - dres = debugserver_client_send_command(debugserver_client, command, &response); + dres = debugserver_client_send_command(debugserver_client, command, &response, NULL); debugserver_command_free(command); command = NULL; if (response) { @@ -447,7 +447,7 @@ int main(int argc, char *argv[]) /* continue running process */ debug_info("Continue running process..."); debugserver_command_new("c", 0, NULL, &command); - dres = debugserver_client_send_command(debugserver_client, command, &response); + dres = debugserver_client_send_command(debugserver_client, command, &response, NULL); debugserver_command_free(command); command = NULL; @@ -470,7 +470,7 @@ int main(int argc, char *argv[]) /* kill process after we finished */ debug_info("Killing process..."); debugserver_command_new("k", 0, NULL, &command); - dres = debugserver_client_send_command(debugserver_client, command, &response); + dres = debugserver_client_send_command(debugserver_client, command, &response, NULL); debugserver_command_free(command); command = NULL; if (response) { -- cgit v1.1-32-gdbae