summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--configure.ac1
-rw-r--r--dev/Makefile.am6
-rw-r--r--dev/plutil.c118
-rw-r--r--dev/plutil.h13
-rw-r--r--src/AFC.c1
-rw-r--r--src/Makefile.am6
-rw-r--r--src/bplist.c793
-rw-r--r--src/lockdown.c130
-rw-r--r--src/lockdown.h1
-rw-r--r--src/plist.c251
-rw-r--r--src/plist.h83
-rw-r--r--src/xplist.c312
12 files changed, 76 insertions, 1639 deletions
diff --git a/configure.ac b/configure.ac
index 286b1d8..9516ec4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -20,6 +20,7 @@ PKG_CHECK_MODULES(libglib2, glib-2.0 >= 2.14.1)
20PKG_CHECK_MODULES(libgthread2, gthread-2.0 >= 2.14.1) 20PKG_CHECK_MODULES(libgthread2, gthread-2.0 >= 2.14.1)
21PKG_CHECK_MODULES(libgnutls, gnutls >= 1.6.3 gnutls <= 2.5.0 ) 21PKG_CHECK_MODULES(libgnutls, gnutls >= 1.6.3 gnutls <= 2.5.0 )
22PKG_CHECK_MODULES(libtasn1, libtasn1 >= 1.1) 22PKG_CHECK_MODULES(libtasn1, libtasn1 >= 1.1)
23PKG_CHECK_MODULES(libplist, libplist-1.0 >= 0.1.0)
23 24
24# Checks for header files. 25# Checks for header files.
25AC_HEADER_STDC 26AC_HEADER_STDC
diff --git a/dev/Makefile.am b/dev/Makefile.am
index 95b4d61..d116581 100644
--- a/dev/Makefile.am
+++ b/dev/Makefile.am
@@ -3,7 +3,7 @@ INCLUDES = -I$(top_srcdir)/include
3AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) -g 3AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) -g
4AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) 4AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS)
5 5
6bin_PROGRAMS = iphoneclient lckd-client afccheck plutil 6bin_PROGRAMS = iphoneclient lckd-client afccheck
7 7
8iphoneclient_SOURCES = main.c 8iphoneclient_SOURCES = main.c
9iphoneclient_LDADD = ../src/libiphone.la 9iphoneclient_LDADD = ../src/libiphone.la
@@ -18,7 +18,3 @@ afccheck_CFLAGS = $(AM_CFLAGS)
18afccheck_LDFLAGS = $(AM_LDFLAGS) 18afccheck_LDFLAGS = $(AM_LDFLAGS)
19afccheck_LDADD = ../src/libiphone.la 19afccheck_LDADD = ../src/libiphone.la
20 20
21plutil_SOURCES = plutil.c
22plutil_CFLAGS = $(AM_CFLAGS)
23plutil_LDFLAGS = $(AM_LDFLAGS)
24plutil_LDADD = ../src/libiphone.la
diff --git a/dev/plutil.c b/dev/plutil.c
deleted file mode 100644
index 3d93797..0000000
--- a/dev/plutil.c
+++ /dev/null
@@ -1,118 +0,0 @@
1/*
2 * main.c for plistutil
3 * right now just prints debug shit
4 */
5
6#include "../src/plist.h"
7#include "plutil.h"
8#include <glib.h>
9#include <string.h>
10#include <stdio.h>
11#include <stdlib.h>
12
13
14int main(int argc, char *argv[])
15{
16 struct stat *filestats = (struct stat *) malloc(sizeof(struct stat));
17 Options *options = parse_arguments(argc, argv);
18
19 if (!options) {
20 print_usage();
21 return 0;
22 }
23
24 iphone_set_debug(options->debug);
25
26 //read input file
27 FILE *iplist = fopen(options->in_file, "r");
28 if (!iplist)
29 return 1;
30 stat(options->in_file, filestats);
31 char *plist_entire = (char *) malloc(sizeof(char) * (filestats->st_size + 1));
32 fread(plist_entire, sizeof(char), filestats->st_size, iplist);
33 fclose(iplist);
34
35
36 //convert one format to another
37 plist_t root_node = NULL;
38 char *plist_out = NULL;
39 int size = 0;
40
41 if (memcmp(plist_entire, "bplist00", 8) == 0) {
42 bin_to_plist(plist_entire, filestats->st_size, &root_node);
43 plist_to_xml(root_node, &plist_out, &size);
44 } else {
45 xml_to_plist(plist_entire, filestats->st_size, &root_node);
46 plist_to_bin(root_node, &plist_out, &size);
47 }
48
49 if (plist_out) {
50 if (options->out_file != NULL) {
51 FILE *oplist = fopen(options->out_file, "wb");
52 if (!oplist)
53 return 1;
54 fwrite(plist_out, size, sizeof(char), oplist);
55 fclose(oplist);
56 }
57 //if no output file specified, write to stdout
58 else
59 fwrite(plist_out, size, sizeof(char), stdout);
60 } else
61 printf("ERROR\n");
62 return 0;
63}
64
65Options *parse_arguments(int argc, char *argv[])
66{
67 int i = 0;
68
69 Options *options = (Options *) malloc(sizeof(Options));
70 memset(options, 0, sizeof(Options));
71
72 for (i = 1; i < argc; i++) {
73 if (!strcmp(argv[i], "--infile") || !strcmp(argv[i], "-i")) {
74 if ((i + 1) == argc) {
75 free(options);
76 return NULL;
77 }
78 options->in_file = argv[i + 1];
79 i++;
80 continue;
81 }
82
83 if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-o")) {
84 if ((i + 1) == argc) {
85 free(options);
86 return NULL;
87 }
88 options->out_file = argv[i + 1];
89 i++;
90 continue;
91 }
92
93 if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d") || !strcmp(argv[i], "-v")) {
94 options->debug = 1;
95 }
96
97 if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
98 free(options);
99 return NULL;
100 }
101 }
102
103 if (!options->in_file /*|| !options->out_file */ ) {
104 free(options);
105 return NULL;
106 }
107
108 return options;
109}
110
111void print_usage()
112{
113 printf("Usage: plistutil -i|--infile in_file.plist -o|--outfile out_file.plist [--debug]\n");
114 printf("\n");
115 printf("\t-i or --infile: The file to read in.\n");
116 printf("\t-o or --outfile: The file to convert to.\n");
117 printf("\t-d, -v or --debug: Provide extended debug information.\n\n");
118}
diff --git a/dev/plutil.h b/dev/plutil.h
deleted file mode 100644
index 2146307..0000000
--- a/dev/plutil.h
+++ /dev/null
@@ -1,13 +0,0 @@
1
2/*
3 * main.h - header for plistutil
4 * Written by FxChiP
5 */
6
7typedef struct _options {
8 char *in_file, *out_file;
9 uint8_t debug, in_fmt, out_fmt;
10} Options;
11
12Options *parse_arguments(int argc, char *argv[]);
13void print_usage();
diff --git a/src/AFC.c b/src/AFC.c
index 899bd47..aefb971 100644
--- a/src/AFC.c
+++ b/src/AFC.c
@@ -21,7 +21,6 @@
21 21
22#include <stdio.h> 22#include <stdio.h>
23#include "AFC.h" 23#include "AFC.h"
24#include "plist.h"
25 24
26 25
27// This is the maximum size an AFC data packet can be 26// This is the maximum size an AFC data packet can be
diff --git a/src/Makefile.am b/src/Makefile.am
index 82fd924..2514367 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1,7 +1,7 @@
1INCLUDES = -I$(top_srcdir)/include 1INCLUDES = -I$(top_srcdir)/include
2 2
3AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) -g 3AM_CFLAGS = $(libxml2_CFLAGS) $(libusb_CFLAGS) $(libglib2_CFLAGS) $(libgnutls_CFLAGS) $(libtasn1_CFLAGS) $(libgthread2_CFLAGS) $(libplist_CFLAGS) -g
4AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) 4AM_LDFLAGS = $(libxml2_LIBS) $(libusb_LIBS) $(libglib2_LIBS) $(libgnutls_LIBS) $(libtasn1_LIBS) $(libgthread2_LIBS) $(libplist_LIBS)
5 5
6bin_PROGRAMS = libiphone-initconf 6bin_PROGRAMS = libiphone-initconf
7 7
@@ -12,4 +12,4 @@ libiphone_initconf_LDFLAGS = $(libgthread2_LIBS) $(AM_LDFLAGS)
12 12
13 13
14lib_LTLIBRARIES = libiphone.la 14lib_LTLIBRARIES = libiphone.la
15libiphone_la_SOURCES = usbmux.c iphone.c plist.c bplist.c xplist.c lockdown.c AFC.c userpref.c utils.c 15libiphone_la_SOURCES = usbmux.c iphone.c lockdown.c AFC.c userpref.c utils.c
diff --git a/src/bplist.c b/src/bplist.c
deleted file mode 100644
index a5b1c9b..0000000
--- a/src/bplist.c
+++ /dev/null
@@ -1,793 +0,0 @@
1/*
2 * plist.c
3 * Binary plist implementation
4 *
5 * Copyright (c) 2008 Jonathan Beck All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23#include "plist.h"
24#include <wchar.h>
25#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28
29/* Magic marker and size. */
30#define BPLIST_MAGIC "bplist"
31#define BPLIST_MAGIC_SIZE 6
32
33#define BPLIST_VERSION "00"
34#define BPLIST_VERSION_SIZE 2
35
36
37#define BPLIST_TRL_SIZE 26
38#define BPLIST_TRL_OFFSIZE_IDX 0
39#define BPLIST_TRL_PARMSIZE_IDX 1
40#define BPLIST_TRL_NUMOBJ_IDX 2
41#define BPLIST_TRL_ROOTOBJ_IDX 10
42#define BPLIST_TRL_OFFTAB_IDX 18
43
44enum {
45 BPLIST_NULL = 0x00,
46 BPLIST_TRUE = 0x08,
47 BPLIST_FALSE = 0x09,
48 BPLIST_FILL = 0x0F, /* will be used for length grabbing */
49 BPLIST_UINT = 0x10,
50 BPLIST_REAL = 0x20,
51 BPLIST_DATE = 0x30,
52 BPLIST_DATA = 0x40,
53 BPLIST_STRING = 0x50,
54 BPLIST_UNICODE = 0x60,
55 BPLIST_UID = 0x70,
56 BPLIST_ARRAY = 0xA0,
57 BPLIST_SET = 0xC0,
58 BPLIST_DICT = 0xD0,
59 BPLIST_MASK = 0xF0
60};
61
62void byte_convert(char *address, size_t size)
63{
64 int i = 0, j = 0;
65 char tmp = '\0';
66
67 for (i = 0; i < (size / 2); i++) {
68 tmp = address[i];
69 j = ((size - 1) + 0) - i;
70 address[i] = address[j];
71 address[j] = tmp;
72 }
73}
74
75#include <byteswap.h>
76#define swap_n_bytes(x, n) \
77 n == 8 ? bswap_64(*(uint64_t *)(x)) : \
78 (n == 4 ? bswap_32(*(uint32_t *)(x)) : \
79 (n == 2 ? bswap_16(*(uint16_t *)(x)) : *(x) ))
80
81#define be64dec(x) bswap_64( *(uint64_t*)(x) )
82
83#define get_needed_bytes(x) (x <= 1<<8 ? 1 : ( x <= 1<<16 ? 2 : ( x <= 1<<32 ? 4 : 8)))
84#define get_real_bytes(x) (x >> 32 ? 4 : 8)
85
86GNode *parse_uint_node(char *bnode, uint8_t size, char **next_object)
87{
88 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
89
90 size = 1 << size; // make length less misleading
91 switch (size) {
92 case sizeof(uint8_t):
93 data->intval = bnode[0];
94 break;
95 case sizeof(uint16_t):
96 memcpy(&data->intval, bnode, size);
97 data->intval = ntohs(data->intval);
98 break;
99 case sizeof(uint32_t):
100 memcpy(&data->intval, bnode, size);
101 data->intval = ntohl(data->intval);
102 break;
103 case sizeof(uint64_t):
104 memcpy(&data->intval, bnode, size);
105 byte_convert((char *) &data->intval, size);
106 break;
107 default:
108 free(data);
109 return NULL;
110 };
111
112 *next_object = bnode + size;
113 data->type = PLIST_UINT;
114 return g_node_new(data);
115}
116
117GNode *parse_real_node(char *bnode, uint8_t size)
118{
119 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
120
121 size = 1 << size; // make length less misleading
122 switch (size) {
123 case sizeof(float):
124 memcpy(&data->realval, bnode, size);
125 byte_convert((char *) &data->realval, size);
126 break;
127 case sizeof(double):
128 memcpy(&data->realval, bnode, size);
129 byte_convert((char *) &data->realval, size);
130 break;
131 default:
132 free(data);
133 return NULL;
134 }
135 data->type = PLIST_REAL;
136 return g_node_new(data);
137}
138
139GNode *parse_string_node(char *bnode, uint8_t size)
140{
141 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
142
143 data->type = PLIST_STRING;
144 data->strval = (char *) malloc(sizeof(char) * (size + 1));
145 memcpy(data->strval, bnode, size);
146 data->strval[size] = '\0';
147
148 return g_node_new(data);
149}
150
151GNode *parse_unicode_node(char *bnode, uint8_t size)
152{
153 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
154
155 data->type = PLIST_UNICODE;
156 data->unicodeval = (wchar_t *) malloc(sizeof(wchar_t) * (size + 1));
157 memcpy(data->unicodeval, bnode, size);
158 data->unicodeval[size] = '\0';
159
160 return g_node_new(data);
161}
162
163GNode *parse_data_node(char *bnode, uint64_t size, uint32_t ref_size)
164{
165 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
166
167 data->type = PLIST_DATA;
168 data->length = size;
169 data->buff = (char *) malloc(sizeof(char) * size);
170 memcpy(data->buff, bnode, sizeof(char) * size);
171
172 return g_node_new(data);
173}
174
175GNode *parse_dict_node(char *bnode, uint64_t size, uint32_t ref_size)
176{
177 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
178
179 data->type = PLIST_DICT;
180 data->length = size;
181 data->buff = (char *) malloc(sizeof(char) * size * ref_size * 2);
182 memcpy(data->buff, bnode, sizeof(char) * size * ref_size * 2);
183
184 return g_node_new(data);
185}
186
187GNode *parse_array_node(char *bnode, uint64_t size, uint32_t ref_size)
188{
189 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
190
191 data->type = PLIST_ARRAY;
192 data->length = size;
193 data->buff = (char *) malloc(sizeof(char) * size * ref_size);
194 memcpy(data->buff, bnode, sizeof(char) * size * ref_size);
195
196 return g_node_new(data);
197}
198
199
200
201GNode *parse_bin_node(char *object, uint8_t dict_size, char **next_object)
202{
203 if (!object)
204 return NULL;
205
206 uint16_t type = *object & 0xF0;
207 uint64_t size = *object & 0x0F;
208 object++;
209
210 switch (type) {
211
212 case BPLIST_NULL:
213 switch (size) {
214
215 case BPLIST_TRUE:
216 {
217 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
218 data->type = PLIST_BOOLEAN;
219 data->boolval = TRUE;
220 return g_node_new(data);
221 }
222
223 case BPLIST_FALSE:
224 {
225 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
226 data->type = PLIST_BOOLEAN;
227 data->boolval = FALSE;
228 return g_node_new(data);
229 }
230
231 case BPLIST_NULL:
232 default:
233 return NULL;
234 }
235
236 case BPLIST_UINT:
237 return parse_uint_node(object, size, next_object);
238
239 case BPLIST_REAL:
240 return parse_real_node(object, size);
241
242 case BPLIST_DATE:
243 if (3 != size)
244 return NULL;
245 else
246 return parse_real_node(object, size);
247
248 case BPLIST_DATA:
249 if (0x0F == size) {
250 plist_t size_node = parse_bin_node(object, dict_size, &object);
251 if (plist_get_node_type(size_node) != PLIST_UINT)
252 return NULL;
253 size = plist_get_node_uint_val(size_node);
254 }
255 return parse_data_node(object, size, dict_size);
256
257 case BPLIST_STRING:
258 if (0x0F == size) {
259 plist_t size_node = parse_bin_node(object, dict_size, &object);
260 if (plist_get_node_type(size_node) != PLIST_UINT)
261 return NULL;
262 size = plist_get_node_uint_val(size_node);
263 }
264 return parse_string_node(object, size);
265
266 case BPLIST_UNICODE:
267 if (0x0F == size) {
268 plist_t size_node = parse_bin_node(object, dict_size, &object);
269 if (plist_get_node_type(size_node) != PLIST_UINT)
270 return NULL;
271 size = plist_get_node_uint_val(size_node);
272 }
273 return parse_unicode_node(object, size);
274
275 case BPLIST_UID:
276 case BPLIST_ARRAY:
277 if (0x0F == size) {
278 plist_t size_node = parse_bin_node(object, dict_size, &object);
279 if (plist_get_node_type(size_node) != PLIST_UINT)
280 return NULL;
281 size = plist_get_node_uint_val(size_node);
282 }
283 return parse_array_node(object, size, dict_size);
284
285 case BPLIST_SET:
286 case BPLIST_DICT:
287 if (0x0F == size) {
288 plist_t size_node = parse_bin_node(object, dict_size, &object);
289 if (plist_get_node_type(size_node) != PLIST_UINT)
290 return NULL;
291 object++;
292 size = plist_get_node_uint_val(size_node);
293 }
294 return parse_dict_node(object, size, dict_size);
295
296 }
297 return NULL;
298}
299
300gpointer copy_plist_data(gconstpointer src, gpointer data)
301{
302 struct plist_data *srcdata = (struct plist_data *) src;
303 struct plist_data *dstdata = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
304
305 dstdata->type = srcdata->type;
306 dstdata->length = srcdata->length;
307 switch (dstdata->type) {
308 case PLIST_BOOLEAN:
309 dstdata->boolval = srcdata->boolval;
310 break;
311 case PLIST_UINT:
312 dstdata->intval = srcdata->intval;
313 break;
314 case PLIST_DATE:
315 case PLIST_REAL:
316 dstdata->realval = srcdata->realval;
317 break;
318 case PLIST_KEY:
319 case PLIST_STRING:
320 dstdata->strval = strdup(srcdata->strval);
321 break;
322 case PLIST_UNICODE:
323 dstdata->unicodeval = wcsdup(srcdata->unicodeval);
324 break;
325 case PLIST_DATA:
326 case PLIST_ARRAY:
327 case PLIST_DICT:
328 dstdata->buff = (char *) malloc(sizeof(char *) * srcdata->length);
329 memcpy(dstdata->buff, srcdata->buff, sizeof(char *) * srcdata->length);
330 break;
331
332 default:
333 break;
334 }
335
336 return dstdata;
337}
338
339void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist)
340{
341 //first check we have enough data
342 if (!(length >= BPLIST_MAGIC_SIZE + BPLIST_VERSION_SIZE + BPLIST_TRL_SIZE))
343 return;
344 //check that plist_bin in actually a plist
345 if (memcmp(plist_bin, BPLIST_MAGIC, BPLIST_MAGIC_SIZE) != 0)
346 return;
347 //check for known version
348 if (memcmp(plist_bin + BPLIST_MAGIC_SIZE, BPLIST_VERSION, BPLIST_VERSION_SIZE) != 0)
349 return;
350
351 //now parse trailer
352 const char *trailer = plist_bin + (length - BPLIST_TRL_SIZE);
353
354 uint8_t offset_size = trailer[BPLIST_TRL_OFFSIZE_IDX];
355 uint8_t dict_param_size = trailer[BPLIST_TRL_PARMSIZE_IDX];
356 uint64_t num_objects = be64dec(trailer + BPLIST_TRL_NUMOBJ_IDX);
357 uint64_t root_object = be64dec(trailer + BPLIST_TRL_ROOTOBJ_IDX);
358 uint64_t offset_table_index = be64dec(trailer + BPLIST_TRL_OFFTAB_IDX);
359
360 log_debug_msg("Offset size: %i\n", offset_size);
361 log_debug_msg("Ref size: %i\n", dict_param_size);
362 log_debug_msg("Number of objects: %lli\n", num_objects);
363 log_debug_msg("Root object index: %lli\n", root_object);
364 log_debug_msg("Offset table index: %lli\n", offset_table_index);
365
366 if (num_objects == 0)
367 return;
368
369 //allocate serialized array of nodes
370 plist_t *nodeslist = NULL;
371 nodeslist = (plist_t *) malloc(sizeof(plist_t) * num_objects);
372
373 if (!nodeslist)
374 return;
375
376 //parse serialized nodes
377 uint64_t i = 0;
378 uint64_t current_offset = 0;
379 const char *offset_table = plist_bin + offset_table_index;
380 for (i = 0; i < num_objects; i++) {
381 current_offset = swap_n_bytes(offset_table + i * offset_size, offset_size);
382
383 log_debug_msg("parse_nodes: current_offset = %i\n", current_offset);
384 char *obj = plist_bin + current_offset;
385 nodeslist[i] = parse_bin_node(obj, dict_param_size, &obj);
386 log_debug_msg("parse_nodes: parse_raw_node done\n");
387 }
388
389 //setup children for structured types
390 int j = 0, str_i = 0, str_j = 0;
391 uint32_t index1 = 0, index2 = 0;
392
393 for (i = 0; i < num_objects; i++) {
394
395 log_debug_msg("parse_nodes: on node %i\n", i);
396 struct plist_data *data = (struct plist_data *) nodeslist[i]->data;
397
398 switch (data->type) {
399 case PLIST_DICT:
400 log_debug_msg("parse_nodes: dictionary found\n");
401 for (j = 0; j < data->length; j++) {
402 str_i = j * dict_param_size;
403 str_j = (j + data->length) * dict_param_size;
404
405 index1 = swap_n_bytes(data->buff + str_i, dict_param_size);
406 index2 = swap_n_bytes(data->buff + str_j, dict_param_size);
407
408 //first one is actually a key
409 ((struct plist_data *) nodeslist[index1]->data)->type = PLIST_KEY;
410
411 if (G_NODE_IS_ROOT(nodeslist[index1]))
412 g_node_append(nodeslist[i], nodeslist[index1]);
413 else
414 g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL));
415
416 if (G_NODE_IS_ROOT(nodeslist[index2]))
417 g_node_append(nodeslist[i], nodeslist[index2]);
418 else
419 g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index2], copy_plist_data, NULL));
420 }
421
422 free(data->buff);
423 break;
424
425 case PLIST_ARRAY:
426 log_debug_msg("parse_nodes: array found\n");
427 for (j = 0; j < data->length; j++) {
428 str_j = j * dict_param_size;
429 index1 = swap_n_bytes(data->buff + str_j, dict_param_size);
430
431 //g_node_append(nodeslist[i], nodeslist[index1]);
432 if (G_NODE_IS_ROOT(nodeslist[index1]))
433 g_node_append(nodeslist[i], nodeslist[index1]);
434 else
435 g_node_append(nodeslist[i], g_node_copy_deep(nodeslist[index1], copy_plist_data, NULL));
436 }
437 free(data->buff);
438 break;
439 default:
440 break;
441 }
442 }
443
444 *plist = nodeslist[root_object];
445}
446
447guint plist_data_hash(gconstpointer key)
448{
449 struct plist_data *data = (struct plist_data *) ((GNode *) key)->data;
450
451 guint hash = data->type;
452 guint i = 0;
453
454 char *buff = NULL;
455 guint size = 0;
456
457 switch (data->type) {
458 case PLIST_BOOLEAN:
459 case PLIST_UINT:
460 case PLIST_REAL:
461 buff = (char *) &data->intval;
462 size = 8;
463 break;
464 case PLIST_KEY:
465 case PLIST_STRING:
466 buff = data->strval;
467 size = strlen(buff);
468 break;
469 case PLIST_UNICODE:
470 buff = data->unicodeval;
471 size = strlen(buff) * sizeof(wchar_t);
472 break;
473 case PLIST_DATA:
474 case PLIST_ARRAY:
475 case PLIST_DICT:
476 //for these types only hash pointer
477 buff = &key;
478 size = sizeof(gconstpointer);
479 break;
480 case PLIST_DATE:
481 default:
482 break;
483 }
484
485 //now perform hash
486 for (i = 0; i < size; buff++, i++)
487 hash = hash << 7 ^ (*buff);
488
489 return hash;
490}
491
492gboolean plist_data_compare(gconstpointer a, gconstpointer b)
493{
494 if (!a || !b)
495 return FALSE;
496
497 if (!((GNode *) a)->data || !((GNode *) b)->data)
498 return FALSE;
499
500 struct plist_data *val_a = (struct plist_data *) ((GNode *) a)->data;
501 struct plist_data *val_b = (struct plist_data *) ((GNode *) b)->data;
502
503 if (val_a->type != val_b->type)
504 return FALSE;
505
506 switch (val_a->type) {
507 case PLIST_BOOLEAN:
508 case PLIST_UINT:
509 case PLIST_REAL:
510 if (val_a->intval == val_b->intval) //it is an union so this is sufficient
511 return TRUE;
512 else
513 return FALSE;
514
515 case PLIST_KEY:
516 case PLIST_STRING:
517 if (!strcmp(val_a->strval, val_b->strval))
518 return TRUE;
519 else
520 return FALSE;
521 case PLIST_UNICODE:
522 if (!strcmp(val_a->unicodeval, val_b->unicodeval))
523 return TRUE;
524 else
525 return FALSE;
526
527 case PLIST_DATA:
528 case PLIST_ARRAY:
529 case PLIST_DICT:
530 //compare pointer
531 if (a == b)
532 return TRUE;
533 else
534 return FALSE;
535 break;
536 case PLIST_DATE:
537 default:
538 break;
539 }
540 return FALSE;
541}
542
543struct serialize_s {
544 GPtrArray *objects;
545 GHashTable *ref_table;
546};
547
548void serialize_plist(GNode * node, gpointer data)
549{
550 struct serialize_s *ser = (struct serialize_s *) data;
551 uint64_t current_index = ser->objects->len;
552
553 //first check that node is not yet in objects
554 gpointer val = g_hash_table_lookup(ser->ref_table, node);
555 if (val) {
556 //data is already in table
557 return;
558 }
559 //insert new ref
560 g_hash_table_insert(ser->ref_table, node, GUINT_TO_POINTER(current_index));
561
562 //now append current node to object array
563 g_ptr_array_add(ser->objects, node);
564
565 //now recurse on children
566 g_node_children_foreach(node, G_TRAVERSE_ALL, serialize_plist, data);
567 return;
568}
569
570#define Log2(x) (x == 8 ? 3 : (x == 4 ? 2 : (x == 2 ? 1 : 0)))
571
572void write_int(GByteArray * bplist, uint64_t val)
573{
574 uint64_t size = get_needed_bytes(val);
575 uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size);
576 buff[0] = BPLIST_UINT | Log2(size);
577 memcpy(buff + 1, &val, size);
578 byte_convert(buff + 1, size);
579 g_byte_array_append(bplist, buff, sizeof(uint8_t) + size);
580 free(buff);
581}
582
583void write_real(GByteArray * bplist, double val)
584{
585 uint64_t size = get_real_bytes(*((uint64_t *) & val)); //cheat to know used space
586 uint8_t *buff = (uint8_t *) malloc(sizeof(uint8_t) + size);
587 buff[0] = BPLIST_REAL | Log2(size);
588 memcpy(buff + 1, &val, size);
589 byte_convert(buff + 1, size);
590 g_byte_array_append(bplist, buff, sizeof(uint8_t) + size);
591 free(buff);
592}
593
594void write_raw_data(GByteArray * bplist, uint8_t mark, uint8_t * val, uint64_t size)
595{
596 uint8_t marker = mark | (size < 15 ? size : 0xf);
597 g_byte_array_append(bplist, &marker, sizeof(uint8_t));
598 if (size >= 15) {
599 GByteArray *int_buff = g_byte_array_new();
600 write_int(int_buff, size);
601 g_byte_array_append(bplist, int_buff->data, int_buff->len);
602 g_byte_array_free(int_buff, TRUE);
603 }
604 uint8_t *buff = (uint8_t *) malloc(size);
605 memcpy(buff, val, size);
606 g_byte_array_append(bplist, buff, size);
607 free(buff);
608}
609
610void write_data(GByteArray * bplist, uint8_t * val, uint64_t size)
611{
612 write_raw_data(bplist, BPLIST_DATA, val, size);
613}
614
615void write_string(GByteArray * bplist, char *val)
616{
617 uint64_t size = strlen(val);
618 write_raw_data(bplist, BPLIST_STRING, val, size);
619}
620
621void write_array(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size)
622{
623 uint64_t size = g_node_n_children(node);
624 uint8_t marker = BPLIST_ARRAY | (size < 15 ? size : 0xf);
625 g_byte_array_append(bplist, &marker, sizeof(uint8_t));
626 if (size >= 15) {
627 GByteArray *int_buff = g_byte_array_new();
628 write_int(int_buff, size);
629 g_byte_array_append(bplist, int_buff->data, int_buff->len);
630 g_byte_array_free(int_buff, TRUE);
631 }
632
633 uint64_t idx = 0;
634 uint8_t *buff = (uint8_t *) malloc(size * dict_param_size);
635
636 GNode *cur = NULL;
637 int i = 0;
638 for (i = 0, cur = node->children; cur && i < size; cur = cur->next, i++) {
639 idx = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur));
640 memcpy(buff + i * dict_param_size, &idx, dict_param_size);
641 byte_convert(buff + i * dict_param_size, dict_param_size);
642 }
643
644 //now append to bplist
645 g_byte_array_append(bplist, buff, size * dict_param_size);
646 free(buff);
647
648}
649
650void write_dict(GByteArray * bplist, GNode * node, GHashTable * ref_table, uint8_t dict_param_size)
651{
652 uint64_t size = g_node_n_children(node) / 2;
653 uint8_t marker = BPLIST_DICT | (size < 15 ? size : 0xf);
654 g_byte_array_append(bplist, &marker, sizeof(uint8_t));
655 if (size >= 15) {
656 GByteArray *int_buff = g_byte_array_new();
657 write_int(int_buff, size);
658 g_byte_array_append(bplist, int_buff->data, int_buff->len);
659 g_byte_array_free(int_buff, TRUE);
660 }
661
662 uint64_t idx1 = 0;
663 uint64_t idx2 = 0;
664 uint8_t *buff = (uint8_t *) malloc(size * 2 * dict_param_size);
665
666 GNode *cur = NULL;
667 int i = 0;
668 for (i = 0, cur = node->children; cur && i < size; cur = cur->next->next, i++) {
669 idx1 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur));
670 memcpy(buff + i * dict_param_size, &idx1, dict_param_size);
671 byte_convert(buff + i * dict_param_size, dict_param_size);
672
673 idx2 = GPOINTER_TO_UINT(g_hash_table_lookup(ref_table, cur->next));
674 memcpy(buff + (i + size) * dict_param_size, &idx2, dict_param_size);
675 byte_convert(buff + (i + size) * dict_param_size, dict_param_size);
676 }
677
678 //now append to bplist
679 g_byte_array_append(bplist, buff, size * 2 * dict_param_size);
680 free(buff);
681
682}
683
684void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length)
685{
686 //check for valid input
687 if (!plist || !plist_bin || *plist_bin || !length)
688 return;
689
690 //list of objects
691 GPtrArray *objects = g_ptr_array_new();
692 //hashtable to write only once same nodes
693 GHashTable *ref_table = g_hash_table_new(plist_data_hash, plist_data_compare);
694
695 //serialize plist
696 struct serialize_s ser_s = { objects, ref_table };
697 serialize_plist(plist, &ser_s);
698
699 //now stream to output buffer
700 uint8_t offset_size = 0; //unknown yet
701 uint8_t dict_param_size = get_needed_bytes(objects->len);
702 uint64_t num_objects = objects->len;
703 uint64_t root_object = 0; //root is first in list
704 uint64_t offset_table_index = 0; //unknown yet
705
706 //setup a dynamic bytes array to store bplist in
707 GByteArray *bplist_buff = g_byte_array_new();
708
709 //set magic number and version
710 g_byte_array_append(bplist_buff, BPLIST_MAGIC, BPLIST_MAGIC_SIZE);
711 g_byte_array_append(bplist_buff, BPLIST_VERSION, BPLIST_VERSION_SIZE);
712
713 //write objects and table
714 int i = 0;
715 uint8_t *buff = NULL;
716 uint8_t size = 0;
717 uint64_t offsets[num_objects];
718 for (i = 0; i < num_objects; i++) {
719
720 offsets[i] = bplist_buff->len;
721 struct plist_data *data = (struct plist_data *) ((GNode *) g_ptr_array_index(objects, i))->data;
722
723 switch (data->type) {
724 case PLIST_BOOLEAN:
725 buff = (uint8_t *) malloc(sizeof(uint8_t));
726 buff[0] = data->boolval ? BPLIST_TRUE : BPLIST_FALSE;
727 g_byte_array_append(bplist_buff, buff, sizeof(uint8_t));
728 free(buff);
729 break;
730
731 case PLIST_UINT:
732 write_int(bplist_buff, data->intval);
733 break;
734
735 case PLIST_REAL:
736 write_real(bplist_buff, data->realval);
737 break;
738
739 case PLIST_KEY:
740 case PLIST_STRING:
741 write_string(bplist_buff, data->strval);
742 break;
743 case PLIST_UNICODE:
744 //TODO
745 break;
746 case PLIST_DATA:
747 write_data(bplist_buff, data->strval, data->length);
748 case PLIST_ARRAY:
749 write_array(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size);
750 break;
751 case PLIST_DICT:
752 write_dict(bplist_buff, g_ptr_array_index(objects, i), ref_table, dict_param_size);
753 break;
754 case PLIST_DATE:
755 //TODO
756 break;
757 default:
758 break;
759 }
760 }
761
762 //write offsets
763 offset_size = get_needed_bytes(bplist_buff->len);
764 offset_table_index = bplist_buff->len;
765 for (i = 0; i <= num_objects; i++) {
766 uint8_t *buff = (uint8_t *) malloc(offset_size);
767 memcpy(buff, offsets + i, offset_size);
768 byte_convert(buff, offset_size);
769 g_byte_array_append(bplist_buff, buff, offset_size);
770 free(buff);
771 }
772
773 //setup trailer
774 num_objects = bswap_64(num_objects);
775 root_object = bswap_64(root_object);
776 offset_table_index = bswap_64(offset_table_index);
777
778 char trailer[BPLIST_TRL_SIZE];
779 memcpy(trailer + BPLIST_TRL_OFFSIZE_IDX, &offset_size, sizeof(uint8_t));
780 memcpy(trailer + BPLIST_TRL_PARMSIZE_IDX, &dict_param_size, sizeof(uint8_t));
781 memcpy(trailer + BPLIST_TRL_NUMOBJ_IDX, &num_objects, sizeof(uint64_t));
782 memcpy(trailer + BPLIST_TRL_ROOTOBJ_IDX, &root_object, sizeof(uint64_t));
783 memcpy(trailer + BPLIST_TRL_OFFTAB_IDX, &offset_table_index, sizeof(uint64_t));
784
785 g_byte_array_append(bplist_buff, trailer, BPLIST_TRL_SIZE);
786
787 //duplicate buffer
788 *plist_bin = (char *) malloc(bplist_buff->len);
789 memcpy(*plist_bin, bplist_buff->data, bplist_buff->len);
790 *length = bplist_buff->len;
791
792 g_byte_array_free(bplist_buff, TRUE);
793}
diff --git a/src/lockdown.c b/src/lockdown.c
index e882128..5b83fb9 100644
--- a/src/lockdown.c
+++ b/src/lockdown.c
@@ -30,6 +30,8 @@
30#include <libtasn1.h> 30#include <libtasn1.h>
31#include <gnutls/x509.h> 31#include <gnutls/x509.h>
32 32
33#include <plist/plist.h>
34
33const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = { 35const ASN1_ARRAY_TYPE pkcs1_asn1_tab[] = {
34 {"PKCS1", 536872976, 0}, 36 {"PKCS1", 536872976, 0},
35 {0, 1073741836, 0}, 37 {0, 1073741836, 0},
@@ -177,10 +179,9 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
177 int bytes = 0, i = 0; 179 int bytes = 0, i = 0;
178 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 180 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
179 181
180 plist_t dict = NULL; 182 plist_t dict = plist_new_dict();
181 plist_new_dict(&dict); 183 plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request"));
182 184 plist_add_sub_element(dict, PLIST_STRING, (void *) "QueryType", strlen("QueryType"));
183 plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "QueryType", strlen("QueryType"));
184 185
185 log_debug_msg("lockdownd_hello() called\n"); 186 log_debug_msg("lockdownd_hello() called\n");
186 char *XML_content = NULL; 187 char *XML_content = NULL;
@@ -197,14 +198,14 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
197 198
198 ret = iphone_lckd_recv(control, &XML_content, &bytes); 199 ret = iphone_lckd_recv(control, &XML_content, &bytes);
199 log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); 200 log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content);
200 xml_to_plist(XML_content, bytes, &dict); 201 plist_from_xml(XML_content, bytes, &dict);
201 202
202 if (!dict) 203 if (!dict)
203 return IPHONE_E_PLIST_ERROR; 204 return IPHONE_E_PLIST_ERROR;
204 205
205 plist_t query_node = find_query_node(dict, "Request", "QueryType"); 206 plist_t query_node = plist_find_node(dict, PLIST_STRING, "QueryType", strlen("QueryType"));
206 plist_t result_node = g_node_next_sibling(query_node); 207 plist_t result_node = plist_get_next_sibling(query_node);
207 plist_t value_node = g_node_next_sibling(result_node); 208 plist_t value_node = plist_get_next_sibling(result_node);
208 209
209 plist_type result_type; 210 plist_type result_type;
210 plist_type value_type; 211 plist_type value_type;
@@ -214,8 +215,8 @@ iphone_error_t lockdownd_hello(iphone_lckd_client_t control)
214 uint64_t result_length = 0; 215 uint64_t result_length = 0;
215 uint64_t value_length = 0; 216 uint64_t value_length = 0;
216 217
217 get_type_and_value(result_node, &result_type, (void *) (&result_value), &result_length); 218 plist_get_type_and_value(result_node, &result_type, (void *) (&result_value), &result_length);
218 get_type_and_value(value_node, &value_type, (void *) (&value_value), &value_length); 219 plist_get_type_and_value(value_node, &value_type, (void *) (&value_value), &value_length);
219 220
220 if (result_type == PLIST_KEY && 221 if (result_type == PLIST_KEY &&
221 value_type == PLIST_STRING && !strcmp(result_value, "Result") && !strcmp(value_value, "Success")) { 222 value_type == PLIST_STRING && !strcmp(result_value, "Result") && !strcmp(value_value, "Success")) {
@@ -247,9 +248,11 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r
247 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 248 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
248 249
249 /* Setup DevicePublicKey request plist */ 250 /* Setup DevicePublicKey request plist */
250 plist_new_dict(&dict); 251 dict = plist_new_dict();
251 plist_add_dict_element(dict, req_key, PLIST_STRING, (void *) req_string, strlen(req_string)); 252 plist_add_sub_element(dict, PLIST_KEY, (void *) req_key, strlen(req_key));
252 plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "GetValue", strlen("GetValue")); 253 plist_add_sub_element(dict, PLIST_STRING, (void *) req_string, strlen(req_string));
254 plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request"));
255 plist_add_sub_element(dict, PLIST_STRING, (void *) "GetValue", strlen("GetValue"));
253 plist_to_xml(dict, &XML_content, &length); 256 plist_to_xml(dict, &XML_content, &length);
254 257
255 /* send to iPhone */ 258 /* send to iPhone */
@@ -271,13 +274,13 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r
271 if (ret != IPHONE_E_SUCCESS) 274 if (ret != IPHONE_E_SUCCESS)
272 return ret; 275 return ret;
273 276
274 xml_to_plist(XML_content, bytes, &dict); 277 plist_from_xml(XML_content, bytes, &dict);
275 if (!dict) 278 if (!dict)
276 return IPHONE_E_PLIST_ERROR; 279 return IPHONE_E_PLIST_ERROR;
277 280
278 plist_t query_node = find_query_node(dict, "Request", "GetValue"); 281 plist_t query_node = plist_find_node(dict, PLIST_STRING, "GetValue", strlen("GetValue"));
279 plist_t result_key_node = g_node_next_sibling(query_node); 282 plist_t result_key_node = plist_get_next_sibling(query_node);
280 plist_t result_value_node = g_node_next_sibling(result_key_node); 283 plist_t result_value_node = plist_get_next_sibling(result_key_node);
281 284
282 plist_type result_key_type; 285 plist_type result_key_type;
283 plist_type result_value_type; 286 plist_type result_value_type;
@@ -286,8 +289,8 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r
286 uint64_t result_length = 0; 289 uint64_t result_length = 0;
287 uint64_t value_length = 0; 290 uint64_t value_length = 0;
288 291
289 get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &result_length); 292 plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &result_length);
290 get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &value_length); 293 plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &value_length);
291 294
292 if (result_key_type == PLIST_KEY && 295 if (result_key_type == PLIST_KEY &&
293 result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { 296 result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
@@ -299,8 +302,8 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r
299 return IPHONE_E_DICT_ERROR; 302 return IPHONE_E_DICT_ERROR;
300 } 303 }
301 304
302 plist_t value_key_node = g_node_next_sibling(result_key_node); 305 plist_t value_key_node = plist_get_next_sibling(result_key_node);
303 plist_t value_value_node = g_node_next_sibling(value_key_node); 306 plist_t value_value_node = plist_get_next_sibling(value_key_node);
304 plist_type value_key_type; 307 plist_type value_key_type;
305 plist_type value_value_type; 308 plist_type value_value_type;
306 char *value_key = NULL; 309 char *value_key = NULL;
@@ -308,8 +311,8 @@ iphone_error_t lockdownd_generic_get_value(iphone_lckd_client_t control, char *r
308 uint64_t key_length = 0; 311 uint64_t key_length = 0;
309 uint64_t valval_length = 0; 312 uint64_t valval_length = 0;
310 313
311 get_type_and_value(value_key_node, &value_key_type, (void *) (&value_key), &key_length); 314 plist_get_type_and_value(value_key_node, &value_key_type, (void *) (&value_key), &key_length);
312 get_type_and_value(value_value_node, &value_value_type, (void *) (&value_value), &valval_length); 315 plist_get_type_and_value(value_value_node, &value_value_type, (void *) (&value_value), &valval_length);
313 316
314 if (value_key_type == PLIST_KEY && !strcmp(result_key, "Value")) { 317 if (value_key_type == PLIST_KEY && !strcmp(result_key, "Value")) {
315 log_debug_msg("lockdownd_generic_get_value(): success\n"); 318 log_debug_msg("lockdownd_generic_get_value(): success\n");
@@ -438,14 +441,19 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch
438 } 441 }
439 442
440 /* Setup Pair request plist */ 443 /* Setup Pair request plist */
441 plist_new_dict(&dict); 444 dict = plist_new_dict();
442 plist_add_dict_element(dict, "PairRecord", PLIST_DICT, NULL, 0); 445 plist_add_sub_element(dict, PLIST_KEY, (void *) "PairRecord", strlen("PairRecord"));
443 dict_record = g_node_last_child(dict); 446 dict_record = plist_add_sub_element(dict, PLIST_DICT, NULL, 0);
444 plist_add_dict_element(dict_record, "DeviceCertificate", PLIST_DATA, (void *) device_cert.data, device_cert.size); 447 plist_add_sub_element(dict_record, PLIST_KEY, (void *) "DeviceCertificate", strlen("DeviceCertificate"));
445 plist_add_dict_element(dict_record, "HostCertificate", PLIST_DATA, (void *) host_cert.data, host_cert.size); 448 plist_add_sub_element(dict_record, PLIST_DATA, (void *) device_cert.data, device_cert.size);
446 plist_add_dict_element(dict_record, "HostID", PLIST_STRING, (void *) host_id, strlen(host_id)); 449 plist_add_sub_element(dict_record, PLIST_KEY, (void *) "HostCertificate", strlen("HostCertificate"));
447 plist_add_dict_element(dict_record, "RootCertificate", PLIST_DATA, (void *) root_cert.data, root_cert.size); 450 plist_add_sub_element(dict_record, PLIST_DATA, (void *) host_cert.data, host_cert.size);
448 plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "Pair", strlen("Pair")); 451 plist_add_sub_element(dict_record, PLIST_KEY, (void *) "HostID", strlen("HostID"));
452 plist_add_sub_element(dict_record, PLIST_STRING, (void *) host_id, strlen(host_id));
453 plist_add_sub_element(dict_record, PLIST_KEY, (void *) "RootCertificate", strlen("RootCertificate"));
454 plist_add_sub_element(dict_record, PLIST_DATA, (void *) root_cert.data, root_cert.size);
455 plist_add_sub_element(dict_record, PLIST_KEY, (void *) "Request", strlen("Request"));
456 plist_add_sub_element(dict_record, PLIST_STRING, (void *) "Pair", strlen("Pair"));
449 plist_to_xml(dict, &XML_content, &length); 457 plist_to_xml(dict, &XML_content, &length);
450 log_debug_msg("XML Pairing request :\nsize : %i\nxml :\n %s", length, XML_content); 458 log_debug_msg("XML Pairing request :\nsize : %i\nxml :\n %s", length, XML_content);
451 459
@@ -469,13 +477,13 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch
469 log_debug_msg(XML_content); 477 log_debug_msg(XML_content);
470 log_debug_msg("\n\n"); 478 log_debug_msg("\n\n");
471 479
472 xml_to_plist(XML_content, bytes, &dict); 480 plist_from_xml(XML_content, bytes, &dict);
473 if (!dict) 481 if (!dict)
474 return IPHONE_E_PLIST_ERROR; 482 return IPHONE_E_PLIST_ERROR;
475 483
476 plist_t query_node = find_query_node(dict, "Request", "Pair"); 484 plist_t query_node = plist_find_node(dict, PLIST_STRING, "Pair", strlen("Pair"));
477 plist_t result_key_node = g_node_next_sibling(query_node); 485 plist_t result_key_node = plist_get_next_sibling(query_node);
478 plist_t result_value_node = g_node_next_sibling(result_key_node); 486 plist_t result_value_node = plist_get_next_sibling(result_key_node);
479 487
480 plist_type result_key_type; 488 plist_type result_key_type;
481 plist_type result_value_type; 489 plist_type result_value_type;
@@ -484,8 +492,8 @@ iphone_error_t lockdownd_pair_device(iphone_lckd_client_t control, char *uid, ch
484 uint64_t key_length = 0; 492 uint64_t key_length = 0;
485 uint64_t val_length = 0; 493 uint64_t val_length = 0;
486 494
487 get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length); 495 plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length);
488 get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length); 496 plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length);
489 497
490 if (result_key_type == PLIST_KEY && 498 if (result_key_type == PLIST_KEY &&
491 result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) { 499 result_value_type == PLIST_STRING && !strcmp(result_key, "Result") && !strcmp(result_value, "Success")) {
@@ -649,9 +657,11 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c
649 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR; 657 iphone_error_t ret = IPHONE_E_UNKNOWN_ERROR;
650 658
651 /* Setup DevicePublicKey request plist */ 659 /* Setup DevicePublicKey request plist */
652 plist_new_dict(&dict); 660 dict = plist_new_dict();
653 plist_add_dict_element(dict, "HostID", PLIST_STRING, (void *) HostID, strlen(HostID)); 661 plist_add_sub_element(dict, PLIST_KEY, (void *) "HostID", strlen("HostID"));
654 plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartSession", strlen("StartSession")); 662 plist_add_sub_element(dict, PLIST_STRING, (void *) HostID, strlen(HostID));
663 plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request"));
664 plist_add_sub_element(dict, PLIST_STRING, (void *) "StartSession", strlen("StartSession"));
655 plist_to_xml(dict, &XML_content, &length); 665 plist_to_xml(dict, &XML_content, &length);
656 log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content); 666 log_debug_msg("Send msg :\nsize : %i\nxml : %s", length, XML_content);
657 667
@@ -668,13 +678,13 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c
668 if (bytes > 0) { 678 if (bytes > 0) {
669 ret = iphone_lckd_recv(control, &XML_content, &bytes); 679 ret = iphone_lckd_recv(control, &XML_content, &bytes);
670 log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content); 680 log_debug_msg("Receive msg :\nsize : %i\nxml : %s", bytes, XML_content);
671 xml_to_plist(XML_content, bytes, &dict); 681 plist_from_xml(XML_content, bytes, &dict);
672 if (!dict) 682 if (!dict)
673 return IPHONE_E_PLIST_ERROR; 683 return IPHONE_E_PLIST_ERROR;
674 684
675 plist_t query_node = find_query_node(dict, "Request", "StartSession"); 685 plist_t query_node = plist_find_node(dict, PLIST_STRING, "StartSession", strlen("StartSession"));
676 plist_t result_key_node = g_node_next_sibling(query_node); 686 plist_t result_key_node = plist_get_next_sibling(query_node);
677 plist_t result_value_node = g_node_next_sibling(result_key_node); 687 plist_t result_value_node = plist_get_next_sibling(result_key_node);
678 688
679 plist_type result_key_type; 689 plist_type result_key_type;
680 plist_type result_value_type; 690 plist_type result_value_type;
@@ -683,8 +693,8 @@ iphone_error_t lockdownd_start_SSL_session(iphone_lckd_client_t control, const c
683 uint64_t key_length = 0; 693 uint64_t key_length = 0;
684 uint64_t val_length = 0; 694 uint64_t val_length = 0;
685 695
686 get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length); 696 plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &key_length);
687 get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length); 697 plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &val_length);
688 698
689 free(XML_content); 699 free(XML_content);
690 XML_content = NULL; 700 XML_content = NULL;
@@ -886,9 +896,11 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
886 free(host_id); 896 free(host_id);
887 host_id = NULL; 897 host_id = NULL;
888 898
889 plist_new_dict(&dict); 899 dict = plist_new_dict();
890 plist_add_dict_element(dict, "Request", PLIST_STRING, (void *) "StartService", strlen("StartService")); 900 plist_add_sub_element(dict, PLIST_KEY, (void *) "Request", strlen("Request"));
891 plist_add_dict_element(dict, "Service", PLIST_STRING, (void *) service, strlen(service)); 901 plist_add_sub_element(dict, PLIST_STRING, (void *) "StartService", strlen("StartService"));
902 plist_add_sub_element(dict, PLIST_KEY, (void *) "Service", strlen("Service"));
903 plist_add_sub_element(dict, PLIST_STRING, (void *) service, strlen(service));
892 plist_to_xml(dict, &XML_content, &length); 904 plist_to_xml(dict, &XML_content, &length);
893 905
894 /* send to iPhone */ 906 /* send to iPhone */
@@ -908,7 +920,7 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
908 if (IPHONE_E_SUCCESS != ret) 920 if (IPHONE_E_SUCCESS != ret)
909 return ret; 921 return ret;
910 922
911 xml_to_plist(XML_content, bytes, &dict); 923 plist_from_xml(XML_content, bytes, &dict);
912 if (!dict) 924 if (!dict)
913 return IPHONE_E_PLIST_ERROR; 925 return IPHONE_E_PLIST_ERROR;
914 926
@@ -917,12 +929,12 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
917 return IPHONE_E_NOT_ENOUGH_DATA; 929 return IPHONE_E_NOT_ENOUGH_DATA;
918 else { 930 else {
919 931
920 plist_t query_node = find_query_node(dict, "Request", "StartService"); 932 plist_t query_node = plist_find_node(dict, PLIST_STRING, "StartService", strlen("StartService"));
921 plist_t result_key_node = g_node_next_sibling(query_node); 933 plist_t result_key_node = plist_get_next_sibling(query_node);
922 plist_t result_value_node = g_node_next_sibling(result_key_node); 934 plist_t result_value_node = plist_get_next_sibling(result_key_node);
923 935
924 plist_t port_key_node = find_node(dict, PLIST_KEY, "Port"); 936 plist_t port_key_node = plist_find_node(dict, PLIST_KEY, "Port", strlen("Port"));
925 plist_t port_value_node = g_node_next_sibling(port_key_node); 937 plist_t port_value_node = plist_get_next_sibling(port_key_node);
926 938
927 plist_type result_key_type; 939 plist_type result_key_type;
928 plist_type result_value_type; 940 plist_type result_value_type;
@@ -937,10 +949,10 @@ iphone_error_t iphone_lckd_start_service(iphone_lckd_client_t client, const char
937 uint64_t port_val_length = 0; 949 uint64_t port_val_length = 0;
938 uint64_t port_value = 0; 950 uint64_t port_value = 0;
939 951
940 get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &res_key_length); 952 plist_get_type_and_value(result_key_node, &result_key_type, (void *) (&result_key), &res_key_length);
941 get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &res_val_length); 953 plist_get_type_and_value(result_value_node, &result_value_type, (void *) (&result_value), &res_val_length);
942 get_type_and_value(port_key_node, &port_key_type, (void *) (&port_key), &port_key_length); 954 plist_get_type_and_value(port_key_node, &port_key_type, (void *) (&port_key), &port_key_length);
943 get_type_and_value(port_value_node, &port_value_type, (void *) (&port_value), &port_val_length); 955 plist_get_type_and_value(port_value_node, &port_value_type, (void *) (&port_value), &port_val_length);
944 956
945 if (result_key_type == PLIST_KEY && 957 if (result_key_type == PLIST_KEY &&
946 result_value_type == PLIST_STRING && 958 result_value_type == PLIST_STRING &&
diff --git a/src/lockdown.h b/src/lockdown.h
index 8b3dd41..18e13aa 100644
--- a/src/lockdown.h
+++ b/src/lockdown.h
@@ -23,7 +23,6 @@
23#define LOCKDOWND_H 23#define LOCKDOWND_H
24 24
25#include "usbmux.h" 25#include "usbmux.h"
26#include "plist.h"
27 26
28#include <gnutls/gnutls.h> 27#include <gnutls/gnutls.h>
29#include <string.h> 28#include <string.h>
diff --git a/src/plist.c b/src/plist.c
deleted file mode 100644
index 932ea5e..0000000
--- a/src/plist.c
+++ /dev/null
@@ -1,251 +0,0 @@
1/*
2 * plist.c
3 * Builds plist XML structures.
4 *
5 * Copyright (c) 2008 Zach C. All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23#include <string.h>
24#include <assert.h>
25#include "utils.h"
26#include "plist.h"
27#include <wchar.h>
28#include <stdlib.h>
29#include <stdio.h>
30
31
32void plist_new_dict(plist_t * plist)
33{
34 if (*plist != NULL)
35 return;
36 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
37 data->type = PLIST_DICT;
38 *plist = g_node_new(data);
39}
40
41void plist_new_array(plist_t * plist)
42{
43 if (*plist != NULL)
44 return;
45 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
46 data->type = PLIST_ARRAY;
47 *plist = g_node_new(data);
48}
49
50void plist_new_dict_in_plist(plist_t plist, plist_t * dict)
51{
52 if (!plist || *dict)
53 return;
54
55 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
56 data->type = PLIST_DICT;
57 *dict = g_node_new(data);
58 g_node_append(plist, *dict);
59}
60
61
62/** Adds a new key pair to a dict.
63 *
64 * @param dict The dict node in the plist.
65 * @param key the key name of the key pair.
66 * @param type The the type of the value in the key pair.
67 * @param value a pointer to the actual buffer containing the value. WARNING : the buffer is supposed to match the type of the value
68 *
69 */
70void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value, uint64_t length)
71{
72 if (!dict || !key || !value)
73 return;
74
75 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
76 data->type = PLIST_KEY;
77 data->strval = strdup(key);
78 GNode *keynode = g_node_new(data);
79 g_node_append(dict, keynode);
80
81 //now handle value
82 struct plist_data *val = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
83 val->type = type;
84 val->length = length;
85
86 switch (type) {
87 case PLIST_BOOLEAN:
88 val->boolval = *((char *) value);
89 break;
90 case PLIST_UINT:
91 val->intval = *((uint64_t *) value);
92 break;
93 case PLIST_REAL:
94 val->realval = *((double *) value);
95 break;
96 case PLIST_STRING:
97 val->strval = strdup((char *) value);
98 break;
99 case PLIST_UNICODE:
100 val->unicodeval = wcsdup((wchar_t *) value);
101 break;
102 case PLIST_DATA:
103 memcpy(val->buff, value, length);
104 break;
105 case PLIST_ARRAY:
106 case PLIST_DICT:
107 case PLIST_DATE:
108 default:
109 break;
110 }
111 GNode *valnode = g_node_new(val);
112 g_node_append(dict, valnode);
113}
114
115void plist_free(plist_t plist)
116{
117 g_node_destroy(plist);
118}
119
120plist_t find_query_node(plist_t plist, char *key, char *request)
121{
122 if (!plist)
123 return NULL;
124
125 GNode *current = NULL;
126 for (current = plist->children; current; current = current->next) {
127
128 struct plist_data *data = (struct plist_data *) current->data;
129
130 if (data->type == PLIST_KEY && !strcmp(data->strval, key) && current->next) {
131
132 data = (struct plist_data *) current->next->data;
133 if (data->type == PLIST_STRING && !strcmp(data->strval, request))
134 return current->next;
135 }
136 if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) {
137 GNode *sub = find_query_node(current, key, request);
138 if (sub)
139 return sub;
140 }
141 }
142 return NULL;
143}
144
145char compare_node_value(plist_type type, struct plist_data *data, void *value)
146{
147 char res = FALSE;
148 switch (type) {
149 case PLIST_BOOLEAN:
150 res = data->boolval == *((char *) value) ? TRUE : FALSE;
151 break;
152 case PLIST_UINT:
153 res = data->intval == *((uint64_t *) value) ? TRUE : FALSE;
154 break;
155 case PLIST_REAL:
156 res = data->realval == *((double *) value) ? TRUE : FALSE;
157 break;
158 case PLIST_KEY:
159 case PLIST_STRING:
160 res = !strcmp(data->strval, ((char *) value));
161 break;
162 case PLIST_UNICODE:
163 res = !wcscmp(data->unicodeval, ((wchar_t *) value));
164 break;
165 case PLIST_DATA:
166 res = !strcmp(data->buff, ((char *) value));
167 break;
168 case PLIST_ARRAY:
169 case PLIST_DICT:
170 case PLIST_DATE:
171 default:
172 break;
173 }
174 return res;
175}
176
177plist_t find_node(plist_t plist, plist_type type, void *value)
178{
179 if (!plist)
180 return NULL;
181
182 GNode *current = NULL;
183 for (current = plist->children; current; current = current->next) {
184
185 struct plist_data *data = (struct plist_data *) current->data;
186
187 if (data->type == type && compare_node_value(type, data, value)) {
188 return current;
189 }
190 if (data->type == PLIST_DICT || data->type == PLIST_ARRAY) {
191 GNode *sub = find_node(current, type, value);
192 if (sub)
193 return sub;
194 }
195 }
196 return NULL;
197}
198
199void get_type_and_value(GNode * node, plist_type * type, void *value, uint64_t * length)
200{
201 if (!node)
202 return;
203
204 struct plist_data *data = (struct plist_data *) node->data;
205
206 *type = data->type;
207 *length = data->length;
208
209 switch (*type) {
210 case PLIST_BOOLEAN:
211 *((char *) value) = data->boolval;
212 break;
213 case PLIST_UINT:
214 *((uint64_t *) value) = data->intval;
215 break;
216 case PLIST_REAL:
217 *((double *) value) = data->realval;
218 break;
219 case PLIST_STRING:
220 *((char **) value) = strdup(data->strval);
221 break;
222 case PLIST_UNICODE:
223 *((wchar_t **) value) = wcsdup(data->unicodeval);
224 break;
225 case PLIST_KEY:
226 *((char **) value) = strdup(data->strval);
227 break;
228 case PLIST_DATA:
229 case PLIST_ARRAY:
230 case PLIST_DICT:
231 case PLIST_DATE:
232 default:
233 break;
234 }
235}
236
237plist_type plist_get_node_type(plist_t node)
238{
239 if (node && node->data)
240 return ((struct plist_data *) node->data)->type;
241 else
242 return PLIST_NONE;
243}
244
245uint64_t plist_get_node_uint_val(plist_t node)
246{
247 if (PLIST_UINT == plist_get_node_type(node))
248 return ((struct plist_data *) node->data)->intval;
249 else
250 return 0;
251}
diff --git a/src/plist.h b/src/plist.h
deleted file mode 100644
index 1dc464a..0000000
--- a/src/plist.h
+++ /dev/null
@@ -1,83 +0,0 @@
1/*
2 * plist.h
3 * contains structures and the like for plists
4 *
5 * Copyright (c) 2008 Zach C. All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#ifndef PLIST_H
23#define PLIST_H
24
25#include <stdint.h>
26#include <wchar.h>
27
28#include <sys/types.h>
29#include <sys/stat.h>
30#include <unistd.h>
31#include <glib.h>
32
33
34typedef enum {
35 PLIST_BOOLEAN,
36 PLIST_UINT,
37 PLIST_REAL,
38 PLIST_STRING,
39 PLIST_UNICODE,
40 PLIST_ARRAY,
41 PLIST_DICT,
42 PLIST_DATE,
43 PLIST_DATA,
44 PLIST_KEY,
45 PLIST_NONE
46} plist_type;
47
48
49struct plist_data {
50 union {
51 char boolval;
52 uint64_t intval;
53 double realval;
54 char *strval;
55 wchar_t *unicodeval;
56 char *buff;
57 };
58 uint64_t length;
59 plist_type type;
60};
61
62
63
64typedef GNode *plist_t;
65
66
67void plist_new_dict(plist_t * plist);
68void plist_new_array(plist_t * plist);
69void plist_new_dict_in_plist(plist_t plist, plist_t * dict);
70void plist_add_dict_element(plist_t dict, char *key, plist_type type, void *value, uint64_t length);
71void plist_free(plist_t plist);
72
73void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length);
74void plist_to_bin(plist_t plist, char **plist_bin, uint32_t * length);
75
76void xml_to_plist(const char *plist_xml, uint32_t length, plist_t * plist);
77void bin_to_plist(const char *plist_bin, uint32_t length, plist_t * plist);
78
79plist_t find_query_node(plist_t plist, char *key, char *request);
80plist_t find_node(plist_t plist, plist_type type, void *value);
81void get_type_and_value(plist_t node, plist_type * type, void *value, uint64_t * length);
82
83#endif
diff --git a/src/xplist.c b/src/xplist.c
deleted file mode 100644
index 2d650b4..0000000
--- a/src/xplist.c
+++ /dev/null
@@ -1,312 +0,0 @@
1/*
2 * plist.c
3 * XML plist implementation
4 *
5 * Copyright (c) 2008 Jonathan Beck All Rights Reserved.
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23#include <string.h>
24#include <assert.h>
25#include "utils.h"
26#include "plist.h"
27#include <wchar.h>
28#include <stdlib.h>
29#include <stdio.h>
30
31
32#include <libxml/parser.h>
33#include <libxml/tree.h>
34
35
36const char *plist_base = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\
37<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n\
38<plist version=\"1.0\">\n\
39</plist>\0";
40
41
42/** Formats a block of text to be a given indentation and width.
43 *
44 * The total width of the return string will be depth + cols.
45 *
46 * @param buf The string to format.
47 * @param cols The number of text columns for returned block of text.
48 * @param depth The number of tabs to indent the returned block of text.
49 *
50 * @return The formatted string.
51 */
52char *format_string(const char *buf, int cols, int depth)
53{
54 int colw = depth + cols + 1;
55 int len = strlen(buf);
56 int nlines = len / cols + 1;
57 char *new_buf = (char *) malloc(nlines * colw + depth + 1);
58 int i = 0;
59 int j = 0;
60
61 assert(cols >= 0);
62 assert(depth >= 0);
63
64 // Inserts new lines and tabs at appropriate locations
65 for (i = 0; i < nlines; i++) {
66 new_buf[i * colw] = '\n';
67 for (j = 0; j < depth; j++)
68 new_buf[i * colw + 1 + j] = '\t';
69 memcpy(new_buf + i * colw + 1 + depth, buf + i * cols, cols);
70 }
71 new_buf[len + (1 + depth) * nlines] = '\n';
72
73 // Inserts final row of indentation and termination character
74 for (j = 0; j < depth; j++)
75 new_buf[len + (1 + depth) * nlines + 1 + j] = '\t';
76 new_buf[len + (1 + depth) * nlines + depth + 1] = '\0';
77
78 return new_buf;
79}
80
81
82
83struct xml_node {
84 xmlNodePtr xml;
85 uint32_t depth;
86};
87
88/** Creates a new plist XML document.
89 *
90 * @return The plist XML document.
91 */
92xmlDocPtr new_xml_plist()
93{
94 char *plist = strdup(plist_base);
95 xmlDocPtr plist_xml = xmlReadMemory(plist, strlen(plist), NULL, NULL, 0);
96
97 if (!plist_xml)
98 return NULL;
99
100 free(plist);
101
102 return plist_xml;
103}
104
105/** Destroys a previously created XML document.
106 *
107 * @param plist The XML document to destroy.
108 */
109void free_plist(xmlDocPtr plist)
110{
111 if (!plist)
112 return;
113
114 xmlFreeDoc(plist);
115}
116
117void node_to_xml(GNode * node, gpointer xml_struct)
118{
119 if (!node)
120 return;
121
122 struct xml_node *xstruct = (struct xml_node *) xml_struct;
123 struct plist_data *node_data = (struct plist_data *) node->data;
124
125 xmlNodePtr child_node = NULL;
126 char isStruct = FALSE;
127
128 gchar *tag = NULL;
129 gchar *val = NULL;
130
131 switch (node_data->type) {
132 case PLIST_BOOLEAN:
133 {
134 if (node_data->boolval)
135 tag = "true";
136 else
137 tag = "false";
138 }
139 break;
140
141 case PLIST_UINT:
142 tag = "integer";
143 val = g_strdup_printf("%lu", (long unsigned int) node_data->intval);
144 break;
145
146 case PLIST_REAL:
147 tag = "real";
148 val = g_strdup_printf("%Lf", (long double) node_data->realval);
149 break;
150
151 case PLIST_STRING:
152 tag = "string";
153 val = g_strdup(node_data->strval);
154 break;
155
156 case PLIST_UNICODE:
157 tag = "string";
158 val = g_strdup((gchar *) node_data->unicodeval);
159 break;
160
161 case PLIST_KEY:
162 tag = "key";
163 val = g_strdup((gchar *) node_data->strval);
164 break;
165
166 case PLIST_DATA:
167 tag = "data";
168 gchar *valtmp = g_base64_encode(node_data->buff, node_data->length);
169 val = format_string(valtmp, 60, xstruct->depth);
170 g_free(valtmp);
171 break;
172 case PLIST_ARRAY:
173 tag = "array";
174 isStruct = TRUE;
175 break;
176 case PLIST_DICT:
177 tag = "dict";
178 isStruct = TRUE;
179 break;
180 case PLIST_DATE: //TODO : handle date tag
181 default:
182 break;
183 }
184
185 int i = 0;
186 for (i = 0; i < xstruct->depth; i++) {
187 xmlNodeAddContent(xstruct->xml, "\t");
188 }
189 child_node = xmlNewChild(xstruct->xml, NULL, tag, val);
190 xmlNodeAddContent(xstruct->xml, "\n");
191 g_free(val);
192
193 //add return for structured types
194 if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT || node_data->type == PLIST_DATA)
195 xmlNodeAddContent(child_node, "\n");
196
197 if (isStruct) {
198 struct xml_node child = { child_node, xstruct->depth + 1 };
199 g_node_children_foreach(node, G_TRAVERSE_ALL, node_to_xml, &child);
200 }
201 //fix indent for structured types
202 if (node_data->type == PLIST_ARRAY || node_data->type == PLIST_DICT || node_data->type == PLIST_DATA) {
203
204 for (i = 0; i < xstruct->depth; i++) {
205 xmlNodeAddContent(child_node, "\t");
206 }
207 }
208
209 return;
210}
211
212void xml_to_node(xmlNodePtr xml_node, plist_t * plist_node)
213{
214 xmlNodePtr node = NULL;
215
216 for (node = xml_node->children; node; node = node->next) {
217
218 while (node && !xmlStrcmp(node->name, "text"))
219 node = node->next;
220 if (!node)
221 break;
222
223 struct plist_data *data = (struct plist_data *) calloc(sizeof(struct plist_data), 1);
224 GNode *subnode = g_node_new(data);
225 if (*plist_node)
226 g_node_append(*plist_node, subnode);
227 else
228 *plist_node = subnode;
229
230 if (!xmlStrcmp(node->name, "true")) {
231 data->boolval = 1;
232 data->type = PLIST_BOOLEAN;
233 continue;
234 }
235
236 if (!xmlStrcmp(node->name, "false")) {
237 data->boolval = 0;
238 data->type = PLIST_BOOLEAN;
239 continue;
240 }
241
242 if (!xmlStrcmp(node->name, "integer")) {
243 char *strval = xmlNodeGetContent(node);
244 data->intval = g_ascii_strtoull(strval, NULL, 0);
245 data->type = PLIST_UINT;
246 continue;
247 }
248
249 if (!xmlStrcmp(node->name, "real")) {
250 char *strval = xmlNodeGetContent(node);
251 data->realval = atof(strval);
252 data->type = PLIST_REAL;
253 continue;
254 }
255
256 if (!xmlStrcmp(node->name, "date"))
257 continue; //TODO : handle date tag
258
259 if (!xmlStrcmp(node->name, "string")) {
260 data->strval = strdup(xmlNodeGetContent(node));
261 data->type = PLIST_STRING;
262 continue;
263 }
264
265 if (!xmlStrcmp(node->name, "key")) {
266 data->strval = strdup(xmlNodeGetContent(node));
267 data->type = PLIST_KEY;
268 continue;
269 }
270
271 if (!xmlStrcmp(node->name, "data")) {
272 gsize size = 0;
273 data->buff = g_base64_decode(xmlNodeGetContent(node), &size);
274 data->length = size;
275 data->type = PLIST_DATA;
276 continue;
277 }
278
279 if (!xmlStrcmp(node->name, "array")) {
280 data->type = PLIST_ARRAY;
281 xml_to_node(node, &subnode);
282 continue;
283 }
284
285 if (!xmlStrcmp(node->name, "dict")) {
286 data->type = PLIST_DICT;
287 xml_to_node(node, &subnode);
288 continue;
289 }
290 }
291}
292
293void plist_to_xml(plist_t plist, char **plist_xml, uint32_t * length)
294{
295 if (!plist || !plist_xml || *plist_xml)
296 return;
297 xmlDocPtr plist_doc = new_xml_plist();
298 xmlNodePtr root_node = xmlDocGetRootElement(plist_doc);
299 struct xml_node root = { root_node, 0 };
300
301 node_to_xml(plist, &root);
302
303 xmlDocDumpMemory(plist_doc, (xmlChar **) plist_xml, length);
304}
305
306void xml_to_plist(const char *plist_xml, uint32_t length, plist_t * plist)
307{
308 xmlDocPtr plist_doc = xmlReadMemory(plist_xml, length, NULL, NULL, 0);
309 xmlNodePtr root_node = xmlDocGetRootElement(plist_doc);
310
311 xml_to_node(root_node, plist);
312}