summaryrefslogtreecommitdiffstats
path: root/src/db-parse-context.c
blob: 88009c97a5c3bb2d639e89835156bd76e36fd208 (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
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
/*
 *  Copyright (C) 2005 Christophe Fergeau
 *
 * 
 *  The code contained in this file is free software; you can redistribute
 *  it and/or modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either version
 *  2.1 of the License, or (at your option) any later version.
 *  
 *  This file is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this code; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 *  iTunes and iPod are trademarks of Apple
 * 
 *  This product is not supported/written/published by Apple!
 *
 */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <sys/stat.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <glib.h>
#include <glib/gstdio.h>
#include "db-parse-context.h"
#include "db-itunes-parser.h"
#include "itdb_endianness.h"

DBParseContext *
db_parse_context_new (const unsigned char *buffer, off_t len, guint byte_order)
{
	DBParseContext *result;

	result = g_new0 (DBParseContext, 1);
	if (result == NULL) {
		return NULL;
	}

	result->buffer = buffer;
	result->cur_pos = buffer;
	result->total_len = len;
	result->byte_order = byte_order;

	return result;
}

void
db_parse_context_destroy (DBParseContext *ctx) 
{
	g_return_if_fail (ctx != NULL);

	if (ctx->mapped_file) {
		g_mapped_file_free(ctx->mapped_file);
	}

	g_free (ctx);
}

static void
db_parse_context_set_header_len (DBParseContext *ctx, off_t len)
{
	/* FIXME: this can probably happen in malformed itunesdb files, 
	 * don't g_assert on this, only output a warning 
	 */
	g_assert ((ctx->cur_pos - ctx->buffer) <= len);
	g_assert (len <= ctx->total_len);
	ctx->header_len = len;
}

void
db_parse_context_set_total_len (DBParseContext *ctx, off_t len)
{
	/* FIXME: this can probably happen in malformed itunesdb files, 
	 * don't g_assert on this, only output a warning 
	 */
	g_assert ((ctx->cur_pos - ctx->buffer) <= len);
	if (ctx->header_len != 0) {
		g_assert (len >= ctx->header_len);
	}
	ctx->total_len = len;
}


off_t
db_parse_context_get_remaining_length (DBParseContext *ctx)
{
	if (ctx->header_len != 0) {
		return ctx->header_len - (ctx->cur_pos - ctx->buffer); 
	} else {
		return ctx->total_len - (ctx->cur_pos - ctx->buffer); 
	}
}

DBParseContext *
db_parse_context_get_sub_context (DBParseContext *ctx, off_t offset)
{
	DBParseContext *sub_ctx;

	if (offset >= ctx->total_len) {
		return NULL;
	}
	sub_ctx = db_parse_context_new (&ctx->buffer[offset], 
				     ctx->total_len - offset, 
				     ctx->byte_order);
	sub_ctx->db = ctx->db;
	sub_ctx->artwork = ctx->artwork;
	return sub_ctx;
}


DBParseContext *
db_parse_context_get_next_child (DBParseContext *ctx)
{
	if (ctx->header_len == 0) {
		return NULL;
	}
	if (ctx->header_len >= ctx->total_len) {
		return NULL;
	}

	return db_parse_context_get_sub_context (ctx, ctx->header_len);
}

void *
db_parse_context_get_m_header_internal (DBParseContext *ctx, const char *id, off_t size) 
{
	MHeader *h;
	char *header_id;

	if (db_parse_context_get_remaining_length (ctx) < 8) {
		return NULL;
	}

	h = (MHeader *)ctx->cur_pos;
	header_id = g_strndup ((char *)h->header_id, 4);
	if (ctx->byte_order == G_BIG_ENDIAN) {
		g_strreverse (header_id);
	}
	if (strncmp (id, header_id, 4) != 0) {
	        g_free (header_id);
		return NULL;
	}

	g_free (header_id);

	/* FIXME: this test sucks for compat: if a field is smaller than 
	 * expected, we probably should create a buffer of the appropriate 
	 * size inited to 0, copy the data that is available in it and use
	 * that buffer in the rest of the code (maybe it's harmful to have
	 * some fields at 0 in some headers though...)
	 */
	if (get_gint32 (h->header_len, ctx->byte_order) < size) {
		return NULL;
	}

	db_parse_context_set_header_len (ctx, get_gint32 (h->header_len, 
							  ctx->byte_order));

	return h;
}

DBParseContext *
db_parse_context_new_from_file (const char *filename, Itdb_DB *db)
{
	DBParseContext *ctx;
	Itdb_Device *device;
	GError* error;
	GMappedFile* mapped_file;
	struct stat stat_buf;

	ctx = NULL;
	error = NULL;
	mapped_file = NULL;

	device = db_get_device (db);
	g_return_val_if_fail (device, NULL);

	if (g_stat (filename, &stat_buf) != 0) {
		return NULL;	
	};
	if (stat_buf.st_size > 64 * 1024 * 1024) {
		g_warning ("%s is too big to be mmapped (%llu bytes)\n",
			   filename, (unsigned long long)stat_buf.st_size);
		return NULL;
	}

	mapped_file = g_mapped_file_new(filename, FALSE, &error);
	
	if (mapped_file == NULL) {
		g_print ("Error while mapping %s: %s\n", filename, 
                    error->message);
		g_error_free(error);
		return NULL;
	}

	if (device->byte_order == 0)
	    itdb_device_autodetect_endianess (device);

	ctx = db_parse_context_new ((guchar *)g_mapped_file_get_contents(mapped_file),
					g_mapped_file_get_length(mapped_file), 
					device->byte_order);

	if (ctx == NULL) {
		g_mapped_file_free(mapped_file);
		return NULL;
	}
	ctx->db = db;
	ctx->mapped_file = mapped_file;

        return ctx;
}