From 7bc63230482c92ba617019d791ba2f527cd311f9 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Sat, 21 Sep 2019 00:06:05 +0200 Subject: asr: Transfer filesystem data more efficiently with larger buffers --- src/asr.c | 48 ++++++++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/asr.c b/src/asr.c index 22192fe..18b6976 100644 --- a/src/asr.c +++ b/src/asr.c @@ -40,9 +40,11 @@ #define ASR_FEC_SLICE_STRIDE 40 #define ASR_PACKETS_PER_FEC 25 #define ASR_PAYLOAD_PACKET_SIZE 1450 +#define ASR_PAYLOAD_CHUNK_SIZE 131072 #define ASR_CHECKSUM_CHUNK_SIZE 131072 -int asr_open_with_timeout(idevice_t device, asr_client_t* asr) { +int asr_open_with_timeout(idevice_t device, asr_client_t* asr) +{ int i = 0; int attempts = 10; idevice_connection_t connection = NULL; @@ -117,7 +119,8 @@ void asr_set_progress_callback(asr_client_t asr, asr_progress_cb_t cbfunc, void* asr->progress_cb_data = userdata; } -int asr_receive(asr_client_t asr, plist_t* data) { +int asr_receive(asr_client_t asr, plist_t* data) +{ uint32_t size = 0; char* buffer = NULL; plist_t request = NULL; @@ -125,12 +128,11 @@ int asr_receive(asr_client_t asr, plist_t* data) { *data = NULL; - buffer = (char*) malloc(ASR_BUFFER_SIZE); + buffer = (char*)malloc(ASR_BUFFER_SIZE); if (buffer == NULL) { error("ERROR: Unable to allocate memory for ASR receive buffer\n"); return -1; } - memset(buffer, '\0', ASR_BUFFER_SIZE); device_error = idevice_connection_receive(asr->connection, buffer, ASR_BUFFER_SIZE, &size); if (device_error != IDEVICE_E_SUCCESS) { @@ -149,7 +151,8 @@ int asr_receive(asr_client_t asr, plist_t* data) { return 0; } -int asr_send(asr_client_t asr, plist_t data) { +int asr_send(asr_client_t asr, plist_t data) +{ uint32_t size = 0; char* buffer = NULL; @@ -165,7 +168,8 @@ int asr_send(asr_client_t asr, plist_t data) { return 0; } -int asr_send_buffer(asr_client_t asr, const char* data, uint32_t size) { +int asr_send_buffer(asr_client_t asr, const char* data, uint32_t size) +{ uint32_t bytes = 0; idevice_error_t device_error = IDEVICE_E_SUCCESS; @@ -178,7 +182,8 @@ int asr_send_buffer(asr_client_t asr, const char* data, uint32_t size) { return 0; } -void asr_free(asr_client_t asr) { +void asr_free(asr_client_t asr) +{ if (asr != NULL) { if (asr->connection != NULL) { idevice_disconnect(asr->connection); @@ -189,7 +194,8 @@ void asr_free(asr_client_t asr) { } } -int asr_perform_validation(asr_client_t asr, const char* filesystem) { +int asr_perform_validation(asr_client_t asr, const char* filesystem) +{ FILE* file = NULL; uint64_t length = 0; char* command = NULL; @@ -279,7 +285,8 @@ int asr_perform_validation(asr_client_t asr, const char* filesystem) { return 0; } -int asr_handle_oob_data_request(asr_client_t asr, plist_t packet, FILE* file) { +int asr_handle_oob_data_request(asr_client_t asr, plist_t packet, FILE* file) +{ char* oob_data = NULL; uint64_t oob_offset = 0; uint64_t oob_length = 0; @@ -313,8 +320,7 @@ int asr_handle_oob_data_request(asr_client_t asr, plist_t packet, FILE* file) { fseeko(file, oob_offset, SEEK_SET); #endif if (fread(oob_data, 1, oob_length, file) != oob_length) { - error("ERROR: Unable to read OOB data from filesystem offset: %s\n", - strerror(errno)); + error("ERROR: Unable to read OOB data from filesystem offset: %s\n", strerror(errno)); free(oob_data); return -1; } @@ -328,16 +334,16 @@ int asr_handle_oob_data_request(asr_client_t asr, plist_t packet, FILE* file) { return 0; } -int asr_send_payload(asr_client_t asr, const char* filesystem) { - char data[ASR_PAYLOAD_PACKET_SIZE]; +int asr_send_payload(asr_client_t asr, const char* filesystem) +{ + char *data = NULL; FILE* file = NULL; uint64_t i, length, bytes = 0; double progress = 0; file = fopen(filesystem, "rb"); if (file == NULL) { - error("ERROR: Unable to open filesystem image %s: %s\n", - filesystem, strerror(errno)); + error("ERROR: Unable to open filesystem image %s: %s\n", filesystem, strerror(errno)); return -1; } @@ -353,6 +359,7 @@ int asr_send_payload(asr_client_t asr, const char* filesystem) { int chunk = 0; int add_checksum = 0; + data = (char*)malloc(ASR_PAYLOAD_CHUNK_SIZE); SHA_CTX sha1; @@ -362,8 +369,8 @@ int asr_send_payload(asr_client_t asr, const char* filesystem) { int size = 0; for (i = length; i > 0; i -= size) { - size = ASR_PAYLOAD_PACKET_SIZE; - if (i < ASR_PAYLOAD_PACKET_SIZE) { + size = ASR_PAYLOAD_CHUNK_SIZE; + if (i < ASR_PAYLOAD_CHUNK_SIZE) { size = i; } @@ -377,14 +384,16 @@ int asr_send_payload(asr_client_t asr, const char* filesystem) { add_checksum = 1; } - if (fread(data, 1, size, file) != (unsigned int) size) { + if (fread(data, 1, size, file) != (size_t)size) { error("Error reading filesystem\n"); + free(data); fclose(file); return -1; } if (asr_send_buffer(asr, data, size) < 0) { error("ERROR: Unable to send filesystem payload\n"); + free(data); fclose(file); return -1; } @@ -400,6 +409,7 @@ int asr_send_payload(asr_client_t asr, const char* filesystem) { // send checksum if (asr_send_buffer(asr, data, 20) < 0) { error("ERROR: Unable to send chunk checksum\n"); + free(data); fclose(file); return -1; } @@ -428,11 +438,13 @@ int asr_send_payload(asr_client_t asr, const char* filesystem) { // send checksum if (asr_send_buffer(asr, data, 20) < 0) { error("ERROR: Unable to send chunk checksum\n"); + free(data); fclose(file); return -1; } } + free(data); fclose(file); return 0; } -- cgit v1.1-32-gdbae