diff options
Diffstat (limited to 'tools')
| -rw-r--r-- | tools/Makefile.am | 11 | ||||
| -rw-r--r-- | tools/plistutil.c | 468 |
2 files changed, 347 insertions, 132 deletions
diff --git a/tools/Makefile.am b/tools/Makefile.am index 67b7dd7..5883286 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -1,7 +1,14 @@ -AM_CFLAGS = $(GLOBAL_CFLAGS) -I$(top_srcdir)/include +if BUILD_TOOLS + +AM_CFLAGS = \ + $(GLOBAL_CFLAGS) \ + -I$(top_srcdir)/include + AM_LDFLAGS = bin_PROGRAMS = plistutil plistutil_SOURCES = plistutil.c -plistutil_LDADD = $(top_builddir)/src/libplist.la +plistutil_LDADD = $(top_builddir)/src/libplist-2.0.la + +endif diff --git a/tools/plistutil.c b/tools/plistutil.c index c7f1a3f..bdf195e 100644 --- a/tools/plistutil.c +++ b/tools/plistutil.c @@ -2,8 +2,9 @@ * plistutil.c * Simple tool to convert a plist into different formats * - * Copyright (c) 2009-2015 Martin Szulecki All Rights Reserved. - * Copyright (c) 2008 Zach C. All Rights Reserved. + * Copyright (c) 2009-2020 Martin Szulecki All Rights Reserved. + * Copyright (c) 2013-2020 Nikias Bassen, All Rights Reserved. + * Copyright (c) 2008 Zach C., All Rights Reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -20,6 +21,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif #include "plist/plist.h" @@ -27,97 +31,178 @@ #include <stdlib.h> #include <string.h> #include <sys/stat.h> +#include <getopt.h> #include <errno.h> +#ifndef _MSC_VER #include <unistd.h> - -#define BUF_SIZE 2048 // Seems to be a decent start to cover most stdin files +#endif #ifdef _MSC_VER #pragma warning(disable:4996) +#define STDIN_FILENO _fileno(stdin) +#define strtok_r strtok_s #endif typedef struct _options { - char *in_file, *out_file; - uint8_t debug, in_fmt, out_fmt; // fmts 0 = undef, 1 = bin, 2 = xml, 3 = json someday + char *in_file, *out_file, *nodepath; + uint8_t in_fmt, out_fmt; // fmts 0 = undef, 1 = bin, 2 = xml, 3 = json, 4 = openstep + uint8_t flags; } options_t; +#define OPT_DEBUG (1 << 0) +#define OPT_COMPACT (1 << 1) +#define OPT_SORT (1 << 2) static void print_usage(int argc, char *argv[]) { char *name = NULL; name = strrchr(argv[0], '/'); - printf("Usage: %s -i|--infile FILE [-o|--outfile FILE] [-d|--debug]\n", (name ? name + 1: argv[0])); - printf("Convert a plist FILE from binary to XML format or vice-versa.\n\n"); + printf("Usage: %s [OPTIONS] [-i FILE] [-o FILE]\n", (name ? name + 1: argv[0])); + printf("\n"); + printf("Convert a plist FILE between binary, XML, JSON, and OpenStep format.\n"); + printf("If -f is omitted, XML plist data will be converted to binary and vice-versa.\n"); + printf("To convert to/from JSON or OpenStep the output format needs to be specified.\n"); + printf("\n"); + printf("OPTIONS:\n"); printf(" -i, --infile FILE Optional FILE to convert from or stdin if - or not used\n"); printf(" -o, --outfile FILE Optional FILE to convert to or stdout if - or not used\n"); - printf(" -f, --format [bin|xml] Force output format, regardless of input type\n"); + printf(" -f, --format FORMAT Force output format, regardless of input type\n"); + printf(" FORMAT is one of xml, bin, json, or openstep\n"); + printf(" If omitted, XML will be converted to binary,\n"); + printf(" and binary to XML.\n"); + printf(" -p, --print FILE Print the PList in human-readable format.\n"); + printf(" -n, --nodepath PATH Restrict output to nodepath defined by PATH.\n"); + printf(" -c, --compact JSON and OpenStep only: Print output in compact form.\n"); + printf(" By default, the output will be pretty-printed.\n"); + printf(" -s, --sort Sort all dictionary nodes lexicographically by key\n"); + printf(" before converting to the output format.\n"); printf(" -d, --debug Enable extended debug output\n"); + printf(" -v, --version Print version information\n"); printf("\n"); + printf("Homepage: <" PACKAGE_URL ">\n"); + printf("Bug Reports: <" PACKAGE_BUGREPORT ">\n"); } static options_t *parse_arguments(int argc, char *argv[]) { - int i = 0; + options_t *options = calloc(1, sizeof(options_t)); + if (!options) + return NULL; - options_t *options = (options_t *) malloc(sizeof(options_t)); - memset(options, 0, sizeof(options_t)); options->out_fmt = 0; - for (i = 1; i < argc; i++) + static struct option long_options[] = { + { "infile", required_argument, 0, 'i' }, + { "outfile", required_argument, 0, 'o' }, + { "format", required_argument, 0, 'f' }, + { "compact", no_argument, 0, 'c' }, + { "sort", no_argument, 0, 's' }, + { "print", required_argument, 0, 'p' }, + { "nodepath", required_argument, 0, 'n' }, + { "debug", no_argument, 0, 'd' }, + { "help", no_argument, 0, 'h' }, + { "version", no_argument, 0, 'v' }, + { 0, 0, 0, 0 } + }; + + int c; + while ((c = getopt_long(argc, argv, "i:o:f:csp:n:dhv", long_options, NULL)) != -1) { - if (!strcmp(argv[i], "--infile") || !strcmp(argv[i], "-i")) + switch (c) { - if ((i + 1) == argc) - { - free(options); - return NULL; - } - options->in_file = argv[i + 1]; - i++; - continue; - } + case 'i': + if (!optarg || optarg[0] == '\0') { + fprintf(stderr, "ERROR: --infile requires a filename or '-' for stdin\n"); + free(options); + return NULL; + } + options->in_file = optarg; + break; - if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-o")) - { - if ((i + 1) == argc) - { - free(options); - return NULL; - } - options->out_file = argv[i + 1]; - i++; - continue; - } + case 'o': + if (!optarg || optarg[0] == '\0') { + fprintf(stderr, "ERROR: --outfile requires a filename or '-' for stdout\n"); + free(options); + return NULL; + } + options->out_file = optarg; + break; - if (!strcmp(argv[i], "--format") || !strcmp(argv[i], "-f")) - { - if ((i + 1) == argc) - { - free(options); - return NULL; + case 'f': + if (!optarg || optarg[0] == '\0') { + fprintf(stderr, "ERROR: --format requires a format (bin|xml|json|openstep)\n"); + free(options); + return NULL; + } + if (!strncmp(optarg, "bin", 3)) { + options->out_fmt = PLIST_FORMAT_BINARY; + } else if (!strncmp(optarg, "xml", 3)) { + options->out_fmt = PLIST_FORMAT_XML; + } else if (!strncmp(optarg, "json", 4)) { + options->out_fmt = PLIST_FORMAT_JSON; + } else if (!strncmp(optarg, "openstep", 8) || + !strncmp(optarg, "ostep", 5)) { + options->out_fmt = PLIST_FORMAT_OSTEP; + } else { + fprintf(stderr, "ERROR: Unsupported output format\n"); + free(options); + return NULL; + } + break; + + case 'c': + options->flags |= OPT_COMPACT; + break; + + case 's': + options->flags |= OPT_SORT; + break; + + case 'p': { + if (!optarg || optarg[0] == '\0') { + fprintf(stderr, "ERROR: --print requires a filename or '-' for stdin\n"); + free(options); + return NULL; + } + options->in_file = optarg; + options->out_fmt = PLIST_FORMAT_PRINT; + + char *env_fmt = getenv("PLIST_OUTPUT_FORMAT"); + if (env_fmt) { + if (!strcmp(env_fmt, "plutil")) { + options->out_fmt = PLIST_FORMAT_PLUTIL; + } else if (!strcmp(env_fmt, "limd")) { + options->out_fmt = PLIST_FORMAT_LIMD; + } + } + break; } - if (!strncmp(argv[i+1], "bin", 3)) { - options->out_fmt = 1; - } else if (!strncmp(argv[i+1], "xml", 3)) { - options->out_fmt = 2; - } else { - printf("ERROR: Unsupported output format\n"); + + case 'n': + if (!optarg || optarg[0] == '\0') { + fprintf(stderr, "ERROR: --extract needs a node path\n"); + free(options); + return NULL; + } + options->nodepath = optarg; + break; + + case 'd': + options->flags |= OPT_DEBUG; + break; + + case 'h': free(options); return NULL; - } - i++; - continue; - } - if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d")) - { - options->debug = 1; - } + case 'v': + printf("plistutil %s\n", libplist_version()); + exit(EXIT_SUCCESS); - if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) - { - free(options); - return NULL; + default: + fprintf(stderr, "ERROR: Invalid option\n"); + free(options); + return NULL; } } @@ -126,11 +211,15 @@ static options_t *parse_arguments(int argc, char *argv[]) int main(int argc, char *argv[]) { + int ret = 0; + int input_res = PLIST_ERR_UNKNOWN; + int output_res = PLIST_ERR_UNKNOWN; FILE *iplist = NULL; plist_t root_node = NULL; char *plist_out = NULL; uint32_t size = 0; - int read_size = 0; + size_t read_size = 0; + size_t read_capacity = 4096; char *plist_entire = NULL; struct stat filestats; options_t *options = parse_arguments(argc, argv); @@ -141,58 +230,72 @@ int main(int argc, char *argv[]) return 0; } + if (options->flags & OPT_DEBUG) + { + plist_set_debug(1); + } + if (!options->in_file || !strcmp(options->in_file, "-")) { - read_size = 0; - plist_entire = malloc(sizeof(char) * BUF_SIZE); + read_size = 0; + plist_entire = malloc(sizeof(char) * read_capacity); if(plist_entire == NULL) { - printf("ERROR: Failed to allocate buffer to read from stdin"); - free(options); + fprintf(stderr, "ERROR: Failed to allocate buffer to read from stdin\n"); + free(options); return 1; } - plist_entire[read_size] = '\0'; - char ch; - while(read(STDIN_FILENO, &ch, 1) > 0) - { - if (read_size >= BUF_SIZE) { - char *old = plist_entire; - plist_entire = realloc(plist_entire, sizeof(char) * (read_size + 1)); - if (plist_entire == NULL) - { - printf("ERROR: Failed to reallocate stdin buffer\n"); - free(old); - free(options); - return 1; + plist_entire[read_size] = '\0'; + char buf[4096]; + ssize_t n; + + while (1) { + n = read(STDIN_FILENO, buf, sizeof(buf)); + if (n > 0) { + size_t needed = read_size + (size_t)n + 1; + if (needed > read_capacity) { + size_t newcap = read_capacity ? read_capacity : 4096; + while (newcap < needed) { + newcap *= 2; + } + + char *tmp = realloc(plist_entire, newcap); + if (!tmp) { + fprintf(stderr, "ERROR: Failed to reallocate stdin buffer\n"); + free(plist_entire); + free(options); + return 1; + } + plist_entire = tmp; + read_capacity = newcap; } + + memcpy(plist_entire + read_size, buf, (size_t)n); + read_size += (size_t)n; + continue; } - plist_entire[read_size] = ch; - read_size++; - } - plist_entire[read_size] = '\0'; - // Not positive we need this, but it doesnt seem to hurt lol - if(ferror(stdin)) - { - printf("ERROR: reading from stdin.\n"); + if (n == 0) { // EOF + break; + } + + // n < 0: error + if (errno == EINTR) + continue; + + fprintf(stderr, "ERROR: Failed to read from stdin\n"); free(plist_entire); free(options); return 1; } - - if (read_size < 8) { - printf("ERROR: Input file is too small to contain valid plist data.\n"); - free(plist_entire); - free(options); - return 1; - } + plist_entire[read_size] = '\0'; } else { - // read input file - iplist = fopen(options->in_file, "rb"); + // read input file + iplist = fopen(options->in_file, "rb"); if (!iplist) { - printf("ERROR: Could not open input file '%s': %s\n", options->in_file, strerror(errno)); + fprintf(stderr, "ERROR: Could not open input file '%s': %s\n", options->in_file, strerror(errno)); free(options); return 1; } @@ -200,55 +303,116 @@ int main(int argc, char *argv[]) memset(&filestats, '\0', sizeof(struct stat)); fstat(fileno(iplist), &filestats); - if (filestats.st_size < 8) { - printf("ERROR: Input file is too small to contain valid plist data.\n"); + plist_entire = (char *) malloc(sizeof(char) * (filestats.st_size + 1)); + if(plist_entire == NULL) + { + fprintf(stderr, "ERROR: Failed to allocate buffer to read from file\n"); free(options); - fclose(iplist); - return -1; + return 1; } - - plist_entire = (char *) malloc(sizeof(char) * (filestats.st_size + 1)); read_size = fread(plist_entire, sizeof(char), filestats.st_size, iplist); + plist_entire[read_size] = '\0'; fclose(iplist); } if (options->out_fmt == 0) { - // convert from binary to xml or vice-versa<br> + // convert from binary to xml or vice-versa if (plist_is_binary(plist_entire, read_size)) { - plist_from_bin(plist_entire, read_size, &root_node); - plist_to_xml(root_node, &plist_out, &size); + input_res = plist_from_bin(plist_entire, read_size, &root_node); + if (input_res == PLIST_ERR_SUCCESS) { + if (options->flags & OPT_SORT) { + plist_sort(root_node); + } + output_res = plist_to_xml(root_node, &plist_out, &size); + } } else { - plist_from_xml(plist_entire, read_size, &root_node); - plist_to_bin(root_node, &plist_out, &size); + input_res = plist_from_xml(plist_entire, read_size, &root_node); + if (input_res == PLIST_ERR_SUCCESS) { + if (options->flags & OPT_SORT) { + plist_sort(root_node); + } + output_res = plist_to_bin(root_node, &plist_out, &size); + } } } else { - if (options->out_fmt == 1) { - if (plist_is_binary(plist_entire, read_size)) - { - plist_out = malloc(sizeof(char) * read_size); - memcpy(plist_out, plist_entire, read_size); - size = read_size; - } - else - { - plist_from_xml(plist_entire, read_size, &root_node); - plist_to_bin(root_node, &plist_out, &size); + input_res = plist_from_memory(plist_entire, read_size, &root_node, NULL); + if (input_res == PLIST_ERR_SUCCESS) { + + if (options->nodepath) { + char *copy = strdup(options->nodepath); + char *tok, *saveptr = NULL; + if (!copy) { + plist_free(root_node); + free(plist_entire); + free(options); + return 1; + } + + plist_t current = root_node; + for (tok = strtok_r(copy, "/", &saveptr); tok; tok = strtok_r(NULL, "/", &saveptr)) { + if (*tok == '\0') continue; + switch (plist_get_node_type(current)) { + case PLIST_DICT: + current = plist_dict_get_item(current, tok); + break; + case PLIST_ARRAY: { + char* endp = NULL; + uint32_t idx = strtoul(tok, &endp, 10); + if (endp == tok || *endp != '\0') { + current = NULL; + break; + } + if (idx >= plist_array_get_size(current)) { + current = NULL; + break; + } + current = plist_array_get_item(current, idx); + break; + } + default: + current = NULL; + break; + } + if (!current) { + break; + } + } + free(copy); + if (current) { + plist_t destnode = plist_copy(current); + plist_free(root_node); + root_node = destnode; + } else { + fprintf(stderr, "ERROR: nodepath '%s' is invalid\n", options->nodepath); + plist_free(root_node); + free(plist_entire); + free(options); + return 1; + } } - } else if (options->out_fmt == 2) { - if (plist_is_binary(plist_entire, read_size)) { - plist_from_bin(plist_entire, read_size, &root_node); - plist_to_xml(root_node, &plist_out, &size); + + if (options->flags & OPT_SORT) { + plist_sort(root_node); } - else - { - plist_out = malloc(sizeof(char) * read_size); - memcpy(plist_out, plist_entire, read_size); - size = read_size; + if (options->out_fmt == PLIST_FORMAT_BINARY) { + output_res = plist_to_bin(root_node, &plist_out, &size); + } else if (options->out_fmt == PLIST_FORMAT_XML) { + output_res = plist_to_xml(root_node, &plist_out, &size); + } else if (options->out_fmt == PLIST_FORMAT_JSON) { + output_res = plist_to_json(root_node, &plist_out, &size, !(options->flags & OPT_COMPACT)); + } else if (options->out_fmt == PLIST_FORMAT_OSTEP) { + output_res = plist_to_openstep(root_node, &plist_out, &size, !(options->flags & OPT_COMPACT)); + } else { + plist_write_to_stream(root_node, stdout, options->out_fmt, PLIST_OPT_PARTIAL_DATA); + plist_free(root_node); + free(plist_entire); + free(options); + return 0; } } } @@ -257,11 +421,11 @@ int main(int argc, char *argv[]) if (plist_out) { - if (options->out_file != NULL && strcmp(options->out_file, "-")) + if (options->out_file != NULL && strcmp(options->out_file, "-") != 0) { FILE *oplist = fopen(options->out_file, "wb"); if (!oplist) { - printf("ERROR: Could not open output file '%s': %s\n", options->out_file, strerror(errno)); + fprintf(stderr, "ERROR: Could not open output file '%s': %s\n", options->out_file, strerror(errno)); free(options); return 1; } @@ -274,9 +438,53 @@ int main(int argc, char *argv[]) free(plist_out); } - else - printf("ERROR: Failed to convert input file.\n"); + + if (input_res == PLIST_ERR_SUCCESS) { + switch (output_res) { + case PLIST_ERR_SUCCESS: + break; + case PLIST_ERR_CIRCULAR_REF: + fprintf(stderr, "ERROR: Circular reference detected.\n"); + ret = 5; + break; + case PLIST_ERR_MAX_NESTING: + fprintf(stderr, "ERROR: Output plist data exceeds maximum nesting depth.\n"); + ret = 4; + break; + case PLIST_ERR_FORMAT: + fprintf(stderr, "ERROR: Input plist data is not compatible with output format.\n"); + ret = 2; + break; + default: + fprintf(stderr, "ERROR: Failed to convert plist data (%d)\n", output_res); + ret = 1; + break; + } + } else { + switch (input_res) { + case PLIST_ERR_PARSE: + if (options->out_fmt == 0) { + fprintf(stderr, "ERROR: Could not parse plist data, expected XML or binary plist\n"); + } else { + fprintf(stderr, "ERROR: Could not parse plist data (%d)\n", input_res); + } + ret = 3; + break; + case PLIST_ERR_CIRCULAR_REF: + fprintf(stderr, "ERROR: Circular reference detected in input plist data.\n"); + ret = 5; + break; + case PLIST_ERR_MAX_NESTING: + fprintf(stderr, "ERROR: Input plist data exceeds maximum nesting depth.\n"); + ret = 4; + break; + default: + fprintf(stderr, "ERROR: Could not parse plist data (%d)\n", input_res); + ret = 1; + break; + } + } free(options); - return 0; + return ret; } |
