4242 */
4343static const char b64_etable [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ;
4444
45+ static LY_ERR lyplg_type_validate_binary (const struct ly_ctx * UNUSED (ctx ), const struct lysc_type * type , const struct lyd_node * UNUSED (ctx_node ), const struct lyd_node * UNUSED (tree ), struct lyd_value * storage , struct ly_err_item * * err );
46+
4547/**
4648 * @brief Encode binary value into a base64 string value.
4749 *
@@ -258,7 +260,6 @@ lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type,
258260 struct ly_err_item * * err )
259261{
260262 LY_ERR ret = LY_SUCCESS ;
261- struct lysc_type_bin * type_bin = (struct lysc_type_bin * )type ;
262263 struct lyd_value_binary * val ;
263264
264265 /* init storage */
@@ -320,11 +321,9 @@ lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type,
320321 }
321322
322323 if (!(options & LYPLG_TYPE_STORE_ONLY )) {
323- /* validate length restriction of the binary value */
324- if (type_bin -> length ) {
325- ret = lyplg_type_validate_range (LY_TYPE_BINARY , type_bin -> length , val -> size , value , value_len , err );
326- LY_CHECK_GOTO (ret , cleanup );
327- }
324+ /* validate value */
325+ ret = lyplg_type_validate_binary (ctx , type , NULL , NULL , storage , err );
326+ LY_CHECK_GOTO (ret , cleanup );
328327 }
329328
330329cleanup :
@@ -338,6 +337,33 @@ lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type,
338337 return ret ;
339338}
340339
340+ /**
341+ * @brief Implementation of ::lyplg_type_validate_clb for the binary type.
342+ */
343+ static LY_ERR
344+ lyplg_type_validate_binary (const struct ly_ctx * ctx , const struct lysc_type * type , const struct lyd_node * UNUSED (ctx_node ),
345+ const struct lyd_node * UNUSED (tree ), struct lyd_value * storage , struct ly_err_item * * err )
346+ {
347+ struct lysc_type_bin * type_bin = (struct lysc_type_bin * )type ;
348+ struct lyd_value_binary * val ;
349+ const void * value ;
350+ size_t value_len ;
351+
352+ LY_CHECK_ARG_RET (NULL , type , storage , err , LY_EINVAL );
353+
354+ val = LYPLG_TYPE_VAL_IS_DYN (val ) ? (struct lyd_value_binary * )(storage -> dyn_mem ) : (struct lyd_value_binary * )(storage -> fixed_mem );
355+ value = lyd_value_get_canonical (ctx , storage );
356+ value_len = strlen (value );
357+ * err = NULL ;
358+
359+ /* length restriction of the binary value */
360+ if (type_bin -> length ) {
361+ LY_CHECK_RET (lyplg_type_validate_range (LY_TYPE_BINARY , type_bin -> length , val -> size , value , value_len , err ));
362+ }
363+
364+ return LY_SUCCESS ;
365+ }
366+
341367LIBYANG_API_DEF LY_ERR
342368lyplg_type_compare_binary (const struct ly_ctx * UNUSED (ctx ), const struct lyd_value * val1 , const struct lyd_value * val2 )
343369{
@@ -473,7 +499,7 @@ const struct lyplg_type_record plugins_binary[] = {
473499
474500 .plugin .id = "libyang 2 - binary, version 1" ,
475501 .plugin .store = lyplg_type_store_binary ,
476- .plugin .validate = NULL ,
502+ .plugin .validate = lyplg_type_validate_binary ,
477503 .plugin .compare = lyplg_type_compare_binary ,
478504 .plugin .sort = lyplg_type_sort_binary ,
479505 .plugin .print = lyplg_type_print_binary ,
0 commit comments