diff options
Diffstat (limited to 'src/plist.c')
| -rw-r--r-- | src/plist.c | 284 |
1 files changed, 284 insertions, 0 deletions
diff --git a/src/plist.c b/src/plist.c index c4d6bfa..0024577 100644 --- a/src/plist.c +++ b/src/plist.c | |||
| @@ -23,8 +23,10 @@ | |||
| 23 | #include <libxml/tree.h> | 23 | #include <libxml/tree.h> |
| 24 | #include <string.h> | 24 | #include <string.h> |
| 25 | #include <assert.h> | 25 | #include <assert.h> |
| 26 | #include "utils.h" | ||
| 26 | #include "plist.h" | 27 | #include "plist.h" |
| 27 | 28 | ||
| 29 | |||
| 28 | const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\ | 30 | const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\ |
| 29 | <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\ | 31 | <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\ |
| 30 | <plist version=\"1.0\">\n\ | 32 | <plist version=\"1.0\">\n\ |
| @@ -243,3 +245,285 @@ void free_dictionary(char **dictionary) | |||
| 243 | 245 | ||
| 244 | free(dictionary); | 246 | free(dictionary); |
| 245 | } | 247 | } |
| 248 | |||
| 249 | /* | ||
| 250 | * Binary propertylist code follows | ||
| 251 | */ | ||
| 252 | |||
| 253 | |||
| 254 | /* | ||
| 255 | * This is how parsing a bplist is going to have to work: | ||
| 256 | * - The entire binary plist is going to have to be in memory. | ||
| 257 | * - A function, parse_nodes(), will have to be a recursive function | ||
| 258 | * which iterates over the binary plist and reads in elements into bplist_node structs | ||
| 259 | * and handles them accordingly. The end result should be a somewhat-hierarchical layout | ||
| 260 | * of bplist_nodes. | ||
| 261 | * - parse_nodes() will return the first node it encounters, which is usually the "root" node. | ||
| 262 | */ | ||
| 263 | |||
| 264 | uint32_t uipow(uint32_t value, uint32_t power) { | ||
| 265 | if (!power) return 1; | ||
| 266 | int i = 0, oVal = value; | ||
| 267 | for (i = 1; i < power; i++) { | ||
| 268 | value *= oVal; | ||
| 269 | } | ||
| 270 | return value; | ||
| 271 | } | ||
| 272 | |||
| 273 | void byte_convert(char *address, size_t size) { | ||
| 274 | int i = 0, j = 0; | ||
| 275 | char tmp = '\0'; | ||
| 276 | |||
| 277 | for (i = 0; i < (size / 2); i++) { | ||
| 278 | tmp = address[i]; | ||
| 279 | j = ((size-1) + 0) - i; | ||
| 280 | address[i] = address[j]; | ||
| 281 | address[j] = tmp; | ||
| 282 | } | ||
| 283 | } | ||
| 284 | |||
| 285 | bplist_node *parse_raw_node(const char *bpbuffer, uint32_t bplength, uint32_t *position, uint8_t ref_size) { | ||
| 286 | if (!position || !bpbuffer || !bplength) return NULL; | ||
| 287 | |||
| 288 | uint8_t modifier = 0; | ||
| 289 | bplist_node *new_node = (bplist_node*)malloc(sizeof(bplist_node)); | ||
| 290 | bplist_node *length_stupidity = NULL; | ||
| 291 | memset(new_node, 0, sizeof(bplist_node)); // initialize the new struct | ||
| 292 | |||
| 293 | int myPos = *position; | ||
| 294 | if (myPos == bplength || (myPos+1) == bplength) { free(new_node); return NULL; } // end of string | ||
| 295 | |||
| 296 | uint32_t length = 0; | ||
| 297 | if (!myPos) { | ||
| 298 | if (strncmp(bpbuffer, "bplist00", strlen("bplist00"))) { | ||
| 299 | return NULL; // badness! | ||
| 300 | } | ||
| 301 | myPos += strlen("bplist00"); | ||
| 302 | } | ||
| 303 | |||
| 304 | // Get the node's type. | ||
| 305 | if (bpbuffer[myPos] == BPLIST_DATE) { // handle date separately, but do it as a real | ||
| 306 | // better handling of date; basically interpret as real or double | ||
| 307 | new_node->type = BPLIST_DATE; | ||
| 308 | new_node->length = 8; // always 8 for "date" (Apple intended it, not me) | ||
| 309 | myPos++; | ||
| 310 | memcpy(&new_node->realval, bpbuffer+myPos, sizeof(new_node->realval)); | ||
| 311 | byte_convert(&new_node->realval, sizeof(new_node->realval)); | ||
| 312 | myPos += new_node->length; | ||
| 313 | *position = myPos; | ||
| 314 | return new_node; | ||
| 315 | } | ||
| 316 | |||
| 317 | new_node->type = bpbuffer[myPos] & BPLIST_MASK; | ||
| 318 | new_node->length = bpbuffer[myPos] & BPLIST_FILL; | ||
| 319 | if (!new_node->type) { | ||
| 320 | // what? check if it's a boolean. | ||
| 321 | if (bpbuffer[myPos] == BPLIST_TRUE || bpbuffer[myPos] == BPLIST_FALSE) { | ||
| 322 | // okay, so it is. Carry on. | ||
| 323 | new_node->type = bpbuffer[myPos]; | ||
| 324 | new_node->length = 0; | ||
| 325 | } else { | ||
| 326 | // er, what? we have a bad type here. Return NULL. | ||
| 327 | free(new_node); | ||
| 328 | //printf("parse_raw_node: lol type: type given %x\n", bpbuffer[myPos]); | ||
| 329 | return NULL; | ||
| 330 | } | ||
| 331 | } | ||
| 332 | |||
| 333 | myPos++; // puts us in the data. | ||
| 334 | if (new_node->length == BPLIST_FILL) { // Data happens to contain length... | ||
| 335 | // what? you're going to make me parse an int for the length. You suck. | ||
| 336 | *position = myPos; | ||
| 337 | length_stupidity = parse_raw_node(bpbuffer, bplength, &myPos, ref_size); | ||
| 338 | switch (length_stupidity->length) { | ||
| 339 | case sizeof(uint8_t): | ||
| 340 | new_node->length = length_stupidity->intval8; | ||
| 341 | break; | ||
| 342 | case sizeof(uint16_t): | ||
| 343 | new_node->length = length_stupidity->intval16; | ||
| 344 | break; | ||
| 345 | case sizeof(uint32_t): | ||
| 346 | new_node->length = length_stupidity->intval32; | ||
| 347 | break; | ||
| 348 | case sizeof(uint64_t): | ||
| 349 | new_node->length = length_stupidity->intval64; | ||
| 350 | break; | ||
| 351 | default: | ||
| 352 | free(new_node); | ||
| 353 | free(length_stupidity); | ||
| 354 | return NULL; | ||
| 355 | } | ||
| 356 | // There, we have our fucking length now. | ||
| 357 | *position = myPos; | ||
| 358 | free(length_stupidity); // cleanup | ||
| 359 | } | ||
| 360 | |||
| 361 | // Now we're in the data. | ||
| 362 | // Error-checking sorta | ||
| 363 | if ((myPos + new_node->length) >= bplength) { | ||
| 364 | new_node->length = bplength - myPos; // truncate the object | ||
| 365 | } | ||
| 366 | |||
| 367 | // And now for the greatest show on earth: the giant fucking switch statement. | ||
| 368 | switch (new_node->type) { | ||
| 369 | case BPLIST_INT: | ||
| 370 | new_node->length = uipow(2, new_node->length); // make length less misleading | ||
| 371 | switch (new_node->length) { | ||
| 372 | case sizeof(uint8_t): | ||
| 373 | new_node->intval8 = bpbuffer[myPos]; | ||
| 374 | break; | ||
| 375 | case sizeof(uint16_t): | ||
| 376 | memcpy(&new_node->intval16, bpbuffer+myPos, sizeof(uint16_t)); | ||
| 377 | new_node->intval16 = ntohs(new_node->intval16); | ||
| 378 | break; | ||
| 379 | case sizeof(uint32_t): | ||
| 380 | memcpy(&new_node->intval32, bpbuffer+myPos, sizeof(uint32_t)); | ||
| 381 | new_node->intval32 = ntohl(new_node->intval32); | ||
| 382 | break; | ||
| 383 | case sizeof(uint64_t): | ||
| 384 | memcpy(&new_node->intval64, bpbuffer+myPos, sizeof(uint64_t)); | ||
| 385 | byte_convert(&new_node->intval64, sizeof(uint64_t)); | ||
| 386 | break; | ||
| 387 | default: | ||
| 388 | free(new_node); | ||
| 389 | printf("parse_raw_node: lol: invalid int: size given %i\n", new_node->length); | ||
| 390 | printf("parse_raw_node: lol: by the way sizeof(uint64) = %i\n", sizeof(uint64_t)); | ||
| 391 | return NULL; | ||
| 392 | } | ||
| 393 | break; | ||
| 394 | |||
| 395 | case BPLIST_REAL: | ||
| 396 | new_node->length = uipow(2, new_node->length); | ||
| 397 | memcpy(&new_node->realval, bpbuffer+myPos, new_node->length); // XXX: probable buffer overflow here | ||
| 398 | //new_node->realval = bpbuffer[myPos]; // why not | ||
| 399 | byte_convert(&new_node->realval, sizeof(double)); | ||
| 400 | break; | ||
| 401 | |||
| 402 | case BPLIST_DICT: /* returning a raw dict, it forward-references, so. */ | ||
| 403 | new_node->length = new_node->length * 2; // dicts lie | ||
| 404 | case BPLIST_ARRAY: /* returning a raw array, it forward-references, so. */ | ||
| 405 | new_node->intval8 = ref_size; // in arrays and dicts, the "ref size" alluded to in the trailer applies, and should be stored in intval8 so as to save space. | ||
| 406 | case BPLIST_STRING: | ||
| 407 | case BPLIST_DATA: | ||
| 408 | default: /* made to hold raw data. */ | ||
| 409 | modifier = (new_node->intval8 > 0) ? new_node->intval8 : 1; | ||
| 410 | new_node->strval = (char*)malloc(sizeof(char) * (new_node->length * modifier)); | ||
| 411 | memcpy(new_node->strval, bpbuffer+myPos, (new_node->length * modifier)); | ||
| 412 | break; | ||
| 413 | |||
| 414 | case BPLIST_UNICODE: | ||
| 415 | new_node->unicodeval = (wchar_t*)malloc(sizeof(wchar_t) * new_node->length); | ||
| 416 | memcpy(new_node->unicodeval, bpbuffer+myPos, new_node->length); | ||
| 417 | break; | ||
| 418 | } | ||
| 419 | |||
| 420 | myPos += new_node->length; | ||
| 421 | *position = myPos; | ||
| 422 | return new_node; | ||
| 423 | } | ||
| 424 | |||
| 425 | void print_bytes(char *val, size_t size) { | ||
| 426 | int i = 0; | ||
| 427 | for (i = 0; i < size; i++) { | ||
| 428 | printf("Byte %i: 0x%x\n", i, val[i]); | ||
| 429 | } | ||
| 430 | } | ||
| 431 | |||
| 432 | bplist_node *parse_nodes(const char *bpbuffer, uint32_t bplength, uint32_t *position) { | ||
| 433 | bplist_node **nodeslist = NULL, **newaddr = NULL; | ||
| 434 | bplist_node *new_node = NULL, *root_node = NULL; | ||
| 435 | |||
| 436 | uint32_t nodeslength = 0; | ||
| 437 | uint8_t offset_size = 0, dict_param_size = 0; | ||
| 438 | offset_size = bpbuffer[bplength-26]; | ||
| 439 | dict_param_size = bpbuffer[bplength-25]; | ||
| 440 | uint64_t current_offset = 0; | ||
| 441 | //uint64_t num_objects = *(bpbuffer+(bplength-24)), root_object = *(bpbuffer+(bplength-16)), offset_table_index = *(bpbuffer+(bplength-8)); | ||
| 442 | uint64_t num_objects = 0, root_object = 0, offset_table_index = 0; | ||
| 443 | memcpy(&num_objects, bpbuffer+bplength-24, sizeof(uint64_t)); | ||
| 444 | memcpy(&root_object, bpbuffer+bplength-16, sizeof(uint64_t)); | ||
| 445 | memcpy(&offset_table_index, bpbuffer+bplength-8, sizeof(uint64_t)); | ||
| 446 | byte_convert(&num_objects, sizeof(uint64_t)); | ||
| 447 | byte_convert(&root_object, sizeof(uint64_t)); | ||
| 448 | byte_convert(&offset_table_index, sizeof(uint64_t)); | ||
| 449 | |||
| 450 | log_debug_msg("Offset size: %i\nGiven: %i\n", offset_size, bpbuffer[bplength-26]); | ||
| 451 | log_debug_msg("Ref size: %i\nGiven: %i\n", dict_param_size, bpbuffer[bplength-25]); | ||
| 452 | log_debug_msg("Number of objects: %lli\nGiven: %llu\n", num_objects, *(bpbuffer+bplength-24)); | ||
| 453 | log_debug_msg("Root object index: %lli\nGiven: %llu\n", root_object, *(bpbuffer+bplength-16)); | ||
| 454 | log_debug_msg("Offset table index: %lli\nGiven: %llu\n", offset_table_index, *(bpbuffer+bplength-8)); | ||
| 455 | log_debug_msg("Size of uint64: %i\n", sizeof(uint64_t)); | ||
| 456 | |||
| 457 | int i = 0, j = 0, k = 0, str_i = 0, str_j = 0; | ||
| 458 | uint32_t index1 = 0, index2 = 0; | ||
| 459 | |||
| 460 | nodeslist = (bplist_node**)malloc(sizeof(bplist_node*) * num_objects); | ||
| 461 | if (!nodeslist) return NULL; | ||
| 462 | |||
| 463 | for (i = 0; i < num_objects; i++) { | ||
| 464 | memcpy(¤t_offset, bpbuffer+(offset_table_index+(i*offset_size)), offset_size); | ||
| 465 | //current_offset = (offset_size == 2) ? ntohs(current_offset) : (offset_size == 4) ? ntohl(current_offset) : current_offset; | ||
| 466 | //if (offset_size == 8) byte_convert(¤t_offset, 8); | ||
| 467 | byte_convert(¤t_offset, (offset_size <= sizeof(current_offset)) ? offset_size : sizeof(current_offset)); | ||
| 468 | log_debug_msg("parse_nodes: current_offset = %x\n", current_offset); | ||
| 469 | nodeslist[i] = parse_raw_node(bpbuffer, bplength, ¤t_offset, dict_param_size); | ||
| 470 | log_debug_msg("parse_nodes: parse_raw_node done\n"); | ||
| 471 | } | ||
| 472 | |||
| 473 | |||
| 474 | for (i = 0; i < num_objects; i++) { | ||
| 475 | // set elements for dicts and arrays and leave the rest alone | ||
| 476 | log_debug_msg("parse_nodes: on node %i\n", i); | ||
| 477 | switch (nodeslist[i]->type) { | ||
| 478 | case BPLIST_DICT: | ||
| 479 | log_debug_msg("parse_nodes: dictionary found\n"); | ||
| 480 | nodeslist[i]->subnodes = (bplist_node*)malloc(sizeof(bplist_node) * nodeslist[i]->length); | ||
| 481 | for (j = 0; j < (nodeslist[i]->length / 2); j++) { | ||
| 482 | str_i = j * nodeslist[i]->intval8; | ||
| 483 | str_j = (j + (nodeslist[i]->length / 2)) * nodeslist[i]->intval8; | ||
| 484 | |||
| 485 | memcpy(&index1, nodeslist[i]->strval+str_i, nodeslist[i]->intval8); | ||
| 486 | memcpy(&index2, nodeslist[i]->strval+str_j, nodeslist[i]->intval8); | ||
| 487 | //index1 = (dict_param_size == 1) ? index1 : (dict_param_size == 2) ? ntohs(index1) : (dict_param_size == 4) ? ntohl(index1) : index1; | ||
| 488 | //index2 = (dict_param_size == 1) ? index2 : (dict_param_size == 2) ? ntohs(index2) : (dict_param_size == 4) ? ntohl(index2) : index2; | ||
| 489 | byte_convert(&index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index2)); | ||
| 490 | byte_convert(&index2, (dict_param_size <= sizeof(index2)) ? dict_param_size : sizeof(index2)); | ||
| 491 | //printf("parse_nodes: key index %i value %i\n", index1, index2); | ||
| 492 | //printf("parse_nodes: key type %x and length %i\n", nodeslist[index1]->type, nodeslist[index1]->length); | ||
| 493 | //printf("parse_nodes: value type %x and length %i\n", nodeslist[index2]->type, nodeslist[index2]->length); | ||
| 494 | nodeslist[i]->subnodes[k++] = nodeslist[index1]; | ||
| 495 | nodeslist[i]->subnodes[k++] = nodeslist[index2]; | ||
| 496 | } | ||
| 497 | |||
| 498 | nodeslist[i]->length = nodeslist[i]->length / 2; | ||
| 499 | free(nodeslist[i]->strval); | ||
| 500 | k = 0; | ||
| 501 | break; | ||
| 502 | |||
| 503 | case BPLIST_ARRAY: | ||
| 504 | log_debug_msg("parse_nodes: array found\n"); | ||
| 505 | nodeslist[i]->subnodes = (bplist_node*)malloc(sizeof(bplist_node) * nodeslist[i]->length); // memory allocation helps a lot when storing data | ||
| 506 | |||
| 507 | for (j = 0; j < nodeslist[i]->length; j++) { | ||
| 508 | log_debug_msg("parse_nodes: array index %i\n", j); | ||
| 509 | str_j = j * nodeslist[i]->intval8; | ||
| 510 | //index1 = nodeslist[i]->strval[j]; | ||
| 511 | memcpy(&index1, nodeslist[i]->strval+str_j, nodeslist[i]->intval8); | ||
| 512 | log_debug_msg("parse_nodes: post-memcpy\n"); | ||
| 513 | //index1 = (dict_param_size == 1) ? index1 : (dict_param_size == 2) ? ntohs(index1) : (dict_param_size == 4) ? ntohl(index1) : index1; | ||
| 514 | byte_convert(&index1, (dict_param_size <= sizeof(index1)) ? dict_param_size : sizeof(index1)); | ||
| 515 | log_debug_msg("parse_nodes: post-ntohl\nindex1 = %i\n", index1); | ||
| 516 | nodeslist[i]->subnodes[j] = nodeslist[index1]; | ||
| 517 | log_debug_msg("parse_nodes: post-assignment\n"); | ||
| 518 | } | ||
| 519 | free(nodeslist[i]->strval); | ||
| 520 | break; | ||
| 521 | default: | ||
| 522 | //printf("lol... type %x\n", nodeslist[i]->type); | ||
| 523 | break; | ||
| 524 | } // those are the only two we need to correct for. | ||
| 525 | } | ||
| 526 | |||
| 527 | root_node = nodeslist[root_object]; | ||
| 528 | return root_node; | ||
| 529 | } | ||
