summaryrefslogtreecommitdiffstats
path: root/dev/plutil.c
blob: 1c7b1405abb5f41bea4dfee505afec2c4ef7957a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 * main.c for plistutil
 * right now just prints debug shit
 */

#include "../src/plist.h"
#include "plutil.h"

int debug = 0;

void print_nodes(bplist_node *root_node) {
	// Yay, great. Let's print the list of nodes recursively...
	int i = 0;
	if (!root_node) return; // or not, because the programmer's stupid.
	
	switch (root_node->type) {
		case BPLIST_DICT:
			printf("Dictionary node.\nLength %i\n", root_node->length);
			for (i = 0; i < (root_node->length * 2); i+=2) {
				// HI!
				printf("Key: ");
				print_nodes(root_node->subnodes[i]);
				printf("Value: ");
				print_nodes(root_node->subnodes[i+1]);
			}
			printf("End dictionary node.\n\n");
			break;
		
		case BPLIST_ARRAY:
			printf("Array node.\n");
			for (i = 0; i < root_node->length; i++) {
				printf("\tElement %i: ", i);
				print_nodes(root_node->subnodes[i]);
			}
			break;
			
		case BPLIST_INT:
			if (root_node->length == sizeof(uint8_t)) {
				printf("Integer: %i\n", root_node->intval8);
			} else if (root_node->length == sizeof(uint16_t)) {
				printf("Integer: %i\n", root_node->intval16);
			} else if (root_node->length == sizeof(uint32_t)) {
				printf("Integer: %i\n", root_node->intval32);
			}
			break;
		
		case BPLIST_STRING:
			printf("String: ");
			fwrite(root_node->strval, sizeof(char), root_node->length, stdout);
			fflush(stdout);
			printf("\n");
			break;

		case BPLIST_DATA:
			printf("Data: ");
			char* data = g_base64_encode(root_node->strval,root_node->length);
			fwrite(format_string(data, 60, 0), sizeof(char), strlen(data), stdout);
			fflush(stdout);
			printf("\n");
			break;
		
		case BPLIST_UNICODE:
			printf("Unicode data, may appear crappy: ");
			fwrite(root_node->unicodeval, sizeof(wchar_t), root_node->length, stdout);
			fflush(stdout);
			printf("\n");
			break;
		
		case BPLIST_TRUE:
			printf("True.\n");
			break;
		
		case BPLIST_FALSE:
			printf("False.\n");
			break;
		
		case BPLIST_REAL:
		case BPLIST_DATE:
			printf("Real(?): %f\n", root_node->realval);
			break;
			
		default:
			printf("oops\nType set to %x and length is %i\n", root_node->type, root_node->length);
			break;
	}
}

int main(int argc, char *argv[]) {
	struct stat *filestats = (struct stat *)malloc(sizeof(struct stat));
	uint32_t position = 0;
	Options *options = parse_arguments(argc, argv);
	int argh = 0;
	
	printf("plistutil version 0.2 written by FxChiP\n");
	
	if (!options) {
		print_usage();
		return 0;
	}

	debug = options->debug;
	
	FILE *bplist = fopen(options->in_file, "r");
	
	stat(options->in_file, filestats);

	printf("here?\n");
	char *bplist_entire = (char*)malloc(sizeof(char) * (filestats->st_size + 1));
	//argh = fgets(bplist_entire, filestats->st_size, bplist);
	argh = fread(bplist_entire, sizeof(char), filestats->st_size, bplist);
	printf("read %i bytes\n", argh);
	fclose(bplist);
	printf("or here?\n");
	// bplist_entire contains our stuff
	 bplist_node *root_node;
	 root_node = parse_nodes(bplist_entire, filestats->st_size, &position);
	 printf("plutil debug mode\n\n");
	 printf("file size %i\n\n", filestats->st_size);
	 if (!root_node) {
	 	printf("Invalid binary plist (or some other error occurred.)\n");
	 	return 0;
	}
	 print_nodes(root_node);
	 return 0;
 }

Options *parse_arguments(int argc, char *argv[]) {
	int i = 0;
	
	Options *options = (Options*)malloc(sizeof(Options));
	memset(options, 0, sizeof(Options));
	
	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "--infile") || !strcmp(argv[i], "-i")) {
			if ((i+1) == argc) {
				free(options);
				return NULL;
			}
			options->in_file = argv[i+1];
			i++;
			continue;
		}
		
		if (!strcmp(argv[i], "--outfile") || !strcmp(argv[i], "-o")) {
			if ((i+1) == argc) {
				free(options);
				return NULL;
			}
			options->out_file = argv[i+1];
			i++;
			continue;
		}
		
		if (!strcmp(argv[i], "--debug") || !strcmp(argv[i], "-d") || !strcmp(argv[i], "-v")) {
			options->debug = 1;
		}
		
		if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
			free(options);
			return NULL;
		}
	}
	
	if (!options->in_file /*|| !options->out_file*/) {
		free(options);
		return NULL;
	}
	
	return options;
}

void print_usage() {
	printf("Usage: plistutil -i|--infile in_file.plist -o|--outfile out_file.plist [--debug]\n");
	printf("\n");
	printf("\t-i or --infile: The file to read in.\n");
	printf("\t-o or --outfile: The file to convert to.\n");
	printf("\t-d, -v or --debug: Provide extended debug information.\n\n");
}