Skip to content

Commit

Permalink
Remove plist-related code in favor of new libplist API
Browse files Browse the repository at this point in the history
  • Loading branch information
nikias committed Apr 19, 2023
1 parent 6fcb879 commit 44f2f75
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 251 deletions.
12 changes: 0 additions & 12 deletions include/libimobiledevice-glue/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#endif

#include <stdio.h>
#include <plist/plist.h>

#define MAC_EPOCH 978307200

Expand All @@ -48,15 +47,4 @@ char *generate_uuid(void);
int buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length);
int 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);

void plist_print_to_stream(plist_t plist, FILE* stream);
void plist_print_to_stream_with_indentation(plist_t plist, FILE* stream, unsigned int indentation);

#endif
240 changes: 1 addition & 239 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Miscellaneous utilities for string manipulation,
* file I/O and plist helper.
*
* Copyright (c) 2014-2019 Nikias Bassen, All Rights Reserved.
* Copyright (c) 2014-2023 Nikias Bassen, All Rights Reserved.
* Copyright (c) 2013-2014 Martin Szulecki, All Rights Reserved.
* Copyright (c) 2013 Federico Mena Quintero
*
Expand Down Expand Up @@ -318,241 +318,3 @@ LIBIMOBILEDEVICE_GLUE_API int buffer_write_to_filename(const char *filename, con
return 0;
}
}

LIBIMOBILEDEVICE_GLUE_API int plist_read_from_filename(plist_t *plist, const char *filename)
{
char *buffer = NULL;
uint64_t length;

if (!filename)
return 0;

if (!buffer_read_from_filename(filename, &buffer, &length)) {
return 0;
}

plist_from_memory(buffer, length, plist);

free(buffer);

return 1;
}

LIBIMOBILEDEVICE_GLUE_API 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;

int res = buffer_write_to_filename(filename, buffer, length);

free(buffer);

return res;
}

static const char base64_str[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const char base64_pad = '=';

static char *base64encode(const unsigned char *buf, size_t size)
{
if (!buf || !(size > 0)) return NULL;
int outlen = (size / 3) * 4;
char *outbuf = (char*)malloc(outlen+5); // 4 spare bytes + 1 for '\0'
size_t n = 0;
size_t m = 0;
unsigned char input[3];
unsigned int output[4];
while (n < size) {
input[0] = buf[n];
input[1] = (n+1 < size) ? buf[n+1] : 0;
input[2] = (n+2 < size) ? buf[n+2] : 0;
output[0] = input[0] >> 2;
output[1] = ((input[0] & 3) << 4) + (input[1] >> 4);
output[2] = ((input[1] & 15) << 2) + (input[2] >> 6);
output[3] = input[2] & 63;
outbuf[m++] = base64_str[(int)output[0]];
outbuf[m++] = base64_str[(int)output[1]];
outbuf[m++] = (n+1 < size) ? base64_str[(int)output[2]] : base64_pad;
outbuf[m++] = (n+2 < size) ? base64_str[(int)output[3]] : base64_pad;
n+=3;
}
outbuf[m] = 0; // 0-termination!
return outbuf;
}

static void plist_node_print_to_stream(plist_t node, int* indent_level, FILE* stream);

static void plist_array_print_to_stream(plist_t node, int* indent_level, FILE* stream)
{
/* iterate over items */
int i, count;
plist_t subnode = NULL;

count = plist_array_get_size(node);

for (i = 0; i < count; i++) {
subnode = plist_array_get_item(node, i);
fprintf(stream, "%*s", *indent_level, "");
fprintf(stream, "%d: ", i);
plist_node_print_to_stream(subnode, indent_level, stream);
}
}

static void plist_dict_print_to_stream(plist_t node, int* indent_level, FILE* stream)
{
/* iterate over key/value pairs */
plist_dict_iter it = NULL;

char* key = NULL;
plist_t subnode = NULL;
plist_dict_new_iter(node, &it);
plist_dict_next_item(node, it, &key, &subnode);
while (subnode)
{
fprintf(stream, "%*s", *indent_level, "");
fprintf(stream, "%s", key);
if (plist_get_node_type(subnode) == PLIST_ARRAY)
fprintf(stream, "[%d]: ", plist_array_get_size(subnode));
else
fprintf(stream, ": ");
free(key);
key = NULL;
plist_node_print_to_stream(subnode, indent_level, stream);
plist_dict_next_item(node, it, &key, &subnode);
}
free(it);
}

static void plist_node_print_to_stream(plist_t node, int* indent_level, FILE* stream)
{
char *s = NULL;
char *data = NULL;
double d;
uint8_t b;
uint64_t u = 0;
struct timeval tv = { 0, 0 };

plist_type t;

if (!node)
return;

t = plist_get_node_type(node);

switch (t) {
case PLIST_BOOLEAN:
plist_get_bool_val(node, &b);
fprintf(stream, "%s\n", (b ? "true" : "false"));
break;

case PLIST_UINT:
plist_get_uint_val(node, &u);
fprintf(stream, "%"PRIu64"\n", u);
break;

case PLIST_REAL:
plist_get_real_val(node, &d);
fprintf(stream, "%f\n", d);
break;

case PLIST_STRING:
plist_get_string_val(node, &s);
fprintf(stream, "%s\n", s);
free(s);
break;

case PLIST_KEY:
plist_get_key_val(node, &s);
fprintf(stream, "%s: ", s);
free(s);
break;

case PLIST_DATA:
plist_get_data_val(node, &data, &u);
if (u > 0) {
s = base64encode((unsigned char*)data, u);
free(data);
if (s) {
fprintf(stream, "%s\n", s);
free(s);
} else {
fprintf(stream, "\n");
}
} else {
fprintf(stream, "\n");
}
break;

case PLIST_DATE:
plist_get_date_val(node, (int32_t*)&tv.tv_sec, (int32_t*)&tv.tv_usec);
{
time_t ti = (time_t)tv.tv_sec + MAC_EPOCH;
struct tm *btime = localtime(&ti);
if (btime) {
s = (char*)malloc(24);
memset(s, 0, 24);
if (strftime(s, 24, "%Y-%m-%dT%H:%M:%SZ", btime) <= 0) {
free (s);
s = NULL;
}
}
}
if (s) {
fprintf(stream, "%s\n", s);
free(s);
} else {
fprintf(stream, "\n");
}
break;

case PLIST_ARRAY:
fprintf(stream, "\n");
(*indent_level)++;
plist_array_print_to_stream(node, indent_level, stream);
(*indent_level)--;
break;

case PLIST_DICT:
fprintf(stream, "\n");
(*indent_level)++;
plist_dict_print_to_stream(node, indent_level, stream);
(*indent_level)--;
break;

default:
break;
}
}

LIBIMOBILEDEVICE_GLUE_API void plist_print_to_stream_with_indentation(plist_t plist, FILE* stream, unsigned int indentation)
{
if (!plist || !stream)
return;

int indent = indentation;
switch (plist_get_node_type(plist)) {
case PLIST_DICT:
plist_dict_print_to_stream(plist, &indent, stream);
break;
case PLIST_ARRAY:
plist_array_print_to_stream(plist, &indent, stream);
break;
default:
plist_node_print_to_stream(plist, &indent, stream);
}
}

LIBIMOBILEDEVICE_GLUE_API void plist_print_to_stream(plist_t plist, FILE* stream)
{
plist_print_to_stream_with_indentation(plist, stream, 0);
}

0 comments on commit 44f2f75

Please sign in to comment.