diff options
| author | 2014-05-21 11:56:51 +0200 | |
|---|---|---|
| committer | 2014-05-21 11:56:51 +0200 | |
| commit | 4563ad4d2e6ad75e42737e6ae381a0648b65f958 (patch) | |
| tree | 90872fd39fb0a45187cee45dc39cb57ae2b491d7 | |
| parent | 850d9790ac867ef2891424e3d4d13b452a9d1451 (diff) | |
| download | libimobiledevice-4563ad4d2e6ad75e42737e6ae381a0648b65f958.tar.gz libimobiledevice-4563ad4d2e6ad75e42737e6ae381a0648b65f958.tar.bz2 | |
ideviceinfo: Move plist print helper code to common
| -rw-r--r-- | common/utils.c | 195 | ||||
| -rw-r--r-- | common/utils.h | 2 | ||||
| -rw-r--r-- | tools/Makefile.am | 2 | ||||
| -rw-r--r-- | tools/ideviceinfo.c | 193 |
4 files changed, 202 insertions, 190 deletions
diff --git a/common/utils.c b/common/utils.c index 68b23b9..aa0332d 100644 --- a/common/utils.c +++ b/common/utils.c | |||
| @@ -27,6 +27,8 @@ | |||
| 27 | #include <stdlib.h> | 27 | #include <stdlib.h> |
| 28 | #include <string.h> | 28 | #include <string.h> |
| 29 | #include <time.h> | 29 | #include <time.h> |
| 30 | #include <sys/time.h> | ||
| 31 | #include <inttypes.h> | ||
| 30 | 32 | ||
| 31 | #include "utils.h" | 33 | #include "utils.h" |
| 32 | 34 | ||
| @@ -222,3 +224,196 @@ int plist_write_to_filename(plist_t plist, const char *filename, enum plist_form | |||
| 222 | 224 | ||
| 223 | return 1; | 225 | return 1; |
| 224 | } | 226 | } |
| 227 | |||
| 228 | static const char base64_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
| 229 | static const char base64_pad = '='; | ||
| 230 | |||
| 231 | static char *base64encode(const unsigned char *buf, size_t size) | ||
| 232 | { | ||
| 233 | if (!buf || !(size > 0)) return NULL; | ||
| 234 | int outlen = (size / 3) * 4; | ||
| 235 | char *outbuf = (char*)malloc(outlen+5); // 4 spare bytes + 1 for '\0' | ||
| 236 | size_t n = 0; | ||
| 237 | size_t m = 0; | ||
| 238 | unsigned char input[3]; | ||
| 239 | unsigned int output[4]; | ||
| 240 | while (n < size) { | ||
| 241 | input[0] = buf[n]; | ||
| 242 | input[1] = (n+1 < size) ? buf[n+1] : 0; | ||
| 243 | input[2] = (n+2 < size) ? buf[n+2] : 0; | ||
| 244 | output[0] = input[0] >> 2; | ||
| 245 | output[1] = ((input[0] & 3) << 4) + (input[1] >> 4); | ||
| 246 | output[2] = ((input[1] & 15) << 2) + (input[2] >> 6); | ||
| 247 | output[3] = input[2] & 63; | ||
| 248 | outbuf[m++] = base64_str[(int)output[0]]; | ||
| 249 | outbuf[m++] = base64_str[(int)output[1]]; | ||
| 250 | outbuf[m++] = (n+1 < size) ? base64_str[(int)output[2]] : base64_pad; | ||
| 251 | outbuf[m++] = (n+2 < size) ? base64_str[(int)output[3]] : base64_pad; | ||
| 252 | n+=3; | ||
| 253 | } | ||
| 254 | outbuf[m] = 0; // 0-termination! | ||
| 255 | return outbuf; | ||
| 256 | } | ||
| 257 | |||
| 258 | static void plist_node_print_to_stream(plist_t node, int* indent_level, FILE* stream); | ||
| 259 | |||
| 260 | static void plist_array_print_to_stream(plist_t node, int* indent_level, FILE* stream) | ||
| 261 | { | ||
| 262 | /* iterate over items */ | ||
| 263 | int i, count; | ||
| 264 | plist_t subnode = NULL; | ||
| 265 | |||
| 266 | count = plist_array_get_size(node); | ||
| 267 | |||
| 268 | for (i = 0; i < count; i++) { | ||
| 269 | subnode = plist_array_get_item(node, i); | ||
| 270 | fprintf(stream, "%*s", *indent_level, ""); | ||
| 271 | fprintf(stream, "%d: ", i); | ||
| 272 | plist_node_print_to_stream(subnode, indent_level, stream); | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | static void plist_dict_print_to_stream(plist_t node, int* indent_level, FILE* stream) | ||
| 277 | { | ||
| 278 | /* iterate over key/value pairs */ | ||
| 279 | plist_dict_iter it = NULL; | ||
| 280 | |||
| 281 | char* key = NULL; | ||
| 282 | plist_t subnode = NULL; | ||
| 283 | plist_dict_new_iter(node, &it); | ||
| 284 | plist_dict_next_item(node, it, &key, &subnode); | ||
| 285 | while (subnode) | ||
| 286 | { | ||
| 287 | fprintf(stream, "%*s", *indent_level, ""); | ||
| 288 | fprintf(stream, "%s", key); | ||
| 289 | if (plist_get_node_type(subnode) == PLIST_ARRAY) | ||
| 290 | fprintf(stream, "[%d]: ", plist_array_get_size(subnode)); | ||
| 291 | else | ||
| 292 | fprintf(stream, ": "); | ||
| 293 | free(key); | ||
| 294 | key = NULL; | ||
| 295 | plist_node_print_to_stream(subnode, indent_level, stream); | ||
| 296 | plist_dict_next_item(node, it, &key, &subnode); | ||
| 297 | } | ||
| 298 | free(it); | ||
| 299 | } | ||
| 300 | |||
| 301 | static void plist_node_print_to_stream(plist_t node, int* indent_level, FILE* stream) | ||
| 302 | { | ||
| 303 | char *s = NULL; | ||
| 304 | char *data = NULL; | ||
| 305 | double d; | ||
| 306 | uint8_t b; | ||
| 307 | uint64_t u = 0; | ||
| 308 | struct timeval tv = { 0, 0 }; | ||
| 309 | |||
| 310 | plist_type t; | ||
| 311 | |||
| 312 | if (!node) | ||
| 313 | return; | ||
| 314 | |||
| 315 | t = plist_get_node_type(node); | ||
| 316 | |||
| 317 | switch (t) { | ||
| 318 | case PLIST_BOOLEAN: | ||
| 319 | plist_get_bool_val(node, &b); | ||
| 320 | fprintf(stream, "%s\n", (b ? "true" : "false")); | ||
| 321 | break; | ||
| 322 | |||
| 323 | case PLIST_UINT: | ||
| 324 | plist_get_uint_val(node, &u); | ||
| 325 | fprintf(stream, "%"PRIu64"\n", (long long)u); | ||
| 326 | break; | ||
| 327 | |||
| 328 | case PLIST_REAL: | ||
| 329 | plist_get_real_val(node, &d); | ||
| 330 | fprintf(stream, "%f\n", d); | ||
| 331 | break; | ||
| 332 | |||
| 333 | case PLIST_STRING: | ||
| 334 | plist_get_string_val(node, &s); | ||
| 335 | fprintf(stream, "%s\n", s); | ||
| 336 | free(s); | ||
| 337 | break; | ||
| 338 | |||
| 339 | case PLIST_KEY: | ||
| 340 | plist_get_key_val(node, &s); | ||
| 341 | fprintf(stream, "%s: ", s); | ||
| 342 | free(s); | ||
| 343 | break; | ||
| 344 | |||
| 345 | case PLIST_DATA: | ||
| 346 | plist_get_data_val(node, &data, &u); | ||
| 347 | if (u > 0) { | ||
| 348 | s = base64encode((unsigned char*)data, u); | ||
| 349 | free(data); | ||
| 350 | if (s) { | ||
| 351 | fprintf(stream, "%s\n", s); | ||
| 352 | free(s); | ||
| 353 | } else { | ||
| 354 | fprintf(stream, "\n"); | ||
| 355 | } | ||
| 356 | } else { | ||
| 357 | fprintf(stream, "\n"); | ||
| 358 | } | ||
| 359 | break; | ||
| 360 | |||
| 361 | case PLIST_DATE: | ||
| 362 | plist_get_date_val(node, (int32_t*)&tv.tv_sec, (int32_t*)&tv.tv_usec); | ||
| 363 | { | ||
| 364 | time_t ti = (time_t)tv.tv_sec; | ||
| 365 | struct tm *btime = localtime(&ti); | ||
| 366 | if (btime) { | ||
| 367 | s = (char*)malloc(24); | ||
| 368 | memset(s, 0, 24); | ||
| 369 | if (strftime(s, 24, "%Y-%m-%dT%H:%M:%SZ", btime) <= 0) { | ||
| 370 | free (s); | ||
| 371 | s = NULL; | ||
| 372 | } | ||
| 373 | } | ||
| 374 | } | ||
| 375 | if (s) { | ||
| 376 | fprintf(stream, "%s\n", s); | ||
| 377 | free(s); | ||
| 378 | } else { | ||
| 379 | fprintf(stream, "\n"); | ||
| 380 | } | ||
| 381 | break; | ||
| 382 | |||
| 383 | case PLIST_ARRAY: | ||
| 384 | fprintf(stream, "\n"); | ||
| 385 | (*indent_level)++; | ||
| 386 | plist_array_print_to_stream(node, indent_level, stream); | ||
| 387 | (*indent_level)--; | ||
| 388 | break; | ||
| 389 | |||
| 390 | case PLIST_DICT: | ||
| 391 | fprintf(stream, "\n"); | ||
| 392 | (*indent_level)++; | ||
| 393 | plist_dict_print_to_stream(node, indent_level, stream); | ||
| 394 | (*indent_level)--; | ||
| 395 | break; | ||
| 396 | |||
| 397 | default: | ||
| 398 | break; | ||
| 399 | } | ||
| 400 | } | ||
| 401 | |||
| 402 | void plist_print_to_stream(plist_t plist, FILE* stream) | ||
| 403 | { | ||
| 404 | int indent = 0; | ||
| 405 | |||
| 406 | if (!plist || !stream) | ||
| 407 | return; | ||
| 408 | |||
| 409 | switch (plist_get_node_type(plist)) { | ||
| 410 | case PLIST_DICT: | ||
| 411 | plist_dict_print_to_stream(plist, &indent, stream); | ||
| 412 | break; | ||
| 413 | case PLIST_ARRAY: | ||
| 414 | plist_array_print_to_stream(plist, &indent, stream); | ||
| 415 | break; | ||
| 416 | default: | ||
| 417 | plist_node_print_to_stream(plist, &indent, stream); | ||
| 418 | } | ||
| 419 | } | ||
diff --git a/common/utils.h b/common/utils.h index c1b6c32..412eb45 100644 --- a/common/utils.h +++ b/common/utils.h | |||
| @@ -50,4 +50,6 @@ enum plist_format_t { | |||
| 50 | int plist_read_from_filename(plist_t *plist, const char *filename); | 50 | int plist_read_from_filename(plist_t *plist, const char *filename); |
| 51 | int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format); | 51 | int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format); |
| 52 | 52 | ||
| 53 | void plist_print_to_stream(plist_t plist, FILE* stream); | ||
| 54 | |||
| 53 | #endif | 55 | #endif |
diff --git a/tools/Makefile.am b/tools/Makefile.am index d87ef96..8a91234 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am | |||
| @@ -7,7 +7,7 @@ bin_PROGRAMS = idevice_id ideviceinfo idevicename idevicepair idevicesyslog idev | |||
| 7 | 7 | ||
| 8 | ideviceinfo_SOURCES = ideviceinfo.c | 8 | ideviceinfo_SOURCES = ideviceinfo.c |
| 9 | ideviceinfo_CFLAGS = $(AM_CFLAGS) | 9 | ideviceinfo_CFLAGS = $(AM_CFLAGS) |
| 10 | ideviceinfo_LDFLAGS = $(AM_LDFLAGS) | 10 | ideviceinfo_LDFLAGS = $(top_builddir)/common/libinternalcommon.la $(AM_LDFLAGS) |
| 11 | ideviceinfo_LDADD = $(top_builddir)/src/libimobiledevice.la | 11 | ideviceinfo_LDADD = $(top_builddir)/src/libimobiledevice.la |
| 12 | 12 | ||
| 13 | idevicename_SOURCES = idevicename.c | 13 | idevicename_SOURCES = idevicename.c |
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 | ||
| 71 | static const char base64_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
| 72 | static const char base64_pad = '='; | ||
| 73 | |||
| 74 | static 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 | |||
| 101 | static int indent_level = 0; | ||
| 102 | |||
| 103 | static int is_domain_known(char *domain) | 69 | static 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 | ||
| 114 | static void plist_node_to_string(plist_t node); | ||
| 115 | |||
| 116 | static 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 | |||
| 132 | static 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 | |||
| 157 | static 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 | |||
| 258 | static void print_usage(int argc, char **argv) | 80 | static 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); |
