dmap-structure.c 27.3 KB
Newer Older
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
/*
 * Copyright (C) 2004,2005 Charles Schmidt <cschmidt2@emich.edu>
 * Copyright (C) 2006 INDT
 *  Andre Moreira Magalhaes <andre.magalhaes@indt.org.br>
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA*
 */

#include "dmap-structure.h"
#include "dmap-utils.h"

#include <glib.h>
#include <glib-object.h>
#include <gobject/gvaluecollector.h>

#include <string.h>
#include <stdarg.h>

#define MAKE_CONTENT_CODE(ch0, ch1, ch2, ch3) \
    (( (gint32)(gchar)(ch0) | ( (gint32)(gchar)(ch1) << 8 ) | \
    ( (gint32)(gchar)(ch2) << 16 ) | \
    ( (gint32)(gchar)(ch3) << 24 ) ))

static const DMAPContentCodeDefinition cc_defs[] = {
    {DMAP_RAW, 0, "", "", DMAP_TYPE_STRING},
    {DMAP_CC_MDCL, MAKE_CONTENT_CODE('m','d','c','l'), "dmap.dictionary", "mdcl", DMAP_TYPE_CONTAINER},
    {DMAP_CC_MSTT, MAKE_CONTENT_CODE('m','s','t','t'), "dmap.status", "mstt", DMAP_TYPE_INT},
    {DMAP_CC_MIID, MAKE_CONTENT_CODE('m','i','i','d'), "dmap.itemid", "miid", DMAP_TYPE_INT},
    {DMAP_CC_MINM, MAKE_CONTENT_CODE('m','i','n','m'), "dmap.itemname", "minm", DMAP_TYPE_STRING},
    {DMAP_CC_MIKD, MAKE_CONTENT_CODE('m','i','k','d'), "dmap.itemkind", "mikd", DMAP_TYPE_BYTE},
    {DMAP_CC_MPER, MAKE_CONTENT_CODE('m','p','e','r'), "dmap.persistentid", "mper", DMAP_TYPE_INT64},
    {DMAP_CC_MCON, MAKE_CONTENT_CODE('m','c','o','n'), "dmap.container", "mcon", DMAP_TYPE_CONTAINER},
    {DMAP_CC_MCTI, MAKE_CONTENT_CODE('m','c','t','i'), "dmap.containeritemid", "mcti", DMAP_TYPE_INT},
    {DMAP_CC_MPCO, MAKE_CONTENT_CODE('m','p','c','o'), "dmap.parentcontainerid", "mpco", DMAP_TYPE_INT},
    {DMAP_CC_MSTS, MAKE_CONTENT_CODE('m','s','t','s'), "dmap.statusstring", "msts", DMAP_TYPE_STRING},
    {DMAP_CC_MIMC, MAKE_CONTENT_CODE('m','i','m','c'), "dmap.itemcount", "mimc", DMAP_TYPE_INT},
    {DMAP_CC_MCTC, MAKE_CONTENT_CODE('m','c','t','c'), "dmap.containercount", "mctc", DMAP_TYPE_INT},
    {DMAP_CC_MRCO, MAKE_CONTENT_CODE('m','r','c','o'), "dmap.returnedcount", "mrco", DMAP_TYPE_INT},
    {DMAP_CC_MTCO, MAKE_CONTENT_CODE('m','t','c','o'), "dmap.specifiedtotalcount", "mtco", DMAP_TYPE_INT},
    {DMAP_CC_MLCL, MAKE_CONTENT_CODE('m','l','c','l'), "dmap.listing", "mlcl", DMAP_TYPE_CONTAINER},
    {DMAP_CC_MLIT, MAKE_CONTENT_CODE('m','l','i','t'), "dmap.listingitem", "mlit", DMAP_TYPE_CONTAINER},
    {DMAP_CC_MBCL, MAKE_CONTENT_CODE('m','b','c','l'), "dmap.bag", "mbcl", DMAP_TYPE_CONTAINER},
    {DMAP_CC_MSRV, MAKE_CONTENT_CODE('m','s','r','v'), "dmap.serverinforesponse", "msrv", DMAP_TYPE_CONTAINER},
    {DMAP_CC_MSAU, MAKE_CONTENT_CODE('m','s','a','u'), "dmap.authenticationmethod", "msau", DMAP_TYPE_BYTE},
    {DMAP_CC_MSLR, MAKE_CONTENT_CODE('m','s','l','r'), "dmap.loginrequired", "mslr", DMAP_TYPE_BYTE},
    {DMAP_CC_MPRO, MAKE_CONTENT_CODE('m','p','r','o'), "dmap.protocolversion", "mpro", DMAP_TYPE_VERSION},
    {DMAP_CC_MSAL, MAKE_CONTENT_CODE('m','s','a','l'), "dmap.supportsautologout", "msal", DMAP_TYPE_BYTE},
    {DMAP_CC_MSUP, MAKE_CONTENT_CODE('m','s','u','p'), "dmap.supportsupdate", "msup", DMAP_TYPE_BYTE},
    {DMAP_CC_MSPI, MAKE_CONTENT_CODE('m','s','p','i'), "dmap.supportspersistenids", "mspi", DMAP_TYPE_BYTE},
    {DMAP_CC_MSEX, MAKE_CONTENT_CODE('m','s','e','x'), "dmap.supportsextensions", "msex", DMAP_TYPE_BYTE},
    {DMAP_CC_MSBR, MAKE_CONTENT_CODE('m','s','b','r'), "dmap.supportsbrowse", "msbr", DMAP_TYPE_BYTE},
    {DMAP_CC_MSQY, MAKE_CONTENT_CODE('m','s','q','y'), "dmap.supportsquery", "msqy", DMAP_TYPE_BYTE},
    {DMAP_CC_MSIX, MAKE_CONTENT_CODE('m','s','i','x'), "dmap.supportsindex", "msix", DMAP_TYPE_BYTE},
    {DMAP_CC_MSRS, MAKE_CONTENT_CODE('m','s','r','s'), "dmap.supportsresolve", "msrs", DMAP_TYPE_BYTE},
    {DMAP_CC_MSTM, MAKE_CONTENT_CODE('m','s','t','m'), "dmap.timeoutinterval", "mstm", DMAP_TYPE_INT},
    {DMAP_CC_MSDC, MAKE_CONTENT_CODE('m','s','d','c'), "dmap.databasescount", "msdc", DMAP_TYPE_INT},
    {DMAP_CC_MCCR, MAKE_CONTENT_CODE('m','c','c','r'), "dmap.contentcodesresponse", "mccr", DMAP_TYPE_CONTAINER},
    {DMAP_CC_MCNM, MAKE_CONTENT_CODE('m','c','n','m'), "dmap.contentcodesnumber", "mcnm", DMAP_TYPE_INT},
    {DMAP_CC_MCNA, MAKE_CONTENT_CODE('m','c','n','a'), "dmap.contentcodesname", "mcna", DMAP_TYPE_STRING},
    {DMAP_CC_MCTY, MAKE_CONTENT_CODE('m','c','t','y'), "dmap.contentcodestype", "mcty", DMAP_TYPE_SHORT},
    {DMAP_CC_MLOG, MAKE_CONTENT_CODE('m','l','o','g'), "dmap.loginresponse", "mlog", DMAP_TYPE_CONTAINER},
    {DMAP_CC_MLID, MAKE_CONTENT_CODE('m','l','i','d'), "dmap.sessionid", "mlid", DMAP_TYPE_INT},
    {DMAP_CC_MUPD, MAKE_CONTENT_CODE('m','u','p','d'), "dmap.updateresponse", "mupd", DMAP_TYPE_CONTAINER},
    {DMAP_CC_MUSR, MAKE_CONTENT_CODE('m','u','s','r'), "dmap.serverrevision", "musr", DMAP_TYPE_INT},
    {DMAP_CC_MUTY, MAKE_CONTENT_CODE('m','u','t','y'), "dmap.updatetype", "muty", DMAP_TYPE_BYTE},
    {DMAP_CC_MUDL, MAKE_CONTENT_CODE('m','u','d','l'), "dmap.deletedidlisting", "mudl", DMAP_TYPE_CONTAINER},
    {DMAP_CC_APRO, MAKE_CONTENT_CODE('a','p','r','o'), "daap.protocolversion", "apro", DMAP_TYPE_VERSION},
    {DMAP_CC_AVDB, MAKE_CONTENT_CODE('a','v','d','b'), "daap.serverdatabases", "avdb", DMAP_TYPE_CONTAINER},
    {DMAP_CC_ABRO, MAKE_CONTENT_CODE('a','b','r','o'), "daap.databasebrowse", "abro", DMAP_TYPE_CONTAINER},
    {DMAP_CC_ABAL, MAKE_CONTENT_CODE('a','b','a','l'), "daap.browsealbumlisting", "abal", DMAP_TYPE_CONTAINER},
    {DMAP_CC_ABAR, MAKE_CONTENT_CODE('a','b','a','r'), "daap.browseartistlisting", "abar", DMAP_TYPE_CONTAINER},
    {DMAP_CC_ABCP, MAKE_CONTENT_CODE('a','b','c','p'), "daap.browsecomposerlisting", "abcp", DMAP_TYPE_CONTAINER},
    {DMAP_CC_ABGN, MAKE_CONTENT_CODE('a','b','g','n'), "daap.browsegenrelisting", "abgn", DMAP_TYPE_CONTAINER},
    {DMAP_CC_ADBS, MAKE_CONTENT_CODE('a','d','b','s'), "daap.returndatabasesongs", "adbs", DMAP_TYPE_CONTAINER},
    {DMAP_CC_ASAL, MAKE_CONTENT_CODE('a','s','a','l'), "daap.songalbum", "asal", DMAP_TYPE_STRING},
    {DMAP_CC_ASAR, MAKE_CONTENT_CODE('a','s','a','r'), "daap.songartist", "asar", DMAP_TYPE_STRING},
    {DMAP_CC_ASBT, MAKE_CONTENT_CODE('a','s','b','t'), "daap.songsbeatsperminute", "asbt", DMAP_TYPE_SHORT},
    {DMAP_CC_ASBR, MAKE_CONTENT_CODE('a','s','b','r'), "daap.songbitrate", "asbr", DMAP_TYPE_SHORT},
    {DMAP_CC_ASCM, MAKE_CONTENT_CODE('a','s','c','m'), "daap.songcomment", "ascm", DMAP_TYPE_STRING},
    {DMAP_CC_ASCO, MAKE_CONTENT_CODE('a','s','c','o'), "daap.songcompliation", "asco", DMAP_TYPE_BYTE},
    {DMAP_CC_ASDA, MAKE_CONTENT_CODE('a','s','d','a'), "daap.songdateadded", "asda", DMAP_TYPE_DATE},
    {DMAP_CC_ASDM, MAKE_CONTENT_CODE('a','s','d','m'), "daap.songdatemodified", "asdm", DMAP_TYPE_DATE},
    {DMAP_CC_ASDC, MAKE_CONTENT_CODE('a','s','d','c'), "daap.songdisccount", "asdc", DMAP_TYPE_SHORT},
    {DMAP_CC_ASDN, MAKE_CONTENT_CODE('a','s','d','n'), "daap.songdiscnumber", "asdn", DMAP_TYPE_SHORT},
    {DMAP_CC_ASDB, MAKE_CONTENT_CODE('a','s','d','b'), "daap.songdisabled", "asdb", DMAP_TYPE_BYTE},
    {DMAP_CC_ASEQ, MAKE_CONTENT_CODE('a','s','e','q'), "daap.songeqpreset", "aseq", DMAP_TYPE_STRING},
    {DMAP_CC_ASFM, MAKE_CONTENT_CODE('a','s','f','m'), "daap.songformat", "asfm", DMAP_TYPE_STRING},
    {DMAP_CC_ASGN, MAKE_CONTENT_CODE('a','s','g','n'), "daap.songgenre", "asgn", DMAP_TYPE_STRING},
    {DMAP_CC_ASDT, MAKE_CONTENT_CODE('a','s','d','t'), "daap.songdescription", "asdt", DMAP_TYPE_STRING},
    {DMAP_CC_ASRV, MAKE_CONTENT_CODE('a','s','r','v'), "daap.songrelativevolume", "asrv", DMAP_TYPE_SIGNED_INT},
    {DMAP_CC_ASSR, MAKE_CONTENT_CODE('a','s','s','r'), "daap.songsamplerate", "assr", DMAP_TYPE_INT},
    {DMAP_CC_ASSZ, MAKE_CONTENT_CODE('a','s','s','z'), "daap.songsize", "assz", DMAP_TYPE_INT},
    {DMAP_CC_ASST, MAKE_CONTENT_CODE('a','s','s','t'), "daap.songstarttime", "asst", DMAP_TYPE_INT},
    {DMAP_CC_ASSP, MAKE_CONTENT_CODE('a','s','s','p'), "daap.songstoptime", "assp", DMAP_TYPE_INT},
    {DMAP_CC_ASTM, MAKE_CONTENT_CODE('a','s','t','m'), "daap.songtime", "astm", DMAP_TYPE_INT},
    {DMAP_CC_ASTC, MAKE_CONTENT_CODE('a','s','t','c'), "daap.songtrackcount", "astc", DMAP_TYPE_SHORT},
    {DMAP_CC_ASTN, MAKE_CONTENT_CODE('a','s','t','n'), "daap.songtracknumber", "astn", DMAP_TYPE_SHORT},
    {DMAP_CC_ASUR, MAKE_CONTENT_CODE('a','s','u','r'), "daap.songuserrating", "asur", DMAP_TYPE_BYTE},
    {DMAP_CC_ASYR, MAKE_CONTENT_CODE('a','s','y','r'), "daap.songyear", "asyr", DMAP_TYPE_SHORT},
    {DMAP_CC_ASDK, MAKE_CONTENT_CODE('a','s','d','k'), "daap.songdatakind", "asdk", DMAP_TYPE_BYTE},
    {DMAP_CC_ASUL, MAKE_CONTENT_CODE('a','s','u','l'), "daap.songdataurl", "asul", DMAP_TYPE_STRING},
114
115
    {DMAP_CC_ASSU, MAKE_CONTENT_CODE('a','s','s','u'), "daap.sortalbum", "assu", DMAP_TYPE_STRING},
    {DMAP_CC_ASSA, MAKE_CONTENT_CODE('a','s','s','a'), "daap.sortartist", "assa", DMAP_TYPE_STRING},
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
    {DMAP_CC_APLY, MAKE_CONTENT_CODE('a','p','l','y'), "daap.databaseplaylists", "aply", DMAP_TYPE_CONTAINER},
    {DMAP_CC_ABPL, MAKE_CONTENT_CODE('a','b','p','l'), "daap.baseplaylist", "abpl", DMAP_TYPE_BYTE},
    {DMAP_CC_APSO, MAKE_CONTENT_CODE('a','p','s','o'), "daap.playlistsongs", "apso", DMAP_TYPE_CONTAINER},
    {DMAP_CC_PRSV, MAKE_CONTENT_CODE('p','r','s','v'), "daap.resolve", "prsv", DMAP_TYPE_CONTAINER},
    {DMAP_CC_ARIF, MAKE_CONTENT_CODE('a','r','i','f'), "daap.resolveinfo", "arif", DMAP_TYPE_CONTAINER},
    {DMAP_CC_AESV, MAKE_CONTENT_CODE('a','e','S','V'), "com.applie.itunes.music-sharing-version", "aesv", DMAP_TYPE_INT},
    {DMAP_CC_MSAS, MAKE_CONTENT_CODE('m','s','a','s'), "daap.authentication.schemes", "msas", DMAP_TYPE_BYTE},
    {DMAP_CC_AGRP, MAKE_CONTENT_CODE('a','g','r','p'), "daap.songgrouping", "agrp", DMAP_TYPE_STRING},
    {DMAP_CC_ASCP, MAKE_CONTENT_CODE('a','s','c','p'), "daap.songcomposer", "ascp", DMAP_TYPE_STRING},
    {DMAP_CC_PPRO, MAKE_CONTENT_CODE('p','p','r','o'), "dpap.protocolversion", "ppro", DMAP_TYPE_VERSION},
    {DMAP_CC_PASP, MAKE_CONTENT_CODE('p','a','s','p'), "dpap.aspectratio", "pasp", DMAP_TYPE_STRING},
    {DMAP_CC_PFDT, MAKE_CONTENT_CODE('p','f','d','t'), "dpap.filedata", "pfdt", DMAP_TYPE_POINTER},
    {DMAP_CC_PICD, MAKE_CONTENT_CODE('p','i','c','d'), "dpap.creationdate", "picd", DMAP_TYPE_INT},
    {DMAP_CC_PIMF, MAKE_CONTENT_CODE('p','i','m','f'), "dpap.imagefilename", "pimf", DMAP_TYPE_STRING},
    {DMAP_CC_PFMT, MAKE_CONTENT_CODE('p','f','m','t'), "dpap.imageformat", "pfmt", DMAP_TYPE_STRING},
    {DMAP_CC_PIFS, MAKE_CONTENT_CODE('p','i','f','s'), "dpap.imagefilesize", "pifs", DMAP_TYPE_INT},
    {DMAP_CC_PLSZ, MAKE_CONTENT_CODE('p','l','s','z'), "dpap.imagelargefilesize", "plsz", DMAP_TYPE_INT},
    {DMAP_CC_PHGT, MAKE_CONTENT_CODE('p','h','g','t'), "dpap.imagepixelheight", "phgt", DMAP_TYPE_INT},
    {DMAP_CC_PWTH, MAKE_CONTENT_CODE('p','w','t','h'), "dpap.imagepixelwidth", "pwth", DMAP_TYPE_INT},
    {DMAP_CC_PRAT, MAKE_CONTENT_CODE('p','r','a','t'), "dpap.imagerating", "prat", DMAP_TYPE_INT},
    {DMAP_CC_PCMT, MAKE_CONTENT_CODE('p','c','m','t'), "dpap.imagecomments", "pcmt", DMAP_TYPE_STRING},
    {DMAP_CC_PRET, MAKE_CONTENT_CODE('p','r','e','t'), "dpap.pret", "pret", DMAP_TYPE_STRING},
    {DMAP_CC_AEHV, MAKE_CONTENT_CODE('a','e','H','V'), "com.apple.itunes.has-video", "aeHV", DMAP_TYPE_BYTE}
};

const gchar * 
dmap_content_code_name (DMAPContentCode code)
{
    return cc_defs[code-1].name;
}

DMAPType 
dmap_content_code_dmap_type (DMAPContentCode code)
{
    return cc_defs[code-1].type;
}

const gchar * 
dmap_content_code_string (DMAPContentCode code)
{
    return cc_defs[code-1].string;
}
            
static GType
dmap_content_code_gtype (DMAPContentCode code)
{
    switch (dmap_content_code_dmap_type (code)) {
        case DMAP_TYPE_BYTE:
        case DMAP_TYPE_SIGNED_INT:
            return G_TYPE_CHAR;
        case DMAP_TYPE_SHORT:
        case DMAP_TYPE_INT:
        case DMAP_TYPE_DATE:
            return G_TYPE_INT;
        case DMAP_TYPE_INT64:
            return G_TYPE_INT64;
        case DMAP_TYPE_VERSION:
            return G_TYPE_DOUBLE;
        case DMAP_TYPE_STRING:
            return G_TYPE_STRING;
        case DMAP_TYPE_POINTER:
            return G_TYPE_POINTER;
        case DMAP_TYPE_CONTAINER:
        default:
            return G_TYPE_NONE;
    }
}

static gboolean 
dmap_structure_node_serialize (GNode *node, 
                  GByteArray *array)
{
    DMAPStructureItem *item = node->data;
    DMAPType dmap_type;
    guint32 size = GINT32_TO_BE (item->size);

    if (item->content_code != DMAP_RAW) {
	    g_byte_array_append (array, (const guint8 *)dmap_content_code_string (item->content_code), 4);
	    g_byte_array_append (array, (const guint8 *)&size, 4);
    }
    
    dmap_type = dmap_content_code_dmap_type (item->content_code);

    switch (dmap_type) {
        case DMAP_TYPE_BYTE: 
        case DMAP_TYPE_SIGNED_INT: {
            gchar c = g_value_get_char (&(item->content));
            
            g_byte_array_append (array, (const guint8 *)&c, 1);
            
            break;
        }
        case DMAP_TYPE_SHORT: {
            gint32 i = g_value_get_int (&(item->content));
            gint16 s = GINT16_TO_BE ((gint16) i);
            
            g_byte_array_append (array, (const guint8 *)&s, 2);

            break;
            }
        case DMAP_TYPE_DATE: 
        case DMAP_TYPE_INT: {
            gint32 i = g_value_get_int (&(item->content));
            gint32 s = GINT32_TO_BE (i);

            g_byte_array_append (array, (const guint8 *)&s, 4);
            
            break;
        }
        case DMAP_TYPE_VERSION: {
            gdouble v = g_value_get_double (&(item->content));
            gint16 major;
            gint8 minor;
            gint8 patch = 0;

            major = (gint16)v;
            minor = (gint8)(v - ((gdouble)major));

            major = GINT16_TO_BE (major);

            g_byte_array_append (array, (const guint8 *)&major, 2);
            g_byte_array_append (array, (const guint8 *)&minor, 1);
            g_byte_array_append (array, (const guint8 *)&patch, 1);
            
            break;
        }        
        case DMAP_TYPE_INT64: {
            gint64 i = g_value_get_int64 (&(item->content));
            gint64 s = GINT64_TO_BE (i);

            g_byte_array_append (array, (const guint8 *)&s, 8);
            
            break;
        }
        case DMAP_TYPE_STRING: {
            const gchar *s = g_value_get_string (&(item->content));

            g_byte_array_append (array, (const guint8 *)s, strlen (s));
            
            break;
        }
        case DMAP_TYPE_POINTER: {
            const gpointer *data = g_value_get_pointer (&(item->content));

            g_byte_array_append (array, (const guint8 *)data, item->size);

            break;
        }
        case DMAP_TYPE_CONTAINER:
        default:
            break;
    }

    return FALSE;
}
    
gchar * 
dmap_structure_serialize (GNode *structure, 
                          guint *length)
{
    GByteArray *array;
    gchar *data;

    array = g_byte_array_new ();

    if (structure) {
        g_node_traverse (structure, G_PRE_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)dmap_structure_node_serialize, array);
    }
    
    data = (gchar *) array->data;
    *length = array->len;
    g_byte_array_free (array, FALSE);
    
    return data;
}

DMAPContentCode 
dmap_content_code_read_from_buffer (const gchar *buf)
{
     gint32 c = MAKE_CONTENT_CODE (buf[0], buf[1], buf[2], buf[3]);
     guint i;

    for (i = 0; i < G_N_ELEMENTS (cc_defs); i++) {
        if (cc_defs[i].int_code == c) {
            return cc_defs[i].code;
        }
    }

    return DMAP_CC_INVALID;
}

static gchar *
dmap_buffer_read_string (const gchar *buf, gssize size)
{
    if (g_utf8_validate (buf, size, NULL) == TRUE) {
        return g_strndup (buf, size);
    } else {
        return g_strdup ("");
    }
}

//#define PARSE_DEBUG
#define PARSE_DEBUG_FILE "dmapbuffer"

#ifdef PARSE_DEBUG
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif

static void 
dmap_structure_parse_container_buffer (GNode *parent, 
                      const guchar *buf, 
                      gint buf_length)
{
    gint l = 0;

    while (l < buf_length) {
        DMAPContentCode cc;
        gint codesize = 0;
        DMAPStructureItem *item = NULL;
        GNode *node = NULL;
        GType gtype;
        
#ifdef PARSE_DEBUG
        g_print ("l is %d and buf_length is %d\n", l, buf_length);
#endif        

        /* we need at least 8 bytes, 4 of content_code and 4 of size */
        if (buf_length - l < 8) {
#ifdef PARSE_DEBUG
            g_print ("Malformed response recieved\n");
#endif
            return;
        }
        
        cc = dmap_content_code_read_from_buffer ((const gchar*)&(buf[l]));
        if (cc == DMAP_CC_INVALID) {
#ifdef PARSE_DEBUG
            g_print ("Invalid/Unknown content_code recieved\n");
#endif
            return;
        }
        l += 4;

        codesize = DMAP_READ_UINT32_BE(&(buf[l]));
        /* CCCCSIZECONTENT
         * if the buffer length (minus 8 for the content code & size)
         * is smaller than the read codesize (ie, someone sent us
         * a codesize that is larger than the remaining data)
         * then get out before we start processing it
         */
        if (codesize > buf_length - l - 4 || codesize < 0) {
#ifdef PARSE_DEBUG
            g_print ("Invalid codesize %d recieved in buf_length %d\n", codesize, buf_length);
#endif
            return;
        }
        l += 4;

#ifdef PARSE_DEBUG
        g_print ("content_code = %d, codesize is %d, l is %d\n", cc, codesize, l);
#endif
        
        item = g_new0 (DMAPStructureItem, 1);
        item->content_code = cc;
        node = g_node_new (item);
        g_node_append (parent, node);
        
        gtype = dmap_content_code_gtype (item->content_code);

        if (gtype != G_TYPE_NONE) {
            g_value_init (&(item->content), gtype);
        }
        
#ifdef PARSE_DEBUG 
        {
            guint i;

            for (i = 2; i < g_node_depth (node); i++) {
                g_print ("\t");
            }
        }
#endif
        
// FIXME USE THE G_TYPE CONVERTOR FUNCTION dmap_type_to_gtype
        switch (dmap_content_code_dmap_type (item->content_code)) {
            case DMAP_TYPE_SIGNED_INT:
            case DMAP_TYPE_BYTE: {
                gchar c = 0;
                
                if (codesize == 1) {
                    c = (gchar) DMAP_READ_UINT8(&(buf[l]));
                }
                
                item->size = 1;
                g_value_set_char (&(item->content), c);
#ifdef PARSE_DEBUG
                g_print ("Code: %s, content (%d): \"%c\"\n", dmap_content_code_string (item->content_code), codesize, (gchar)c);
#endif

                break;
            }
            case DMAP_TYPE_SHORT: {
                gint16 s = 0;

                if (codesize == 2) {
                    s = DMAP_READ_UINT16_BE(&(buf[l]));
                }

                item->size = 2;
                g_value_set_int (&(item->content),(gint32)s);
#ifdef PARSE_DEBUG
                g_print ("Code: %s, content (%d): %hi\n", dmap_content_code_string (item->content_code), codesize, s);
#endif

                break;
            }
            case DMAP_TYPE_DATE:
            case DMAP_TYPE_INT: {
                gint32 i = 0;

                if (codesize == 4) {
                    i = DMAP_READ_UINT32_BE(&(buf[l]));
                }
                
                item->size = 4;
                g_value_set_int (&(item->content), i);
#ifdef PARSE_DEBUG
                g_print ("Code: %s, content (%d): %d\n", dmap_content_code_string (item->content_code), codesize, i);
#endif
                break;
            }
            case DMAP_TYPE_INT64: {
                gint64 i = 0;
        
                if (codesize == 8) {
                    i = DMAP_READ_UINT16_BE(&(buf[l]));
                }
                
                item->size = 8;
                g_value_set_int64 (&(item->content), i);
#ifdef PARSE_DEBUG
                g_print ("Code: %s, content (%d): %"G_GINT64_FORMAT"\n", dmap_content_code_string (item->content_code), codesize, i);
#endif

                break;
            }
            case DMAP_TYPE_STRING: {
                gchar *s = dmap_buffer_read_string ((const gchar*)&(buf[l]), codesize);

                item->size = strlen (s);
                g_value_set_string (&(item->content), s);
#ifdef PARSE_DEBUG
                g_print ("Code: %s, content (%d): \"%s\"\n", dmap_content_code_string (item->content_code), codesize, s);
#endif

                break;
            }
            case DMAP_TYPE_POINTER: {
                gpointer *data = g_memdup ((const gchar*)&(buf[l]), codesize);
            
                item->size = codesize;
                g_value_set_pointer (&(item->content), data);
                
                break;
            }
            case DMAP_TYPE_VERSION: {
                gint16 major = 0;
                gint16 minor = 0;
                gint16 patch = 0;
                gdouble v = 0;

                if (codesize == 4) {
                    major = DMAP_READ_UINT16_BE(&(buf[l]));
                    minor = DMAP_READ_UINT8(&(buf[l]) + 2);
                    patch = DMAP_READ_UINT8(&(buf[l]) + 3);
                }

                v = (gdouble)major;
                v += (gdouble)(minor * 0.1);
                v += (gdouble)(patch * 0.01);
                
                item->size = 4;
                g_value_set_double (&(item->content), v);
#ifdef PARSE_DEBUG
                g_print ("Code: %s, content: %f\n", dmap_content_code_string (item->content_code), v);
#endif

                break;
            }
            case DMAP_TYPE_CONTAINER: {
#ifdef PARSE_DEBUG
                g_print ("Code: %s, container\n", dmap_content_code_string (item->content_code));
#endif
                dmap_structure_parse_container_buffer (node,&(buf[l]), codesize);
                break;
            }
        }

        l += codesize;
    }

    return;
}

GNode * 
dmap_structure_parse (const gchar *buf, 
                      gint buf_length)
{
    GNode *root = NULL;
    GNode *child = NULL;

#ifdef PARSE_DEBUG
    {
        gint fd;

        fd = open (PARSE_DEBUG_FILE, O_WRONLY | O_CREAT);
        write (fd, (const void *)buf, (size_t)buf_length);
        close (fd);
    }
#endif
    
    root = g_node_new (NULL);

    dmap_structure_parse_container_buffer (root, (guchar *)buf, buf_length);

    child = root->children;
    if (child) {
        g_node_unlink (child);
    }
    g_node_destroy (root);
    
    return child;
}

struct NodeFinder {
    DMAPContentCode code;
    GNode *node;
};

static gboolean 
gnode_find_node (GNode *node, 
         gpointer data)
{
    struct NodeFinder *finder = (struct NodeFinder *)data;
    DMAPStructureItem *item = node->data;

    if (item->content_code == finder->code) {
        finder->node = node;
        return TRUE;
    }

    return FALSE;
}

DMAPStructureItem * 
dmap_structure_find_item (GNode *structure, 
                          DMAPContentCode code)
{
    GNode *node = NULL;
    
    node = dmap_structure_find_node (structure, code);

    if (node) {
        return node->data;
    }

    return NULL;
}

GNode *
dmap_structure_add (GNode *parent,
		       DMAPContentCode cc,
		       ...)
{
	DMAPType dmap_type;
	GType gtype;
	DMAPStructureItem *item;
	va_list list;
	GNode *node;
	gchar *error = NULL;

	va_start (list, cc);

	dmap_type = dmap_content_code_dmap_type (cc);
602
	g_print ("%d %d %d %d\n\n\n", dmap_type, cc, DMAP_TYPE_SHORT, DMAP_TYPE_STRING);
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
	gtype = dmap_content_code_gtype (cc);

	item = g_new0(DMAPStructureItem, 1);
	item->content_code = cc;

	if (gtype != G_TYPE_NONE) {
		g_value_init (&(item->content), gtype);
	}

	if (dmap_type != DMAP_TYPE_STRING && dmap_type != DMAP_TYPE_CONTAINER && dmap_type != DMAP_TYPE_POINTER) {
		G_VALUE_COLLECT (&(item->content), list, G_VALUE_NOCOPY_CONTENTS, &error);
		if (error) {
			g_warning ("%s", error);
			g_free (error);
		}
	}

	switch (dmap_type) {
		case DMAP_TYPE_BYTE:
		case DMAP_TYPE_SIGNED_INT:
			item->size = 1;
			break;
		case DMAP_TYPE_SHORT:
			item->size = 2;
			break;
		case DMAP_TYPE_DATE:
		case DMAP_TYPE_INT:
		case DMAP_TYPE_VERSION:
			item->size = 4;
			break;
		case DMAP_TYPE_INT64:
			item->size = 8;
			break;
		case DMAP_TYPE_STRING: {
			gchar *s = va_arg (list, gchar *);

			g_value_set_string (&(item->content), s);

			/* we dont use G_VALUE_COLLECT for this because we also
			 * need the length */
			item->size = strlen (s);
			break;
		}
		case DMAP_TYPE_POINTER: {
			gpointer p = va_arg (list, gpointer);
			gint s = va_arg (list, gint);

			g_value_set_pointer (&(item->content), p);

			/* we dont use G_VALUE_COLLECT for this because we also
			 * need the size */
			item->size = s;
			break;

		}
		case DMAP_TYPE_CONTAINER:
		default:
			break;
	}

	node = g_node_new (item);

	if (parent) {
		g_node_append (parent, node);

		while (parent) {
			DMAPStructureItem *parent_item = parent->data;

			if (cc == DMAP_RAW)
				parent_item->size += item->size;
			else
				parent_item->size += (4 + 4 + item->size);

			parent = parent->parent;
		}
	}

	return node;
}

GNode * 
dmap_structure_find_node (GNode *structure, 
                 DMAPContentCode code)
{
    struct NodeFinder *finder;
    GNode *node = NULL;

    finder = g_new0(struct NodeFinder,1);
    finder->code = code;

    g_node_traverse (structure, G_IN_ORDER, G_TRAVERSE_ALL, -1, gnode_find_node, finder);

    node = finder->node;
    g_free (finder);
    finder = NULL;

    return node;
}



static void
dmap_item_free (DMAPStructureItem *item)
{
    if (dmap_content_code_dmap_type (item->content_code) != DMAP_TYPE_CONTAINER) {
        g_value_unset (&(item->content));
    }

    g_free (item);
}

static gboolean
gnode_free_dmap_item (GNode *node,
             gpointer data)
{
    dmap_item_free ((DMAPStructureItem *)node->data);

    return FALSE;
}

void 
dmap_structure_destroy (GNode *structure)
{
    if (structure) {
        g_node_traverse (structure, G_IN_ORDER, G_TRAVERSE_ALL, -1, gnode_free_dmap_item, NULL);

        g_node_destroy (structure);

        structure = NULL;
    }
}

const DMAPContentCodeDefinition *
dmap_content_codes (guint *number)
{
    *number = G_N_ELEMENTS (cc_defs);

    return cc_defs;
}

gint32 
dmap_content_code_string_as_int32 (const gchar *str)
{
    union {
        gint32 i;
        gchar str[4];
    } u;

    strncpy (u.str, str, 4);

    return g_htonl (u.i);
}

static gboolean 
print_dmap_item (GNode *node, 
                 gpointer data)
{
    DMAPStructureItem *item;
    const gchar *name;
    gchar *value;
    gint i;

    for (i = 1; i < g_node_depth (node); i++) {
        g_print ("\t");
    }

    item = node->data;

    name = dmap_content_code_name (item->content_code);

    if (G_IS_VALUE (&(item->content))) {
        value = g_strdup_value_contents (&(item->content));
    } else {
        value = g_strdup ("");
    }

    g_print ("%d, %s = %s (%d)\n", g_node_depth (node), name, value, item->size);
    g_free (value);

    return FALSE;
}

void
dmap_structure_print (GNode *structure)
{
    if (structure) {
        g_node_traverse (structure, G_PRE_ORDER, G_TRAVERSE_ALL, -1, (GNodeTraverseFunc)print_dmap_item, NULL);
    }
}