aboutsummaryrefslogtreecommitdiffstats
path: root/libxo/xo_encoder.c
blob: eb2960bf7d9fb40c2291c3c6b9a5e92a3b719018 (plain) (blame)
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
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
/*
 * Copyright (c) 2015, Juniper Networks, Inc.
 * All rights reserved.
 * This SOFTWARE is licensed under the LICENSE provided in the
 * ../Copyright file. By downloading, installing, copying, or otherwise
 * using the SOFTWARE, you agree to be bound by the terms of that
 * LICENSE.
 * Phil Shafer, August 2015
 */

/**
 * libxo includes a number of fixed encoding styles.  But other
 * external encoders are need to deal with new encoders.  Rather
 * than expose a swarm of libxo internals, we create a distinct
 * API, with a simpler API than we use internally.
 */

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/queue.h>
#include <sys/param.h>
#include <dlfcn.h>

#include "xo_config.h"
#include "xo.h"
#include "xo_encoder.h"

#ifdef HAVE_DLFCN_H
#include <dlfcn.h>
#if !defined(HAVE_DLFUNC)
#define dlfunc(_p, _n)		dlsym(_p, _n)
#endif
#else /* HAVE_DLFCN_H */
#define dlopen(_n, _f)		NULL /* Fail */
#define dlsym(_p, _n)		NULL /* Fail */
#define dlfunc(_p, _n)		NULL /* Fail */
#endif /* HAVE_DLFCN_H */

static void xo_encoder_setup (void); /* Forward decl */

/*
 * Need a simple string collection
 */
typedef struct xo_string_node_s {
    TAILQ_ENTRY(xo_string_node_s) xs_link; /* Next string */
    char xs_data[0];		      /* String data */
} xo_string_node_t;

typedef TAILQ_HEAD(xo_string_list_s, xo_string_node_s) xo_string_list_t;

static inline void
xo_string_list_init (xo_string_list_t *listp)
{
    if (listp->tqh_last == NULL)
	TAILQ_INIT(listp);
}

static inline xo_string_node_t *
xo_string_add (xo_string_list_t *listp, const char *str)
{
    if (listp == NULL || str == NULL)
	return NULL;

    xo_string_list_init(listp);
    size_t len = strlen(str);
    xo_string_node_t *xsp;

    xsp = xo_realloc(NULL, sizeof(*xsp) + len + 1);
    if (xsp) {
	memcpy(xsp->xs_data, str, len);
	xsp->xs_data[len] = '\0';
	TAILQ_INSERT_TAIL(listp, xsp, xs_link);
    }

    return xsp;
}

#define XO_STRING_LIST_FOREACH(_xsp, _listp) \
    xo_string_list_init(_listp); \
    TAILQ_FOREACH(_xsp, _listp, xs_link)

static inline void
xo_string_list_clean (xo_string_list_t *listp)
{
    xo_string_node_t *xsp;

    xo_string_list_init(listp);

    for (;;) {
	xsp = TAILQ_FIRST(listp);
        if (xsp == NULL)
            break;
        TAILQ_REMOVE(listp, xsp, xs_link);
	xo_free(xsp);
    }
}

static xo_string_list_t xo_encoder_path;

void
xo_encoder_path_add (const char *path)
{
    xo_encoder_setup();

    if (path)
	xo_string_add(&xo_encoder_path, path);
}

/* ---------------------------------------------------------------------- */

typedef struct xo_encoder_node_s {
    TAILQ_ENTRY(xo_encoder_node_s) xe_link; /* Next session */
    char *xe_name;			/* Name for this encoder */
    xo_encoder_func_t xe_handler;	/* Callback function */
    void *xe_dlhandle;			/* dlopen handle */
} xo_encoder_node_t;

typedef TAILQ_HEAD(xo_encoder_list_s, xo_encoder_node_s) xo_encoder_list_t;

#define XO_ENCODER_LIST_FOREACH(_xep, _listp) \
    xo_encoder_list_init(_listp); \
    TAILQ_FOREACH(_xep, _listp, xe_link)

static xo_encoder_list_t xo_encoders;

static void
xo_encoder_list_init (xo_encoder_list_t *listp)
{
    if (listp->tqh_last == NULL)
	TAILQ_INIT(listp);
}

static xo_encoder_node_t *
xo_encoder_list_add (const char *name)
{
    if (name == NULL)
	return NULL;

    xo_encoder_node_t *xep = xo_realloc(NULL, sizeof(*xep));
    if (xep) {
	int len = strlen(name) + 1;
	xep->xe_name = xo_realloc(NULL, len);
	if (xep->xe_name == NULL) {
	    xo_free(xep);
	    return NULL;
	}

	memcpy(xep->xe_name, name, len);

	TAILQ_INSERT_TAIL(&xo_encoders, xep, xe_link);
    }

    return xep;
}

void
xo_encoders_clean (void)
{
    xo_encoder_node_t *xep;

    xo_encoder_setup();

    for (;;) {
	xep = TAILQ_FIRST(&xo_encoders);
        if (xep == NULL)
            break;

        TAILQ_REMOVE(&xo_encoders, xep, xe_link);

	if (xep->xe_dlhandle)
	    dlclose(xep->xe_dlhandle);

	xo_free(xep);
    }

    xo_string_list_clean(&xo_encoder_path);
}

static void
xo_encoder_setup (void)
{
    static int initted;
    if (!initted) {
	initted = 1;

	xo_string_list_init(&xo_encoder_path);
	xo_encoder_list_init(&xo_encoders);

	xo_encoder_path_add(XO_ENCODERDIR);
    }
}

static xo_encoder_node_t *
xo_encoder_find (const char *name)
{
    xo_encoder_node_t *xep;

    xo_encoder_list_init(&xo_encoders);

    XO_ENCODER_LIST_FOREACH(xep, &xo_encoders) {
	if (strcmp(xep->xe_name, name) == 0)
	    return xep;
    }

    return NULL;
}

static xo_encoder_node_t *
xo_encoder_discover (const char *name)
{
    void *dlp = NULL;
    char buf[MAXPATHLEN];
    xo_string_node_t *xsp;
    xo_encoder_node_t *xep = NULL;

    XO_STRING_LIST_FOREACH(xsp, &xo_encoder_path) {
	static const char fmt[] = "%s/%s.enc";
	char *dir = xsp->xs_data;
	size_t len = snprintf(buf, sizeof(buf), fmt, dir, name);

	if (len > sizeof(buf))	/* Should not occur */
	    continue;

	dlp = dlopen((const char *) buf, RTLD_NOW);
	if (dlp)
	    break;
    }

    if (dlp) {
	/*
	 * If the library exists, find the initializer function and
	 * call it.
	 */
	xo_encoder_init_func_t func;

	func = (xo_encoder_init_func_t) dlfunc(dlp, XO_ENCODER_INIT_NAME);
	if (func) {
	    xo_encoder_init_args_t xei;

	    bzero(&xei, sizeof(xei));

	    xei.xei_version = XO_ENCODER_VERSION;
	    int rc = func(&xei);
	    if (rc == 0 && xei.xei_handler) {
		xep = xo_encoder_list_add(name);
		if (xep) {
		    xep->xe_handler = xei.xei_handler;
		    xep->xe_dlhandle = dlp;
		}
	    }
	}

	if (xep == NULL)
	    dlclose(dlp);
    }

    return xep;
}

void
xo_encoder_register (const char *name, xo_encoder_func_t func)
{
    xo_encoder_setup();

    xo_encoder_node_t *xep = xo_encoder_find(name);

    if (xep)			/* "We alla-ready got one" */
	return;

    xep = xo_encoder_list_add(name);
    if (xep)
	xep->xe_handler = func;
}

void
xo_encoder_unregister (const char *name)
{
    xo_encoder_setup();

    xo_encoder_node_t *xep = xo_encoder_find(name);
    if (xep) {
	TAILQ_REMOVE(&xo_encoders, xep, xe_link);
	xo_free(xep);
    }
}

int
xo_encoder_init (xo_handle_t *xop, const char *name)
{
    xo_encoder_setup();

    /* Can't have names containing '/' or ':' */
    if (strchr(name, '/') != NULL || strchr(name, ':') != NULL)
	return -1;

   /*
     * First we look on the list of known (registered) encoders.
     * If we don't find it, we follow the set of paths to find
     * the encoding library.
     */
    xo_encoder_node_t *xep = xo_encoder_find(name);
    if (xep == NULL) {
	xep = xo_encoder_discover(name);
	if (xep == NULL)
	    return -1;
    }

    xo_set_encoder(xop, xep->xe_handler);

    return xo_encoder_handle(xop, XO_OP_CREATE, NULL, NULL);
}

/*
 * A couple of function varieties here, to allow for multiple
 * use cases.  This variant is for when the main program knows
 * its own encoder needs.
 */
xo_handle_t *
xo_encoder_create (const char *name, xo_xof_flags_t flags)
{
    xo_handle_t *xop;

    xop = xo_create(XO_STYLE_ENCODER, flags);
    if (xop) {
	if (xo_encoder_init(xop, name)) {
	    xo_destroy(xop);
	    xop = NULL;
	}
    }

    return xop;
}

int
xo_encoder_handle (xo_handle_t *xop, xo_encoder_op_t op,
		  const char *name, const char *value)
{
    void *private = xo_get_private(xop);
    xo_encoder_func_t func = xo_get_encoder(xop);

    if (func == NULL)
	return -1;

    return func(xop, op, name, value, private);
}

const char *
xo_encoder_op_name (xo_encoder_op_t op)
{
    static const char *names[] = {
	/*  0 */ "unknown",
	/*  1 */ "create",
	/*  2 */ "open_container",
	/*  3 */ "close_container",
	/*  4 */ "open_list",
	/*  5 */ "close_list",
	/*  6 */ "open_leaf_list",
	/*  7 */ "close_leaf_list",
	/*  8 */ "open_instance",
	/*  9 */ "close_instance",
	/* 10 */ "string",
	/* 11 */ "content",
	/* 12 */ "finish",
	/* 13 */ "flush",
	/* 14 */ "destroy",
	/* 15 */ "attr",
	/* 16 */ "version",
    };

    if (op > sizeof(names) / sizeof(names[0]))
	return "unknown";

    return names[op];
}