diff options
| author | 2009-11-17 00:17:22 +0100 | |
|---|---|---|
| committer | 2009-11-30 21:01:32 -0800 | |
| commit | 12a44afce569c2cdf31018c259745e09b13bffc3 (patch) | |
| tree | 4b203632fb8191e963ae96d0548a8c21355601c3 /src/AFC.c | |
| parent | a386752ce0f4ab8dd364b50e9a60c2155827ba66 (diff) | |
| download | libimobiledevice-12a44afce569c2cdf31018c259745e09b13bffc3.tar.gz libimobiledevice-12a44afce569c2cdf31018c259745e09b13bffc3.tar.bz2 | |
added endian safety to AFC
This should make libiphone compatible with big endian machines.
[#85 state:resolved]
Signed-off-by: Matt Colyer <matt@colyer.name>
Diffstat (limited to 'src/AFC.c')
| -rw-r--r-- | src/AFC.c | 34 |
1 files changed, 19 insertions, 15 deletions
| @@ -161,6 +161,7 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l | |||
| 161 | return -1; | 161 | return -1; |
| 162 | } | 162 | } |
| 163 | 163 | ||
| 164 | AFCPacket_to_LE(client->afc_packet); | ||
| 164 | iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); | 165 | iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); |
| 165 | if (sent == 0) { | 166 | if (sent == 0) { |
| 166 | return bytes; | 167 | return bytes; |
| @@ -190,6 +191,7 @@ static int afc_dispatch_packet(afc_client_t client, const char *data, uint64_t l | |||
| 190 | log_debug_buffer((char*)client->afc_packet, sizeof(AFCPacket)); | 191 | log_debug_buffer((char*)client->afc_packet, sizeof(AFCPacket)); |
| 191 | log_debug_msg("\n"); | 192 | log_debug_msg("\n"); |
| 192 | 193 | ||
| 194 | AFCPacket_to_LE(client->afc_packet); | ||
| 193 | iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); | 195 | iphone_device_send(client->connection, (void*)client->afc_packet, sizeof(AFCPacket), &sent); |
| 194 | if (sent == 0) { | 196 | if (sent == 0) { |
| 195 | return bytes; | 197 | return bytes; |
| @@ -230,6 +232,7 @@ static afc_error_t afc_receive_data(afc_client_t client, char **dump_here, int * | |||
| 230 | 232 | ||
| 231 | /* first, read the AFC header */ | 233 | /* first, read the AFC header */ |
| 232 | iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), (uint32_t*)bytes); | 234 | iphone_device_recv(client->connection, (char*)&header, sizeof(AFCPacket), (uint32_t*)bytes); |
| 235 | AFCPacket_from_LE(&header); | ||
| 233 | if (*bytes <= 0) { | 236 | if (*bytes <= 0) { |
| 234 | log_debug_msg("%s: Just didn't get enough.\n", __func__); | 237 | log_debug_msg("%s: Just didn't get enough.\n", __func__); |
| 235 | *dump_here = NULL; | 238 | *dump_here = NULL; |
| @@ -688,7 +691,7 @@ iphone_error_t | |||
| 688 | afc_file_open(afc_client_t client, const char *filename, | 691 | afc_file_open(afc_client_t client, const char *filename, |
| 689 | afc_file_mode_t file_mode, uint64_t *handle) | 692 | afc_file_mode_t file_mode, uint64_t *handle) |
| 690 | { | 693 | { |
| 691 | uint32_t ag = 0; | 694 | uint64_t file_mode_loc = GUINT64_TO_LE(file_mode); |
| 692 | int bytes = 0; | 695 | int bytes = 0; |
| 693 | char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); | 696 | char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1)); |
| 694 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 697 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| @@ -702,8 +705,7 @@ afc_file_open(afc_client_t client, const char *filename, | |||
| 702 | afc_lock(client); | 705 | afc_lock(client); |
| 703 | 706 | ||
| 704 | // Send command | 707 | // Send command |
| 705 | memcpy(data, &file_mode, 4); | 708 | memcpy(data, &file_mode_loc, 8); |
| 706 | memcpy(data + 4, &ag, 4); | ||
| 707 | memcpy(data + 8, filename, strlen(filename)); | 709 | memcpy(data + 8, filename, strlen(filename)); |
| 708 | data[8 + strlen(filename)] = '\0'; | 710 | data[8 + strlen(filename)] = '\0'; |
| 709 | client->afc_packet->operation = AFC_OP_FILE_OPEN; | 711 | client->afc_packet->operation = AFC_OP_FILE_OPEN; |
| @@ -765,7 +767,7 @@ afc_file_read(afc_client_t client, uint64_t handle, char *data, int length, uint | |||
| 765 | // Send the read command | 767 | // Send the read command |
| 766 | AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); | 768 | AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket)); |
| 767 | packet->filehandle = handle; | 769 | packet->filehandle = handle; |
| 768 | packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE; | 770 | packet->size = GUINT64_TO_LE(((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE); |
| 769 | client->afc_packet->operation = AFC_OP_READ; | 771 | client->afc_packet->operation = AFC_OP_READ; |
| 770 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 772 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 771 | bytes_loc = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket)); | 773 | bytes_loc = afc_dispatch_packet(client, (char *) packet, sizeof(AFCFilePacket)); |
| @@ -822,7 +824,7 @@ afc_file_write(afc_client_t client, uint64_t handle, | |||
| 822 | { | 824 | { |
| 823 | char *acknowledgement = NULL; | 825 | char *acknowledgement = NULL; |
| 824 | const int MAXIMUM_WRITE_SIZE = 1 << 15; | 826 | const int MAXIMUM_WRITE_SIZE = 1 << 15; |
| 825 | uint32_t zero = 0, current_count = 0, i = 0; | 827 | uint32_t current_count = 0, i = 0; |
| 826 | uint32_t segments = (length / MAXIMUM_WRITE_SIZE); | 828 | uint32_t segments = (length / MAXIMUM_WRITE_SIZE); |
| 827 | int bytes_loc = 0; | 829 | int bytes_loc = 0; |
| 828 | char *out_buffer = NULL; | 830 | char *out_buffer = NULL; |
| @@ -890,7 +892,6 @@ afc_file_write(afc_client_t client, uint64_t handle, | |||
| 890 | return AFC_E_SUCCESS; | 892 | return AFC_E_SUCCESS; |
| 891 | } | 893 | } |
| 892 | 894 | ||
| 893 | zero = bytes_loc; | ||
| 894 | ret = afc_receive_data(client, &acknowledgement, &bytes_loc); | 895 | ret = afc_receive_data(client, &acknowledgement, &bytes_loc); |
| 895 | afc_unlock(client); | 896 | afc_unlock(client); |
| 896 | if (ret != AFC_E_SUCCESS) { | 897 | if (ret != AFC_E_SUCCESS) { |
| @@ -958,7 +959,7 @@ afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t op | |||
| 958 | { | 959 | { |
| 959 | char *buffer = malloc(16); | 960 | char *buffer = malloc(16); |
| 960 | int bytes = 0; | 961 | int bytes = 0; |
| 961 | uint64_t op = operation; | 962 | uint64_t op = GUINT64_TO_LE(operation); |
| 962 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 963 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 963 | 964 | ||
| 964 | if (!client || (handle == 0)) | 965 | if (!client || (handle == 0)) |
| @@ -1006,7 +1007,8 @@ afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t op | |||
| 1006 | afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) | 1007 | afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) |
| 1007 | { | 1008 | { |
| 1008 | char *buffer = (char *) malloc(sizeof(char) * 24); | 1009 | char *buffer = (char *) malloc(sizeof(char) * 24); |
| 1009 | uint32_t zero = 0; | 1010 | int64_t offset_loc = (int64_t)GUINT64_TO_LE(offset); |
| 1011 | uint64_t whence_loc = GUINT64_TO_LE(whence); | ||
| 1010 | int bytes = 0; | 1012 | int bytes = 0; |
| 1011 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 1013 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1012 | 1014 | ||
| @@ -1017,9 +1019,8 @@ afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, | |||
| 1017 | 1019 | ||
| 1018 | // Send the command | 1020 | // Send the command |
| 1019 | memcpy(buffer, &handle, sizeof(uint64_t)); // handle | 1021 | memcpy(buffer, &handle, sizeof(uint64_t)); // handle |
| 1020 | memcpy(buffer + 8, &whence, sizeof(int32_t)); // fromwhere | 1022 | memcpy(buffer + 8, &whence_loc, sizeof(uint64_t)); // fromwhere |
| 1021 | memcpy(buffer + 12, &zero, sizeof(uint32_t)); // pad | 1023 | memcpy(buffer + 16, &offset_loc, sizeof(uint64_t)); // offset |
| 1022 | memcpy(buffer + 16, &offset, sizeof(uint64_t)); // offset | ||
| 1023 | client->afc_packet->operation = AFC_OP_FILE_SEEK; | 1024 | client->afc_packet->operation = AFC_OP_FILE_SEEK; |
| 1024 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | 1025 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; |
| 1025 | bytes = afc_dispatch_packet(client, buffer, 24); | 1026 | bytes = afc_dispatch_packet(client, buffer, 24); |
| @@ -1077,6 +1078,7 @@ afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *positi | |||
| 1077 | if (bytes > 0 && buffer) { | 1078 | if (bytes > 0 && buffer) { |
| 1078 | /* Get the position */ | 1079 | /* Get the position */ |
| 1079 | memcpy(position, buffer, sizeof(uint64_t)); | 1080 | memcpy(position, buffer, sizeof(uint64_t)); |
| 1081 | *position = GUINT64_FROM_LE(*position); | ||
| 1080 | } | 1082 | } |
| 1081 | if (buffer) | 1083 | if (buffer) |
| 1082 | free(buffer); | 1084 | free(buffer); |
| @@ -1101,6 +1103,7 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new | |||
| 1101 | { | 1103 | { |
| 1102 | char *buffer = (char *) malloc(sizeof(char) * 16); | 1104 | char *buffer = (char *) malloc(sizeof(char) * 16); |
| 1103 | int bytes = 0; | 1105 | int bytes = 0; |
| 1106 | uint64_t newsize_loc = GUINT64_TO_LE(newsize); | ||
| 1104 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 1107 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1105 | 1108 | ||
| 1106 | if (!client || (handle == 0)) | 1109 | if (!client || (handle == 0)) |
| @@ -1110,7 +1113,7 @@ afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t new | |||
| 1110 | 1113 | ||
| 1111 | // Send command | 1114 | // Send command |
| 1112 | memcpy(buffer, &handle, sizeof(uint64_t)); // handle | 1115 | memcpy(buffer, &handle, sizeof(uint64_t)); // handle |
| 1113 | memcpy(buffer + 8, &newsize, sizeof(uint64_t)); // newsize | 1116 | memcpy(buffer + 8, &newsize_loc, sizeof(uint64_t)); // newsize |
| 1114 | client->afc_packet->operation = AFC_OP_FILE_SET_SIZE; | 1117 | client->afc_packet->operation = AFC_OP_FILE_SET_SIZE; |
| 1115 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; | 1118 | client->afc_packet->this_length = client->afc_packet->entire_length = 0; |
| 1116 | bytes = afc_dispatch_packet(client, buffer, 16); | 1119 | bytes = afc_dispatch_packet(client, buffer, 16); |
| @@ -1145,7 +1148,7 @@ afc_error_t afc_truncate(afc_client_t client, const char *path, off_t newsize) | |||
| 1145 | char *response = NULL; | 1148 | char *response = NULL; |
| 1146 | char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); | 1149 | char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); |
| 1147 | int bytes = 0; | 1150 | int bytes = 0; |
| 1148 | uint64_t size_requested = newsize; | 1151 | uint64_t size_requested = GUINT64_TO_LE(newsize); |
| 1149 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 1152 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1150 | 1153 | ||
| 1151 | if (!client || !path || !client->afc_packet || !client->connection) | 1154 | if (!client || !path || !client->afc_packet || !client->connection) |
| @@ -1189,7 +1192,7 @@ afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const c | |||
| 1189 | char *response = NULL; | 1192 | char *response = NULL; |
| 1190 | char *send = (char *) malloc(sizeof(char) * (strlen(target)+1 + strlen(linkname)+1 + 8)); | 1193 | char *send = (char *) malloc(sizeof(char) * (strlen(target)+1 + strlen(linkname)+1 + 8)); |
| 1191 | int bytes = 0; | 1194 | int bytes = 0; |
| 1192 | uint64_t type = linktype; | 1195 | uint64_t type = GUINT64_TO_LE(linktype); |
| 1193 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 1196 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1194 | 1197 | ||
| 1195 | if (!client || !target || !linkname || !client->afc_packet || !client->connection) | 1198 | if (!client || !target || !linkname || !client->afc_packet || !client->connection) |
| @@ -1237,6 +1240,7 @@ afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mt | |||
| 1237 | char *response = NULL; | 1240 | char *response = NULL; |
| 1238 | char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); | 1241 | char *send = (char *) malloc(sizeof(char) * (strlen(path) + 1 + 8)); |
| 1239 | int bytes = 0; | 1242 | int bytes = 0; |
| 1243 | uint64_t mtime_loc = GUINT64_TO_LE(mtime); | ||
| 1240 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; | 1244 | afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1241 | 1245 | ||
| 1242 | if (!client || !path || !client->afc_packet || !client->connection) | 1246 | if (!client || !path || !client->afc_packet || !client->connection) |
| @@ -1245,7 +1249,7 @@ afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mt | |||
| 1245 | afc_lock(client); | 1249 | afc_lock(client); |
| 1246 | 1250 | ||
| 1247 | // Send command | 1251 | // Send command |
| 1248 | memcpy(send, &mtime, 8); | 1252 | memcpy(send, &mtime_loc, 8); |
| 1249 | memcpy(send + 8, path, strlen(path) + 1); | 1253 | memcpy(send + 8, path, strlen(path) + 1); |
| 1250 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; | 1254 | client->afc_packet->entire_length = client->afc_packet->this_length = 0; |
| 1251 | client->afc_packet->operation = AFC_OP_SET_FILE_TIME; | 1255 | client->afc_packet->operation = AFC_OP_SET_FILE_TIME; |
