1/*
2 * Copyright (c) 2008 Rob Braun
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Rob Braun nor the names of his contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 */
29#include "config.h"
30#include <stdlib.h>
31#include <stdio.h>
32#include <string.h>
33#include <assert.h>
34#include <libgen.h>
35
36#ifndef HAVE_ASPRINTF
37#include "asprintf.h"
38#endif
39#include "xar.h"
40#include "filetree.h"
41#include "archive.h"
42#include "b64.h"
43#include "ea.h"
44
45struct __xar_ea_t {
46	const struct __xar_prop_t *prop;
47	const struct __xar_ea_t *next;
48};
49
50#define XAR_EA(x) ((struct __xar_ea_t *)(x))
51
52xar_ea_t xar_ea_new(xar_file_t f, const char *name)
53{
54	xar_ea_t ret;
55
56	ret = calloc(sizeof(struct __xar_ea_t), 1);
57	 if( !ret )
58		return NULL;
59
60	XAR_EA(ret)->prop = xar_prop_new(f, NULL);
61	if( !XAR_EA(ret)->prop ) {
62		free(ret);
63		return NULL;
64	}
65
66	xar_prop_setkey(XAR_EA(ret)->prop, "ea");
67	xar_prop_setvalue(XAR_EA(ret)->prop, NULL);
68	XAR_PROP(XAR_EA(ret)->prop)->attrs = xar_attr_new();
69	XAR_ATTR(XAR_PROP(XAR_EA(ret)->prop)->attrs)->key = strdup("id");
70	asprintf((char **)&XAR_ATTR(XAR_PROP(XAR_EA(ret)->prop)->attrs)->value, "%lld", XAR_FILE(f)->nexteaid++);
71
72	xar_prop_pset(f, XAR_EA(ret)->prop, "name", name);
73
74	return ret;
75}
76
77int32_t xar_ea_pset(xar_file_t f, xar_ea_t e, const char *key, const char *value){
78	if( xar_prop_pset(f, XAR_EA(e)->prop, key, value) )
79		return 0;
80	return -1;
81}
82
83int32_t xar_ea_pget(xar_ea_t e, const char *key, const char **value) {
84	xar_prop_t r = xar_prop_find(XAR_EA(e)->prop, key);
85	if( !r ) {
86		if( value )
87			*value = NULL;
88		return -1;
89	}
90
91	if(value)
92		*value = XAR_PROP(r)->value;
93
94	return 0;
95}
96
97xar_prop_t xar_ea_root(xar_ea_t e) {
98	return XAR_EA(e)->prop;
99}
100
101xar_prop_t xar_ea_find(xar_file_t f, const char *name)
102{
103	xar_prop_t p;
104
105	for(p = xar_prop_pfirst(f); p; p = xar_prop_pnext(p)) {
106		const char *tmp;
107		xar_prop_t tmpp;
108
109		tmp = xar_prop_getkey(p);
110		if( strncmp(tmp, XAR_EA_FORK, strlen(XAR_EA_FORK)) != 0 )
111			continue;
112		if( strlen(tmp) != strlen(XAR_EA_FORK) )
113			continue;
114
115		tmpp = xar_prop_pget(p, "name");
116		if( !tmpp )
117			continue;
118		tmp = xar_prop_getvalue(tmpp);
119		if( !tmp )
120			continue;
121
122		if( strcmp(tmp, name) == 0 )
123			return p;
124	}
125
126	return NULL;
127}
128