* https://www.openssl.org/source/license.html
*/
-#ifndef JSON_ENC_H
-# define JSON_ENC_H
+#ifndef OSSL_JSON_ENC_H
+# define OSSL_JSON_ENC_H
# include <openssl/bio.h>
size_t alloc, cur;
};
-typedef struct json_enc_st {
+typedef struct ossl_json_enc_st {
uint32_t flags;
/* error: 1 if an error has occurred. */
/* state: current state. */
unsigned char stack_small[16];
struct json_write_buf wbuf;
size_t stack_end_byte, stack_bytes;
-} JSON_ENC;
+} OSSL_JSON_ENC;
/*
* ossl_json_init
*
* Initialises a JSON encoder.
*
- * If the flag JSON_FLAG_SEQ is passed, the output is in JSON-SEQ. The caller
- * should use the encoder as though it is encoding members of a JSON array (but
- * without calling ossl_json_array_begin() or ossl_json_array_end()). Each
- * top-level JSON item (e.g. JSON object) encoded will be separated correctly as
- * per the JSON-SEQ format.
+ * If the flag OSSL_JSON_FLAG_SEQ is passed, the output is in JSON-SEQ. The
+ * caller should use the encoder as though it is encoding members of a JSON
+ * array (but without calling ossl_json_array_begin() or ossl_json_array_end()).
+ * Each top-level JSON item (e.g. JSON object) encoded will be separated
+ * correctly as per the JSON-SEQ format.
*
- * If the flag JSON_FLAG_SEQ is not passed, the output is in JSON format.
- * Generally the caller should encode only a single output item (e.g.
- * a JSON object).
+ * If the flag OSSL_JSON_FLAG_SEQ is not passed, the output is in JSON format.
+ * Generally the caller should encode only a single output item (e.g. a JSON
+ * object).
*
- * By default, JSON output is maximally compact. If JSON_FLAG_PRETTY is set,
- * JSON/JSON-SEQ output is spaced for optimal human readability.
+ * By default, JSON output is maximally compact. If OSSL_JSON_FLAG_PRETTY is
+ * set, JSON/JSON-SEQ output is spaced for optimal human readability.
*
- * If JSON_FLAG_IJSON is set, integers outside the range `[-2**53 + 1, 2**53 -
- * 1]` are automatically converted to decimal strings before serialization.
+ * If OSSL_JSON_FLAG_IJSON is set, integers outside the range `[-2**53 + 1,
+ * 2**53 - 1]` are automatically converted to decimal strings before
+ * serialization.
*/
-#define JSON_FLAG_NONE 0
-#define JSON_FLAG_SEQ (1U << 0)
-#define JSON_FLAG_PRETTY (1U << 1)
-#define JSON_FLAG_IJSON (1U << 2)
+#define OSSL_JSON_FLAG_NONE 0
+#define OSSL_JSON_FLAG_SEQ (1U << 0)
+#define OSSL_JSON_FLAG_PRETTY (1U << 1)
+#define OSSL_JSON_FLAG_IJSON (1U << 2)
-int ossl_json_init(JSON_ENC *json, BIO *bio, uint32_t flags);
+int ossl_json_init(OSSL_JSON_ENC *json, BIO *bio, uint32_t flags);
/*
* ossl_json_cleanup
*
* Destroys a JSON encoder.
*/
-void ossl_json_cleanup(JSON_ENC *json);
+void ossl_json_cleanup(OSSL_JSON_ENC *json);
/*
* ossl_json_reset
*
* Automatically calls ossl_json_flush().
*/
-int ossl_json_reset(JSON_ENC *json);
+int ossl_json_reset(OSSL_JSON_ENC *json);
/*
* ossl_json_flush
* autonomously as buffers are filled, but the caller must use this function
* to guarantee all data has been flushed.
*/
-int ossl_json_flush(JSON_ENC *json);
+int ossl_json_flush(OSSL_JSON_ENC *json);
/*
* ossl_json_flush_cleanup
* ossl_json_cleanup regardless of the result. The result of the flush call is
* returned.
*/
-int ossl_json_flush_cleanup(JSON_ENC *json);
+int ossl_json_flush_cleanup(OSSL_JSON_ENC *json);
/*
* ossl_json_set_sink
*
* Changes the sink used by the JSON encoder.
*/
-int ossl_json_set_sink(JSON_ENC *json, BIO *bio);
+int ossl_json_set_sink(OSSL_JSON_ENC *json, BIO *bio);
/*
* ossl_json_in_error
* flushing. It is expected that errors will ordinarily be either caller errors
* (programming errors) or BIO errors.
*/
-int ossl_json_in_error(JSON_ENC *json);
+int ossl_json_in_error(OSSL_JSON_ENC *json);
/*
* JSON Builder Calls
*/
/* Begin a new JSON object. */
-void ossl_json_object_begin(JSON_ENC *json);
+void ossl_json_object_begin(OSSL_JSON_ENC *json);
/* End a JSON obejct. Must be matched with a call to ossl_json_object_begin(). */
-void ossl_json_object_end(JSON_ENC *json);
+void ossl_json_object_end(OSSL_JSON_ENC *json);
/* Begin a new JSON array. */
-void ossl_json_array_begin(JSON_ENC *json);
+void ossl_json_array_begin(OSSL_JSON_ENC *json);
/* End a JSON array. Must be matched with a call to ossl_json_array_end(). */
-void ossl_json_array_end(JSON_ENC *json);
+void ossl_json_array_end(OSSL_JSON_ENC *json);
/*
* Encode a JSON key within an object. Pass a zero-terminated string, which can
* be freed immediately following the call to this function.
*/
-void ossl_json_key(JSON_ENC *json, const char *key);
+void ossl_json_key(OSSL_JSON_ENC *json, const char *key);
/* Encode a JSON 'null' value. */
-void ossl_json_null(JSON_ENC *json);
+void ossl_json_null(OSSL_JSON_ENC *json);
/* Encode a JSON boolean value. */
-void ossl_json_bool(JSON_ENC *json, int value);
+void ossl_json_bool(OSSL_JSON_ENC *json, int value);
/* Encode a JSON integer from a uint64_t. */
-void ossl_json_u64(JSON_ENC *json, uint64_t value);
+void ossl_json_u64(OSSL_JSON_ENC *json, uint64_t value);
/* Encode a JSON integer from an int64_t. */
-void ossl_json_i64(JSON_ENC *json, int64_t value);
+void ossl_json_i64(OSSL_JSON_ENC *json, int64_t value);
/* Encode a JSON number from a 64-bit floating point value. */
-void ossl_json_f64(JSON_ENC *json, double value);
+void ossl_json_f64(OSSL_JSON_ENC *json, double value);
/*
* Encode a JSON UTF-8 string from a zero-terminated string. The string passed
* can be freed immediately following the call to this function.
*/
-void ossl_json_str(JSON_ENC *json, const char *str);
+void ossl_json_str(OSSL_JSON_ENC *json, const char *str);
/*
* Encode a JSON UTF-8 string from a string with the given length. The string
* passed can be freed immediately following the call to this function.
*/
-void ossl_json_str_len(JSON_ENC *json, const char *str, size_t str_len);
+void ossl_json_str_len(OSSL_JSON_ENC *json, const char *str, size_t str_len);
/*
* Encode binary data as a lowercase hex string. data_len is the data length in
* bytes.
*/
-void ossl_json_str_hex(JSON_ENC *json, const void *data, size_t data_len);
+void ossl_json_str_hex(OSSL_JSON_ENC *json, const void *data, size_t data_len);
#endif
}
/*
- * JSON_ENC: Stack Management
- * ==========================
+ * OSSL_JSON_ENC: Stack Management
+ * ===============================
*/
-static int json_ensure_stack_size(JSON_ENC *json, size_t num_bytes)
+static int json_ensure_stack_size(OSSL_JSON_ENC *json, size_t num_bytes)
{
unsigned char *stack;
}
/* Push one bit onto the stack. Returns 0 on allocation failure. */
-static int json_push(JSON_ENC *json, unsigned int v)
+static int json_push(OSSL_JSON_ENC *json, unsigned int v)
{
if (v > 1)
return 0;
* Pop a bit from the stack. Returns 0 if stack is empty. Use json_peek() to get
* the value before calling this.
*/
-static int json_pop(JSON_ENC *json)
+static int json_pop(OSSL_JSON_ENC *json)
{
if (json->stack_end_byte == 0 && json->stack_end_bit == 0)
return 0;
/*
* Returns the bit on the top of the stack, or -1 if the stack is empty.
*/
-static int json_peek(JSON_ENC *json)
+static int json_peek(OSSL_JSON_ENC *json)
{
size_t obyte, obit;
}
/*
- * JSON_ENC: Initialisation
- * ========================
+ * OSSL_JSON_ENC: Initialisation
+ * =============================
*/
enum {
STATE_PRE_COMMA
};
-static ossl_inline int in_ijson(const JSON_ENC *json)
+static ossl_inline int in_ijson(const OSSL_JSON_ENC *json)
{
- return (json->flags & JSON_FLAG_IJSON) != 0;
+ return (json->flags & OSSL_JSON_FLAG_IJSON) != 0;
}
-static ossl_inline int in_seq(const JSON_ENC *json)
+static ossl_inline int in_seq(const OSSL_JSON_ENC *json)
{
- return (json->flags & JSON_FLAG_SEQ) != 0;
+ return (json->flags & OSSL_JSON_FLAG_SEQ) != 0;
}
-static ossl_inline int in_pretty(const JSON_ENC *json)
+static ossl_inline int in_pretty(const OSSL_JSON_ENC *json)
{
- return (json->flags & JSON_FLAG_PRETTY) != 0;
+ return (json->flags & OSSL_JSON_FLAG_PRETTY) != 0;
}
-int ossl_json_init(JSON_ENC *json, BIO *bio, uint32_t flags)
+int ossl_json_init(OSSL_JSON_ENC *json, BIO *bio, uint32_t flags)
{
memset(json, 0, sizeof(*json));
json->flags = flags;
return ossl_json_reset(json);
}
-void ossl_json_cleanup(JSON_ENC *json)
+void ossl_json_cleanup(OSSL_JSON_ENC *json)
{
wbuf_cleanup(&json->wbuf);
json->stack = NULL;
}
-int ossl_json_flush_cleanup(JSON_ENC *json)
+int ossl_json_flush_cleanup(OSSL_JSON_ENC *json)
{
int ok = ossl_json_flush(json);
return ok;
}
-int ossl_json_reset(JSON_ENC *json)
+int ossl_json_reset(OSSL_JSON_ENC *json)
{
wbuf_clean(&json->wbuf);
json->stack_end_byte = 0;
return 1;
}
-int ossl_json_flush(JSON_ENC *json)
+int ossl_json_flush(OSSL_JSON_ENC *json)
{
return wbuf_flush(&json->wbuf);
}
-int ossl_json_set_sink(JSON_ENC *json, BIO *bio)
+int ossl_json_set_sink(OSSL_JSON_ENC *json, BIO *bio)
{
wbuf_set_bio(&json->wbuf, bio);
return 1;
}
-int ossl_json_in_error(JSON_ENC *json)
+int ossl_json_in_error(OSSL_JSON_ENC *json)
{
return json->error;
}
* ==================
*/
-static void json_write_qstring(JSON_ENC *json, const char *str);
-static void json_indent(JSON_ENC *json);
+static void json_write_qstring(OSSL_JSON_ENC *json, const char *str);
+static void json_indent(OSSL_JSON_ENC *json);
-static ossl_inline int json_in_error(const JSON_ENC *json)
+static ossl_inline int json_in_error(const OSSL_JSON_ENC *json)
{
return json->error;
}
-static void json_raise_error(JSON_ENC *json)
+static void json_raise_error(OSSL_JSON_ENC *json)
{
json->error = 1;
}
-static void json_undefer(JSON_ENC *json)
+static void json_undefer(OSSL_JSON_ENC *json)
{
if (!json->defer_indent)
return;
json_indent(json);
}
-static void json_write_char(JSON_ENC *json, char ch)
+static void json_write_char(OSSL_JSON_ENC *json, char ch)
{
if (json_in_error(json))
return;
json_raise_error(json);
}
-static void json_write_str(JSON_ENC *json, const char *s)
+static void json_write_str(OSSL_JSON_ENC *json, const char *s)
{
if (json_in_error(json))
return;
json_raise_error(json);
}
-static void json_indent(JSON_ENC *json)
+static void json_indent(OSSL_JSON_ENC *json)
{
size_t i, depth;
json_write_str(json, " ");
}
-static int json_pre_item(JSON_ENC *json)
+static int json_pre_item(OSSL_JSON_ENC *json)
{
int s;
return 1;
}
-static void json_post_item(JSON_ENC *json)
+static void json_post_item(OSSL_JSON_ENC *json)
{
int s = json_peek(json);
*
* type: 0=object, 1=array.
*/
-static void composite_begin(JSON_ENC *json, int type, char ch)
+static void composite_begin(OSSL_JSON_ENC *json, int type, char ch)
{
if (!json_pre_item(json)
|| !json_push(json, type))
*
* type: 0=object, 1=array. Errors on mismatch.
*/
-static void composite_end(JSON_ENC *json, int type, char ch)
+static void composite_end(OSSL_JSON_ENC *json, int type, char ch)
{
int was_defer = json->defer_indent;
}
/* Begin a new JSON object. */
-void ossl_json_object_begin(JSON_ENC *json)
+void ossl_json_object_begin(OSSL_JSON_ENC *json)
{
composite_begin(json, 0, '{');
json->state = STATE_PRE_KEY;
}
/* End a JSON obejct. Must be matched with a call to ossl_json_object_begin(). */
-void ossl_json_object_end(JSON_ENC *json)
+void ossl_json_object_end(OSSL_JSON_ENC *json)
{
composite_end(json, 0, '}');
}
/* Begin a new JSON array. */
-void ossl_json_array_begin(JSON_ENC *json)
+void ossl_json_array_begin(OSSL_JSON_ENC *json)
{
composite_begin(json, 1, '[');
json->state = STATE_PRE_ITEM;
}
/* End a JSON array. Must be matched with a call to ossl_json_array_end(). */
-void ossl_json_array_end(JSON_ENC *json)
+void ossl_json_array_end(OSSL_JSON_ENC *json)
{
composite_end(json, 1, ']');
}
* Encode a JSON key within an object. Pass a zero-terminated string, which can
* be freed immediately following the call to this function.
*/
-void ossl_json_key(JSON_ENC *json, const char *key)
+void ossl_json_key(OSSL_JSON_ENC *json, const char *key)
{
if (json_in_error(json))
return;
}
/* Encode a JSON 'null' value. */
-void ossl_json_null(JSON_ENC *json)
+void ossl_json_null(OSSL_JSON_ENC *json)
{
if (!json_pre_item(json))
return;
json_post_item(json);
}
-void ossl_json_bool(JSON_ENC *json, int v)
+void ossl_json_bool(OSSL_JSON_ENC *json, int v)
{
if (!json_pre_item(json))
return;
#define POW_53 (((int64_t)1) << 53)
/* Encode a JSON integer from a uint64_t. */
-static void json_u64(JSON_ENC *json, uint64_t v, int noquote)
+static void json_u64(OSSL_JSON_ENC *json, uint64_t v, int noquote)
{
char buf[22], *p = buf + sizeof(buf) - 1;
int quote = !noquote && in_ijson(json) && v > (uint64_t)(POW_53 - 1);
json_post_item(json);
}
-void ossl_json_u64(JSON_ENC *json, uint64_t v)
+void ossl_json_u64(OSSL_JSON_ENC *json, uint64_t v)
{
json_u64(json, v, 0);
}
/* Encode a JSON integer from an int64_t. */
-void ossl_json_i64(JSON_ENC *json, int64_t value)
+void ossl_json_i64(OSSL_JSON_ENC *json, int64_t value)
{
uint64_t uv;
int quote;
}
/* Encode a JSON number from a 64-bit floating point value. */
-void ossl_json_f64(JSON_ENC *json, double value)
+void ossl_json_f64(OSSL_JSON_ENC *json, double value)
{
char buf[32];
}
static ossl_inline void
-json_write_qstring_inner(JSON_ENC *json, const char *str, size_t str_len,
+json_write_qstring_inner(OSSL_JSON_ENC *json, const char *str, size_t str_len,
int nul_term)
{
char c, *o, obuf[7];
}
static void
-json_write_qstring(JSON_ENC *json, const char *str)
+json_write_qstring(OSSL_JSON_ENC *json, const char *str)
{
json_write_qstring_inner(json, str, 0, 1);
}
static void
-json_write_qstring_len(JSON_ENC *json, const char *str, size_t str_len)
+json_write_qstring_len(OSSL_JSON_ENC *json, const char *str, size_t str_len)
{
json_write_qstring_inner(json, str, str_len, 0);
}
-void ossl_json_str(JSON_ENC *json, const char *str)
+void ossl_json_str(OSSL_JSON_ENC *json, const char *str)
{
if (!json_pre_item(json))
return;
json_post_item(json);
}
-void ossl_json_str_len(JSON_ENC *json, const char *str, size_t str_len)
+void ossl_json_str_len(OSSL_JSON_ENC *json, const char *str, size_t str_len)
{
if (!json_pre_item(json))
return;
* Encode binary data as a lowercase hex string. data_len is the data length in
* bytes.
*/
-void ossl_json_str_hex(JSON_ENC *json, const void *data, size_t data_len)
+void ossl_json_str_hex(OSSL_JSON_ENC *json, const void *data, size_t data_len)
{
const unsigned char *b = data, *end = b + data_len;
unsigned char c;
p[bit_no / BITS_PER_WORD] &= mask;
}
-/* TODO abort */
-
struct qlog_st {
QLOG_TRACE_INFO info;
- BIO *bio;
- size_t enabled[NUM_ENABLED_W];
- uint32_t event_type;
- const char *event_cat, *event_name, *event_combined_name;
- OSSL_TIME event_time, prev_event_time;
- JSON_ENC json;
- int header_done, first_event_done;
+ BIO *bio;
+ size_t enabled[NUM_ENABLED_W];
+ uint32_t event_type;
+ const char *event_cat, *event_name, *event_combined_name;
+ OSSL_TIME event_time, prev_event_time;
+ OSSL_JSON_ENC json;
+ int header_done, first_event_done;
};
static OSSL_TIME default_now(void *arg)
&& (qlog->info.group_id = OPENSSL_strdup(info->group_id)) == NULL)
goto err;
- if (!ossl_json_init(&qlog->json, NULL, JSON_FLAG_IJSON | JSON_FLAG_SEQ))
+ if (!ossl_json_init(&qlog->json, NULL,
+ OSSL_JSON_FLAG_IJSON | OSSL_JSON_FLAG_SEQ))
goto err;
if (qlog->info.now_cb == NULL)
#include "internal/json_enc.h"
struct helper {
- JSON_ENC j;
- int init;
- uint32_t flags;
- BIO *mem_bio;
+ OSSL_JSON_ENC j;
+ int init;
+ uint32_t flags;
+ BIO *mem_bio;
};
static int helper_ensure(struct helper *h)
enum {
OPK_END,
- OPK_CALL, /* (JSON_ENC *) */
- OPK_CALL_P, /* (JSON_ENC *, const void *) */
- OPK_CALL_I, /* (JSON_ENC *, int) */
- OPK_CALL_U64, /* (JSON_ENC *, uint64_t) */
- OPK_CALL_I64, /* (JSON_ENC *, int64_t) */
- OPK_CALL_D, /* (JSON_ENC *, double) */
- OPK_CALL_PZ, /* (JSON_ENC *, const void *, size_t) */
- OPK_ASSERT_ERROR, /* (JSON_ENC *, int expect_error) */
+ OPK_CALL, /* (OSSL_JSON_ENC *) */
+ OPK_CALL_P, /* (OSSL_JSON_ENC *, const void *) */
+ OPK_CALL_I, /* (OSSL_JSON_ENC *, int) */
+ OPK_CALL_U64, /* (OSSL_JSON_ENC *, uint64_t) */
+ OPK_CALL_I64, /* (OSSL_JSON_ENC *, int64_t) */
+ OPK_CALL_D, /* (OSSL_JSON_ENC *, double) */
+ OPK_CALL_PZ, /* (OSSL_JSON_ENC *, const void *, size_t) */
+ OPK_ASSERT_ERROR, /* (OSSL_JSON_ENC *, int expect_error) */
OPK_INIT_FLAGS, /* (uint32_t flags) */
};
-typedef void (*fp_type)(JSON_ENC *);
-typedef void (*fp_p_type)(JSON_ENC *, const void *);
-typedef void (*fp_i_type)(JSON_ENC *, int);
-typedef void (*fp_u64_type)(JSON_ENC *, uint64_t);
-typedef void (*fp_i64_type)(JSON_ENC *, int64_t);
-typedef void (*fp_d_type)(JSON_ENC *, double);
-typedef void (*fp_pz_type)(JSON_ENC *, const void *, size_t);
+typedef void (*fp_type)(OSSL_JSON_ENC *);
+typedef void (*fp_p_type)(OSSL_JSON_ENC *, const void *);
+typedef void (*fp_i_type)(OSSL_JSON_ENC *, int);
+typedef void (*fp_u64_type)(OSSL_JSON_ENC *, uint64_t);
+typedef void (*fp_i64_type)(OSSL_JSON_ENC *, int64_t);
+typedef void (*fp_d_type)(OSSL_JSON_ENC *, double);
+typedef void (*fp_pz_type)(OSSL_JSON_ENC *, const void *, size_t);
#define OP_END() OP_U64(OPK_END)
#define OP_CALL(f) OP_U64(OPK_CALL) OP_FP(f)
OP_ASSERT_ERROR(1)
END_SCRIPT_EXPECTING_S("\"")
-BEGIN_SCRIPT(ijson_int, "I-JSON: large integer", JSON_FLAG_IJSON)
+BEGIN_SCRIPT(ijson_int, "I-JSON: large integer", OSSL_JSON_FLAG_IJSON)
OPJ_BEGIN_A()
OPJ_U64(1)
OPJ_I64(-1)
OPJ_END_A()
END_SCRIPT_EXPECTING_S("nullnull[][]")
-BEGIN_SCRIPT(seq, "JSON-SEQ", JSON_FLAG_SEQ)
+BEGIN_SCRIPT(seq, "JSON-SEQ", OSSL_JSON_FLAG_SEQ)
OPJ_NULL()
OPJ_NULL()
OPJ_NULL()