summaryrefslogtreecommitdiffstats
path: root/src/buffer.c
blob: d64f94475ab83d5afa8d93fe2c3a834a14908bdc (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
/*
 * (C) 2012-2014 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdarg.h>
#include <inttypes.h>
#include <string.h>
#include <buffer.h>
#include <libnftnl/common.h>
#include "internal.h"

int nft_buf_update(struct nft_buf *b, int ret)
{
	if (ret < 0) {
		b->fail = true;
	} else {
		b->off += ret;
		if (ret > b->len)
			ret = b->len;
		b->size += ret;
		b->len -= ret;
	}

	return ret;
}

int nft_buf_done(struct nft_buf *b)
{
	if (b->fail)
		return -1;

	/* Remove trailing comma in json */
	if (b->size > 0 && b->buf[b->size - 1] == ',') {
		b->off--;
		b->size--;
		b->len++;
	}

	return b->off;
}

static int nft_buf_put(struct nft_buf *b, const char *fmt, ...)
{
	int ret;
	va_list ap;

	va_start(ap, fmt);
	ret = vsnprintf(b->buf + b->off, b->len, fmt, ap);
	ret = nft_buf_update(b, ret);
	va_end(ap);

	return ret;
}

int nft_buf_open(struct nft_buf *b, int type, const char *tag)
{
	switch (type) {
	case NFT_OUTPUT_XML:
		return nft_buf_put(b, "<%s>", tag);
	case NFT_OUTPUT_JSON:
		return nft_buf_put(b, "{\"%s\":{", tag);
	default:
		return 0;
	}
}

int nft_buf_close(struct nft_buf *b, int type, const char *tag)
{
	switch (type) {
	case NFT_OUTPUT_XML:
		return nft_buf_put(b, "</%s>");
	case NFT_OUTPUT_JSON:
		/* Remove trailing comma in json */
		if (b->size > 0 && b->buf[b->size - 1] == ',') {
			b->off--;
			b->size--;
			b->len++;
		}

		return nft_buf_put(b, "}}");
	default:
		return 0;
	}
}

int nft_buf_u32(struct nft_buf *b, int type, uint32_t value, const char *tag)
{
	switch (type) {
	case NFT_OUTPUT_XML:
		return nft_buf_put(b, "<%s>%u</%s>", tag, value, tag);
	case NFT_OUTPUT_JSON:
		return nft_buf_put(b, "\"%s\":%u,", tag, value);
	default:
		return 0;
	}
}

int nft_buf_s32(struct nft_buf *b, int type, uint32_t value, const char *tag)
{
	switch (type) {
	case NFT_OUTPUT_XML:
		return nft_buf_put(b, "<%s>%d</%s>", tag, value, tag);
	case NFT_OUTPUT_JSON:
		return nft_buf_put(b, "\"%s\":%d,", tag, value);
	default:
		return 0;
	}
}

int nft_buf_u64(struct nft_buf *b, int type, uint64_t value, const char *tag)
{
	switch (type) {
	case NFT_OUTPUT_XML:
		return nft_buf_put(b, "<%s>%"PRIu64"</%s>", tag, value, tag);
	case NFT_OUTPUT_JSON:
		return nft_buf_put(b, "\"%s\":%"PRIu64",", tag, value);
	default:
		return 0;
	}
}

int nft_buf_str(struct nft_buf *b, int type, const char *str, const char *tag)
{
	switch (type) {
	case NFT_OUTPUT_XML:
		return nft_buf_put(b, "<%s>%s</%s>", tag, str, tag);
	case NFT_OUTPUT_JSON:
		return nft_buf_put(b, "\"%s\":\"%s\",", tag, str);
	default:
		return 0;
	}
}

int nft_buf_reg(struct nft_buf *b, int type, union nft_data_reg *reg,
		int reg_type, const char *tag)
{
	int ret;

	switch (type) {
	case NFT_OUTPUT_XML:
		ret = nft_buf_put(b, "<%s>", tag);
		ret = nft_data_reg_snprintf(b->buf + b->off, b->len, reg,
                                    NFT_OUTPUT_XML, 0, reg_type);
		nft_buf_update(b, ret);
		return nft_buf_put(b, "</%s>", tag);
	case NFT_OUTPUT_JSON:
		nft_buf_put(b, "\"%s\":{", tag);
		ret = nft_data_reg_snprintf(b->buf + b->off, b->len, reg,
					    NFT_OUTPUT_JSON, 0, reg_type);
		nft_buf_update(b, ret);
		return nft_buf_put(b, "},");
	}
	return 0;
}