summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/utils.c49
-rw-r--r--common/utils.h6
-rw-r--r--src/debugserver.c47
3 files changed, 77 insertions, 25 deletions
diff --git a/common/utils.c b/common/utils.c
index 4a45d95..7f66ec2 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -1,7 +1,10 @@
1/* 1/*
2 * utils.c 2 * utils.c
3 * Miscellaneous utilities for string manipulation 3 * Miscellaneous utilities for string manipulation,
4 * file I/O and plist helper.
4 * 5 *
6 * Copyright (c) 2014-2019 Nikias Bassen, All Rights Reserved.
7 * Copyright (c) 2013-2014 Martin Szulecki, All Rights Reserved.
5 * Copyright (c) 2013 Federico Mena Quintero 8 * Copyright (c) 2013 Federico Mena Quintero
6 * 9 *
7 * This library is free software; you can redistribute it and/or 10 * This library is free software; you can redistribute it and/or
@@ -111,6 +114,50 @@ char *string_concat(const char *str, ...)
111 return result; 114 return result;
112} 115}
113 116
117char *string_append(char* str, ...)
118{
119 size_t len = 0;
120 size_t slen;
121 va_list args;
122 char *s;
123 char *result;
124 char *dest;
125
126 /* Compute final length */
127
128 if (str) {
129 len = strlen(str);
130 }
131 slen = len;
132 len++; /* plus 1 for the null terminator */
133
134 va_start(args, str);
135 s = va_arg(args, char *);
136 while (s) {
137 len += strlen(s);
138 s = va_arg(args, char*);
139 }
140 va_end(args);
141
142 result = realloc(str, len);
143 if (!result)
144 return NULL; /* errno remains set */
145
146 dest = result + slen;
147
148 /* Concat additional strings */
149
150 va_start(args, str);
151 s = va_arg(args, char *);
152 while (s) {
153 dest = stpcpy(dest, s);
154 s = va_arg(args, char *);
155 }
156 va_end(args);
157
158 return result;
159}
160
114char *string_build_path(const char *elem, ...) 161char *string_build_path(const char *elem, ...)
115{ 162{
116 if (!elem) 163 if (!elem)
diff --git a/common/utils.h b/common/utils.h
index 8426bc0..2c3acec 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -1,7 +1,10 @@
1/* 1/*
2 * utils.h 2 * utils.h
3 * Miscellaneous utilities for string manipulation 3 * Miscellaneous utilities for string manipulation,
4 * file I/O and plist helper.
4 * 5 *
6 * Copyright (c) 2014-2019 Nikias Bassen, All Rights Reserved.
7 * Copyright (c) 2013-2014 Martin Szulecki, All Rights Reserved.
5 * Copyright (c) 2013 Federico Mena Quintero 8 * Copyright (c) 2013 Federico Mena Quintero
6 * 9 *
7 * This library is free software; you can redistribute it and/or 10 * This library is free software; you can redistribute it and/or
@@ -39,6 +42,7 @@
39char *stpcpy(char *s1, const char *s2); 42char *stpcpy(char *s1, const char *s2);
40#endif 43#endif
41char *string_concat(const char *str, ...); 44char *string_concat(const char *str, ...);
45char *string_append(char *str, ...);
42char *string_build_path(const char *elem, ...); 46char *string_build_path(const char *elem, ...);
43char *string_format_size(uint64_t size); 47char *string_format_size(uint64_t size);
44char *string_toupper(char *str); 48char *string_toupper(char *str);
diff --git a/src/debugserver.c b/src/debugserver.c
index 1e06233..967d01d 100644
--- a/src/debugserver.c
+++ b/src/debugserver.c
@@ -2,7 +2,8 @@
2 * debugserver.c 2 * debugserver.c
3 * com.apple.debugserver service implementation. 3 * com.apple.debugserver service implementation.
4 * 4 *
5 * Copyright (c) 2014 Martin Szulecki All Rights Reserved. 5 * Copyright (c) 2019 Nikias Bassen, All Rights Reserved.
6 * Copyright (c) 2014-2015 Martin Szulecki All Rights Reserved.
6 * 7 *
7 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public 9 * modify it under the terms of the GNU Lesser General Public
@@ -378,6 +379,7 @@ LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive_response(deb
378 379
379 char* buffer = NULL; 380 char* buffer = NULL;
380 uint32_t buffer_size = 0; 381 uint32_t buffer_size = 0;
382 uint32_t buffer_capacity = 0;
381 383
382 if (response) 384 if (response)
383 *response = NULL; 385 *response = NULL;
@@ -390,7 +392,9 @@ LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive_response(deb
390 if (strncmp(ack, command_prefix, sizeof(char)) == 0) { 392 if (strncmp(ack, command_prefix, sizeof(char)) == 0) {
391 should_receive = 1; 393 should_receive = 1;
392 skip_prefix = 1; 394 skip_prefix = 1;
393 buffer = strdup(command_prefix); 395 buffer = malloc(1024);
396 buffer_capacity = 1024;
397 strcpy(buffer, command_prefix);
394 buffer_size += sizeof(char); 398 buffer_size += sizeof(char);
395 debug_info("received ACK"); 399 debug_info("received ACK");
396 } 400 }
@@ -404,9 +408,11 @@ LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive_response(deb
404 debug_info("received command_prefix: %c", *command_prefix); 408 debug_info("received command_prefix: %c", *command_prefix);
405 if (should_receive) { 409 if (should_receive) {
406 if (buffer) { 410 if (buffer) {
407 memcpy(buffer, command_prefix, sizeof(char)); 411 strcpy(buffer, command_prefix);
408 } else { 412 } else {
409 buffer = strdup(command_prefix); 413 buffer = malloc(1024);
414 buffer_capacity = 1024;
415 strcpy(buffer, command_prefix);
410 buffer_size += sizeof(char); 416 buffer_size += sizeof(char);
411 } 417 }
412 } 418 }
@@ -418,6 +424,7 @@ LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive_response(deb
418 uint32_t checksum_length = DEBUGSERVER_CHECKSUM_HASH_LENGTH; 424 uint32_t checksum_length = DEBUGSERVER_CHECKSUM_HASH_LENGTH;
419 int receiving_checksum_response = 0; 425 int receiving_checksum_response = 0;
420 debug_info("attempting to read up response until checksum"); 426 debug_info("attempting to read up response until checksum");
427
421 while ((checksum_length > 0)) { 428 while ((checksum_length > 0)) {
422 char data[2] = {'#', '\0'}; 429 char data[2] = {'#', '\0'};
423 if (debugserver_client_receive_internal_check(client, data)) { 430 if (debugserver_client_receive_internal_check(client, data)) {
@@ -426,16 +433,20 @@ LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_receive_response(deb
426 if (receiving_checksum_response) { 433 if (receiving_checksum_response) {
427 checksum_length--; 434 checksum_length--;
428 } 435 }
429 char* newbuffer = string_concat(buffer, data, NULL); 436 if (buffer_size + 1 >= buffer_capacity) {
437 char* newbuffer = realloc(buffer, buffer_capacity+1024);
438 if (!newbuffer) {
439 return DEBUGSERVER_E_UNKNOWN_ERROR;
440 }
441 buffer = newbuffer;
442 buffer[buffer_capacity] = '\0';
443 buffer_capacity += 1024;
444 }
445 strcat(buffer, data);
430 buffer_size += sizeof(char); 446 buffer_size += sizeof(char);
431 free(buffer);
432 buffer = NULL;
433 buffer = newbuffer;
434 newbuffer = NULL;
435 } 447 }
436 debug_info("validating response checksum..."); 448 debug_info("validating response checksum...");
437 int valid_response = debugserver_response_is_checksum_valid(buffer, buffer_size); 449 if (client->noack_mode || debugserver_response_is_checksum_valid(buffer, buffer_size)) {
438 if (valid_response) {
439 if (response) { 450 if (response) {
440 /* assemble response string */ 451 /* assemble response string */
441 uint32_t response_size = sizeof(char) * (buffer_size - DEBUGSERVER_CHECKSUM_HASH_LENGTH - 1); 452 uint32_t response_size = sizeof(char) * (buffer_size - DEBUGSERVER_CHECKSUM_HASH_LENGTH - 1);
@@ -482,25 +493,15 @@ LIBIMOBILEDEVICE_API debugserver_error_t debugserver_client_send_command(debugse
482 char* command_arguments = NULL; 493 char* command_arguments = NULL;
483 494
484 /* concat all arguments */ 495 /* concat all arguments */
485 char* tmp = NULL;
486 char* newtmp = NULL;
487 for (i = 0; i < command->argc; i++) { 496 for (i = 0; i < command->argc; i++) {
488 debug_info("argv[%d]: %s", i, command->argv[i]); 497 debug_info("argv[%d]: %s", i, command->argv[i]);
489 if (!tmp) { 498 command_arguments = string_append(command_arguments, command->argv[i], NULL);
490 tmp = strdup(command->argv[i]);
491 } else {
492 newtmp = string_concat(tmp, command->argv[i], NULL);
493 free(tmp);
494 tmp = newtmp;
495 }
496 } 499 }
497 command_arguments = tmp;
498 tmp = NULL;
499 500
500 debug_info("command_arguments(%d): %s", command->argc, command_arguments); 501 debug_info("command_arguments(%d): %s", command->argc, command_arguments);
501 502
502 /* encode command arguments, add checksum if required and assemble entire command */ 503 /* encode command arguments, add checksum if required and assemble entire command */
503 debugserver_format_command("$", command->name, command_arguments, !client->noack_mode, &send_buffer, &send_buffer_size); 504 debugserver_format_command("$", command->name, command_arguments, 1, &send_buffer, &send_buffer_size);
504 505
505 debug_info("sending encoded command: %s", send_buffer); 506 debug_info("sending encoded command: %s", send_buffer);
506 507