Skip to content

Commit

Permalink
win32: Rename and reorder internal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nikias committed Nov 17, 2023
1 parent d15e347 commit 1a12fc2
Showing 1 changed file with 57 additions and 61 deletions.
118 changes: 57 additions & 61 deletions src/libirecovery.c
Original file line number Diff line number Diff line change
Expand Up @@ -1089,11 +1089,52 @@ typedef struct usb_control_request {
char data[];
} usb_control_request;

irecv_error_t mobiledevice_openpipes(irecv_client_t client);
void mobiledevice_closepipes(irecv_client_t client);
irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid);
static irecv_error_t win32_openpipes(irecv_client_t client)
{
if (client->iBootPath && !(client->hIB = CreateFileA(client->iBootPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL))) {
irecv_close(client);
return IRECV_E_UNABLE_TO_CONNECT;
}

irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid)
if (client->DfuPath && !(client->hDFU = CreateFileA(client->DfuPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL))) {
irecv_close(client);
return IRECV_E_UNABLE_TO_CONNECT;
}

client->mode = 0;
if (client->iBootPath == NULL) {
if (strncmp(client->DfuPath, "\\\\?\\usb#vid_05ac&pid_", 21) == 0) {
sscanf(client->DfuPath+21, "%x#", &client->mode);
}
client->handle = client->hDFU;
} else {
if (strncmp(client->iBootPath, "\\\\?\\usb#vid_05ac&pid_", 21) == 0) {
sscanf(client->iBootPath+21, "%x#", &client->mode);
}
client->handle = client->hIB;
}

if (client->mode == 0) {
irecv_close(client);
return IRECV_E_UNABLE_TO_CONNECT;
}

return IRECV_E_SUCCESS;
}

static void win32_closepipes(irecv_client_t client)
{
if (client->hDFU!=NULL) {
CloseHandle(client->hDFU);
client->hDFU = NULL;
}
if (client->hIB!=NULL) {
CloseHandle(client->hIB);
client->hIB = NULL;
}
}

static irecv_error_t win32_open_with_ecid(irecv_client_t* client, uint64_t ecid)
{
int found = 0;
SP_DEVICE_INTERFACE_DATA currentInterface;
Expand Down Expand Up @@ -1124,8 +1165,8 @@ irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid)
free(details);

_client->DfuPath = result;
if (mobiledevice_openpipes(_client) != IRECV_E_SUCCESS) {
mobiledevice_closepipes(_client);
if (win32_openpipes(_client) != IRECV_E_SUCCESS) {
win32_closepipes(_client);
continue;
}

Expand All @@ -1140,7 +1181,7 @@ irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid)

if ((ecid != 0) && (_client->mode == IRECV_K_WTF_MODE)) {
/* we can't get ecid in WTF mode */
mobiledevice_closepipes(_client);
win32_closepipes(_client);
continue;
}

Expand All @@ -1155,7 +1196,7 @@ irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid)
}

if (serial_str[0] == '\0') {
mobiledevice_closepipes(_client);
win32_closepipes(_client);
continue;
}

Expand All @@ -1177,7 +1218,7 @@ irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid)

if (ecid != 0) {
if (_client->device_info.ecid != ecid) {
mobiledevice_closepipes(_client);
win32_closepipes(_client);
continue;
}
debug("found device with ECID %016" PRIx64 "\n", (uint64_t)ecid);
Expand Down Expand Up @@ -1215,14 +1256,14 @@ irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid)
free(details);

_client->iBootPath = result;
if (mobiledevice_openpipes(_client) != IRECV_E_SUCCESS) {
mobiledevice_closepipes(_client);
if (win32_openpipes(_client) != IRECV_E_SUCCESS) {
win32_closepipes(_client);
continue;
}

if ((ecid != 0) && (_client->mode == IRECV_K_WTF_MODE)) {
/* we can't get ecid in WTF mode */
mobiledevice_closepipes(_client);
win32_closepipes(_client);
continue;
}

Expand All @@ -1237,7 +1278,7 @@ irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid)
}

if (serial_str[0] == '\0') {
mobiledevice_closepipes(_client);
win32_closepipes(_client);
continue;
}

Expand All @@ -1259,7 +1300,7 @@ irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid)

if (ecid != 0) {
if (_client->device_info.ecid != ecid) {
mobiledevice_closepipes(_client);
win32_closepipes(_client);
continue;
}
debug("found device with ECID %016" PRIx64 "\n", (uint64_t)ecid);
Expand All @@ -1279,51 +1320,6 @@ irecv_error_t mobiledevice_connect(irecv_client_t* client, uint64_t ecid)

return IRECV_E_SUCCESS;
}

irecv_error_t mobiledevice_openpipes(irecv_client_t client)
{
if (client->iBootPath && !(client->hIB = CreateFileA(client->iBootPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL))) {
irecv_close(client);
return IRECV_E_UNABLE_TO_CONNECT;
}

if (client->DfuPath && !(client->hDFU = CreateFileA(client->DfuPath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL))) {
irecv_close(client);
return IRECV_E_UNABLE_TO_CONNECT;
}

client->mode = 0;
if (client->iBootPath == NULL) {
if (strncmp(client->DfuPath, "\\\\?\\usb#vid_05ac&pid_", 21) == 0) {
sscanf(client->DfuPath+21, "%x#", &client->mode);
}
client->handle = client->hDFU;
} else {
if (strncmp(client->iBootPath, "\\\\?\\usb#vid_05ac&pid_", 21) == 0) {
sscanf(client->iBootPath+21, "%x#", &client->mode);
}
client->handle = client->hIB;
}

if (client->mode == 0) {
irecv_close(client);
return IRECV_E_UNABLE_TO_CONNECT;
}

return IRECV_E_SUCCESS;
}

void mobiledevice_closepipes(irecv_client_t client)
{
if (client->hDFU!=NULL) {
CloseHandle(client->hDFU);
client->hDFU = NULL;
}
if (client->hIB!=NULL) {
CloseHandle(client->hIB);
client->hIB = NULL;
}
}
#endif

#ifdef HAVE_IOKIT
Expand Down Expand Up @@ -1856,7 +1852,7 @@ irecv_error_t irecv_open_with_ecid(irecv_client_t* pclient, uint64_t ecid)
error = libusb_open_with_ecid(pclient, ecid);
#endif
#else
error = mobiledevice_connect(pclient, ecid);
error = win32_open_with_ecid(pclient, ecid);
#endif
irecv_client_t client = *pclient;
if (error != IRECV_E_SUCCESS) {
Expand Down Expand Up @@ -2987,7 +2983,7 @@ irecv_error_t irecv_close(irecv_client_t client)
client->iBootPath = NULL;
free(client->DfuPath);
client->DfuPath = NULL;
mobiledevice_closepipes(client);
win32_closepipes(client);
#endif
free(client->device_info.srnm);
free(client->device_info.imei);
Expand Down

0 comments on commit 1a12fc2

Please sign in to comment.