summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorGravatar Martin Szulecki2013-08-18 05:16:06 +0200
committerGravatar Martin Szulecki2013-09-17 11:43:33 +0200
commitdf539bdbc7708fb05eea22eec62d6e2f609c10b5 (patch)
tree6ce9e7e4a5960c051af67821f5e3a66fe2adc5bb /common
parentc7c49ae95ca05750fa5609d39c28b148f93dbe15 (diff)
downloadlibimobiledevice-df539bdbc7708fb05eea22eec62d6e2f609c10b5.tar.gz
libimobiledevice-df539bdbc7708fb05eea22eec62d6e2f609c10b5.tar.bz2
common: Add helpers to read and write plist files
Diffstat (limited to 'common')
-rw-r--r--common/utils.c88
-rw-r--r--common/utils.h18
2 files changed, 106 insertions, 0 deletions
diff --git a/common/utils.c b/common/utils.c
index bd2bf1f..5248c2c 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -106,3 +106,91 @@ char *string_concat(const char *str, ...)
return result;
}
+
+void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length)
+{
+ FILE *f;
+ uint64_t size;
+
+ *length = 0;
+
+ f = fopen(filename, "rb");
+ if (!f) {
+ return;
+ }
+
+ fseek(f, 0, SEEK_END);
+ size = ftell(f);
+ rewind(f);
+
+ if (size == 0) {
+ fclose(f);
+ return;
+ }
+
+ *buffer = (char*)malloc(sizeof(char)*(size+1));
+ fread(*buffer, sizeof(char), size, f);
+ fclose(f);
+
+ *length = size;
+}
+
+void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length)
+{
+ FILE *f;
+
+ f = fopen(filename, "ab");
+ if (!f)
+ f = fopen(filename, "wb");
+ if (f) {
+ fwrite(buffer, sizeof(char), length, f);
+ fclose(f);
+ }
+}
+
+int plist_read_from_filename(plist_t *plist, const char *filename)
+{
+ char *buffer = NULL;
+ uint64_t length;
+
+ if (!filename)
+ return 0;
+
+ buffer_read_from_filename(filename, &buffer, &length);
+
+ if (!buffer) {
+ return 0;
+ }
+
+ if ((length > 8) && (memcmp(buffer, "bplist00", 8) == 0)) {
+ plist_from_bin(buffer, length, plist);
+ } else {
+ plist_from_xml(buffer, length, plist);
+ }
+
+ free(buffer);
+
+ return 1;
+}
+
+int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format)
+{
+ char *buffer = NULL;
+ uint32_t length;
+
+ if (!plist || !filename)
+ return 0;
+
+ if (format == PLIST_FORMAT_XML)
+ plist_to_xml(plist, &buffer, &length);
+ else if (format == PLIST_FORMAT_BINARY)
+ plist_to_bin(plist, &buffer, &length);
+ else
+ return 0;
+
+ buffer_write_to_filename(filename, buffer, length);
+
+ free(buffer);
+
+ return 1;
+}
diff --git a/common/utils.h b/common/utils.h
index 441d188..8394272 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -22,9 +22,27 @@
#ifndef __UTILS_H
#define __UTILS_H
+#ifdef WIN32
+#include <windows.h>
+#endif
+
+#include <stdio.h>
+#include <plist/plist.h>
+
#ifndef HAVE_STPCPY
char *stpcpy(char * s1, const char * s2);
#endif
char *string_concat(const char *str, ...);
+void buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length);
+void buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length);
+
+enum plist_format_t {
+ PLIST_FORMAT_XML,
+ PLIST_FORMAT_BINARY
+};
+
+int plist_read_from_filename(plist_t *plist, const char *filename);
+int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format);
+
#endif