@@ -1058,15 +1058,6 @@ lydcbor_subtree_r(struct lyd_cbor_ctx *lydctx, struct lyd_node *parent,
10581058
10591059 assert (lydctx && first_p && parsed && cbor_obj );
10601060
1061- /* assuming that the top level structure is always a map
1062- to be modified to include anything else that it can support */
1063-
1064- if (!cbor_isa_map (cbor_obj ))
1065- {
1066- LOGVAL (lydctx -> cborctx -> ctx , LYVE_SYNTAX , "Expected CBOR map" );
1067- return LY_EVALID ;
1068- }
1069-
10701061 size_t map_size = cbor_map_size (cbor_obj );
10711062 struct cbor_pair * pairs = cbor_map_handle (cbor_obj );
10721063
@@ -1159,28 +1150,37 @@ lydcbor_ctx_init(const struct ly_ctx *ctx, struct ly_in *in, uint32_t parse_opts
11591150 uint32_t val_opts , struct lyd_cbor_ctx * * lydctx_p )
11601151{
11611152 LY_ERR ret = LY_SUCCESS ;
1162- struct lyd_cbor_ctx * lydctx = NULL ;
1153+ struct lyd_cbor_ctx * lydctx ;
1154+ enum cbor_type cbortype ;
11631155
11641156 assert (lydctx_p );
11651157
1166- /* Initialize context with calloc to ensure all fields are zero */
1158+ /* init context */
11671159 lydctx = calloc (1 , sizeof * lydctx );
11681160 LY_CHECK_ERR_RET (!lydctx , LOGMEM (ctx ), LY_EMEM );
11691161 lydctx -> parse_opts = parse_opts ;
11701162 lydctx -> val_opts = val_opts ;
11711163 lydctx -> free = lyd_cbor_ctx_free ;
11721164
1173- /* Create low-level CBOR context */
1174- LY_CHECK_GOTO (ret = lycbor_ctx_new (ctx , in , & lydctx -> cborctx ), cleanup );
1175-
1176- * lydctx_p = lydctx ;
1177- return ret ;
1165+ /* Create low-level CBOR context (includes CBOR parsing) */
1166+ LY_CHECK_ERR_RET (ret = lycbor_ctx_new (ctx , in , & lydctx -> cborctx ), free (lydctx ), ret );
1167+ cbortype = cbor_typeof (lydctx -> cborctx -> cbor_data );
11781168
1179- cleanup :
1180- if (lydctx )
1169+ /* assuming that the top level structure is always a map
1170+ - though this is not mentioned explicitly in RFC9254 - it is implied
1171+ and it is almost always the case - This is a similar assumption made
1172+ to the RFC 7951 where JSON Encoding of data modeled by YANG is always assumed
1173+ to a have a top-level structure as an object */
1174+ if (!cbor_isa_map (lydctx -> cborctx -> cbor_data ))
11811175 {
1176+ /* expecting top-level map */
1177+ LOGVAL (ctx , LYVE_SYNTAX_CBOR , "Expected top-level CBOR map, but %s found." , lycbor_token2str (cbortype ));
1178+ * lydctx_p = NULL ;
11821179 lyd_cbor_ctx_free ((struct lyd_ctx * )lydctx );
1180+ return LY_EVALID ;
11831181 }
1182+
1183+ * lydctx_p = lydctx ;
11841184 return ret ;
11851185}
11861186
@@ -1189,72 +1189,43 @@ lyd_parse_cbor(const struct ly_ctx *ctx, const struct lysc_ext_instance *ext, st
11891189 struct lyd_node * * first_p , struct ly_in * in , uint32_t parse_opts , uint32_t val_opts , uint32_t int_opts ,
11901190 struct ly_set * parsed , ly_bool * subtree_sibling , struct lyd_ctx * * lydctx_p )
11911191{
1192- LY_ERR ret = LY_SUCCESS ;
1192+ LY_ERR r , rc = LY_SUCCESS ;
11931193 struct lyd_cbor_ctx * lydctx = NULL ;
1194- cbor_item_t * cbor_data = NULL ;
1195- struct cbor_load_result result = {0 };
1194+ printf ("Entering lyd_parse_cbor\n AHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH\n" );
11961195
1197- /* Initialize context */
1198- LY_CHECK_GOTO (ret = lydcbor_ctx_init (ctx , in , parse_opts , val_opts , & lydctx ), cleanup );
1196+ /* Initialize context (CBOR parsing happens in lycbor_ctx_new) */
1197+ rc = lydcbor_ctx_init (ctx , in , parse_opts , val_opts , & lydctx );
1198+ LY_CHECK_GOTO (rc , cleanup );
11991199
12001200 lydctx -> int_opts = int_opts ;
12011201 lydctx -> ext = ext ;
12021202
12031203 /* find the operation node if it exists already */
1204- LY_CHECK_GOTO (ret = lyd_parser_find_operation (parent , int_opts , & lydctx -> op_node ), cleanup );
1205-
1206-
1207- /*
1208- * Loads CBOR data from the current input buffer.
1209- *
1210- * Parameters:
1211- * in->current - Pointer to the current position in the input buffer.
1212- * in->length - Length of the data to be loaded.
1213- * &result - Pointer to a variable where the result status will be stored.
1214- *
1215- * Returns:
1216- * cbor_data - Pointer to the loaded CBOR data structure, or NULL on failure.
1217- */
1218- /* need to convert in->current from const char* to cbor_data type */
1219- cbor_data = cbor_load (in -> current , in -> length , & result );
1220- lydctx -> cborctx -> cbor_data = cbor_data ;
1221-
1222- if (!cbor_data )
1223- {
1224- LOGVAL (ctx , LYVE_SYNTAX , "Failed to parse CBOR data: no data returned from cbor_load()." );
1225- ret = LY_EVALID ;
1226- goto cleanup ;
1227- }
1228- if (result .error .code != CBOR_ERR_NONE )
1229- {
1230- LOGVAL (ctx , LYVE_SYNTAX , "Failed to parse CBOR data: parsing error (code %d)." , result .error .code );
1231- ret = LY_EVALID ;
1232- goto cleanup ;
1233- }
1204+ LY_CHECK_GOTO (rc = lyd_parser_find_operation (parent , int_opts , & lydctx -> op_node ), cleanup );
12341205
1235- /* Probably need to check if the obtained data is a operational node and
1236- then write functions to parse them accordingly. If not then continue below */
1206+ /* Parse the CBOR structure - read subtrees */
1207+ r = lydcbor_subtree_r (lydctx , parent , first_p , parsed , lydctx -> cborctx -> cbor_data );
1208+ LY_DPARSER_ERR_GOTO (r , rc = r , lydctx , cleanup );
12371209
1238- /* Parse the CBOR structure */
1239- ret = lydcbor_subtree_r (lydctx , parent , first_p , parsed , cbor_data );
1210+ /* Unexpected sibling node error handling */
12401211
1241- cleanup :
1242- if (cbor_data )
1243- {
1244- cbor_decref (& cbor_data );
1212+ /* Validate operation node presence */
1213+ if ((int_opts & (LYD_INTOPT_RPC | LYD_INTOPT_ACTION | LYD_INTOPT_NOTIF | LYD_INTOPT_REPLY )) &&
1214+ !lydctx -> op_node ) {
1215+ LOGVAL (ctx , LYVE_DATA , "Missing the operation node." );
1216+ r = LY_EVALID ;
1217+ LY_DPARSER_ERR_GOTO (r , rc = r , lydctx , cleanup );
12451218 }
1219+ /* also need to deal with metadata linking etc*/
12461220
1247- if (ret )
1248- {
1249- if (lydctx )
1250- {
1251- lyd_cbor_ctx_free ((struct lyd_ctx * )lydctx );
1252- lydctx = NULL ;
1253- }
1221+ cleanup :
1222+ if (rc && (!lydctx || !(lydctx -> val_opts & LYD_VALIDATE_MULTI_ERROR ) || (rc != LY_EVALID ))) {
1223+ lyd_cbor_ctx_free ((struct lyd_ctx * )lydctx );
1224+ lydctx = NULL ;
12541225 }
12551226
12561227 * lydctx_p = (struct lyd_ctx * )lydctx ;
1257- return ret ;
1228+ return rc ;
12581229}
12591230
12601231#endif /* ENABLE_CBOR_SUPPORT */
0 commit comments