summaryrefslogtreecommitdiffstats
path: root/src/AFC.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/AFC.c')
-rw-r--r--src/AFC.c597
1 files changed, 339 insertions, 258 deletions
diff --git a/src/AFC.c b/src/AFC.c
index b475a06..42ada68 100644
--- a/src/AFC.c
+++ b/src/AFC.c
@@ -33,10 +33,12 @@ extern int debug;
33 * 33 *
34 * @param client The AFC client connection to lock 34 * @param client The AFC client connection to lock
35 */ 35 */
36static void afc_lock(iphone_afc_client_t client) { 36static void afc_lock(iphone_afc_client_t client)
37 if (debug) fprintf(stderr, "Locked\n"); 37{
38 if (debug)
39 fprintf(stderr, "Locked\n");
38 while (client->lock) { 40 while (client->lock) {
39 usleep(500); // they say it's obsolete, but whatever 41 usleep(500); // they say it's obsolete, but whatever
40 } 42 }
41 client->lock = 1; 43 client->lock = 1;
42} 44}
@@ -45,9 +47,11 @@ static void afc_lock(iphone_afc_client_t client) {
45 * 47 *
46 * @param client The AFC 48 * @param client The AFC
47 */ 49 */
48static void afc_unlock(iphone_afc_client_t client) { // just to be pretty 50static void afc_unlock(iphone_afc_client_t client)
49 if (debug) fprintf(stderr, "Unlocked\n"); 51{ // just to be pretty
50 client->lock = 0; 52 if (debug)
53 fprintf(stderr, "Unlocked\n");
54 client->lock = 0;
51} 55}
52 56
53/** Makes a connection to the AFC service on the phone. 57/** Makes a connection to the AFC service on the phone.
@@ -58,22 +62,23 @@ static void afc_unlock(iphone_afc_client_t client) { // just to be pretty
58 * 62 *
59 * @return A handle to the newly-connected client or NULL upon error. 63 * @return A handle to the newly-connected client or NULL upon error.
60 */ 64 */
61iphone_error_t iphone_afc_new_client ( iphone_device_t device, int src_port, int dst_port, iphone_afc_client_t *client ) { 65iphone_error_t iphone_afc_new_client(iphone_device_t device, int src_port, int dst_port, iphone_afc_client_t * client)
66{
62 int ret = IPHONE_E_SUCCESS; 67 int ret = IPHONE_E_SUCCESS;
63 iphone_afc_client_t client_loc = (iphone_afc_client_t)malloc(sizeof(struct iphone_afc_client_int)); 68 iphone_afc_client_t client_loc = (iphone_afc_client_t) malloc(sizeof(struct iphone_afc_client_int));
64 69
65 if (!device) return IPHONE_E_INVALID_ARG; 70 if (!device)
66 71 return IPHONE_E_INVALID_ARG;
72
67 // Attempt connection 73 // Attempt connection
68 client_loc->connection = NULL; 74 client_loc->connection = NULL;
69 ret = iphone_mux_new_client(device, src_port, dst_port,&client_loc->connection); 75 ret = iphone_mux_new_client(device, src_port, dst_port, &client_loc->connection);
70 if (IPHONE_E_SUCCESS != ret || !client_loc->connection) { 76 if (IPHONE_E_SUCCESS != ret || !client_loc->connection) {
71 free(client_loc); 77 free(client_loc);
72 return ret; 78 return ret;
73 } 79 }
74
75 // Allocate a packet 80 // Allocate a packet
76 client_loc->afc_packet = (AFCPacket*)malloc(sizeof(AFCPacket)); 81 client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket));
77 if (!client_loc->afc_packet) { 82 if (!client_loc->afc_packet) {
78 iphone_mux_free_client(client_loc->connection); 83 iphone_mux_free_client(client_loc->connection);
79 free(client_loc); 84 free(client_loc);
@@ -100,10 +105,11 @@ iphone_error_t iphone_afc_new_client ( iphone_device_t device, int src_port, int
100 * 105 *
101 * @param client The client to disconnect. 106 * @param client The client to disconnect.
102 */ 107 */
103iphone_error_t iphone_afc_free_client ( iphone_afc_client_t client ) { 108iphone_error_t iphone_afc_free_client(iphone_afc_client_t client)
109{
104 if (!client || !client->connection || !client->afc_packet) 110 if (!client || !client->connection || !client->afc_packet)
105 return IPHONE_E_INVALID_ARG; 111 return IPHONE_E_INVALID_ARG;
106 112
107 iphone_mux_free_client(client->connection); 113 iphone_mux_free_client(client->connection);
108 free(client->afc_packet); 114 free(client->afc_packet);
109 free(client); 115 free(client);
@@ -124,65 +130,78 @@ iphone_error_t iphone_afc_free_client ( iphone_afc_client_t client ) {
124 * reason is that if you set them to different values, it indicates 130 * reason is that if you set them to different values, it indicates
125 * you want to send the data as two packets. 131 * you want to send the data as two packets.
126 */ 132 */
127static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int length) { 133static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int length)
134{
128 int bytes = 0, offset = 0; 135 int bytes = 0, offset = 0;
129 char *buffer; 136 char *buffer;
137
138 if (!client || !client->connection || !client->afc_packet)
139 return 0;
140 if (!data || !length)
141 length = 0;
130 142
131 if (!client || !client->connection || !client->afc_packet) return 0;
132 if (!data || !length) length = 0;
133
134 client->afc_packet->packet_num++; 143 client->afc_packet->packet_num++;
135 if (!client->afc_packet->entire_length) { 144 if (!client->afc_packet->entire_length) {
136 client->afc_packet->entire_length = (length) ? sizeof(AFCPacket) + length + 1 : sizeof(AFCPacket); 145 client->afc_packet->entire_length = (length) ? sizeof(AFCPacket) + length + 1 : sizeof(AFCPacket);
137 client->afc_packet->this_length = client->afc_packet->entire_length; 146 client->afc_packet->this_length = client->afc_packet->entire_length;
138 } 147 }
139 if (!client->afc_packet->this_length){ 148 if (!client->afc_packet->this_length) {
140 client->afc_packet->this_length = sizeof(AFCPacket); 149 client->afc_packet->this_length = sizeof(AFCPacket);
141 } 150 }
142 151 // We want to send two segments; buffer+sizeof(AFCPacket) to
143 // We want to send two segments; buffer+sizeof(AFCPacket) to this_length is the parameters 152 // this_length is the parameters
144 // And everything beyond that is the next packet. (for writing) 153 // And everything beyond that is the next packet. (for writing)
145 if (client->afc_packet->this_length != client->afc_packet->entire_length) { 154 if (client->afc_packet->this_length != client->afc_packet->entire_length) {
146 buffer = (char*)malloc(client->afc_packet->this_length); 155 buffer = (char *) malloc(client->afc_packet->this_length);
147 memcpy(buffer, (char*)client->afc_packet, sizeof(AFCPacket)); 156 memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket));
148 offset = client->afc_packet->this_length - sizeof(AFCPacket); 157 offset = client->afc_packet->this_length - sizeof(AFCPacket);
149 158
150 if (debug) fprintf(stderr, "dispatch_AFC_packet: Offset: %i\n", offset); 159 if (debug)
160 fprintf(stderr, "dispatch_AFC_packet: Offset: %i\n", offset);
151 if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) { 161 if ((length) < (client->afc_packet->entire_length - client->afc_packet->this_length)) {
152 if (debug){ 162 if (debug) {
153 fprintf(stderr, "dispatch_AFC_packet: Length did not resemble what it was supposed"); 163 fprintf(stderr, "dispatch_AFC_packet: Length did not resemble what it was supposed");
154 fprintf(stderr, "to based on the packet.\n"); 164 fprintf(stderr, "to based on the packet.\n");
155 fprintf(stderr, "length minus offset: %i\n", length-offset); 165 fprintf(stderr, "length minus offset: %i\n", length - offset);
156 fprintf(stderr, "rest of packet: %i\n", client->afc_packet->entire_length - client->afc_packet->this_length); 166 fprintf(stderr, "rest of packet: %i\n",
167 client->afc_packet->entire_length - client->afc_packet->this_length);
157 } 168 }
158 free(buffer); 169 free(buffer);
159 return -1; 170 return -1;
160 } 171 }
161 memcpy(buffer+sizeof(AFCPacket), data, offset); 172 memcpy(buffer + sizeof(AFCPacket), data, offset);
162 iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); 173 iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes);
163 free(buffer); 174 free(buffer);
164 if (bytes <= 0) { 175 if (bytes <= 0) {
165 return bytes; 176 return bytes;
166 } 177 }
167 178
168 if (debug) { 179 if (debug) {
169 fprintf(stderr, "dispatch_AFC_packet: sent the first now go with the second\n"); 180 fprintf(stderr, "dispatch_AFC_packet: sent the first now go with the second\n");
170 fprintf(stderr, "Length: %i\n", length-offset); 181 fprintf(stderr, "Length: %i\n", length - offset);
171 fprintf(stderr, "Buffer: \n"); 182 fprintf(stderr, "Buffer: \n");
172 fwrite(data+offset, 1, length-offset, stdout); 183 fwrite(data + offset, 1, length - offset, stdout);
173 } 184 }
174 185
175 iphone_mux_send(client->connection, data+offset, length-offset, &bytes); 186 iphone_mux_send(client->connection, data + offset, length - offset, &bytes);
176 return bytes; 187 return bytes;
177 } else { 188 } else {
178 if (debug) fprintf(stderr, "dispatch_AFC_packet doin things the old way\n"); 189 if (debug)
179 char *buffer = (char*)malloc(sizeof(char) * client->afc_packet->this_length); 190 fprintf(stderr, "dispatch_AFC_packet doin things the old way\n");
180 if (debug) fprintf(stderr, "dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length); 191 char *buffer = (char *) malloc(sizeof(char) * client->afc_packet->this_length);
181 memcpy(buffer, (char*)client->afc_packet, sizeof(AFCPacket)); 192 if (debug)
182 if (debug) fprintf(stderr, "dispatch_AFC_packet packet data follows\n"); 193 fprintf(stderr, "dispatch_AFC_packet packet length = %i\n", client->afc_packet->this_length);
183 if (length > 0) { memcpy(buffer+sizeof(AFCPacket), data, length); buffer[sizeof(AFCPacket)+length] = '\0'; } 194 memcpy(buffer, (char *) client->afc_packet, sizeof(AFCPacket));
184 if (debug) fwrite(buffer, 1, client->afc_packet->this_length, stdout); 195 if (debug)
185 if (debug) fprintf(stderr, "\n"); 196 fprintf(stderr, "dispatch_AFC_packet packet data follows\n");
197 if (length > 0) {
198 memcpy(buffer + sizeof(AFCPacket), data, length);
199 buffer[sizeof(AFCPacket) + length] = '\0';
200 }
201 if (debug)
202 fwrite(buffer, 1, client->afc_packet->this_length, stdout);
203 if (debug)
204 fprintf(stderr, "\n");
186 iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes); 205 iphone_mux_send(client->connection, buffer, client->afc_packet->this_length, &bytes);
187 206
188 if (buffer) { 207 if (buffer) {
@@ -205,13 +224,14 @@ static int dispatch_AFC_packet(iphone_afc_client_t client, const char *data, int
205 * AFC_ERROR operation) 224 * AFC_ERROR operation)
206 */ 225 */
207 226
208static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) { 227static int receive_AFC_data(iphone_afc_client_t client, char **dump_here)
228{
209 AFCPacket *r_packet; 229 AFCPacket *r_packet;
210 char *buffer = (char*)malloc(sizeof(AFCPacket) * 4); 230 char *buffer = (char *) malloc(sizeof(AFCPacket) * 4);
211 char *final_buffer = NULL; 231 char *final_buffer = NULL;
212 int bytes = 0, recv_len = 0, current_count=0; 232 int bytes = 0, recv_len = 0, current_count = 0;
213 int retval = 0; 233 int retval = 0;
214 234
215 iphone_mux_recv(client->connection, buffer, sizeof(AFCPacket) * 4, &bytes); 235 iphone_mux_recv(client->connection, buffer, sizeof(AFCPacket) * 4, &bytes);
216 if (bytes <= 0) { 236 if (bytes <= 0) {
217 free(buffer); 237 free(buffer);
@@ -219,102 +239,119 @@ static int receive_AFC_data(iphone_afc_client_t client, char **dump_here) {
219 *dump_here = NULL; 239 *dump_here = NULL;
220 return -1; 240 return -1;
221 } 241 }
222 242
223 r_packet = (AFCPacket*)malloc(sizeof(AFCPacket)); 243 r_packet = (AFCPacket *) malloc(sizeof(AFCPacket));
224 memcpy(r_packet, buffer, sizeof(AFCPacket)); 244 memcpy(r_packet, buffer, sizeof(AFCPacket));
225 245
226 if (r_packet->entire_length == r_packet->this_length && r_packet->entire_length > sizeof(AFCPacket) && r_packet->operation != AFC_ERROR) { 246 if (r_packet->entire_length == r_packet->this_length
227 *dump_here = (char*)malloc(sizeof(char) * (r_packet->entire_length-sizeof(AFCPacket))); 247 && r_packet->entire_length > sizeof(AFCPacket) && r_packet->operation != AFC_ERROR) {
228 memcpy(*dump_here, buffer+sizeof(AFCPacket), r_packet->entire_length-sizeof(AFCPacket)); 248 *dump_here = (char *) malloc(sizeof(char) * (r_packet->entire_length - sizeof(AFCPacket)));
229 retval = r_packet->entire_length - sizeof(AFCPacket); 249 memcpy(*dump_here, buffer + sizeof(AFCPacket), r_packet->entire_length - sizeof(AFCPacket));
250 retval = r_packet->entire_length - sizeof(AFCPacket);
230 free(buffer); 251 free(buffer);
231 free(r_packet); 252 free(r_packet);
232 return retval; 253 return retval;
233 } 254 }
234 255
235 uint32 param1 = buffer[sizeof(AFCPacket)]; 256 uint32 param1 = buffer[sizeof(AFCPacket)];
236 free(buffer); 257 free(buffer);
237 258
238 if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) { 259 if (r_packet->operation == AFC_ERROR && !(client->afc_packet->operation == AFC_DELETE && param1 == 7)) {
239 if (debug) fprintf(stderr, "Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n", 260 if (debug)
240 r_packet->operation, client->afc_packet->operation, param1); 261 fprintf(stderr,
262 "Oops? Bad operation code received: 0x%X, operation=0x%X, param1=%d\n",
263 r_packet->operation, client->afc_packet->operation, param1);
241 recv_len = r_packet->entire_length - r_packet->this_length; 264 recv_len = r_packet->entire_length - r_packet->this_length;
242 free(r_packet); 265 free(r_packet);
243 if (debug) fprintf(stderr, "recv_len=%d\n", recv_len); 266 if (debug)
244 if(param1 == 0) { 267 fprintf(stderr, "recv_len=%d\n", recv_len);
245 if (debug) fprintf(stderr, "... false alarm, but still\n"); 268 if (param1 == 0) {
269 if (debug)
270 fprintf(stderr, "... false alarm, but still\n");
246 *dump_here = NULL; 271 *dump_here = NULL;
247 return 0; 272 return 0;
273 } else {
274 if (debug)
275 fprintf(stderr, "Errno %i\n", param1);
248 } 276 }
249 else { if (debug) fprintf(stderr, "Errno %i\n", param1); }
250 *dump_here = NULL; 277 *dump_here = NULL;
251 return -1; 278 return -1;
252 } else { 279 } else {
253 if (debug) fprintf(stderr, "Operation code %x\nFull length %i and this length %i\n", r_packet->operation, r_packet->entire_length, r_packet->this_length); 280 if (debug)
281 fprintf(stderr,
282 "Operation code %x\nFull length %i and this length %i\n",
283 r_packet->operation, r_packet->entire_length, r_packet->this_length);
254 } 284 }
255 285
256 recv_len = r_packet->entire_length - r_packet->this_length; 286 recv_len = r_packet->entire_length - r_packet->this_length;
257 free(r_packet); 287 free(r_packet);
258 if (!recv_len && r_packet->operation == AFC_SUCCESS_RESPONSE) 288 if (!recv_len && r_packet->operation == AFC_SUCCESS_RESPONSE) {
259 {
260 *dump_here = NULL; 289 *dump_here = NULL;
261 return 0; 290 return 0;
262 } 291 }
263
264 // Keep collecting packets until we have received the entire file. 292 // Keep collecting packets until we have received the entire file.
265 buffer = (char*)malloc(sizeof(char) * (recv_len < MAXIMUM_PACKET_SIZE) ? recv_len : MAXIMUM_PACKET_SIZE); 293 buffer = (char *) malloc(sizeof(char) * (recv_len < MAXIMUM_PACKET_SIZE) ? recv_len : MAXIMUM_PACKET_SIZE);
266 final_buffer = (char*)malloc(sizeof(char) * recv_len); 294 final_buffer = (char *) malloc(sizeof(char) * recv_len);
267 while(current_count < recv_len){ 295 while (current_count < recv_len) {
268 iphone_mux_recv(client->connection, buffer, recv_len-current_count, &bytes); 296 iphone_mux_recv(client->connection, buffer, recv_len - current_count, &bytes);
269 if (debug) fprintf(stderr, "receive_AFC_data: still collecting packets\n"); 297 if (debug)
270 if (bytes < 0) 298 fprintf(stderr, "receive_AFC_data: still collecting packets\n");
271 { 299 if (bytes < 0) {
272 if(debug) fprintf(stderr, "receive_AFC_data: mux_recv failed: %d\n", bytes); 300 if (debug)
301 fprintf(stderr, "receive_AFC_data: mux_recv failed: %d\n", bytes);
273 break; 302 break;
274 } 303 }
275 if (bytes > recv_len-current_count) 304 if (bytes > recv_len - current_count) {
276 { 305 if (debug)
277 if(debug) fprintf(stderr, "receive_AFC_data: mux_recv delivered too much data\n"); 306 fprintf(stderr, "receive_AFC_data: mux_recv delivered too much data\n");
278 break; 307 break;
279 } 308 }
280 if (bytes > 7 && strstr(buffer, "CFA6LPAA")) { 309 if (bytes > 7 && strstr(buffer, "CFA6LPAA")) {
281 if (debug) fprintf(stderr, "receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n", strstr(buffer, "CFA6LPAA") - buffer); 310 if (debug)
282 if (debug) fprintf(stderr, "receive_AFC_data: the total packet length is %i\n", bytes); 311 fprintf(stderr,
312 "receive_AFC_data: WARNING: there is AFC data in this packet at %ti\n",
313 strstr(buffer, "CFA6LPAA") - buffer);
314 if (debug)
315 fprintf(stderr, "receive_AFC_data: the total packet length is %i\n", bytes);
283 } 316 }
284 317
285 memcpy(final_buffer+current_count, buffer, bytes); 318 memcpy(final_buffer + current_count, buffer, bytes);
286 current_count += bytes; 319 current_count += bytes;
287 } 320 }
288 free(buffer); 321 free(buffer);
289 322
290 *dump_here = final_buffer; 323 *dump_here = final_buffer;
291 return current_count; 324 return current_count;
292} 325}
293 326
294static int count_nullspaces(char *string, int number) { 327static int count_nullspaces(char *string, int number)
328{
295 int i = 0, nulls = 0; 329 int i = 0, nulls = 0;
296 330
297 for (i = 0; i < number; i++) { 331 for (i = 0; i < number; i++) {
298 if (string[i] == '\0') nulls++; 332 if (string[i] == '\0')
333 nulls++;
299 } 334 }
300 335
301 return nulls; 336 return nulls;
302} 337}
303 338
304static char **make_strings_list(char *tokens, int true_length) { 339static char **make_strings_list(char *tokens, int true_length)
340{
305 int nulls = 0, i = 0, j = 0; 341 int nulls = 0, i = 0, j = 0;
306 char **list = NULL; 342 char **list = NULL;
307 343
308 if (!tokens || !true_length) return NULL; 344 if (!tokens || !true_length)
309 345 return NULL;
346
310 nulls = count_nullspaces(tokens, true_length); 347 nulls = count_nullspaces(tokens, true_length);
311 list = (char**)malloc(sizeof(char*) * (nulls + 1)); 348 list = (char **) malloc(sizeof(char *) * (nulls + 1));
312 for (i = 0; i < nulls; i++) { 349 for (i = 0; i < nulls; i++) {
313 list[i] = strdup(tokens+j); 350 list[i] = strdup(tokens + j);
314 j += strlen(list[i]) + 1; 351 j += strlen(list[i]) + 1;
315 } 352 }
316 list[i] = NULL; 353 list[i] = NULL;
317 354
318 return list; 355 return list;
319} 356}
320 357
@@ -326,15 +363,17 @@ static char **make_strings_list(char *tokens, int true_length) {
326 * @return A char ** list of files in that directory, terminated by an empty 363 * @return A char ** list of files in that directory, terminated by an empty
327 * string for now or NULL if there was an error. 364 * string for now or NULL if there was an error.
328 */ 365 */
329iphone_error_t iphone_afc_get_dir_list ( iphone_afc_client_t client, const char *dir, char ***list) { 366iphone_error_t iphone_afc_get_dir_list(iphone_afc_client_t client, const char *dir, char ***list)
367{
330 int bytes = 0; 368 int bytes = 0;
331 char *data = NULL, **list_loc = NULL; 369 char *data = NULL, **list_loc = NULL;
332 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 370 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
333 371
334 if (!client || !dir || !list || (list && *list)) return IPHONE_E_INVALID_ARG; 372 if (!client || !dir || !list || (list && *list))
373 return IPHONE_E_INVALID_ARG;
335 374
336 afc_lock(client); 375 afc_lock(client);
337 376
338 // Send the command 377 // Send the command
339 client->afc_packet->operation = AFC_LIST_DIR; 378 client->afc_packet->operation = AFC_LIST_DIR;
340 client->afc_packet->entire_length = 0; 379 client->afc_packet->entire_length = 0;
@@ -344,22 +383,22 @@ iphone_error_t iphone_afc_get_dir_list ( iphone_afc_client_t client, const char
344 afc_unlock(client); 383 afc_unlock(client);
345 return IPHONE_E_NOT_ENOUGH_DATA; 384 return IPHONE_E_NOT_ENOUGH_DATA;
346 } 385 }
347
348 // Receive the data 386 // Receive the data
349 bytes = receive_AFC_data(client, &data); 387 bytes = receive_AFC_data(client, &data);
350 if (bytes < 0 && !data) { 388 if (bytes < 0 && !data) {
351 afc_unlock(client); 389 afc_unlock(client);
352 return IPHONE_E_NOT_ENOUGH_DATA; 390 return IPHONE_E_NOT_ENOUGH_DATA;
353 } 391 }
354
355 // Parse the data 392 // Parse the data
356 list_loc = make_strings_list(data, bytes); 393 list_loc = make_strings_list(data, bytes);
357 if (list_loc) ret = IPHONE_E_SUCCESS; 394 if (list_loc)
358 if (data) free(data); 395 ret = IPHONE_E_SUCCESS;
396 if (data)
397 free(data);
359 398
360 afc_unlock(client); 399 afc_unlock(client);
361 *list = list_loc; 400 *list = list_loc;
362 401
363 return ret; 402 return ret;
364} 403}
365 404
@@ -370,14 +409,16 @@ iphone_error_t iphone_afc_get_dir_list ( iphone_afc_client_t client, const char
370 * @return A char ** list of parameters as given by AFC or NULL if there was an 409 * @return A char ** list of parameters as given by AFC or NULL if there was an
371 * error. 410 * error.
372 */ 411 */
373iphone_error_t iphone_afc_get_devinfo ( iphone_afc_client_t client, char ***infos) { 412iphone_error_t iphone_afc_get_devinfo(iphone_afc_client_t client, char ***infos)
413{
374 int bytes = 0; 414 int bytes = 0;
375 char *data = NULL, **list = NULL; 415 char *data = NULL, **list = NULL;
376 416
377 if (!client || !infos) return IPHONE_E_INVALID_ARG; 417 if (!client || !infos)
418 return IPHONE_E_INVALID_ARG;
378 419
379 afc_lock(client); 420 afc_lock(client);
380 421
381 // Send the command 422 // Send the command
382 client->afc_packet->operation = AFC_GET_DEVINFO; 423 client->afc_packet->operation = AFC_GET_DEVINFO;
383 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 424 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
@@ -386,18 +427,17 @@ iphone_error_t iphone_afc_get_devinfo ( iphone_afc_client_t client, char ***info
386 afc_unlock(client); 427 afc_unlock(client);
387 return IPHONE_E_NOT_ENOUGH_DATA; 428 return IPHONE_E_NOT_ENOUGH_DATA;
388 } 429 }
389
390 // Receive the data 430 // Receive the data
391 bytes = receive_AFC_data(client, &data); 431 bytes = receive_AFC_data(client, &data);
392 if (bytes < 0 && !data) { 432 if (bytes < 0 && !data) {
393 afc_unlock(client); 433 afc_unlock(client);
394 return IPHONE_E_NOT_ENOUGH_DATA; 434 return IPHONE_E_NOT_ENOUGH_DATA;
395 } 435 }
396
397 // Parse the data 436 // Parse the data
398 list = make_strings_list(data, bytes); 437 list = make_strings_list(data, bytes);
399 if (data) free(data); 438 if (data)
400 439 free(data);
440
401 afc_unlock(client); 441 afc_unlock(client);
402 *infos = list; 442 *infos = list;
403 return IPHONE_E_SUCCESS; 443 return IPHONE_E_SUCCESS;
@@ -411,14 +451,16 @@ iphone_error_t iphone_afc_get_devinfo ( iphone_afc_client_t client, char ***info
411 * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG 451 * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG
412 * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. 452 * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise.
413 */ 453 */
414iphone_error_t iphone_afc_delete_file ( iphone_afc_client_t client, const char *path) { 454iphone_error_t iphone_afc_delete_file(iphone_afc_client_t client, const char *path)
455{
415 char *response = NULL; 456 char *response = NULL;
416 int bytes; 457 int bytes;
417 458
418 if (!client || !path || !client->afc_packet || !client->connection) return IPHONE_E_INVALID_ARG; 459 if (!client || !path || !client->afc_packet || !client->connection)
419 460 return IPHONE_E_INVALID_ARG;
461
420 afc_lock(client); 462 afc_lock(client);
421 463
422 // Send command 464 // Send command
423 client->afc_packet->this_length = client->afc_packet->entire_length = 0; 465 client->afc_packet->this_length = client->afc_packet->entire_length = 0;
424 client->afc_packet->operation = AFC_DELETE; 466 client->afc_packet->operation = AFC_DELETE;
@@ -427,13 +469,13 @@ iphone_error_t iphone_afc_delete_file ( iphone_afc_client_t client, const char *
427 afc_unlock(client); 469 afc_unlock(client);
428 return IPHONE_E_NOT_ENOUGH_DATA; 470 return IPHONE_E_NOT_ENOUGH_DATA;
429 } 471 }
430
431 // Receive response 472 // Receive response
432 bytes = receive_AFC_data(client, &response); 473 bytes = receive_AFC_data(client, &response);
433 if (response) free(response); 474 if (response)
434 475 free(response);
476
435 afc_unlock(client); 477 afc_unlock(client);
436 478
437 if (bytes < 0) { 479 if (bytes < 0) {
438 return IPHONE_E_NOT_ENOUGH_DATA; 480 return IPHONE_E_NOT_ENOUGH_DATA;
439 } else { 481 } else {
@@ -450,18 +492,20 @@ iphone_error_t iphone_afc_delete_file ( iphone_afc_client_t client, const char *
450 * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG 492 * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG
451 * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. 493 * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise.
452 */ 494 */
453iphone_error_t iphone_afc_rename_file ( iphone_afc_client_t client, const char *from, const char *to) { 495iphone_error_t iphone_afc_rename_file(iphone_afc_client_t client, const char *from, const char *to)
496{
454 char *response = NULL; 497 char *response = NULL;
455 char *send = (char*)malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32))); 498 char *send = (char *) malloc(sizeof(char) * (strlen(from) + strlen(to) + 1 + sizeof(uint32)));
456 int bytes = 0; 499 int bytes = 0;
457 500
458 if (!client || !from || !to || !client->afc_packet || !client->connection) return IPHONE_E_INVALID_ARG; 501 if (!client || !from || !to || !client->afc_packet || !client->connection)
459 502 return IPHONE_E_INVALID_ARG;
503
460 afc_lock(client); 504 afc_lock(client);
461 505
462 // Send command 506 // Send command
463 memcpy(send, from, strlen(from)+1); 507 memcpy(send, from, strlen(from) + 1);
464 memcpy(send+strlen(from)+1, to, strlen(to)+1); 508 memcpy(send + strlen(from) + 1, to, strlen(to) + 1);
465 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 509 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
466 client->afc_packet->operation = AFC_RENAME; 510 client->afc_packet->operation = AFC_RENAME;
467 bytes = dispatch_AFC_packet(client, send, strlen(to) + strlen(from) + 2); 511 bytes = dispatch_AFC_packet(client, send, strlen(to) + strlen(from) + 2);
@@ -470,13 +514,13 @@ iphone_error_t iphone_afc_rename_file ( iphone_afc_client_t client, const char *
470 afc_unlock(client); 514 afc_unlock(client);
471 return IPHONE_E_NOT_ENOUGH_DATA; 515 return IPHONE_E_NOT_ENOUGH_DATA;
472 } 516 }
473
474 // Receive response 517 // Receive response
475 bytes = receive_AFC_data(client, &response); 518 bytes = receive_AFC_data(client, &response);
476 if (response) free(response); 519 if (response)
520 free(response);
477 521
478 afc_unlock(client); 522 afc_unlock(client);
479 523
480 if (bytes < 0) { 524 if (bytes < 0) {
481 return IPHONE_E_NOT_ENOUGH_DATA; 525 return IPHONE_E_NOT_ENOUGH_DATA;
482 } else { 526 } else {
@@ -493,14 +537,16 @@ iphone_error_t iphone_afc_rename_file ( iphone_afc_client_t client, const char *
493 * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG 537 * @return IPHONE_E_SUCCESS if everythong went well, IPHONE_E_INVALID_ARG
494 * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise. 538 * if arguments are NULL or invalid, IPHONE_E_NOT_ENOUGH_DATA otherwise.
495 */ 539 */
496iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir) { 540iphone_error_t iphone_afc_mkdir(iphone_afc_client_t client, const char *dir)
541{
497 int bytes = 0; 542 int bytes = 0;
498 char *response = NULL; 543 char *response = NULL;
499 544
500 if (!client) return IPHONE_E_INVALID_ARG; 545 if (!client)
501 546 return IPHONE_E_INVALID_ARG;
547
502 afc_lock(client); 548 afc_lock(client);
503 549
504 // Send command 550 // Send command
505 client->afc_packet->operation = AFC_MAKE_DIR; 551 client->afc_packet->operation = AFC_MAKE_DIR;
506 client->afc_packet->this_length = client->afc_packet->entire_length = 0; 552 client->afc_packet->this_length = client->afc_packet->entire_length = 0;
@@ -509,13 +555,13 @@ iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir) {
509 afc_unlock(client); 555 afc_unlock(client);
510 return IPHONE_E_NOT_ENOUGH_DATA; 556 return IPHONE_E_NOT_ENOUGH_DATA;
511 } 557 }
512
513 // Receive response 558 // Receive response
514 bytes = receive_AFC_data(client, &response); 559 bytes = receive_AFC_data(client, &response);
515 if (response) free(response); 560 if (response)
561 free(response);
516 562
517 afc_unlock(client); 563 afc_unlock(client);
518 564
519 if (bytes < 0) { 565 if (bytes < 0) {
520 return IPHONE_E_NOT_ENOUGH_DATA; 566 return IPHONE_E_NOT_ENOUGH_DATA;
521 } else { 567 } else {
@@ -531,18 +577,19 @@ iphone_error_t iphone_afc_mkdir ( iphone_afc_client_t client, const char *dir) {
531 * @return A pointer to an AFCFile struct containing the information received, 577 * @return A pointer to an AFCFile struct containing the information received,
532 * or NULL on failure. 578 * or NULL on failure.
533 */ 579 */
534iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path) { 580iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path)
581{
535 char *received, **list; 582 char *received, **list;
536 iphone_afc_file_t my_file; 583 iphone_afc_file_t my_file;
537 int length, i = 0; 584 int length, i = 0;
538 585
539 afc_lock(client); 586 afc_lock(client);
540 587
541 // Send command 588 // Send command
542 client->afc_packet->operation = AFC_GET_INFO; 589 client->afc_packet->operation = AFC_GET_INFO;
543 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 590 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
544 dispatch_AFC_packet(client, path, strlen(path)); 591 dispatch_AFC_packet(client, path, strlen(path));
545 592
546 // Receive data 593 // Receive data
547 length = receive_AFC_data(client, &received); 594 length = receive_AFC_data(client, &received);
548 if (received) { 595 if (received) {
@@ -554,23 +601,23 @@ iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path
554 } 601 }
555 602
556 afc_unlock(client); 603 afc_unlock(client);
557 604
558 // Parse the data 605 // Parse the data
559 if (list) { 606 if (list) {
560 my_file = (iphone_afc_file_t)malloc(sizeof(struct iphone_afc_file_int)); 607 my_file = (iphone_afc_file_t) malloc(sizeof(struct iphone_afc_file_int));
561 for (i = 0; list[i]; i++) { 608 for (i = 0; list[i]; i++) {
562 if (!strcmp(list[i], "st_size")) { 609 if (!strcmp(list[i], "st_size")) {
563 my_file->size = atoi(list[i+1]); 610 my_file->size = atoi(list[i + 1]);
564 } 611 }
565 612
566 if (!strcmp(list[i], "st_blocks")) { 613 if (!strcmp(list[i], "st_blocks")) {
567 my_file->blocks = atoi(list[i+1]); 614 my_file->blocks = atoi(list[i + 1]);
568 } 615 }
569 616
570 if (!strcmp(list[i], "st_ifmt")) { 617 if (!strcmp(list[i], "st_ifmt")) {
571 if (!strcmp(list[i+1], "S_IFREG")) { 618 if (!strcmp(list[i + 1], "S_IFREG")) {
572 my_file->type = S_IFREG; 619 my_file->type = S_IFREG;
573 } else if (!strcmp(list[i+1], "S_IFDIR")) { 620 } else if (!strcmp(list[i + 1], "S_IFDIR")) {
574 my_file->type = S_IFDIR; 621 my_file->type = S_IFDIR;
575 } 622 }
576 } 623 }
@@ -591,24 +638,27 @@ iphone_afc_file_t afc_get_file_info(iphone_afc_client_t client, const char *path
591 * @return A pointer to an AFCFile struct containing the information received, 638 * @return A pointer to an AFCFile struct containing the information received,
592 * or NULL on failure. 639 * or NULL on failure.
593 */ 640 */
594iphone_error_t iphone_afc_get_file_attr ( iphone_afc_client_t client, const char *filename, struct stat *stbuf ) { 641iphone_error_t iphone_afc_get_file_attr(iphone_afc_client_t client, const char *filename, struct stat * stbuf)
642{
595 643
596 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 644 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
597 if (!client ||!client->connection || !client->afc_packet || !stbuf) return IPHONE_E_INVALID_ARG; 645 if (!client || !client->connection || !client->afc_packet || !stbuf)
646 return IPHONE_E_INVALID_ARG;
598 647
599 memset(stbuf, 0, sizeof(struct stat)); 648 memset(stbuf, 0, sizeof(struct stat));
600 iphone_afc_file_t file = afc_get_file_info(client, filename); 649 iphone_afc_file_t file = afc_get_file_info(client, filename);
601 if (!file){ 650 if (!file) {
602 ret = IPHONE_E_NO_SUCH_FILE; 651 ret = IPHONE_E_NO_SUCH_FILE;
603 } else { 652 } else {
604 stbuf->st_mode = file->type | (S_ISDIR(file->type) ? 0755 : 0644); 653 stbuf->st_mode = file->type | (S_ISDIR(file->type) ? 0755 : 0644);
605 stbuf->st_size = file->size; 654 stbuf->st_size = file->size;
606 stbuf->st_blksize = 2048; // FIXME: Is this the actual block size used on the iPhone? 655 stbuf->st_blksize = 2048; // FIXME: Is this the actual block
656 // size used on the iPhone?
607 stbuf->st_blocks = file->blocks; 657 stbuf->st_blocks = file->blocks;
608 stbuf->st_uid = getuid(); 658 stbuf->st_uid = getuid();
609 stbuf->st_gid = getgid(); 659 stbuf->st_gid = getgid();
610 660
611 ret = iphone_afc_close_file(client,file); 661 ret = iphone_afc_close_file(client, file);
612 } 662 }
613 return ret; 663 return ret;
614} 664}
@@ -626,32 +676,36 @@ iphone_error_t iphone_afc_get_file_attr ( iphone_afc_client_t client, const char
626 * received by afc_get_file_info) as well as the handle to the file or 676 * received by afc_get_file_info) as well as the handle to the file or
627 * NULL in the case of failure. 677 * NULL in the case of failure.
628 */ 678 */
629iphone_error_t iphone_afc_open_file ( iphone_afc_client_t client, const char *filename, iphone_afc_file_mode_t file_mode, iphone_afc_file_t *file ) { 679iphone_error_t
680iphone_afc_open_file(iphone_afc_client_t client, const char *filename,
681 iphone_afc_file_mode_t file_mode, iphone_afc_file_t * file)
682{
630 iphone_afc_file_t file_loc = NULL; 683 iphone_afc_file_t file_loc = NULL;
631 uint32 ag = 0; 684 uint32 ag = 0;
632 int bytes = 0, length = 0; 685 int bytes = 0, length = 0;
633 char *data = (char*)malloc(sizeof(char) * (8 + strlen(filename) + 1)); 686 char *data = (char *) malloc(sizeof(char) * (8 + strlen(filename) + 1));
634 687
635 if (!client ||!client->connection || !client->afc_packet) return IPHONE_E_INVALID_ARG; 688 if (!client || !client->connection || !client->afc_packet)
636 689 return IPHONE_E_INVALID_ARG;
690
637 afc_lock(client); 691 afc_lock(client);
638 692
639 // Send command 693 // Send command
640 memcpy(data, &file_mode, 4); 694 memcpy(data, &file_mode, 4);
641 memcpy(data+4, &ag, 4); 695 memcpy(data + 4, &ag, 4);
642 memcpy(data+8, filename, strlen(filename)); 696 memcpy(data + 8, filename, strlen(filename));
643 data[8+strlen(filename)] = '\0'; 697 data[8 + strlen(filename)] = '\0';
644 client->afc_packet->operation = AFC_FILE_OPEN; 698 client->afc_packet->operation = AFC_FILE_OPEN;
645 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 699 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
646 bytes = dispatch_AFC_packet(client, data, 8+strlen(filename)); 700 bytes = dispatch_AFC_packet(client, data, 8 + strlen(filename));
647 free(data); 701 free(data);
648 702
649 if (bytes <= 0) { 703 if (bytes <= 0) {
650 if (debug) fprintf(stderr, "afc_open_file: Didn't receive a response to the command\n"); 704 if (debug)
705 fprintf(stderr, "afc_open_file: Didn't receive a response to the command\n");
651 afc_unlock(client); 706 afc_unlock(client);
652 return IPHONE_E_NOT_ENOUGH_DATA; 707 return IPHONE_E_NOT_ENOUGH_DATA;
653 } 708 }
654
655 // Receive the data 709 // Receive the data
656 length = receive_AFC_data(client, &data); 710 length = receive_AFC_data(client, &data);
657 if (length > 0 && data) { 711 if (length > 0 && data) {
@@ -664,13 +718,14 @@ iphone_error_t iphone_afc_open_file ( iphone_afc_client_t client, const char *fi
664 *file = file_loc; 718 *file = file_loc;
665 return IPHONE_E_SUCCESS; 719 return IPHONE_E_SUCCESS;
666 } else { 720 } else {
667 if (debug) fprintf(stderr, "afc_open_file: Didn't get any further data\n"); 721 if (debug)
722 fprintf(stderr, "afc_open_file: Didn't get any further data\n");
668 afc_unlock(client); 723 afc_unlock(client);
669 return IPHONE_E_NOT_ENOUGH_DATA; 724 return IPHONE_E_NOT_ENOUGH_DATA;
670 } 725 }
671 726
672 afc_unlock(client); 727 afc_unlock(client);
673 728
674 return IPHONE_E_UNKNOWN_ERROR; 729 return IPHONE_E_UNKNOWN_ERROR;
675} 730}
676 731
@@ -683,59 +738,70 @@ iphone_error_t iphone_afc_open_file ( iphone_afc_client_t client, const char *fi
683 * 738 *
684 * @return The number of bytes read if successful. If there was an error -1. 739 * @return The number of bytes read if successful. If there was an error -1.
685 */ 740 */
686iphone_error_t iphone_afc_read_file ( iphone_afc_client_t client, iphone_afc_file_t file, char *data, int length, uint32_t *bytes) { 741iphone_error_t
742iphone_afc_read_file(iphone_afc_client_t client, iphone_afc_file_t file, char *data, int length, uint32_t * bytes)
743{
687 char *input = NULL; 744 char *input = NULL;
688 int current_count = 0, bytes_loc = 0; 745 int current_count = 0, bytes_loc = 0;
689 const int MAXIMUM_READ_SIZE = 1 << 16; 746 const int MAXIMUM_READ_SIZE = 1 << 16;
690 747
691 if (!client || !client->afc_packet || !client->connection || !file) return IPHONE_E_INVALID_ARG; 748 if (!client || !client->afc_packet || !client->connection || !file)
692 if (debug) fprintf(stderr, "afc_read_file called for length %i\n", length); 749 return IPHONE_E_INVALID_ARG;
750 if (debug)
751 fprintf(stderr, "afc_read_file called for length %i\n", length);
693 752
694 afc_lock(client); 753 afc_lock(client);
695 754
696 // Looping here to get around the maximum amount of data that recieve_AFC_data can handle 755 // Looping here to get around the maximum amount of data that
697 while (current_count < length){ 756 // recieve_AFC_data can handle
698 if (debug) fprintf(stderr, "afc_read_file: current count is %i but length is %i\n", current_count, length); 757 while (current_count < length) {
699 758 if (debug)
759 fprintf(stderr, "afc_read_file: current count is %i but length is %i\n", current_count, length);
760
700 // Send the read command 761 // Send the read command
701 AFCFilePacket *packet = (AFCFilePacket*)malloc(sizeof(AFCFilePacket)); 762 AFCFilePacket *packet = (AFCFilePacket *) malloc(sizeof(AFCFilePacket));
702 packet->unknown1 = packet->unknown2 = 0; 763 packet->unknown1 = packet->unknown2 = 0;
703 packet->filehandle = file->filehandle; 764 packet->filehandle = file->filehandle;
704 packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE; 765 packet->size = ((length - current_count) < MAXIMUM_READ_SIZE) ? (length - current_count) : MAXIMUM_READ_SIZE;
705 client->afc_packet->operation = AFC_READ; 766 client->afc_packet->operation = AFC_READ;
706 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 767 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
707 bytes_loc = dispatch_AFC_packet(client, (char*)packet, sizeof(AFCFilePacket)); 768 bytes_loc = dispatch_AFC_packet(client, (char *) packet, sizeof(AFCFilePacket));
708 free(packet); 769 free(packet);
709 770
710 if (bytes_loc <= 0) { 771 if (bytes_loc <= 0) {
711 afc_unlock(client); 772 afc_unlock(client);
712 return IPHONE_E_NOT_ENOUGH_DATA; 773 return IPHONE_E_NOT_ENOUGH_DATA;
713 } 774 }
714
715 // Receive the data 775 // Receive the data
716 bytes_loc = receive_AFC_data(client, &input); 776 bytes_loc = receive_AFC_data(client, &input);
717 if (debug) fprintf(stderr, "afc_read_file: bytes returned: %i\n", bytes_loc); 777 if (debug)
778 fprintf(stderr, "afc_read_file: bytes returned: %i\n", bytes_loc);
718 if (bytes_loc < 0) { 779 if (bytes_loc < 0) {
719 if (input) free(input); 780 if (input)
781 free(input);
720 afc_unlock(client); 782 afc_unlock(client);
721 return IPHONE_E_NOT_ENOUGH_DATA; 783 return IPHONE_E_NOT_ENOUGH_DATA;
722 } else if (bytes_loc == 0) { 784 } else if (bytes_loc == 0) {
723 if (input) free(input); 785 if (input)
786 free(input);
724 afc_unlock(client); 787 afc_unlock(client);
725 *bytes = current_count; 788 *bytes = current_count;
726 return IPHONE_E_SUCCESS; //FIXME check that's actually a success 789 return IPHONE_E_SUCCESS; // FIXME check that's actually a
790 // success
727 } else { 791 } else {
728 if (input) { 792 if (input) {
729 if (debug) fprintf(stderr, "afc_read_file: %d\n", bytes_loc); 793 if (debug)
730 memcpy(data+current_count, input, (bytes_loc > length) ? length : bytes_loc); 794 fprintf(stderr, "afc_read_file: %d\n", bytes_loc);
795 memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc);
731 free(input); 796 free(input);
732 input = NULL; 797 input = NULL;
733 current_count += (bytes_loc > length) ? length : bytes_loc; 798 current_count += (bytes_loc > length) ? length : bytes_loc;
734 } 799 }
735 } 800 }
736 } 801 }
737 if (debug) fprintf(stderr, "afc_read_file: returning current_count as %i\n", current_count); 802 if (debug)
738 803 fprintf(stderr, "afc_read_file: returning current_count as %i\n", current_count);
804
739 afc_unlock(client); 805 afc_unlock(client);
740 *bytes = current_count; 806 *bytes = current_count;
741 return IPHONE_E_SUCCESS; 807 return IPHONE_E_SUCCESS;
@@ -751,17 +817,22 @@ iphone_error_t iphone_afc_read_file ( iphone_afc_client_t client, iphone_afc_fil
751 * @return The number of bytes written to the file, or a value less than 0 if 817 * @return The number of bytes written to the file, or a value less than 0 if
752 * none were written... 818 * none were written...
753 */ 819 */
754iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_file_t file, const char *data, int length, uint32_t *bytes) { 820iphone_error_t
821iphone_afc_write_file(iphone_afc_client_t client, iphone_afc_file_t file,
822 const char *data, int length, uint32_t * bytes)
823{
755 char *acknowledgement = NULL; 824 char *acknowledgement = NULL;
756 const int MAXIMUM_WRITE_SIZE = 1 << 16; 825 const int MAXIMUM_WRITE_SIZE = 1 << 16;
757 uint32 zero = 0, bytes_loc = 0, segments = (length / MAXIMUM_WRITE_SIZE), current_count = 0, i = 0; 826 uint32 zero = 0, bytes_loc = 0, segments = (length / MAXIMUM_WRITE_SIZE), current_count = 0, i = 0;
758 char *out_buffer = NULL; 827 char *out_buffer = NULL;
759 828
760 if (!client ||!client->afc_packet || !client->connection || !file || !bytes) return IPHONE_E_INVALID_ARG; 829 if (!client || !client->afc_packet || !client->connection || !file || !bytes)
761 830 return IPHONE_E_INVALID_ARG;
831
762 afc_lock(client); 832 afc_lock(client);
763 833
764 if (debug) fprintf(stderr, "afc_write_file: Write length: %i\n", length); 834 if (debug)
835 fprintf(stderr, "afc_write_file: Write length: %i\n", length);
765 836
766 // Divide the file into segments. 837 // Divide the file into segments.
767 for (i = 0; i < segments; i++) { 838 for (i = 0; i < segments; i++) {
@@ -769,10 +840,10 @@ iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_fi
769 client->afc_packet->this_length = sizeof(AFCPacket) + 8; 840 client->afc_packet->this_length = sizeof(AFCPacket) + 8;
770 client->afc_packet->entire_length = client->afc_packet->this_length + MAXIMUM_WRITE_SIZE; 841 client->afc_packet->entire_length = client->afc_packet->this_length + MAXIMUM_WRITE_SIZE;
771 client->afc_packet->operation = AFC_WRITE; 842 client->afc_packet->operation = AFC_WRITE;
772 out_buffer = (char*)malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); 843 out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket));
773 memcpy(out_buffer, (char*)&file->filehandle, sizeof(uint32)); 844 memcpy(out_buffer, (char *) &file->filehandle, sizeof(uint32));
774 memcpy(out_buffer+4, (char*)&zero, sizeof(uint32)); 845 memcpy(out_buffer + 4, (char *) &zero, sizeof(uint32));
775 memcpy(out_buffer+8, data+current_count, MAXIMUM_WRITE_SIZE); 846 memcpy(out_buffer + 8, data + current_count, MAXIMUM_WRITE_SIZE);
776 bytes_loc = dispatch_AFC_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8); 847 bytes_loc = dispatch_AFC_packet(client, out_buffer, MAXIMUM_WRITE_SIZE + 8);
777 if (bytes_loc < 0) { 848 if (bytes_loc < 0) {
778 afc_unlock(client); 849 afc_unlock(client);
@@ -788,39 +859,42 @@ iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_fi
788 return IPHONE_E_NOT_ENOUGH_DATA; 859 return IPHONE_E_NOT_ENOUGH_DATA;
789 } 860 }
790 } 861 }
791 862
792 // By this point, we should be at the end. i.e. the last segment that didn't get sent in the for loop 863 // By this point, we should be at the end. i.e. the last segment that
793 // this length is fine because it's always sizeof(AFCPacket) + 8, but to be sure we do it again 864 // didn't get sent in the for loop
865 // this length is fine because it's always sizeof(AFCPacket) + 8, but
866 // to be sure we do it again
794 if (current_count == length) { 867 if (current_count == length) {
795 afc_unlock(client); 868 afc_unlock(client);
796 *bytes = current_count; 869 *bytes = current_count;
797 return IPHONE_E_SUCCESS; 870 return IPHONE_E_SUCCESS;
798 } 871 }
799 872
800 client->afc_packet->this_length = sizeof(AFCPacket) + 8; 873 client->afc_packet->this_length = sizeof(AFCPacket) + 8;
801 client->afc_packet->entire_length = client->afc_packet->this_length + (length - current_count); 874 client->afc_packet->entire_length = client->afc_packet->this_length + (length - current_count);
802 client->afc_packet->operation = AFC_WRITE; 875 client->afc_packet->operation = AFC_WRITE;
803 out_buffer = (char*)malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket)); 876 out_buffer = (char *) malloc(sizeof(char) * client->afc_packet->entire_length - sizeof(AFCPacket));
804 memcpy(out_buffer, (char*)&file->filehandle, sizeof(uint32)); 877 memcpy(out_buffer, (char *) &file->filehandle, sizeof(uint32));
805 memcpy(out_buffer+4, (char*)&zero, sizeof(uint32)); 878 memcpy(out_buffer + 4, (char *) &zero, sizeof(uint32));
806 memcpy(out_buffer+8, data+current_count, (length - current_count)); 879 memcpy(out_buffer + 8, data + current_count, (length - current_count));
807 bytes_loc = dispatch_AFC_packet(client, out_buffer, (length - current_count) + 8); 880 bytes_loc = dispatch_AFC_packet(client, out_buffer, (length - current_count) + 8);
808 free(out_buffer); 881 free(out_buffer);
809 out_buffer = NULL; 882 out_buffer = NULL;
810 883
811 current_count += bytes_loc; 884 current_count += bytes_loc;
812 885
813 if (bytes_loc <= 0) { 886 if (bytes_loc <= 0) {
814 afc_unlock(client); 887 afc_unlock(client);
815 *bytes = current_count; 888 *bytes = current_count;
816 return IPHONE_E_SUCCESS; 889 return IPHONE_E_SUCCESS;
817 } 890 }
818 891
819 zero = bytes_loc; 892 zero = bytes_loc;
820 bytes_loc = receive_AFC_data(client, &acknowledgement); 893 bytes_loc = receive_AFC_data(client, &acknowledgement);
821 afc_unlock(client); 894 afc_unlock(client);
822 if (bytes_loc < 0) { 895 if (bytes_loc < 0) {
823 if (debug) fprintf(stderr, "afc_write_file: uh oh?\n"); 896 if (debug)
897 fprintf(stderr, "afc_write_file: uh oh?\n");
824 } 898 }
825 *bytes = current_count; 899 *bytes = current_count;
826 return IPHONE_E_SUCCESS; 900 return IPHONE_E_SUCCESS;
@@ -832,19 +906,22 @@ iphone_error_t iphone_afc_write_file ( iphone_afc_client_t client, iphone_afc_fi
832 * @param file A pointer to an AFCFile struct containing the file handle of the 906 * @param file A pointer to an AFCFile struct containing the file handle of the
833 * file to close. 907 * file to close.
834 */ 908 */
835iphone_error_t iphone_afc_close_file ( iphone_afc_client_t client, iphone_afc_file_t file) { 909iphone_error_t iphone_afc_close_file(iphone_afc_client_t client, iphone_afc_file_t file)
836 if (!client || !file) return IPHONE_E_INVALID_ARG; 910{
911 if (!client || !file)
912 return IPHONE_E_INVALID_ARG;
837 char *buffer = malloc(sizeof(char) * 8); 913 char *buffer = malloc(sizeof(char) * 8);
838 uint32 zero = 0; 914 uint32 zero = 0;
839 int bytes = 0; 915 int bytes = 0;
840 916
841 afc_lock(client); 917 afc_lock(client);
842 918
843 if (debug) fprintf(stderr, "afc_close_file: File handle %i\n", file->filehandle); 919 if (debug)
844 920 fprintf(stderr, "afc_close_file: File handle %i\n", file->filehandle);
921
845 // Send command 922 // Send command
846 memcpy(buffer, &file->filehandle, sizeof(uint32)); 923 memcpy(buffer, &file->filehandle, sizeof(uint32));
847 memcpy(buffer+sizeof(uint32), &zero, sizeof(zero)); 924 memcpy(buffer + sizeof(uint32), &zero, sizeof(zero));
848 client->afc_packet->operation = AFC_FILE_CLOSE; 925 client->afc_packet->operation = AFC_FILE_CLOSE;
849 client->afc_packet->entire_length = client->afc_packet->this_length = 0; 926 client->afc_packet->entire_length = client->afc_packet->this_length = 0;
850 bytes = dispatch_AFC_packet(client, buffer, sizeof(char) * 8); 927 bytes = dispatch_AFC_packet(client, buffer, sizeof(char) * 8);
@@ -852,16 +929,17 @@ iphone_error_t iphone_afc_close_file ( iphone_afc_client_t client, iphone_afc_fi
852 buffer = NULL; 929 buffer = NULL;
853 930
854 // FIXME: Is this necesary? 931 // FIXME: Is this necesary?
855 //client->afc_packet->entire_length = client->afc_packet->this_length = 0; 932 // client->afc_packet->entire_length = client->afc_packet->this_length
856 933 // = 0;
857 if (bytes <= 0) { 934
858 afc_unlock(client); 935 if (bytes <= 0) {
936 afc_unlock(client);
859 return IPHONE_E_UNKNOWN_ERROR; 937 return IPHONE_E_UNKNOWN_ERROR;
860 } 938 }
861
862 // Receive the response 939 // Receive the response
863 bytes = receive_AFC_data(client, &buffer); 940 bytes = receive_AFC_data(client, &buffer);
864 if (buffer) free(buffer); 941 if (buffer)
942 free(buffer);
865 free(file); 943 free(file);
866 afc_unlock(client); 944 afc_unlock(client);
867 return IPHONE_E_SUCCESS; 945 return IPHONE_E_SUCCESS;
@@ -876,39 +954,41 @@ iphone_error_t iphone_afc_close_file ( iphone_afc_client_t client, iphone_afc_fi
876 * 954 *
877 * @return IPHONE_E_SUCCESS on success, IPHONE_E_NOT_ENOUGH_DATA on failure. 955 * @return IPHONE_E_SUCCESS on success, IPHONE_E_NOT_ENOUGH_DATA on failure.
878 */ 956 */
879iphone_error_t iphone_afc_seek_file ( iphone_afc_client_t client, iphone_afc_file_t file, int seekpos) { 957iphone_error_t iphone_afc_seek_file(iphone_afc_client_t client, iphone_afc_file_t file, int seekpos)
880 char *buffer = (char*)malloc(sizeof(char) * 24); 958{
959 char *buffer = (char *) malloc(sizeof(char) * 24);
881 uint32 seekto = 0, bytes = 0, zero = 0; 960 uint32 seekto = 0, bytes = 0, zero = 0;
882 961
883 if (seekpos < 0) seekpos = file->size - abs(seekpos); 962 if (seekpos < 0)
963 seekpos = file->size - abs(seekpos);
884 964
885 afc_lock(client); 965 afc_lock(client);
886 966
887 // Send the command 967 // Send the command
888 seekto = seekpos; 968 seekto = seekpos;
889 memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle 969 memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle
890 memcpy(buffer+4, &zero, sizeof(uint32)); // pad 970 memcpy(buffer + 4, &zero, sizeof(uint32)); // pad
891 memcpy(buffer+8, &zero, sizeof(uint32)); // fromwhere 971 memcpy(buffer + 8, &zero, sizeof(uint32)); // fromwhere
892 memcpy(buffer+12, &zero, sizeof(uint32)); // pad 972 memcpy(buffer + 12, &zero, sizeof(uint32)); // pad
893 memcpy(buffer+16, &seekto, sizeof(uint32)); // offset 973 memcpy(buffer + 16, &seekto, sizeof(uint32)); // offset
894 memcpy(buffer+20, &zero, sizeof(uint32)); // pad 974 memcpy(buffer + 20, &zero, sizeof(uint32)); // pad
895 client->afc_packet->operation = AFC_FILE_SEEK; 975 client->afc_packet->operation = AFC_FILE_SEEK;
896 client->afc_packet->this_length = client->afc_packet->entire_length = 0; 976 client->afc_packet->this_length = client->afc_packet->entire_length = 0;
897 bytes = dispatch_AFC_packet(client, buffer, 23); 977 bytes = dispatch_AFC_packet(client, buffer, 23);
898 free(buffer); 978 free(buffer);
899 buffer = NULL; 979 buffer = NULL;
900 980
901 if (bytes <= 0) { 981 if (bytes <= 0) {
902 afc_unlock(client); 982 afc_unlock(client);
903 return IPHONE_E_NOT_ENOUGH_DATA; 983 return IPHONE_E_NOT_ENOUGH_DATA;
904 } 984 }
905
906 // Receive response 985 // Receive response
907 bytes = receive_AFC_data(client, &buffer); 986 bytes = receive_AFC_data(client, &buffer);
908 if (buffer) free(buffer); 987 if (buffer)
909 988 free(buffer);
989
910 afc_unlock(client); 990 afc_unlock(client);
911 991
912 if (bytes >= 0) { 992 if (bytes >= 0) {
913 return IPHONE_E_SUCCESS; 993 return IPHONE_E_SUCCESS;
914 } else { 994 } else {
@@ -927,17 +1007,18 @@ iphone_error_t iphone_afc_seek_file ( iphone_afc_client_t client, iphone_afc_fil
927 * @note This function is more akin to ftruncate than truncate, and truncate 1007 * @note This function is more akin to ftruncate than truncate, and truncate
928 * calls would have to open the file before calling this, sadly. 1008 * calls would have to open the file before calling this, sadly.
929 */ 1009 */
930iphone_error_t iphone_afc_truncate_file ( iphone_afc_client_t client, iphone_afc_file_t file, uint32_t newsize) { 1010iphone_error_t iphone_afc_truncate_file(iphone_afc_client_t client, iphone_afc_file_t file, uint32_t newsize)
931 char *buffer = (char*)malloc(sizeof(char) * 16); 1011{
1012 char *buffer = (char *) malloc(sizeof(char) * 16);
932 uint32 bytes = 0, zero = 0; 1013 uint32 bytes = 0, zero = 0;
933 1014
934 afc_lock(client); 1015 afc_lock(client);
935 1016
936 // Send command 1017 // Send command
937 memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle 1018 memcpy(buffer, &file->filehandle, sizeof(uint32)); // handle
938 memcpy(buffer+4, &zero, sizeof(uint32)); // pad 1019 memcpy(buffer + 4, &zero, sizeof(uint32)); // pad
939 memcpy(buffer+8, &newsize, sizeof(uint32)); // newsize 1020 memcpy(buffer + 8, &newsize, sizeof(uint32)); // newsize
940 memcpy(buffer+12, &zero, 3); // pad 1021 memcpy(buffer + 12, &zero, 3); // pad
941 client->afc_packet->operation = AFC_FILE_TRUNCATE; 1022 client->afc_packet->operation = AFC_FILE_TRUNCATE;
942 client->afc_packet->this_length = client->afc_packet->entire_length = 0; 1023 client->afc_packet->this_length = client->afc_packet->entire_length = 0;
943 bytes = dispatch_AFC_packet(client, buffer, 15); 1024 bytes = dispatch_AFC_packet(client, buffer, 15);
@@ -948,13 +1029,13 @@ iphone_error_t iphone_afc_truncate_file ( iphone_afc_client_t client, iphone_afc
948 afc_unlock(client); 1029 afc_unlock(client);
949 return IPHONE_E_NOT_ENOUGH_DATA; 1030 return IPHONE_E_NOT_ENOUGH_DATA;
950 } 1031 }
951
952 // Receive response 1032 // Receive response
953 bytes = receive_AFC_data(client, &buffer); 1033 bytes = receive_AFC_data(client, &buffer);
954 if (buffer) free(buffer); 1034 if (buffer)
955 1035 free(buffer);
1036
956 afc_unlock(client); 1037 afc_unlock(client);
957 1038
958 if (bytes >= 0) { 1039 if (bytes >= 0) {
959 return IPHONE_E_SUCCESS; 1040 return IPHONE_E_SUCCESS;
960 } else { 1041 } else {