summaryrefslogtreecommitdiffstats
path: root/tools/ideviceinfo.c
diff options
context:
space:
mode:
authorGravatar Nikias Bassen2014-05-21 11:56:51 +0200
committerGravatar Nikias Bassen2014-05-21 11:56:51 +0200
commit4563ad4d2e6ad75e42737e6ae381a0648b65f958 (patch)
tree90872fd39fb0a45187cee45dc39cb57ae2b491d7 /tools/ideviceinfo.c
parent850d9790ac867ef2891424e3d4d13b452a9d1451 (diff)
downloadlibimobiledevice-4563ad4d2e6ad75e42737e6ae381a0648b65f958.tar.gz
libimobiledevice-4563ad4d2e6ad75e42737e6ae381a0648b65f958.tar.bz2
ideviceinfo: Move plist print helper code to common
Diffstat (limited to 'tools/ideviceinfo.c')
-rw-r--r--tools/ideviceinfo.c193
1 files changed, 4 insertions, 189 deletions
diff --git a/tools/ideviceinfo.c b/tools/ideviceinfo.c
index d270b73..7b3b924 100644
--- a/tools/ideviceinfo.c
+++ b/tools/ideviceinfo.c
@@ -23,12 +23,10 @@
23#include <string.h> 23#include <string.h>
24#include <errno.h> 24#include <errno.h>
25#include <stdlib.h> 25#include <stdlib.h>
26#include <time.h>
27#include <sys/time.h>
28#include <inttypes.h>
29 26
30#include <libimobiledevice/libimobiledevice.h> 27#include <libimobiledevice/libimobiledevice.h>
31#include <libimobiledevice/lockdown.h> 28#include <libimobiledevice/lockdown.h>
29#include "common/utils.h"
32 30
33#define FORMAT_KEY_VALUE 1 31#define FORMAT_KEY_VALUE 1
34#define FORMAT_XML 2 32#define FORMAT_XML 2
@@ -68,38 +66,6 @@ static const char *domains[] = {
68 NULL 66 NULL
69}; 67};
70 68
71static const char base64_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
72static const char base64_pad = '=';
73
74static char *base64encode(const unsigned char *buf, size_t size)
75{
76 if (!buf || !(size > 0)) return NULL;
77 int outlen = (size / 3) * 4;
78 char *outbuf = (char*)malloc(outlen+5); // 4 spare bytes + 1 for '\0'
79 size_t n = 0;
80 size_t m = 0;
81 unsigned char input[3];
82 unsigned int output[4];
83 while (n < size) {
84 input[0] = buf[n];
85 input[1] = (n+1 < size) ? buf[n+1] : 0;
86 input[2] = (n+2 < size) ? buf[n+2] : 0;
87 output[0] = input[0] >> 2;
88 output[1] = ((input[0] & 3) << 4) + (input[1] >> 4);
89 output[2] = ((input[1] & 15) << 2) + (input[2] >> 6);
90 output[3] = input[2] & 63;
91 outbuf[m++] = base64_str[(int)output[0]];
92 outbuf[m++] = base64_str[(int)output[1]];
93 outbuf[m++] = (n+1 < size) ? base64_str[(int)output[2]] : base64_pad;
94 outbuf[m++] = (n+2 < size) ? base64_str[(int)output[3]] : base64_pad;
95 n+=3;
96 }
97 outbuf[m] = 0; // 0-termination!
98 return outbuf;
99}
100
101static int indent_level = 0;
102
103static int is_domain_known(char *domain) 69static int is_domain_known(char *domain)
104{ 70{
105 int i = 0; 71 int i = 0;
@@ -111,150 +77,6 @@ static int is_domain_known(char *domain)
111 return 0; 77 return 0;
112} 78}
113 79
114static void plist_node_to_string(plist_t node);
115
116static void plist_array_to_string(plist_t node)
117{
118 /* iterate over items */
119 int i, count;
120 plist_t subnode = NULL;
121
122 count = plist_array_get_size(node);
123
124 for (i = 0; i < count; i++) {
125 subnode = plist_array_get_item(node, i);
126 printf("%*s", indent_level, "");
127 printf("%d: ", i);
128 plist_node_to_string(subnode);
129 }
130}
131
132static void plist_dict_to_string(plist_t node)
133{
134 /* iterate over key/value pairs */
135 plist_dict_iter it = NULL;
136
137 char* key = NULL;
138 plist_t subnode = NULL;
139 plist_dict_new_iter(node, &it);
140 plist_dict_next_item(node, it, &key, &subnode);
141 while (subnode)
142 {
143 printf("%*s", indent_level, "");
144 printf("%s", key);
145 if (plist_get_node_type(subnode) == PLIST_ARRAY)
146 printf("[%d]: ", plist_array_get_size(subnode));
147 else
148 printf(": ");
149 free(key);
150 key = NULL;
151 plist_node_to_string(subnode);
152 plist_dict_next_item(node, it, &key, &subnode);
153 }
154 free(it);
155}
156
157static void plist_node_to_string(plist_t node)
158{
159 char *s = NULL;
160 char *data = NULL;
161 double d;
162 uint8_t b;
163 uint64_t u = 0;
164 struct timeval tv = { 0, 0 };
165
166 plist_type t;
167
168 if (!node)
169 return;
170
171 t = plist_get_node_type(node);
172
173 switch (t) {
174 case PLIST_BOOLEAN:
175 plist_get_bool_val(node, &b);
176 printf("%s\n", (b ? "true" : "false"));
177 break;
178
179 case PLIST_UINT:
180 plist_get_uint_val(node, &u);
181 printf("%"PRIu64"\n", (long long)u);
182 break;
183
184 case PLIST_REAL:
185 plist_get_real_val(node, &d);
186 printf("%f\n", d);
187 break;
188
189 case PLIST_STRING:
190 plist_get_string_val(node, &s);
191 printf("%s\n", s);
192 free(s);
193 break;
194
195 case PLIST_KEY:
196 plist_get_key_val(node, &s);
197 printf("%s: ", s);
198 free(s);
199 break;
200
201 case PLIST_DATA:
202 plist_get_data_val(node, &data, &u);
203 if (u > 0) {
204 s = base64encode((unsigned char*)data, u);
205 free(data);
206 if (s) {
207 printf("%s\n", s);
208 free(s);
209 } else {
210 printf("\n");
211 }
212 } else {
213 printf("\n");
214 }
215 break;
216
217 case PLIST_DATE:
218 plist_get_date_val(node, (int32_t*)&tv.tv_sec, (int32_t*)&tv.tv_usec);
219 {
220 time_t ti = (time_t)tv.tv_sec;
221 struct tm *btime = localtime(&ti);
222 if (btime) {
223 s = (char*)malloc(24);
224 memset(s, 0, 24);
225 if (strftime(s, 24, "%Y-%m-%dT%H:%M:%SZ", btime) <= 0) {
226 free (s);
227 s = NULL;
228 }
229 }
230 }
231 if (s) {
232 printf("%s\n", s);
233 free(s);
234 } else {
235 printf("\n");
236 }
237 break;
238
239 case PLIST_ARRAY:
240 printf("\n");
241 indent_level++;
242 plist_array_to_string(node);
243 indent_level--;
244 break;
245
246 case PLIST_DICT:
247 printf("\n");
248 indent_level++;
249 plist_dict_to_string(node);
250 indent_level--;
251 break;
252
253 default:
254 break;
255 }
256}
257
258static void print_usage(int argc, char **argv) 80static void print_usage(int argc, char **argv)
259{ 81{
260 int i = 0; 82 int i = 0;
@@ -292,7 +114,6 @@ int main(int argc, char *argv[])
292 char *xml_doc = NULL; 114 char *xml_doc = NULL;
293 uint32_t xml_length; 115 uint32_t xml_length;
294 plist_t node = NULL; 116 plist_t node = NULL;
295 plist_type node_type;
296 117
297 /* parse cmdline args */ 118 /* parse cmdline args */
298 for (i = 1; i < argc; i++) { 119 for (i = 1; i < argc; i++) {
@@ -375,17 +196,11 @@ int main(int argc, char *argv[])
375 free(xml_doc); 196 free(xml_doc);
376 break; 197 break;
377 case FORMAT_KEY_VALUE: 198 case FORMAT_KEY_VALUE:
378 node_type = plist_get_node_type(node); 199 plist_print_to_stream(node, stdout);
379 if (node_type == PLIST_DICT) { 200 break;
380 plist_dict_to_string(node);
381 break;
382 } else if (node_type == PLIST_ARRAY) {
383 plist_array_to_string(node);
384 break;
385 }
386 default: 201 default:
387 if (key != NULL) 202 if (key != NULL)
388 plist_node_to_string(node); 203 plist_print_to_stream(node, stdout);
389 break; 204 break;
390 } 205 }
391 plist_free(node); 206 plist_free(node);