summaryrefslogtreecommitdiff
path: root/src/app_adbdump.c
blob: 3914563c70a1cb3ef3d2a143c4d3ce760c1ac540 (plain) (blame)
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include "apk_adb.h"
#include "apk_applet.h"
#include "apk_print.h"

struct adb_dump_ctx {
	struct adb *db;
	struct adb_trust *trust;
	char prefix[128], *pfx;
};

struct adb_db_schema {
	unsigned long magic;
	const struct adb_object_schema *root;
};

static void ctx_nest(struct adb_dump_ctx *ctx, unsigned depth)
{
	while (depth--) *ctx->pfx++ = ' ';
	assert(ctx->pfx < &ctx->prefix[ARRAY_SIZE(ctx->prefix)]);
	*ctx->pfx = 0;
}

static void ctx_unnest(struct adb_dump_ctx *ctx, unsigned depth)
{
	ctx->pfx -= depth;
	assert(ctx->pfx >= ctx->prefix);
	*ctx->pfx = 0;
}

static void ctx_itemstart(struct adb_dump_ctx *ctx)
{
	ctx->pfx[-2] = '-';
}

static void ctx_itemdone(struct adb_dump_ctx *ctx)
{
	memset(ctx->prefix, ' ', ctx->pfx - ctx->prefix);
}

static void dump_object(struct adb_dump_ctx *ctx, const struct adb_object_schema *schema, adb_val_t v);
static void dump_adb(struct adb_dump_ctx *ctx);

static void dump_item(struct adb_dump_ctx *ctx, const char *name, const uint8_t *kind, adb_val_t v)
{
	struct adb db, *origdb;
	struct adb_obj o;
	struct adb_object_schema *obj_schema;
	char tmp[256];
	apk_blob_t b, nl = APK_BLOB_STR("\n");

	switch (*kind) {
	case ADB_KIND_ARRAY:
		obj_schema = container_of(kind, struct adb_object_schema, kind);
		adb_r_obj(ctx->db, v, &o, obj_schema);
		if (!adb_ra_num(&o)) return;

		fprintf(stdout, "%s%s: # %u items\n", ctx->prefix, name, adb_ra_num(&o));
		ctx_nest(ctx, 4);
		for (size_t i = ADBI_FIRST; i <= adb_ra_num(&o); i++) {
			ctx_itemstart(ctx);
			dump_item(ctx, NULL, obj_schema->fields[0].kind, adb_ro_val(&o, i));
			ctx_itemdone(ctx);
		}
		ctx_unnest(ctx, 4);
		break;
	case ADB_KIND_ADB:
		db.hdr.schema = container_of(kind, struct adb_adb_schema, kind)->schema_id;
		db.data = adb_r_blob(ctx->db, v);
		origdb = ctx->db;
		ctx->db = &db;
		dump_adb(ctx);
		ctx->db = origdb;
		break;
	case ADB_KIND_OBJECT:
		if (name) {
			fprintf(stdout, "%s%s:\n", ctx->prefix, name);
			ctx_nest(ctx, 4);
		}
		dump_object(ctx, container_of(kind, struct adb_object_schema, kind), v);
		if (name) ctx_unnest(ctx, 4);
		break;
	case ADB_KIND_BLOB:
	case ADB_KIND_INT:;
		struct adb_scalar_schema *scalar = container_of(kind, struct adb_scalar_schema, kind);
		if (scalar->tostring) {
			b = scalar->tostring(ctx->db, v, tmp, sizeof tmp);
		} else {
			b = APK_BLOB_STR("(unknown)");
		}
		if (!APK_BLOB_IS_NULL(b)) {
			fputs(ctx->prefix, stdout);
			if (name) fprintf(stdout, "%s: ", name);
			if (b.len >= 60 || apk_blob_chr(b, '\n')) {
				/* long or multiline */
				apk_blob_t l;
				fprintf(stdout, "|\n");
				ctx_itemdone(ctx);
				ctx_nest(ctx, 4);
				while (apk_blob_split(b, nl, &l, &b)) {
					fprintf(stdout, "%s"BLOB_FMT"\n",
						ctx->prefix, BLOB_PRINTF(l));
				}
				if (b.len) {
					fprintf(stdout, "%s"BLOB_FMT"\n",
						ctx->prefix, BLOB_PRINTF(b));
				}
				ctx_unnest(ctx, 4);
			} else {
				fwrite(b.ptr, 1, b.len, stdout);
				fputc('\n', stdout);
			}
		}
		break;
	}
}

static void dump_object(struct adb_dump_ctx *ctx, const struct adb_object_schema *schema, adb_val_t v)
{
	size_t schema_len = 0;
	struct adb_obj o;
	char tmp[256];
	apk_blob_t b;

	adb_r_obj(ctx->db, v, &o, schema);
	if (schema) {
		if (schema->tostring) {
			b = schema->tostring(&o, tmp, sizeof tmp);
			if (!APK_BLOB_IS_NULL(b))
				fprintf(stdout, "%s"BLOB_FMT"\n", ctx->prefix, BLOB_PRINTF(b));
			ctx_itemdone(ctx);
			return;
		}
		schema_len = schema->num_fields;
	}

	for (size_t i = ADBI_FIRST; i < adb_ro_num(&o); i++) {
		adb_val_t val = adb_ro_val(&o, i);
		if (val == ADB_NULL) continue;
		if (i < schema_len && schema->fields[i-1].kind != 0) {
			dump_item(ctx, schema->fields[i-1].name, schema->fields[i-1].kind, val);
			ctx_itemdone(ctx);
		}
	}
}

static const struct adb_db_schema dbschemas[] = {
	{ .magic = ADB_SCHEMA_INDEX,		.root = &schema_index, },
	{ .magic = ADB_SCHEMA_INSTALLED_DB,	.root = &schema_idb, },
	{ .magic = ADB_SCHEMA_PACKAGE,		.root = &schema_package },
};

static void dump_adb(struct adb_dump_ctx *ctx)
{
	struct adb_block *blk;
	struct adb_sign_hdr *s;
	struct adb_verify_ctx vfy = {};
	const struct adb_db_schema *ds;
	unsigned char *id;
	uint32_t schema_magic = ctx->db->hdr.schema;
	int r;

	for (ds = dbschemas; ds < &dbschemas[ARRAY_SIZE(dbschemas)]; ds++)
		if (ds->magic == schema_magic) break;
	if (ds >= &dbschemas[ARRAY_SIZE(dbschemas)]) ds = NULL;

	adb_foreach_block(blk, ctx->db->data) {
		apk_blob_t b = APK_BLOB_PTR_LEN((char*)(blk+1), ADB_BLOCK_SIZE(blk));
		switch (ADB_BLOCK_TYPE(blk)) {
		case ADB_BLOCK_ADB:
			fprintf(stdout, "%s# ADB block, size: %d\n", ctx->prefix, ADB_BLOCK_SIZE(blk));
			ctx_itemdone(ctx);
			ctx->db->adb = b;
			if (ds)
				dump_object(ctx, ds->root, adb_r_root(ctx->db));
			else
				fprintf(stdout, "%s# Unrecognized schema: 0x%08x\n", ctx->prefix, schema_magic);
			break;
		case ADB_BLOCK_SIG:
			s = (struct adb_sign_hdr*) b.ptr;
			fprintf(stdout, "%s# signature: v%d ", ctx->prefix, s->sign_ver);
			ctx_itemdone(ctx);
			r = adb_trust_verify_signature(ctx->trust, ctx->db, &vfy, b);
			switch (s->sign_ver) {
			case 0:
				id = (unsigned char*)(s + 1);
				for (size_t j = 0; j < 16; j++)
					fprintf(stdout, "%02x", id[j]);
				break;
			default:
				break;
			}
			fprintf(stdout, ": %s\n", r ? apk_error_str(r) : "OK");
			break;
		default:
			fprintf(stdout, "%s# unknown block %d, size: %d\n",
				ctx->prefix, ADB_BLOCK_TYPE(blk), ADB_BLOCK_SIZE(blk));
			ctx_itemdone(ctx);
		}
	}
	if (IS_ERR(blk)) fprintf(stdout, "%s# block enumeration error: corrupt data area\n", ctx->prefix);
}

static int mmap_and_dump_adb(struct adb_trust *trust, int fd)
{
	struct adb db;
	struct adb_dump_ctx ctx = {
		.db = &db,
		.pfx = ctx.prefix,
		.trust = trust,
	};
	int r;

	r = adb_m_map(&db, fd, 0, NULL);
	if (r) return r;

	dump_adb(&ctx);
	adb_free(&db);
	return 0;
}

static int adbdump_main(void *pctx, struct apk_database *db, struct apk_string_array *args)
{
	char **arg;
	int r;

	foreach_array_item(arg, args) {
		r = mmap_and_dump_adb(&db->trust, open(*arg, O_RDONLY));
		if (r) {
			apk_error("%s: %s", *arg, apk_error_str(r));
			return r;
		}
	}

	return 0;
}

static struct apk_applet apk_adbdump = {
	.name = "adbdump",
	.open_flags = APK_OPENF_READ | APK_OPENF_NO_STATE | APK_OPENF_NO_REPOS,
	.main = adbdump_main,
};
APK_DEFINE_APPLET(apk_adbdump);