2 * Copyright (c) 2015, Juniper Networks, Inc.
4 * This SOFTWARE is licensed under the LICENSE provided in the
5 * ../Copyright file. By downloading, installing, copying, or otherwise
6 * using the SOFTWARE, you agree to be bound by the terms of that
8 * Phil Shafer, August 2015
12 * CBOR (RFC 7049) mades a suitable test case for libxo's external
13 * encoder API. It's simple, streaming, well documented, and an
16 * This encoder uses the "pretty" flag for diagnostics, which isn't
17 * really kosher, but it's example code.
21 #include <sys/types.h>
29 #include "xo_encoder.h"
33 * memdump(): dump memory contents in hex/ascii
35 0123456789012345678901234567890123456789012345678901234567890123456789012345
36 XX XX XX XX XX XX XX XX - XX XX XX XX XX XX XX XX abcdefghijklmnop
39 cbor_memdump (FILE *fp, const char *title, const char *data,
40 size_t len, const char *tag, int indent)
42 enum { MAX_PER_LINE = 16 };
48 static const int ends[ MAX_PER_LINE ] = { 2, 5, 8, 11, 15, 18, 21, 24,
49 29, 32, 35, 38, 42, 45, 48, 51 };
57 fprintf(fp, "%*s[%s] @ %p (%lx/%lu)\n", indent + 1, tag,
58 title, data, (unsigned long) len, (unsigned long) len);
64 for (i = 0; i < MAX_PER_LINE && i < len; i++) {
65 if (i && (i % 4) == 0) *bp++ = ' ';
70 sprintf(bp, "%02x ", (unsigned char) *data);
72 *tp++ = (isprint((int) *data) && *data >= ' ') ? *data : '.';
78 fprintf(fp, "%*s%-54s%s\n", indent + 1, tag, buf, text);
84 * CBOR breaks the first byte into two pieces, the major type in the
85 * top 3 bits and the minor value in the low 5 bits. The value can be
86 * a small value (0 .. 23), an 8-bit value (24), a 16-bit value (25),
87 * a 32-bit value (26), or a 64-bit value (27). A value of 31
88 * represents an unknown length, which we'll use extensively for
89 * streaming our content.
91 #define CBOR_MAJOR_MASK 0xE0
92 #define CBOR_MINOR_MASK 0x1F
93 #define CBOR_MAJOR_SHIFT 5
95 #define CBOR_MAJOR(_x) ((_x) & CBOR_MAJOR_MASK)
96 #define CBOR_MAJOR_VAL(_x) ((_x) << CBOR_MAJOR_SHIFT)
97 #define CBOR_MINOR_VAL(_x) ((_x) & CBOR_MINOR_MASK)
99 /* Major type codes */
100 #define CBOR_UNSIGNED CBOR_MAJOR_VAL(0) /* 0x00 */
101 #define CBOR_NEGATIVE CBOR_MAJOR_VAL(1) /* 0x20 */
102 #define CBOR_BYTES CBOR_MAJOR_VAL(2) /* 0x40 */
103 #define CBOR_STRING CBOR_MAJOR_VAL(3) /* 0x60 */
104 #define CBOR_ARRAY CBOR_MAJOR_VAL(4) /* 0x80 */
105 #define CBOR_MAP CBOR_MAJOR_VAL(5) /* 0xa0 */
106 #define CBOR_SEMANTIC CBOR_MAJOR_VAL(6) /* 0xc0 */
107 #define CBOR_SPECIAL CBOR_MAJOR_VAL(7) /* 0xe0 */
109 #define CBOR_ULIMIT 24 /* Largest unsigned value */
110 #define CBOR_NLIMIT 23 /* Largest negative value */
112 #define CBOR_BREAK 0xFF
113 #define CBOR_INDEF 0x1F
115 #define CBOR_FALSE 0xF4
116 #define CBOR_TRUE 0xF5
117 #define CBOR_NULL 0xF6
118 #define CBOR_UNDEF 0xF7
120 #define CBOR_LEN8 0x18 /* 24 - 8-bit value */
121 #define CBOR_LEN16 0x19 /* 25 - 16-bit value */
122 #define CBOR_LEN32 0x1a /* 26 - 32-bit value */
123 #define CBOR_LEN64 0x1b /* 27 - 64-bit value */
124 #define CBOR_LEN128 0x1c /* 28 - 128-bit value */
126 typedef struct cbor_private_s {
127 xo_buffer_t c_data; /* Our data buffer */
128 unsigned c_indent; /* Indent level */
129 unsigned c_open_leaf_list; /* Open leaf list construct? */
133 cbor_encode_uint (xo_buffer_t *xbp, uint64_t minor, unsigned limit)
135 char *bp = xbp->xb_curp;
138 if (minor > (1ULL << 32)) {
142 } else if (minor > (1<<16)) {
146 } else if (minor > (1<<8)) {
150 } else if (minor > limit) {
154 *bp++ |= minor & CBOR_MINOR_MASK;
159 for (i = m - 8; i >= 0; i -= 8)
167 cbor_append (xo_handle_t *xop, cbor_private_t *cbor, xo_buffer_t *xbp,
168 unsigned major, unsigned minor, const char *data)
170 if (!xo_buf_has_room(xbp, minor + 2))
173 unsigned offset = xo_buf_offset(xbp);
175 *xbp->xb_curp = major;
176 cbor_encode_uint(xbp, minor, CBOR_ULIMIT);
178 xo_buf_append(xbp, data, minor);
180 if (xo_get_flags(xop) & XOF_PRETTY)
181 cbor_memdump(stdout, "append", xo_buf_data(xbp, offset),
182 xbp->xb_curp - xbp->xb_bufp - offset, "",
187 cbor_create (xo_handle_t *xop)
189 cbor_private_t *cbor = xo_realloc(NULL, sizeof(*cbor));
193 bzero(cbor, sizeof(*cbor));
194 xo_buf_init(&cbor->c_data);
196 xo_set_private(xop, cbor);
198 cbor_append(xop, cbor, &cbor->c_data, CBOR_MAP | CBOR_INDEF, 0, NULL);
204 cbor_content (xo_handle_t *xop, cbor_private_t *cbor, xo_buffer_t *xbp,
209 unsigned offset = xo_buf_offset(xbp);
211 if (value == NULL || *value == '\0' || xo_streq(value, "true"))
212 cbor_append(xop, cbor, &cbor->c_data, CBOR_TRUE, 0, NULL);
213 else if (xo_streq(value, "false"))
214 cbor_append(xop, cbor, &cbor->c_data, CBOR_FALSE, 0, NULL);
223 unsigned long long ival;
224 ival = strtoull(value, &ep, 0);
225 if (ival == ULLONG_MAX) /* Sometimes a string is just a string */
226 cbor_append(xop, cbor, xbp, CBOR_STRING, strlen(value), value);
228 *xbp->xb_curp = negative ? CBOR_NEGATIVE : CBOR_UNSIGNED;
230 ival -= 1; /* Don't waste a negative zero */
231 cbor_encode_uint(xbp, ival, negative ? CBOR_NLIMIT : CBOR_ULIMIT);
235 if (xo_get_flags(xop) & XOF_PRETTY)
236 cbor_memdump(stdout, "content", xo_buf_data(xbp, offset),
237 xbp->xb_curp - xbp->xb_bufp - offset, "",
244 cbor_handler (XO_ENCODER_HANDLER_ARGS)
247 cbor_private_t *cbor = private;
248 xo_buffer_t *xbp = cbor ? &cbor->c_data : NULL;
250 if (xo_get_flags(xop) & XOF_PRETTY) {
251 printf("%*sop %s: [%s] [%s]\n", cbor ? cbor->c_indent * 2 + 4 : 0, "",
252 xo_encoder_op_name(op), name, value);
256 /* If we don't have private data, we're sunk */
257 if (cbor == NULL && op != XO_OP_CREATE)
261 case XO_OP_CREATE: /* Called when the handle is init'd */
262 rc = cbor_create(xop);
265 case XO_OP_OPEN_CONTAINER:
266 cbor_append(xop, cbor, xbp, CBOR_STRING, strlen(name), name);
267 cbor_append(xop, cbor, xbp, CBOR_MAP | CBOR_INDEF, 0, NULL);
271 case XO_OP_CLOSE_CONTAINER:
272 cbor_append(xop, cbor, xbp, CBOR_BREAK, 0, NULL);
276 case XO_OP_OPEN_LIST:
277 cbor_append(xop, cbor, xbp, CBOR_STRING, strlen(name), name);
278 cbor_append(xop, cbor, xbp, CBOR_ARRAY | CBOR_INDEF, 0, NULL);
282 case XO_OP_CLOSE_LIST:
283 cbor_append(xop, cbor, xbp, CBOR_BREAK, 0, NULL);
287 case XO_OP_OPEN_LEAF_LIST:
288 cbor_append(xop, cbor, xbp, CBOR_STRING, strlen(name), name);
289 cbor_append(xop, cbor, xbp, CBOR_ARRAY | CBOR_INDEF, 0, NULL);
291 cbor->c_open_leaf_list = 1;
294 case XO_OP_CLOSE_LEAF_LIST:
295 cbor_append(xop, cbor, xbp, CBOR_BREAK, 0, NULL);
297 cbor->c_open_leaf_list = 0;
300 case XO_OP_OPEN_INSTANCE:
301 cbor_append(xop, cbor, xbp, CBOR_MAP | CBOR_INDEF, 0, NULL);
305 case XO_OP_CLOSE_INSTANCE:
306 cbor_append(xop, cbor, xbp, CBOR_BREAK, 0, NULL);
310 case XO_OP_STRING: /* Quoted UTF-8 string */
311 if (!cbor->c_open_leaf_list)
312 cbor_append(xop, cbor, xbp, CBOR_STRING, strlen(name), name);
313 cbor_append(xop, cbor, xbp, CBOR_STRING, strlen(value), value);
316 case XO_OP_CONTENT: /* Other content */
317 if (!cbor->c_open_leaf_list)
318 cbor_append(xop, cbor, xbp, CBOR_STRING, strlen(name), name);
321 * It's content, not string, so we need to look at the
322 * string and build some content. Turns out we only
323 * care about true, false, null, and numbers.
325 cbor_content(xop, cbor, xbp, value);
328 case XO_OP_FINISH: /* Clean up function */
329 cbor_append(xop, cbor, xbp, CBOR_BREAK, 0, NULL);
333 case XO_OP_FLUSH: /* Clean up function */
334 if (xo_get_flags(xop) & XOF_PRETTY)
335 cbor_memdump(stdout, "cbor",
336 xbp->xb_bufp, xbp->xb_curp - xbp->xb_bufp,
339 rc = write(1, xbp->xb_bufp, xbp->xb_curp - xbp->xb_bufp);
345 case XO_OP_DESTROY: /* Clean up function */
348 case XO_OP_ATTRIBUTE: /* Attribute name/value */
351 case XO_OP_VERSION: /* Version string */
360 xo_encoder_library_init (XO_ENCODER_INIT_ARGS)
362 arg->xei_handler = cbor_handler;
363 arg->xei_version = XO_ENCODER_VERSION;