summaryrefslogtreecommitdiff
path: root/lib/header_internal.c
blob: 382d8988a5f7ad98a96336cdb71f872a5e3a4e8e (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
/** \ingroup header
 * \file lib/header_internal.c
 */

#include "system.h"

#include <rpm/rpmtypes.h>
#include "lib/header_internal.h"

#include "debug.h"

uint64_t htonll( uint64_t n ) {
    uint32_t *i = (uint32_t*)&n;
    uint32_t b = i[0];
    i[0] = htonl(i[1]);
    i[1] = htonl(b);
    return n;
}

/*
 * Backwards compatibility wrappers for legacy interfaces.
 * Remove these some day...
 */
#define _RPM_4_4_COMPAT
#include <rpm/rpmlegacy.h>

/* dumb macro to avoid 50 copies of this code while converting... */
#define TDWRAP() \
    if (type) \
	*type = td.type; \
    if (p) \
	*p = td.data; \
    else \
	rpmtdFreeData(&td); \
    if (c) \
	*c = td.count

int headerRemoveEntry(Header h, rpmTag tag)
{
    return headerDel(h, tag);
}

static void *_headerFreeData(rpm_data_t data, rpmTagType type)
{
    if (data) {
	if (type == RPM_FORCEFREE_TYPE ||
	    type == RPM_STRING_ARRAY_TYPE ||
	    type == RPM_I18NSTRING_TYPE ||
	    type == RPM_BIN_TYPE)
		free(data);
    }
    return NULL;
}

void * headerFreeData(rpm_data_t data, rpmTagType type)
{
    return _headerFreeData(data, type);
}

void * headerFreeTag(Header h, rpm_data_t data, rpmTagType type)
{
    return _headerFreeData(data, type);
}

static int headerGetWrap(Header h, rpmTag tag,
		rpmTagType * type,
		rpm_data_t * p,
		rpm_count_t * c,
		headerGetFlags flags)
{
    struct rpmtd_s td;
    int rc;

    rc = headerGet(h, tag, &td, flags);
    TDWRAP();
    return rc;
}

int headerGetEntry(Header h, rpmTag tag,
			rpmTagType * type,
			rpm_data_t * p,
			rpm_count_t * c)
{
    return headerGetWrap(h, tag, type, p, c, HEADERGET_DEFAULT);
}

int headerGetEntryMinMemory(Header h, rpmTag tag,
			rpmTagType * type,
			rpm_data_t * p,
			rpm_count_t * c)
{
    return headerGetWrap(h, tag, type, (rpm_data_t) p, c, HEADERGET_MINMEM);
}

int headerGetRawEntry(Header h, rpmTag tag, rpmTagType * type, rpm_data_t * p,
		rpm_count_t * c)
{
    if (p == NULL) 
	return headerIsEntry(h, tag);

    return headerGetWrap(h, tag, type, p, c, HEADERGET_RAW);
}

int headerNextIterator(HeaderIterator hi,
		rpmTag * tag,
		rpmTagType * type,
		rpm_data_t * p,
		rpm_count_t * c)
{
    struct rpmtd_s td;
    int rc;

    rc = headerNext(hi, &td);
    if (tag)
	*tag = td.tag;
    TDWRAP();
    return rc;
}

int headerModifyEntry(Header h, rpmTag tag, rpmTagType type,
			rpm_constdata_t p, rpm_count_t c)
{
    struct rpmtd_s td = {
	.tag = tag,
	.type = type,
	.data = (void *) p,
	.count = c,
    };
    return headerMod(h, &td);
}

static int headerPutWrap(Header h, rpmTag tag, rpmTagType type,
		rpm_constdata_t p, rpm_count_t c, headerPutFlags flags)
{
    struct rpmtd_s td = {
	.tag = tag,
	.type = type,
	.data = (void *) p,
	.count = c,
    };
    return headerPut(h, &td, flags);
}

int headerAddOrAppendEntry(Header h, rpmTag tag, rpmTagType type,
		rpm_constdata_t p, rpm_count_t c)
{
    return headerPutWrap(h, tag, type, p, c, HEADERPUT_APPEND);
}

int headerAppendEntry(Header h, rpmTag tag, rpmTagType type,
		rpm_constdata_t p, rpm_count_t c)
{
    return headerPutWrap(h, tag, type, p, c, HEADERPUT_APPEND);
}

int headerAddEntry(Header h, rpmTag tag, rpmTagType type,
		rpm_constdata_t p, rpm_count_t c)
{
    return headerPutWrap(h, tag, type, p, c, HEADERPUT_DEFAULT);
}
#undef _RPM_4_4_COMPAT