diff options
Diffstat (limited to 'src/xplist.c')
| -rw-r--r-- | src/xplist.c | 312 |
1 files changed, 0 insertions, 312 deletions
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 | |||
| 36 | const 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 | */ | ||
| 52 | char *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 | |||
| 83 | struct 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 | */ | ||
| 92 | xmlDocPtr 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 | */ | ||
| 109 | void free_plist(xmlDocPtr plist) | ||
| 110 | { | ||
| 111 | if (!plist) | ||
| 112 | return; | ||
| 113 | |||
| 114 | xmlFreeDoc(plist); | ||
| 115 | } | ||
| 116 | |||
| 117 | void 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 | |||
| 212 | void 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 | |||
| 293 | void 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 | |||
| 306 | void 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 | } | ||
