libyang REFACTOR parsed/printed bytes in in/out structs
diff --git a/src/parser_lyb.c b/src/parser_lyb.c
index 499cf28..18e29e4 100644
--- a/src/parser_lyb.c
+++ b/src/parser_lyb.c
@@ -43,7 +43,6 @@
static void
lyb_read(uint8_t *buf, size_t count, struct lyd_lyb_ctx *lybctx)
{
- int parsed = 0;
LY_ARRAY_COUNT_TYPE u;
struct lyd_lyb_subtree *empty;
size_t to_read;
@@ -72,7 +71,9 @@
/* we are actually reading some data, not just finishing another chunk */
if (to_read) {
if (buf) {
- memcpy(buf, lybctx->data + parsed, to_read);
+ ly_in_read(lybctx->in, buf, to_read);
+ } else {
+ ly_in_skip(lybctx->in, to_read);
}
LY_ARRAY_FOR(lybctx->subtrees, u) {
@@ -85,25 +86,18 @@
if (buf) {
buf += to_read;
}
-
- parsed += to_read;
}
if (empty) {
/* read the next chunk meta information */
- memcpy(meta_buf, lybctx->data + parsed, LYB_META_BYTES);
+ ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES);
empty->written = meta_buf[0];
empty->inner_chunks = meta_buf[1];
/* remember whether there is a following chunk or not */
empty->position = (empty->written == LYB_SIZE_MAX ? 1 : 0);
-
- parsed += LYB_META_BYTES;
}
}
-
- lybctx->byte_count += parsed;
- lybctx->data += parsed;
}
/**
@@ -233,15 +227,13 @@
lybctx->subtree_size = u + LYB_SUBTREE_STEP;
}
- memcpy(meta_buf, lybctx->data, LYB_META_BYTES);
+ LY_CHECK_RET(ly_in_read(lybctx->in, meta_buf, LYB_META_BYTES));
LY_ARRAY_INCREMENT(lybctx->subtrees);
LYB_LAST_SUBTREE(lybctx).written = meta_buf[0];
LYB_LAST_SUBTREE(lybctx).inner_chunks = meta_buf[LYB_SIZE_BYTES];
LYB_LAST_SUBTREE(lybctx).position = (LYB_LAST_SUBTREE(lybctx).written == LYB_SIZE_MAX ? 1 : 0);
- lybctx->byte_count += LYB_META_BYTES;
- lybctx->data += LYB_META_BYTES;
return LY_SUCCESS;
}
@@ -693,13 +685,9 @@
static void
lyb_skip_subtree(struct lyd_lyb_ctx *lybctx)
{
- int parsed;
-
do {
/* first skip any meta information inside */
- parsed = LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES;
- lybctx->data += parsed;
- lybctx->byte_count += parsed;
+ ly_in_skip(lybctx->in, LYB_LAST_SUBTREE(lybctx).inner_chunks * LYB_META_BYTES);
/* then read data */
lyb_read(NULL, LYB_LAST_SUBTREE(lybctx).written, lybctx);
@@ -1017,7 +1005,8 @@
}
LY_ERR
-lyd_parse_lyb_data(const struct ly_ctx *ctx, const char *data, int parse_options, int validate_options, struct lyd_node **tree, int *parsed_bytes)
+lyd_parse_lyb_data(const struct ly_ctx *ctx, struct ly_in *in, int parse_options, int validate_options,
+ struct lyd_node **tree)
{
LY_ERR ret = LY_SUCCESS;
struct lyd_lyb_ctx lybctx = {0};
@@ -1027,7 +1016,7 @@
*tree = NULL;
- lybctx.data = data;
+ lybctx.in = in;
lybctx.ctx = ctx;
lybctx.parse_options = parse_options;
lybctx.validate_options = validate_options;
@@ -1045,14 +1034,13 @@
LY_CHECK_GOTO(ret, cleanup);
/* read subtree(s) */
- while (lybctx.data[0]) {
+ while (lybctx.in->current[0]) {
ret = lyb_parse_subtree_r(&lybctx, NULL, tree);
LY_CHECK_GOTO(ret, cleanup);
}
/* read the last zero, parsing finished */
- ++lybctx.byte_count;
- ++lybctx.data;
+ ly_in_skip(lybctx.in, 1);
/* TODO validation */
@@ -1063,9 +1051,6 @@
ly_set_erase(&lybctx.unres_meta_type, NULL);
ly_set_erase(&lybctx.when_check, NULL);
- if (parsed_bytes) {
- *parsed_bytes = lybctx.byte_count;
- }
if (ret) {
lyd_free_all(*tree);
*tree = NULL;
@@ -1074,12 +1059,12 @@
}
LY_ERR
-lyd_parse_lyb_rpc(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree, struct lyd_node **op, int *parsed_bytes)
+lyd_parse_lyb_rpc(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree, struct lyd_node **op)
{
LY_ERR ret = LY_SUCCESS;
struct lyd_lyb_ctx lybctx = {0};
- lybctx.data = data;
+ lybctx.in = in;
lybctx.ctx = ctx;
lybctx.parse_options = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
lybctx.int_opts = LYD_INTOPT_RPC;
@@ -1102,14 +1087,13 @@
LY_CHECK_GOTO(ret, cleanup);
/* read subtree(s) */
- while (lybctx.data[0]) {
+ while (lybctx.in->current[0]) {
ret = lyb_parse_subtree_r(&lybctx, NULL, tree);
LY_CHECK_GOTO(ret, cleanup);
}
/* read the last zero, parsing finished */
- ++lybctx.byte_count;
- ++lybctx.data;
+ ly_in_skip(lybctx.in, 1);
/* make sure we have parsed some operation */
if (!lybctx.op_ntf) {
@@ -1128,9 +1112,6 @@
LY_ARRAY_FREE(lybctx.models);
assert(!lybctx.unres_node_type.count && !lybctx.unres_meta_type.count && !lybctx.when_check.count);
- if (parsed_bytes) {
- *parsed_bytes = lybctx.byte_count;
- }
if (ret) {
lyd_free_all(*tree);
*tree = NULL;
@@ -1139,12 +1120,12 @@
}
LY_ERR
-lyd_parse_lyb_notif(const struct ly_ctx *ctx, const char *data, struct lyd_node **tree, struct lyd_node **ntf, int *parsed_bytes)
+lyd_parse_lyb_notif(const struct ly_ctx *ctx, struct ly_in *in, struct lyd_node **tree, struct lyd_node **ntf)
{
LY_ERR ret = LY_SUCCESS;
struct lyd_lyb_ctx lybctx = {0};
- lybctx.data = data;
+ lybctx.in = in;
lybctx.ctx = ctx;
lybctx.parse_options = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
lybctx.int_opts = LYD_INTOPT_NOTIF;
@@ -1167,14 +1148,13 @@
LY_CHECK_GOTO(ret, cleanup);
/* read subtree(s) */
- while (lybctx.data[0]) {
+ while (lybctx.in->current[0]) {
ret = lyb_parse_subtree_r(&lybctx, NULL, tree);
LY_CHECK_GOTO(ret, cleanup);
}
/* read the last zero, parsing finished */
- ++lybctx.byte_count;
- ++lybctx.data;
+ ly_in_skip(lybctx.in, 1);
/* make sure we have parsed some notification */
if (!lybctx.op_ntf) {
@@ -1193,9 +1173,6 @@
LY_ARRAY_FREE(lybctx.models);
assert(!lybctx.unres_node_type.count && !lybctx.unres_meta_type.count && !lybctx.when_check.count);
- if (parsed_bytes) {
- *parsed_bytes = lybctx.byte_count;
- }
if (ret) {
lyd_free_all(*tree);
*tree = NULL;
@@ -1204,14 +1181,13 @@
}
LY_ERR
-lyd_parse_lyb_reply(const struct lyd_node *request, const char *data, struct lyd_node **tree, struct lyd_node **op,
- int *parsed_bytes)
+lyd_parse_lyb_reply(const struct lyd_node *request, struct ly_in *in, struct lyd_node **tree, struct lyd_node **op)
{
LY_ERR ret = LY_SUCCESS;
struct lyd_lyb_ctx lybctx = {0};
struct lyd_node *iter, *req_op, *rep_op = NULL;
- lybctx.data = data;
+ lybctx.in = in;
lybctx.ctx = LYD_NODE_CTX(request);
lybctx.parse_options = LYD_PARSE_ONLY | LYD_PARSE_STRICT;
lybctx.int_opts = LYD_INTOPT_REPLY;
@@ -1251,14 +1227,13 @@
LY_CHECK_GOTO(ret, cleanup);
/* read subtree(s) */
- while (lybctx.data[0]) {
+ while (lybctx.in->current[0]) {
ret = lyb_parse_subtree_r(&lybctx, (struct lyd_node_inner *)rep_op, NULL);
LY_CHECK_GOTO(ret, cleanup);
}
/* read the last zero, parsing finished */
- ++lybctx.byte_count;
- ++lybctx.data;
+ ly_in_skip(lybctx.in, 1);
if (op) {
*op = rep_op;
@@ -1273,9 +1248,6 @@
LY_ARRAY_FREE(lybctx.models);
assert(!lybctx.unres_node_type.count && !lybctx.unres_meta_type.count && !lybctx.when_check.count);
- if (parsed_bytes) {
- *parsed_bytes = lybctx.byte_count;
- }
if (ret) {
lyd_free_all(*tree);
*tree = NULL;
@@ -1296,7 +1268,8 @@
return -1;
}
- lybctx.data = data;
+ ret = ly_in_new_memory(data, &lybctx.in);
+ LY_CHECK_GOTO(ret, cleanup);
/* read magic number */
ret = lyb_parse_magic_number(&lybctx);
@@ -1322,7 +1295,7 @@
lyb_read(buf, 2, &lybctx);
}
- while (lybctx.data[0]) {
+ while (lybctx.in->current[0]) {
/* register a new subtree */
ret = lyb_read_start_subtree(&lybctx);
LY_CHECK_GOTO(ret, cleanup);
@@ -1336,10 +1309,12 @@
}
/* read the last zero, parsing finished */
- ++lybctx.byte_count;
- ++lybctx.data;
+ ly_in_skip(lybctx.in, 1);
cleanup:
+ count = lybctx.in->current - lybctx.in->start;
+
+ ly_in_free(lybctx.in, 0);
LY_ARRAY_FREE(lybctx.subtrees);
- return ret ? -1 : (signed)lybctx.byte_count;
+ return ret ? -1 : count;
}