lyb parser REFACTOR add lyb_read_hashes
diff --git a/src/parser_lyb.c b/src/parser_lyb.c
index 9ede71d..ef47fed 100644
--- a/src/parser_lyb.c
+++ b/src/parser_lyb.c
@@ -635,6 +635,52 @@
}
/**
+ * @brief Fill @p hash with hash values.
+ *
+ * @param[in] lybctx LYB context.
+ * @param[in,out] hash Pointer to the array in which the hash values are to be written.
+ * @param[out] hash_count Number of hashes in @p hash.
+ * @return LY_ERR value.
+ */
+static LY_ERR
+lyb_read_hashes(struct lylyb_ctx *lybctx, LYB_HASH *hash, uint8_t *hash_count)
+{
+ uint8_t i = 0, j;
+
+ /* read the first hash */
+ lyb_read(&hash[0], sizeof *hash, lybctx);
+
+ if (!hash[0]) {
+ *hash_count = i + 1;
+ return LY_SUCCESS;
+ }
+
+ /* based on the first hash read all the other ones, if any */
+ for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
+ if (i > LYB_HASH_BITS) {
+ LOGINT_RET(lybctx->ctx);
+ }
+ }
+
+ /* move the first hash on its accurate position */
+ hash[i] = hash[0];
+
+ /* read the rest of hashes */
+ for (j = i; j; --j) {
+ lyb_read(&hash[j - 1], sizeof *hash, lybctx);
+
+ /* correct collision ID */
+ assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
+ /* preceded with zeros */
+ assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
+ }
+
+ *hash_count = i + 1;
+
+ return LY_SUCCESS;
+}
+
+/**
* @brief Check whether a schema node matches a hash(es).
*
* @param[in] sibling Schema node to check.
@@ -674,41 +720,21 @@
const struct lysc_node **snode)
{
LY_ERR ret;
- uint8_t i, j;
const struct lysc_node *sibling;
LYB_HASH hash[LYB_HASH_BITS - 1];
uint32_t getnext_opts;
+ uint8_t hash_count;
- *snode = NULL;
- getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
-
- /* read the first hash */
- lyb_read(&hash[0], sizeof *hash, lybctx->lybctx);
+ ret = lyb_read_hashes(lybctx->lybctx, hash, &hash_count);
+ LY_CHECK_RET(ret);
if (!hash[0]) {
/* opaque node */
return LY_SUCCESS;
}
- /* based on the first hash read all the other ones, if any */
- for (i = 0; !(hash[0] & (LYB_HASH_COLLISION_ID >> i)); ++i) {
- if (i > LYB_HASH_BITS) {
- LOGINT_RET(lybctx->lybctx->ctx);
- }
- }
-
- /* move the first hash on its accurate position */
- hash[i] = hash[0];
-
- /* read the rest of hashes */
- for (j = i; j; --j) {
- lyb_read(&hash[j - 1], sizeof *hash, lybctx->lybctx);
-
- /* correct collision ID */
- assert(hash[j - 1] & (LYB_HASH_COLLISION_ID >> (j - 1)));
- /* preceded with zeros */
- assert(!(hash[j - 1] & (LYB_HASH_MASK << (LYB_HASH_BITS - (j - 1)))));
- }
+ *snode = NULL;
+ getnext_opts = lybctx->int_opts & LYD_INTOPT_REPLY ? LYS_GETNEXT_OUTPUT : 0;
/* find our node with matching hashes */
sibling = NULL;
@@ -723,7 +749,7 @@
}
/* skip schema nodes from models not present during printing */
if (lyb_has_schema_model(sibling, lybctx->lybctx->models) &&
- lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, i + 1)) {
+ lyb_is_schema_hash_match((struct lysc_node *)sibling, hash, hash_count)) {
/* match found */
break;
}