Skip to content

Commit 2498924

Browse files
committed
reformat: restructured the code to match the current format
- arranged all the high level parser code in parser_cbor.c - added all the common high level ctx variables to parser_internal.h - added lcbor.c and lcbor.h as a wrapper over libcbor but to keep the coding style consistent
1 parent de5666b commit 2498924

File tree

7 files changed

+887
-874
lines changed

7 files changed

+887
-874
lines changed

CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,10 +327,10 @@ if(ENABLE_CBOR_SUPPORT)
327327
add_definitions(-DENABLE_CBOR_SUPPORT)
328328
include_directories(${LIBCBOR_INCLUDE_DIRS})
329329
# Add CBOR parser files to the library sources
330-
list(APPEND libsrc src/parser_cbor.c)
331-
list(APPEND headers src/parser_cbor.h)
330+
list(APPEND libsrc src/parser_cbor.c src/lcbor.c)
331+
list(APPEND headers src/lcbor.h)
332332
# Add CBOR files to format sources
333-
list(APPEND format_sources src/parser_cbor.c src/parser_cbor.h)
333+
list(APPEND format_sources src/parser_cbor.c src/lcbor.h src/lcbor.c)
334334
else()
335335
message(FATAL_ERROR "libcbor not found! Please install libcbor development package or disable CBOR support with -DENABLE_CBOR_SUPPORT=OFF")
336336
endif()
@@ -344,10 +344,10 @@ if(ENABLE_CBOR_SUPPORT)
344344
include_directories(${LIBCBOR_INCLUDE_DIR})
345345
set(LIBCBOR_LIBRARIES ${LIBCBOR_LIBRARY})
346346
# Add CBOR parser files to the library sources
347-
list(APPEND libsrc src/parser_cbor.c)
348-
list(APPEND headers src/parser_cbor.h)
347+
list(APPEND libsrc src/parser_cbor.c src/lcbor.c)
348+
list(APPEND headers src/lcbor.h)
349349
# Add CBOR files to format sources
350-
list(APPEND format_sources src/parser_cbor.c src/parser_cbor.h)
350+
list(APPEND format_sources src/parser_cbor.c src/lcbor.h src/lcbor.c)
351351
else()
352352
message(FATAL_ERROR "libcbor not found! Please install libcbor development package or disable CBOR support with -DENABLE_CBOR_SUPPORT=OFF")
353353
endif()

src/lcbor.c

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/**
2+
* @file lcbor.h
3+
* @author MeherRushi <[email protected]>
4+
* @brief CBOR data parser for libyang (abstraction over libcbor)
5+
*
6+
* Copyright (c) 2020 - 2023 CESNET, z.s.p.o.
7+
*
8+
* This source code is licensed under BSD 3-Clause License (the "License").
9+
* You may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* https://opensource.org/licenses/BSD-3-Clause
13+
*/
14+
15+
#ifdef ENABLE_CBOR_SUPPORT
16+
17+
#include <assert.h>
18+
#include <ctype.h>
19+
#include <errno.h>
20+
21+
#include "in_internal.h"
22+
#include "lcbor.h"
23+
#include "log.h"
24+
#include "ly_common.h"
25+
26+
/**
27+
* @brief Free CBOR context.
28+
*
29+
* @param[in] cbor_ctx CBOR context to free.
30+
*/
31+
void lycbor_ctx_free(struct lycbor_ctx *cbor_ctx)
32+
{
33+
if (cbor_ctx)
34+
{
35+
free(cbor_ctx);
36+
}
37+
}
38+
39+
/**
40+
* @brief Detect CBOR format variant from input data.
41+
*
42+
* @param[in] in Input structure to analyze.
43+
* @param[out] format Detected format.
44+
* @return LY_ERR value.
45+
*/
46+
static LY_ERR
47+
lydcbor_detect_format(struct ly_in *in, enum lyd_cbor_format *format)
48+
{
49+
/* Simple heuristic: try to parse as CBOR and examine structure */
50+
/* For now, default to named format */
51+
(void)in;
52+
*format = LYD_CBOR_NAMED;
53+
return LY_SUCCESS;
54+
}
55+
56+
/**
57+
* @brief Create new CBOR context for parsing.
58+
*
59+
* @param[in] ctx libyang context.
60+
* @param[in] in Input handler.
61+
* @param[out] cbor_ctx_p Pointer to store the created CBOR context.
62+
* @return LY_ERR value.
63+
*/
64+
LY_ERR
65+
lycbor_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lycbor_ctx **cbor_ctx_p)
66+
{
67+
/* TODO : Need to restructure error handling here */
68+
LY_ERR ret = LY_SUCCESS;
69+
struct lycbor_ctx *cbor_ctx;
70+
enum lyd_cbor_format format;
71+
72+
assert(ctx && in && cbor_ctx_p);
73+
74+
/* TODO : error handling after the detect_format function call */
75+
ret = lydcbor_detect_format(in, &format);
76+
77+
/* Allocate and initialize CBOR context */
78+
cbor_ctx = calloc(1, sizeof *cbor_ctx);
79+
LY_CHECK_ERR_RET(!cbor_ctx, LOGMEM(ctx), LY_EMEM);
80+
81+
cbor_ctx->ctx = ctx;
82+
cbor_ctx->in = in;
83+
cbor_ctx->format = format;
84+
85+
*cbor_ctx_p = cbor_ctx;
86+
return ret;
87+
}
88+
89+
#endif /* ENABLE_CBOR_SUPPORT */

src/lcbor.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @file lcbor.h
3+
* @author MeherRushi <[email protected]>
4+
* @brief CBOR data parser routines for libyang (abstraction over libcbor)
5+
*
6+
* Copyright (c) 2020 - 2023 CESNET, z.s.p.o.
7+
*
8+
* This source code is licensed under BSD 3-Clause License (the "License").
9+
* You may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* https://opensource.org/licenses/BSD-3-Clause
13+
*/
14+
15+
#ifndef LY_LCBOR_H_
16+
#define LY_LCBOR_H_
17+
18+
#ifdef ENABLE_CBOR_SUPPORT
19+
20+
#include <stddef.h>
21+
#include <stdint.h>
22+
/* using libcbor as the low-level parser */
23+
#include <cbor.h>
24+
25+
26+
#include "log.h"
27+
#include "set.h"
28+
29+
struct ly_ctx;
30+
struct ly_in;
31+
32+
/**
33+
* @brief CBOR format variants for different encoding schemes
34+
*/
35+
enum lyd_cbor_format
36+
{
37+
LYD_CBOR_NAMED, /**< CBOR with named identifiers (JSON-like) */
38+
LYD_CBOR_SID /**< CBOR with Schema Item identifiers (future implementation) */
39+
};
40+
41+
struct lycbor_ctx {
42+
const struct ly_ctx *ctx; /**< libyang context */
43+
struct ly_in *in; /**< input structure */
44+
cbor_item_t *cbor_data; /**< parsed CBOR data */
45+
enum lyd_cbor_format format; /**< CBOR format variant */
46+
uint32_t parse_opts; /**< parser options */
47+
uint32_t val_opts; /**< validation options */
48+
};
49+
50+
/**
51+
* @brief Create new CBOR context for parsing.
52+
*
53+
* @param[in] ctx libyang context.
54+
* @param[in] in Input handler.
55+
* @param[out] cbor_ctx_p Pointer to store the created CBOR context.
56+
* @return LY_ERR value.
57+
*/
58+
LY_ERR
59+
lycbor_ctx_new(const struct ly_ctx *ctx, struct ly_in *in, struct lycbor_ctx **cbor_ctx_p);
60+
61+
/**
62+
* @brief Free CBOR context.
63+
*
64+
* @param[in] cbor_ctx CBOR context to free.
65+
*/
66+
void
67+
lycbor_ctx_free(struct lycbor_ctx *cbor_ctx);
68+
69+
#endif /* ENABLE_CBOR_SUPPORT */
70+
71+
#endif /* LY_LCBOR_H_ */

0 commit comments

Comments
 (0)