-
Notifications
You must be signed in to change notification settings - Fork 324
Add CBOR Support to libyang (#2130) #2449
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MeherRushi
wants to merge
8
commits into
CESNET:devel
Choose a base branch
from
MeherRushi:cbor_draft
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+3,624
−2
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4f426f9
feat: add initial CBOR format support with libcbor integration
MeherRushi 10f3582
refactor: restructure CBOR parser to match libyang conventions
MeherRushi c861b17
feat: add CBOR printer skeleton and rename parser functions
MeherRushi d3f1d6f
refactor: align CBOR implementation with project structure
MeherRushi d0f0949
feat: complete CBOR parser with metadata, opaque nodes, and LY_VALUE_…
MeherRushi 93d4684
feat: implement CBOR printer functionality
MeherRushi 6756989
fix: address PR review and migrate parser_cbor.c to current API
46af930
fix: address PR review and formatting changes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Try to find libcbor | ||
| # Once done this will define | ||
| # | ||
| # Read-Only variables: | ||
| # CBOR_FOUND - system has libcbor | ||
| # CBOR_INCLUDE_DIR - the libcbor include directory | ||
| # CBOR_LIBRARY - Link these to use libcbor | ||
|
|
||
| find_path(CBOR_INCLUDE_DIR | ||
| NAMES | ||
| cbor.h | ||
| PATHS | ||
| /usr/include | ||
| /usr/local/include | ||
| /opt/local/include | ||
| /sw/include | ||
| ${CMAKE_INCLUDE_PATH} | ||
| ${CMAKE_INSTALL_PREFIX}/include | ||
| ) | ||
|
|
||
| find_library(CBOR_LIBRARY | ||
| NAMES | ||
| cbor | ||
| libcbor | ||
| PATHS | ||
| /usr/lib | ||
| /usr/lib64 | ||
| /usr/local/lib | ||
| /usr/local/lib64 | ||
| /opt/local/lib | ||
| /sw/lib | ||
| ${CMAKE_LIBRARY_PATH} | ||
| ${CMAKE_INSTALL_PREFIX}/lib | ||
| ) | ||
|
|
||
| include(FindPackageHandleStandardArgs) | ||
| find_package_handle_standard_args(CBOR FOUND_VAR CBOR_FOUND REQUIRED_VARS CBOR_INCLUDE_DIR CBOR_LIBRARY) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /** | ||
| * @file lcbor.c | ||
| * @author MeherRushi <meherrrushi2@gmail.com> | ||
| * @brief CBOR data parser for libyang (abstraction over libcbor) | ||
| * | ||
| * Copyright (c) 2026 CESNET, z.s.p.o. | ||
| * | ||
| * This source code is licensed under BSD 3-Clause License (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
|
|
||
| #include <assert.h> | ||
| #include <ctype.h> | ||
| #include <errno.h> | ||
|
|
||
| #include "in_internal.h" | ||
| #include "lcbor.h" | ||
| #include "log.h" | ||
| #include "ly_common.h" | ||
|
|
||
| const char * | ||
| lycbor_token2str(enum cbor_type cbortype) | ||
| { | ||
| switch (cbortype) { | ||
| case CBOR_TYPE_UINT: | ||
| return "unsigned integer"; | ||
| case CBOR_TYPE_NEGINT: | ||
| return "negative integer"; | ||
| case CBOR_TYPE_BYTESTRING: | ||
| return "byte string"; | ||
| case CBOR_TYPE_STRING: | ||
| return "string"; | ||
| case CBOR_TYPE_ARRAY: | ||
| return "array"; | ||
| case CBOR_TYPE_MAP: | ||
| return "map"; | ||
| case CBOR_TYPE_TAG: | ||
| return "tag"; | ||
| case CBOR_TYPE_FLOAT_CTRL: | ||
| return "decimals and special values (true, false, nil, ...)"; | ||
| } | ||
|
|
||
| return ""; | ||
| } | ||
|
|
||
| void | ||
| lycbor_ctx_free(struct lycbor_ctx *cborctx) | ||
| { | ||
| if (cborctx) { | ||
| if (cborctx->cbor_data) { | ||
| cbor_decref(&cborctx->cbor_data); | ||
| } | ||
| free(cborctx); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @brief Detect CBOR format variant from input data. | ||
| * | ||
| * @param[in] in Input structure to analyze. | ||
| * @param[out] format Detected format. | ||
| * @return LY_ERR value. | ||
| */ | ||
| static LY_ERR | ||
| lydcbor_detect_format(struct ly_in *in, enum lyd_cbor_format *format) | ||
| { | ||
| /* Simple heuristic: try to parse as CBOR and examine structure */ | ||
| /* For now, default to named format */ | ||
| (void)in; | ||
| *format = LYD_CBOR_NAMED; | ||
| return LY_SUCCESS; | ||
| } | ||
|
|
||
| LY_ERR | ||
| lycbor_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lycbor_ctx **cborctx_p) | ||
| { | ||
| /* TODO : Need to restructure error handling here */ | ||
| LY_ERR ret = LY_SUCCESS; | ||
| struct lycbor_ctx *cborctx; | ||
| struct cbor_load_result result = {0}; | ||
| enum lyd_cbor_format format; | ||
|
|
||
| assert(ctx && in && cborctx_p); | ||
|
|
||
| /* TODO : error handling after the detect_format function call */ | ||
| ret = lydcbor_detect_format(in, &format); | ||
|
|
||
| /* Allocate and initialize CBOR context */ | ||
| cborctx = calloc(1, sizeof *cborctx); | ||
| LY_CHECK_ERR_RET(!cborctx, LOGMEM(ctx), LY_EMEM); | ||
| cborctx->ctx = ctx; | ||
| cborctx->in = in; | ||
| cborctx->format = format; | ||
|
|
||
| /* input line logging */ | ||
| ly_log_location(NULL, NULL, in); | ||
|
|
||
| /* load and parse CBOR data */ | ||
| cborctx->cbor_data = cbor_load((cbor_data)in->current, in->length, &result); | ||
| if (!cborctx->cbor_data) { | ||
| LOGVAL(ctx, NULL, LYVE_SYNTAX, "Failed to parse CBOR data."); | ||
| free(cborctx); | ||
| return LY_EVALID; | ||
| } | ||
| if (result.error.code != CBOR_ERR_NONE) { | ||
| LOGVAL(ctx, NULL, LYVE_SYNTAX, "CBOR parsing error (code %d).", result.error.code); | ||
| cbor_decref(&cborctx->cbor_data); | ||
| free(cborctx); | ||
| return LY_EVALID; | ||
| } | ||
|
|
||
| *cborctx_p = cborctx; | ||
| return ret; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /** | ||
| * @file lcbor.h | ||
| * @author MeherRushi <meherrrushi2@gmail.com> | ||
| * @brief CBOR data parser routines for libyang (abstraction over libcbor) | ||
| * | ||
| * Copyright (c) 2026 CESNET, z.s.p.o. | ||
| * | ||
| * This source code is licensed under BSD 3-Clause License (the "License"). | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://opensource.org/licenses/BSD-3-Clause | ||
| */ | ||
|
|
||
| #ifndef LY_CBOR_H_ | ||
| #define LY_CBOR_H_ | ||
|
|
||
| #include <stddef.h> | ||
| #include <stdint.h> | ||
| /* using libcbor as the low-level parser */ | ||
| #include <cbor.h> | ||
|
|
||
|
|
||
| #include "log.h" | ||
| #include "set.h" | ||
|
|
||
| struct ly_ctx; | ||
| struct ly_in; | ||
|
|
||
| /** | ||
| * @brief CBOR format variants for different encoding schemes | ||
| */ | ||
| enum lyd_cbor_format | ||
| { | ||
| LYD_CBOR_NAMED, /**< CBOR with named identifiers (JSON-like) */ | ||
| LYD_CBOR_SID /**< CBOR with Schema Item identifiers (future implementation) */ | ||
| }; | ||
|
|
||
| struct lycbor_ctx { | ||
| const struct ly_ctx *ctx; | ||
| struct ly_in *in; /** input structure */ | ||
|
|
||
| cbor_item_t *cbor_data; /**< parsed CBOR data */ | ||
|
|
||
| enum lyd_cbor_format format; /**< CBOR format variant */ | ||
|
|
||
| struct { | ||
| cbor_item_t *cbor_data; /**< parsed CBOR data */ | ||
| enum lyd_cbor_format format; /**< CBOR format variant */ | ||
| const char *input; | ||
| } backup; | ||
| }; | ||
|
|
||
| /** | ||
| * @brief Get a human-readable name for a CBOR type. | ||
| * | ||
| * @param[in] cbortype CBOR type. | ||
| * @return String representation of the CBOR type. | ||
| */ | ||
| const char *lycbor_token2str(enum cbor_type cbortype); | ||
|
|
||
| /** | ||
| * @brief Create new CBOR context for parsing. | ||
| * | ||
| * @param[in] ctx libyang context. | ||
| * @param[in] in CBOR string data to parse. | ||
| * @param[out] cborctx New CBOR parser context containing parsed CBOR data. | ||
| * @return LY_ERR value. | ||
| */ | ||
| LY_ERR lycbor_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lycbor_ctx **cborctx); | ||
|
|
||
| /** | ||
| * @brief Free CBOR context. | ||
| * | ||
| * @param[in] cborctx CBOR context to free. | ||
| */ | ||
| void lycbor_ctx_free(struct lycbor_ctx *cborctx); | ||
|
|
||
| #endif /* LY_CBOR_H_ */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the TODO.